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.
11 #include <lib-lib/lib-lib.h>
17 #include <lib-mime/mime.h>
18 #include <lib-ui/curses.h>
19 #include <lib-ui/enter.h>
20 #include <lib-sys/unix.h>
21 #include <lib-mx/mx.h>
29 #include <imap/imap.h>
31 #include <lib-crypt/crypt.h>
33 #define SW (option(OPTMBOXPANE)?SidebarWidth:0)
35 /* Modified by blong to accept a "suggestion" for file name. If
36 * that file exists, then construct one with unique name but
37 * keep any extension. This might fail, I guess.
38 * Renamed to mutt_adv_mktemp so I only have to change where it's
39 * called, and not all possible cases.
41 void mutt_adv_mktemp (const char* dir, char *s, ssize_t l)
43 char buf[_POSIX_PATH_MAX];
44 char tmp[_POSIX_PATH_MAX];
49 m_strcpy(buf, sizeof(buf), m_strisempty(dir) ? NONULL(Tempdir) : dir);
50 mutt_expand_path (buf, sizeof (buf));
52 snprintf (s, l, "%s/muttXXXXXX", buf);
56 m_strcpy(tmp, sizeof(tmp), s);
57 mutt_sanitize_filename (tmp, 1);
58 snprintf (s, l, "%s/%s", buf, tmp);
59 if (lstat (s, &sb) == -1 && errno == ENOENT)
61 if ((period = strrchr (tmp, '.')) != NULL)
63 snprintf (s, l, "%s/%s.XXXXXX", buf, tmp);
68 m_strcpy(s + sl, l - sl, period);
73 /* create a send-mode duplicate from a receive-mode body */
75 int mutt_copy_body (FILE * fp, BODY ** tgt, BODY * src)
77 char tmp[_POSIX_PATH_MAX];
80 parameter_t *par, **ppar;
86 m_strcpy(tmp, sizeof(tmp), src->filename);
93 mutt_adv_mktemp (NULL, tmp, sizeof (tmp));
94 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
100 memcpy (b, src, sizeof (BODY));
104 b->filename = m_strdup(tmp);
105 b->use_disp = use_disp;
108 if (mutt_is_text_part (b))
111 b->xtype = m_strdup(b->xtype);
112 b->subtype = m_strdup(b->subtype);
113 b->form_name = m_strdup(b->form_name);
114 b->filename = m_strdup(b->filename);
115 b->d_filename = m_strdup(b->d_filename);
116 b->description = m_strdup(b->description);
119 * we don't seem to need the HEADER structure currently.
120 * XXX - this may change in the future
126 /* copy parameters */
127 for (par = b->parameter, ppar = &b->parameter; par;
128 ppar = &(*ppar)->next, par = par->next) {
129 *ppar = parameter_new();
130 (*ppar)->attribute = m_strdup(par->attribute);
131 (*ppar)->value = m_strdup(par->value);
134 mutt_stamp_attachment (b);
139 /* returns true if the header contained in "s" is in list "t" */
140 int mutt_matches_ignore (const char *s, string_list_t * t)
142 for (; t; t = t->next) {
143 if (!ascii_strncasecmp (s, t->data, m_strlen(t->data))
150 char *mutt_expand_path (char *s, ssize_t slen)
152 return _mutt_expand_path (s, slen, 0);
155 char *_mutt_expand_path (char *s, ssize_t slen, int rx)
157 char p[_POSIX_PATH_MAX] = "";
158 char q[_POSIX_PATH_MAX] = "";
159 char tmp[_POSIX_PATH_MAX];
162 const char *tail = "";
172 if (*(s + 1) == '/' || *(s + 1) == 0) {
173 m_strcpy(p, sizeof(p), NONULL(Homedir));
179 if ((t = strchr (s + 1, '/')))
182 if ((pw = getpwnam (s + 1))) {
183 m_strcpy(p, sizeof(p), pw->pw_dir);
192 /* user not found! */
205 /* if folder = imap[s]://host/: don't append slash */
206 if (imap_is_magic (NONULL (Maildir), NULL) == M_IMAP &&
207 Maildir[m_strlen(Maildir) - 1] == '/')
208 m_strcpy(p, sizeof(p), NONULL(Maildir));
210 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
216 /* elm compatibility, @ expands alias to user name */
224 if ((alias = alias_lookup(s + 1))) {
226 h->env = envelope_new();
227 h->env->from = h->env->to = alias;
228 mutt_default_save (p, sizeof (p), h);
229 h->env->from = h->env->to = NULL;
231 /* Avoid infinite recursion if the resulting folder starts with '@' */
242 m_strcpy(p, sizeof(p), NONULL(Inbox));
249 m_strcpy(p, sizeof(p), NONULL(Outbox));
256 if (*(s + 1) == '!') {
257 m_strcpy(p, sizeof(p), NONULL(LastFolder));
261 m_strcpy(p, sizeof(p), NONULL(Spoolfile));
269 m_strcpy(p, sizeof(p), NONULL(LastFolder));
276 m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
288 if (rx && *p && !recurse) {
289 rx_sanitize_string (q, sizeof (q), p);
290 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
293 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
295 m_strcpy(s, slen, tmp);
302 void mutt_mktemp (char *s)
305 snprintf (s, _POSIX_PATH_MAX, "%s/madmutt-%s-%d-%d-%d-%x%x", NONULL (Tempdir),
306 NONULL (Hostname), (int) getuid (), (int) getpid (), Counter++,
307 (unsigned int) rand(), (unsigned int) rand());
311 /* collapse the pathname using ~ or = when possible */
312 void mutt_pretty_mailbox (char *s)
318 scheme = url_check_scheme (s);
320 if (scheme == U_IMAP || scheme == U_IMAPS) {
321 imap_pretty_mailbox (s);
325 /* if s is an url, only collapse path component */
326 if (scheme != U_UNKNOWN) {
327 p = strchr (s, ':') + 1;
328 if (!strncmp (p, "//", 2))
329 q = strchr (p + 2, '/');
331 q = strchr (p, '\0');
335 /* first attempt to collapse the pathname */
337 if (*p == '/' && p[1] == '/') {
341 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
350 if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
353 memmove (s, s + len, m_strlen(s + len) + 1);
355 else if (m_strncmp(s, Homedir, (len = m_strlen(Homedir))) == 0 &&
358 memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
362 void mutt_pretty_size (char *s, ssize_t len, long n)
365 m_strcpy(s, len, "0K");
366 else if (n < 10189) /* 0.1K - 9.9K */
367 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
368 else if (n < 1023949) { /* 10K - 999K */
369 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
370 snprintf (s, len, "%ldK", (n + 51) / 1024);
372 else if (n < 10433332) /* 1.0M - 9.9M */
373 snprintf (s, len, "%3.1fM", n / 1048576.0);
376 /* (10433332 + 52428) / 1048576 = 10 */
377 snprintf (s, len, "%ldM", (n + 52428) / 1048576);
381 void mutt_expand_file_fmt(char *dest, ssize_t destlen,
382 const char *fmt, const char *src)
384 char tmp[LONG_STRING];
386 mutt_quote_filename(tmp, sizeof(tmp), src);
387 mutt_expand_fmt(dest, destlen, fmt, tmp);
390 void mutt_expand_fmt(char *dst, ssize_t dlen,
391 const char *fmt, const char *src)
396 while (*fmt && pos < dlen - 1) {
401 pos += m_strcpy(dst + pos, dlen - pos, src);
419 pos += m_strcpy(dst + pos, dlen - pos, " ");
420 pos += m_strcpy(dst + pos, dlen - pos, src);
424 /* return 0 on success, -1 on abort, 1 on error */
425 int mutt_check_overwrite (const char *attname, const char *path,
426 char *fname, ssize_t flen, int *append,
430 char tmp[_POSIX_PATH_MAX];
433 m_strcpy(fname, flen, path);
434 if (access (fname, F_OK) != 0)
436 if (stat (fname, &st) != 0)
438 if (S_ISDIR (st.st_mode)) {
440 switch (mutt_multi_choice
441 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
444 m_strreplace(directory, fname);
459 mutt_yesorno (_("File is a directory, save under it?"),
461 return (rc == M_NO) ? 1 : -1;
463 if (!attname || !attname[0]) {
465 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
466 M_FILE | M_CLEAR) != 0 || !tmp[0])
468 mutt_concat_path(fname, flen, path, tmp);
471 mutt_concat_path(fname, flen, path, mutt_basename(attname));
474 if (*append == 0 && access (fname, F_OK) == 0) {
475 switch (mutt_multi_choice
476 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
484 *append = M_SAVE_APPEND;
486 case 1: /* overwrite */
487 *append = M_SAVE_OVERWRITE;
494 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
496 if (a && a->mailbox) {
497 m_strcpy(d, dsize, a->mailbox);
499 if (!option(OPTSAVEADDRESS)) {
500 char *p = strpbrk(d, "%@");
510 void mutt_safe_path(char *s, ssize_t l, address_t *a)
512 mutt_save_path(s, l, a);
515 if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
521 void mutt_FormatString (char *dest, /* output buffer */
522 ssize_t destlen, /* output buffer len */
523 const char *src, /* template string */
524 format_t * callback, /* callback for processing */
525 unsigned long data, /* callback data */
527 { /* callback flags */
528 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
529 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
530 ssize_t wlen, wid, count, col, len;
533 destlen--; /* save room for the terminal \0 */
534 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
537 while (*src && wlen < destlen) {
548 flags |= M_FORMAT_OPTIONAL;
552 flags &= ~M_FORMAT_OPTIONAL;
554 /* eat the format string */
557 while (count < ssizeof (prefix) &&
558 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
567 break; /* bad format */
569 ch = *src++; /* save the character to switch on */
571 if (flags & M_FORMAT_OPTIONAL) {
573 break; /* bad format */
576 /* eat the `if' part of the string */
579 while (count < ssizeof (ifstring) && *src && *src != '?'
586 /* eat the `else' part of the string (optional) */
588 src++; /* skip the & */
591 while (count < ssizeof (elsestring) && *src && *src != '?') {
598 break; /* bad format */
600 src++; /* move past the trailing `?' */
603 /* handle generic cases first */
605 /* right justify to EOL */
606 ch = *src++; /* pad char */
607 /* calculate space left on line. if we've already written more data
608 than will fit on the line, ignore the rest of the line */
609 if (DrawFullLine || option (OPTSTATUSONTOP))
610 count = (COLS < destlen ? COLS : destlen);
612 count = ((COLS - SW) < destlen ? (COLS - SW) : destlen);
614 count -= col; /* how many columns left on this line */
615 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
618 count -= wid; /* how many chars to pad */
619 memset (wptr, ch, count);
623 if (wid + wlen > destlen)
624 len = destlen - wlen;
627 memcpy (wptr, buf, len);
630 col += mutt_strwidth (buf);
632 break; /* skip rest of input */
634 else if (ch == '|') {
639 if (destlen > wlen) {
640 count = destlen - wlen;
641 memset (wptr, ch, count);
644 break; /* skip rest of input */
650 while (ch == '_' || ch == ':') {
659 /* use callback function to handle this case */
661 callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring,
674 if ((len = m_strlen(buf)) + wlen > destlen)
675 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
677 memcpy (wptr, buf, len);
680 col += mutt_strwidth (buf);
683 else if (*src == '\\') {
712 unsigned int bar = strcspn(src, "%\\");
713 char *bar2 = p_dupstr(src, bar);
719 col += mutt_strwidth (bar2);
726 if (flags & M_FORMAT_MAKEPRINT) {
727 /* Make sure that the string is printable by changing all non-printable
728 chars to dots, or spaces for non-printable whitespace */
729 for (cp = dest; *cp; cp++)
730 if (!isprint(*cp) && !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
731 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
736 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
737 int mutt_save_confirm (const char *s, struct stat *st)
739 char tmp[_POSIX_PATH_MAX];
744 magic = mx_get_magic (s);
746 if (magic == M_POP) {
747 mutt_error _("Can't save message to POP mailbox.");
753 if (magic == M_NNTP) {
754 mutt_error _("Can't save message to newsserver.");
760 if (magic > 0 && !mx_access (s, W_OK)) {
761 if (option (OPTCONFIRMAPPEND) &&
762 (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
763 /* if we're appending to the trash, there's no point in asking */
764 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
765 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
772 if (stat (s, st) != -1) {
774 mutt_error (_("%s is not a mailbox!"), s);
784 if (errno == ENOENT) {
785 if (option (OPTCONFIRMCREATE)) {
786 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
787 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
800 CLEARLINE (LINES - 1);
804 void mutt_sleep (short s)
806 sleep(MAX(s, SleepTime));
809 /* Decrease a file's modification time by 1 second */
810 time_t mutt_decrease_mtime (const char *f, struct stat *st)
817 if (stat (f, &_st) == -1)
822 if ((mtime = st->st_mtime) == time (NULL)) {
825 utim.modtime = mtime;
832 const char *mutt_make_version (int full)
834 static char vstring[STRING];
837 snprintf (vstring, sizeof (vstring),
838 "Madmutt/%s-r%s (based on Mutt 1.5.11)",
839 MUTT_VERSION, MUTT_REVISION);
841 snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
842 MUTT_VERSION, MUTT_REVISION);
846 void mutt_free_spam_list (SPAM_LIST ** list)
854 *list = (*list)->next;
856 p_delete(&p->template);
861 int mutt_match_spam_list (const char *s, SPAM_LIST * l, char *text, int x)
863 static regmatch_t *pmatch = NULL;
864 static int nmatch = 0;
873 for (; l; l = l->next) {
874 /* If this pattern needs more matches, expand pmatch. */
875 if (l->nmatch > nmatch) {
876 p_realloc(&pmatch, l->nmatch);
880 /* Does this pattern match? */
881 if (regexec(l->rx->rx, s, l->nmatch, (regmatch_t *)pmatch, (int) 0) == 0)
883 /* Copy template into text, with substitutions. */
884 for (p = l->template; *p;) {
886 n = atoi (++p); /* find pmatch index */
887 while (isdigit ((unsigned char) *p))
888 ++p; /* skip subst token */
889 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
904 /* return 1 if address lists are strictly identical */
905 static int mutt_cmp_addr (const address_t * a, const address_t * b)
908 if (m_strcmp(a->mailbox, b->mailbox) ||
909 m_strcmp(a->personal, b->personal))
921 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
924 if (m_strcmp(a->data, b->data))
936 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
939 if (m_strcmp(e1->message_id, e2->message_id) ||
940 m_strcmp(e1->subject, e2->subject) ||
941 !mutt_cmp_list (e1->references, e2->references) ||
942 !mutt_cmp_addr (e1->from, e2->from) ||
943 !mutt_cmp_addr (e1->sender, e2->sender) ||
944 !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
945 !mutt_cmp_addr (e1->to, e2->to) ||
946 !mutt_cmp_addr (e1->cc, e2->cc) ||
947 !mutt_cmp_addr (e1->return_path, e2->return_path))
953 if (e1 == NULL && e2 == NULL)
960 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
962 if (b1->type != b2->type ||
963 b1->encoding != b2->encoding ||
964 m_strcmp(b1->subtype, b2->subtype) ||
965 m_strcmp(b1->description, b2->description) ||
966 !parameter_equal(b1->parameter, b2->parameter) ||
967 b1->length != b2->length)
971 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
973 if (h1->received != h2->received ||
974 h1->date_sent != h2->date_sent ||
975 h1->content->length != h2->content->length ||
976 h1->lines != h2->lines ||
977 h1->zhours != h2->zhours ||
978 h1->zminutes != h2->zminutes ||
979 h1->zoccident != h2->zoccident ||
980 h1->mime != h2->mime ||
981 !mutt_cmp_env (h1->env, h2->env) ||
982 !mutt_cmp_body (h1->content, h2->content))
988 if (h1 == NULL && h2 == NULL)
996 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
999 char qc = 0; /* quote char */
1002 /* reset the destination pointer to the beginning of the buffer */
1003 dest->dptr = dest->data;
1005 tok->dptr = vskipspaces(tok->dptr);
1006 while ((ch = *tok->dptr)) {
1008 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
1009 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
1010 || (ch == '=' && (flags & M_TOKEN_EQUAL))
1011 || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
1012 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
1021 qc = 0; /* end of quote */
1023 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
1026 if (ch == '\\' && qc != '\'') {
1028 return -1; /* premature end of token */
1030 switch (ch = *tok->dptr++) {
1034 return -1; /* premature end of token */
1035 mutt_buffer_addch(dest,
1036 (ascii_toupper(*tok->dptr) - 'A' + 1) & 0x7f);
1040 mutt_buffer_addch(dest, '\r');
1043 mutt_buffer_addch(dest, '\n');
1046 mutt_buffer_addch(dest, '\t');
1049 mutt_buffer_addch(dest, '\f');
1052 mutt_buffer_addch(dest, '\033');
1055 if (isdigit((unsigned char)ch)
1056 && isdigit((unsigned char)*tok->dptr)
1057 && isdigit((unsigned char)*(tok->dptr + 1)))
1059 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
1060 *(tok->dptr + 1) - 3504);
1063 mutt_buffer_addch(dest, ch);
1067 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
1069 return -1; /* premature end of token */
1072 mutt_buffer_addch(dest, ch);
1075 mutt_buffer_addch(dest, '\033');
1077 if (isalpha((unsigned char)ch)) {
1078 mutt_buffer_addch(dest, ascii_toupper(ch) - 'A' + 1);
1080 mutt_buffer_addch(dest, '^');
1081 mutt_buffer_addch(dest, ch);
1084 if (ch == '`' && (!qc || qc == '"')) {
1094 if ((pc = strpbrk(pc, "\\`"))) {
1095 /* skip any quoted chars */
1099 } while (pc && *pc != '`');
1104 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
1105 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
1115 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
1117 mutt_wait_filter(pid);
1119 /* if we got output, make a new string consiting of the shell ouptput
1120 plus whatever else was left on the original line */
1121 /* BUT: If this is inside a quoted string, directly add output to
1123 if (expn.data && qc) {
1124 mutt_buffer_addstr(dest, expn.data);
1125 p_delete(&expn.data);
1128 expnlen = m_strlen(expn.data);
1129 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
1130 ptr = xmalloc(tok->dsize);
1131 memcpy(ptr, expn.data, expnlen);
1132 strcpy(ptr + expnlen, tok->dptr); /* __STRCPY_CHECKED__ */
1134 p_delete(&tok->data);
1137 tok->destroy = 1; /* mark that the caller should destroy this data */
1139 p_delete(&expn.data);
1142 if (ch == '$' && (!qc || qc == '"')
1143 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
1145 char *env = NULL, *var = NULL;
1147 if (*tok->dptr == '{') {
1149 if ((pc = strchr (tok->dptr, '}'))) {
1150 var = p_dupstr(tok->dptr, pc - tok->dptr);
1154 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
1156 var = p_dupstr(tok->dptr, pc - tok->dptr);
1161 if ((env = getenv (var))
1162 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
1164 mutt_buffer_addstr (dest, env);
1169 mutt_buffer_addch(dest, ch);
1172 mutt_buffer_addch(dest, 0); /* terminate the string */
1173 tok->dptr = vskipspaces(tok->dptr);