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 /* returns true if the header contained in "s" is in list "t" */
74 int mutt_matches_ignore (const char *s, string_list_t * t)
76 for (; t; t = t->next) {
77 if (!ascii_strncasecmp (s, t->data, m_strlen(t->data))
84 ssize_t _mutt_expand_path(char *s, ssize_t slen, int rx)
86 char p[_POSIX_PATH_MAX] = "";
87 char tmp[_POSIX_PATH_MAX];
88 const char *tail = "";
91 const address_t *alias;
95 if (s[1] == '/' || s[1] == '\0') {
96 m_strcpy(p, sizeof(p), Homedir);
100 tail = m_strchrnul(s + 1, '/');
102 m_strncpy(tmp, sizeof(tmp), s + 1, tail - s - 1);
104 if ((pw = getpwnam(tmp))) {
105 m_strcpy(p, sizeof(p), pw->pw_dir);
107 /* user not found! */
115 /* if folder = imap[s]://host/: don't append slash */
116 if (imap_is_magic(NONULL(Maildir), NULL) == M_IMAP
117 && Maildir[m_strlen(Maildir) - 1] == '/') {
118 m_strcpy(p, sizeof(p), Maildir);
120 snprintf(p, sizeof(p), "%s/", NONULL(Maildir));
126 /* elm compatibility, @ expands alias to user name */
129 if ((alias = alias_lookup(s + 1))) {
132 h.env = envelope_new();
133 h.env->from = h.env->to = (address_t *)alias;
134 mutt_default_save(p, sizeof (p), &h);
135 h.env->from = h.env->to = NULL;
139 /* recurse iff the result do not starts with '@' */
140 m_strcpy(s, slen, p);
147 m_strcpy(p, sizeof(p), Inbox);
152 m_strcpy(p, sizeof(p), Outbox);
158 m_strcpy(p, sizeof(p), LastFolder);
161 m_strcpy(p, sizeof(p), Spoolfile);
167 m_strcpy(p, sizeof(p), NONULL(LastFolder));
172 m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
183 char q[_POSIX_PATH_MAX];
184 rx_sanitize_string(q, sizeof(q), p);
185 snprintf(tmp, sizeof(tmp), "%s%s", q, tail);
187 snprintf(tmp, sizeof(tmp), "%s%s", p, tail);
190 return m_strcpy(s, slen, tmp);
193 void mutt_mktemp (char *s)
196 snprintf (s, _POSIX_PATH_MAX, "%s/madmutt-%s-%d-%d-%d-%x%x", NONULL (Tempdir),
197 NONULL (Hostname), (int) getuid (), (int) getpid (), Counter++,
198 (unsigned int) rand(), (unsigned int) rand());
202 /* collapse the pathname using ~ or = when possible */
203 void mutt_pretty_mailbox (char *s)
209 scheme = url_check_scheme (s);
211 if (scheme == U_IMAP || scheme == U_IMAPS) {
212 imap_pretty_mailbox (s);
216 /* if s is an url, only collapse path component */
217 if (scheme != U_UNKNOWN) {
218 p = strchr (s, ':') + 1;
219 if (!strncmp (p, "//", 2))
220 q = strchr (p + 2, '/');
222 q = strchr (p, '\0');
226 /* first attempt to collapse the pathname */
228 if (*p == '/' && p[1] == '/') {
232 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
241 if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
244 memmove (s, s + len, m_strlen(s + len) + 1);
246 else if (m_strncmp(s, Homedir, (len = m_strlen(Homedir))) == 0 &&
249 memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
254 mutt_expand_file_fmt(char *dst, ssize_t n, const char *fmt, const char *src)
256 char tmp[LONG_STRING];
257 mutt_quote_filename(tmp, sizeof(tmp), src);
258 m_snsubst(dst, n, fmt, tmp);
261 /* return 0 on success, -1 on abort, 1 on error */
262 int mutt_check_overwrite (const char *attname, const char *path,
263 char *fname, ssize_t flen, int *append,
267 char tmp[_POSIX_PATH_MAX];
270 m_strcpy(fname, flen, path);
271 if (access (fname, F_OK) != 0)
273 if (stat (fname, &st) != 0)
275 if (S_ISDIR (st.st_mode)) {
277 switch (mutt_multi_choice
278 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
281 m_strreplace(directory, fname);
295 if ((rc = mutt_yesorno(_("File is a directory, save under it?"),
297 return (rc == M_NO) ? 1 : -1;
299 if (!attname || !attname[0]) {
301 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
302 M_FILE | M_CLEAR) != 0 || !tmp[0])
304 mutt_concat_path(fname, flen, path, tmp);
307 mutt_concat_path(fname, flen, path, mutt_basename(attname));
310 if (*append == 0 && access (fname, F_OK) == 0) {
311 switch (mutt_multi_choice
312 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
320 *append = M_SAVE_APPEND;
322 case 1: /* overwrite */
323 *append = M_SAVE_OVERWRITE;
330 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
332 if (a && a->mailbox) {
333 m_strcpy(d, dsize, a->mailbox);
335 if (!option(OPTSAVEADDRESS)) {
336 char *p = strpbrk(d, "%@");
346 void mutt_safe_path(char *s, ssize_t l, address_t *a)
348 mutt_save_path(s, l, a);
351 if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
357 void mutt_FormatString (char *dest, /* output buffer */
358 ssize_t destlen, /* output buffer len */
359 const char *src, /* template string */
360 format_t * callback, /* callback for processing */
361 unsigned long data, /* callback data */
363 { /* callback flags */
364 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
365 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
366 ssize_t wlen, wid, count, col, len;
369 destlen--; /* save room for the terminal \0 */
370 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
373 while (*src && wlen < destlen) {
384 flags |= M_FORMAT_OPTIONAL;
388 flags &= ~M_FORMAT_OPTIONAL;
390 /* eat the format string */
393 while (count < ssizeof (prefix) &&
394 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
403 break; /* bad format */
405 ch = *src++; /* save the character to switch on */
407 if (flags & M_FORMAT_OPTIONAL) {
409 break; /* bad format */
412 /* eat the `if' part of the string */
415 while (count < ssizeof (ifstring) && *src && *src != '?'
422 /* eat the `else' part of the string (optional) */
424 src++; /* skip the & */
427 while (count < ssizeof (elsestring) && *src && *src != '?') {
434 break; /* bad format */
436 src++; /* move past the trailing `?' */
439 /* handle generic cases first */
441 /* right justify to EOL */
442 ch = *src++; /* pad char */
443 /* calculate space left on line. if we've already written more data
444 than will fit on the line, ignore the rest of the line */
445 if (DrawFullLine || option (OPTSTATUSONTOP))
446 count = (COLS < destlen ? COLS : destlen);
448 count = ((COLS - SW) < destlen ? (COLS - SW) : destlen);
450 count -= col; /* how many columns left on this line */
451 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
454 count -= wid; /* how many chars to pad */
455 memset (wptr, ch, count);
459 if (wid + wlen > destlen)
460 len = destlen - wlen;
463 memcpy (wptr, buf, len);
466 col += mutt_strwidth (buf);
468 break; /* skip rest of input */
470 else if (ch == '|') {
475 if (destlen > wlen) {
476 count = destlen - wlen;
477 memset (wptr, ch, count);
480 break; /* skip rest of input */
486 while (ch == '_' || ch == ':') {
495 /* use callback function to handle this case */
497 callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring,
510 if ((len = m_strlen(buf)) + wlen > destlen)
511 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
513 memcpy (wptr, buf, len);
516 col += mutt_strwidth (buf);
519 else if (*src == '\\') {
548 unsigned int bar = strcspn(src, "%\\");
549 char *bar2 = p_dupstr(src, bar);
555 col += mutt_strwidth (bar2);
562 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
563 int mutt_save_confirm (const char *s, struct stat *st)
565 char tmp[_POSIX_PATH_MAX];
570 magic = mx_get_magic (s);
572 if (magic == M_POP) {
573 mutt_error _("Can't save message to POP mailbox.");
579 if (magic == M_NNTP) {
580 mutt_error _("Can't save message to newsserver.");
586 if (magic > 0 && !mx_access (s, W_OK)) {
587 if (option (OPTCONFIRMAPPEND) &&
588 (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
589 /* if we're appending to the trash, there's no point in asking */
590 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
591 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
598 if (stat (s, st) != -1) {
600 mutt_error (_("%s is not a mailbox!"), s);
609 if (errno == ENOENT) {
610 if (option (OPTCONFIRMCREATE)) {
611 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
612 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
624 CLEARLINE (LINES - 1);
628 void mutt_sleep (short s)
630 sleep(MAX(s, SleepTime));
633 /* Decrease a file's modification time by 1 second */
634 time_t mutt_decrease_mtime (const char *f, struct stat *st)
641 if (stat (f, &_st) == -1)
646 if ((mtime = st->st_mtime) == time (NULL)) {
649 utim.modtime = mtime;
656 const char *mutt_make_version (int full)
658 static char vstring[STRING];
661 snprintf (vstring, sizeof (vstring),
662 "Madmutt/%s-r%s (based on Mutt 1.5.11)",
663 MUTT_VERSION, MUTT_REVISION);
665 snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
666 MUTT_VERSION, MUTT_REVISION);
670 int mutt_match_spam_list (const char *s, rx_t * l, char *text, int x)
672 static regmatch_t *pmatch = NULL;
673 static int nmatch = 0;
682 for (; l; l = l->next) {
683 /* If this pattern needs more matches, expand pmatch. */
684 if (l->nmatch > nmatch) {
685 p_realloc(&pmatch, l->nmatch);
689 /* Does this pattern match? */
690 if (regexec(l->rx, s, l->nmatch, (regmatch_t *)pmatch, (int) 0) == 0) {
691 /* Copy template into text, with substitutions. */
692 for (p = l->template; *p;) {
694 n = atoi (++p); /* find pmatch index */
695 while (isdigit ((unsigned char) *p))
696 ++p; /* skip subst token */
697 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
712 /* return 1 if address lists are strictly identical */
713 static int mutt_cmp_addr (const address_t * a, const address_t * b)
716 if (m_strcmp(a->mailbox, b->mailbox) ||
717 m_strcmp(a->personal, b->personal))
729 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
732 if (m_strcmp(a->data, b->data))
744 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
747 if (m_strcmp(e1->message_id, e2->message_id) ||
748 m_strcmp(e1->subject, e2->subject) ||
749 !mutt_cmp_list (e1->references, e2->references) ||
750 !mutt_cmp_addr (e1->from, e2->from) ||
751 !mutt_cmp_addr (e1->sender, e2->sender) ||
752 !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
753 !mutt_cmp_addr (e1->to, e2->to) ||
754 !mutt_cmp_addr (e1->cc, e2->cc) ||
755 !mutt_cmp_addr (e1->return_path, e2->return_path))
761 if (e1 == NULL && e2 == NULL)
768 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
770 if (b1->type != b2->type ||
771 b1->encoding != b2->encoding ||
772 m_strcmp(b1->subtype, b2->subtype) ||
773 m_strcmp(b1->description, b2->description) ||
774 !parameter_equal(b1->parameter, b2->parameter) ||
775 b1->length != b2->length)
779 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
781 if (h1->received != h2->received ||
782 h1->date_sent != h2->date_sent ||
783 h1->content->length != h2->content->length ||
784 h1->lines != h2->lines ||
785 h1->zhours != h2->zhours ||
786 h1->zminutes != h2->zminutes ||
787 h1->zoccident != h2->zoccident ||
788 h1->mime != h2->mime ||
789 !mutt_cmp_env (h1->env, h2->env) ||
790 !mutt_cmp_body (h1->content, h2->content))
796 if (h1 == NULL && h2 == NULL)
804 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
807 char qc = 0; /* quote char */
810 /* reset the destination pointer to the beginning of the buffer */
811 dest->dptr = dest->data;
813 tok->dptr = vskipspaces(tok->dptr);
814 while ((ch = *tok->dptr)) {
816 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
817 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
818 || (ch == '=' && (flags & M_TOKEN_EQUAL))
819 || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
820 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
829 qc = 0; /* end of quote */
831 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
834 if (ch == '\\' && qc != '\'') {
836 return -1; /* premature end of token */
838 switch (ch = *tok->dptr++) {
842 return -1; /* premature end of token */
843 mutt_buffer_addch(dest,
844 (ascii_toupper(*tok->dptr) - 'A' + 1) & 0x7f);
848 mutt_buffer_addch(dest, '\r');
851 mutt_buffer_addch(dest, '\n');
854 mutt_buffer_addch(dest, '\t');
857 mutt_buffer_addch(dest, '\f');
860 mutt_buffer_addch(dest, '\033');
863 if (isdigit((unsigned char)ch)
864 && isdigit((unsigned char)*tok->dptr)
865 && isdigit((unsigned char)*(tok->dptr + 1)))
867 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
868 *(tok->dptr + 1) - 3504);
871 mutt_buffer_addch(dest, ch);
875 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
877 return -1; /* premature end of token */
880 mutt_buffer_addch(dest, ch);
883 mutt_buffer_addch(dest, '\033');
885 if (isalpha((unsigned char)ch)) {
886 mutt_buffer_addch(dest, ascii_toupper(ch) - 'A' + 1);
888 mutt_buffer_addch(dest, '^');
889 mutt_buffer_addch(dest, ch);
892 if (ch == '`' && (!qc || qc == '"')) {
902 if ((pc = strpbrk(pc, "\\`"))) {
903 /* skip any quoted chars */
907 } while (pc && *pc != '`');
912 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
913 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
923 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
925 mutt_wait_filter(pid);
927 /* if we got output, make a new string consiting of the shell ouptput
928 plus whatever else was left on the original line */
929 /* BUT: If this is inside a quoted string, directly add output to
931 if (expn.data && qc) {
932 mutt_buffer_addstr(dest, expn.data);
933 p_delete(&expn.data);
936 expnlen = m_strlen(expn.data);
937 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
938 ptr = xmalloc(tok->dsize);
939 memcpy(ptr, expn.data, expnlen);
940 strcpy(ptr + expnlen, tok->dptr); /* __STRCPY_CHECKED__ */
942 p_delete(&tok->data);
945 tok->destroy = 1; /* mark that the caller should destroy this data */
947 p_delete(&expn.data);
950 if (ch == '$' && (!qc || qc == '"')
951 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
953 char *env = NULL, *var = NULL;
955 if (*tok->dptr == '{') {
957 if ((pc = strchr (tok->dptr, '}'))) {
958 var = p_dupstr(tok->dptr, pc - tok->dptr);
962 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
964 var = p_dupstr(tok->dptr, pc - tok->dptr);
969 if ((env = getenv (var))
970 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
972 mutt_buffer_addstr (dest, env);
977 mutt_buffer_addch(dest, ch);
980 mutt_buffer_addch(dest, 0); /* terminate the string */
981 tok->dptr = vskipspaces(tok->dptr);