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>
33 #include <lib-crypt/crypt.h>
35 #define SW (option(OPTMBOXPANE)?SidebarWidth:0)
37 /* Modified by blong to accept a "suggestion" for file name. If
38 * that file exists, then construct one with unique name but
39 * keep any extension. This might fail, I guess.
40 * Renamed to mutt_adv_mktemp so I only have to change where it's
41 * called, and not all possible cases.
43 void mutt_adv_mktemp (const char* dir, char *s, ssize_t l)
45 char buf[_POSIX_PATH_MAX];
46 char tmp[_POSIX_PATH_MAX];
51 m_strcpy(buf, sizeof(buf), m_strisempty(dir) ? NONULL(Tempdir) : dir);
52 mutt_expand_path (buf, sizeof (buf));
54 snprintf (s, l, "%s/muttXXXXXX", buf);
58 m_strcpy(tmp, sizeof(tmp), s);
59 mutt_sanitize_filename (tmp, 1);
60 snprintf (s, l, "%s/%s", buf, tmp);
61 if (lstat (s, &sb) == -1 && errno == ENOENT)
63 if ((period = strrchr (tmp, '.')) != NULL)
65 snprintf (s, l, "%s/%s.XXXXXX", buf, tmp);
70 m_strcpy(s + sl, l - sl, period);
75 /* create a send-mode duplicate from a receive-mode body */
77 int mutt_copy_body (FILE * fp, BODY ** tgt, BODY * src)
79 char tmp[_POSIX_PATH_MAX];
82 parameter_t *par, **ppar;
88 m_strcpy(tmp, sizeof(tmp), src->filename);
95 mutt_adv_mktemp (NULL, tmp, sizeof (tmp));
96 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
102 memcpy (b, src, sizeof (BODY));
106 b->filename = m_strdup(tmp);
107 b->use_disp = use_disp;
110 if (mutt_is_text_part (b))
113 b->xtype = m_strdup(b->xtype);
114 b->subtype = m_strdup(b->subtype);
115 b->form_name = m_strdup(b->form_name);
116 b->filename = m_strdup(b->filename);
117 b->d_filename = m_strdup(b->d_filename);
118 b->description = m_strdup(b->description);
121 * we don't seem to need the HEADER structure currently.
122 * XXX - this may change in the future
128 /* copy parameters */
129 for (par = b->parameter, ppar = &b->parameter; par;
130 ppar = &(*ppar)->next, par = par->next) {
131 *ppar = parameter_new();
132 (*ppar)->attribute = m_strdup(par->attribute);
133 (*ppar)->value = m_strdup(par->value);
136 mutt_stamp_attachment (b);
141 /* returns true if the header contained in "s" is in list "t" */
142 int mutt_matches_ignore (const char *s, string_list_t * t)
144 for (; t; t = t->next) {
145 if (!ascii_strncasecmp (s, t->data, m_strlen(t->data))
152 char *mutt_expand_path (char *s, ssize_t slen)
154 return _mutt_expand_path (s, slen, 0);
157 char *_mutt_expand_path (char *s, ssize_t slen, int rx)
159 char p[_POSIX_PATH_MAX] = "";
160 char q[_POSIX_PATH_MAX] = "";
161 char tmp[_POSIX_PATH_MAX];
164 const char *tail = "";
174 if (*(s + 1) == '/' || *(s + 1) == 0) {
175 m_strcpy(p, sizeof(p), NONULL(Homedir));
181 if ((t = strchr (s + 1, '/')))
184 if ((pw = getpwnam (s + 1))) {
185 m_strcpy(p, sizeof(p), pw->pw_dir);
194 /* user not found! */
207 /* if folder = imap[s]://host/: don't append slash */
208 if (imap_is_magic (NONULL (Maildir), NULL) == M_IMAP &&
209 Maildir[m_strlen(Maildir) - 1] == '/')
210 m_strcpy(p, sizeof(p), NONULL(Maildir));
212 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
218 /* elm compatibility, @ expands alias to user name */
226 if ((alias = alias_lookup(s + 1))) {
228 h->env = envelope_new();
229 h->env->from = h->env->to = alias;
230 mutt_default_save (p, sizeof (p), h);
231 h->env->from = h->env->to = NULL;
233 /* Avoid infinite recursion if the resulting folder starts with '@' */
244 m_strcpy(p, sizeof(p), NONULL(Inbox));
251 m_strcpy(p, sizeof(p), NONULL(Outbox));
258 if (*(s + 1) == '!') {
259 m_strcpy(p, sizeof(p), NONULL(LastFolder));
263 m_strcpy(p, sizeof(p), NONULL(Spoolfile));
271 m_strcpy(p, sizeof(p), NONULL(LastFolder));
278 m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
290 if (rx && *p && !recurse) {
291 rx_sanitize_string (q, sizeof (q), p);
292 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
295 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
297 m_strcpy(s, slen, tmp);
304 void mutt_mktemp (char *s)
307 snprintf (s, _POSIX_PATH_MAX, "%s/madmutt-%s-%d-%d-%d-%x%x", NONULL (Tempdir),
308 NONULL (Hostname), (int) getuid (), (int) getpid (), Counter++,
309 (unsigned int) rand(), (unsigned int) rand());
313 /* collapse the pathname using ~ or = when possible */
314 void mutt_pretty_mailbox (char *s)
320 scheme = url_check_scheme (s);
322 if (scheme == U_IMAP || scheme == U_IMAPS) {
323 imap_pretty_mailbox (s);
327 /* if s is an url, only collapse path component */
328 if (scheme != U_UNKNOWN) {
329 p = strchr (s, ':') + 1;
330 if (!strncmp (p, "//", 2))
331 q = strchr (p + 2, '/');
333 q = strchr (p, '\0');
337 /* first attempt to collapse the pathname */
339 if (*p == '/' && p[1] == '/') {
343 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
352 if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
355 memmove (s, s + len, m_strlen(s + len) + 1);
357 else if (m_strncmp(s, Homedir, (len = m_strlen(Homedir))) == 0 &&
360 memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
364 void mutt_pretty_size (char *s, ssize_t len, long n)
367 m_strcpy(s, len, "0K");
368 else if (n < 10189) /* 0.1K - 9.9K */
369 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
370 else if (n < 1023949) { /* 10K - 999K */
371 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
372 snprintf (s, len, "%ldK", (n + 51) / 1024);
374 else if (n < 10433332) /* 1.0M - 9.9M */
375 snprintf (s, len, "%3.1fM", n / 1048576.0);
378 /* (10433332 + 52428) / 1048576 = 10 */
379 snprintf (s, len, "%ldM", (n + 52428) / 1048576);
383 void mutt_expand_file_fmt(char *dest, ssize_t destlen,
384 const char *fmt, const char *src)
386 char tmp[LONG_STRING];
388 mutt_quote_filename(tmp, sizeof(tmp), src);
389 mutt_expand_fmt(dest, destlen, fmt, tmp);
392 void mutt_expand_fmt(char *dst, ssize_t dlen,
393 const char *fmt, const char *src)
398 while (*fmt && pos < dlen - 1) {
403 pos += m_strcpy(dst + pos, dlen - pos, src);
421 pos += m_strcpy(dst + pos, dlen - pos, " ");
422 pos += m_strcpy(dst + pos, dlen - pos, src);
426 /* return 0 on success, -1 on abort, 1 on error */
427 int mutt_check_overwrite (const char *attname, const char *path,
428 char *fname, ssize_t flen, int *append,
432 char tmp[_POSIX_PATH_MAX];
435 m_strcpy(fname, flen, path);
436 if (access (fname, F_OK) != 0)
438 if (stat (fname, &st) != 0)
440 if (S_ISDIR (st.st_mode)) {
442 switch (mutt_multi_choice
443 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
446 m_strreplace(directory, fname);
461 mutt_yesorno (_("File is a directory, save under it?"),
463 return (rc == M_NO) ? 1 : -1;
465 if (!attname || !attname[0]) {
467 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
468 M_FILE | M_CLEAR) != 0 || !tmp[0])
470 mutt_concat_path(fname, flen, path, tmp);
473 mutt_concat_path(fname, flen, path, mutt_basename(attname));
476 if (*append == 0 && access (fname, F_OK) == 0) {
477 switch (mutt_multi_choice
478 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
486 *append = M_SAVE_APPEND;
488 case 1: /* overwrite */
489 *append = M_SAVE_OVERWRITE;
496 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
498 if (a && a->mailbox) {
499 m_strcpy(d, dsize, a->mailbox);
501 if (!option(OPTSAVEADDRESS)) {
502 char *p = strpbrk(d, "%@");
512 void mutt_safe_path(char *s, ssize_t l, address_t *a)
514 mutt_save_path(s, l, a);
517 if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
523 void mutt_FormatString (char *dest, /* output buffer */
524 ssize_t destlen, /* output buffer len */
525 const char *src, /* template string */
526 format_t * callback, /* callback for processing */
527 unsigned long data, /* callback data */
529 { /* callback flags */
530 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
531 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
532 ssize_t wlen, wid, count, col, len;
535 destlen--; /* save room for the terminal \0 */
536 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
539 while (*src && wlen < destlen) {
550 flags |= M_FORMAT_OPTIONAL;
554 flags &= ~M_FORMAT_OPTIONAL;
556 /* eat the format string */
559 while (count < ssizeof (prefix) &&
560 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
569 break; /* bad format */
571 ch = *src++; /* save the character to switch on */
573 if (flags & M_FORMAT_OPTIONAL) {
575 break; /* bad format */
578 /* eat the `if' part of the string */
581 while (count < ssizeof (ifstring) && *src && *src != '?'
588 /* eat the `else' part of the string (optional) */
590 src++; /* skip the & */
593 while (count < ssizeof (elsestring) && *src && *src != '?') {
600 break; /* bad format */
602 src++; /* move past the trailing `?' */
605 /* handle generic cases first */
607 /* right justify to EOL */
608 ch = *src++; /* pad char */
609 /* calculate space left on line. if we've already written more data
610 than will fit on the line, ignore the rest of the line */
611 if (DrawFullLine || option (OPTSTATUSONTOP))
612 count = (COLS < destlen ? COLS : destlen);
614 count = ((COLS - SW) < destlen ? (COLS - SW) : destlen);
616 count -= col; /* how many columns left on this line */
617 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
620 count -= wid; /* how many chars to pad */
621 memset (wptr, ch, count);
625 if (wid + wlen > destlen)
626 len = destlen - wlen;
629 memcpy (wptr, buf, len);
632 col += mutt_strwidth (buf);
634 break; /* skip rest of input */
636 else if (ch == '|') {
641 if (destlen > wlen) {
642 count = destlen - wlen;
643 memset (wptr, ch, count);
646 break; /* skip rest of input */
652 while (ch == '_' || ch == ':') {
661 /* use callback function to handle this case */
663 callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring,
676 if ((len = m_strlen(buf)) + wlen > destlen)
677 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
679 memcpy (wptr, buf, len);
682 col += mutt_strwidth (buf);
685 else if (*src == '\\') {
714 unsigned int bar = strcspn(src, "%\\");
715 char *bar2 = p_dupstr(src, bar);
721 col += mutt_strwidth (bar2);
728 if (flags & M_FORMAT_MAKEPRINT) {
729 /* Make sure that the string is printable by changing all non-printable
730 chars to dots, or spaces for non-printable whitespace */
731 for (cp = dest; *cp; cp++)
732 if (!isprint(*cp) && !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
733 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
738 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
739 int mutt_save_confirm (const char *s, struct stat *st)
741 char tmp[_POSIX_PATH_MAX];
746 magic = mx_get_magic (s);
748 if (magic == M_POP) {
749 mutt_error _("Can't save message to POP mailbox.");
755 if (magic == M_NNTP) {
756 mutt_error _("Can't save message to newsserver.");
762 if (magic > 0 && !mx_access (s, W_OK)) {
763 if (option (OPTCONFIRMAPPEND) &&
764 (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
765 /* if we're appending to the trash, there's no point in asking */
766 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
767 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
774 if (stat (s, st) != -1) {
776 mutt_error (_("%s is not a mailbox!"), s);
786 if (errno == ENOENT) {
787 if (option (OPTCONFIRMCREATE)) {
788 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
789 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
802 CLEARLINE (LINES - 1);
806 void mutt_sleep (short s)
808 sleep(MAX(s, SleepTime));
811 /* Decrease a file's modification time by 1 second */
812 time_t mutt_decrease_mtime (const char *f, struct stat *st)
819 if (stat (f, &_st) == -1)
824 if ((mtime = st->st_mtime) == time (NULL)) {
827 utim.modtime = mtime;
834 const char *mutt_make_version (int full)
836 static char vstring[STRING];
839 snprintf (vstring, sizeof (vstring),
840 "Madmutt/%s-r%s (based on Mutt 1.5.11)",
841 MUTT_VERSION, MUTT_REVISION);
843 snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
844 MUTT_VERSION, MUTT_REVISION);
848 void mutt_free_spam_list (SPAM_LIST ** list)
856 *list = (*list)->next;
858 p_delete(&p->template);
863 int mutt_match_spam_list (const char *s, SPAM_LIST * l, char *text, int x)
865 static regmatch_t *pmatch = NULL;
866 static int nmatch = 0;
875 for (; l; l = l->next) {
876 /* If this pattern needs more matches, expand pmatch. */
877 if (l->nmatch > nmatch) {
878 p_realloc(&pmatch, l->nmatch);
882 /* Does this pattern match? */
883 if (regexec(l->rx->rx, s, l->nmatch, (regmatch_t *)pmatch, (int) 0) == 0)
885 /* Copy template into text, with substitutions. */
886 for (p = l->template; *p;) {
888 n = atoi (++p); /* find pmatch index */
889 while (isdigit ((unsigned char) *p))
890 ++p; /* skip subst token */
891 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
906 /* return 1 if address lists are strictly identical */
907 static int mutt_cmp_addr (const address_t * a, const address_t * b)
910 if (m_strcmp(a->mailbox, b->mailbox) ||
911 m_strcmp(a->personal, b->personal))
923 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
926 if (m_strcmp(a->data, b->data))
938 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
941 if (m_strcmp(e1->message_id, e2->message_id) ||
942 m_strcmp(e1->subject, e2->subject) ||
943 !mutt_cmp_list (e1->references, e2->references) ||
944 !mutt_cmp_addr (e1->from, e2->from) ||
945 !mutt_cmp_addr (e1->sender, e2->sender) ||
946 !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
947 !mutt_cmp_addr (e1->to, e2->to) ||
948 !mutt_cmp_addr (e1->cc, e2->cc) ||
949 !mutt_cmp_addr (e1->return_path, e2->return_path))
955 if (e1 == NULL && e2 == NULL)
962 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
964 if (b1->type != b2->type ||
965 b1->encoding != b2->encoding ||
966 m_strcmp(b1->subtype, b2->subtype) ||
967 m_strcmp(b1->description, b2->description) ||
968 !parameter_equal(b1->parameter, b2->parameter) ||
969 b1->length != b2->length)
973 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
975 if (h1->received != h2->received ||
976 h1->date_sent != h2->date_sent ||
977 h1->content->length != h2->content->length ||
978 h1->lines != h2->lines ||
979 h1->zhours != h2->zhours ||
980 h1->zminutes != h2->zminutes ||
981 h1->zoccident != h2->zoccident ||
982 h1->mime != h2->mime ||
983 !mutt_cmp_env (h1->env, h2->env) ||
984 !mutt_cmp_body (h1->content, h2->content))
990 if (h1 == NULL && h2 == NULL)
998 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
1001 char qc = 0; /* quote char */
1004 /* reset the destination pointer to the beginning of the buffer */
1005 dest->dptr = dest->data;
1007 tok->dptr = vskipspaces(tok->dptr);
1008 while ((ch = *tok->dptr)) {
1010 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
1011 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
1012 || (ch == '=' && (flags & M_TOKEN_EQUAL))
1013 || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
1014 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
1023 qc = 0; /* end of quote */
1025 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
1028 if (ch == '\\' && qc != '\'') {
1030 return -1; /* premature end of token */
1032 switch (ch = *tok->dptr++) {
1036 return -1; /* premature end of token */
1037 mutt_buffer_addch(dest,
1038 (ascii_toupper(*tok->dptr) - 'A' + 1) & 0x7f);
1042 mutt_buffer_addch(dest, '\r');
1045 mutt_buffer_addch(dest, '\n');
1048 mutt_buffer_addch(dest, '\t');
1051 mutt_buffer_addch(dest, '\f');
1054 mutt_buffer_addch(dest, '\033');
1057 if (isdigit((unsigned char)ch)
1058 && isdigit((unsigned char)*tok->dptr)
1059 && isdigit((unsigned char)*(tok->dptr + 1)))
1061 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
1062 *(tok->dptr + 1) - 3504);
1065 mutt_buffer_addch(dest, ch);
1069 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
1071 return -1; /* premature end of token */
1074 mutt_buffer_addch(dest, ch);
1077 mutt_buffer_addch(dest, '\033');
1079 if (isalpha((unsigned char)ch)) {
1080 mutt_buffer_addch(dest, ascii_toupper(ch) - 'A' + 1);
1082 mutt_buffer_addch(dest, '^');
1083 mutt_buffer_addch(dest, ch);
1086 if (ch == '`' && (!qc || qc == '"')) {
1096 if ((pc = strpbrk(pc, "\\`"))) {
1097 /* skip any quoted chars */
1101 } while (pc && *pc != '`');
1106 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
1107 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
1117 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
1119 mutt_wait_filter(pid);
1121 /* if we got output, make a new string consiting of the shell ouptput
1122 plus whatever else was left on the original line */
1123 /* BUT: If this is inside a quoted string, directly add output to
1125 if (expn.data && qc) {
1126 mutt_buffer_addstr(dest, expn.data);
1127 p_delete(&expn.data);
1130 expnlen = m_strlen(expn.data);
1131 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
1132 ptr = xmalloc(tok->dsize);
1133 memcpy(ptr, expn.data, expnlen);
1134 strcpy(ptr + expnlen, tok->dptr); /* __STRCPY_CHECKED__ */
1136 p_delete(&tok->data);
1139 tok->destroy = 1; /* mark that the caller should destroy this data */
1141 p_delete(&expn.data);
1144 if (ch == '$' && (!qc || qc == '"')
1145 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
1147 char *env = NULL, *var = NULL;
1149 if (*tok->dptr == '{') {
1151 if ((pc = strchr (tok->dptr, '}'))) {
1152 var = p_dupstr(tok->dptr, pc - tok->dptr);
1156 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
1158 var = p_dupstr(tok->dptr, pc - tok->dptr);
1163 if ((env = getenv (var))
1164 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
1166 mutt_buffer_addstr (dest, env);
1171 mutt_buffer_addch(dest, ch);
1174 mutt_buffer_addch(dest, 0); /* terminate the string */
1175 tok->dptr = vskipspaces(tok->dptr);