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 /* returns true if the header contained in "s" is in list "t" */
161 int mutt_matches_ignore (const char *s, string_list_t * t)
163 for (; t; t = t->next) {
164 if (!ascii_strncasecmp (s, t->data, m_strlen(t->data))
171 /* prepend the path part of *path to *lnk */
172 void mutt_expand_link (char *newpath, const char *path, const char *lnk)
174 const char *lb = NULL;
177 /* lnk is full path */
179 m_strcpy(newpath, _POSIX_PATH_MAX, lnk);
183 if ((lb = strrchr (path, '/')) == NULL) {
185 m_strcpy(newpath, _POSIX_PATH_MAX, lnk);
190 memcpy (newpath, path, len);
191 m_strcpy(newpath + len, _POSIX_PATH_MAX - len, lnk);
194 char *mutt_expand_path (char *s, ssize_t slen)
196 return _mutt_expand_path (s, slen, 0);
199 char *_mutt_expand_path (char *s, ssize_t slen, int rx)
201 char p[_POSIX_PATH_MAX] = "";
202 char q[_POSIX_PATH_MAX] = "";
203 char tmp[_POSIX_PATH_MAX];
206 const char *tail = "";
216 if (*(s + 1) == '/' || *(s + 1) == 0) {
217 m_strcpy(p, sizeof(p), NONULL(Homedir));
223 if ((t = strchr (s + 1, '/')))
226 if ((pw = getpwnam (s + 1))) {
227 m_strcpy(p, sizeof(p), pw->pw_dir);
236 /* user not found! */
249 /* if folder = imap[s]://host/: don't append slash */
250 if (imap_is_magic (NONULL (Maildir), NULL) == M_IMAP &&
251 Maildir[m_strlen(Maildir) - 1] == '/')
252 m_strcpy(p, sizeof(p), NONULL(Maildir));
254 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
260 /* elm compatibility, @ expands alias to user name */
268 if ((alias = alias_lookup(s + 1))) {
270 h->env = envelope_new();
271 h->env->from = h->env->to = alias;
272 mutt_default_save (p, sizeof (p), h);
273 h->env->from = h->env->to = NULL;
275 /* Avoid infinite recursion if the resulting folder starts with '@' */
286 m_strcpy(p, sizeof(p), NONULL(Inbox));
293 m_strcpy(p, sizeof(p), NONULL(Outbox));
300 if (*(s + 1) == '!') {
301 m_strcpy(p, sizeof(p), NONULL(LastFolder));
305 m_strcpy(p, sizeof(p), NONULL(Spoolfile));
313 m_strcpy(p, sizeof(p), NONULL(LastFolder));
320 m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
332 if (rx && *p && !recurse) {
333 rx_sanitize_string (q, sizeof (q), p);
334 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
337 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
339 m_strcpy(s, slen, tmp);
346 /* move all the headers from extra not present in base into base */
347 void mutt_merge_envelopes(ENVELOPE* base, ENVELOPE** extra)
349 /* copies each existing element if necessary, and sets the element
350 * to NULL in the source so that envelope_delete doesn't leave us
351 * with dangling pointers. */
352 #define MOVE_ELEM(h) if (!base->h) { base->h = (*extra)->h; (*extra)->h = NULL; }
353 MOVE_ELEM(return_path);
360 MOVE_ELEM(mail_followup_to);
361 MOVE_ELEM(list_post);
362 MOVE_ELEM(message_id);
363 MOVE_ELEM(supersedes);
366 if (!base->refs_changed) {
367 MOVE_ELEM(references);
369 if (!base->irt_changed) {
370 MOVE_ELEM(in_reply_to);
372 /* real_subj is subordinate to subject */
373 if (!base->subject) {
374 base->subject = (*extra)->subject;
375 base->real_subj = (*extra)->real_subj;
376 (*extra)->subject = NULL;
377 (*extra)->real_subj = NULL;
379 /* spam and user headers should never be hashed, and the new envelope may
380 * have better values. Use new versions regardless. */
381 mutt_buffer_free (&base->spam);
382 string_list_wipe(&base->userhdrs);
387 envelope_delete(extra);
390 void _mutt_mktemp (char *s, const char *src, int line)
393 snprintf (s, _POSIX_PATH_MAX, "%s/madmutt-%s-%d-%d-%d-%x%x", NONULL (Tempdir),
394 NONULL (Hostname), (int) getuid (), (int) getpid (), Counter++,
395 (unsigned int) rand(), (unsigned int) rand());
399 /* collapse the pathname using ~ or = when possible */
400 void mutt_pretty_mailbox (char *s)
406 scheme = url_check_scheme (s);
408 if (scheme == U_IMAP || scheme == U_IMAPS) {
409 imap_pretty_mailbox (s);
413 /* if s is an url, only collapse path component */
414 if (scheme != U_UNKNOWN) {
415 p = strchr (s, ':') + 1;
416 if (!strncmp (p, "//", 2))
417 q = strchr (p + 2, '/');
419 q = strchr (p, '\0');
423 /* first attempt to collapse the pathname */
425 if (*p == '/' && p[1] == '/') {
429 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
438 if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
441 memmove (s, s + len, m_strlen(s + len) + 1);
443 else if (m_strncmp(s, Homedir, (len = m_strlen(Homedir))) == 0 &&
446 memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
450 void mutt_pretty_size (char *s, ssize_t len, long n)
453 m_strcpy(s, len, "0K");
454 else if (n < 10189) /* 0.1K - 9.9K */
455 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
456 else if (n < 1023949) { /* 10K - 999K */
457 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
458 snprintf (s, len, "%ldK", (n + 51) / 1024);
460 else if (n < 10433332) /* 1.0M - 9.9M */
461 snprintf (s, len, "%3.1fM", n / 1048576.0);
464 /* (10433332 + 52428) / 1048576 = 10 */
465 snprintf (s, len, "%ldM", (n + 52428) / 1048576);
469 void mutt_expand_file_fmt (char *dest, ssize_t destlen, const char *fmt,
472 char tmp[LONG_STRING];
474 mutt_quote_filename (tmp, sizeof (tmp), src);
475 mutt_expand_fmt (dest, destlen, fmt, tmp);
478 void mutt_expand_fmt (char *dest, ssize_t destlen, const char *fmt,
486 slen = m_strlen(src);
489 for (p = fmt, d = dest; destlen && *p; p++) {
498 m_strcpy(d, destlen + 1, src);
499 d += destlen > slen ? slen : destlen;
500 destlen -= destlen > slen ? slen : destlen;
517 if (!found && destlen > 0) {
518 m_strcat(dest, destlen, " ");
519 m_strcat(dest, destlen, src);
524 /* return 0 on success, -1 on abort, 1 on error */
525 int mutt_check_overwrite (const char *attname, const char *path,
526 char *fname, ssize_t flen, int *append,
530 char tmp[_POSIX_PATH_MAX];
533 m_strcpy(fname, flen, path);
534 if (access (fname, F_OK) != 0)
536 if (stat (fname, &st) != 0)
538 if (S_ISDIR (st.st_mode)) {
540 switch (mutt_multi_choice
541 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
544 m_strreplace(directory, fname);
559 mutt_yesorno (_("File is a directory, save under it?"),
561 return (rc == M_NO) ? 1 : -1;
563 if (!attname || !attname[0]) {
565 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
566 M_FILE | M_CLEAR) != 0 || !tmp[0])
568 mutt_concat_path(fname, flen, path, tmp);
571 mutt_concat_path(fname, flen, path, mutt_basename(attname));
574 if (*append == 0 && access (fname, F_OK) == 0) {
575 switch (mutt_multi_choice
576 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
584 *append = M_SAVE_APPEND;
586 case 1: /* overwrite */
587 *append = M_SAVE_OVERWRITE;
594 void mutt_save_path (char *d, ssize_t dsize, address_t * a)
596 if (a && a->mailbox) {
597 m_strcpy(d, dsize, a->mailbox);
598 if (!option (OPTSAVEADDRESS)) {
601 if ((p = strpbrk (d, "%@")))
610 void mutt_safe_path (char *s, ssize_t l, address_t * a)
614 mutt_save_path (s, l, a);
616 if (*p == '/' || ISSPACE (*p) || !IsPrint ((unsigned char) *p))
620 /* counts how many characters in s can be skipped while none of the
621 * characters of c appears */
622 int mutt_skipchars (const char *s, const char *c)
628 register const char *t = c;
638 return (m_strlen(p));
641 void mutt_FormatString (char *dest, /* output buffer */
642 ssize_t destlen, /* output buffer len */
643 const char *src, /* template string */
644 format_t * callback, /* callback for processing */
645 unsigned long data, /* callback data */
647 { /* callback flags */
648 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
649 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
650 ssize_t wlen, wid, count, col, len;
653 destlen--; /* save room for the terminal \0 */
654 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
657 while (*src && wlen < destlen) {
668 flags |= M_FORMAT_OPTIONAL;
672 flags &= ~M_FORMAT_OPTIONAL;
674 /* eat the format string */
677 while (count < ssizeof (prefix) &&
678 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
687 break; /* bad format */
689 ch = *src++; /* save the character to switch on */
691 if (flags & M_FORMAT_OPTIONAL) {
693 break; /* bad format */
696 /* eat the `if' part of the string */
699 while (count < ssizeof (ifstring) && *src && *src != '?'
706 /* eat the `else' part of the string (optional) */
708 src++; /* skip the & */
711 while (count < ssizeof (elsestring) && *src && *src != '?') {
718 break; /* bad format */
720 src++; /* move past the trailing `?' */
723 /* handle generic cases first */
725 /* right justify to EOL */
726 ch = *src++; /* pad char */
727 /* calculate space left on line. if we've already written more data
728 than will fit on the line, ignore the rest of the line */
729 if (DrawFullLine || option (OPTSTATUSONTOP))
730 count = (COLS < destlen ? COLS : destlen);
732 count = ((COLS - SW) < destlen ? (COLS - SW) : destlen);
734 count -= col; /* how many columns left on this line */
735 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
738 count -= wid; /* how many chars to pad */
739 memset (wptr, ch, count);
743 if (wid + wlen > destlen)
744 len = destlen - wlen;
747 memcpy (wptr, buf, len);
750 col += mutt_strwidth (buf);
752 break; /* skip rest of input */
754 else if (ch == '|') {
759 if (destlen > wlen) {
760 count = destlen - wlen;
761 memset (wptr, ch, count);
764 break; /* skip rest of input */
770 while (ch == '_' || ch == ':') {
779 /* use callback function to handle this case */
781 callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring,
794 if ((len = m_strlen(buf)) + wlen > destlen)
795 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
797 memcpy (wptr, buf, len);
800 col += mutt_strwidth (buf);
803 else if (*src == '\\') {
832 unsigned int bar = mutt_skipchars (src, "%\\");
833 char *bar2 = p_dupstr(src, bar);
839 col += mutt_strwidth (bar2);
846 if (flags & M_FORMAT_MAKEPRINT) {
847 /* Make sure that the string is printable by changing all non-printable
848 chars to dots, or spaces for non-printable whitespace */
849 for (cp = dest; *cp; cp++)
850 if (!IsPrint (*cp) && !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
851 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
856 /* This function allows the user to specify a command to read stdout from in
857 place of a normal file. If the last character in the string is a pipe (|),
858 then we assume it is a commmand to run instead of a normal file. */
859 FILE *mutt_open_read (const char *path, pid_t * thepid)
861 int len = m_strlen(path);
864 if (path[len - 1] == '|') {
865 char *s = m_strdup(path);
867 /* read from a pipe */
871 *thepid = mutt_create_filter (s, NULL, &f, NULL);
874 f = fopen (path, "r");
883 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
884 int mutt_save_confirm (const char *s, struct stat *st)
886 char tmp[_POSIX_PATH_MAX];
891 magic = mx_get_magic (s);
893 if (magic == M_POP) {
894 mutt_error _("Can't save message to POP mailbox.");
900 if (magic == M_NNTP) {
901 mutt_error _("Can't save message to newsserver.");
907 if (magic > 0 && !mx_access (s, W_OK)) {
908 if (option (OPTCONFIRMAPPEND) &&
909 (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
910 /* if we're appending to the trash, there's no point in asking */
911 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
912 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
919 if (stat (s, st) != -1) {
921 mutt_error (_("%s is not a mailbox!"), s);
931 if (errno == ENOENT) {
932 if (option (OPTCONFIRMCREATE)) {
933 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
934 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
947 CLEARLINE (LINES - 1);
951 void mutt_display_sanitize (char *s)
959 void mutt_sleep (short s)
967 /* Decrease a file's modification time by 1 second */
968 time_t mutt_decrease_mtime (const char *f, struct stat *st)
975 if (stat (f, &_st) == -1)
980 if ((mtime = st->st_mtime) == time (NULL)) {
983 utim.modtime = mtime;
990 /* sets mtime of 'to' to mtime of 'from' */
991 void mutt_set_mtime (const char* from, const char* to) {
995 if (stat (from, &st) != -1) {
996 utim.actime = st.st_mtime;
997 utim.modtime = st.st_mtime;
1002 const char *mutt_make_version (int full)
1004 static char vstring[STRING];
1007 snprintf (vstring, sizeof (vstring),
1008 "Madmutt/%s-r%s (based on Mutt 1.5.11)",
1009 MUTT_VERSION, MUTT_REVISION);
1011 snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
1012 MUTT_VERSION, MUTT_REVISION);
1016 void mutt_free_spam_list (SPAM_LIST ** list)
1024 *list = (*list)->next;
1026 p_delete(&p->template);
1031 int mutt_match_spam_list (const char *s, SPAM_LIST * l, char *text, int x)
1033 static regmatch_t *pmatch = NULL;
1034 static int nmatch = 0;
1043 for (; l; l = l->next) {
1044 /* If this pattern needs more matches, expand pmatch. */
1045 if (l->nmatch > nmatch) {
1046 p_realloc(&pmatch, l->nmatch);
1050 /* Does this pattern match? */
1051 if (regexec(l->rx->rx, s, l->nmatch, (regmatch_t *)pmatch, (int) 0) == 0)
1053 /* Copy template into text, with substitutions. */
1054 for (p = l->template; *p;) {
1056 n = atoi (++p); /* find pmatch index */
1057 while (isdigit ((unsigned char) *p))
1058 ++p; /* skip subst token */
1059 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
1060 text[tlen++] = s[i];
1063 text[tlen++] = *p++;
1074 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
1076 if (h1->received != h2->received ||
1077 h1->date_sent != h2->date_sent ||
1078 h1->content->length != h2->content->length ||
1079 h1->lines != h2->lines ||
1080 h1->zhours != h2->zhours ||
1081 h1->zminutes != h2->zminutes ||
1082 h1->zoccident != h2->zoccident ||
1083 h1->mime != h2->mime ||
1084 !mutt_cmp_env (h1->env, h2->env) ||
1085 !mutt_cmp_body (h1->content, h2->content))
1091 if (h1 == NULL && h2 == NULL)
1098 /* return 1 if address lists are strictly identical */
1099 int mutt_cmp_addr (const address_t * a, const address_t * b)
1102 if (m_strcmp(a->mailbox, b->mailbox) ||
1103 m_strcmp(a->personal, b->personal))
1115 int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
1118 if (m_strcmp(a->data, b->data))
1130 int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
1133 if (m_strcmp(e1->message_id, e2->message_id) ||
1134 m_strcmp(e1->subject, e2->subject) ||
1135 !mutt_cmp_list (e1->references, e2->references) ||
1136 !mutt_cmp_addr (e1->from, e2->from) ||
1137 !mutt_cmp_addr (e1->sender, e2->sender) ||
1138 !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
1139 !mutt_cmp_addr (e1->to, e2->to) ||
1140 !mutt_cmp_addr (e1->cc, e2->cc) ||
1141 !mutt_cmp_addr (e1->return_path, e2->return_path))
1147 if (e1 == NULL && e2 == NULL)
1154 int mutt_cmp_body (const BODY * b1, const BODY * b2)
1156 if (b1->type != b2->type ||
1157 b1->encoding != b2->encoding ||
1158 m_strcmp(b1->subtype, b2->subtype) ||
1159 m_strcmp(b1->description, b2->description) ||
1160 !parameter_equal(b1->parameter, b2->parameter) ||
1161 b1->length != b2->length)