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 ssize_t _mutt_expand_path(char *s, ssize_t slen, int rx)
152 char p[_POSIX_PATH_MAX] = "";
153 char tmp[_POSIX_PATH_MAX];
154 const char *tail = "";
157 const address_t *alias;
161 if (s[1] == '/' || s[1] == '\0') {
162 m_strcpy(p, sizeof(p), Homedir);
166 tail = m_strchrnul(s + 1, '/');
168 m_strncpy(tmp, sizeof(tmp), s + 1, tail - s - 1);
170 if ((pw = getpwnam(tmp))) {
171 m_strcpy(p, sizeof(p), pw->pw_dir);
173 /* user not found! */
181 /* if folder = imap[s]://host/: don't append slash */
182 if (imap_is_magic(NONULL(Maildir), NULL) == M_IMAP
183 && Maildir[m_strlen(Maildir) - 1] == '/') {
184 m_strcpy(p, sizeof(p), Maildir);
186 snprintf(p, sizeof(p), "%s/", NONULL(Maildir));
192 /* elm compatibility, @ expands alias to user name */
195 if ((alias = alias_lookup(s + 1))) {
198 h.env = envelope_new();
199 h.env->from = h.env->to = (address_t *)alias;
200 mutt_default_save(p, sizeof (p), &h);
201 h.env->from = h.env->to = NULL;
205 /* recurse iff the result do not starts with '@' */
206 m_strcpy(s, slen, p);
213 m_strcpy(p, sizeof(p), Inbox);
218 m_strcpy(p, sizeof(p), Outbox);
224 m_strcpy(p, sizeof(p), LastFolder);
227 m_strcpy(p, sizeof(p), Spoolfile);
233 m_strcpy(p, sizeof(p), NONULL(LastFolder));
238 m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
249 char q[_POSIX_PATH_MAX];
250 rx_sanitize_string(q, sizeof(q), p);
251 snprintf(tmp, sizeof(tmp), "%s%s", q, tail);
253 snprintf(tmp, sizeof(tmp), "%s%s", p, tail);
256 return m_strcpy(s, slen, tmp);
259 void mutt_mktemp (char *s)
262 snprintf (s, _POSIX_PATH_MAX, "%s/madmutt-%s-%d-%d-%d-%x%x", NONULL (Tempdir),
263 NONULL (Hostname), (int) getuid (), (int) getpid (), Counter++,
264 (unsigned int) rand(), (unsigned int) rand());
268 /* collapse the pathname using ~ or = when possible */
269 void mutt_pretty_mailbox (char *s)
275 scheme = url_check_scheme (s);
277 if (scheme == U_IMAP || scheme == U_IMAPS) {
278 imap_pretty_mailbox (s);
282 /* if s is an url, only collapse path component */
283 if (scheme != U_UNKNOWN) {
284 p = strchr (s, ':') + 1;
285 if (!strncmp (p, "//", 2))
286 q = strchr (p + 2, '/');
288 q = strchr (p, '\0');
292 /* first attempt to collapse the pathname */
294 if (*p == '/' && p[1] == '/') {
298 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
307 if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
310 memmove (s, s + len, m_strlen(s + len) + 1);
312 else if (m_strncmp(s, Homedir, (len = m_strlen(Homedir))) == 0 &&
315 memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
319 void mutt_pretty_size (char *s, ssize_t len, long n)
322 m_strcpy(s, len, "0K");
323 else if (n < 10189) /* 0.1K - 9.9K */
324 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
325 else if (n < 1023949) { /* 10K - 999K */
326 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
327 snprintf (s, len, "%ldK", (n + 51) / 1024);
329 else if (n < 10433332) /* 1.0M - 9.9M */
330 snprintf (s, len, "%3.1fM", n / 1048576.0);
333 /* (10433332 + 52428) / 1048576 = 10 */
334 snprintf (s, len, "%ldM", (n + 52428) / 1048576);
338 void mutt_expand_file_fmt(char *dest, ssize_t destlen,
339 const char *fmt, const char *src)
341 char tmp[LONG_STRING];
343 mutt_quote_filename(tmp, sizeof(tmp), src);
344 mutt_expand_fmt(dest, destlen, fmt, tmp);
347 void mutt_expand_fmt(char *dst, ssize_t dlen,
348 const char *fmt, const char *src)
353 while (*fmt && pos < dlen - 1) {
358 pos += m_strcpy(dst + pos, dlen - pos, src);
376 pos += m_strcpy(dst + pos, dlen - pos, " ");
377 pos += m_strcpy(dst + pos, dlen - pos, src);
381 /* return 0 on success, -1 on abort, 1 on error */
382 int mutt_check_overwrite (const char *attname, const char *path,
383 char *fname, ssize_t flen, int *append,
387 char tmp[_POSIX_PATH_MAX];
390 m_strcpy(fname, flen, path);
391 if (access (fname, F_OK) != 0)
393 if (stat (fname, &st) != 0)
395 if (S_ISDIR (st.st_mode)) {
397 switch (mutt_multi_choice
398 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
401 m_strreplace(directory, fname);
416 mutt_yesorno (_("File is a directory, save under it?"),
418 return (rc == M_NO) ? 1 : -1;
420 if (!attname || !attname[0]) {
422 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
423 M_FILE | M_CLEAR) != 0 || !tmp[0])
425 mutt_concat_path(fname, flen, path, tmp);
428 mutt_concat_path(fname, flen, path, mutt_basename(attname));
431 if (*append == 0 && access (fname, F_OK) == 0) {
432 switch (mutt_multi_choice
433 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
441 *append = M_SAVE_APPEND;
443 case 1: /* overwrite */
444 *append = M_SAVE_OVERWRITE;
451 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
453 if (a && a->mailbox) {
454 m_strcpy(d, dsize, a->mailbox);
456 if (!option(OPTSAVEADDRESS)) {
457 char *p = strpbrk(d, "%@");
467 void mutt_safe_path(char *s, ssize_t l, address_t *a)
469 mutt_save_path(s, l, a);
472 if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
478 void mutt_FormatString (char *dest, /* output buffer */
479 ssize_t destlen, /* output buffer len */
480 const char *src, /* template string */
481 format_t * callback, /* callback for processing */
482 unsigned long data, /* callback data */
484 { /* callback flags */
485 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
486 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
487 ssize_t wlen, wid, count, col, len;
490 destlen--; /* save room for the terminal \0 */
491 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
494 while (*src && wlen < destlen) {
505 flags |= M_FORMAT_OPTIONAL;
509 flags &= ~M_FORMAT_OPTIONAL;
511 /* eat the format string */
514 while (count < ssizeof (prefix) &&
515 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
524 break; /* bad format */
526 ch = *src++; /* save the character to switch on */
528 if (flags & M_FORMAT_OPTIONAL) {
530 break; /* bad format */
533 /* eat the `if' part of the string */
536 while (count < ssizeof (ifstring) && *src && *src != '?'
543 /* eat the `else' part of the string (optional) */
545 src++; /* skip the & */
548 while (count < ssizeof (elsestring) && *src && *src != '?') {
555 break; /* bad format */
557 src++; /* move past the trailing `?' */
560 /* handle generic cases first */
562 /* right justify to EOL */
563 ch = *src++; /* pad char */
564 /* calculate space left on line. if we've already written more data
565 than will fit on the line, ignore the rest of the line */
566 if (DrawFullLine || option (OPTSTATUSONTOP))
567 count = (COLS < destlen ? COLS : destlen);
569 count = ((COLS - SW) < destlen ? (COLS - SW) : destlen);
571 count -= col; /* how many columns left on this line */
572 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
575 count -= wid; /* how many chars to pad */
576 memset (wptr, ch, count);
580 if (wid + wlen > destlen)
581 len = destlen - wlen;
584 memcpy (wptr, buf, len);
587 col += mutt_strwidth (buf);
589 break; /* skip rest of input */
591 else if (ch == '|') {
596 if (destlen > wlen) {
597 count = destlen - wlen;
598 memset (wptr, ch, count);
601 break; /* skip rest of input */
607 while (ch == '_' || ch == ':') {
616 /* use callback function to handle this case */
618 callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring,
631 if ((len = m_strlen(buf)) + wlen > destlen)
632 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
634 memcpy (wptr, buf, len);
637 col += mutt_strwidth (buf);
640 else if (*src == '\\') {
669 unsigned int bar = strcspn(src, "%\\");
670 char *bar2 = p_dupstr(src, bar);
676 col += mutt_strwidth (bar2);
683 if (flags & M_FORMAT_MAKEPRINT) {
684 /* Make sure that the string is printable by changing all non-printable
685 chars to dots, or spaces for non-printable whitespace */
686 for (cp = dest; *cp; cp++)
687 if (!isprint(*cp) && !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
688 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
693 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
694 int mutt_save_confirm (const char *s, struct stat *st)
696 char tmp[_POSIX_PATH_MAX];
701 magic = mx_get_magic (s);
703 if (magic == M_POP) {
704 mutt_error _("Can't save message to POP mailbox.");
710 if (magic == M_NNTP) {
711 mutt_error _("Can't save message to newsserver.");
717 if (magic > 0 && !mx_access (s, W_OK)) {
718 if (option (OPTCONFIRMAPPEND) &&
719 (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
720 /* if we're appending to the trash, there's no point in asking */
721 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
722 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
729 if (stat (s, st) != -1) {
731 mutt_error (_("%s is not a mailbox!"), s);
741 if (errno == ENOENT) {
742 if (option (OPTCONFIRMCREATE)) {
743 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
744 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
757 CLEARLINE (LINES - 1);
761 void mutt_sleep (short s)
763 sleep(MAX(s, SleepTime));
766 /* Decrease a file's modification time by 1 second */
767 time_t mutt_decrease_mtime (const char *f, struct stat *st)
774 if (stat (f, &_st) == -1)
779 if ((mtime = st->st_mtime) == time (NULL)) {
782 utim.modtime = mtime;
789 const char *mutt_make_version (int full)
791 static char vstring[STRING];
794 snprintf (vstring, sizeof (vstring),
795 "Madmutt/%s-r%s (based on Mutt 1.5.11)",
796 MUTT_VERSION, MUTT_REVISION);
798 snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
799 MUTT_VERSION, MUTT_REVISION);
803 void mutt_free_spam_list (SPAM_LIST ** list)
811 *list = (*list)->next;
813 p_delete(&p->template);
818 int mutt_match_spam_list (const char *s, SPAM_LIST * l, char *text, int x)
820 static regmatch_t *pmatch = NULL;
821 static int nmatch = 0;
830 for (; l; l = l->next) {
831 /* If this pattern needs more matches, expand pmatch. */
832 if (l->nmatch > nmatch) {
833 p_realloc(&pmatch, l->nmatch);
837 /* Does this pattern match? */
838 if (regexec(l->rx->rx, s, l->nmatch, (regmatch_t *)pmatch, (int) 0) == 0)
840 /* Copy template into text, with substitutions. */
841 for (p = l->template; *p;) {
843 n = atoi (++p); /* find pmatch index */
844 while (isdigit ((unsigned char) *p))
845 ++p; /* skip subst token */
846 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
861 /* return 1 if address lists are strictly identical */
862 static int mutt_cmp_addr (const address_t * a, const address_t * b)
865 if (m_strcmp(a->mailbox, b->mailbox) ||
866 m_strcmp(a->personal, b->personal))
878 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
881 if (m_strcmp(a->data, b->data))
893 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
896 if (m_strcmp(e1->message_id, e2->message_id) ||
897 m_strcmp(e1->subject, e2->subject) ||
898 !mutt_cmp_list (e1->references, e2->references) ||
899 !mutt_cmp_addr (e1->from, e2->from) ||
900 !mutt_cmp_addr (e1->sender, e2->sender) ||
901 !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
902 !mutt_cmp_addr (e1->to, e2->to) ||
903 !mutt_cmp_addr (e1->cc, e2->cc) ||
904 !mutt_cmp_addr (e1->return_path, e2->return_path))
910 if (e1 == NULL && e2 == NULL)
917 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
919 if (b1->type != b2->type ||
920 b1->encoding != b2->encoding ||
921 m_strcmp(b1->subtype, b2->subtype) ||
922 m_strcmp(b1->description, b2->description) ||
923 !parameter_equal(b1->parameter, b2->parameter) ||
924 b1->length != b2->length)
928 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
930 if (h1->received != h2->received ||
931 h1->date_sent != h2->date_sent ||
932 h1->content->length != h2->content->length ||
933 h1->lines != h2->lines ||
934 h1->zhours != h2->zhours ||
935 h1->zminutes != h2->zminutes ||
936 h1->zoccident != h2->zoccident ||
937 h1->mime != h2->mime ||
938 !mutt_cmp_env (h1->env, h2->env) ||
939 !mutt_cmp_body (h1->content, h2->content))
945 if (h1 == NULL && h2 == NULL)
953 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
956 char qc = 0; /* quote char */
959 /* reset the destination pointer to the beginning of the buffer */
960 dest->dptr = dest->data;
962 tok->dptr = vskipspaces(tok->dptr);
963 while ((ch = *tok->dptr)) {
965 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
966 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
967 || (ch == '=' && (flags & M_TOKEN_EQUAL))
968 || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
969 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
978 qc = 0; /* end of quote */
980 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
983 if (ch == '\\' && qc != '\'') {
985 return -1; /* premature end of token */
987 switch (ch = *tok->dptr++) {
991 return -1; /* premature end of token */
992 mutt_buffer_addch(dest,
993 (ascii_toupper(*tok->dptr) - 'A' + 1) & 0x7f);
997 mutt_buffer_addch(dest, '\r');
1000 mutt_buffer_addch(dest, '\n');
1003 mutt_buffer_addch(dest, '\t');
1006 mutt_buffer_addch(dest, '\f');
1009 mutt_buffer_addch(dest, '\033');
1012 if (isdigit((unsigned char)ch)
1013 && isdigit((unsigned char)*tok->dptr)
1014 && isdigit((unsigned char)*(tok->dptr + 1)))
1016 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
1017 *(tok->dptr + 1) - 3504);
1020 mutt_buffer_addch(dest, ch);
1024 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
1026 return -1; /* premature end of token */
1029 mutt_buffer_addch(dest, ch);
1032 mutt_buffer_addch(dest, '\033');
1034 if (isalpha((unsigned char)ch)) {
1035 mutt_buffer_addch(dest, ascii_toupper(ch) - 'A' + 1);
1037 mutt_buffer_addch(dest, '^');
1038 mutt_buffer_addch(dest, ch);
1041 if (ch == '`' && (!qc || qc == '"')) {
1051 if ((pc = strpbrk(pc, "\\`"))) {
1052 /* skip any quoted chars */
1056 } while (pc && *pc != '`');
1061 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
1062 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
1072 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
1074 mutt_wait_filter(pid);
1076 /* if we got output, make a new string consiting of the shell ouptput
1077 plus whatever else was left on the original line */
1078 /* BUT: If this is inside a quoted string, directly add output to
1080 if (expn.data && qc) {
1081 mutt_buffer_addstr(dest, expn.data);
1082 p_delete(&expn.data);
1085 expnlen = m_strlen(expn.data);
1086 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
1087 ptr = xmalloc(tok->dsize);
1088 memcpy(ptr, expn.data, expnlen);
1089 strcpy(ptr + expnlen, tok->dptr); /* __STRCPY_CHECKED__ */
1091 p_delete(&tok->data);
1094 tok->destroy = 1; /* mark that the caller should destroy this data */
1096 p_delete(&expn.data);
1099 if (ch == '$' && (!qc || qc == '"')
1100 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
1102 char *env = NULL, *var = NULL;
1104 if (*tok->dptr == '{') {
1106 if ((pc = strchr (tok->dptr, '}'))) {
1107 var = p_dupstr(tok->dptr, pc - tok->dptr);
1111 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
1113 var = p_dupstr(tok->dptr, pc - tok->dptr);
1118 if ((env = getenv (var))
1119 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
1121 mutt_buffer_addstr (dest, env);
1126 mutt_buffer_addch(dest, ch);
1129 mutt_buffer_addch(dest, 0); /* terminate the string */
1130 tok->dptr = vskipspaces(tok->dptr);