f185e1291951794751073acbadf46e56d3804372
[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 (!m_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 ssize_t m_strformat(char *dst, ssize_t dlen, const char *fmt,
332                     format_t *callback, unsigned long data, format_flag flags)
333 {
334     ssize_t pos = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
335
336     while (*fmt) {
337         char ifstring[STRING], elsestring[STRING], prefix[STRING] = "";
338         int ch;
339
340         if (*fmt == '%') {
341             if (*++fmt == '%') {
342                 pos += m_strputc(dst + pos, dlen - pos, *fmt++);
343                 continue;
344             }
345
346             if (*fmt == '?') {
347                 flags |= M_FORMAT_OPTIONAL;
348                 fmt++;
349             } else {
350                 ssize_t pfxlen;
351                 flags &= ~M_FORMAT_OPTIONAL;
352
353                 /* eat the format string */
354                 pfxlen = strspn(fmt, "0123456789.-");
355                 m_strncpy(prefix, sizeof(prefix), fmt, pfxlen);
356                 fmt   += pfxlen;
357             }
358
359             /* save the character to switch on */
360             if (!(ch = *fmt++))
361                 break;
362
363             if (flags & M_FORMAT_OPTIONAL) {
364                 ssize_t iflen;
365                 const char *p;
366
367                 if (*fmt++ != '?')
368                     break;                /* bad format */
369
370                 /* eat the `if' part of the string */
371                 iflen = strcspn(fmt, "?&");
372                 m_strncpy(ifstring, ssizeof(ifstring), fmt, iflen);
373                 fmt  += iflen;
374
375                 /* eat the `else' part of the string (optional) */
376                 if (*fmt == '&')
377                     fmt++;                /* skip the & */
378
379                 p = m_strchrnul(fmt, '?');
380                 m_strncpy(elsestring, ssizeof(elsestring), fmt, p - fmt);
381                 fmt = p;
382
383                 if (!*fmt++)              /* move past the trailing `?' */
384                     break;                /* bad format */
385             } else {
386                 *ifstring = *elsestring = '\0';
387             }
388
389             switch (ch) {
390                 ssize_t col;
391                 char lower, nodots, buf[LONG_STRING];
392
393               case '>':                 /* right justify to EOL */
394                 col = mutt_strwidth(dst);
395
396                 ch = *fmt++;            /* pad char */
397
398                 if (COLS - SW > col) {
399                     m_strformat(buf, sizeof(buf), fmt, callback, data, flags);
400                     pos += m_strpad(dst + pos, dlen - pos, ch,
401                                     COLS - SW - col - mutt_strwidth(buf));
402                     pos += m_strcpy(dst + pos, dlen - pos, buf);
403                 }
404                 return pos;             /* skip rest of input */
405
406               case '|':
407                 col = mutt_strwidth(dst);
408
409                 ch = *fmt++;
410                 /* pad to EOL */
411                 pos += m_strpad(dst + pos, dlen - pos, ch, COLS - SW - col);
412                 return pos;             /* skip rest of input */
413
414               default:
415                 lower = nodots = 0;
416
417                 while (ch == '_' || ch == ':') {
418                     lower  |= ch == '_';
419                     nodots |= ch == ':';
420                     ch = *fmt++;
421                 }
422
423                 /* use callback function to handle this case */
424                 fmt = callback(buf, sizeof (buf), ch, fmt, prefix,
425                                ifstring, elsestring, data, flags);
426
427                 if (lower)
428                     m_strtolower(buf);
429
430                 if (nodots) {
431                     char *p;
432
433                     for (p = buf; *p; p++) {
434                         if (*p == '.')
435                             *p = '_';
436                     }
437                 }
438
439                 pos += m_strcpy(dst + pos, dlen - pos, buf);
440                 break;
441             }
442             continue;
443         }
444
445         if (*fmt == '\\') {
446             if (!*++fmt)
447                 break;
448             switch ((ch = *fmt++)) {
449               case 'n': pos += m_strputc(dst + pos, dlen - pos, '\n'); break;
450               case 't': pos += m_strputc(dst + pos, dlen - pos, '\t'); break;
451               case 'r': pos += m_strputc(dst + pos, dlen - pos, '\r'); break;
452               case 'f': pos += m_strputc(dst + pos, dlen - pos, '\f'); break;
453               case 'v': pos += m_strputc(dst + pos, dlen - pos, '\v'); break;
454               default:  pos += m_strputc(dst + pos, dlen - pos, ch);   break;
455             }
456         } else {
457             ssize_t len = strcspn(fmt, "%\\");
458
459             pos += m_strncpy(dst + pos, dlen - pos, fmt, len);
460             fmt += len;
461         }
462     }
463
464     return pos;
465 }
466
467 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
468 int mutt_save_confirm (const char *s, struct stat *st)
469 {
470   char tmp[_POSIX_PATH_MAX];
471   int ret = 0;
472   int rc;
473   int magic = 0;
474
475   magic = mx_get_magic (s);
476
477   if (magic == M_POP) {
478     mutt_error _("Can't save message to POP mailbox.");
479
480     return 1;
481   }
482
483 #ifdef USE_NNTP
484   if (magic == M_NNTP) {
485     mutt_error _("Can't save message to newsserver.");
486
487     return 0;
488   }
489 #endif
490
491   if (magic > 0 && !mx_access (s, W_OK)) {
492     if (option (OPTCONFIRMAPPEND) &&
493         (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
494       /* if we're appending to the trash, there's no point in asking */
495       snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
496       if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
497         ret = 1;
498       else if (rc == -1)
499         ret = -1;
500     }
501   }
502
503   if (stat (s, st) != -1) {
504     if (magic == -1) {
505       mutt_error (_("%s is not a mailbox!"), s);
506       return 1;
507     }
508   } else {
509     if (magic != M_IMAP)
510     {
511       st->st_mtime = 0;
512       st->st_atime = 0;
513
514       if (errno == ENOENT) {
515         if (option (OPTCONFIRMCREATE)) {
516           snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
517           if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
518             ret = 1;
519           else if (rc == -1)
520             ret = -1;
521         }
522       } else {
523         mutt_perror (s);
524         return 1;
525       }
526     }
527   }
528
529   CLEARLINE (LINES - 1);
530   return (ret);
531 }
532
533 void mutt_sleep (short s)
534 {
535     sleep(MAX(s, SleepTime));
536 }
537
538 const char *mutt_make_version (int full)
539 {
540   static char vstring[STRING];
541
542   if (full)
543     snprintf (vstring, sizeof (vstring),
544               "Madmutt/%s-r%s (based on Mutt 1.5.11)",
545               MUTT_VERSION, MUTT_REVISION);
546   else
547     snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
548               MUTT_VERSION, MUTT_REVISION);
549   return vstring;
550 }
551
552 /* return 1 if address lists are strictly identical */
553 static int mutt_cmp_addr (const address_t * a, const address_t * b)
554 {
555   while (a && b) {
556     if (m_strcmp(a->mailbox, b->mailbox) ||
557         m_strcmp(a->personal, b->personal))
558       return (0);
559
560     a = a->next;
561     b = b->next;
562   }
563   if (a || b)
564     return (0);
565
566   return (1);
567 }
568
569 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
570 {
571   while (a && b) {
572     if (m_strcmp(a->data, b->data))
573       return (0);
574
575     a = a->next;
576     b = b->next;
577   }
578   if (a || b)
579     return (0);
580
581   return (1);
582 }
583
584 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
585 {
586   if (e1 && e2) {
587     if (m_strcmp(e1->message_id, e2->message_id) ||
588         m_strcmp(e1->subject, e2->subject) ||
589         !mutt_cmp_list (e1->references, e2->references) ||
590         !mutt_cmp_addr (e1->from, e2->from) ||
591         !mutt_cmp_addr (e1->sender, e2->sender) ||
592         !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
593         !mutt_cmp_addr (e1->to, e2->to) ||
594         !mutt_cmp_addr (e1->cc, e2->cc) ||
595         !mutt_cmp_addr (e1->return_path, e2->return_path))
596       return (0);
597     else
598       return (1);
599   }
600   else {
601     if (e1 == NULL && e2 == NULL)
602       return (1);
603     else
604       return (0);
605   }
606 }
607
608 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
609 {
610   if (b1->type != b2->type ||
611       b1->encoding != b2->encoding ||
612       m_strcmp(b1->subtype, b2->subtype) ||
613       m_strcmp(b1->description, b2->description) ||
614       !parameter_equal(b1->parameter, b2->parameter) ||
615       b1->length != b2->length)
616     return (0);
617   return (1);
618 }
619 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
620   if (h1 && h2) {
621     if (h1->received != h2->received ||
622         h1->date_sent != h2->date_sent ||
623         h1->content->length != h2->content->length ||
624         h1->lines != h2->lines ||
625         h1->zhours != h2->zhours ||
626         h1->zminutes != h2->zminutes ||
627         h1->zoccident != h2->zoccident ||
628         h1->mime != h2->mime ||
629         !mutt_cmp_env (h1->env, h2->env) ||
630         !mutt_cmp_body (h1->content, h2->content))
631       return (0);
632     else
633       return (1);
634   }
635   else {
636     if (h1 == NULL && h2 == NULL)
637       return (1);
638     else
639       return (0);
640   }
641 }
642
643
644 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
645 {
646     char ch;
647     char qc = 0;                  /* quote char */
648     char *pc;
649
650     /* reset the destination pointer to the beginning of the buffer */
651     dest->dptr = dest->data;
652
653     tok->dptr = vskipspaces(tok->dptr);
654     while ((ch = *tok->dptr)) {
655         if (!qc) {
656             if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
657             || (ch == '#' && !(flags & M_TOKEN_COMMENT))
658             || (ch == '=' && (flags & M_TOKEN_EQUAL))
659             || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
660             || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
661             {
662                 break;
663             }
664         }
665
666         tok->dptr++;
667
668         if (ch == qc) {
669             qc = 0;                     /* end of quote */
670         } else
671         if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
672             qc = ch;
673         } else
674         if (ch == '\\' && qc != '\'') {
675             if (!*tok->dptr)
676                 return -1;              /* premature end of token */
677
678             switch (ch = *tok->dptr++) {
679               case 'c':
680               case 'C':
681                 if (!*tok->dptr)
682                     return -1;          /* premature end of token */
683                 mutt_buffer_addch(dest,
684                                   (toupper((unsigned char)*tok->dptr) - 'A' + 1) & 0x7f);
685                 tok->dptr++;
686                 break;
687               case 'r':
688                 mutt_buffer_addch(dest, '\r');
689                 break;
690               case 'n':
691                 mutt_buffer_addch(dest, '\n');
692                 break;
693               case 't':
694                 mutt_buffer_addch(dest, '\t');
695                 break;
696               case 'f':
697                 mutt_buffer_addch(dest, '\f');
698                 break;
699               case 'e':
700                 mutt_buffer_addch(dest, '\033');
701                 break;
702               default:
703                 if (isdigit((unsigned char)ch)
704                 &&  isdigit((unsigned char)*tok->dptr)
705                 &&  isdigit((unsigned char)*(tok->dptr + 1)))
706                 {
707                     mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
708                                             *(tok->dptr + 1) - 3504);
709                     tok->dptr += 2;
710                 } else {
711                     mutt_buffer_addch(dest, ch);
712                 }
713             }
714         } else
715         if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
716             if (!*tok->dptr)
717                 return -1;              /* premature end of token */
718             ch = *tok->dptr++;
719             if (ch == '^') {
720                 mutt_buffer_addch(dest, ch);
721             } else
722             if (ch == '[') {
723                 mutt_buffer_addch(dest, '\033');
724             } else
725             if (isalpha((unsigned char)ch)) {
726                 mutt_buffer_addch(dest, toupper((unsigned char)ch) - 'A' + 1);
727             } else {
728                 mutt_buffer_addch(dest, '^');
729                 mutt_buffer_addch(dest, ch);
730             }
731         } else
732         if (ch == '`' && (!qc || qc == '"')) {
733             FILE *fp;
734             pid_t pid;
735             char *cmd, *ptr;
736             ssize_t expnlen;
737             BUFFER expn;
738             int line = 0;
739
740             pc = tok->dptr;
741             do {
742                 if ((pc = strpbrk(pc, "\\`"))) {
743                     /* skip any quoted chars */
744                     if (*pc == '\\')
745                         pc += 2;
746                 }
747             } while (pc && *pc != '`');
748             if (!pc) {
749                 return (-1);
750             }
751
752             cmd = p_dupstr(tok->dptr, pc - tok->dptr);
753             if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
754                 p_delete(&cmd);
755                 return -1;
756             }
757             p_delete(&cmd);
758
759             tok->dptr = pc + 1;
760
761             /* read line */
762             p_clear(&expn, 1);
763             expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
764             m_fclose(&fp);
765             mutt_wait_filter(pid);
766
767             /* if we got output, make a new string consiting of the shell ouptput
768                plus whatever else was left on the original line */
769             /* BUT: If this is inside a quoted string, directly add output to 
770              * the token */
771             if (expn.data && qc) {
772                 mutt_buffer_addstr(dest, expn.data);
773                 p_delete(&expn.data);
774             } else
775             if (expn.data) {
776                 expnlen = m_strlen(expn.data);
777                 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
778                 ptr = xmalloc(tok->dsize);
779                 memcpy(ptr, expn.data, expnlen);
780                 m_strcpy(ptr + expnlen, tok->dsize - expnlen, tok->dptr);
781                 if (tok->destroy)
782                     p_delete(&tok->data);
783                 tok->data = ptr;
784                 tok->dptr = ptr;
785                 tok->destroy = 1;       /* mark that the caller should destroy this data */
786                 ptr = NULL;
787                 p_delete(&expn.data);
788             }
789         } else
790         if (ch == '$' && (!qc || qc == '"')
791         && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
792         {
793             char *env = NULL, *var = NULL;
794
795             if (*tok->dptr == '{') {
796                 tok->dptr++;
797                 if ((pc = strchr (tok->dptr, '}'))) {
798                     var = p_dupstr(tok->dptr, pc - tok->dptr);
799                     tok->dptr = pc + 1;
800                 }
801             } else {
802                 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
803                      pc++);
804                 var = p_dupstr(tok->dptr, pc - tok->dptr);
805                 tok->dptr = pc;
806             }
807             if (var) {
808                 char tmp[STRING];
809                 if ((env = getenv (var))
810                 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
811                 {
812                     mutt_buffer_addstr (dest, env);
813                 }
814             }
815             p_delete(&var);
816         } else {
817             mutt_buffer_addch(dest, ch);
818         }
819     }
820     mutt_buffer_addch(dest, 0);  /* terminate the string */
821     tok->dptr = vskipspaces(tok->dptr);
822     return 0;
823 }