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/curses.h>
17 #include <lib-ui/enter.h>
18 #include <lib-sys/unix.h>
19 #include <imap/imap.h>
25 /* Modified by blong to accept a "suggestion" for file name. If
26 * that file exists, then construct one with unique name but
27 * keep any extension. This might fail, I guess.
28 * Renamed to mutt_adv_mktemp so I only have to change where it's
29 * called, and not all possible cases.
31 void mutt_adv_mktemp (const char* dir, char *s, ssize_t l)
35 fd = m_tempfd(s, l, m_strisempty(dir) ? NONULL(MCore.tmpdir) : dir, s);
44 void mutt_mktemp(char *s)
46 int fd = m_tempfd(s, _POSIX_PATH_MAX, NONULL(MCore.tmpdir), NULL);
55 ssize_t _mutt_expand_path(char *buf, ssize_t len, const char *s, int rx)
57 char p[_POSIX_PATH_MAX] = "";
58 char tmp[_POSIX_PATH_MAX];
59 const char *tail = "";
60 const address_t *alias;
64 if (s[1] == '/' || s[1] == '\0') {
65 m_strcpy(p, sizeof(p), MCore.homedir);
69 tail = m_strchrnul(s + 1, '/');
71 m_strncpy(tmp, sizeof(tmp), s + 1, tail - s - 1);
73 if ((pw = getpwnam(tmp))) {
74 m_strcpy(p, sizeof(p), pw->pw_dir);
84 /* if folder = imap[s]://host/: don't append slash */
85 if (imap_is_magic(NONULL(Maildir), NULL) == M_IMAP
86 && Maildir[m_strlen(Maildir) - 1] == '/') {
87 m_strcpy(p, sizeof(p), Maildir);
89 snprintf(p, sizeof(p), "%s/", NONULL(Maildir));
95 /* elm compatibility, @ expands alias to user name */
98 if ((alias = alias_lookup(s + 1))) {
101 h.env = envelope_new();
102 h.env->from = h.env->to = (address_t *)alias;
103 mutt_default_save(p, sizeof (p), &h);
104 h.env->from = h.env->to = NULL;
108 return _mutt_expand_path(buf, len, p, rx);
113 m_strcpy(p, sizeof(p), Inbox);
118 m_strcpy(p, sizeof(p), Outbox);
124 m_strcpy(p, sizeof(p), LastFolder);
127 m_strcpy(p, sizeof(p), Spoolfile);
133 m_strcpy(p, sizeof(p), NONULL(LastFolder));
138 m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
148 char q[_POSIX_PATH_MAX];
149 rx_sanitize_string(q, sizeof(q), p);
150 snprintf(tmp, sizeof(tmp), "%s%s", q, tail);
152 snprintf(tmp, sizeof(tmp), "%s%s", p, tail);
155 return m_strcpy(buf, len, tmp);
158 /* collapse the pathname using ~ or = when possible */
159 void mutt_pretty_mailbox (char *s)
165 scheme = url_check_scheme (s);
167 if (scheme == U_IMAP || scheme == U_IMAPS) {
168 imap_pretty_mailbox (s);
172 /* if s is an url, only collapse path component */
173 if (scheme != U_UNKNOWN) {
174 p = strchr (s, ':') + 1;
175 if (!m_strncmp (p, "//", 2))
176 q = strchr (p + 2, '/');
178 q = strchr (p, '\0');
182 /* first attempt to collapse the pathname */
184 if (*p == '/' && p[1] == '/') {
188 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
197 if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
200 memmove (s, s + len, m_strlen(s + len) + 1);
202 else if (m_strncmp(s, MCore.homedir, (len = m_strlen(MCore.homedir))) == 0
205 memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
209 /* return 0 on success, -1 on abort, 1 on error */
210 int mutt_check_overwrite (const char *attname, const char *path,
211 char *fname, ssize_t flen, int *append,
215 char tmp[_POSIX_PATH_MAX];
218 m_strcpy(fname, flen, path);
219 if (access (fname, F_OK) != 0)
221 if (stat (fname, &st) != 0)
223 if (S_ISDIR (st.st_mode)) {
225 switch (mutt_multi_choice
226 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
229 m_strreplace(directory, fname);
243 if ((rc = mutt_yesorno(_("File is a directory, save under it?"),
245 return (rc == M_NO) ? 1 : -1;
247 if (!attname || !attname[0]) {
249 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
250 M_FILE | M_CLEAR) != 0 || !tmp[0])
252 mutt_concat_path(fname, flen, path, tmp);
255 mutt_concat_path(fname, flen, path, mutt_basename(attname));
258 if (*append == 0 && access (fname, F_OK) == 0) {
259 switch (mutt_multi_choice
260 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
268 *append = M_SAVE_APPEND;
270 case 1: /* overwrite */
271 *append = M_SAVE_OVERWRITE;
278 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
280 if (a && a->mailbox) {
281 m_strcpy(d, dsize, a->mailbox);
283 if (!option(OPTSAVEADDRESS)) {
284 char *p = strpbrk(d, "%@");
294 void mutt_safe_path(char *s, ssize_t l, address_t *a)
296 mutt_save_path(s, l, a);
299 if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
305 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
306 int mutt_save_confirm (const char *s, struct stat *st)
308 char tmp[_POSIX_PATH_MAX];
313 magic = mx_get_magic (s);
315 if (magic == M_POP) {
316 mutt_error _("Can't save message to POP mailbox.");
322 if (magic == M_NNTP) {
323 mutt_error _("Can't save message to newsserver.");
329 if (magic > 0 && !mx_access (s, W_OK)) {
330 if (option (OPTCONFIRMAPPEND) &&
331 (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
332 /* if we're appending to the trash, there's no point in asking */
333 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
334 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
341 if (stat (s, st) != -1) {
343 mutt_error (_("%s is not a mailbox!"), s);
352 if (errno == ENOENT) {
353 if (option (OPTCONFIRMCREATE)) {
354 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
355 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
367 CLEARLINE (LINES - 1);
371 void mutt_sleep (short s)
373 sleep(MAX(s, SleepTime));
376 const char *mutt_make_version(void)
378 static char vstring[STRING];
379 snprintf(vstring, sizeof (vstring), "Madmutt/%s", MUTT_VERSION);
383 /* return 1 if address lists are strictly identical */
384 static int mutt_cmp_addr (const address_t * a, const address_t * b)
387 if (m_strcmp(a->mailbox, b->mailbox) ||
388 m_strcmp(a->personal, b->personal))
400 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
403 if (m_strcmp(a->data, b->data))
415 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
418 if (m_strcmp(e1->message_id, e2->message_id) ||
419 m_strcmp(e1->subject, e2->subject) ||
420 !mutt_cmp_list (e1->references, e2->references) ||
421 !mutt_cmp_addr (e1->from, e2->from) ||
422 !mutt_cmp_addr (e1->sender, e2->sender) ||
423 !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
424 !mutt_cmp_addr (e1->to, e2->to) ||
425 !mutt_cmp_addr (e1->cc, e2->cc) ||
426 !mutt_cmp_addr (e1->return_path, e2->return_path))
432 if (e1 == NULL && e2 == NULL)
439 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
441 if (b1->type != b2->type ||
442 b1->encoding != b2->encoding ||
443 m_strcmp(b1->subtype, b2->subtype) ||
444 m_strcmp(b1->description, b2->description) ||
445 !parameter_equal(b1->parameter, b2->parameter) ||
446 b1->length != b2->length)
450 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
452 if (h1->received != h2->received ||
453 h1->date_sent != h2->date_sent ||
454 h1->content->length != h2->content->length ||
455 h1->lines != h2->lines ||
456 h1->zhours != h2->zhours ||
457 h1->zminutes != h2->zminutes ||
458 h1->zoccident != h2->zoccident ||
459 h1->mime != h2->mime ||
460 !mutt_cmp_env (h1->env, h2->env) ||
461 !mutt_cmp_body (h1->content, h2->content))
467 if (h1 == NULL && h2 == NULL)
475 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
478 char qc = 0; /* quote char */
481 /* reset the destination pointer to the beginning of the buffer */
482 dest->dptr = dest->data;
484 tok->dptr = vskipspaces(tok->dptr);
485 while ((ch = *tok->dptr)) {
487 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
488 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
489 || (ch == '=' && (flags & M_TOKEN_EQUAL))
490 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
499 qc = 0; /* end of quote */
501 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
504 if (ch == '\\' && qc != '\'') {
506 return -1; /* premature end of token */
508 switch (ch = *tok->dptr++) {
512 return -1; /* premature end of token */
513 mutt_buffer_addch(dest,
514 (toupper((unsigned char)*tok->dptr) - 'A' + 1) & 0x7f);
518 mutt_buffer_addch(dest, '\r');
521 mutt_buffer_addch(dest, '\n');
524 mutt_buffer_addch(dest, '\t');
527 mutt_buffer_addch(dest, '\f');
530 mutt_buffer_addch(dest, '\033');
533 if (isdigit((unsigned char)ch)
534 && isdigit((unsigned char)*tok->dptr)
535 && isdigit((unsigned char)*(tok->dptr + 1)))
537 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
538 *(tok->dptr + 1) - 3504);
541 mutt_buffer_addch(dest, ch);
545 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
547 return -1; /* premature end of token */
550 mutt_buffer_addch(dest, ch);
553 mutt_buffer_addch(dest, '\033');
555 if (isalpha((unsigned char)ch)) {
556 mutt_buffer_addch(dest, toupper((unsigned char)ch) - 'A' + 1);
558 mutt_buffer_addch(dest, '^');
559 mutt_buffer_addch(dest, ch);
562 if (ch == '`' && (!qc || qc == '"')) {
572 if ((pc = strpbrk(pc, "\\`"))) {
573 /* skip any quoted chars */
577 } while (pc && *pc != '`');
582 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
583 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
593 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
595 mutt_wait_filter(pid);
597 /* if we got output, make a new string consiting of the shell ouptput
598 plus whatever else was left on the original line */
599 /* BUT: If this is inside a quoted string, directly add output to
601 if (expn.data && qc) {
602 mutt_buffer_addstr(dest, expn.data);
603 p_delete(&expn.data);
606 expnlen = m_strlen(expn.data);
607 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
608 ptr = xmalloc(tok->dsize);
609 memcpy(ptr, expn.data, expnlen);
610 m_strcpy(ptr + expnlen, tok->dsize - expnlen, tok->dptr);
612 p_delete(&tok->data);
615 tok->destroy = 1; /* mark that the caller should destroy this data */
617 p_delete(&expn.data);
620 if (ch == '$' && (!qc || qc == '"')
621 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
623 char *env = NULL, *var = NULL;
625 if (*tok->dptr == '{') {
627 if ((pc = strchr (tok->dptr, '}'))) {
628 var = p_dupstr(tok->dptr, pc - tok->dptr);
632 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
634 var = p_dupstr(tok->dptr, pc - tok->dptr);
639 if ((env = getenv (var))
640 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
642 mutt_buffer_addstr (dest, env);
647 mutt_buffer_addch(dest, ch);
650 mutt_buffer_addch(dest, 0); /* terminate the string */
651 tok->dptr = vskipspaces(tok->dptr);