bye bye DT_SYS variables.
[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-ui/curses.h>
17 #include <lib-ui/enter.h>
18 #include <lib-sys/unix.h>
19 #include <imap/imap.h>
20
21 #include "alias.h"
22 #include "mutt.h"
23 #include "attach.h"
24
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.
30  */
31 void mutt_adv_mktemp (const char* dir, char *s, ssize_t l)
32 {
33     int fd;
34
35     fd = m_tempfd(s, l, m_strisempty(dir) ? NONULL(Tempdir) : dir, s);
36     if (fd < 0) {
37         *s = '\0';
38     } else {
39         close(fd);
40         unlink(s);
41     }
42 }
43
44 void mutt_mktemp(char *s)
45 {
46     int fd = m_tempfd(s, _POSIX_PATH_MAX, NONULL(Tempdir), NULL);
47     if (fd < 0) {
48         *s = '\0';
49     } else {
50         close(fd);
51         unlink(s);
52     }
53 }
54
55 ssize_t _mutt_expand_path(char *s, ssize_t slen, int rx)
56 {
57     char p[_POSIX_PATH_MAX] = "";
58     char tmp[_POSIX_PATH_MAX];
59     const char *tail = "";
60
61     do {
62         const address_t *alias;
63
64         switch (*s) {
65           case '~':
66             if (s[1] == '/' || s[1] == '\0') {
67                 m_strcpy(p, sizeof(p), Homedir);
68                 tail = s + 1;
69             } else {
70                 struct passwd *pw;
71                 tail = m_strchrnul(s + 1, '/');
72
73                 m_strncpy(tmp, sizeof(tmp), s + 1, tail - s - 1);
74
75                 if ((pw = getpwnam(tmp))) {
76                     m_strcpy(p, sizeof(p), pw->pw_dir);
77                 } else {
78                     /* user not found! */
79                     tail = s;
80                 }
81             }
82             break;
83
84           case '=':
85           case '+':
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);
90             } else {
91                 snprintf(p, sizeof(p), "%s/", NONULL(Maildir));
92             }
93
94             tail = s + 1;
95             break;
96
97             /* elm compatibility, @ expands alias to user name */
98
99           case '@':
100             if ((alias = alias_lookup(s + 1))) {
101                 HEADER h;
102                 header_init(&h);
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;
107                 header_wipe(&h);
108
109                 if (*p != '@') {
110                     /* recurse iff the result do not starts with '@' */
111                     m_strcpy(s, slen, p);
112                     continue;
113                 }
114             }
115             break;
116
117           case '>':
118             m_strcpy(p, sizeof(p), Inbox);
119             tail = s + 1;
120             break;
121
122           case '<':
123             m_strcpy(p, sizeof(p), Outbox);
124             tail = s + 1;
125             break;
126
127           case '!':
128             if (s[1] == '!') {
129                 m_strcpy(p, sizeof(p), LastFolder);
130                 tail = s + 2;
131             } else {
132                 m_strcpy(p, sizeof(p), Spoolfile);
133                 tail = s + 1;
134             }
135             break;
136
137           case '-':
138             m_strcpy(p, sizeof(p), NONULL(LastFolder));
139             tail = s + 1;
140             break;
141
142           case '^':
143             m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
144             tail = s + 1;
145             break;
146
147           default:
148             *p = '\0';
149             tail = s;
150         }
151     } while (0);
152
153     if (rx) {
154         char q[_POSIX_PATH_MAX];
155         rx_sanitize_string(q, sizeof(q), p);
156         snprintf(tmp, sizeof(tmp), "%s%s", q, tail);
157     } else {
158         snprintf(tmp, sizeof(tmp), "%s%s", p, tail);
159     }
160
161     return m_strcpy(s, slen, tmp);
162 }
163
164 /* collapse the pathname using ~ or = when possible */
165 void mutt_pretty_mailbox (char *s)
166 {
167   char *p = s, *q = s;
168   ssize_t len;
169   url_scheme_t scheme;
170
171   scheme = url_check_scheme (s);
172
173   if (scheme == U_IMAP || scheme == U_IMAPS) {
174     imap_pretty_mailbox (s);
175     return;
176   }
177
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, '/');
183     if (!q)
184       q = strchr (p, '\0');
185     p = q;
186   }
187
188   /* first attempt to collapse the pathname */
189   while (*p) {
190     if (*p == '/' && p[1] == '/') {
191       *q++ = '/';
192       p += 2;
193     }
194     else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
195       *q++ = '/';
196       p += 3;
197     }
198     else
199       *q++ = *p++;
200   }
201   *q = 0;
202
203   if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
204       s[len] == '/') {
205     *s++ = '=';
206     memmove (s, s + len, m_strlen(s + len) + 1);
207   }
208   else if (m_strncmp(s, Homedir, (len = m_strlen(Homedir))) == 0 &&
209            s[len] == '/') {
210     *s++ = '~';
211     memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
212   }
213 }
214
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,
218                           char **directory)
219 {
220   int rc = 0;
221   char tmp[_POSIX_PATH_MAX];
222   struct stat st;
223
224   m_strcpy(fname, flen, path);
225   if (access (fname, F_OK) != 0)
226     return 0;
227   if (stat (fname, &st) != 0)
228     return -1;
229   if (S_ISDIR (st.st_mode)) {
230     if (directory) {
231       switch (mutt_multi_choice
232               (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
233                _("yna"))) {
234       case 3:                  /* all */
235         m_strreplace(directory, fname);
236         break;
237       case 1:                  /* yes */
238         p_delete(directory);
239         break;
240       case -1:                 /* abort */
241         p_delete(directory);
242         return -1;
243       case 2:                  /* no */
244         p_delete(directory);
245         return 1;
246       }
247     }
248     else
249       if ((rc = mutt_yesorno(_("File is a directory, save under it?"),
250                              M_YES)) != M_YES)
251       return (rc == M_NO) ? 1 : -1;
252
253     if (!attname || !attname[0]) {
254       tmp[0] = 0;
255       if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
256                           M_FILE | M_CLEAR) != 0 || !tmp[0])
257         return (-1);
258       mutt_concat_path(fname, flen, path, tmp);
259     }
260     else
261       mutt_concat_path(fname, flen, path, mutt_basename(attname));
262   }
263
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")))
267     {
268     case -1:                   /* abort */
269       return -1;
270     case 3:                    /* cancel */
271       return 1;
272
273     case 2:                    /* append */
274       *append = M_SAVE_APPEND;
275       break;
276     case 1:                    /* overwrite */
277       *append = M_SAVE_OVERWRITE;
278       break;
279     }
280   }
281   return 0;
282 }
283
284 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
285 {
286     if (a && a->mailbox) {
287         m_strcpy(d, dsize, a->mailbox);
288
289         if (!option(OPTSAVEADDRESS)) {
290             char *p = strpbrk(d, "%@");
291             if (p)
292                 *p = '\0';
293         }
294         m_strtolower(d);
295     } else {
296         *d = '\0';
297     }
298 }
299
300 void mutt_safe_path(char *s, ssize_t l, address_t *a)
301 {
302     mutt_save_path(s, l, a);
303
304     while (*s) {
305         if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
306             *s = '_';
307         s++;
308     }
309 }
310
311 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
312 int mutt_save_confirm (const char *s, struct stat *st)
313 {
314   char tmp[_POSIX_PATH_MAX];
315   int ret = 0;
316   int rc;
317   int magic = 0;
318
319   magic = mx_get_magic (s);
320
321   if (magic == M_POP) {
322     mutt_error _("Can't save message to POP mailbox.");
323
324     return 1;
325   }
326
327 #ifdef USE_NNTP
328   if (magic == M_NNTP) {
329     mutt_error _("Can't save message to newsserver.");
330
331     return 0;
332   }
333 #endif
334
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)
341         ret = 1;
342       else if (rc == -1)
343         ret = -1;
344     }
345   }
346
347   if (stat (s, st) != -1) {
348     if (magic == -1) {
349       mutt_error (_("%s is not a mailbox!"), s);
350       return 1;
351     }
352   } else {
353     if (magic != M_IMAP)
354     {
355       st->st_mtime = 0;
356       st->st_atime = 0;
357
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)
362             ret = 1;
363           else if (rc == -1)
364             ret = -1;
365         }
366       } else {
367         mutt_perror (s);
368         return 1;
369       }
370     }
371   }
372
373   CLEARLINE (LINES - 1);
374   return (ret);
375 }
376
377 void mutt_sleep (short s)
378 {
379     sleep(MAX(s, SleepTime));
380 }
381
382 const char *mutt_make_version(void)
383 {
384   static char vstring[STRING];
385   snprintf(vstring, sizeof (vstring), "Madmutt/%s", MUTT_VERSION);
386   return vstring;
387 }
388
389 /* return 1 if address lists are strictly identical */
390 static int mutt_cmp_addr (const address_t * a, const address_t * b)
391 {
392   while (a && b) {
393     if (m_strcmp(a->mailbox, b->mailbox) ||
394         m_strcmp(a->personal, b->personal))
395       return (0);
396
397     a = a->next;
398     b = b->next;
399   }
400   if (a || b)
401     return (0);
402
403   return (1);
404 }
405
406 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
407 {
408   while (a && b) {
409     if (m_strcmp(a->data, b->data))
410       return (0);
411
412     a = a->next;
413     b = b->next;
414   }
415   if (a || b)
416     return (0);
417
418   return (1);
419 }
420
421 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
422 {
423   if (e1 && 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))
433       return (0);
434     else
435       return (1);
436   }
437   else {
438     if (e1 == NULL && e2 == NULL)
439       return (1);
440     else
441       return (0);
442   }
443 }
444
445 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
446 {
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)
453     return (0);
454   return (1);
455 }
456 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
457   if (h1 && 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))
468       return (0);
469     else
470       return (1);
471   }
472   else {
473     if (h1 == NULL && h2 == NULL)
474       return (1);
475     else
476       return (0);
477   }
478 }
479
480
481 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
482 {
483     char ch;
484     char qc = 0;                  /* quote char */
485     char *pc;
486
487     /* reset the destination pointer to the beginning of the buffer */
488     dest->dptr = dest->data;
489
490     tok->dptr = vskipspaces(tok->dptr);
491     while ((ch = *tok->dptr)) {
492         if (!qc) {
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)))
497             {
498                 break;
499             }
500         }
501
502         tok->dptr++;
503
504         if (ch == qc) {
505             qc = 0;                     /* end of quote */
506         } else
507         if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
508             qc = ch;
509         } else
510         if (ch == '\\' && qc != '\'') {
511             if (!*tok->dptr)
512                 return -1;              /* premature end of token */
513
514             switch (ch = *tok->dptr++) {
515               case 'c':
516               case 'C':
517                 if (!*tok->dptr)
518                     return -1;          /* premature end of token */
519                 mutt_buffer_addch(dest,
520                                   (toupper((unsigned char)*tok->dptr) - 'A' + 1) & 0x7f);
521                 tok->dptr++;
522                 break;
523               case 'r':
524                 mutt_buffer_addch(dest, '\r');
525                 break;
526               case 'n':
527                 mutt_buffer_addch(dest, '\n');
528                 break;
529               case 't':
530                 mutt_buffer_addch(dest, '\t');
531                 break;
532               case 'f':
533                 mutt_buffer_addch(dest, '\f');
534                 break;
535               case 'e':
536                 mutt_buffer_addch(dest, '\033');
537                 break;
538               default:
539                 if (isdigit((unsigned char)ch)
540                 &&  isdigit((unsigned char)*tok->dptr)
541                 &&  isdigit((unsigned char)*(tok->dptr + 1)))
542                 {
543                     mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
544                                             *(tok->dptr + 1) - 3504);
545                     tok->dptr += 2;
546                 } else {
547                     mutt_buffer_addch(dest, ch);
548                 }
549             }
550         } else
551         if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
552             if (!*tok->dptr)
553                 return -1;              /* premature end of token */
554             ch = *tok->dptr++;
555             if (ch == '^') {
556                 mutt_buffer_addch(dest, ch);
557             } else
558             if (ch == '[') {
559                 mutt_buffer_addch(dest, '\033');
560             } else
561             if (isalpha((unsigned char)ch)) {
562                 mutt_buffer_addch(dest, toupper((unsigned char)ch) - 'A' + 1);
563             } else {
564                 mutt_buffer_addch(dest, '^');
565                 mutt_buffer_addch(dest, ch);
566             }
567         } else
568         if (ch == '`' && (!qc || qc == '"')) {
569             FILE *fp;
570             pid_t pid;
571             char *cmd, *ptr;
572             ssize_t expnlen;
573             BUFFER expn;
574             int line = 0;
575
576             pc = tok->dptr;
577             do {
578                 if ((pc = strpbrk(pc, "\\`"))) {
579                     /* skip any quoted chars */
580                     if (*pc == '\\')
581                         pc += 2;
582                 }
583             } while (pc && *pc != '`');
584             if (!pc) {
585                 return (-1);
586             }
587
588             cmd = p_dupstr(tok->dptr, pc - tok->dptr);
589             if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
590                 p_delete(&cmd);
591                 return -1;
592             }
593             p_delete(&cmd);
594
595             tok->dptr = pc + 1;
596
597             /* read line */
598             p_clear(&expn, 1);
599             expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
600             m_fclose(&fp);
601             mutt_wait_filter(pid);
602
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 
606              * the token */
607             if (expn.data && qc) {
608                 mutt_buffer_addstr(dest, expn.data);
609                 p_delete(&expn.data);
610             } else
611             if (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);
617                 if (tok->destroy)
618                     p_delete(&tok->data);
619                 tok->data = ptr;
620                 tok->dptr = ptr;
621                 tok->destroy = 1;       /* mark that the caller should destroy this data */
622                 ptr = NULL;
623                 p_delete(&expn.data);
624             }
625         } else
626         if (ch == '$' && (!qc || qc == '"')
627         && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
628         {
629             char *env = NULL, *var = NULL;
630
631             if (*tok->dptr == '{') {
632                 tok->dptr++;
633                 if ((pc = strchr (tok->dptr, '}'))) {
634                     var = p_dupstr(tok->dptr, pc - tok->dptr);
635                     tok->dptr = pc + 1;
636                 }
637             } else {
638                 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
639                      pc++);
640                 var = p_dupstr(tok->dptr, pc - tok->dptr);
641                 tok->dptr = pc;
642             }
643             if (var) {
644                 char tmp[STRING];
645                 if ((env = getenv (var))
646                 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
647                 {
648                     mutt_buffer_addstr (dest, env);
649                 }
650             }
651             p_delete(&var);
652         } else {
653             mutt_buffer_addch(dest, ch);
654         }
655     }
656     mutt_buffer_addch(dest, 0);  /* terminate the string */
657     tok->dptr = vskipspaces(tok->dptr);
658     return 0;
659 }