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>
16 #include <lib-mime/mime.h>
17 #include <lib-ui/curses.h>
18 #include <lib-ui/enter.h>
19 #include <lib-sys/unix.h>
20 #include <lib-mx/mx.h>
28 #include <imap/imap.h>
30 #include <lib-crypt/crypt.h>
32 #define SW (option(OPTMBOXPANE)?SidebarWidth:0)
34 /* Modified by blong to accept a "suggestion" for file name. If
35 * that file exists, then construct one with unique name but
36 * keep any extension. This might fail, I guess.
37 * Renamed to mutt_adv_mktemp so I only have to change where it's
38 * called, and not all possible cases.
40 void mutt_adv_mktemp (const char* dir, char *s, ssize_t l)
44 fd = m_tempfd(s, l, m_strisempty(dir) ? NONULL(Tempdir) : dir, s);
53 /* returns true if the header contained in "s" is in list "t" */
54 int mutt_matches_ignore (const char *s, string_list_t * t)
56 for (; t; t = t->next) {
57 if (!ascii_strncasecmp (s, t->data, m_strlen(t->data))
64 ssize_t _mutt_expand_path(char *s, ssize_t slen, int rx)
66 char p[_POSIX_PATH_MAX] = "";
67 char tmp[_POSIX_PATH_MAX];
68 const char *tail = "";
71 const address_t *alias;
75 if (s[1] == '/' || s[1] == '\0') {
76 m_strcpy(p, sizeof(p), Homedir);
80 tail = m_strchrnul(s + 1, '/');
82 m_strncpy(tmp, sizeof(tmp), s + 1, tail - s - 1);
84 if ((pw = getpwnam(tmp))) {
85 m_strcpy(p, sizeof(p), pw->pw_dir);
95 /* if folder = imap[s]://host/: don't append slash */
96 if (imap_is_magic(NONULL(Maildir), NULL) == M_IMAP
97 && Maildir[m_strlen(Maildir) - 1] == '/') {
98 m_strcpy(p, sizeof(p), Maildir);
100 snprintf(p, sizeof(p), "%s/", NONULL(Maildir));
106 /* elm compatibility, @ expands alias to user name */
109 if ((alias = alias_lookup(s + 1))) {
112 h.env = envelope_new();
113 h.env->from = h.env->to = (address_t *)alias;
114 mutt_default_save(p, sizeof (p), &h);
115 h.env->from = h.env->to = NULL;
119 /* recurse iff the result do not starts with '@' */
120 m_strcpy(s, slen, p);
127 m_strcpy(p, sizeof(p), Inbox);
132 m_strcpy(p, sizeof(p), Outbox);
138 m_strcpy(p, sizeof(p), LastFolder);
141 m_strcpy(p, sizeof(p), Spoolfile);
147 m_strcpy(p, sizeof(p), NONULL(LastFolder));
152 m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
163 char q[_POSIX_PATH_MAX];
164 rx_sanitize_string(q, sizeof(q), p);
165 snprintf(tmp, sizeof(tmp), "%s%s", q, tail);
167 snprintf(tmp, sizeof(tmp), "%s%s", p, tail);
170 return m_strcpy(s, slen, tmp);
173 void mutt_mktemp(char *s)
175 int fd = m_tempfd(s, _POSIX_PATH_MAX, NONULL(Tempdir), NULL);
184 /* collapse the pathname using ~ or = when possible */
185 void mutt_pretty_mailbox (char *s)
191 scheme = url_check_scheme (s);
193 if (scheme == U_IMAP || scheme == U_IMAPS) {
194 imap_pretty_mailbox (s);
198 /* if s is an url, only collapse path component */
199 if (scheme != U_UNKNOWN) {
200 p = strchr (s, ':') + 1;
201 if (!m_strncmp (p, "//", 2))
202 q = strchr (p + 2, '/');
204 q = strchr (p, '\0');
208 /* first attempt to collapse the pathname */
210 if (*p == '/' && p[1] == '/') {
214 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
223 if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
226 memmove (s, s + len, m_strlen(s + len) + 1);
228 else if (m_strncmp(s, Homedir, (len = m_strlen(Homedir))) == 0 &&
231 memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
235 /* return 0 on success, -1 on abort, 1 on error */
236 int mutt_check_overwrite (const char *attname, const char *path,
237 char *fname, ssize_t flen, int *append,
241 char tmp[_POSIX_PATH_MAX];
244 m_strcpy(fname, flen, path);
245 if (access (fname, F_OK) != 0)
247 if (stat (fname, &st) != 0)
249 if (S_ISDIR (st.st_mode)) {
251 switch (mutt_multi_choice
252 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
255 m_strreplace(directory, fname);
269 if ((rc = mutt_yesorno(_("File is a directory, save under it?"),
271 return (rc == M_NO) ? 1 : -1;
273 if (!attname || !attname[0]) {
275 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
276 M_FILE | M_CLEAR) != 0 || !tmp[0])
278 mutt_concat_path(fname, flen, path, tmp);
281 mutt_concat_path(fname, flen, path, mutt_basename(attname));
284 if (*append == 0 && access (fname, F_OK) == 0) {
285 switch (mutt_multi_choice
286 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
294 *append = M_SAVE_APPEND;
296 case 1: /* overwrite */
297 *append = M_SAVE_OVERWRITE;
304 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
306 if (a && a->mailbox) {
307 m_strcpy(d, dsize, a->mailbox);
309 if (!option(OPTSAVEADDRESS)) {
310 char *p = strpbrk(d, "%@");
320 void mutt_safe_path(char *s, ssize_t l, address_t *a)
322 mutt_save_path(s, l, a);
325 if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
331 void mutt_FormatString (char *dest, /* output buffer */
332 ssize_t destlen, /* output buffer len */
333 const char *src, /* template string */
334 format_t * callback, /* callback for processing */
335 unsigned long data, /* callback data */
337 { /* callback flags */
338 char prefix[STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
339 char ifstring[STRING], elsestring[STRING];
340 ssize_t wlen, wid, count, col, len;
343 destlen--; /* save room for the terminal \0 */
344 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
347 while (*src && wlen < destlen) {
358 flags |= M_FORMAT_OPTIONAL;
362 flags &= ~M_FORMAT_OPTIONAL;
364 /* eat the format string */
367 while (count < ssizeof (prefix) &&
368 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
377 break; /* bad format */
379 ch = *src++; /* save the character to switch on */
381 if (flags & M_FORMAT_OPTIONAL) {
383 break; /* bad format */
386 /* eat the `if' part of the string */
389 while (count < ssizeof (ifstring) && *src && *src != '?'
396 /* eat the `else' part of the string (optional) */
398 src++; /* skip the & */
401 while (count < ssizeof (elsestring) && *src && *src != '?') {
408 break; /* bad format */
410 src++; /* move past the trailing `?' */
413 /* handle generic cases first */
415 /* right justify to EOL */
416 ch = *src++; /* pad char */
417 /* calculate space left on line. if we've already written more data
418 than will fit on the line, ignore the rest of the line */
419 if (DrawFullLine || option (OPTSTATUSONTOP))
420 count = (COLS < destlen ? COLS : destlen);
422 count = ((COLS - SW) < destlen ? (COLS - SW) : destlen);
424 count -= col; /* how many columns left on this line */
425 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
428 count -= wid; /* how many chars to pad */
429 memset (wptr, ch, count);
433 if (wid + wlen > destlen)
434 len = destlen - wlen;
437 memcpy (wptr, buf, len);
440 col += mutt_strwidth (buf);
442 break; /* skip rest of input */
444 else if (ch == '|') {
449 if (destlen > wlen) {
450 count = destlen - wlen;
451 memset (wptr, ch, count);
454 break; /* skip rest of input */
460 while (ch == '_' || ch == ':') {
469 /* use callback function to handle this case */
471 callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring,
484 if ((len = m_strlen(buf)) + wlen > destlen)
485 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
487 memcpy (wptr, buf, len);
490 col += mutt_strwidth (buf);
493 else if (*src == '\\') {
522 unsigned int bar = strcspn(src, "%\\");
523 char *bar2 = p_dupstr(src, bar);
529 col += mutt_strwidth (bar2);
536 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
537 int mutt_save_confirm (const char *s, struct stat *st)
539 char tmp[_POSIX_PATH_MAX];
544 magic = mx_get_magic (s);
546 if (magic == M_POP) {
547 mutt_error _("Can't save message to POP mailbox.");
553 if (magic == M_NNTP) {
554 mutt_error _("Can't save message to newsserver.");
560 if (magic > 0 && !mx_access (s, W_OK)) {
561 if (option (OPTCONFIRMAPPEND) &&
562 (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
563 /* if we're appending to the trash, there's no point in asking */
564 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
565 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
572 if (stat (s, st) != -1) {
574 mutt_error (_("%s is not a mailbox!"), s);
583 if (errno == ENOENT) {
584 if (option (OPTCONFIRMCREATE)) {
585 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
586 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
598 CLEARLINE (LINES - 1);
602 void mutt_sleep (short s)
604 sleep(MAX(s, SleepTime));
607 const char *mutt_make_version (int full)
609 static char vstring[STRING];
612 snprintf (vstring, sizeof (vstring),
613 "Madmutt/%s-r%s (based on Mutt 1.5.11)",
614 MUTT_VERSION, MUTT_REVISION);
616 snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
617 MUTT_VERSION, MUTT_REVISION);
621 /* return 1 if address lists are strictly identical */
622 static int mutt_cmp_addr (const address_t * a, const address_t * b)
625 if (m_strcmp(a->mailbox, b->mailbox) ||
626 m_strcmp(a->personal, b->personal))
638 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
641 if (m_strcmp(a->data, b->data))
653 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
656 if (m_strcmp(e1->message_id, e2->message_id) ||
657 m_strcmp(e1->subject, e2->subject) ||
658 !mutt_cmp_list (e1->references, e2->references) ||
659 !mutt_cmp_addr (e1->from, e2->from) ||
660 !mutt_cmp_addr (e1->sender, e2->sender) ||
661 !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
662 !mutt_cmp_addr (e1->to, e2->to) ||
663 !mutt_cmp_addr (e1->cc, e2->cc) ||
664 !mutt_cmp_addr (e1->return_path, e2->return_path))
670 if (e1 == NULL && e2 == NULL)
677 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
679 if (b1->type != b2->type ||
680 b1->encoding != b2->encoding ||
681 m_strcmp(b1->subtype, b2->subtype) ||
682 m_strcmp(b1->description, b2->description) ||
683 !parameter_equal(b1->parameter, b2->parameter) ||
684 b1->length != b2->length)
688 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
690 if (h1->received != h2->received ||
691 h1->date_sent != h2->date_sent ||
692 h1->content->length != h2->content->length ||
693 h1->lines != h2->lines ||
694 h1->zhours != h2->zhours ||
695 h1->zminutes != h2->zminutes ||
696 h1->zoccident != h2->zoccident ||
697 h1->mime != h2->mime ||
698 !mutt_cmp_env (h1->env, h2->env) ||
699 !mutt_cmp_body (h1->content, h2->content))
705 if (h1 == NULL && h2 == NULL)
713 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
716 char qc = 0; /* quote char */
719 /* reset the destination pointer to the beginning of the buffer */
720 dest->dptr = dest->data;
722 tok->dptr = vskipspaces(tok->dptr);
723 while ((ch = *tok->dptr)) {
725 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
726 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
727 || (ch == '=' && (flags & M_TOKEN_EQUAL))
728 || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
729 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
738 qc = 0; /* end of quote */
740 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
743 if (ch == '\\' && qc != '\'') {
745 return -1; /* premature end of token */
747 switch (ch = *tok->dptr++) {
751 return -1; /* premature end of token */
752 mutt_buffer_addch(dest,
753 (ascii_toupper(*tok->dptr) - 'A' + 1) & 0x7f);
757 mutt_buffer_addch(dest, '\r');
760 mutt_buffer_addch(dest, '\n');
763 mutt_buffer_addch(dest, '\t');
766 mutt_buffer_addch(dest, '\f');
769 mutt_buffer_addch(dest, '\033');
772 if (isdigit((unsigned char)ch)
773 && isdigit((unsigned char)*tok->dptr)
774 && isdigit((unsigned char)*(tok->dptr + 1)))
776 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
777 *(tok->dptr + 1) - 3504);
780 mutt_buffer_addch(dest, ch);
784 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
786 return -1; /* premature end of token */
789 mutt_buffer_addch(dest, ch);
792 mutt_buffer_addch(dest, '\033');
794 if (isalpha((unsigned char)ch)) {
795 mutt_buffer_addch(dest, ascii_toupper(ch) - 'A' + 1);
797 mutt_buffer_addch(dest, '^');
798 mutt_buffer_addch(dest, ch);
801 if (ch == '`' && (!qc || qc == '"')) {
811 if ((pc = strpbrk(pc, "\\`"))) {
812 /* skip any quoted chars */
816 } while (pc && *pc != '`');
821 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
822 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
832 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
834 mutt_wait_filter(pid);
836 /* if we got output, make a new string consiting of the shell ouptput
837 plus whatever else was left on the original line */
838 /* BUT: If this is inside a quoted string, directly add output to
840 if (expn.data && qc) {
841 mutt_buffer_addstr(dest, expn.data);
842 p_delete(&expn.data);
845 expnlen = m_strlen(expn.data);
846 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
847 ptr = xmalloc(tok->dsize);
848 memcpy(ptr, expn.data, expnlen);
849 m_strcpy(ptr + expnlen, tok->dsize - expnlen, tok->dptr);
851 p_delete(&tok->data);
854 tok->destroy = 1; /* mark that the caller should destroy this data */
856 p_delete(&expn.data);
859 if (ch == '$' && (!qc || qc == '"')
860 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
862 char *env = NULL, *var = NULL;
864 if (*tok->dptr == '{') {
866 if ((pc = strchr (tok->dptr, '}'))) {
867 var = p_dupstr(tok->dptr, pc - tok->dptr);
871 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
873 var = p_dupstr(tok->dptr, pc - tok->dptr);
878 if ((env = getenv (var))
879 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
881 mutt_buffer_addstr (dest, env);
886 mutt_buffer_addch(dest, ch);
889 mutt_buffer_addch(dest, 0); /* terminate the string */
890 tok->dptr = vskipspaces(tok->dptr);