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"
28 #include "mutt_crypt.h"
43 #include <sys/types.h>
46 BODY *mutt_new_body (void)
48 BODY *p = (BODY *) safe_calloc (1, sizeof (BODY));
50 p->disposition = DISPATTACH;
56 /* Modified by blong to accept a "suggestion" for file name. If
57 * that file exists, then construct one with unique name but
58 * keep any extension. This might fail, I guess.
59 * Renamed to mutt_adv_mktemp so I only have to change where it's
60 * called, and not all possible cases.
62 void mutt_adv_mktemp (char *s, size_t l)
64 char buf[_POSIX_PATH_MAX];
65 char tmp[_POSIX_PATH_MAX];
70 strfcpy (buf, NONULL (Tempdir), sizeof (buf));
71 mutt_expand_path (buf, sizeof (buf));
73 snprintf (s, l, "%s/muttXXXXXX", buf);
77 strfcpy (tmp, s, sizeof (tmp));
78 mutt_sanitize_filename (tmp, 1);
79 snprintf (s, l, "%s/%s", buf, tmp);
80 if (lstat (s, &sb) == -1 && errno == ENOENT)
82 if ((period = strrchr (tmp, '.')) != NULL)
84 snprintf (s, l, "%s/%s.XXXXXX", buf, tmp);
89 strfcpy (s + sl, period, l - sl);
94 /* create a send-mode duplicate from a receive-mode body */
96 int mutt_copy_body (FILE * fp, BODY ** tgt, BODY * src)
98 char tmp[_POSIX_PATH_MAX];
101 PARAMETER *par, **ppar;
107 strfcpy (tmp, src->filename, sizeof (tmp));
114 mutt_adv_mktemp (tmp, sizeof (tmp));
115 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
118 *tgt = mutt_new_body ();
121 memcpy (b, src, sizeof (BODY));
125 b->filename = safe_strdup (tmp);
126 b->use_disp = use_disp;
129 if (mutt_is_text_part (b))
132 b->xtype = safe_strdup (b->xtype);
133 b->subtype = safe_strdup (b->subtype);
134 b->form_name = safe_strdup (b->form_name);
135 b->filename = safe_strdup (b->filename);
136 b->d_filename = safe_strdup (b->d_filename);
137 b->description = safe_strdup (b->description);
140 * we don't seem to need the HEADER structure currently.
141 * XXX - this may change in the future
147 /* copy parameters */
148 for (par = b->parameter, ppar = &b->parameter; par;
149 ppar = &(*ppar)->next, par = par->next) {
150 *ppar = mutt_new_parameter ();
151 (*ppar)->attribute = safe_strdup (par->attribute);
152 (*ppar)->value = safe_strdup (par->value);
155 mutt_stamp_attachment (b);
162 void mutt_free_body (BODY ** p)
171 mutt_free_parameter (&b->parameter);
172 if (b->unlink && b->filename) {
173 dprint (1, (debugfile, "mutt_free_body: Unlinking %s.\n", b->filename));
174 unlink (b->filename);
176 else if (b->filename)
178 (debugfile, "mutt_free_body: Not unlinking %s.\n",
185 FREE (&b->description);
186 FREE (&b->form_name);
189 /* Don't free twice (b->hdr->content = b->parts) */
190 b->hdr->content = NULL;
191 mutt_free_header (&b->hdr);
195 mutt_free_body (&b->parts);
203 void mutt_free_parameter (PARAMETER ** p)
209 FREE (&t->attribute);
218 LIST *mutt_add_list (LIST * head, const char *data)
222 for (tmp = head; tmp && tmp->next; tmp = tmp->next);
224 tmp->next = safe_malloc (sizeof (LIST));
228 head = tmp = safe_malloc (sizeof (LIST));
230 tmp->data = safe_strdup (data);
235 void mutt_free_list (LIST ** list)
243 *list = (*list)->next;
249 HEADER *mutt_dup_header (HEADER * h)
253 hnew = mutt_new_header ();
254 memcpy (hnew, h, sizeof (HEADER));
258 void mutt_free_header (HEADER ** h)
262 mutt_free_envelope (&(*h)->env);
263 mutt_free_body (&(*h)->content);
264 FREE (&(*h)->maildir_flags);
268 mutt_free_list (&(*h)->chain);
270 #if defined USE_POP || defined USE_IMAP || defined USE_NNTP
276 /* returns true if the header contained in "s" is in list "t" */
277 int mutt_matches_ignore (const char *s, LIST * t)
279 for (; t; t = t->next) {
280 if (!ascii_strncasecmp (s, t->data, mutt_strlen (t->data))
287 /* prepend the path part of *path to *link */
288 void mutt_expand_link (char *newpath, const char *path, const char *link)
290 const char *lb = NULL;
293 /* link is full path */
295 strfcpy (newpath, link, _POSIX_PATH_MAX);
299 if ((lb = strrchr (path, '/')) == NULL) {
300 /* no path in link */
301 strfcpy (newpath, link, _POSIX_PATH_MAX);
306 memcpy (newpath, path, len);
307 strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
310 char *mutt_expand_path (char *s, size_t slen)
312 return _mutt_expand_path (s, slen, 0);
315 char *_mutt_expand_path (char *s, size_t slen, int rx)
317 char p[_POSIX_PATH_MAX] = "";
318 char q[_POSIX_PATH_MAX] = "";
319 char tmp[_POSIX_PATH_MAX];
332 if (*(s + 1) == '/' || *(s + 1) == 0) {
333 strfcpy (p, NONULL (Homedir), sizeof (p));
339 if ((t = strchr (s + 1, '/')))
342 if ((pw = getpwnam (s + 1))) {
343 strfcpy (p, pw->pw_dir, sizeof (p));
352 /* user not found! */
366 /* if folder = {host} or imap[s]://host/: don't append slash */
367 if (mx_is_imap (NONULL (Maildir)) &&
368 (Maildir[mutt_strlen (Maildir) - 1] == '}' ||
369 Maildir[mutt_strlen (Maildir) - 1] == '/'))
370 strfcpy (p, NONULL (Maildir), sizeof (p));
373 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
379 /* elm compatibility, @ expands alias to user name */
386 if ((alias = mutt_lookup_alias (s + 1))) {
387 h = mutt_new_header ();
388 h->env = mutt_new_envelope ();
389 h->env->from = h->env->to = alias;
390 mutt_default_save (p, sizeof (p), h);
391 h->env->from = h->env->to = NULL;
392 mutt_free_header (&h);
393 /* Avoid infinite recursion if the resulting folder starts with '@' */
404 strfcpy (p, NONULL (Inbox), sizeof (p));
411 strfcpy (p, NONULL (Outbox), sizeof (p));
418 if (*(s + 1) == '!') {
419 strfcpy (p, NONULL (LastFolder), sizeof (p));
423 strfcpy (p, NONULL (Spoolfile), sizeof (p));
431 strfcpy (p, NONULL (LastFolder), sizeof (p));
438 strfcpy (p, NONULL (CurrentFolder), sizeof (p));
450 if (rx && *p && !recurse) {
451 mutt_rx_sanitize_string (q, sizeof (q), p);
452 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
455 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
457 strfcpy (s, tmp, slen);
462 /* Rewrite IMAP path in canonical form - aids in string comparisons of
463 * folders. May possibly fail, in which case s should be the same. */
465 imap_expand_path (s, slen);
471 /* Extract the real name from /etc/passwd's GECOS field.
472 * When set, honor the regular expression in GecosMask,
473 * otherwise assume that the GECOS field is a
474 * comma-separated list.
475 * Replace "&" by a capitalized version of the user's login
479 char *mutt_gecos_name (char *dest, size_t destlen, struct passwd *pw)
481 regmatch_t pat_match[1];
486 if (!pw || !pw->pw_gecos)
489 memset (dest, 0, destlen);
492 if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0)
493 strfcpy (dest, pw->pw_gecos + pat_match[0].rm_so,
494 MIN (pat_match[0].rm_eo - pat_match[0].rm_so + 1, destlen));
496 else if ((p = strchr (pw->pw_gecos, ',')))
497 strfcpy (dest, pw->pw_gecos, MIN (destlen, p - pw->pw_gecos + 1));
499 strfcpy (dest, pw->pw_gecos, destlen);
501 pwnl = mutt_strlen (pw->pw_name);
503 for (idx = 0; dest[idx]; idx++) {
504 if (dest[idx] == '&') {
505 memmove (&dest[idx + pwnl], &dest[idx + 1],
506 MAX (destlen - idx - pwnl - 1, 0));
507 memcpy (&dest[idx], pw->pw_name, MIN (destlen - idx - 1, pwnl));
508 dest[idx] = toupper ((unsigned char) dest[idx]);
516 char *mutt_get_parameter (const char *s, PARAMETER * p)
518 for (; p; p = p->next)
519 if (ascii_strcasecmp (s, p->attribute) == 0)
525 void mutt_set_parameter (const char *attribute, const char *value,
531 mutt_delete_parameter (attribute, p);
535 for (q = *p; q; q = q->next) {
536 if (ascii_strcasecmp (attribute, q->attribute) == 0) {
537 mutt_str_replace (&q->value, value);
542 q = mutt_new_parameter ();
543 q->attribute = safe_strdup (attribute);
544 q->value = safe_strdup (value);
549 void mutt_delete_parameter (const char *attribute, PARAMETER ** p)
553 for (q = *p; q; p = &q->next, q = q->next) {
554 if (ascii_strcasecmp (attribute, q->attribute) == 0) {
557 mutt_free_parameter (&q);
563 /* returns 1 if Mutt can't display this type of data, 0 otherwise */
564 int mutt_needs_mailcap (BODY * m)
569 if (!ascii_strcasecmp ("plain", m->subtype) ||
570 !ascii_strcasecmp ("rfc822-headers", m->subtype) ||
571 !ascii_strcasecmp ("enriched", m->subtype))
575 case TYPEAPPLICATION:
576 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (m))
578 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (m))
590 int mutt_is_text_part (BODY * b)
593 char *s = b->subtype;
595 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
601 if (t == TYPEMESSAGE) {
602 if (!ascii_strcasecmp ("delivery-status", s))
606 if ((WithCrypto & APPLICATION_PGP) && t == TYPEAPPLICATION) {
607 if (!ascii_strcasecmp ("pgp-keys", s))
614 void mutt_free_envelope (ENVELOPE ** p)
618 rfc822_free_address (&(*p)->return_path);
619 rfc822_free_address (&(*p)->from);
620 rfc822_free_address (&(*p)->to);
621 rfc822_free_address (&(*p)->cc);
622 rfc822_free_address (&(*p)->bcc);
623 rfc822_free_address (&(*p)->sender);
624 rfc822_free_address (&(*p)->reply_to);
625 rfc822_free_address (&(*p)->mail_followup_to);
627 FREE (&(*p)->list_post);
628 FREE (&(*p)->subject);
629 /* real_subj is just an offset to subject and shouldn't be freed */
630 FREE (&(*p)->message_id);
631 FREE (&(*p)->supersedes);
633 FREE (&(*p)->x_label);
634 FREE (&(*p)->organization);
636 FREE (&(*p)->newsgroups);
638 FREE (&(*p)->followup_to);
639 FREE (&(*p)->x_comment_to);
642 mutt_buffer_free (&(*p)->spam);
643 mutt_free_list (&(*p)->references);
644 mutt_free_list (&(*p)->in_reply_to);
645 mutt_free_list (&(*p)->userhdrs);
649 void _mutt_mktemp (char *s, const char *src, int line)
651 snprintf (s, _POSIX_PATH_MAX, "%s/mutt-%s-%d-%d-%d", NONULL (Tempdir),
652 NONULL (Hostname), (int) getuid (), (int) getpid (), Counter++);
654 (debugfile, "%s:%d: mutt_mktemp returns \"%s\".\n", src, line, s));
658 void mutt_free_alias (ALIAS ** p)
666 rfc822_free_address (&t->addr);
671 /* collapse the pathname using ~ or = when possible */
672 void mutt_pretty_mailbox (char *s)
678 scheme = url_check_scheme (s);
681 if (scheme == U_IMAP || scheme == U_IMAPS) {
682 imap_pretty_mailbox (s);
687 /* if s is an url, only collapse path component */
688 if (scheme != U_UNKNOWN) {
689 p = strchr (s, ':') + 1;
690 if (!strncmp (p, "//", 2))
691 q = strchr (p + 2, '/');
693 q = strchr (p, '\0');
697 /* first attempt to collapse the pathname */
699 if (*p == '/' && p[1] == '/') {
703 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
712 if (mutt_strncmp (s, Maildir, (len = mutt_strlen (Maildir))) == 0 &&
715 memmove (s, s + len, mutt_strlen (s + len) + 1);
717 else if (mutt_strncmp (s, Homedir, (len = mutt_strlen (Homedir))) == 0 &&
720 memmove (s, s + len - 1, mutt_strlen (s + len - 1) + 1);
724 void mutt_pretty_size (char *s, size_t len, long n)
727 strfcpy (s, "0K", len);
728 else if (n < 10189) /* 0.1K - 9.9K */
729 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
730 else if (n < 1023949) { /* 10K - 999K */
731 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
732 snprintf (s, len, "%ldK", (n + 51) / 1024);
734 else if (n < 10433332) /* 1.0M - 9.9M */
735 snprintf (s, len, "%3.1fM", n / 1048576.0);
738 /* (10433332 + 52428) / 1048576 = 10 */
739 snprintf (s, len, "%ldM", (n + 52428) / 1048576);
743 void mutt_expand_file_fmt (char *dest, size_t destlen, const char *fmt,
746 char tmp[LONG_STRING];
748 mutt_quote_filename (tmp, sizeof (tmp), src);
749 mutt_expand_fmt (dest, destlen, fmt, tmp);
752 void mutt_expand_fmt (char *dest, size_t destlen, const char *fmt,
760 slen = mutt_strlen (src);
763 for (p = fmt, d = dest; destlen && *p; p++) {
772 strfcpy (d, src, destlen + 1);
773 d += destlen > slen ? slen : destlen;
774 destlen -= destlen > slen ? slen : destlen;
791 if (!found && destlen > 0) {
792 safe_strcat (dest, destlen, " ");
793 safe_strcat (dest, destlen, src);
798 /* return 0 on success, -1 on abort, 1 on error */
799 int mutt_check_overwrite (const char *attname, const char *path,
800 char *fname, size_t flen, int *append,
804 char tmp[_POSIX_PATH_MAX];
807 strfcpy (fname, path, flen);
808 if (access (fname, F_OK) != 0)
810 if (stat (fname, &st) != 0)
812 if (S_ISDIR (st.st_mode)) {
814 switch (mutt_multi_choice
815 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
818 mutt_str_replace (directory, fname);
833 mutt_yesorno (_("File is a directory, save under it?"),
835 return (rc == M_NO) ? 1 : -1;
837 if (!attname || !attname[0]) {
839 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
840 M_FILE | M_CLEAR) != 0 || !tmp[0])
842 mutt_concat_path (fname, path, tmp, flen);
845 mutt_concat_path (fname, path, mutt_basename (attname), flen);
848 if (*append == 0 && access (fname, F_OK) == 0) {
849 switch (mutt_multi_choice
850 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
858 *append = M_SAVE_APPEND;
860 case 1: /* overwrite */
861 *append = M_SAVE_OVERWRITE;
868 void mutt_save_path (char *d, size_t dsize, ADDRESS * a)
870 if (a && a->mailbox) {
871 strfcpy (d, a->mailbox, dsize);
872 if (!option (OPTSAVEADDRESS)) {
875 if ((p = strpbrk (d, "%@")))
884 void mutt_safe_path (char *s, size_t l, ADDRESS * a)
888 mutt_save_path (s, l, a);
890 if (*p == '/' || ISSPACE (*p) || !IsPrint ((unsigned char) *p))
894 /* counts how many characters in s can be skipped while none of the
895 * characters of c appears */
896 int mutt_skipchars (const char *s, const char *c)
902 register const char *t = c;
912 return (mutt_strlen (p));
915 void mutt_FormatString (char *dest, /* output buffer */
916 size_t destlen, /* output buffer len */
917 const char *src, /* template string */
918 format_t * callback, /* callback for processing */
919 unsigned long data, /* callback data */
921 { /* callback flags */
922 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
923 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
924 size_t wlen, count, len, col, wid;
927 destlen--; /* save room for the terminal \0 */
928 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
931 while (*src && wlen < destlen) {
942 flags |= M_FORMAT_OPTIONAL;
946 flags &= ~M_FORMAT_OPTIONAL;
948 /* eat the format string */
951 while (count < sizeof (prefix) &&
952 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
961 break; /* bad format */
963 ch = *src++; /* save the character to switch on */
965 if (flags & M_FORMAT_OPTIONAL) {
967 break; /* bad format */
970 /* eat the `if' part of the string */
973 while (count < sizeof (ifstring) && *src && *src != '?'
980 /* eat the `else' part of the string (optional) */
982 src++; /* skip the & */
985 while (count < sizeof (elsestring) && *src && *src != '?') {
992 break; /* bad format */
994 src++; /* move past the trailing `?' */
997 /* handle generic cases first */
999 /* right justify to EOL */
1000 ch = *src++; /* pad char */
1001 /* calculate space left on line. if we've already written more data
1002 than will fit on the line, ignore the rest of the line */
1003 if (DrawFullLine || option (OPTSTATUSONTOP))
1004 count = (COLS < destlen ? COLS : destlen);
1007 ((COLS - SidebarWidth) <
1008 destlen ? (COLS - SidebarWidth) : destlen);
1010 count -= col; /* how many columns left on this line */
1011 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
1012 wid = mutt_strlen (buf);
1014 count -= wid; /* how many chars to pad */
1015 memset (wptr, ch, count);
1019 if (wid + wlen > destlen)
1020 len = destlen - wlen;
1023 memcpy (wptr, buf, len);
1026 col += mutt_strwidth (buf);
1028 break; /* skip rest of input */
1030 else if (ch == '|') {
1035 if (destlen > wlen) {
1036 count = destlen - wlen;
1037 memset (wptr, ch, count);
1040 break; /* skip rest of input */
1046 while (ch == '_' || ch == ':') {
1055 /* use callback function to handle this case */
1057 callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring,
1061 mutt_strlower (buf);
1070 if ((len = mutt_strlen (buf)) + wlen > destlen)
1071 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
1073 memcpy (wptr, buf, len);
1076 col += mutt_strwidth (buf);
1079 else if (*src == '\\') {
1108 unsigned int bar = mutt_skipchars (src, "%\\");
1109 char *bar2 = safe_malloc (bar + 1);
1111 strfcpy (bar2, src, bar + 1);
1116 col += mutt_strwidth (bar2);
1123 if (flags & M_FORMAT_MAKEPRINT) {
1124 /* Make sure that the string is printable by changing all non-printable
1125 chars to dots, or spaces for non-printable whitespace */
1126 for (cp = dest; *cp; cp++)
1127 if (!IsPrint (*cp) && !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
1128 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
1133 /* This function allows the user to specify a command to read stdout from in
1134 place of a normal file. If the last character in the string is a pipe (|),
1135 then we assume it is a commmand to run instead of a normal file. */
1136 FILE *mutt_open_read (const char *path, pid_t * thepid)
1141 int len = mutt_strlen (path);
1143 if (path[len - 1] == '|') {
1144 /* read from a pipe */
1146 char *s = safe_strdup (path);
1150 *thepid = mutt_create_filter (s, NULL, &f, NULL);
1154 if (stat (path, &s) < 0)
1156 if (S_ISDIR (s.st_mode)) {
1160 f = fopen (path, "r");
1166 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
1167 int mutt_save_confirm (const char *s, struct stat *st)
1169 char tmp[_POSIX_PATH_MAX];
1174 magic = mx_get_magic (s);
1177 if (magic == M_POP) {
1178 mutt_error _("Can't save message to POP mailbox.");
1185 if (magic == M_NNTP) {
1186 mutt_error _("Can't save message to newsserver.");
1192 if (stat (s, st) != -1) {
1194 mutt_error (_("%s is not a mailbox!"), s);
1198 if (option (OPTCONFIRMAPPEND) &&
1199 (!TrashPath || (mutt_strcmp (s, TrashPath) != 0)))
1200 /* if we're appending to the trash, there's no point in asking */
1202 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
1203 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1211 if (magic != M_IMAP)
1212 #endif /* execute the block unconditionally if we don't use imap */
1217 if (errno == ENOENT) {
1218 if (option (OPTCONFIRMCREATE)) {
1219 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
1220 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1233 CLEARLINE (LINES - 1);
1237 void state_prefix_putc (char c, STATE * s)
1239 if (s->flags & M_PENDINGPREFIX) {
1242 i = mutt_strlen (Quotebuf);
1245 if (i == sizeof (Quotebuf) - 1 || c == '\n') {
1246 char buf[2 * SHORT_STRING];
1247 int j = 0, offset = 0;
1248 regmatch_t pmatch[1];
1250 state_reset_prefix (s);
1252 ((regex_t *) QuoteRegexp.rx, &Quotebuf[offset], 1, pmatch,
1254 offset += pmatch->rm_eo;
1256 if (!option (OPTQUOTEEMPTY) && Quotebuf[offset] == '\n') {
1260 else if (option (OPTQUOTEQUOTED) && offset) {
1261 for (i = 0; i < offset; i++)
1262 if (Quotebuf[i] != ' ')
1264 strncpy (buf, Quotebuf, j + 1);
1265 strcpy (buf + j + 1, Quotebuf + j);
1268 snprintf (buf, sizeof (buf), "%s%s", NONULL (s->prefix), Quotebuf);
1270 state_puts (buf, s);
1277 state_set_prefix (s);
1282 int state_printf (STATE * s, const char *fmt, ...)
1288 rv = vfprintf (s->fpout, fmt, ap);
1294 void state_mark_attach (STATE * s)
1296 if ((s->flags & M_DISPLAY) && !mutt_strcmp (Pager, "builtin"))
1297 state_puts (AttachmentMarker, s);
1300 void state_attach_puts (const char *t, STATE * s)
1303 state_mark_attach (s);
1306 if (*t++ == '\n' && *t)
1308 state_mark_attach (s);
1312 void mutt_display_sanitize (char *s)
1320 void mutt_sleep (short s)
1329 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1330 * just initializes. Frees anything already in the buffer.
1332 * Disregards the 'destroy' flag, which seems reserved for caller.
1333 * This is bad, but there's no apparent protocol for it.
1335 BUFFER *mutt_buffer_init (BUFFER * b)
1338 b = safe_malloc (sizeof (BUFFER));
1345 memset (b, 0, sizeof (BUFFER));
1350 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1351 * just initializes. Frees anything already in the buffer. Copies in
1354 * Disregards the 'destroy' flag, which seems reserved for caller.
1355 * This is bad, but there's no apparent protocol for it.
1357 BUFFER *mutt_buffer_from (BUFFER * b, char *seed)
1362 b = mutt_buffer_init (b);
1363 b->data = safe_strdup (seed);
1364 b->dsize = mutt_strlen (seed);
1365 b->dptr = (char *) b->data + b->dsize;
1369 void mutt_buffer_addstr (BUFFER * buf, const char *s)
1371 mutt_buffer_add (buf, s, mutt_strlen (s));
1374 void mutt_buffer_addch (BUFFER * buf, char c)
1376 mutt_buffer_add (buf, &c, 1);
1379 void mutt_buffer_free (BUFFER ** p)
1385 /* dptr is just an offset to data and shouldn't be freed */
1389 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
1390 * Always one byte bigger than necessary for the null terminator, and
1391 * the buffer is always null-terminated */
1392 void mutt_buffer_add (BUFFER * buf, const char *s, size_t len)
1396 if (buf->dptr + len + 1 > buf->data + buf->dsize) {
1397 offset = buf->dptr - buf->data;
1398 buf->dsize += len < 128 ? 128 : len + 1;
1399 safe_realloc ((void **) &buf->data, buf->dsize);
1400 buf->dptr = buf->data + offset;
1402 memcpy (buf->dptr, s, len);
1404 *(buf->dptr) = '\0';
1407 /* Decrease a file's modification time by 1 second */
1409 time_t mutt_decrease_mtime (const char *f, struct stat *st)
1411 struct utimbuf utim;
1416 if (stat (f, &_st) == -1)
1421 if ((mtime = st->st_mtime) == time (NULL)) {
1423 utim.actime = mtime;
1424 utim.modtime = mtime;
1431 const char *mutt_make_version (void)
1433 static char vstring[STRING];
1435 snprintf (vstring, sizeof (vstring), "Mutt-ng %s (%s)",
1436 MUTT_VERSION, ReleaseDate);
1440 REGEXP *mutt_compile_regexp (const char *s, int flags)
1442 REGEXP *pp = safe_calloc (sizeof (REGEXP), 1);
1444 pp->pattern = safe_strdup (s);
1445 pp->rx = safe_calloc (sizeof (regex_t), 1);
1446 if (REGCOMP (pp->rx, NONULL (s), flags) != 0)
1447 mutt_free_regexp (&pp);
1452 void mutt_free_regexp (REGEXP ** pp)
1454 FREE (&(*pp)->pattern);
1455 regfree ((*pp)->rx);
1460 void mutt_free_rx_list (RX_LIST ** list)
1468 *list = (*list)->next;
1469 mutt_free_regexp (&p->rx);
1474 void mutt_free_spam_list (SPAM_LIST ** list)
1482 *list = (*list)->next;
1483 mutt_free_regexp (&p->rx);
1489 int mutt_match_rx_list (const char *s, RX_LIST * l)
1494 for (; l; l = l->next) {
1495 if (regexec (l->rx->rx, s, (size_t) 0, (regmatch_t *) 0, (int) 0) == 0) {
1497 (debugfile, "mutt_match_rx_list: %s matches %s\n", s,
1506 int mutt_match_spam_list (const char *s, SPAM_LIST * l, char *text, int x)
1508 static regmatch_t *pmatch = NULL;
1509 static int nmatch = 0;
1518 for (; l; l = l->next) {
1519 /* If this pattern needs more matches, expand pmatch. */
1520 if (l->nmatch > nmatch) {
1521 safe_realloc (&pmatch, l->nmatch * sizeof (regmatch_t));
1525 /* Does this pattern match? */
1527 (l->rx->rx, s, (size_t) l->nmatch, (regmatch_t *) pmatch,
1530 (debugfile, "mutt_match_spam_list: %s matches %s\n", s,
1533 (debugfile, "mutt_match_spam_list: %d subs\n",
1534 l->rx->rx->re_nsub));
1536 /* Copy template into text, with substitutions. */
1537 for (p = l->template; *p;) {
1539 n = atoi (++p); /* find pmatch index */
1540 while (isdigit (*p))
1541 ++p; /* skip subst token */
1542 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
1543 text[tlen++] = s[i];
1546 text[tlen++] = *p++;
1550 dprint (5, (debugfile, "mutt_match_spam_list: \"%s\"\n", text));