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(Tempdir) : dir, s);
44 void mutt_mktemp(char *s)
46 int fd = m_tempfd(s, _POSIX_PATH_MAX, NONULL(Tempdir), NULL);
55 ssize_t _mutt_expand_path(char *s, ssize_t slen, int rx)
57 char p[_POSIX_PATH_MAX] = "";
58 char tmp[_POSIX_PATH_MAX];
59 const char *tail = "";
62 const address_t *alias;
66 if (s[1] == '/' || s[1] == '\0') {
67 m_strcpy(p, sizeof(p), Homedir);
71 tail = m_strchrnul(s + 1, '/');
73 m_strncpy(tmp, sizeof(tmp), s + 1, tail - s - 1);
75 if ((pw = getpwnam(tmp))) {
76 m_strcpy(p, sizeof(p), pw->pw_dir);
86 /* if folder = imap[s]://host/: don't append slash */
87 if (imap_is_magic(NONULL(Maildir), NULL) == M_IMAP
88 && Maildir[m_strlen(Maildir) - 1] == '/') {
89 m_strcpy(p, sizeof(p), Maildir);
91 snprintf(p, sizeof(p), "%s/", NONULL(Maildir));
97 /* elm compatibility, @ expands alias to user name */
100 if ((alias = alias_lookup(s + 1))) {
103 h.env = envelope_new();
104 h.env->from = h.env->to = (address_t *)alias;
105 mutt_default_save(p, sizeof (p), &h);
106 h.env->from = h.env->to = NULL;
110 /* recurse iff the result do not starts with '@' */
111 m_strcpy(s, slen, p);
118 m_strcpy(p, sizeof(p), Inbox);
123 m_strcpy(p, sizeof(p), Outbox);
129 m_strcpy(p, sizeof(p), LastFolder);
132 m_strcpy(p, sizeof(p), Spoolfile);
138 m_strcpy(p, sizeof(p), NONULL(LastFolder));
143 m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
154 char q[_POSIX_PATH_MAX];
155 rx_sanitize_string(q, sizeof(q), p);
156 snprintf(tmp, sizeof(tmp), "%s%s", q, tail);
158 snprintf(tmp, sizeof(tmp), "%s%s", p, tail);
161 return m_strcpy(s, slen, tmp);
164 /* collapse the pathname using ~ or = when possible */
165 void mutt_pretty_mailbox (char *s)
171 scheme = url_check_scheme (s);
173 if (scheme == U_IMAP || scheme == U_IMAPS) {
174 imap_pretty_mailbox (s);
178 /* if s is an url, only collapse path component */
179 if (scheme != U_UNKNOWN) {
180 p = strchr (s, ':') + 1;
181 if (!m_strncmp (p, "//", 2))
182 q = strchr (p + 2, '/');
184 q = strchr (p, '\0');
188 /* first attempt to collapse the pathname */
190 if (*p == '/' && p[1] == '/') {
194 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
203 if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
206 memmove (s, s + len, m_strlen(s + len) + 1);
208 else if (m_strncmp(s, Homedir, (len = m_strlen(Homedir))) == 0 &&
211 memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
215 /* return 0 on success, -1 on abort, 1 on error */
216 int mutt_check_overwrite (const char *attname, const char *path,
217 char *fname, ssize_t flen, int *append,
221 char tmp[_POSIX_PATH_MAX];
224 m_strcpy(fname, flen, path);
225 if (access (fname, F_OK) != 0)
227 if (stat (fname, &st) != 0)
229 if (S_ISDIR (st.st_mode)) {
231 switch (mutt_multi_choice
232 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
235 m_strreplace(directory, fname);
249 if ((rc = mutt_yesorno(_("File is a directory, save under it?"),
251 return (rc == M_NO) ? 1 : -1;
253 if (!attname || !attname[0]) {
255 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
256 M_FILE | M_CLEAR) != 0 || !tmp[0])
258 mutt_concat_path(fname, flen, path, tmp);
261 mutt_concat_path(fname, flen, path, mutt_basename(attname));
264 if (*append == 0 && access (fname, F_OK) == 0) {
265 switch (mutt_multi_choice
266 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
274 *append = M_SAVE_APPEND;
276 case 1: /* overwrite */
277 *append = M_SAVE_OVERWRITE;
284 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
286 if (a && a->mailbox) {
287 m_strcpy(d, dsize, a->mailbox);
289 if (!option(OPTSAVEADDRESS)) {
290 char *p = strpbrk(d, "%@");
300 void mutt_safe_path(char *s, ssize_t l, address_t *a)
302 mutt_save_path(s, l, a);
305 if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
311 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
312 int mutt_save_confirm (const char *s, struct stat *st)
314 char tmp[_POSIX_PATH_MAX];
319 magic = mx_get_magic (s);
321 if (magic == M_POP) {
322 mutt_error _("Can't save message to POP mailbox.");
328 if (magic == M_NNTP) {
329 mutt_error _("Can't save message to newsserver.");
335 if (magic > 0 && !mx_access (s, W_OK)) {
336 if (option (OPTCONFIRMAPPEND) &&
337 (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
338 /* if we're appending to the trash, there's no point in asking */
339 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
340 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
347 if (stat (s, st) != -1) {
349 mutt_error (_("%s is not a mailbox!"), s);
358 if (errno == ENOENT) {
359 if (option (OPTCONFIRMCREATE)) {
360 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
361 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
373 CLEARLINE (LINES - 1);
377 void mutt_sleep (short s)
379 sleep(MAX(s, SleepTime));
382 const char *mutt_make_version(void)
384 static char vstring[STRING];
385 snprintf(vstring, sizeof (vstring), "Madmutt/%s", MUTT_VERSION);
389 /* return 1 if address lists are strictly identical */
390 static int mutt_cmp_addr (const address_t * a, const address_t * b)
393 if (m_strcmp(a->mailbox, b->mailbox) ||
394 m_strcmp(a->personal, b->personal))
406 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
409 if (m_strcmp(a->data, b->data))
421 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
424 if (m_strcmp(e1->message_id, e2->message_id) ||
425 m_strcmp(e1->subject, e2->subject) ||
426 !mutt_cmp_list (e1->references, e2->references) ||
427 !mutt_cmp_addr (e1->from, e2->from) ||
428 !mutt_cmp_addr (e1->sender, e2->sender) ||
429 !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
430 !mutt_cmp_addr (e1->to, e2->to) ||
431 !mutt_cmp_addr (e1->cc, e2->cc) ||
432 !mutt_cmp_addr (e1->return_path, e2->return_path))
438 if (e1 == NULL && e2 == NULL)
445 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
447 if (b1->type != b2->type ||
448 b1->encoding != b2->encoding ||
449 m_strcmp(b1->subtype, b2->subtype) ||
450 m_strcmp(b1->description, b2->description) ||
451 !parameter_equal(b1->parameter, b2->parameter) ||
452 b1->length != b2->length)
456 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
458 if (h1->received != h2->received ||
459 h1->date_sent != h2->date_sent ||
460 h1->content->length != h2->content->length ||
461 h1->lines != h2->lines ||
462 h1->zhours != h2->zhours ||
463 h1->zminutes != h2->zminutes ||
464 h1->zoccident != h2->zoccident ||
465 h1->mime != h2->mime ||
466 !mutt_cmp_env (h1->env, h2->env) ||
467 !mutt_cmp_body (h1->content, h2->content))
473 if (h1 == NULL && h2 == NULL)
481 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
484 char qc = 0; /* quote char */
487 /* reset the destination pointer to the beginning of the buffer */
488 dest->dptr = dest->data;
490 tok->dptr = vskipspaces(tok->dptr);
491 while ((ch = *tok->dptr)) {
493 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
494 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
495 || (ch == '=' && (flags & M_TOKEN_EQUAL))
496 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
505 qc = 0; /* end of quote */
507 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
510 if (ch == '\\' && qc != '\'') {
512 return -1; /* premature end of token */
514 switch (ch = *tok->dptr++) {
518 return -1; /* premature end of token */
519 mutt_buffer_addch(dest,
520 (toupper((unsigned char)*tok->dptr) - 'A' + 1) & 0x7f);
524 mutt_buffer_addch(dest, '\r');
527 mutt_buffer_addch(dest, '\n');
530 mutt_buffer_addch(dest, '\t');
533 mutt_buffer_addch(dest, '\f');
536 mutt_buffer_addch(dest, '\033');
539 if (isdigit((unsigned char)ch)
540 && isdigit((unsigned char)*tok->dptr)
541 && isdigit((unsigned char)*(tok->dptr + 1)))
543 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
544 *(tok->dptr + 1) - 3504);
547 mutt_buffer_addch(dest, ch);
551 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
553 return -1; /* premature end of token */
556 mutt_buffer_addch(dest, ch);
559 mutt_buffer_addch(dest, '\033');
561 if (isalpha((unsigned char)ch)) {
562 mutt_buffer_addch(dest, toupper((unsigned char)ch) - 'A' + 1);
564 mutt_buffer_addch(dest, '^');
565 mutt_buffer_addch(dest, ch);
568 if (ch == '`' && (!qc || qc == '"')) {
578 if ((pc = strpbrk(pc, "\\`"))) {
579 /* skip any quoted chars */
583 } while (pc && *pc != '`');
588 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
589 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
599 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
601 mutt_wait_filter(pid);
603 /* if we got output, make a new string consiting of the shell ouptput
604 plus whatever else was left on the original line */
605 /* BUT: If this is inside a quoted string, directly add output to
607 if (expn.data && qc) {
608 mutt_buffer_addstr(dest, expn.data);
609 p_delete(&expn.data);
612 expnlen = m_strlen(expn.data);
613 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
614 ptr = xmalloc(tok->dsize);
615 memcpy(ptr, expn.data, expnlen);
616 m_strcpy(ptr + expnlen, tok->dsize - expnlen, tok->dptr);
618 p_delete(&tok->data);
621 tok->destroy = 1; /* mark that the caller should destroy this data */
623 p_delete(&expn.data);
626 if (ch == '$' && (!qc || qc == '"')
627 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
629 char *env = NULL, *var = NULL;
631 if (*tok->dptr == '{') {
633 if ((pc = strchr (tok->dptr, '}'))) {
634 var = p_dupstr(tok->dptr, pc - tok->dptr);
638 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
640 var = p_dupstr(tok->dptr, pc - tok->dptr);
645 if ((env = getenv (var))
646 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
648 mutt_buffer_addstr (dest, env);
653 mutt_buffer_addch(dest, ch);
656 mutt_buffer_addch(dest, 0); /* terminate the string */
657 tok->dptr = vskipspaces(tok->dptr);