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>
19 #include <lib-ui/curses.h>
20 #include <lib-ui/enter.h>
22 #include <lib-sys/unix.h>
31 #include <imap/imap.h>
32 #include <imap/mx_imap.h>
34 #include <lib-crypt/crypt.h>
36 #define SW (option(OPTMBOXPANE)?SidebarWidth:0)
38 /* Modified by blong to accept a "suggestion" for file name. If
39 * that file exists, then construct one with unique name but
40 * keep any extension. This might fail, I guess.
41 * Renamed to mutt_adv_mktemp so I only have to change where it's
42 * called, and not all possible cases.
44 void mutt_adv_mktemp (const char* dir, char *s, ssize_t l)
46 char buf[_POSIX_PATH_MAX];
47 char tmp[_POSIX_PATH_MAX];
52 m_strcpy(buf, sizeof(buf), m_strisempty(dir) ? NONULL(Tempdir) : dir);
53 mutt_expand_path (buf, sizeof (buf));
55 snprintf (s, l, "%s/muttXXXXXX", buf);
59 m_strcpy(tmp, sizeof(tmp), s);
60 mutt_sanitize_filename (tmp, 1);
61 snprintf (s, l, "%s/%s", buf, tmp);
62 if (lstat (s, &sb) == -1 && errno == ENOENT)
64 if ((period = strrchr (tmp, '.')) != NULL)
66 snprintf (s, l, "%s/%s.XXXXXX", buf, tmp);
71 m_strcpy(s + sl, l - sl, period);
76 /* create a send-mode duplicate from a receive-mode body */
78 int mutt_copy_body (FILE * fp, BODY ** tgt, BODY * src)
80 char tmp[_POSIX_PATH_MAX];
83 parameter_t *par, **ppar;
89 m_strcpy(tmp, sizeof(tmp), src->filename);
96 mutt_adv_mktemp (NULL, tmp, sizeof (tmp));
97 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
103 memcpy (b, src, sizeof (BODY));
107 b->filename = m_strdup(tmp);
108 b->use_disp = use_disp;
111 if (mutt_is_text_part (b))
114 b->xtype = m_strdup(b->xtype);
115 b->subtype = m_strdup(b->subtype);
116 b->form_name = m_strdup(b->form_name);
117 b->filename = m_strdup(b->filename);
118 b->d_filename = m_strdup(b->d_filename);
119 b->description = m_strdup(b->description);
122 * we don't seem to need the HEADER structure currently.
123 * XXX - this may change in the future
129 /* copy parameters */
130 for (par = b->parameter, ppar = &b->parameter; par;
131 ppar = &(*ppar)->next, par = par->next) {
132 *ppar = parameter_new();
133 (*ppar)->attribute = m_strdup(par->attribute);
134 (*ppar)->value = m_strdup(par->value);
137 mutt_stamp_attachment (b);
142 /* returns true if the header contained in "s" is in list "t" */
143 int mutt_matches_ignore (const char *s, string_list_t * t)
145 for (; t; t = t->next) {
146 if (!ascii_strncasecmp (s, t->data, m_strlen(t->data))
153 char *mutt_expand_path (char *s, ssize_t slen)
155 return _mutt_expand_path (s, slen, 0);
158 char *_mutt_expand_path (char *s, ssize_t slen, int rx)
160 char p[_POSIX_PATH_MAX] = "";
161 char q[_POSIX_PATH_MAX] = "";
162 char tmp[_POSIX_PATH_MAX];
165 const char *tail = "";
175 if (*(s + 1) == '/' || *(s + 1) == 0) {
176 m_strcpy(p, sizeof(p), NONULL(Homedir));
182 if ((t = strchr (s + 1, '/')))
185 if ((pw = getpwnam (s + 1))) {
186 m_strcpy(p, sizeof(p), pw->pw_dir);
195 /* user not found! */
208 /* if folder = imap[s]://host/: don't append slash */
209 if (imap_is_magic (NONULL (Maildir), NULL) == M_IMAP &&
210 Maildir[m_strlen(Maildir) - 1] == '/')
211 m_strcpy(p, sizeof(p), NONULL(Maildir));
213 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
219 /* elm compatibility, @ expands alias to user name */
227 if ((alias = alias_lookup(s + 1))) {
229 h->env = envelope_new();
230 h->env->from = h->env->to = alias;
231 mutt_default_save (p, sizeof (p), h);
232 h->env->from = h->env->to = NULL;
234 /* Avoid infinite recursion if the resulting folder starts with '@' */
245 m_strcpy(p, sizeof(p), NONULL(Inbox));
252 m_strcpy(p, sizeof(p), NONULL(Outbox));
259 if (*(s + 1) == '!') {
260 m_strcpy(p, sizeof(p), NONULL(LastFolder));
264 m_strcpy(p, sizeof(p), NONULL(Spoolfile));
272 m_strcpy(p, sizeof(p), NONULL(LastFolder));
279 m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
291 if (rx && *p && !recurse) {
292 rx_sanitize_string (q, sizeof (q), p);
293 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
296 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
298 m_strcpy(s, slen, tmp);
305 void mutt_mktemp (char *s)
308 snprintf (s, _POSIX_PATH_MAX, "%s/madmutt-%s-%d-%d-%d-%x%x", NONULL (Tempdir),
309 NONULL (Hostname), (int) getuid (), (int) getpid (), Counter++,
310 (unsigned int) rand(), (unsigned int) rand());
314 /* collapse the pathname using ~ or = when possible */
315 void mutt_pretty_mailbox (char *s)
321 scheme = url_check_scheme (s);
323 if (scheme == U_IMAP || scheme == U_IMAPS) {
324 imap_pretty_mailbox (s);
328 /* if s is an url, only collapse path component */
329 if (scheme != U_UNKNOWN) {
330 p = strchr (s, ':') + 1;
331 if (!strncmp (p, "//", 2))
332 q = strchr (p + 2, '/');
334 q = strchr (p, '\0');
338 /* first attempt to collapse the pathname */
340 if (*p == '/' && p[1] == '/') {
344 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
353 if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
356 memmove (s, s + len, m_strlen(s + len) + 1);
358 else if (m_strncmp(s, Homedir, (len = m_strlen(Homedir))) == 0 &&
361 memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
365 void mutt_pretty_size (char *s, ssize_t len, long n)
368 m_strcpy(s, len, "0K");
369 else if (n < 10189) /* 0.1K - 9.9K */
370 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
371 else if (n < 1023949) { /* 10K - 999K */
372 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
373 snprintf (s, len, "%ldK", (n + 51) / 1024);
375 else if (n < 10433332) /* 1.0M - 9.9M */
376 snprintf (s, len, "%3.1fM", n / 1048576.0);
379 /* (10433332 + 52428) / 1048576 = 10 */
380 snprintf (s, len, "%ldM", (n + 52428) / 1048576);
384 void mutt_expand_file_fmt(char *dest, ssize_t destlen,
385 const char *fmt, const char *src)
387 char tmp[LONG_STRING];
389 mutt_quote_filename(tmp, sizeof(tmp), src);
390 mutt_expand_fmt(dest, destlen, fmt, tmp);
393 void mutt_expand_fmt(char *dst, ssize_t dlen,
394 const char *fmt, const char *src)
399 while (*fmt && pos < dlen - 1) {
404 pos += m_strcpy(dst + pos, dlen - pos, src);
422 pos += m_strcpy(dst + pos, dlen - pos, " ");
423 pos += m_strcpy(dst + pos, dlen - pos, src);
427 /* return 0 on success, -1 on abort, 1 on error */
428 int mutt_check_overwrite (const char *attname, const char *path,
429 char *fname, ssize_t flen, int *append,
433 char tmp[_POSIX_PATH_MAX];
436 m_strcpy(fname, flen, path);
437 if (access (fname, F_OK) != 0)
439 if (stat (fname, &st) != 0)
441 if (S_ISDIR (st.st_mode)) {
443 switch (mutt_multi_choice
444 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
447 m_strreplace(directory, fname);
462 mutt_yesorno (_("File is a directory, save under it?"),
464 return (rc == M_NO) ? 1 : -1;
466 if (!attname || !attname[0]) {
468 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
469 M_FILE | M_CLEAR) != 0 || !tmp[0])
471 mutt_concat_path(fname, flen, path, tmp);
474 mutt_concat_path(fname, flen, path, mutt_basename(attname));
477 if (*append == 0 && access (fname, F_OK) == 0) {
478 switch (mutt_multi_choice
479 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
487 *append = M_SAVE_APPEND;
489 case 1: /* overwrite */
490 *append = M_SAVE_OVERWRITE;
497 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
499 if (a && a->mailbox) {
500 m_strcpy(d, dsize, a->mailbox);
502 if (!option(OPTSAVEADDRESS)) {
503 char *p = strpbrk(d, "%@");
513 void mutt_safe_path(char *s, ssize_t l, address_t *a)
515 mutt_save_path(s, l, a);
518 if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
524 void mutt_FormatString (char *dest, /* output buffer */
525 ssize_t destlen, /* output buffer len */
526 const char *src, /* template string */
527 format_t * callback, /* callback for processing */
528 unsigned long data, /* callback data */
530 { /* callback flags */
531 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
532 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
533 ssize_t wlen, wid, count, col, len;
536 destlen--; /* save room for the terminal \0 */
537 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
540 while (*src && wlen < destlen) {
551 flags |= M_FORMAT_OPTIONAL;
555 flags &= ~M_FORMAT_OPTIONAL;
557 /* eat the format string */
560 while (count < ssizeof (prefix) &&
561 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
570 break; /* bad format */
572 ch = *src++; /* save the character to switch on */
574 if (flags & M_FORMAT_OPTIONAL) {
576 break; /* bad format */
579 /* eat the `if' part of the string */
582 while (count < ssizeof (ifstring) && *src && *src != '?'
589 /* eat the `else' part of the string (optional) */
591 src++; /* skip the & */
594 while (count < ssizeof (elsestring) && *src && *src != '?') {
601 break; /* bad format */
603 src++; /* move past the trailing `?' */
606 /* handle generic cases first */
608 /* right justify to EOL */
609 ch = *src++; /* pad char */
610 /* calculate space left on line. if we've already written more data
611 than will fit on the line, ignore the rest of the line */
612 if (DrawFullLine || option (OPTSTATUSONTOP))
613 count = (COLS < destlen ? COLS : destlen);
615 count = ((COLS - SW) < destlen ? (COLS - SW) : destlen);
617 count -= col; /* how many columns left on this line */
618 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
621 count -= wid; /* how many chars to pad */
622 memset (wptr, ch, count);
626 if (wid + wlen > destlen)
627 len = destlen - wlen;
630 memcpy (wptr, buf, len);
633 col += mutt_strwidth (buf);
635 break; /* skip rest of input */
637 else if (ch == '|') {
642 if (destlen > wlen) {
643 count = destlen - wlen;
644 memset (wptr, ch, count);
647 break; /* skip rest of input */
653 while (ch == '_' || ch == ':') {
662 /* use callback function to handle this case */
664 callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring,
677 if ((len = m_strlen(buf)) + wlen > destlen)
678 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
680 memcpy (wptr, buf, len);
683 col += mutt_strwidth (buf);
686 else if (*src == '\\') {
715 unsigned int bar = strcspn(src, "%\\");
716 char *bar2 = p_dupstr(src, bar);
722 col += mutt_strwidth (bar2);
729 if (flags & M_FORMAT_MAKEPRINT) {
730 /* Make sure that the string is printable by changing all non-printable
731 chars to dots, or spaces for non-printable whitespace */
732 for (cp = dest; *cp; cp++)
733 if (!isprint(*cp) && !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
734 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
739 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
740 int mutt_save_confirm (const char *s, struct stat *st)
742 char tmp[_POSIX_PATH_MAX];
747 magic = mx_get_magic (s);
749 if (magic == M_POP) {
750 mutt_error _("Can't save message to POP mailbox.");
756 if (magic == M_NNTP) {
757 mutt_error _("Can't save message to newsserver.");
763 if (magic > 0 && !mx_access (s, W_OK)) {
764 if (option (OPTCONFIRMAPPEND) &&
765 (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
766 /* if we're appending to the trash, there's no point in asking */
767 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
768 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
775 if (stat (s, st) != -1) {
777 mutt_error (_("%s is not a mailbox!"), s);
787 if (errno == ENOENT) {
788 if (option (OPTCONFIRMCREATE)) {
789 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
790 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
803 CLEARLINE (LINES - 1);
807 void mutt_sleep (short s)
809 sleep(MAX(s, SleepTime));
812 /* Decrease a file's modification time by 1 second */
813 time_t mutt_decrease_mtime (const char *f, struct stat *st)
820 if (stat (f, &_st) == -1)
825 if ((mtime = st->st_mtime) == time (NULL)) {
828 utim.modtime = mtime;
835 const char *mutt_make_version (int full)
837 static char vstring[STRING];
840 snprintf (vstring, sizeof (vstring),
841 "Madmutt/%s-r%s (based on Mutt 1.5.11)",
842 MUTT_VERSION, MUTT_REVISION);
844 snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
845 MUTT_VERSION, MUTT_REVISION);
849 void mutt_free_spam_list (SPAM_LIST ** list)
857 *list = (*list)->next;
859 p_delete(&p->template);
864 int mutt_match_spam_list (const char *s, SPAM_LIST * l, char *text, int x)
866 static regmatch_t *pmatch = NULL;
867 static int nmatch = 0;
876 for (; l; l = l->next) {
877 /* If this pattern needs more matches, expand pmatch. */
878 if (l->nmatch > nmatch) {
879 p_realloc(&pmatch, l->nmatch);
883 /* Does this pattern match? */
884 if (regexec(l->rx->rx, s, l->nmatch, (regmatch_t *)pmatch, (int) 0) == 0)
886 /* Copy template into text, with substitutions. */
887 for (p = l->template; *p;) {
889 n = atoi (++p); /* find pmatch index */
890 while (isdigit ((unsigned char) *p))
891 ++p; /* skip subst token */
892 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
907 /* return 1 if address lists are strictly identical */
908 static int mutt_cmp_addr (const address_t * a, const address_t * b)
911 if (m_strcmp(a->mailbox, b->mailbox) ||
912 m_strcmp(a->personal, b->personal))
924 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
927 if (m_strcmp(a->data, b->data))
939 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
942 if (m_strcmp(e1->message_id, e2->message_id) ||
943 m_strcmp(e1->subject, e2->subject) ||
944 !mutt_cmp_list (e1->references, e2->references) ||
945 !mutt_cmp_addr (e1->from, e2->from) ||
946 !mutt_cmp_addr (e1->sender, e2->sender) ||
947 !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
948 !mutt_cmp_addr (e1->to, e2->to) ||
949 !mutt_cmp_addr (e1->cc, e2->cc) ||
950 !mutt_cmp_addr (e1->return_path, e2->return_path))
956 if (e1 == NULL && e2 == NULL)
963 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
965 if (b1->type != b2->type ||
966 b1->encoding != b2->encoding ||
967 m_strcmp(b1->subtype, b2->subtype) ||
968 m_strcmp(b1->description, b2->description) ||
969 !parameter_equal(b1->parameter, b2->parameter) ||
970 b1->length != b2->length)
974 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
976 if (h1->received != h2->received ||
977 h1->date_sent != h2->date_sent ||
978 h1->content->length != h2->content->length ||
979 h1->lines != h2->lines ||
980 h1->zhours != h2->zhours ||
981 h1->zminutes != h2->zminutes ||
982 h1->zoccident != h2->zoccident ||
983 h1->mime != h2->mime ||
984 !mutt_cmp_env (h1->env, h2->env) ||
985 !mutt_cmp_body (h1->content, h2->content))
991 if (h1 == NULL && h2 == NULL)
999 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
1002 char qc = 0; /* quote char */
1005 /* reset the destination pointer to the beginning of the buffer */
1006 dest->dptr = dest->data;
1008 tok->dptr = vskipspaces(tok->dptr);
1009 while ((ch = *tok->dptr)) {
1011 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
1012 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
1013 || (ch == '=' && (flags & M_TOKEN_EQUAL))
1014 || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
1015 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
1024 qc = 0; /* end of quote */
1026 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
1029 if (ch == '\\' && qc != '\'') {
1031 return -1; /* premature end of token */
1033 switch (ch = *tok->dptr++) {
1037 return -1; /* premature end of token */
1038 mutt_buffer_addch(dest,
1039 (ascii_toupper(*tok->dptr) - 'A' + 1) & 0x7f);
1043 mutt_buffer_addch(dest, '\r');
1046 mutt_buffer_addch(dest, '\n');
1049 mutt_buffer_addch(dest, '\t');
1052 mutt_buffer_addch(dest, '\f');
1055 mutt_buffer_addch(dest, '\033');
1058 if (isdigit((unsigned char)ch)
1059 && isdigit((unsigned char)*tok->dptr)
1060 && isdigit((unsigned char)*(tok->dptr + 1)))
1062 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
1063 *(tok->dptr + 1) - 3504);
1066 mutt_buffer_addch(dest, ch);
1070 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
1072 return -1; /* premature end of token */
1075 mutt_buffer_addch(dest, ch);
1078 mutt_buffer_addch(dest, '\033');
1080 if (isalpha((unsigned char)ch)) {
1081 mutt_buffer_addch(dest, ascii_toupper(ch) - 'A' + 1);
1083 mutt_buffer_addch(dest, '^');
1084 mutt_buffer_addch(dest, ch);
1087 if (ch == '`' && (!qc || qc == '"')) {
1097 if ((pc = strpbrk(pc, "\\`"))) {
1098 /* skip any quoted chars */
1102 } while (pc && *pc != '`');
1107 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
1108 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
1118 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
1120 mutt_wait_filter(pid);
1122 /* if we got output, make a new string consiting of the shell ouptput
1123 plus whatever else was left on the original line */
1124 /* BUT: If this is inside a quoted string, directly add output to
1126 if (expn.data && qc) {
1127 mutt_buffer_addstr(dest, expn.data);
1128 p_delete(&expn.data);
1131 expnlen = m_strlen(expn.data);
1132 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
1133 ptr = xmalloc(tok->dsize);
1134 memcpy(ptr, expn.data, expnlen);
1135 strcpy(ptr + expnlen, tok->dptr); /* __STRCPY_CHECKED__ */
1137 p_delete(&tok->data);
1140 tok->destroy = 1; /* mark that the caller should destroy this data */
1142 p_delete(&expn.data);
1145 if (ch == '$' && (!qc || qc == '"')
1146 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
1148 char *env = NULL, *var = NULL;
1150 if (*tok->dptr == '{') {
1152 if ((pc = strchr (tok->dptr, '}'))) {
1153 var = p_dupstr(tok->dptr, pc - tok->dptr);
1157 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
1159 var = p_dupstr(tok->dptr, pc - tok->dptr);
1164 if ((env = getenv (var))
1165 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
1167 mutt_buffer_addstr (dest, env);
1172 mutt_buffer_addch(dest, ch);
1175 mutt_buffer_addch(dest, 0); /* terminate the string */
1176 tok->dptr = vskipspaces(tok->dptr);