move more functions in file.c
[apps/madmutt.git] / muttlib.c
1 /*
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>
5  *
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.
9  */
10
11 #include <lib-lib/lib-lib.h>
12
13 #include <grp.h>
14 #include <pwd.h>
15
16 #include <lib-mime/mime.h>
17 #include <lib-ui/curses.h>
18 #include <lib-ui/enter.h>
19 #include <lib-sys/unix.h>
20 #include <lib-mx/mx.h>
21
22 #include "alias.h"
23 #include "mutt.h"
24 #include "attach.h"
25
26 #include "version.h"
27
28 #include <imap/imap.h>
29
30 #include <lib-crypt/crypt.h>
31
32 #define SW              (option(OPTMBOXPANE)?SidebarWidth:0)
33
34 /* Modified by blong to accept a "suggestion" for file name.  If
35  * that file exists, then construct one with unique name but 
36  * keep any extension.  This might fail, I guess.
37  * Renamed to mutt_adv_mktemp so I only have to change where it's
38  * called, and not all possible cases.
39  */
40 void mutt_adv_mktemp (const char* dir, char *s, ssize_t l)
41 {
42     int fd;
43
44     fd = m_tempfd(s, l, m_strisempty(dir) ? NONULL(Tempdir) : dir, s);
45     if (fd < 0) {
46         *s = '\0';
47     } else {
48         close(fd);
49         unlink(s);
50     }
51 }
52
53 /* returns true if the header contained in "s" is in list "t" */
54 int mutt_matches_ignore (const char *s, string_list_t * t)
55 {
56   for (; t; t = t->next) {
57     if (!ascii_strncasecmp (s, t->data, m_strlen(t->data))
58         || *t->data == '*')
59       return 1;
60   }
61   return 0;
62 }
63
64 ssize_t _mutt_expand_path(char *s, ssize_t slen, int rx)
65 {
66     char p[_POSIX_PATH_MAX] = "";
67     char tmp[_POSIX_PATH_MAX];
68     const char *tail = "";
69
70     do {
71         const address_t *alias;
72
73         switch (*s) {
74           case '~':
75             if (s[1] == '/' || s[1] == '\0') {
76                 m_strcpy(p, sizeof(p), Homedir);
77                 tail = s + 1;
78             } else {
79                 struct passwd *pw;
80                 tail = m_strchrnul(s + 1, '/');
81
82                 m_strncpy(tmp, sizeof(tmp), s + 1, tail - s - 1);
83
84                 if ((pw = getpwnam(tmp))) {
85                     m_strcpy(p, sizeof(p), pw->pw_dir);
86                 } else {
87                     /* user not found! */
88                     tail = s;
89                 }
90             }
91             break;
92
93           case '=':
94           case '+':
95             /* if folder = imap[s]://host/: don't append slash */
96             if (imap_is_magic(NONULL(Maildir), NULL) == M_IMAP
97             &&  Maildir[m_strlen(Maildir) - 1] == '/') {
98                 m_strcpy(p, sizeof(p), Maildir);
99             } else {
100                 snprintf(p, sizeof(p), "%s/", NONULL(Maildir));
101             }
102
103             tail = s + 1;
104             break;
105
106             /* elm compatibility, @ expands alias to user name */
107
108           case '@':
109             if ((alias = alias_lookup(s + 1))) {
110                 HEADER h;
111                 header_init(&h);
112                 h.env = envelope_new();
113                 h.env->from = h.env->to = (address_t *)alias;
114                 mutt_default_save(p, sizeof (p), &h);
115                 h.env->from = h.env->to = NULL;
116                 header_wipe(&h);
117
118                 if (*p != '@') {
119                     /* recurse iff the result do not starts with '@' */
120                     m_strcpy(s, slen, p);
121                     continue;
122                 }
123             }
124             break;
125
126           case '>':
127             m_strcpy(p, sizeof(p), Inbox);
128             tail = s + 1;
129             break;
130
131           case '<':
132             m_strcpy(p, sizeof(p), Outbox);
133             tail = s + 1;
134             break;
135
136           case '!':
137             if (s[1] == '!') {
138                 m_strcpy(p, sizeof(p), LastFolder);
139                 tail = s + 2;
140             } else {
141                 m_strcpy(p, sizeof(p), Spoolfile);
142                 tail = s + 1;
143             }
144             break;
145
146           case '-':
147             m_strcpy(p, sizeof(p), NONULL(LastFolder));
148             tail = s + 1;
149             break;
150
151           case '^':
152             m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
153             tail = s + 1;
154             break;
155
156           default:
157             *p = '\0';
158             tail = s;
159         }
160     } while (0);
161
162     if (rx) {
163         char q[_POSIX_PATH_MAX];
164         rx_sanitize_string(q, sizeof(q), p);
165         snprintf(tmp, sizeof(tmp), "%s%s", q, tail);
166     } else {
167         snprintf(tmp, sizeof(tmp), "%s%s", p, tail);
168     }
169
170     return m_strcpy(s, slen, tmp);
171 }
172
173 void mutt_mktemp(char *s)
174 {
175     int fd = m_tempfd(s, _POSIX_PATH_MAX, NONULL(Tempdir), NULL);
176     if (fd < 0) {
177         *s = '\0';
178     } else {
179         close(fd);
180         unlink(s);
181     }
182 }
183
184 /* collapse the pathname using ~ or = when possible */
185 void mutt_pretty_mailbox (char *s)
186 {
187   char *p = s, *q = s;
188   ssize_t len;
189   url_scheme_t scheme;
190
191   scheme = url_check_scheme (s);
192
193   if (scheme == U_IMAP || scheme == U_IMAPS) {
194     imap_pretty_mailbox (s);
195     return;
196   }
197
198   /* if s is an url, only collapse path component */
199   if (scheme != U_UNKNOWN) {
200     p = strchr (s, ':') + 1;
201     if (!strncmp (p, "//", 2))
202       q = strchr (p + 2, '/');
203     if (!q)
204       q = strchr (p, '\0');
205     p = q;
206   }
207
208   /* first attempt to collapse the pathname */
209   while (*p) {
210     if (*p == '/' && p[1] == '/') {
211       *q++ = '/';
212       p += 2;
213     }
214     else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
215       *q++ = '/';
216       p += 3;
217     }
218     else
219       *q++ = *p++;
220   }
221   *q = 0;
222
223   if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
224       s[len] == '/') {
225     *s++ = '=';
226     memmove (s, s + len, m_strlen(s + len) + 1);
227   }
228   else if (m_strncmp(s, Homedir, (len = m_strlen(Homedir))) == 0 &&
229            s[len] == '/') {
230     *s++ = '~';
231     memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
232   }
233 }
234
235 /* return 0 on success, -1 on abort, 1 on error */
236 int mutt_check_overwrite (const char *attname, const char *path,
237                           char *fname, ssize_t flen, int *append,
238                           char **directory)
239 {
240   int rc = 0;
241   char tmp[_POSIX_PATH_MAX];
242   struct stat st;
243
244   m_strcpy(fname, flen, path);
245   if (access (fname, F_OK) != 0)
246     return 0;
247   if (stat (fname, &st) != 0)
248     return -1;
249   if (S_ISDIR (st.st_mode)) {
250     if (directory) {
251       switch (mutt_multi_choice
252               (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
253                _("yna"))) {
254       case 3:                  /* all */
255         m_strreplace(directory, fname);
256         break;
257       case 1:                  /* yes */
258         p_delete(directory);
259         break;
260       case -1:                 /* abort */
261         p_delete(directory);
262         return -1;
263       case 2:                  /* no */
264         p_delete(directory);
265         return 1;
266       }
267     }
268     else
269       if ((rc = mutt_yesorno(_("File is a directory, save under it?"),
270                              M_YES)) != M_YES)
271       return (rc == M_NO) ? 1 : -1;
272
273     if (!attname || !attname[0]) {
274       tmp[0] = 0;
275       if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
276                           M_FILE | M_CLEAR) != 0 || !tmp[0])
277         return (-1);
278       mutt_concat_path(fname, flen, path, tmp);
279     }
280     else
281       mutt_concat_path(fname, flen, path, mutt_basename(attname));
282   }
283
284   if (*append == 0 && access (fname, F_OK) == 0) {
285     switch (mutt_multi_choice
286             (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
287     {
288     case -1:                   /* abort */
289       return -1;
290     case 3:                    /* cancel */
291       return 1;
292
293     case 2:                    /* append */
294       *append = M_SAVE_APPEND;
295       break;
296     case 1:                    /* overwrite */
297       *append = M_SAVE_OVERWRITE;
298       break;
299     }
300   }
301   return 0;
302 }
303
304 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
305 {
306     if (a && a->mailbox) {
307         m_strcpy(d, dsize, a->mailbox);
308
309         if (!option(OPTSAVEADDRESS)) {
310             char *p = strpbrk(d, "%@");
311             if (p)
312                 *p = '\0';
313         }
314         m_strtolower(d);
315     } else {
316         *d = '\0';
317     }
318 }
319
320 void mutt_safe_path(char *s, ssize_t l, address_t *a)
321 {
322     mutt_save_path(s, l, a);
323
324     while (*s) {
325         if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
326             *s = '_';
327         s++;
328     }
329 }
330
331 void mutt_FormatString (char *dest,     /* output buffer */
332                         ssize_t destlen, /* output buffer len */
333                         const char *src,        /* template string */
334                         format_t * callback,    /* callback for processing */
335                         unsigned long data,     /* callback data */
336                         format_flag flags)
337 {                               /* callback flags */
338   char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
339   char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
340   ssize_t wlen, wid, count, col, len;
341
342   prefix[0] = '\0';
343   destlen--;                    /* save room for the terminal \0 */
344   wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
345   col = wlen;
346
347   while (*src && wlen < destlen) {
348     if (*src == '%') {
349       if (*++src == '%') {
350         *wptr++ = '%';
351         wlen++;
352         col++;
353         src++;
354         continue;
355       }
356
357       if (*src == '?') {
358         flags |= M_FORMAT_OPTIONAL;
359         src++;
360       }
361       else {
362         flags &= ~M_FORMAT_OPTIONAL;
363
364         /* eat the format string */
365         cp = prefix;
366         count = 0;
367         while (count < ssizeof (prefix) &&
368                (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
369         {
370           *cp++ = *src++;
371           count++;
372         }
373         *cp = 0;
374       }
375
376       if (!*src)
377         break;                  /* bad format */
378
379       ch = *src++;              /* save the character to switch on */
380
381       if (flags & M_FORMAT_OPTIONAL) {
382         if (*src != '?')
383           break;                /* bad format */
384         src++;
385
386         /* eat the `if' part of the string */
387         cp = ifstring;
388         count = 0;
389         while (count < ssizeof (ifstring) && *src && *src != '?'
390                && *src != '&') {
391           *cp++ = *src++;
392           count++;
393         }
394         *cp = 0;
395
396         /* eat the `else' part of the string (optional) */
397         if (*src == '&')
398           src++;                /* skip the & */
399         cp = elsestring;
400         count = 0;
401         while (count < ssizeof (elsestring) && *src && *src != '?') {
402           *cp++ = *src++;
403           count++;
404         }
405         *cp = 0;
406
407         if (!*src)
408           break;                /* bad format */
409
410         src++;                  /* move past the trailing `?' */
411       }
412
413       /* handle generic cases first */
414       if (ch == '>') {
415         /* right justify to EOL */
416         ch = *src++;            /* pad char */
417         /* calculate space left on line.  if we've already written more data
418            than will fit on the line, ignore the rest of the line */
419         if (DrawFullLine || option (OPTSTATUSONTOP))
420           count = (COLS < destlen ? COLS : destlen);
421         else
422           count = ((COLS - SW) < destlen ? (COLS - SW) : destlen);
423         if (count > col) {
424           count -= col;         /* how many columns left on this line */
425           mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
426           wid = m_strlen(buf);
427           if (count > wid) {
428             count -= wid;       /* how many chars to pad */
429             memset (wptr, ch, count);
430             wptr += count;
431             col += count;
432           }
433           if (wid + wlen > destlen)
434             len = destlen - wlen;
435           else
436             len = wid;
437           memcpy (wptr, buf, len);
438           wptr += len;
439           wlen += len;
440           col += mutt_strwidth (buf);
441         }
442         break;                  /* skip rest of input */
443       }
444       else if (ch == '|') {
445         /* pad to EOL */
446         ch = *src++;
447         if (destlen > COLS)
448           destlen = COLS;
449         if (destlen > wlen) {
450           count = destlen - wlen;
451           memset (wptr, ch, count);
452           wptr += count;
453         }
454         break;                  /* skip rest of input */
455       }
456       else {
457         short lower = 0;
458         short nodots = 0;
459
460         while (ch == '_' || ch == ':') {
461           if (ch == '_')
462             lower = 1;
463           else if (ch == ':')
464             nodots = 1;
465
466           ch = *src++;
467         }
468
469         /* use callback function to handle this case */
470         src =
471           callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring,
472                     data, flags);
473
474         if (lower)
475           m_strtolower(buf);
476         if (nodots) {
477           char *p = buf;
478
479           for (; *p; p++)
480             if (*p == '.')
481               *p = '_';
482         }
483
484         if ((len = m_strlen(buf)) + wlen > destlen)
485           len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
486
487         memcpy (wptr, buf, len);
488         wptr += len;
489         wlen += len;
490         col += mutt_strwidth (buf);
491       }
492     }
493     else if (*src == '\\') {
494       if (!*++src)
495         break;
496       switch (*src) {
497       case 'n':
498         *wptr = '\n';
499         break;
500       case 't':
501         *wptr = '\t';
502         break;
503       case 'r':
504         *wptr = '\r';
505         break;
506       case 'f':
507         *wptr = '\f';
508         break;
509       case 'v':
510         *wptr = '\v';
511         break;
512       default:
513         *wptr = *src;
514         break;
515       }
516       src++;
517       wptr++;
518       wlen++;
519       col++;
520     }
521     else {
522       unsigned int bar = strcspn(src, "%\\");
523       char *bar2 = p_dupstr(src, bar);
524
525       while (bar--) {
526         *wptr++ = *src++;
527         wlen++;
528       }
529       col += mutt_strwidth (bar2);
530       p_delete(&bar2);
531     }
532   }
533   *wptr = 0;
534 }
535
536 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
537 int mutt_save_confirm (const char *s, struct stat *st)
538 {
539   char tmp[_POSIX_PATH_MAX];
540   int ret = 0;
541   int rc;
542   int magic = 0;
543
544   magic = mx_get_magic (s);
545
546   if (magic == M_POP) {
547     mutt_error _("Can't save message to POP mailbox.");
548
549     return 1;
550   }
551
552 #ifdef USE_NNTP
553   if (magic == M_NNTP) {
554     mutt_error _("Can't save message to newsserver.");
555
556     return 0;
557   }
558 #endif
559
560   if (magic > 0 && !mx_access (s, W_OK)) {
561     if (option (OPTCONFIRMAPPEND) &&
562         (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
563       /* if we're appending to the trash, there's no point in asking */
564       snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
565       if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
566         ret = 1;
567       else if (rc == -1)
568         ret = -1;
569     }
570   }
571
572   if (stat (s, st) != -1) {
573     if (magic == -1) {
574       mutt_error (_("%s is not a mailbox!"), s);
575       return 1;
576     }
577   } else {
578     if (magic != M_IMAP)
579     {
580       st->st_mtime = 0;
581       st->st_atime = 0;
582
583       if (errno == ENOENT) {
584         if (option (OPTCONFIRMCREATE)) {
585           snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
586           if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
587             ret = 1;
588           else if (rc == -1)
589             ret = -1;
590         }
591       } else {
592         mutt_perror (s);
593         return 1;
594       }
595     }
596   }
597
598   CLEARLINE (LINES - 1);
599   return (ret);
600 }
601
602 void mutt_sleep (short s)
603 {
604     sleep(MAX(s, SleepTime));
605 }
606
607 const char *mutt_make_version (int full)
608 {
609   static char vstring[STRING];
610
611   if (full)
612     snprintf (vstring, sizeof (vstring),
613               "Madmutt/%s-r%s (based on Mutt 1.5.11)",
614               MUTT_VERSION, MUTT_REVISION);
615   else
616     snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
617               MUTT_VERSION, MUTT_REVISION);
618   return vstring;
619 }
620
621 int mutt_match_spam_list (const char *s, rx_t * l, char *text, int x)
622 {
623   static regmatch_t *pmatch = NULL;
624   static int nmatch = 0;
625   int i, n, tlen;
626   char *p;
627
628   if (!s)
629     return 0;
630
631   tlen = 0;
632
633   for (; l; l = l->next) {
634     /* If this pattern needs more matches, expand pmatch. */
635     if (l->nmatch > nmatch) {
636       p_realloc(&pmatch, l->nmatch);
637       nmatch = l->nmatch;
638     }
639
640     /* Does this pattern match? */
641     if (regexec(l->rx, s, l->nmatch, (regmatch_t *)pmatch, (int) 0) == 0) {
642       /* Copy template into text, with substitutions. */
643       for (p = l->template; *p;) {
644         if (*p == '%') {
645           n = atoi (++p);       /* find pmatch index */
646           while (isdigit ((unsigned char) *p))
647             ++p;                /* skip subst token */
648           for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
649             text[tlen++] = s[i];
650         }
651         else {
652           text[tlen++] = *p++;
653         }
654       }
655       text[tlen] = '\0';
656       return 1;
657     }
658   }
659
660   return 0;
661 }
662
663 /* return 1 if address lists are strictly identical */
664 static int mutt_cmp_addr (const address_t * a, const address_t * b)
665 {
666   while (a && b) {
667     if (m_strcmp(a->mailbox, b->mailbox) ||
668         m_strcmp(a->personal, b->personal))
669       return (0);
670
671     a = a->next;
672     b = b->next;
673   }
674   if (a || b)
675     return (0);
676
677   return (1);
678 }
679
680 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
681 {
682   while (a && b) {
683     if (m_strcmp(a->data, b->data))
684       return (0);
685
686     a = a->next;
687     b = b->next;
688   }
689   if (a || b)
690     return (0);
691
692   return (1);
693 }
694
695 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
696 {
697   if (e1 && e2) {
698     if (m_strcmp(e1->message_id, e2->message_id) ||
699         m_strcmp(e1->subject, e2->subject) ||
700         !mutt_cmp_list (e1->references, e2->references) ||
701         !mutt_cmp_addr (e1->from, e2->from) ||
702         !mutt_cmp_addr (e1->sender, e2->sender) ||
703         !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
704         !mutt_cmp_addr (e1->to, e2->to) ||
705         !mutt_cmp_addr (e1->cc, e2->cc) ||
706         !mutt_cmp_addr (e1->return_path, e2->return_path))
707       return (0);
708     else
709       return (1);
710   }
711   else {
712     if (e1 == NULL && e2 == NULL)
713       return (1);
714     else
715       return (0);
716   }
717 }
718
719 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
720 {
721   if (b1->type != b2->type ||
722       b1->encoding != b2->encoding ||
723       m_strcmp(b1->subtype, b2->subtype) ||
724       m_strcmp(b1->description, b2->description) ||
725       !parameter_equal(b1->parameter, b2->parameter) ||
726       b1->length != b2->length)
727     return (0);
728   return (1);
729 }
730 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
731   if (h1 && h2) {
732     if (h1->received != h2->received ||
733         h1->date_sent != h2->date_sent ||
734         h1->content->length != h2->content->length ||
735         h1->lines != h2->lines ||
736         h1->zhours != h2->zhours ||
737         h1->zminutes != h2->zminutes ||
738         h1->zoccident != h2->zoccident ||
739         h1->mime != h2->mime ||
740         !mutt_cmp_env (h1->env, h2->env) ||
741         !mutt_cmp_body (h1->content, h2->content))
742       return (0);
743     else
744       return (1);
745   }
746   else {
747     if (h1 == NULL && h2 == NULL)
748       return (1);
749     else
750       return (0);
751   }
752 }
753
754
755 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
756 {
757     char ch;
758     char qc = 0;                  /* quote char */
759     char *pc;
760
761     /* reset the destination pointer to the beginning of the buffer */
762     dest->dptr = dest->data;
763
764     tok->dptr = vskipspaces(tok->dptr);
765     while ((ch = *tok->dptr)) {
766         if (!qc) {
767             if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
768             || (ch == '#' && !(flags & M_TOKEN_COMMENT))
769             || (ch == '=' && (flags & M_TOKEN_EQUAL))
770             || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
771             || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
772             {
773                 break;
774             }
775         }
776
777         tok->dptr++;
778
779         if (ch == qc) {
780             qc = 0;                     /* end of quote */
781         } else
782         if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
783             qc = ch;
784         } else
785         if (ch == '\\' && qc != '\'') {
786             if (!*tok->dptr)
787                 return -1;              /* premature end of token */
788
789             switch (ch = *tok->dptr++) {
790               case 'c':
791               case 'C':
792                 if (!*tok->dptr)
793                     return -1;          /* premature end of token */
794                 mutt_buffer_addch(dest,
795                                   (ascii_toupper(*tok->dptr) - 'A' + 1) & 0x7f);
796                 tok->dptr++;
797                 break;
798               case 'r':
799                 mutt_buffer_addch(dest, '\r');
800                 break;
801               case 'n':
802                 mutt_buffer_addch(dest, '\n');
803                 break;
804               case 't':
805                 mutt_buffer_addch(dest, '\t');
806                 break;
807               case 'f':
808                 mutt_buffer_addch(dest, '\f');
809                 break;
810               case 'e':
811                 mutt_buffer_addch(dest, '\033');
812                 break;
813               default:
814                 if (isdigit((unsigned char)ch)
815                 &&  isdigit((unsigned char)*tok->dptr)
816                 &&  isdigit((unsigned char)*(tok->dptr + 1)))
817                 {
818                     mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
819                                             *(tok->dptr + 1) - 3504);
820                     tok->dptr += 2;
821                 } else {
822                     mutt_buffer_addch(dest, ch);
823                 }
824             }
825         } else
826         if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
827             if (!*tok->dptr)
828                 return -1;              /* premature end of token */
829             ch = *tok->dptr++;
830             if (ch == '^') {
831                 mutt_buffer_addch(dest, ch);
832             } else
833             if (ch == '[') {
834                 mutt_buffer_addch(dest, '\033');
835             } else
836             if (isalpha((unsigned char)ch)) {
837                 mutt_buffer_addch(dest, ascii_toupper(ch) - 'A' + 1);
838             } else {
839                 mutt_buffer_addch(dest, '^');
840                 mutt_buffer_addch(dest, ch);
841             }
842         } else
843         if (ch == '`' && (!qc || qc == '"')) {
844             FILE *fp;
845             pid_t pid;
846             char *cmd, *ptr;
847             ssize_t expnlen;
848             BUFFER expn;
849             int line = 0;
850
851             pc = tok->dptr;
852             do {
853                 if ((pc = strpbrk(pc, "\\`"))) {
854                     /* skip any quoted chars */
855                     if (*pc == '\\')
856                         pc += 2;
857                 }
858             } while (pc && *pc != '`');
859             if (!pc) {
860                 return (-1);
861             }
862
863             cmd = p_dupstr(tok->dptr, pc - tok->dptr);
864             if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
865                 p_delete(&cmd);
866                 return -1;
867             }
868             p_delete(&cmd);
869
870             tok->dptr = pc + 1;
871
872             /* read line */
873             p_clear(&expn, 1);
874             expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
875             m_fclose(&fp);
876             mutt_wait_filter(pid);
877
878             /* if we got output, make a new string consiting of the shell ouptput
879                plus whatever else was left on the original line */
880             /* BUT: If this is inside a quoted string, directly add output to 
881              * the token */
882             if (expn.data && qc) {
883                 mutt_buffer_addstr(dest, expn.data);
884                 p_delete(&expn.data);
885             } else
886             if (expn.data) {
887                 expnlen = m_strlen(expn.data);
888                 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
889                 ptr = xmalloc(tok->dsize);
890                 memcpy(ptr, expn.data, expnlen);
891                 strcpy(ptr + expnlen, tok->dptr);      /* __STRCPY_CHECKED__ */
892                 if (tok->destroy)
893                     p_delete(&tok->data);
894                 tok->data = ptr;
895                 tok->dptr = ptr;
896                 tok->destroy = 1;       /* mark that the caller should destroy this data */
897                 ptr = NULL;
898                 p_delete(&expn.data);
899             }
900         } else
901         if (ch == '$' && (!qc || qc == '"')
902         && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
903         {
904             char *env = NULL, *var = NULL;
905
906             if (*tok->dptr == '{') {
907                 tok->dptr++;
908                 if ((pc = strchr (tok->dptr, '}'))) {
909                     var = p_dupstr(tok->dptr, pc - tok->dptr);
910                     tok->dptr = pc + 1;
911                 }
912             } else {
913                 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
914                      pc++);
915                 var = p_dupstr(tok->dptr, pc - tok->dptr);
916                 tok->dptr = pc;
917             }
918             if (var) {
919                 char tmp[STRING];
920                 if ((env = getenv (var))
921                 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
922                 {
923                     mutt_buffer_addstr (dest, env);
924                 }
925             }
926             p_delete(&var);
927         } else {
928             mutt_buffer_addch(dest, ch);
929         }
930     }
931     mutt_buffer_addch(dest, 0);  /* terminate the string */
932     tok->dptr = vskipspaces(tok->dptr);
933     return 0;
934 }