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 /* Modified by blong to accept a "suggestion" for file name. If
33 * that file exists, then construct one with unique name but
34 * keep any extension. This might fail, I guess.
35 * Renamed to mutt_adv_mktemp so I only have to change where it's
36 * called, and not all possible cases.
38 void mutt_adv_mktemp (const char* dir, char *s, ssize_t l)
42 fd = m_tempfd(s, l, m_strisempty(dir) ? NONULL(Tempdir) : dir, s);
51 /* returns true if the header contained in "s" is in list "t" */
52 int mutt_matches_ignore (const char *s, string_list_t * t)
54 for (; t; t = t->next) {
55 if (!ascii_strncasecmp (s, t->data, m_strlen(t->data))
62 ssize_t _mutt_expand_path(char *s, ssize_t slen, int rx)
64 char p[_POSIX_PATH_MAX] = "";
65 char tmp[_POSIX_PATH_MAX];
66 const char *tail = "";
69 const address_t *alias;
73 if (s[1] == '/' || s[1] == '\0') {
74 m_strcpy(p, sizeof(p), Homedir);
78 tail = m_strchrnul(s + 1, '/');
80 m_strncpy(tmp, sizeof(tmp), s + 1, tail - s - 1);
82 if ((pw = getpwnam(tmp))) {
83 m_strcpy(p, sizeof(p), pw->pw_dir);
93 /* if folder = imap[s]://host/: don't append slash */
94 if (imap_is_magic(NONULL(Maildir), NULL) == M_IMAP
95 && Maildir[m_strlen(Maildir) - 1] == '/') {
96 m_strcpy(p, sizeof(p), Maildir);
98 snprintf(p, sizeof(p), "%s/", NONULL(Maildir));
104 /* elm compatibility, @ expands alias to user name */
107 if ((alias = alias_lookup(s + 1))) {
110 h.env = envelope_new();
111 h.env->from = h.env->to = (address_t *)alias;
112 mutt_default_save(p, sizeof (p), &h);
113 h.env->from = h.env->to = NULL;
117 /* recurse iff the result do not starts with '@' */
118 m_strcpy(s, slen, p);
125 m_strcpy(p, sizeof(p), Inbox);
130 m_strcpy(p, sizeof(p), Outbox);
136 m_strcpy(p, sizeof(p), LastFolder);
139 m_strcpy(p, sizeof(p), Spoolfile);
145 m_strcpy(p, sizeof(p), NONULL(LastFolder));
150 m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
161 char q[_POSIX_PATH_MAX];
162 rx_sanitize_string(q, sizeof(q), p);
163 snprintf(tmp, sizeof(tmp), "%s%s", q, tail);
165 snprintf(tmp, sizeof(tmp), "%s%s", p, tail);
168 return m_strcpy(s, slen, tmp);
171 void mutt_mktemp(char *s)
173 int fd = m_tempfd(s, _POSIX_PATH_MAX, NONULL(Tempdir), NULL);
182 /* collapse the pathname using ~ or = when possible */
183 void mutt_pretty_mailbox (char *s)
189 scheme = url_check_scheme (s);
191 if (scheme == U_IMAP || scheme == U_IMAPS) {
192 imap_pretty_mailbox (s);
196 /* if s is an url, only collapse path component */
197 if (scheme != U_UNKNOWN) {
198 p = strchr (s, ':') + 1;
199 if (!m_strncmp (p, "//", 2))
200 q = strchr (p + 2, '/');
202 q = strchr (p, '\0');
206 /* first attempt to collapse the pathname */
208 if (*p == '/' && p[1] == '/') {
212 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
221 if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
224 memmove (s, s + len, m_strlen(s + len) + 1);
226 else if (m_strncmp(s, Homedir, (len = m_strlen(Homedir))) == 0 &&
229 memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
233 /* return 0 on success, -1 on abort, 1 on error */
234 int mutt_check_overwrite (const char *attname, const char *path,
235 char *fname, ssize_t flen, int *append,
239 char tmp[_POSIX_PATH_MAX];
242 m_strcpy(fname, flen, path);
243 if (access (fname, F_OK) != 0)
245 if (stat (fname, &st) != 0)
247 if (S_ISDIR (st.st_mode)) {
249 switch (mutt_multi_choice
250 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
253 m_strreplace(directory, fname);
267 if ((rc = mutt_yesorno(_("File is a directory, save under it?"),
269 return (rc == M_NO) ? 1 : -1;
271 if (!attname || !attname[0]) {
273 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
274 M_FILE | M_CLEAR) != 0 || !tmp[0])
276 mutt_concat_path(fname, flen, path, tmp);
279 mutt_concat_path(fname, flen, path, mutt_basename(attname));
282 if (*append == 0 && access (fname, F_OK) == 0) {
283 switch (mutt_multi_choice
284 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
292 *append = M_SAVE_APPEND;
294 case 1: /* overwrite */
295 *append = M_SAVE_OVERWRITE;
302 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
304 if (a && a->mailbox) {
305 m_strcpy(d, dsize, a->mailbox);
307 if (!option(OPTSAVEADDRESS)) {
308 char *p = strpbrk(d, "%@");
318 void mutt_safe_path(char *s, ssize_t l, address_t *a)
320 mutt_save_path(s, l, a);
323 if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
329 ssize_t m_strformat(char *dst, ssize_t dlen, int width, const char *fmt,
330 format_t *callback, anytype cdata, format_flag flags)
332 ssize_t pos = flags & M_FORMAT_ARROWCURSOR ? 3 : 0;
334 m_strpad(dst, dlen, '\0', pos + 1);
342 char ifstr[STRING], elstr[STRING], prefix[STRING];
344 *ifstr = *elstr = *prefix = '\0';
347 pos += m_strputc(dst + pos, dlen - pos, *fmt++);
352 flags |= M_FORMAT_OPTIONAL;
356 flags &= ~M_FORMAT_OPTIONAL;
358 /* eat the format string */
359 pfxlen = strspn(fmt, "0123456789.-");
360 m_strncpy(prefix, sizeof(prefix), fmt, pfxlen);
364 /* save the character to switch on */
368 if (flags & M_FORMAT_OPTIONAL) {
373 break; /* bad format */
375 /* eat the `if' part of the string */
376 iflen = strcspn(fmt, "?&");
377 m_strncpy(ifstr, ssizeof(ifstr), fmt, iflen);
380 /* eat the `else' part of the string (optional) */
382 fmt++; /* skip the & */
384 p = m_strchrnul(fmt, '?');
385 m_strncpy(elstr, ssizeof(elstr), fmt, p - fmt);
388 if (!*fmt++) /* move past the trailing `?' */
389 break; /* bad format */
393 char lower, nodots, buf[LONG_STRING];
395 case '>': /* right justify to EOL */
396 width -= mutt_strwidth(dst);
398 ch = *fmt++; /* pad char */
401 m_strformat(buf, sizeof(buf), 0, fmt, callback, cdata, flags);
402 width -= mutt_strwidth(buf);
403 pos += m_strpad(dst + pos, dlen - pos, ch, width);
404 pos += m_strcpy(dst + pos, dlen - pos, buf);
406 return pos; /* skip rest of input */
408 case '|': /* pad to EOL */
409 width -= mutt_strwidth(dst);
410 return pos + m_strpad(dst + pos, dlen - pos, *fmt, width);
415 while (ch == '_' || ch == ':') {
421 /* use callback function to handle this case */
422 fmt = callback(buf, sizeof (buf), ch, fmt, prefix,
423 ifstr, elstr, cdata, flags);
431 for (p = buf; *p; p++) {
437 pos += m_strcpy(dst + pos, dlen - pos, buf);
446 switch ((ch = *fmt++)) {
447 case 'n': pos += m_strputc(dst + pos, dlen - pos, '\n'); break;
448 case 't': pos += m_strputc(dst + pos, dlen - pos, '\t'); break;
449 case 'r': pos += m_strputc(dst + pos, dlen - pos, '\r'); break;
450 case 'f': pos += m_strputc(dst + pos, dlen - pos, '\f'); break;
451 case 'v': pos += m_strputc(dst + pos, dlen - pos, '\v'); break;
452 default: pos += m_strputc(dst + pos, dlen - pos, ch); break;
455 ssize_t len = strcspn(fmt, "%\\");
457 pos += m_strncpy(dst + pos, dlen - pos, fmt, len);
465 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
466 int mutt_save_confirm (const char *s, struct stat *st)
468 char tmp[_POSIX_PATH_MAX];
473 magic = mx_get_magic (s);
475 if (magic == M_POP) {
476 mutt_error _("Can't save message to POP mailbox.");
482 if (magic == M_NNTP) {
483 mutt_error _("Can't save message to newsserver.");
489 if (magic > 0 && !mx_access (s, W_OK)) {
490 if (option (OPTCONFIRMAPPEND) &&
491 (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
492 /* if we're appending to the trash, there's no point in asking */
493 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
494 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
501 if (stat (s, st) != -1) {
503 mutt_error (_("%s is not a mailbox!"), s);
512 if (errno == ENOENT) {
513 if (option (OPTCONFIRMCREATE)) {
514 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
515 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
527 CLEARLINE (LINES - 1);
531 void mutt_sleep (short s)
533 sleep(MAX(s, SleepTime));
536 const char *mutt_make_version (int full)
538 static char vstring[STRING];
541 snprintf (vstring, sizeof (vstring),
542 "Madmutt/%s-r%s (based on Mutt 1.5.11)",
543 MUTT_VERSION, MUTT_REVISION);
545 snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
546 MUTT_VERSION, MUTT_REVISION);
550 /* return 1 if address lists are strictly identical */
551 static int mutt_cmp_addr (const address_t * a, const address_t * b)
554 if (m_strcmp(a->mailbox, b->mailbox) ||
555 m_strcmp(a->personal, b->personal))
567 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
570 if (m_strcmp(a->data, b->data))
582 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
585 if (m_strcmp(e1->message_id, e2->message_id) ||
586 m_strcmp(e1->subject, e2->subject) ||
587 !mutt_cmp_list (e1->references, e2->references) ||
588 !mutt_cmp_addr (e1->from, e2->from) ||
589 !mutt_cmp_addr (e1->sender, e2->sender) ||
590 !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
591 !mutt_cmp_addr (e1->to, e2->to) ||
592 !mutt_cmp_addr (e1->cc, e2->cc) ||
593 !mutt_cmp_addr (e1->return_path, e2->return_path))
599 if (e1 == NULL && e2 == NULL)
606 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
608 if (b1->type != b2->type ||
609 b1->encoding != b2->encoding ||
610 m_strcmp(b1->subtype, b2->subtype) ||
611 m_strcmp(b1->description, b2->description) ||
612 !parameter_equal(b1->parameter, b2->parameter) ||
613 b1->length != b2->length)
617 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
619 if (h1->received != h2->received ||
620 h1->date_sent != h2->date_sent ||
621 h1->content->length != h2->content->length ||
622 h1->lines != h2->lines ||
623 h1->zhours != h2->zhours ||
624 h1->zminutes != h2->zminutes ||
625 h1->zoccident != h2->zoccident ||
626 h1->mime != h2->mime ||
627 !mutt_cmp_env (h1->env, h2->env) ||
628 !mutt_cmp_body (h1->content, h2->content))
634 if (h1 == NULL && h2 == NULL)
642 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
645 char qc = 0; /* quote char */
648 /* reset the destination pointer to the beginning of the buffer */
649 dest->dptr = dest->data;
651 tok->dptr = vskipspaces(tok->dptr);
652 while ((ch = *tok->dptr)) {
654 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
655 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
656 || (ch == '=' && (flags & M_TOKEN_EQUAL))
657 || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
658 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
667 qc = 0; /* end of quote */
669 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
672 if (ch == '\\' && qc != '\'') {
674 return -1; /* premature end of token */
676 switch (ch = *tok->dptr++) {
680 return -1; /* premature end of token */
681 mutt_buffer_addch(dest,
682 (toupper((unsigned char)*tok->dptr) - 'A' + 1) & 0x7f);
686 mutt_buffer_addch(dest, '\r');
689 mutt_buffer_addch(dest, '\n');
692 mutt_buffer_addch(dest, '\t');
695 mutt_buffer_addch(dest, '\f');
698 mutt_buffer_addch(dest, '\033');
701 if (isdigit((unsigned char)ch)
702 && isdigit((unsigned char)*tok->dptr)
703 && isdigit((unsigned char)*(tok->dptr + 1)))
705 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
706 *(tok->dptr + 1) - 3504);
709 mutt_buffer_addch(dest, ch);
713 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
715 return -1; /* premature end of token */
718 mutt_buffer_addch(dest, ch);
721 mutt_buffer_addch(dest, '\033');
723 if (isalpha((unsigned char)ch)) {
724 mutt_buffer_addch(dest, toupper((unsigned char)ch) - 'A' + 1);
726 mutt_buffer_addch(dest, '^');
727 mutt_buffer_addch(dest, ch);
730 if (ch == '`' && (!qc || qc == '"')) {
740 if ((pc = strpbrk(pc, "\\`"))) {
741 /* skip any quoted chars */
745 } while (pc && *pc != '`');
750 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
751 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
761 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
763 mutt_wait_filter(pid);
765 /* if we got output, make a new string consiting of the shell ouptput
766 plus whatever else was left on the original line */
767 /* BUT: If this is inside a quoted string, directly add output to
769 if (expn.data && qc) {
770 mutt_buffer_addstr(dest, expn.data);
771 p_delete(&expn.data);
774 expnlen = m_strlen(expn.data);
775 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
776 ptr = xmalloc(tok->dsize);
777 memcpy(ptr, expn.data, expnlen);
778 m_strcpy(ptr + expnlen, tok->dsize - expnlen, tok->dptr);
780 p_delete(&tok->data);
783 tok->destroy = 1; /* mark that the caller should destroy this data */
785 p_delete(&expn.data);
788 if (ch == '$' && (!qc || qc == '"')
789 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
791 char *env = NULL, *var = NULL;
793 if (*tok->dptr == '{') {
795 if ((pc = strchr (tok->dptr, '}'))) {
796 var = p_dupstr(tok->dptr, pc - tok->dptr);
800 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
802 var = p_dupstr(tok->dptr, pc - tok->dptr);
807 if ((env = getenv (var))
808 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
810 mutt_buffer_addstr (dest, env);
815 mutt_buffer_addch(dest, ch);
818 mutt_buffer_addch(dest, 0); /* terminate the string */
819 tok->dptr = vskipspaces(tok->dptr);