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.
23 #include <sys/types.h>
29 #include <lib-lib/mem.h>
30 #include <lib-lib/ascii.h>
31 #include <lib-lib/str.h>
32 #include <lib-lib/macros.h>
33 #include <lib-lib/buffer.h>
34 #include <lib-lib/file.h>
35 #include <lib-lib/url.h>
37 #include <lib-mime/mime.h>
39 #include <lib-ui/curses.h>
40 #include <lib-ui/enter.h>
49 #include <imap/imap.h>
50 #include <imap/mx_imap.h>
52 #include <lib-crypt/crypt.h>
54 #define SW (option(OPTMBOXPANE)?SidebarWidth:0)
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 (const char* dir, char *s, ssize_t l)
64 char buf[_POSIX_PATH_MAX];
65 char tmp[_POSIX_PATH_MAX];
70 m_strcpy(buf, sizeof(buf), m_strisempty(dir) ? NONULL(Tempdir) : dir);
71 mutt_expand_path (buf, sizeof (buf));
73 snprintf (s, l, "%s/muttXXXXXX", buf);
77 m_strcpy(tmp, sizeof(tmp), s);
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 m_strcpy(s + sl, l - sl, period);
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_t *par, **ppar;
107 m_strcpy(tmp, sizeof(tmp), src->filename);
114 mutt_adv_mktemp (NULL, tmp, sizeof (tmp));
115 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
121 memcpy (b, src, sizeof (BODY));
125 b->filename = m_strdup(tmp);
126 b->use_disp = use_disp;
129 if (mutt_is_text_part (b))
132 b->xtype = m_strdup(b->xtype);
133 b->subtype = m_strdup(b->subtype);
134 b->form_name = m_strdup(b->form_name);
135 b->filename = m_strdup(b->filename);
136 b->d_filename = m_strdup(b->d_filename);
137 b->description = m_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 = parameter_new();
151 (*ppar)->attribute = m_strdup(par->attribute);
152 (*ppar)->value = m_strdup(par->value);
155 mutt_stamp_attachment (b);
160 HEADER *mutt_dup_header (HEADER * h)
165 memcpy (hnew, h, sizeof (HEADER));
169 /* returns true if the header contained in "s" is in list "t" */
170 int mutt_matches_ignore (const char *s, string_list_t * t)
172 for (; t; t = t->next) {
173 if (!ascii_strncasecmp (s, t->data, m_strlen(t->data))
180 /* prepend the path part of *path to *lnk */
181 void mutt_expand_link (char *newpath, const char *path, const char *lnk)
183 const char *lb = NULL;
186 /* lnk is full path */
188 m_strcpy(newpath, _POSIX_PATH_MAX, lnk);
192 if ((lb = strrchr (path, '/')) == NULL) {
194 m_strcpy(newpath, _POSIX_PATH_MAX, lnk);
199 memcpy (newpath, path, len);
200 m_strcpy(newpath + len, _POSIX_PATH_MAX - len, lnk);
203 char *mutt_expand_path (char *s, ssize_t slen)
205 return _mutt_expand_path (s, slen, 0);
208 char *_mutt_expand_path (char *s, ssize_t slen, int rx)
210 char p[_POSIX_PATH_MAX] = "";
211 char q[_POSIX_PATH_MAX] = "";
212 char tmp[_POSIX_PATH_MAX];
215 const char *tail = "";
225 if (*(s + 1) == '/' || *(s + 1) == 0) {
226 m_strcpy(p, sizeof(p), NONULL(Homedir));
232 if ((t = strchr (s + 1, '/')))
235 if ((pw = getpwnam (s + 1))) {
236 m_strcpy(p, sizeof(p), pw->pw_dir);
245 /* user not found! */
258 /* if folder = imap[s]://host/: don't append slash */
259 if (imap_is_magic (NONULL (Maildir), NULL) == M_IMAP &&
260 Maildir[m_strlen(Maildir) - 1] == '/')
261 m_strcpy(p, sizeof(p), NONULL(Maildir));
263 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
269 /* elm compatibility, @ expands alias to user name */
277 if ((alias = alias_lookup(s + 1))) {
279 h->env = envelope_new();
280 h->env->from = h->env->to = alias;
281 mutt_default_save (p, sizeof (p), h);
282 h->env->from = h->env->to = NULL;
284 /* Avoid infinite recursion if the resulting folder starts with '@' */
295 m_strcpy(p, sizeof(p), NONULL(Inbox));
302 m_strcpy(p, sizeof(p), NONULL(Outbox));
309 if (*(s + 1) == '!') {
310 m_strcpy(p, sizeof(p), NONULL(LastFolder));
314 m_strcpy(p, sizeof(p), NONULL(Spoolfile));
322 m_strcpy(p, sizeof(p), NONULL(LastFolder));
329 m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
341 if (rx && *p && !recurse) {
342 rx_sanitize_string (q, sizeof (q), p);
343 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
346 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
348 m_strcpy(s, slen, tmp);
355 /* returns 1 if Mutt can't display this type of data, 0 otherwise */
356 int mutt_needs_mailcap (BODY * m)
361 if (!ascii_strcasecmp ("plain", m->subtype) ||
362 !ascii_strcasecmp ("rfc822-headers", m->subtype) ||
363 !ascii_strcasecmp ("enriched", m->subtype))
367 case TYPEAPPLICATION:
368 if (mutt_is_application_pgp (m))
370 if (mutt_is_application_smime (m))
382 int mutt_is_text_part (BODY * b)
385 char *s = b->subtype;
387 if (mutt_is_application_pgp (b))
393 if (t == TYPEMESSAGE) {
394 if (!ascii_strcasecmp ("delivery-status", s))
398 if (t == TYPEAPPLICATION) {
399 if (!ascii_strcasecmp ("pgp-keys", s))
406 /* move all the headers from extra not present in base into base */
407 void mutt_merge_envelopes(ENVELOPE* base, ENVELOPE** extra)
409 /* copies each existing element if necessary, and sets the element
410 * to NULL in the source so that envelope_delete doesn't leave us
411 * with dangling pointers. */
412 #define MOVE_ELEM(h) if (!base->h) { base->h = (*extra)->h; (*extra)->h = NULL; }
413 MOVE_ELEM(return_path);
420 MOVE_ELEM(mail_followup_to);
421 MOVE_ELEM(list_post);
422 MOVE_ELEM(message_id);
423 MOVE_ELEM(supersedes);
426 if (!base->refs_changed) {
427 MOVE_ELEM(references);
429 if (!base->irt_changed) {
430 MOVE_ELEM(in_reply_to);
432 /* real_subj is subordinate to subject */
433 if (!base->subject) {
434 base->subject = (*extra)->subject;
435 base->real_subj = (*extra)->real_subj;
436 (*extra)->subject = NULL;
437 (*extra)->real_subj = NULL;
439 /* spam and user headers should never be hashed, and the new envelope may
440 * have better values. Use new versions regardless. */
441 mutt_buffer_free (&base->spam);
442 string_list_wipe(&base->userhdrs);
447 envelope_delete(extra);
450 void _mutt_mktemp (char *s, const char *src, int line)
453 snprintf (s, _POSIX_PATH_MAX, "%s/madmutt-%s-%d-%d-%d-%x%x", NONULL (Tempdir),
454 NONULL (Hostname), (int) getuid (), (int) getpid (), Counter++,
455 (unsigned int) rand(), (unsigned int) rand());
459 /* collapse the pathname using ~ or = when possible */
460 void mutt_pretty_mailbox (char *s)
466 scheme = url_check_scheme (s);
468 if (scheme == U_IMAP || scheme == U_IMAPS) {
469 imap_pretty_mailbox (s);
473 /* if s is an url, only collapse path component */
474 if (scheme != U_UNKNOWN) {
475 p = strchr (s, ':') + 1;
476 if (!strncmp (p, "//", 2))
477 q = strchr (p + 2, '/');
479 q = strchr (p, '\0');
483 /* first attempt to collapse the pathname */
485 if (*p == '/' && p[1] == '/') {
489 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
498 if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
501 memmove (s, s + len, m_strlen(s + len) + 1);
503 else if (m_strncmp(s, Homedir, (len = m_strlen(Homedir))) == 0 &&
506 memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
510 void mutt_pretty_size (char *s, ssize_t len, long n)
513 m_strcpy(s, len, "0K");
514 else if (n < 10189) /* 0.1K - 9.9K */
515 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
516 else if (n < 1023949) { /* 10K - 999K */
517 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
518 snprintf (s, len, "%ldK", (n + 51) / 1024);
520 else if (n < 10433332) /* 1.0M - 9.9M */
521 snprintf (s, len, "%3.1fM", n / 1048576.0);
524 /* (10433332 + 52428) / 1048576 = 10 */
525 snprintf (s, len, "%ldM", (n + 52428) / 1048576);
529 void mutt_expand_file_fmt (char *dest, ssize_t destlen, const char *fmt,
532 char tmp[LONG_STRING];
534 mutt_quote_filename (tmp, sizeof (tmp), src);
535 mutt_expand_fmt (dest, destlen, fmt, tmp);
538 void mutt_expand_fmt (char *dest, ssize_t destlen, const char *fmt,
546 slen = m_strlen(src);
549 for (p = fmt, d = dest; destlen && *p; p++) {
558 m_strcpy(d, destlen + 1, src);
559 d += destlen > slen ? slen : destlen;
560 destlen -= destlen > slen ? slen : destlen;
577 if (!found && destlen > 0) {
578 m_strcat(dest, destlen, " ");
579 m_strcat(dest, destlen, src);
584 /* return 0 on success, -1 on abort, 1 on error */
585 int mutt_check_overwrite (const char *attname, const char *path,
586 char *fname, ssize_t flen, int *append,
590 char tmp[_POSIX_PATH_MAX];
593 m_strcpy(fname, flen, path);
594 if (access (fname, F_OK) != 0)
596 if (stat (fname, &st) != 0)
598 if (S_ISDIR (st.st_mode)) {
600 switch (mutt_multi_choice
601 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
604 m_strreplace(directory, fname);
619 mutt_yesorno (_("File is a directory, save under it?"),
621 return (rc == M_NO) ? 1 : -1;
623 if (!attname || !attname[0]) {
625 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
626 M_FILE | M_CLEAR) != 0 || !tmp[0])
628 mutt_concat_path(fname, flen, path, tmp);
631 mutt_concat_path(fname, flen, path, mutt_basename(attname));
634 if (*append == 0 && access (fname, F_OK) == 0) {
635 switch (mutt_multi_choice
636 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
644 *append = M_SAVE_APPEND;
646 case 1: /* overwrite */
647 *append = M_SAVE_OVERWRITE;
654 void mutt_save_path (char *d, ssize_t dsize, address_t * a)
656 if (a && a->mailbox) {
657 m_strcpy(d, dsize, a->mailbox);
658 if (!option (OPTSAVEADDRESS)) {
661 if ((p = strpbrk (d, "%@")))
670 void mutt_safe_path (char *s, ssize_t l, address_t * a)
674 mutt_save_path (s, l, a);
676 if (*p == '/' || ISSPACE (*p) || !IsPrint ((unsigned char) *p))
680 /* counts how many characters in s can be skipped while none of the
681 * characters of c appears */
682 int mutt_skipchars (const char *s, const char *c)
688 register const char *t = c;
698 return (m_strlen(p));
701 void mutt_FormatString (char *dest, /* output buffer */
702 ssize_t destlen, /* output buffer len */
703 const char *src, /* template string */
704 format_t * callback, /* callback for processing */
705 unsigned long data, /* callback data */
707 { /* callback flags */
708 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
709 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
710 ssize_t wlen, wid, count, col, len;
713 destlen--; /* save room for the terminal \0 */
714 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
717 while (*src && wlen < destlen) {
728 flags |= M_FORMAT_OPTIONAL;
732 flags &= ~M_FORMAT_OPTIONAL;
734 /* eat the format string */
737 while (count < ssizeof (prefix) &&
738 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
747 break; /* bad format */
749 ch = *src++; /* save the character to switch on */
751 if (flags & M_FORMAT_OPTIONAL) {
753 break; /* bad format */
756 /* eat the `if' part of the string */
759 while (count < ssizeof (ifstring) && *src && *src != '?'
766 /* eat the `else' part of the string (optional) */
768 src++; /* skip the & */
771 while (count < ssizeof (elsestring) && *src && *src != '?') {
778 break; /* bad format */
780 src++; /* move past the trailing `?' */
783 /* handle generic cases first */
785 /* right justify to EOL */
786 ch = *src++; /* pad char */
787 /* calculate space left on line. if we've already written more data
788 than will fit on the line, ignore the rest of the line */
789 if (DrawFullLine || option (OPTSTATUSONTOP))
790 count = (COLS < destlen ? COLS : destlen);
792 count = ((COLS - SW) < destlen ? (COLS - SW) : destlen);
794 count -= col; /* how many columns left on this line */
795 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
798 count -= wid; /* how many chars to pad */
799 memset (wptr, ch, count);
803 if (wid + wlen > destlen)
804 len = destlen - wlen;
807 memcpy (wptr, buf, len);
810 col += mutt_strwidth (buf);
812 break; /* skip rest of input */
814 else if (ch == '|') {
819 if (destlen > wlen) {
820 count = destlen - wlen;
821 memset (wptr, ch, count);
824 break; /* skip rest of input */
830 while (ch == '_' || ch == ':') {
839 /* use callback function to handle this case */
841 callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring,
854 if ((len = m_strlen(buf)) + wlen > destlen)
855 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
857 memcpy (wptr, buf, len);
860 col += mutt_strwidth (buf);
863 else if (*src == '\\') {
892 unsigned int bar = mutt_skipchars (src, "%\\");
893 char *bar2 = p_dupstr(src, bar);
899 col += mutt_strwidth (bar2);
906 if (flags & M_FORMAT_MAKEPRINT) {
907 /* Make sure that the string is printable by changing all non-printable
908 chars to dots, or spaces for non-printable whitespace */
909 for (cp = dest; *cp; cp++)
910 if (!IsPrint (*cp) && !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
911 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
916 /* This function allows the user to specify a command to read stdout from in
917 place of a normal file. If the last character in the string is a pipe (|),
918 then we assume it is a commmand to run instead of a normal file. */
919 FILE *mutt_open_read (const char *path, pid_t * thepid)
921 int len = m_strlen(path);
924 if (path[len - 1] == '|') {
925 char *s = m_strdup(path);
927 /* read from a pipe */
931 *thepid = mutt_create_filter (s, NULL, &f, NULL);
934 f = fopen (path, "r");
943 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
944 int mutt_save_confirm (const char *s, struct stat *st)
946 char tmp[_POSIX_PATH_MAX];
951 magic = mx_get_magic (s);
953 if (magic == M_POP) {
954 mutt_error _("Can't save message to POP mailbox.");
960 if (magic == M_NNTP) {
961 mutt_error _("Can't save message to newsserver.");
967 if (magic > 0 && !mx_access (s, W_OK)) {
968 if (option (OPTCONFIRMAPPEND) &&
969 (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
970 /* if we're appending to the trash, there's no point in asking */
971 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
972 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
979 if (stat (s, st) != -1) {
981 mutt_error (_("%s is not a mailbox!"), s);
991 if (errno == ENOENT) {
992 if (option (OPTCONFIRMCREATE)) {
993 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
994 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1007 CLEARLINE (LINES - 1);
1011 void mutt_display_sanitize (char *s)
1019 void mutt_sleep (short s)
1027 /* Decrease a file's modification time by 1 second */
1028 time_t mutt_decrease_mtime (const char *f, struct stat *st)
1030 struct utimbuf utim;
1035 if (stat (f, &_st) == -1)
1040 if ((mtime = st->st_mtime) == time (NULL)) {
1042 utim.actime = mtime;
1043 utim.modtime = mtime;
1050 /* sets mtime of 'to' to mtime of 'from' */
1051 void mutt_set_mtime (const char* from, const char* to) {
1052 struct utimbuf utim;
1055 if (stat (from, &st) != -1) {
1056 utim.actime = st.st_mtime;
1057 utim.modtime = st.st_mtime;
1062 const char *mutt_make_version (int full)
1064 static char vstring[STRING];
1067 snprintf (vstring, sizeof (vstring),
1068 "Madmutt/%s-r%s (based on Mutt 1.5.11)",
1069 MUTT_VERSION, MUTT_REVISION);
1071 snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
1072 MUTT_VERSION, MUTT_REVISION);
1076 void mutt_free_spam_list (SPAM_LIST ** list)
1084 *list = (*list)->next;
1086 p_delete(&p->template);
1091 int mutt_match_spam_list (const char *s, SPAM_LIST * l, char *text, int x)
1093 static regmatch_t *pmatch = NULL;
1094 static int nmatch = 0;
1103 for (; l; l = l->next) {
1104 /* If this pattern needs more matches, expand pmatch. */
1105 if (l->nmatch > nmatch) {
1106 p_realloc(&pmatch, l->nmatch);
1110 /* Does this pattern match? */
1111 if (regexec(l->rx->rx, s, l->nmatch, (regmatch_t *)pmatch, (int) 0) == 0)
1113 /* Copy template into text, with substitutions. */
1114 for (p = l->template; *p;) {
1116 n = atoi (++p); /* find pmatch index */
1117 while (isdigit ((unsigned char) *p))
1118 ++p; /* skip subst token */
1119 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
1120 text[tlen++] = s[i];
1123 text[tlen++] = *p++;
1134 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
1136 if (h1->received != h2->received ||
1137 h1->date_sent != h2->date_sent ||
1138 h1->content->length != h2->content->length ||
1139 h1->lines != h2->lines ||
1140 h1->zhours != h2->zhours ||
1141 h1->zminutes != h2->zminutes ||
1142 h1->zoccident != h2->zoccident ||
1143 h1->mime != h2->mime ||
1144 !mutt_cmp_env (h1->env, h2->env) ||
1145 !mutt_cmp_body (h1->content, h2->content))
1151 if (h1 == NULL && h2 == NULL)
1158 /* return 1 if address lists are strictly identical */
1159 int mutt_cmp_addr (const address_t * a, const address_t * b)
1162 if (m_strcmp(a->mailbox, b->mailbox) ||
1163 m_strcmp(a->personal, b->personal))
1175 int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
1178 if (m_strcmp(a->data, b->data))
1190 int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
1193 if (m_strcmp(e1->message_id, e2->message_id) ||
1194 m_strcmp(e1->subject, e2->subject) ||
1195 !mutt_cmp_list (e1->references, e2->references) ||
1196 !mutt_cmp_addr (e1->from, e2->from) ||
1197 !mutt_cmp_addr (e1->sender, e2->sender) ||
1198 !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
1199 !mutt_cmp_addr (e1->to, e2->to) ||
1200 !mutt_cmp_addr (e1->cc, e2->cc) ||
1201 !mutt_cmp_addr (e1->return_path, e2->return_path))
1207 if (e1 == NULL && e2 == NULL)
1214 int mutt_cmp_body (const BODY * b1, const BODY * b2)
1216 if (b1->type != b2->type ||
1217 b1->encoding != b2->encoding ||
1218 m_strcmp(b1->subtype, b2->subtype) ||
1219 m_strcmp(b1->description, b2->description) ||
1220 !parameter_equal(b1->parameter, b2->parameter) ||
1221 b1->length != b2->length)