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 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
330 int mutt_save_confirm (const char *s, struct stat *st)
332 char tmp[_POSIX_PATH_MAX];
337 magic = mx_get_magic (s);
339 if (magic == M_POP) {
340 mutt_error _("Can't save message to POP mailbox.");
346 if (magic == M_NNTP) {
347 mutt_error _("Can't save message to newsserver.");
353 if (magic > 0 && !mx_access (s, W_OK)) {
354 if (option (OPTCONFIRMAPPEND) &&
355 (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
356 /* if we're appending to the trash, there's no point in asking */
357 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
358 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
365 if (stat (s, st) != -1) {
367 mutt_error (_("%s is not a mailbox!"), s);
376 if (errno == ENOENT) {
377 if (option (OPTCONFIRMCREATE)) {
378 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
379 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
391 CLEARLINE (LINES - 1);
395 void mutt_sleep (short s)
397 sleep(MAX(s, SleepTime));
400 const char *mutt_make_version (int full)
402 static char vstring[STRING];
405 snprintf (vstring, sizeof (vstring),
406 "Madmutt/%s-r%s (based on Mutt 1.5.11)",
407 MUTT_VERSION, MUTT_REVISION);
409 snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
410 MUTT_VERSION, MUTT_REVISION);
414 /* return 1 if address lists are strictly identical */
415 static int mutt_cmp_addr (const address_t * a, const address_t * b)
418 if (m_strcmp(a->mailbox, b->mailbox) ||
419 m_strcmp(a->personal, b->personal))
431 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
434 if (m_strcmp(a->data, b->data))
446 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
449 if (m_strcmp(e1->message_id, e2->message_id) ||
450 m_strcmp(e1->subject, e2->subject) ||
451 !mutt_cmp_list (e1->references, e2->references) ||
452 !mutt_cmp_addr (e1->from, e2->from) ||
453 !mutt_cmp_addr (e1->sender, e2->sender) ||
454 !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
455 !mutt_cmp_addr (e1->to, e2->to) ||
456 !mutt_cmp_addr (e1->cc, e2->cc) ||
457 !mutt_cmp_addr (e1->return_path, e2->return_path))
463 if (e1 == NULL && e2 == NULL)
470 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
472 if (b1->type != b2->type ||
473 b1->encoding != b2->encoding ||
474 m_strcmp(b1->subtype, b2->subtype) ||
475 m_strcmp(b1->description, b2->description) ||
476 !parameter_equal(b1->parameter, b2->parameter) ||
477 b1->length != b2->length)
481 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
483 if (h1->received != h2->received ||
484 h1->date_sent != h2->date_sent ||
485 h1->content->length != h2->content->length ||
486 h1->lines != h2->lines ||
487 h1->zhours != h2->zhours ||
488 h1->zminutes != h2->zminutes ||
489 h1->zoccident != h2->zoccident ||
490 h1->mime != h2->mime ||
491 !mutt_cmp_env (h1->env, h2->env) ||
492 !mutt_cmp_body (h1->content, h2->content))
498 if (h1 == NULL && h2 == NULL)
506 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
509 char qc = 0; /* quote char */
512 /* reset the destination pointer to the beginning of the buffer */
513 dest->dptr = dest->data;
515 tok->dptr = vskipspaces(tok->dptr);
516 while ((ch = *tok->dptr)) {
518 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
519 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
520 || (ch == '=' && (flags & M_TOKEN_EQUAL))
521 || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
522 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
531 qc = 0; /* end of quote */
533 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
536 if (ch == '\\' && qc != '\'') {
538 return -1; /* premature end of token */
540 switch (ch = *tok->dptr++) {
544 return -1; /* premature end of token */
545 mutt_buffer_addch(dest,
546 (toupper((unsigned char)*tok->dptr) - 'A' + 1) & 0x7f);
550 mutt_buffer_addch(dest, '\r');
553 mutt_buffer_addch(dest, '\n');
556 mutt_buffer_addch(dest, '\t');
559 mutt_buffer_addch(dest, '\f');
562 mutt_buffer_addch(dest, '\033');
565 if (isdigit((unsigned char)ch)
566 && isdigit((unsigned char)*tok->dptr)
567 && isdigit((unsigned char)*(tok->dptr + 1)))
569 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
570 *(tok->dptr + 1) - 3504);
573 mutt_buffer_addch(dest, ch);
577 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
579 return -1; /* premature end of token */
582 mutt_buffer_addch(dest, ch);
585 mutt_buffer_addch(dest, '\033');
587 if (isalpha((unsigned char)ch)) {
588 mutt_buffer_addch(dest, toupper((unsigned char)ch) - 'A' + 1);
590 mutt_buffer_addch(dest, '^');
591 mutt_buffer_addch(dest, ch);
594 if (ch == '`' && (!qc || qc == '"')) {
604 if ((pc = strpbrk(pc, "\\`"))) {
605 /* skip any quoted chars */
609 } while (pc && *pc != '`');
614 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
615 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
625 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
627 mutt_wait_filter(pid);
629 /* if we got output, make a new string consiting of the shell ouptput
630 plus whatever else was left on the original line */
631 /* BUT: If this is inside a quoted string, directly add output to
633 if (expn.data && qc) {
634 mutt_buffer_addstr(dest, expn.data);
635 p_delete(&expn.data);
638 expnlen = m_strlen(expn.data);
639 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
640 ptr = xmalloc(tok->dsize);
641 memcpy(ptr, expn.data, expnlen);
642 m_strcpy(ptr + expnlen, tok->dsize - expnlen, tok->dptr);
644 p_delete(&tok->data);
647 tok->destroy = 1; /* mark that the caller should destroy this data */
649 p_delete(&expn.data);
652 if (ch == '$' && (!qc || qc == '"')
653 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
655 char *env = NULL, *var = NULL;
657 if (*tok->dptr == '{') {
659 if ((pc = strchr (tok->dptr, '}'))) {
660 var = p_dupstr(tok->dptr, pc - tok->dptr);
664 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
666 var = p_dupstr(tok->dptr, pc - tok->dptr);
671 if ((env = getenv (var))
672 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
674 mutt_buffer_addstr (dest, env);
679 mutt_buffer_addch(dest, ch);
682 mutt_buffer_addch(dest, 0); /* terminate the string */
683 tok->dptr = vskipspaces(tok->dptr);