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-ui/lib-ui.h>
17 #include <lib-sys/unix.h>
18 #include <imap/imap.h>
24 const char *madmutt_version = "Madmutt/" MUTT_VERSION;
26 /* Modified by blong to accept a "suggestion" for file name. If
27 * that file exists, then construct one with unique name but
28 * keep any extension. This might fail, I guess.
29 * Renamed to mutt_adv_mktemp so I only have to change where it's
30 * called, and not all possible cases.
32 void mutt_adv_mktemp (const char* dir, char *s, ssize_t l)
36 fd = m_tempfd(s, l, m_strisempty(dir) ? NONULL(mod_core.tmpdir) : dir, s);
45 void mutt_mktemp(char *s)
47 int fd = m_tempfd(s, _POSIX_PATH_MAX, NONULL(mod_core.tmpdir), NULL);
56 ssize_t _mutt_expand_path(char *buf, ssize_t len, const char *s, int rx)
58 char p[_POSIX_PATH_MAX] = "";
59 char tmp[_POSIX_PATH_MAX];
60 const char *tail = "";
61 const address_t *alias;
65 if (s[1] == '/' || s[1] == '\0') {
66 m_strcpy(p, sizeof(p), mod_core.homedir);
70 tail = m_strchrnul(s + 1, '/');
72 m_strncpy(tmp, sizeof(tmp), s + 1, tail - s - 1);
74 if ((pw = getpwnam(tmp))) {
75 m_strcpy(p, sizeof(p), pw->pw_dir);
85 /* if folder = imap[s]://host/: don't append slash */
86 if (imap_is_magic(NONULL(Maildir), NULL) == M_IMAP
87 && Maildir[m_strlen(Maildir) - 1] == '/') {
88 m_strcpy(p, sizeof(p), Maildir);
90 snprintf(p, sizeof(p), "%s/", NONULL(Maildir));
96 /* elm compatibility, @ expands alias to user name */
99 if ((alias = alias_lookup(s + 1))) {
102 h.env = envelope_new();
103 h.env->from = h.env->to = (address_t *)alias;
104 mutt_default_save(p, sizeof (p), &h);
105 h.env->from = h.env->to = NULL;
109 return _mutt_expand_path(buf, len, p, rx);
114 m_strcpy(p, sizeof(p), Inbox);
119 m_strcpy(p, sizeof(p), MAlias.record);
125 m_strcpy(p, sizeof(p), LastFolder);
128 m_strcpy(p, sizeof(p), Spoolfile);
134 m_strcpy(p, sizeof(p), NONULL(LastFolder));
139 m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
149 char q[_POSIX_PATH_MAX];
150 rx_sanitize_string(q, sizeof(q), p);
151 snprintf(tmp, sizeof(tmp), "%s%s", q, tail);
153 snprintf(tmp, sizeof(tmp), "%s%s", p, tail);
156 return m_strcpy(buf, len, tmp);
159 /* collapse the pathname using ~ or = when possible */
160 void mutt_pretty_mailbox (char *s)
166 scheme = url_check_scheme (s);
168 if (scheme == U_IMAP || scheme == U_IMAPS) {
169 imap_pretty_mailbox (s);
173 /* if s is an url, only collapse path component */
174 if (scheme != U_UNKNOWN) {
175 p = strchr (s, ':') + 1;
176 if (!m_strncmp (p, "//", 2))
177 q = strchr (p + 2, '/');
179 q = strchr (p, '\0');
183 /* first attempt to collapse the pathname */
185 if (*p == '/' && p[1] == '/') {
189 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
198 if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
201 memmove (s, s + len, m_strlen(s + len) + 1);
203 else if (m_strncmp(s, mod_core.homedir, (len = m_strlen(mod_core.homedir))) == 0
206 memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
210 /* return 0 on success, -1 on abort, 1 on error */
211 int mutt_check_overwrite (const char *attname, const char *path,
212 char *fname, ssize_t flen, int *append,
216 char tmp[_POSIX_PATH_MAX];
219 m_strcpy(fname, flen, path);
220 if (access (fname, F_OK) != 0)
222 if (stat (fname, &st) != 0)
224 if (S_ISDIR (st.st_mode)) {
226 switch (mutt_multi_choice
227 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
230 m_strreplace(directory, fname);
244 if ((rc = mutt_yesorno(_("File is a directory, save under it?"),
246 return (rc == M_NO) ? 1 : -1;
248 if (!attname || !attname[0]) {
250 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
251 M_FILE | M_CLEAR) != 0 || !tmp[0])
253 mutt_concat_path(fname, flen, path, tmp);
256 mutt_concat_path(fname, flen, path, mutt_basename(attname));
259 if (*append == 0 && access (fname, F_OK) == 0) {
260 switch (mutt_multi_choice
261 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
269 *append = M_SAVE_APPEND;
271 case 1: /* overwrite */
272 *append = M_SAVE_OVERWRITE;
279 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
281 if (a && a->mailbox) {
282 m_strncpy(d, dsize, a->mailbox, strcspn(d, "%@"));
289 void mutt_safe_path(char *s, ssize_t l, address_t *a)
291 mutt_save_path(s, l, a);
294 if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
300 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
301 int mutt_save_confirm (const char *s, struct stat *st)
303 char tmp[_POSIX_PATH_MAX];
308 magic = mx_get_magic (s);
310 if (magic == M_POP) {
311 mutt_error _("Can't save message to POP mailbox.");
316 if (magic > 0 && !mx_access (s, W_OK)) {
317 if (option (OPTCONFIRMAPPEND) &&
318 (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
319 /* if we're appending to the trash, there's no point in asking */
320 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
321 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
328 if (stat (s, st) != -1) {
330 mutt_error (_("%s is not a mailbox!"), s);
339 if (errno == ENOENT) {
340 if (option (OPTCONFIRMCREATE)) {
341 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
342 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
356 void mutt_sleep (short s)
358 sleep(MAX(s, SleepTime));
361 /* return 1 if address lists are strictly identical */
362 static int mutt_cmp_addr (const address_t * a, const address_t * b)
365 if (m_strcmp(a->mailbox, b->mailbox) ||
366 m_strcmp(a->personal, b->personal))
378 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
381 if (m_strcmp(a->data, b->data))
393 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
396 if (m_strcmp(e1->message_id, e2->message_id) ||
397 m_strcmp(e1->subject, e2->subject) ||
398 !mutt_cmp_list (e1->references, e2->references) ||
399 !mutt_cmp_addr (e1->from, e2->from) ||
400 !mutt_cmp_addr (e1->sender, e2->sender) ||
401 !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
402 !mutt_cmp_addr (e1->to, e2->to) ||
403 !mutt_cmp_addr (e1->cc, e2->cc) ||
404 !mutt_cmp_addr (e1->return_path, e2->return_path))
410 if (e1 == NULL && e2 == NULL)
417 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
419 if (b1->type != b2->type ||
420 b1->encoding != b2->encoding ||
421 m_strcmp(b1->subtype, b2->subtype) ||
422 m_strcmp(b1->description, b2->description) ||
423 !parameter_equal(b1->parameter, b2->parameter) ||
424 b1->length != b2->length)
428 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
430 if (h1->received != h2->received ||
431 h1->date_sent != h2->date_sent ||
432 h1->content->length != h2->content->length ||
433 h1->lines != h2->lines ||
434 h1->zhours != h2->zhours ||
435 h1->zminutes != h2->zminutes ||
436 h1->zoccident != h2->zoccident ||
437 h1->mime != h2->mime ||
438 !mutt_cmp_env (h1->env, h2->env) ||
439 !mutt_cmp_body (h1->content, h2->content))
445 if (h1 == NULL && h2 == NULL)
453 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
456 char qc = 0; /* quote char */
459 /* reset the destination pointer to the beginning of the buffer */
460 dest->dptr = dest->data;
462 tok->dptr = vskipspaces(tok->dptr);
463 while ((ch = *tok->dptr)) {
465 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
466 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
467 || (ch == '=' && (flags & M_TOKEN_EQUAL))
468 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
477 qc = 0; /* end of quote */
479 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
482 if (ch == '\\' && qc != '\'') {
484 return -1; /* premature end of token */
486 switch (ch = *tok->dptr++) {
490 return -1; /* premature end of token */
491 mutt_buffer_addch(dest,
492 (toupper((unsigned char)*tok->dptr) - 'A' + 1) & 0x7f);
496 mutt_buffer_addch(dest, '\r');
499 mutt_buffer_addch(dest, '\n');
502 mutt_buffer_addch(dest, '\t');
505 mutt_buffer_addch(dest, '\f');
508 mutt_buffer_addch(dest, '\033');
511 if (isdigit((unsigned char)ch)
512 && isdigit((unsigned char)*tok->dptr)
513 && isdigit((unsigned char)*(tok->dptr + 1)))
515 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
516 *(tok->dptr + 1) - 3504);
519 mutt_buffer_addch(dest, ch);
523 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
525 return -1; /* premature end of token */
528 mutt_buffer_addch(dest, ch);
531 mutt_buffer_addch(dest, '\033');
533 if (isalpha((unsigned char)ch)) {
534 mutt_buffer_addch(dest, toupper((unsigned char)ch) - 'A' + 1);
536 mutt_buffer_addch(dest, '^');
537 mutt_buffer_addch(dest, ch);
540 if (ch == '`' && (!qc || qc == '"')) {
550 if ((pc = strpbrk(pc, "\\`"))) {
551 /* skip any quoted chars */
555 } while (pc && *pc != '`');
560 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
561 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
571 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
573 mutt_wait_filter(pid);
575 /* if we got output, make a new string consiting of the shell ouptput
576 plus whatever else was left on the original line */
577 /* BUT: If this is inside a quoted string, directly add output to
579 if (expn.data && qc) {
580 mutt_buffer_addstr(dest, expn.data);
581 p_delete(&expn.data);
584 expnlen = m_strlen(expn.data);
585 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
586 ptr = xmalloc(tok->dsize);
587 memcpy(ptr, expn.data, expnlen);
588 m_strcpy(ptr + expnlen, tok->dsize - expnlen, tok->dptr);
590 p_delete(&tok->data);
593 tok->destroy = 1; /* mark that the caller should destroy this data */
595 p_delete(&expn.data);
598 if (ch == '$' && (!qc || qc == '"')
599 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
601 char *env = NULL, *var = NULL;
603 if (*tok->dptr == '{') {
605 if ((pc = strchr (tok->dptr, '}'))) {
606 var = p_dupstr(tok->dptr, pc - tok->dptr);
610 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
612 var = p_dupstr(tok->dptr, pc - tok->dptr);
617 if ((env = getenv (var))
618 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
620 mutt_buffer_addstr (dest, env);
625 mutt_buffer_addch(dest, ch);
628 mutt_buffer_addch(dest, 0); /* terminate the string */
629 tok->dptr = vskipspaces(tok->dptr);