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 /* Modified by blong to accept a "suggestion" for file name. If
25 * that file exists, then construct one with unique name but
26 * keep any extension. This might fail, I guess.
27 * Renamed to mutt_adv_mktemp so I only have to change where it's
28 * called, and not all possible cases.
30 void mutt_adv_mktemp (const char* dir, char *s, ssize_t l)
34 fd = m_tempfd(s, l, m_strisempty(dir) ? NONULL(mod_core.tmpdir) : dir, s);
43 void mutt_mktemp(char *s)
45 int fd = m_tempfd(s, _POSIX_PATH_MAX, NONULL(mod_core.tmpdir), NULL);
54 ssize_t _mutt_expand_path(char *buf, ssize_t len, const char *s, int rx)
56 char p[_POSIX_PATH_MAX] = "";
57 char tmp[_POSIX_PATH_MAX];
58 const char *tail = "";
59 const address_t *alias;
63 if (s[1] == '/' || s[1] == '\0') {
64 m_strcpy(p, sizeof(p), mod_core.homedir);
68 tail = m_strchrnul(s + 1, '/');
70 m_strncpy(tmp, sizeof(tmp), s + 1, tail - s - 1);
72 if ((pw = getpwnam(tmp))) {
73 m_strcpy(p, sizeof(p), pw->pw_dir);
83 /* if folder = imap[s]://host/: don't append slash */
84 if (imap_is_magic(NONULL(Maildir), NULL) == M_IMAP
85 && Maildir[m_strlen(Maildir) - 1] == '/') {
86 m_strcpy(p, sizeof(p), Maildir);
88 snprintf(p, sizeof(p), "%s/", NONULL(Maildir));
94 /* elm compatibility, @ expands alias to user name */
97 if ((alias = alias_lookup(s + 1))) {
100 h.env = envelope_new();
101 h.env->from = h.env->to = (address_t *)alias;
102 mutt_default_save(p, sizeof (p), &h);
103 h.env->from = h.env->to = NULL;
107 return _mutt_expand_path(buf, len, p, rx);
112 m_strcpy(p, sizeof(p), Inbox);
117 m_strcpy(p, sizeof(p), MAlias.record);
123 m_strcpy(p, sizeof(p), LastFolder);
126 m_strcpy(p, sizeof(p), Spoolfile);
132 m_strcpy(p, sizeof(p), NONULL(LastFolder));
137 m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
147 char q[_POSIX_PATH_MAX];
148 rx_sanitize_string(q, sizeof(q), p);
149 snprintf(tmp, sizeof(tmp), "%s%s", q, tail);
151 snprintf(tmp, sizeof(tmp), "%s%s", p, tail);
154 return m_strcpy(buf, len, tmp);
157 /* collapse the pathname using ~ or = when possible */
158 void mutt_pretty_mailbox (char *s)
164 scheme = url_check_scheme (s);
166 if (scheme == U_IMAP || scheme == U_IMAPS) {
167 imap_pretty_mailbox (s);
171 /* if s is an url, only collapse path component */
172 if (scheme != U_UNKNOWN) {
173 p = strchr (s, ':') + 1;
174 if (!m_strncmp (p, "//", 2))
175 q = strchr (p + 2, '/');
177 q = strchr (p, '\0');
181 /* first attempt to collapse the pathname */
183 if (*p == '/' && p[1] == '/') {
187 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
196 if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
199 memmove (s, s + len, m_strlen(s + len) + 1);
201 else if (m_strncmp(s, mod_core.homedir, (len = m_strlen(mod_core.homedir))) == 0
204 memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
208 /* return 0 on success, -1 on abort, 1 on error */
209 int mutt_check_overwrite (const char *attname, const char *path,
210 char *fname, ssize_t flen, int *append,
214 char tmp[_POSIX_PATH_MAX];
217 m_strcpy(fname, flen, path);
218 if (access (fname, F_OK) != 0)
220 if (stat (fname, &st) != 0)
222 if (S_ISDIR (st.st_mode)) {
224 switch (mutt_multi_choice
225 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
228 m_strreplace(directory, fname);
242 if ((rc = mutt_yesorno(_("File is a directory, save under it?"),
244 return (rc == M_NO) ? 1 : -1;
246 if (!attname || !attname[0]) {
248 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
249 M_FILE | M_CLEAR) != 0 || !tmp[0])
251 mutt_concat_path(fname, flen, path, tmp);
254 mutt_concat_path(fname, flen, path, mutt_basename(attname));
257 if (*append == 0 && access (fname, F_OK) == 0) {
258 switch (mutt_multi_choice
259 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
267 *append = M_SAVE_APPEND;
269 case 1: /* overwrite */
270 *append = M_SAVE_OVERWRITE;
277 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
279 if (a && a->mailbox) {
280 m_strncpy(d, dsize, a->mailbox, strcspn(d, "%@"));
287 void mutt_safe_path(char *s, ssize_t l, address_t *a)
289 mutt_save_path(s, l, a);
292 if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
298 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
299 int mutt_save_confirm (const char *s, struct stat *st)
301 char tmp[_POSIX_PATH_MAX];
306 magic = mx_get_magic (s);
308 if (magic == M_POP) {
309 mutt_error _("Can't save message to POP mailbox.");
314 if (magic > 0 && !mx_access (s, W_OK)) {
315 if (option (OPTCONFIRMAPPEND) &&
316 (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
317 /* if we're appending to the trash, there's no point in asking */
318 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
319 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
326 if (stat (s, st) != -1) {
328 mutt_error (_("%s is not a mailbox!"), s);
337 if (errno == ENOENT) {
338 if (option (OPTCONFIRMCREATE)) {
339 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
340 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
354 void mutt_sleep (short s)
356 sleep(MAX(s, SleepTime));
359 const char *mutt_make_version(void)
361 static char vstring[STRING];
362 snprintf(vstring, sizeof (vstring), "Madmutt/%s", MUTT_VERSION);
366 /* return 1 if address lists are strictly identical */
367 static int mutt_cmp_addr (const address_t * a, const address_t * b)
370 if (m_strcmp(a->mailbox, b->mailbox) ||
371 m_strcmp(a->personal, b->personal))
383 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
386 if (m_strcmp(a->data, b->data))
398 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
401 if (m_strcmp(e1->message_id, e2->message_id) ||
402 m_strcmp(e1->subject, e2->subject) ||
403 !mutt_cmp_list (e1->references, e2->references) ||
404 !mutt_cmp_addr (e1->from, e2->from) ||
405 !mutt_cmp_addr (e1->sender, e2->sender) ||
406 !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
407 !mutt_cmp_addr (e1->to, e2->to) ||
408 !mutt_cmp_addr (e1->cc, e2->cc) ||
409 !mutt_cmp_addr (e1->return_path, e2->return_path))
415 if (e1 == NULL && e2 == NULL)
422 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
424 if (b1->type != b2->type ||
425 b1->encoding != b2->encoding ||
426 m_strcmp(b1->subtype, b2->subtype) ||
427 m_strcmp(b1->description, b2->description) ||
428 !parameter_equal(b1->parameter, b2->parameter) ||
429 b1->length != b2->length)
433 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
435 if (h1->received != h2->received ||
436 h1->date_sent != h2->date_sent ||
437 h1->content->length != h2->content->length ||
438 h1->lines != h2->lines ||
439 h1->zhours != h2->zhours ||
440 h1->zminutes != h2->zminutes ||
441 h1->zoccident != h2->zoccident ||
442 h1->mime != h2->mime ||
443 !mutt_cmp_env (h1->env, h2->env) ||
444 !mutt_cmp_body (h1->content, h2->content))
450 if (h1 == NULL && h2 == NULL)
458 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
461 char qc = 0; /* quote char */
464 /* reset the destination pointer to the beginning of the buffer */
465 dest->dptr = dest->data;
467 tok->dptr = vskipspaces(tok->dptr);
468 while ((ch = *tok->dptr)) {
470 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
471 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
472 || (ch == '=' && (flags & M_TOKEN_EQUAL))
473 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
482 qc = 0; /* end of quote */
484 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
487 if (ch == '\\' && qc != '\'') {
489 return -1; /* premature end of token */
491 switch (ch = *tok->dptr++) {
495 return -1; /* premature end of token */
496 mutt_buffer_addch(dest,
497 (toupper((unsigned char)*tok->dptr) - 'A' + 1) & 0x7f);
501 mutt_buffer_addch(dest, '\r');
504 mutt_buffer_addch(dest, '\n');
507 mutt_buffer_addch(dest, '\t');
510 mutt_buffer_addch(dest, '\f');
513 mutt_buffer_addch(dest, '\033');
516 if (isdigit((unsigned char)ch)
517 && isdigit((unsigned char)*tok->dptr)
518 && isdigit((unsigned char)*(tok->dptr + 1)))
520 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
521 *(tok->dptr + 1) - 3504);
524 mutt_buffer_addch(dest, ch);
528 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
530 return -1; /* premature end of token */
533 mutt_buffer_addch(dest, ch);
536 mutt_buffer_addch(dest, '\033');
538 if (isalpha((unsigned char)ch)) {
539 mutt_buffer_addch(dest, toupper((unsigned char)ch) - 'A' + 1);
541 mutt_buffer_addch(dest, '^');
542 mutt_buffer_addch(dest, ch);
545 if (ch == '`' && (!qc || qc == '"')) {
555 if ((pc = strpbrk(pc, "\\`"))) {
556 /* skip any quoted chars */
560 } while (pc && *pc != '`');
565 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
566 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
576 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
578 mutt_wait_filter(pid);
580 /* if we got output, make a new string consiting of the shell ouptput
581 plus whatever else was left on the original line */
582 /* BUT: If this is inside a quoted string, directly add output to
584 if (expn.data && qc) {
585 mutt_buffer_addstr(dest, expn.data);
586 p_delete(&expn.data);
589 expnlen = m_strlen(expn.data);
590 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
591 ptr = xmalloc(tok->dsize);
592 memcpy(ptr, expn.data, expnlen);
593 m_strcpy(ptr + expnlen, tok->dsize - expnlen, tok->dptr);
595 p_delete(&tok->data);
598 tok->destroy = 1; /* mark that the caller should destroy this data */
600 p_delete(&expn.data);
603 if (ch == '$' && (!qc || qc == '"')
604 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
606 char *env = NULL, *var = NULL;
608 if (*tok->dptr == '{') {
610 if ((pc = strchr (tok->dptr, '}'))) {
611 var = p_dupstr(tok->dptr, pc - tok->dptr);
615 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
617 var = p_dupstr(tok->dptr, pc - tok->dptr);
622 if ((env = getenv (var))
623 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
625 mutt_buffer_addstr (dest, env);
630 mutt_buffer_addch(dest, ch);
633 mutt_buffer_addch(dest, 0); /* terminate the string */
634 tok->dptr = vskipspaces(tok->dptr);