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"
25 #include "imap/mx_imap.h"
28 #include "mutt_crypt.h"
33 #include "lib/debug.h"
44 #include <sys/types.h>
47 BODY *mutt_new_body (void)
49 BODY *p = (BODY *) safe_calloc (1, sizeof (BODY));
51 p->disposition = DISPATTACH;
57 /* Modified by blong to accept a "suggestion" for file name. If
58 * that file exists, then construct one with unique name but
59 * keep any extension. This might fail, I guess.
60 * Renamed to mutt_adv_mktemp so I only have to change where it's
61 * called, and not all possible cases.
63 void mutt_adv_mktemp (char *s, size_t l)
65 char buf[_POSIX_PATH_MAX];
66 char tmp[_POSIX_PATH_MAX];
71 strfcpy (buf, NONULL (Tempdir), sizeof (buf));
72 mutt_expand_path (buf, sizeof (buf));
74 snprintf (s, l, "%s/muttXXXXXX", buf);
78 strfcpy (tmp, s, sizeof (tmp));
79 mutt_sanitize_filename (tmp, 1);
80 snprintf (s, l, "%s/%s", buf, tmp);
81 if (lstat (s, &sb) == -1 && errno == ENOENT)
83 if ((period = strrchr (tmp, '.')) != NULL)
85 snprintf (s, l, "%s/%s.XXXXXX", buf, tmp);
90 strfcpy (s + sl, period, l - sl);
95 /* create a send-mode duplicate from a receive-mode body */
97 int mutt_copy_body (FILE * fp, BODY ** tgt, BODY * src)
99 char tmp[_POSIX_PATH_MAX];
102 PARAMETER *par, **ppar;
108 strfcpy (tmp, src->filename, sizeof (tmp));
115 mutt_adv_mktemp (tmp, sizeof (tmp));
116 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
119 *tgt = mutt_new_body ();
122 memcpy (b, src, sizeof (BODY));
126 b->filename = safe_strdup (tmp);
127 b->use_disp = use_disp;
130 if (mutt_is_text_part (b))
133 b->xtype = safe_strdup (b->xtype);
134 b->subtype = safe_strdup (b->subtype);
135 b->form_name = safe_strdup (b->form_name);
136 b->filename = safe_strdup (b->filename);
137 b->d_filename = safe_strdup (b->d_filename);
138 b->description = safe_strdup (b->description);
141 * we don't seem to need the HEADER structure currently.
142 * XXX - this may change in the future
148 /* copy parameters */
149 for (par = b->parameter, ppar = &b->parameter; par;
150 ppar = &(*ppar)->next, par = par->next) {
151 *ppar = mutt_new_parameter ();
152 (*ppar)->attribute = safe_strdup (par->attribute);
153 (*ppar)->value = safe_strdup (par->value);
156 mutt_stamp_attachment (b);
163 void mutt_free_body (BODY ** p)
172 mutt_free_parameter (&b->parameter);
173 if (b->unlink && b->filename) {
174 debug_print (1, ("unlinking %s.\n", b->filename));
175 unlink (b->filename);
177 else if (b->filename)
178 debug_print (1, ("not unlinking %s.\n", b->filename));
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, mutt_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 = imap[s]://host/: don't append slash */
366 if (imap_is_magic (NONULL (Maildir), NULL) == M_IMAP &&
367 Maildir[mutt_strlen (Maildir) - 1] == '/')
368 strfcpy (p, NONULL (Maildir), sizeof (p));
371 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
377 /* elm compatibility, @ expands alias to user name */
384 if ((alias = mutt_lookup_alias (s + 1))) {
385 h = mutt_new_header ();
386 h->env = mutt_new_envelope ();
387 h->env->from = h->env->to = alias;
388 mutt_default_save (p, sizeof (p), h);
389 h->env->from = h->env->to = NULL;
390 mutt_free_header (&h);
391 /* Avoid infinite recursion if the resulting folder starts with '@' */
402 strfcpy (p, NONULL (Inbox), sizeof (p));
409 strfcpy (p, NONULL (Outbox), sizeof (p));
416 if (*(s + 1) == '!') {
417 strfcpy (p, NONULL (LastFolder), sizeof (p));
421 strfcpy (p, NONULL (Spoolfile), sizeof (p));
429 strfcpy (p, NONULL (LastFolder), sizeof (p));
436 strfcpy (p, NONULL (CurrentFolder), sizeof (p));
448 if (rx && *p && !recurse) {
449 mutt_rx_sanitize_string (q, sizeof (q), p);
450 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
453 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
455 strfcpy (s, tmp, slen);
462 /* Extract the real name from /etc/passwd's GECOS field.
463 * When set, honor the regular expression in GecosMask,
464 * otherwise assume that the GECOS field is a
465 * comma-separated list.
466 * Replace "&" by a capitalized version of the user's login
470 char *mutt_gecos_name (char *dest, size_t destlen, struct passwd *pw)
472 regmatch_t pat_match[1];
477 if (!pw || !pw->pw_gecos)
480 memset (dest, 0, destlen);
483 if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0)
484 strfcpy (dest, pw->pw_gecos + pat_match[0].rm_so,
485 MIN (pat_match[0].rm_eo - pat_match[0].rm_so + 1, destlen));
487 else if ((p = strchr (pw->pw_gecos, ',')))
488 strfcpy (dest, pw->pw_gecos, MIN (destlen, p - pw->pw_gecos + 1));
490 strfcpy (dest, pw->pw_gecos, destlen);
492 pwnl = mutt_strlen (pw->pw_name);
494 for (idx = 0; dest[idx]; idx++) {
495 if (dest[idx] == '&') {
496 memmove (&dest[idx + pwnl], &dest[idx + 1],
497 MAX (destlen - idx - pwnl - 1, 0));
498 memcpy (&dest[idx], pw->pw_name, MIN (destlen - idx - 1, pwnl));
499 dest[idx] = toupper ((unsigned char) dest[idx]);
507 char *mutt_get_parameter (const char *s, PARAMETER * p)
509 for (; p; p = p->next)
510 if (ascii_strcasecmp (s, p->attribute) == 0)
516 void mutt_set_parameter (const char *attribute, const char *value,
522 mutt_delete_parameter (attribute, p);
526 for (q = *p; q; q = q->next) {
527 if (ascii_strcasecmp (attribute, q->attribute) == 0) {
528 str_replace (&q->value, value);
533 q = mutt_new_parameter ();
534 q->attribute = safe_strdup (attribute);
535 q->value = safe_strdup (value);
540 void mutt_delete_parameter (const char *attribute, PARAMETER ** p)
544 for (q = *p; q; p = &q->next, q = q->next) {
545 if (ascii_strcasecmp (attribute, q->attribute) == 0) {
548 mutt_free_parameter (&q);
554 /* returns 1 if Mutt can't display this type of data, 0 otherwise */
555 int mutt_needs_mailcap (BODY * m)
560 if (!ascii_strcasecmp ("plain", m->subtype) ||
561 !ascii_strcasecmp ("rfc822-headers", m->subtype) ||
562 !ascii_strcasecmp ("enriched", m->subtype))
566 case TYPEAPPLICATION:
567 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (m))
569 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (m))
581 int mutt_is_text_part (BODY * b)
584 char *s = b->subtype;
586 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
592 if (t == TYPEMESSAGE) {
593 if (!ascii_strcasecmp ("delivery-status", s))
597 if ((WithCrypto & APPLICATION_PGP) && t == TYPEAPPLICATION) {
598 if (!ascii_strcasecmp ("pgp-keys", s))
605 void mutt_free_envelope (ENVELOPE ** p)
609 rfc822_free_address (&(*p)->return_path);
610 rfc822_free_address (&(*p)->from);
611 rfc822_free_address (&(*p)->to);
612 rfc822_free_address (&(*p)->cc);
613 rfc822_free_address (&(*p)->bcc);
614 rfc822_free_address (&(*p)->sender);
615 rfc822_free_address (&(*p)->reply_to);
616 rfc822_free_address (&(*p)->mail_followup_to);
618 FREE (&(*p)->list_post);
619 FREE (&(*p)->subject);
620 /* real_subj is just an offset to subject and shouldn't be freed */
621 FREE (&(*p)->message_id);
622 FREE (&(*p)->supersedes);
624 FREE (&(*p)->x_label);
625 FREE (&(*p)->organization);
627 FREE (&(*p)->newsgroups);
629 FREE (&(*p)->followup_to);
630 FREE (&(*p)->x_comment_to);
633 mutt_buffer_free (&(*p)->spam);
634 mutt_free_list (&(*p)->references);
635 mutt_free_list (&(*p)->in_reply_to);
636 mutt_free_list (&(*p)->userhdrs);
640 /* move all the headers from extra not present in base into base */
641 void mutt_merge_envelopes(ENVELOPE* base, ENVELOPE** extra)
643 /* copies each existing element if necessary, and sets the element
644 * to NULL in the source so that mutt_free_envelope doesn't leave us
645 * with dangling pointers. */
646 #define MOVE_ELEM(h) if (!base->h) { base->h = (*extra)->h; (*extra)->h = NULL; }
647 MOVE_ELEM(return_path);
654 MOVE_ELEM(mail_followup_to);
655 MOVE_ELEM(list_post);
656 MOVE_ELEM(message_id);
657 MOVE_ELEM(supersedes);
660 if (!base->refs_changed) {
661 MOVE_ELEM(references);
663 if (!base->irt_changed) {
664 MOVE_ELEM(in_reply_to);
666 /* real_subj is subordinate to subject */
667 if (!base->subject) {
668 base->subject = (*extra)->subject;
669 base->real_subj = (*extra)->real_subj;
670 (*extra)->subject = NULL;
671 (*extra)->real_subj = NULL;
673 /* spam and user headers should never be hashed, and the new envelope may
674 * have better values. Use new versions regardless. */
675 mutt_buffer_free (&base->spam);
676 mutt_free_list (&base->userhdrs);
681 mutt_free_envelope(extra);
684 void _mutt_mktemp (char *s, const char *src, int line)
687 snprintf (s, _POSIX_PATH_MAX, "%s/muttng-%s-%d-%d-%d-%x%x", NONULL (Tempdir),
688 NONULL (Hostname), (int) getuid (), (int) getpid (), Counter++,
689 (unsigned int) rand(), (unsigned int) rand());
690 debug_print (1, ("%s:%d: mutt_mktemp returns \"%s\".\n", src, line, s));
694 void mutt_free_alias (ALIAS ** p)
702 rfc822_free_address (&t->addr);
707 /* collapse the pathname using ~ or = when possible */
708 void mutt_pretty_mailbox (char *s)
714 scheme = url_check_scheme (s);
717 if (scheme == U_IMAP || scheme == U_IMAPS) {
718 imap_pretty_mailbox (s);
723 /* if s is an url, only collapse path component */
724 if (scheme != U_UNKNOWN) {
725 p = strchr (s, ':') + 1;
726 if (!strncmp (p, "//", 2))
727 q = strchr (p + 2, '/');
729 q = strchr (p, '\0');
733 /* first attempt to collapse the pathname */
735 if (*p == '/' && p[1] == '/') {
739 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
748 if (safe_strncmp (s, Maildir, (len = mutt_strlen (Maildir))) == 0 &&
751 memmove (s, s + len, mutt_strlen (s + len) + 1);
753 else if (safe_strncmp (s, Homedir, (len = mutt_strlen (Homedir))) == 0 &&
756 memmove (s, s + len - 1, mutt_strlen (s + len - 1) + 1);
760 void mutt_pretty_size (char *s, size_t len, long n)
763 strfcpy (s, "0K", len);
764 else if (n < 10189) /* 0.1K - 9.9K */
765 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
766 else if (n < 1023949) { /* 10K - 999K */
767 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
768 snprintf (s, len, "%ldK", (n + 51) / 1024);
770 else if (n < 10433332) /* 1.0M - 9.9M */
771 snprintf (s, len, "%3.1fM", n / 1048576.0);
774 /* (10433332 + 52428) / 1048576 = 10 */
775 snprintf (s, len, "%ldM", (n + 52428) / 1048576);
779 void mutt_expand_file_fmt (char *dest, size_t destlen, const char *fmt,
782 char tmp[LONG_STRING];
784 mutt_quote_filename (tmp, sizeof (tmp), src);
785 mutt_expand_fmt (dest, destlen, fmt, tmp);
788 void mutt_expand_fmt (char *dest, size_t destlen, const char *fmt,
796 slen = mutt_strlen (src);
799 for (p = fmt, d = dest; destlen && *p; p++) {
808 strfcpy (d, src, destlen + 1);
809 d += destlen > slen ? slen : destlen;
810 destlen -= destlen > slen ? slen : destlen;
827 if (!found && destlen > 0) {
828 safe_strcat (dest, destlen, " ");
829 safe_strcat (dest, destlen, src);
834 /* return 0 on success, -1 on abort, 1 on error */
835 int mutt_check_overwrite (const char *attname, const char *path,
836 char *fname, size_t flen, int *append,
840 char tmp[_POSIX_PATH_MAX];
843 strfcpy (fname, path, flen);
844 if (access (fname, F_OK) != 0)
846 if (stat (fname, &st) != 0)
848 if (S_ISDIR (st.st_mode)) {
850 switch (mutt_multi_choice
851 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
854 str_replace (directory, fname);
869 mutt_yesorno (_("File is a directory, save under it?"),
871 return (rc == M_NO) ? 1 : -1;
873 if (!attname || !attname[0]) {
875 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
876 M_FILE | M_CLEAR) != 0 || !tmp[0])
878 mutt_concat_path (fname, path, tmp, flen);
881 mutt_concat_path (fname, path, mutt_basename (attname), flen);
884 if (*append == 0 && access (fname, F_OK) == 0) {
885 switch (mutt_multi_choice
886 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
894 *append = M_SAVE_APPEND;
896 case 1: /* overwrite */
897 *append = M_SAVE_OVERWRITE;
904 void mutt_save_path (char *d, size_t dsize, ADDRESS * a)
906 if (a && a->mailbox) {
907 strfcpy (d, a->mailbox, dsize);
908 if (!option (OPTSAVEADDRESS)) {
911 if ((p = strpbrk (d, "%@")))
920 void mutt_safe_path (char *s, size_t l, ADDRESS * a)
924 mutt_save_path (s, l, a);
926 if (*p == '/' || ISSPACE (*p) || !IsPrint ((unsigned char) *p))
930 /* counts how many characters in s can be skipped while none of the
931 * characters of c appears */
932 int mutt_skipchars (const char *s, const char *c)
938 register const char *t = c;
948 return (mutt_strlen (p));
951 void mutt_FormatString (char *dest, /* output buffer */
952 size_t destlen, /* output buffer len */
953 const char *src, /* template string */
954 format_t * callback, /* callback for processing */
955 unsigned long data, /* callback data */
957 { /* callback flags */
958 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
959 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
960 size_t wlen, count, len, col, wid;
963 destlen--; /* save room for the terminal \0 */
964 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
967 while (*src && wlen < destlen) {
978 flags |= M_FORMAT_OPTIONAL;
982 flags &= ~M_FORMAT_OPTIONAL;
984 /* eat the format string */
987 while (count < sizeof (prefix) &&
988 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
997 break; /* bad format */
999 ch = *src++; /* save the character to switch on */
1001 if (flags & M_FORMAT_OPTIONAL) {
1003 break; /* bad format */
1006 /* eat the `if' part of the string */
1009 while (count < sizeof (ifstring) && *src && *src != '?'
1016 /* eat the `else' part of the string (optional) */
1018 src++; /* skip the & */
1021 while (count < sizeof (elsestring) && *src && *src != '?') {
1028 break; /* bad format */
1030 src++; /* move past the trailing `?' */
1033 /* handle generic cases first */
1035 /* right justify to EOL */
1036 ch = *src++; /* pad char */
1037 /* calculate space left on line. if we've already written more data
1038 than will fit on the line, ignore the rest of the line */
1039 if (DrawFullLine || option (OPTSTATUSONTOP))
1040 count = (COLS < destlen ? COLS : destlen);
1043 ((COLS - SidebarWidth) <
1044 destlen ? (COLS - SidebarWidth) : destlen);
1046 count -= col; /* how many columns left on this line */
1047 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
1048 wid = mutt_strlen (buf);
1050 count -= wid; /* how many chars to pad */
1051 memset (wptr, ch, count);
1055 if (wid + wlen > destlen)
1056 len = destlen - wlen;
1059 memcpy (wptr, buf, len);
1062 col += mutt_strwidth (buf);
1064 break; /* skip rest of input */
1066 else if (ch == '|') {
1071 if (destlen > wlen) {
1072 count = destlen - wlen;
1073 memset (wptr, ch, count);
1076 break; /* skip rest of input */
1082 while (ch == '_' || ch == ':') {
1091 /* use callback function to handle this case */
1093 callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring,
1106 if ((len = mutt_strlen (buf)) + wlen > destlen)
1107 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
1109 memcpy (wptr, buf, len);
1112 col += mutt_strwidth (buf);
1115 else if (*src == '\\') {
1144 unsigned int bar = mutt_skipchars (src, "%\\");
1145 char *bar2 = safe_malloc (bar + 1);
1147 strfcpy (bar2, src, bar + 1);
1152 col += mutt_strwidth (bar2);
1159 if (flags & M_FORMAT_MAKEPRINT) {
1160 /* Make sure that the string is printable by changing all non-printable
1161 chars to dots, or spaces for non-printable whitespace */
1162 for (cp = dest; *cp; cp++)
1163 if (!IsPrint (*cp) && !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
1164 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
1169 /* This function allows the user to specify a command to read stdout from in
1170 place of a normal file. If the last character in the string is a pipe (|),
1171 then we assume it is a commmand to run instead of a normal file. */
1172 FILE *mutt_open_read (const char *path, pid_t * thepid)
1177 int len = mutt_strlen (path);
1179 if (path[len - 1] == '|') {
1180 /* read from a pipe */
1182 char *s = safe_strdup (path);
1186 *thepid = mutt_create_filter (s, NULL, &f, NULL);
1190 if (stat (path, &s) < 0)
1192 if (S_ISDIR (s.st_mode)) {
1196 f = fopen (path, "r");
1202 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
1203 int mutt_save_confirm (const char *s, struct stat *st)
1205 char tmp[_POSIX_PATH_MAX];
1210 magic = mx_get_magic (s);
1213 if (magic == M_POP) {
1214 mutt_error _("Can't save message to POP mailbox.");
1221 if (magic == M_NNTP) {
1222 mutt_error _("Can't save message to newsserver.");
1228 if (stat (s, st) != -1) {
1230 mutt_error (_("%s is not a mailbox!"), s);
1234 if (option (OPTCONFIRMAPPEND) &&
1235 (!TrashPath || (mutt_strcmp (s, TrashPath) != 0)))
1236 /* if we're appending to the trash, there's no point in asking */
1238 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
1239 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1247 if (magic != M_IMAP)
1248 #endif /* execute the block unconditionally if we don't use imap */
1253 if (errno == ENOENT) {
1254 if (option (OPTCONFIRMCREATE)) {
1255 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
1256 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1269 CLEARLINE (LINES - 1);
1273 void state_prefix_putc (char c, STATE * s)
1275 if (s->flags & M_PENDINGPREFIX) {
1278 i = mutt_strlen (Quotebuf);
1281 if (i == sizeof (Quotebuf) - 1 || c == '\n') {
1282 char buf[2 * SHORT_STRING];
1283 int j = 0, offset = 0;
1284 regmatch_t pmatch[1];
1286 state_reset_prefix (s);
1288 ((regex_t *) QuoteRegexp.rx, &Quotebuf[offset], 1, pmatch,
1290 offset += pmatch->rm_eo;
1292 if (!option (OPTQUOTEEMPTY) && Quotebuf[offset] == '\n') {
1296 else if (option (OPTQUOTEQUOTED) && offset) {
1297 for (i = 0; i < offset; i++)
1298 if (Quotebuf[i] != ' ')
1300 strncpy (buf, Quotebuf, j + 1);
1301 strcpy (buf + j + 1, Quotebuf + j);
1304 snprintf (buf, sizeof (buf), "%s%s", NONULL (s->prefix), Quotebuf);
1306 state_puts (buf, s);
1313 state_set_prefix (s);
1318 int state_printf (STATE * s, const char *fmt, ...)
1324 rv = vfprintf (s->fpout, fmt, ap);
1330 void state_mark_attach (STATE * s)
1332 if ((s->flags & M_DISPLAY) && !mutt_strcmp (Pager, "builtin"))
1333 state_puts (AttachmentMarker, s);
1336 void state_attach_puts (const char *t, STATE * s)
1339 state_mark_attach (s);
1342 if (*t++ == '\n' && *t)
1344 state_mark_attach (s);
1348 void mutt_display_sanitize (char *s)
1356 void mutt_sleep (short s)
1365 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1366 * just initializes. Frees anything already in the buffer.
1368 * Disregards the 'destroy' flag, which seems reserved for caller.
1369 * This is bad, but there's no apparent protocol for it.
1371 BUFFER *mutt_buffer_init (BUFFER * b)
1374 b = safe_malloc (sizeof (BUFFER));
1381 memset (b, 0, sizeof (BUFFER));
1386 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1387 * just initializes. Frees anything already in the buffer. Copies in
1390 * Disregards the 'destroy' flag, which seems reserved for caller.
1391 * This is bad, but there's no apparent protocol for it.
1393 BUFFER *mutt_buffer_from (BUFFER * b, char *seed)
1398 b = mutt_buffer_init (b);
1399 b->data = safe_strdup (seed);
1400 b->dsize = mutt_strlen (seed);
1401 b->dptr = (char *) b->data + b->dsize;
1405 void mutt_buffer_addstr (BUFFER * buf, const char *s)
1407 mutt_buffer_add (buf, s, mutt_strlen (s));
1410 void mutt_buffer_addch (BUFFER * buf, char c)
1412 mutt_buffer_add (buf, &c, 1);
1415 void mutt_buffer_free (BUFFER ** p)
1421 /* dptr is just an offset to data and shouldn't be freed */
1425 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
1426 * Always one byte bigger than necessary for the null terminator, and
1427 * the buffer is always null-terminated */
1428 void mutt_buffer_add (BUFFER * buf, const char *s, size_t len)
1432 if (buf->dptr + len + 1 > buf->data + buf->dsize) {
1433 offset = buf->dptr - buf->data;
1434 buf->dsize += len < 128 ? 128 : len + 1;
1435 safe_realloc ((void **) &buf->data, buf->dsize);
1436 buf->dptr = buf->data + offset;
1438 memcpy (buf->dptr, s, len);
1440 *(buf->dptr) = '\0';
1443 /* Decrease a file's modification time by 1 second */
1445 time_t mutt_decrease_mtime (const char *f, struct stat *st)
1447 struct utimbuf utim;
1452 if (stat (f, &_st) == -1)
1457 if ((mtime = st->st_mtime) == time (NULL)) {
1459 utim.actime = mtime;
1460 utim.modtime = mtime;
1467 const char *mutt_make_version (void)
1469 static char vstring[STRING];
1471 snprintf (vstring, sizeof (vstring), "Mutt-ng %s (%s) based on Mutt 1.5.9",
1472 MUTT_VERSION, ReleaseDate);
1476 void mutt_free_spam_list (SPAM_LIST ** list)
1484 *list = (*list)->next;
1491 int mutt_match_spam_list (const char *s, SPAM_LIST * l, char *text, int x)
1493 static regmatch_t *pmatch = NULL;
1494 static int nmatch = 0;
1503 for (; l; l = l->next) {
1504 /* If this pattern needs more matches, expand pmatch. */
1505 if (l->nmatch > nmatch) {
1506 safe_realloc (&pmatch, l->nmatch * sizeof (regmatch_t));
1510 /* Does this pattern match? */
1512 (l->rx->rx, s, (size_t) l->nmatch, (regmatch_t *) pmatch,
1514 debug_print (5, ("%s matches %s\n%d subst", s, l->rx->pattern, l->rx->rx->re_nsub));
1516 /* Copy template into text, with substitutions. */
1517 for (p = l->template; *p;) {
1519 n = atoi (++p); /* find pmatch index */
1520 while (isdigit (*p))
1521 ++p; /* skip subst token */
1522 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
1523 text[tlen++] = s[i];
1526 text[tlen++] = *p++;
1530 debug_print (5, ("\"%s\"\n", text));