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>
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(Tempdir) : dir, s);
45 void mutt_mktemp(char *s)
47 int fd = m_tempfd(s, _POSIX_PATH_MAX, NONULL(Tempdir), NULL);
56 ssize_t _mutt_expand_path(char *s, ssize_t slen, int rx)
58 char p[_POSIX_PATH_MAX] = "";
59 char tmp[_POSIX_PATH_MAX];
60 const char *tail = "";
63 const address_t *alias;
67 if (s[1] == '/' || s[1] == '\0') {
68 m_strcpy(p, sizeof(p), Homedir);
72 tail = m_strchrnul(s + 1, '/');
74 m_strncpy(tmp, sizeof(tmp), s + 1, tail - s - 1);
76 if ((pw = getpwnam(tmp))) {
77 m_strcpy(p, sizeof(p), pw->pw_dir);
87 /* if folder = imap[s]://host/: don't append slash */
88 if (imap_is_magic(NONULL(Maildir), NULL) == M_IMAP
89 && Maildir[m_strlen(Maildir) - 1] == '/') {
90 m_strcpy(p, sizeof(p), Maildir);
92 snprintf(p, sizeof(p), "%s/", NONULL(Maildir));
98 /* elm compatibility, @ expands alias to user name */
101 if ((alias = alias_lookup(s + 1))) {
104 h.env = envelope_new();
105 h.env->from = h.env->to = (address_t *)alias;
106 mutt_default_save(p, sizeof (p), &h);
107 h.env->from = h.env->to = NULL;
111 /* recurse iff the result do not starts with '@' */
112 m_strcpy(s, slen, p);
119 m_strcpy(p, sizeof(p), Inbox);
124 m_strcpy(p, sizeof(p), Outbox);
130 m_strcpy(p, sizeof(p), LastFolder);
133 m_strcpy(p, sizeof(p), Spoolfile);
139 m_strcpy(p, sizeof(p), NONULL(LastFolder));
144 m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
155 char q[_POSIX_PATH_MAX];
156 rx_sanitize_string(q, sizeof(q), p);
157 snprintf(tmp, sizeof(tmp), "%s%s", q, tail);
159 snprintf(tmp, sizeof(tmp), "%s%s", p, tail);
162 return m_strcpy(s, slen, tmp);
165 /* collapse the pathname using ~ or = when possible */
166 void mutt_pretty_mailbox (char *s)
172 scheme = url_check_scheme (s);
174 if (scheme == U_IMAP || scheme == U_IMAPS) {
175 imap_pretty_mailbox (s);
179 /* if s is an url, only collapse path component */
180 if (scheme != U_UNKNOWN) {
181 p = strchr (s, ':') + 1;
182 if (!m_strncmp (p, "//", 2))
183 q = strchr (p + 2, '/');
185 q = strchr (p, '\0');
189 /* first attempt to collapse the pathname */
191 if (*p == '/' && p[1] == '/') {
195 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
204 if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
207 memmove (s, s + len, m_strlen(s + len) + 1);
209 else if (m_strncmp(s, Homedir, (len = m_strlen(Homedir))) == 0 &&
212 memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
216 /* return 0 on success, -1 on abort, 1 on error */
217 int mutt_check_overwrite (const char *attname, const char *path,
218 char *fname, ssize_t flen, int *append,
222 char tmp[_POSIX_PATH_MAX];
225 m_strcpy(fname, flen, path);
226 if (access (fname, F_OK) != 0)
228 if (stat (fname, &st) != 0)
230 if (S_ISDIR (st.st_mode)) {
232 switch (mutt_multi_choice
233 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
236 m_strreplace(directory, fname);
250 if ((rc = mutt_yesorno(_("File is a directory, save under it?"),
252 return (rc == M_NO) ? 1 : -1;
254 if (!attname || !attname[0]) {
256 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
257 M_FILE | M_CLEAR) != 0 || !tmp[0])
259 mutt_concat_path(fname, flen, path, tmp);
262 mutt_concat_path(fname, flen, path, mutt_basename(attname));
265 if (*append == 0 && access (fname, F_OK) == 0) {
266 switch (mutt_multi_choice
267 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
275 *append = M_SAVE_APPEND;
277 case 1: /* overwrite */
278 *append = M_SAVE_OVERWRITE;
285 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
287 if (a && a->mailbox) {
288 m_strcpy(d, dsize, a->mailbox);
290 if (!option(OPTSAVEADDRESS)) {
291 char *p = strpbrk(d, "%@");
301 void mutt_safe_path(char *s, ssize_t l, address_t *a)
303 mutt_save_path(s, l, a);
306 if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
312 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
313 int mutt_save_confirm (const char *s, struct stat *st)
315 char tmp[_POSIX_PATH_MAX];
320 magic = mx_get_magic (s);
322 if (magic == M_POP) {
323 mutt_error _("Can't save message to POP mailbox.");
329 if (magic == M_NNTP) {
330 mutt_error _("Can't save message to newsserver.");
336 if (magic > 0 && !mx_access (s, W_OK)) {
337 if (option (OPTCONFIRMAPPEND) &&
338 (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
339 /* if we're appending to the trash, there's no point in asking */
340 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
341 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
348 if (stat (s, st) != -1) {
350 mutt_error (_("%s is not a mailbox!"), s);
359 if (errno == ENOENT) {
360 if (option (OPTCONFIRMCREATE)) {
361 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
362 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
374 CLEARLINE (LINES - 1);
378 void mutt_sleep (short s)
380 sleep(MAX(s, SleepTime));
383 const char *mutt_make_version (int full)
385 static char vstring[STRING];
388 snprintf (vstring, sizeof (vstring),
389 "Madmutt/%s-r%s (based on Mutt 1.5.11)",
390 MUTT_VERSION, MUTT_REVISION);
392 snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
393 MUTT_VERSION, MUTT_REVISION);
397 /* return 1 if address lists are strictly identical */
398 static int mutt_cmp_addr (const address_t * a, const address_t * b)
401 if (m_strcmp(a->mailbox, b->mailbox) ||
402 m_strcmp(a->personal, b->personal))
414 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
417 if (m_strcmp(a->data, b->data))
429 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
432 if (m_strcmp(e1->message_id, e2->message_id) ||
433 m_strcmp(e1->subject, e2->subject) ||
434 !mutt_cmp_list (e1->references, e2->references) ||
435 !mutt_cmp_addr (e1->from, e2->from) ||
436 !mutt_cmp_addr (e1->sender, e2->sender) ||
437 !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
438 !mutt_cmp_addr (e1->to, e2->to) ||
439 !mutt_cmp_addr (e1->cc, e2->cc) ||
440 !mutt_cmp_addr (e1->return_path, e2->return_path))
446 if (e1 == NULL && e2 == NULL)
453 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
455 if (b1->type != b2->type ||
456 b1->encoding != b2->encoding ||
457 m_strcmp(b1->subtype, b2->subtype) ||
458 m_strcmp(b1->description, b2->description) ||
459 !parameter_equal(b1->parameter, b2->parameter) ||
460 b1->length != b2->length)
464 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
466 if (h1->received != h2->received ||
467 h1->date_sent != h2->date_sent ||
468 h1->content->length != h2->content->length ||
469 h1->lines != h2->lines ||
470 h1->zhours != h2->zhours ||
471 h1->zminutes != h2->zminutes ||
472 h1->zoccident != h2->zoccident ||
473 h1->mime != h2->mime ||
474 !mutt_cmp_env (h1->env, h2->env) ||
475 !mutt_cmp_body (h1->content, h2->content))
481 if (h1 == NULL && h2 == NULL)
489 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
492 char qc = 0; /* quote char */
495 /* reset the destination pointer to the beginning of the buffer */
496 dest->dptr = dest->data;
498 tok->dptr = vskipspaces(tok->dptr);
499 while ((ch = *tok->dptr)) {
501 if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
502 || (ch == '#' && !(flags & M_TOKEN_COMMENT))
503 || (ch == '=' && (flags & M_TOKEN_EQUAL))
504 || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
513 qc = 0; /* end of quote */
515 if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
518 if (ch == '\\' && qc != '\'') {
520 return -1; /* premature end of token */
522 switch (ch = *tok->dptr++) {
526 return -1; /* premature end of token */
527 mutt_buffer_addch(dest,
528 (toupper((unsigned char)*tok->dptr) - 'A' + 1) & 0x7f);
532 mutt_buffer_addch(dest, '\r');
535 mutt_buffer_addch(dest, '\n');
538 mutt_buffer_addch(dest, '\t');
541 mutt_buffer_addch(dest, '\f');
544 mutt_buffer_addch(dest, '\033');
547 if (isdigit((unsigned char)ch)
548 && isdigit((unsigned char)*tok->dptr)
549 && isdigit((unsigned char)*(tok->dptr + 1)))
551 mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
552 *(tok->dptr + 1) - 3504);
555 mutt_buffer_addch(dest, ch);
559 if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
561 return -1; /* premature end of token */
564 mutt_buffer_addch(dest, ch);
567 mutt_buffer_addch(dest, '\033');
569 if (isalpha((unsigned char)ch)) {
570 mutt_buffer_addch(dest, toupper((unsigned char)ch) - 'A' + 1);
572 mutt_buffer_addch(dest, '^');
573 mutt_buffer_addch(dest, ch);
576 if (ch == '`' && (!qc || qc == '"')) {
586 if ((pc = strpbrk(pc, "\\`"))) {
587 /* skip any quoted chars */
591 } while (pc && *pc != '`');
596 cmd = p_dupstr(tok->dptr, pc - tok->dptr);
597 if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
607 expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
609 mutt_wait_filter(pid);
611 /* if we got output, make a new string consiting of the shell ouptput
612 plus whatever else was left on the original line */
613 /* BUT: If this is inside a quoted string, directly add output to
615 if (expn.data && qc) {
616 mutt_buffer_addstr(dest, expn.data);
617 p_delete(&expn.data);
620 expnlen = m_strlen(expn.data);
621 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
622 ptr = xmalloc(tok->dsize);
623 memcpy(ptr, expn.data, expnlen);
624 m_strcpy(ptr + expnlen, tok->dsize - expnlen, tok->dptr);
626 p_delete(&tok->data);
629 tok->destroy = 1; /* mark that the caller should destroy this data */
631 p_delete(&expn.data);
634 if (ch == '$' && (!qc || qc == '"')
635 && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
637 char *env = NULL, *var = NULL;
639 if (*tok->dptr == '{') {
641 if ((pc = strchr (tok->dptr, '}'))) {
642 var = p_dupstr(tok->dptr, pc - tok->dptr);
646 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
648 var = p_dupstr(tok->dptr, pc - tok->dptr);
653 if ((env = getenv (var))
654 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
656 mutt_buffer_addstr (dest, env);
661 mutt_buffer_addch(dest, ch);
664 mutt_buffer_addch(dest, 0); /* terminate the string */
665 tok->dptr = vskipspaces(tok->dptr);