2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
16 #include "mutt_curses.h"
27 #include "mutt_crypt.h"
42 #include <sys/types.h>
45 BODY *mutt_new_body (void)
47 BODY *p = (BODY *) safe_calloc (1, sizeof (BODY));
49 p->disposition = DISPATTACH;
55 /* Modified by blong to accept a "suggestion" for file name. If
56 * that file exists, then construct one with unique name but
57 * keep any extension. This might fail, I guess.
58 * Renamed to mutt_adv_mktemp so I only have to change where it's
59 * called, and not all possible cases.
61 void mutt_adv_mktemp (char *s, size_t l)
63 char buf[_POSIX_PATH_MAX];
64 char tmp[_POSIX_PATH_MAX];
69 strfcpy (buf, NONULL (Tempdir), sizeof (buf));
70 mutt_expand_path (buf, sizeof (buf));
72 snprintf (s, l, "%s/muttXXXXXX", buf);
76 strfcpy (tmp, s, sizeof (tmp));
77 mutt_sanitize_filename (tmp, 1);
78 snprintf (s, l, "%s/%s", buf, tmp);
79 if (lstat (s, &sb) == -1 && errno == ENOENT)
81 if ((period = strrchr (tmp, '.')) != NULL)
83 snprintf (s, l, "%s/%s.XXXXXX", buf, tmp);
88 strfcpy (s + sl, period, l - sl);
93 /* create a send-mode duplicate from a receive-mode body */
95 int mutt_copy_body (FILE * fp, BODY ** tgt, BODY * src)
97 char tmp[_POSIX_PATH_MAX];
100 PARAMETER *par, **ppar;
106 strfcpy (tmp, src->filename, sizeof (tmp));
113 mutt_adv_mktemp (tmp, sizeof (tmp));
114 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
117 *tgt = mutt_new_body ();
120 memcpy (b, src, sizeof (BODY));
124 b->filename = safe_strdup (tmp);
125 b->use_disp = use_disp;
128 if (mutt_is_text_part (b))
131 b->xtype = safe_strdup (b->xtype);
132 b->subtype = safe_strdup (b->subtype);
133 b->form_name = safe_strdup (b->form_name);
134 b->filename = safe_strdup (b->filename);
135 b->d_filename = safe_strdup (b->d_filename);
136 b->description = safe_strdup (b->description);
139 * we don't seem to need the HEADER structure currently.
140 * XXX - this may change in the future
146 /* copy parameters */
147 for (par = b->parameter, ppar = &b->parameter; par;
148 ppar = &(*ppar)->next, par = par->next) {
149 *ppar = mutt_new_parameter ();
150 (*ppar)->attribute = safe_strdup (par->attribute);
151 (*ppar)->value = safe_strdup (par->value);
154 mutt_stamp_attachment (b);
161 void mutt_free_body (BODY ** p)
170 mutt_free_parameter (&b->parameter);
171 if (b->unlink && b->filename) {
172 dprint (1, (debugfile, "mutt_free_body: Unlinking %s.\n", b->filename));
173 unlink (b->filename);
175 else if (b->filename)
177 (debugfile, "mutt_free_body: Not unlinking %s.\n",
184 FREE (&b->description);
185 FREE (&b->form_name);
188 /* Don't free twice (b->hdr->content = b->parts) */
189 b->hdr->content = NULL;
190 mutt_free_header (&b->hdr);
194 mutt_free_body (&b->parts);
202 void mutt_free_parameter (PARAMETER ** p)
208 FREE (&t->attribute);
217 LIST *mutt_add_list (LIST * head, const char *data)
221 for (tmp = head; tmp && tmp->next; tmp = tmp->next);
223 tmp->next = safe_malloc (sizeof (LIST));
227 head = tmp = safe_malloc (sizeof (LIST));
229 tmp->data = safe_strdup (data);
234 void mutt_free_list (LIST ** list)
242 *list = (*list)->next;
248 HEADER *mutt_dup_header (HEADER * h)
252 hnew = mutt_new_header ();
253 memcpy (hnew, h, sizeof (HEADER));
257 void mutt_free_header (HEADER ** h)
261 mutt_free_envelope (&(*h)->env);
262 mutt_free_body (&(*h)->content);
263 FREE (&(*h)->maildir_flags);
267 mutt_free_list (&(*h)->chain);
269 #if defined USE_POP || defined USE_IMAP || defined USE_NNTP
275 /* returns true if the header contained in "s" is in list "t" */
276 int mutt_matches_ignore (const char *s, LIST * t)
278 for (; t; t = t->next) {
279 if (!ascii_strncasecmp (s, t->data, safe_strlen (t->data))
286 /* prepend the path part of *path to *link */
287 void mutt_expand_link (char *newpath, const char *path, const char *link)
289 const char *lb = NULL;
292 /* link is full path */
294 strfcpy (newpath, link, _POSIX_PATH_MAX);
298 if ((lb = strrchr (path, '/')) == NULL) {
299 /* no path in link */
300 strfcpy (newpath, link, _POSIX_PATH_MAX);
305 memcpy (newpath, path, len);
306 strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
309 char *mutt_expand_path (char *s, size_t slen)
311 return _mutt_expand_path (s, slen, 0);
314 char *_mutt_expand_path (char *s, size_t slen, int rx)
316 char p[_POSIX_PATH_MAX] = "";
317 char q[_POSIX_PATH_MAX] = "";
318 char tmp[_POSIX_PATH_MAX];
331 if (*(s + 1) == '/' || *(s + 1) == 0) {
332 strfcpy (p, NONULL (Homedir), sizeof (p));
338 if ((t = strchr (s + 1, '/')))
341 if ((pw = getpwnam (s + 1))) {
342 strfcpy (p, pw->pw_dir, sizeof (p));
351 /* user not found! */
365 /* if folder = {host} or imap[s]://host/: don't append slash */
366 if (mx_get_magic (NONULL (Maildir)) == M_IMAP &&
367 (Maildir[safe_strlen (Maildir) - 1] == '}' ||
368 Maildir[safe_strlen (Maildir) - 1] == '/'))
369 strfcpy (p, NONULL (Maildir), sizeof (p));
372 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
378 /* elm compatibility, @ expands alias to user name */
385 if ((alias = mutt_lookup_alias (s + 1))) {
386 h = mutt_new_header ();
387 h->env = mutt_new_envelope ();
388 h->env->from = h->env->to = alias;
389 mutt_default_save (p, sizeof (p), h);
390 h->env->from = h->env->to = NULL;
391 mutt_free_header (&h);
392 /* Avoid infinite recursion if the resulting folder starts with '@' */
403 strfcpy (p, NONULL (Inbox), sizeof (p));
410 strfcpy (p, NONULL (Outbox), sizeof (p));
417 if (*(s + 1) == '!') {
418 strfcpy (p, NONULL (LastFolder), sizeof (p));
422 strfcpy (p, NONULL (Spoolfile), sizeof (p));
430 strfcpy (p, NONULL (LastFolder), sizeof (p));
437 strfcpy (p, NONULL (CurrentFolder), sizeof (p));
449 if (rx && *p && !recurse) {
450 mutt_rx_sanitize_string (q, sizeof (q), p);
451 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
454 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
456 strfcpy (s, tmp, slen);
461 /* Rewrite IMAP path in canonical form - aids in string comparisons of
462 * folders. May possibly fail, in which case s should be the same. */
463 if (mx_get_magic (s) == M_IMAP)
464 imap_expand_path (s, slen);
470 /* Extract the real name from /etc/passwd's GECOS field.
471 * When set, honor the regular expression in GecosMask,
472 * otherwise assume that the GECOS field is a
473 * comma-separated list.
474 * Replace "&" by a capitalized version of the user's login
478 char *mutt_gecos_name (char *dest, size_t destlen, struct passwd *pw)
480 regmatch_t pat_match[1];
485 if (!pw || !pw->pw_gecos)
488 memset (dest, 0, destlen);
491 if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0)
492 strfcpy (dest, pw->pw_gecos + pat_match[0].rm_so,
493 MIN (pat_match[0].rm_eo - pat_match[0].rm_so + 1, destlen));
495 else if ((p = strchr (pw->pw_gecos, ',')))
496 strfcpy (dest, pw->pw_gecos, MIN (destlen, p - pw->pw_gecos + 1));
498 strfcpy (dest, pw->pw_gecos, destlen);
500 pwnl = safe_strlen (pw->pw_name);
502 for (idx = 0; dest[idx]; idx++) {
503 if (dest[idx] == '&') {
504 memmove (&dest[idx + pwnl], &dest[idx + 1],
505 MAX (destlen - idx - pwnl - 1, 0));
506 memcpy (&dest[idx], pw->pw_name, MIN (destlen - idx - 1, pwnl));
507 dest[idx] = toupper ((unsigned char) dest[idx]);
515 char *mutt_get_parameter (const char *s, PARAMETER * p)
517 for (; p; p = p->next)
518 if (ascii_strcasecmp (s, p->attribute) == 0)
524 void mutt_set_parameter (const char *attribute, const char *value,
530 mutt_delete_parameter (attribute, p);
534 for (q = *p; q; q = q->next) {
535 if (ascii_strcasecmp (attribute, q->attribute) == 0) {
536 str_replace (&q->value, value);
541 q = mutt_new_parameter ();
542 q->attribute = safe_strdup (attribute);
543 q->value = safe_strdup (value);
548 void mutt_delete_parameter (const char *attribute, PARAMETER ** p)
552 for (q = *p; q; p = &q->next, q = q->next) {
553 if (ascii_strcasecmp (attribute, q->attribute) == 0) {
556 mutt_free_parameter (&q);
562 /* returns 1 if Mutt can't display this type of data, 0 otherwise */
563 int mutt_needs_mailcap (BODY * m)
568 if (!ascii_strcasecmp ("plain", m->subtype) ||
569 !ascii_strcasecmp ("rfc822-headers", m->subtype) ||
570 !ascii_strcasecmp ("enriched", m->subtype))
574 case TYPEAPPLICATION:
575 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (m))
577 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (m))
589 int mutt_is_text_part (BODY * b)
592 char *s = b->subtype;
594 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
600 if (t == TYPEMESSAGE) {
601 if (!ascii_strcasecmp ("delivery-status", s))
605 if ((WithCrypto & APPLICATION_PGP) && t == TYPEAPPLICATION) {
606 if (!ascii_strcasecmp ("pgp-keys", s))
613 void mutt_free_envelope (ENVELOPE ** p)
617 rfc822_free_address (&(*p)->return_path);
618 rfc822_free_address (&(*p)->from);
619 rfc822_free_address (&(*p)->to);
620 rfc822_free_address (&(*p)->cc);
621 rfc822_free_address (&(*p)->bcc);
622 rfc822_free_address (&(*p)->sender);
623 rfc822_free_address (&(*p)->reply_to);
624 rfc822_free_address (&(*p)->mail_followup_to);
626 FREE (&(*p)->list_post);
627 FREE (&(*p)->subject);
628 /* real_subj is just an offset to subject and shouldn't be freed */
629 FREE (&(*p)->message_id);
630 FREE (&(*p)->supersedes);
632 FREE (&(*p)->x_label);
633 FREE (&(*p)->organization);
635 FREE (&(*p)->newsgroups);
637 FREE (&(*p)->followup_to);
638 FREE (&(*p)->x_comment_to);
641 mutt_buffer_free (&(*p)->spam);
642 mutt_free_list (&(*p)->references);
643 mutt_free_list (&(*p)->in_reply_to);
644 mutt_free_list (&(*p)->userhdrs);
648 void _mutt_mktemp (char *s, const char *src, int line)
650 snprintf (s, _POSIX_PATH_MAX, "%s/muttng-%s-%d-%d-%d", NONULL (Tempdir),
651 NONULL (Hostname), (int) getuid (), (int) getpid (), Counter++);
653 (debugfile, "%s:%d: mutt_mktemp returns \"%s\".\n", src, line, s));
657 void mutt_free_alias (ALIAS ** p)
665 rfc822_free_address (&t->addr);
670 /* collapse the pathname using ~ or = when possible */
671 void mutt_pretty_mailbox (char *s)
677 scheme = url_check_scheme (s);
680 if (scheme == U_IMAP || scheme == U_IMAPS) {
681 imap_pretty_mailbox (s);
686 /* if s is an url, only collapse path component */
687 if (scheme != U_UNKNOWN) {
688 p = strchr (s, ':') + 1;
689 if (!strncmp (p, "//", 2))
690 q = strchr (p + 2, '/');
692 q = strchr (p, '\0');
696 /* first attempt to collapse the pathname */
698 if (*p == '/' && p[1] == '/') {
702 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
711 if (safe_strncmp (s, Maildir, (len = safe_strlen (Maildir))) == 0 &&
714 memmove (s, s + len, safe_strlen (s + len) + 1);
716 else if (safe_strncmp (s, Homedir, (len = safe_strlen (Homedir))) == 0 &&
719 memmove (s, s + len - 1, safe_strlen (s + len - 1) + 1);
723 void mutt_pretty_size (char *s, size_t len, long n)
726 strfcpy (s, "0K", len);
727 else if (n < 10189) /* 0.1K - 9.9K */
728 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
729 else if (n < 1023949) { /* 10K - 999K */
730 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
731 snprintf (s, len, "%ldK", (n + 51) / 1024);
733 else if (n < 10433332) /* 1.0M - 9.9M */
734 snprintf (s, len, "%3.1fM", n / 1048576.0);
737 /* (10433332 + 52428) / 1048576 = 10 */
738 snprintf (s, len, "%ldM", (n + 52428) / 1048576);
742 void mutt_expand_file_fmt (char *dest, size_t destlen, const char *fmt,
745 char tmp[LONG_STRING];
747 mutt_quote_filename (tmp, sizeof (tmp), src);
748 mutt_expand_fmt (dest, destlen, fmt, tmp);
751 void mutt_expand_fmt (char *dest, size_t destlen, const char *fmt,
759 slen = safe_strlen (src);
762 for (p = fmt, d = dest; destlen && *p; p++) {
771 strfcpy (d, src, destlen + 1);
772 d += destlen > slen ? slen : destlen;
773 destlen -= destlen > slen ? slen : destlen;
790 if (!found && destlen > 0) {
791 safe_strcat (dest, destlen, " ");
792 safe_strcat (dest, destlen, src);
797 /* return 0 on success, -1 on abort, 1 on error */
798 int mutt_check_overwrite (const char *attname, const char *path,
799 char *fname, size_t flen, int *append,
803 char tmp[_POSIX_PATH_MAX];
806 strfcpy (fname, path, flen);
807 if (access (fname, F_OK) != 0)
809 if (stat (fname, &st) != 0)
811 if (S_ISDIR (st.st_mode)) {
813 switch (mutt_multi_choice
814 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
817 str_replace (directory, fname);
832 mutt_yesorno (_("File is a directory, save under it?"),
834 return (rc == M_NO) ? 1 : -1;
836 if (!attname || !attname[0]) {
838 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
839 M_FILE | M_CLEAR) != 0 || !tmp[0])
841 mutt_concat_path (fname, path, tmp, flen);
844 mutt_concat_path (fname, path, mutt_basename (attname), flen);
847 if (*append == 0 && access (fname, F_OK) == 0) {
848 switch (mutt_multi_choice
849 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
857 *append = M_SAVE_APPEND;
859 case 1: /* overwrite */
860 *append = M_SAVE_OVERWRITE;
867 void mutt_save_path (char *d, size_t dsize, ADDRESS * a)
869 if (a && a->mailbox) {
870 strfcpy (d, a->mailbox, dsize);
871 if (!option (OPTSAVEADDRESS)) {
874 if ((p = strpbrk (d, "%@")))
883 void mutt_safe_path (char *s, size_t l, ADDRESS * a)
887 mutt_save_path (s, l, a);
889 if (*p == '/' || ISSPACE (*p) || !IsPrint ((unsigned char) *p))
893 /* counts how many characters in s can be skipped while none of the
894 * characters of c appears */
895 int mutt_skipchars (const char *s, const char *c)
901 register const char *t = c;
911 return (safe_strlen (p));
914 void mutt_FormatString (char *dest, /* output buffer */
915 size_t destlen, /* output buffer len */
916 const char *src, /* template string */
917 format_t * callback, /* callback for processing */
918 unsigned long data, /* callback data */
920 { /* callback flags */
921 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
922 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
923 size_t wlen, count, len, col, wid;
926 destlen--; /* save room for the terminal \0 */
927 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
930 while (*src && wlen < destlen) {
941 flags |= M_FORMAT_OPTIONAL;
945 flags &= ~M_FORMAT_OPTIONAL;
947 /* eat the format string */
950 while (count < sizeof (prefix) &&
951 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
960 break; /* bad format */
962 ch = *src++; /* save the character to switch on */
964 if (flags & M_FORMAT_OPTIONAL) {
966 break; /* bad format */
969 /* eat the `if' part of the string */
972 while (count < sizeof (ifstring) && *src && *src != '?'
979 /* eat the `else' part of the string (optional) */
981 src++; /* skip the & */
984 while (count < sizeof (elsestring) && *src && *src != '?') {
991 break; /* bad format */
993 src++; /* move past the trailing `?' */
996 /* handle generic cases first */
998 /* right justify to EOL */
999 ch = *src++; /* pad char */
1000 /* calculate space left on line. if we've already written more data
1001 than will fit on the line, ignore the rest of the line */
1002 if (DrawFullLine || option (OPTSTATUSONTOP))
1003 count = (COLS < destlen ? COLS : destlen);
1006 ((COLS - SidebarWidth) <
1007 destlen ? (COLS - SidebarWidth) : destlen);
1009 count -= col; /* how many columns left on this line */
1010 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
1011 wid = safe_strlen (buf);
1013 count -= wid; /* how many chars to pad */
1014 memset (wptr, ch, count);
1018 if (wid + wlen > destlen)
1019 len = destlen - wlen;
1022 memcpy (wptr, buf, len);
1025 col += mutt_strwidth (buf);
1027 break; /* skip rest of input */
1029 else if (ch == '|') {
1034 if (destlen > wlen) {
1035 count = destlen - wlen;
1036 memset (wptr, ch, count);
1039 break; /* skip rest of input */
1045 while (ch == '_' || ch == ':') {
1054 /* use callback function to handle this case */
1056 callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring,
1069 if ((len = safe_strlen (buf)) + wlen > destlen)
1070 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
1072 memcpy (wptr, buf, len);
1075 col += mutt_strwidth (buf);
1078 else if (*src == '\\') {
1107 unsigned int bar = mutt_skipchars (src, "%\\");
1108 char *bar2 = safe_malloc (bar + 1);
1110 strfcpy (bar2, src, bar + 1);
1115 col += mutt_strwidth (bar2);
1122 if (flags & M_FORMAT_MAKEPRINT) {
1123 /* Make sure that the string is printable by changing all non-printable
1124 chars to dots, or spaces for non-printable whitespace */
1125 for (cp = dest; *cp; cp++)
1126 if (!IsPrint (*cp) && !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
1127 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
1132 /* This function allows the user to specify a command to read stdout from in
1133 place of a normal file. If the last character in the string is a pipe (|),
1134 then we assume it is a commmand to run instead of a normal file. */
1135 FILE *mutt_open_read (const char *path, pid_t * thepid)
1140 int len = safe_strlen (path);
1142 if (path[len - 1] == '|') {
1143 /* read from a pipe */
1145 char *s = safe_strdup (path);
1149 *thepid = mutt_create_filter (s, NULL, &f, NULL);
1153 if (stat (path, &s) < 0)
1155 if (S_ISDIR (s.st_mode)) {
1159 f = fopen (path, "r");
1165 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
1166 int mutt_save_confirm (const char *s, struct stat *st)
1168 char tmp[_POSIX_PATH_MAX];
1173 magic = mx_get_magic (s);
1176 if (magic == M_POP) {
1177 mutt_error _("Can't save message to POP mailbox.");
1184 if (magic == M_NNTP) {
1185 mutt_error _("Can't save message to newsserver.");
1191 if (stat (s, st) != -1) {
1193 mutt_error (_("%s is not a mailbox!"), s);
1197 if (option (OPTCONFIRMAPPEND) &&
1198 (!TrashPath || (safe_strcmp (s, TrashPath) != 0)))
1199 /* if we're appending to the trash, there's no point in asking */
1201 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
1202 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1210 if (magic != M_IMAP)
1211 #endif /* execute the block unconditionally if we don't use imap */
1216 if (errno == ENOENT) {
1217 if (option (OPTCONFIRMCREATE)) {
1218 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
1219 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1232 CLEARLINE (LINES - 1);
1236 void state_prefix_putc (char c, STATE * s)
1238 if (s->flags & M_PENDINGPREFIX) {
1241 i = safe_strlen (Quotebuf);
1244 if (i == sizeof (Quotebuf) - 1 || c == '\n') {
1245 char buf[2 * SHORT_STRING];
1246 int j = 0, offset = 0;
1247 regmatch_t pmatch[1];
1249 state_reset_prefix (s);
1251 ((regex_t *) QuoteRegexp.rx, &Quotebuf[offset], 1, pmatch,
1253 offset += pmatch->rm_eo;
1255 if (!option (OPTQUOTEEMPTY) && Quotebuf[offset] == '\n') {
1259 else if (option (OPTQUOTEQUOTED) && offset) {
1260 for (i = 0; i < offset; i++)
1261 if (Quotebuf[i] != ' ')
1263 strncpy (buf, Quotebuf, j + 1);
1264 strcpy (buf + j + 1, Quotebuf + j);
1267 snprintf (buf, sizeof (buf), "%s%s", NONULL (s->prefix), Quotebuf);
1269 state_puts (buf, s);
1276 state_set_prefix (s);
1281 int state_printf (STATE * s, const char *fmt, ...)
1287 rv = vfprintf (s->fpout, fmt, ap);
1293 void state_mark_attach (STATE * s)
1295 if ((s->flags & M_DISPLAY) && !safe_strcmp (Pager, "builtin"))
1296 state_puts (AttachmentMarker, s);
1299 void state_attach_puts (const char *t, STATE * s)
1302 state_mark_attach (s);
1305 if (*t++ == '\n' && *t)
1307 state_mark_attach (s);
1311 void mutt_display_sanitize (char *s)
1319 void mutt_sleep (short s)
1328 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1329 * just initializes. Frees anything already in the buffer.
1331 * Disregards the 'destroy' flag, which seems reserved for caller.
1332 * This is bad, but there's no apparent protocol for it.
1334 BUFFER *mutt_buffer_init (BUFFER * b)
1337 b = safe_malloc (sizeof (BUFFER));
1344 memset (b, 0, sizeof (BUFFER));
1349 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1350 * just initializes. Frees anything already in the buffer. Copies in
1353 * Disregards the 'destroy' flag, which seems reserved for caller.
1354 * This is bad, but there's no apparent protocol for it.
1356 BUFFER *mutt_buffer_from (BUFFER * b, char *seed)
1361 b = mutt_buffer_init (b);
1362 b->data = safe_strdup (seed);
1363 b->dsize = safe_strlen (seed);
1364 b->dptr = (char *) b->data + b->dsize;
1368 void mutt_buffer_addstr (BUFFER * buf, const char *s)
1370 mutt_buffer_add (buf, s, safe_strlen (s));
1373 void mutt_buffer_addch (BUFFER * buf, char c)
1375 mutt_buffer_add (buf, &c, 1);
1378 void mutt_buffer_free (BUFFER ** p)
1384 /* dptr is just an offset to data and shouldn't be freed */
1388 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
1389 * Always one byte bigger than necessary for the null terminator, and
1390 * the buffer is always null-terminated */
1391 void mutt_buffer_add (BUFFER * buf, const char *s, size_t len)
1395 if (buf->dptr + len + 1 > buf->data + buf->dsize) {
1396 offset = buf->dptr - buf->data;
1397 buf->dsize += len < 128 ? 128 : len + 1;
1398 safe_realloc ((void **) &buf->data, buf->dsize);
1399 buf->dptr = buf->data + offset;
1401 memcpy (buf->dptr, s, len);
1403 *(buf->dptr) = '\0';
1406 /* Decrease a file's modification time by 1 second */
1408 time_t mutt_decrease_mtime (const char *f, struct stat *st)
1410 struct utimbuf utim;
1415 if (stat (f, &_st) == -1)
1420 if ((mtime = st->st_mtime) == time (NULL)) {
1422 utim.actime = mtime;
1423 utim.modtime = mtime;
1430 const char *mutt_make_version (void)
1432 static char vstring[STRING];
1434 snprintf (vstring, sizeof (vstring), "Mutt-ng %s (%s)",
1435 MUTT_VERSION, ReleaseDate);
1439 void mutt_free_spam_list (SPAM_LIST ** list)
1447 *list = (*list)->next;
1454 int mutt_match_spam_list (const char *s, SPAM_LIST * l, char *text, int x)
1456 static regmatch_t *pmatch = NULL;
1457 static int nmatch = 0;
1466 for (; l; l = l->next) {
1467 /* If this pattern needs more matches, expand pmatch. */
1468 if (l->nmatch > nmatch) {
1469 safe_realloc (&pmatch, l->nmatch * sizeof (regmatch_t));
1473 /* Does this pattern match? */
1475 (l->rx->rx, s, (size_t) l->nmatch, (regmatch_t *) pmatch,
1478 (debugfile, "mutt_match_spam_list: %s matches %s\n", s,
1481 (debugfile, "mutt_match_spam_list: %d subs\n",
1482 l->rx->rx->re_nsub));
1484 /* Copy template into text, with substitutions. */
1485 for (p = l->template; *p;) {
1487 n = atoi (++p); /* find pmatch index */
1488 while (isdigit (*p))
1489 ++p; /* skip subst token */
1490 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
1491 text[tlen++] = s[i];
1494 text[tlen++] = *p++;
1498 dprint (5, (debugfile, "mutt_match_spam_list: \"%s\"\n", text));