Reorder TARGET_LINK_LIBRARIES
[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/lib-ui.h>
17 #include <lib-sys/unix.h>
18 #include <imap/imap.h>
19
20 #include "alias.h"
21 #include "mutt.h"
22 #include "attach.h"
23
24 const char *madmutt_version = "Madmutt/" MUTT_VERSION;
25
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.
31  */
32 void mutt_adv_mktemp (const char* dir, char *s, ssize_t l)
33 {
34     int fd;
35
36     fd = m_tempfd(s, l, m_strisempty(dir) ? NONULL(mod_core.tmpdir) : dir, s);
37     if (fd < 0) {
38         *s = '\0';
39     } else {
40         close(fd);
41         unlink(s);
42     }
43 }
44
45 void mutt_mktemp(char *s)
46 {
47     int fd = m_tempfd(s, _POSIX_PATH_MAX, NONULL(mod_core.tmpdir), NULL);
48     if (fd < 0) {
49         *s = '\0';
50     } else {
51         close(fd);
52         unlink(s);
53     }
54 }
55
56 ssize_t _mutt_expand_path(char *buf, ssize_t len, const char *s, int rx)
57 {
58     char p[_POSIX_PATH_MAX] = "";
59     char tmp[_POSIX_PATH_MAX];
60     const char *tail = "";
61     const address_t *alias;
62
63     switch (*s) {
64       case '~':
65         if (s[1] == '/' || s[1] == '\0') {
66             m_strcpy(p, sizeof(p), mod_core.homedir);
67             tail = s + 1;
68         } else {
69             struct passwd *pw;
70             tail = m_strchrnul(s + 1, '/');
71
72             m_strncpy(tmp, sizeof(tmp), s + 1, tail - s - 1);
73
74             if ((pw = getpwnam(tmp))) {
75                 m_strcpy(p, sizeof(p), pw->pw_dir);
76             } else {
77                 /* user not found! */
78                 tail = s;
79             }
80         }
81         break;
82
83       case '=':
84       case '+':
85         /* if folder = imap[s]://host/: don't append slash */
86         if (imap_is_magic(NONULL(Maildir), NULL) == M_IMAP
87         &&  Maildir[m_strlen(Maildir) - 1] == '/') {
88             m_strcpy(p, sizeof(p), Maildir);
89         } else {
90             snprintf(p, sizeof(p), "%s/", NONULL(Maildir));
91         }
92
93         tail = s + 1;
94         break;
95
96         /* elm compatibility, @ expands alias to user name */
97
98       case '@':
99         if ((alias = alias_lookup(s + 1))) {
100             HEADER h;
101             header_init(&h);
102             h.env = envelope_new();
103             h.env->from = h.env->to = (address_t *)alias;
104             mutt_default_save(p, sizeof (p), &h);
105             h.env->from = h.env->to = NULL;
106             header_wipe(&h);
107
108             if (*p != '@')
109                 return _mutt_expand_path(buf, len, p, rx);
110         }
111         break;
112
113       case '>':
114         m_strcpy(p, sizeof(p), Inbox);
115         tail = s + 1;
116         break;
117
118       case '<':
119         m_strcpy(p, sizeof(p), MAlias.record);
120         tail = s + 1;
121         break;
122
123       case '!':
124         if (s[1] == '!') {
125             m_strcpy(p, sizeof(p), LastFolder);
126             tail = s + 2;
127         } else {
128             m_strcpy(p, sizeof(p), Spoolfile);
129             tail = s + 1;
130         }
131         break;
132
133       case '-':
134         m_strcpy(p, sizeof(p), NONULL(LastFolder));
135         tail = s + 1;
136         break;
137
138       case '^':
139         m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
140         tail = s + 1;
141         break;
142
143       default:
144         *p = '\0';
145         tail = s;
146     }
147
148     if (rx) {
149         char q[_POSIX_PATH_MAX];
150         rx_sanitize_string(q, sizeof(q), p);
151         snprintf(tmp, sizeof(tmp), "%s%s", q, tail);
152     } else {
153         snprintf(tmp, sizeof(tmp), "%s%s", p, tail);
154     }
155
156     return m_strcpy(buf, len, tmp);
157 }
158
159 /* collapse the pathname using ~ or = when possible */
160 void mutt_pretty_mailbox (char *s)
161 {
162   char *p = s, *q = s;
163   ssize_t len;
164   url_scheme_t scheme;
165
166   scheme = url_check_scheme (s);
167
168   if (scheme == U_IMAP || scheme == U_IMAPS) {
169     imap_pretty_mailbox (s);
170     return;
171   }
172
173   /* if s is an url, only collapse path component */
174   if (scheme != U_UNKNOWN) {
175     p = strchr (s, ':') + 1;
176     if (!m_strncmp (p, "//", 2))
177       q = strchr (p + 2, '/');
178     if (!q)
179       q = strchr (p, '\0');
180     p = q;
181   }
182
183   /* first attempt to collapse the pathname */
184   while (*p) {
185     if (*p == '/' && p[1] == '/') {
186       *q++ = '/';
187       p += 2;
188     }
189     else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
190       *q++ = '/';
191       p += 3;
192     }
193     else
194       *q++ = *p++;
195   }
196   *q = 0;
197
198   if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
199       s[len] == '/') {
200     *s++ = '=';
201     memmove (s, s + len, m_strlen(s + len) + 1);
202   }
203   else if (m_strncmp(s, mod_core.homedir, (len = m_strlen(mod_core.homedir))) == 0
204            && s[len] == '/') {
205     *s++ = '~';
206     memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
207   }
208 }
209
210 /* return 0 on success, -1 on abort, 1 on error */
211 int mutt_check_overwrite (const char *attname, const char *path,
212                           char *fname, ssize_t flen, int *append,
213                           char **directory)
214 {
215   int rc = 0;
216   char tmp[_POSIX_PATH_MAX];
217   struct stat st;
218
219   m_strcpy(fname, flen, path);
220   if (access (fname, F_OK) != 0)
221     return 0;
222   if (stat (fname, &st) != 0)
223     return -1;
224   if (S_ISDIR (st.st_mode)) {
225     if (directory) {
226       switch (mutt_multi_choice
227               (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
228                _("yna"))) {
229       case 3:                  /* all */
230         m_strreplace(directory, fname);
231         break;
232       case 1:                  /* yes */
233         p_delete(directory);
234         break;
235       case -1:                 /* abort */
236         p_delete(directory);
237         return -1;
238       case 2:                  /* no */
239         p_delete(directory);
240         return 1;
241       }
242     }
243     else
244       if ((rc = mutt_yesorno(_("File is a directory, save under it?"),
245                              M_YES)) != M_YES)
246       return (rc == M_NO) ? 1 : -1;
247
248     if (!attname || !attname[0]) {
249       tmp[0] = 0;
250       if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
251                           M_FILE | M_CLEAR) != 0 || !tmp[0])
252         return -1;
253       mutt_concat_path(fname, flen, path, tmp);
254     }
255     else
256       mutt_concat_path(fname, flen, path, mutt_basename(attname));
257   }
258
259   if (*append == 0 && access (fname, F_OK) == 0) {
260     switch (mutt_multi_choice
261             (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
262     {
263     case -1:                   /* abort */
264       return -1;
265     case 3:                    /* cancel */
266       return 1;
267
268     case 2:                    /* append */
269       *append = M_SAVE_APPEND;
270       break;
271     case 1:                    /* overwrite */
272       *append = M_SAVE_OVERWRITE;
273       break;
274     }
275   }
276   return 0;
277 }
278
279 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
280 {
281     if (a && a->mailbox) {
282         m_strncpy(d, dsize, a->mailbox, strcspn(d, "%@"));
283         m_strtolower(d);
284     } else {
285         *d = '\0';
286     }
287 }
288
289 void mutt_safe_path(char *s, ssize_t l, address_t *a)
290 {
291     mutt_save_path(s, l, a);
292
293     while (*s) {
294         if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
295             *s = '_';
296         s++;
297     }
298 }
299
300 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
301 int mutt_save_confirm (const char *s, struct stat *st)
302 {
303   char tmp[_POSIX_PATH_MAX];
304   int ret = 0;
305   int rc;
306   int magic = 0;
307
308   magic = mx_get_magic (s);
309
310   if (magic == M_POP) {
311     mutt_error _("Can't save message to POP mailbox.");
312
313     return 1;
314   }
315
316   if (magic > 0 && !mx_access (s, W_OK)) {
317     if (option (OPTCONFIRMAPPEND) &&
318         (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
319       /* if we're appending to the trash, there's no point in asking */
320       snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
321       if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
322         ret = 1;
323       else if (rc == -1)
324         ret = -1;
325     }
326   }
327
328   if (stat (s, st) != -1) {
329     if (magic == -1) {
330       mutt_error (_("%s is not a mailbox!"), s);
331       return 1;
332     }
333   } else {
334     if (magic != M_IMAP)
335     {
336       st->st_mtime = 0;
337       st->st_atime = 0;
338
339       if (errno == ENOENT) {
340         if (option (OPTCONFIRMCREATE)) {
341           snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
342           if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
343             ret = 1;
344           else if (rc == -1)
345             ret = -1;
346         }
347       } else {
348         mutt_perror (s);
349         return 1;
350       }
351     }
352   }
353   return ret;
354 }
355
356 void mutt_sleep (short s)
357 {
358     sleep(MAX(s, SleepTime));
359 }
360
361 /* return 1 if address lists are strictly identical */
362 static int mutt_cmp_addr (const address_t * a, const address_t * b)
363 {
364   while (a && b) {
365     if (m_strcmp(a->mailbox, b->mailbox) ||
366         m_strcmp(a->personal, b->personal))
367       return 0;
368
369     a = a->next;
370     b = b->next;
371   }
372   return !(a || b);
373 }
374
375 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
376 {
377   while (a && b) {
378     if (m_strcmp(a->data, b->data))
379       return 0;
380
381     a = a->next;
382     b = b->next;
383   }
384   return !(a || b);
385 }
386
387 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
388 {
389   if (e1 && e2) {
390     if (m_strcmp(e1->message_id, e2->message_id) ||
391         m_strcmp(e1->subject, e2->subject) ||
392         !mutt_cmp_list (e1->references, e2->references) ||
393         !mutt_cmp_addr (e1->from, e2->from) ||
394         !mutt_cmp_addr (e1->sender, e2->sender) ||
395         !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
396         !mutt_cmp_addr (e1->to, e2->to) ||
397         !mutt_cmp_addr (e1->cc, e2->cc) ||
398         !mutt_cmp_addr (e1->return_path, e2->return_path))
399       return 0;
400     else
401       return 1;
402   }
403   return e1 == NULL && e2 == NULL;
404 }
405
406 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
407 {
408   if (b1->type != b2->type ||
409       b1->encoding != b2->encoding ||
410       m_strcmp(b1->subtype, b2->subtype) ||
411       m_strcmp(b1->description, b2->description) ||
412       !parameter_equal(b1->parameter, b2->parameter) ||
413       b1->length != b2->length)
414     return 0;
415   return 1;
416 }
417 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
418   if (h1 && h2) {
419     if (h1->received != h2->received ||
420         h1->date_sent != h2->date_sent ||
421         h1->content->length != h2->content->length ||
422         h1->lines != h2->lines ||
423         h1->zhours != h2->zhours ||
424         h1->zminutes != h2->zminutes ||
425         h1->zoccident != h2->zoccident ||
426         h1->mime != h2->mime ||
427         !mutt_cmp_env (h1->env, h2->env) ||
428         !mutt_cmp_body (h1->content, h2->content))
429       return 0;
430     else
431       return 1;
432   }
433   return h1 == NULL && h2 == NULL;
434 }
435
436
437 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
438 {
439     char ch;
440     char qc = 0;                  /* quote char */
441     char *pc;
442
443     /* reset the destination pointer to the beginning of the buffer */
444     dest->dptr = dest->data;
445
446     tok->dptr = vskipspaces(tok->dptr);
447     while ((ch = *tok->dptr)) {
448         if (!qc) {
449             if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
450             || (ch == '#' && !(flags & M_TOKEN_COMMENT))
451             || (ch == '=' && (flags & M_TOKEN_EQUAL))
452             || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
453             {
454                 break;
455             }
456         }
457
458         tok->dptr++;
459
460         if (ch == qc) {
461             qc = 0;                     /* end of quote */
462         } else
463         if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
464             qc = ch;
465         } else
466         if (ch == '\\' && qc != '\'') {
467             if (!*tok->dptr)
468                 return -1;              /* premature end of token */
469
470             switch (ch = *tok->dptr++) {
471               case 'c':
472               case 'C':
473                 if (!*tok->dptr)
474                     return -1;          /* premature end of token */
475                 mutt_buffer_addch(dest,
476                                   (toupper((unsigned char)*tok->dptr) - 'A' + 1) & 0x7f);
477                 tok->dptr++;
478                 break;
479               case 'r':
480                 mutt_buffer_addch(dest, '\r');
481                 break;
482               case 'n':
483                 mutt_buffer_addch(dest, '\n');
484                 break;
485               case 't':
486                 mutt_buffer_addch(dest, '\t');
487                 break;
488               case 'f':
489                 mutt_buffer_addch(dest, '\f');
490                 break;
491               case 'e':
492                 mutt_buffer_addch(dest, '\033');
493                 break;
494               default:
495                 if (isdigit((unsigned char)ch)
496                 &&  isdigit((unsigned char)*tok->dptr)
497                 &&  isdigit((unsigned char)*(tok->dptr + 1)))
498                 {
499                     mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
500                                             *(tok->dptr + 1) - 3504);
501                     tok->dptr += 2;
502                 } else {
503                     mutt_buffer_addch(dest, ch);
504                 }
505             }
506         } else
507         if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
508             if (!*tok->dptr)
509                 return -1;              /* premature end of token */
510             ch = *tok->dptr++;
511             if (ch == '^') {
512                 mutt_buffer_addch(dest, ch);
513             } else
514             if (ch == '[') {
515                 mutt_buffer_addch(dest, '\033');
516             } else
517             if (isalpha((unsigned char)ch)) {
518                 mutt_buffer_addch(dest, toupper((unsigned char)ch) - 'A' + 1);
519             } else {
520                 mutt_buffer_addch(dest, '^');
521                 mutt_buffer_addch(dest, ch);
522             }
523         } else
524         if (ch == '`' && (!qc || qc == '"')) {
525             FILE *fp;
526             pid_t pid;
527             char *cmd, *ptr;
528             ssize_t expnlen;
529             BUFFER expn;
530             int line = 0;
531
532             pc = tok->dptr;
533             do {
534                 if ((pc = strpbrk(pc, "\\`"))) {
535                     /* skip any quoted chars */
536                     if (*pc == '\\')
537                         pc += 2;
538                 }
539             } while (pc && *pc != '`');
540             if (!pc) {
541                 return -1;
542             }
543
544             cmd = p_dupstr(tok->dptr, pc - tok->dptr);
545             if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
546                 p_delete(&cmd);
547                 return -1;
548             }
549             p_delete(&cmd);
550
551             tok->dptr = pc + 1;
552
553             /* read line */
554             p_clear(&expn, 1);
555             expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
556             m_fclose(&fp);
557             mutt_wait_filter(pid);
558
559             /* if we got output, make a new string consiting of the shell ouptput
560                plus whatever else was left on the original line */
561             /* BUT: If this is inside a quoted string, directly add output to 
562              * the token */
563             if (expn.data && qc) {
564                 mutt_buffer_addstr(dest, expn.data);
565                 p_delete(&expn.data);
566             } else
567             if (expn.data) {
568                 expnlen = m_strlen(expn.data);
569                 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
570                 ptr = xmalloc(tok->dsize);
571                 memcpy(ptr, expn.data, expnlen);
572                 m_strcpy(ptr + expnlen, tok->dsize - expnlen, tok->dptr);
573                 if (tok->destroy)
574                     p_delete(&tok->data);
575                 tok->data = ptr;
576                 tok->dptr = ptr;
577                 tok->destroy = 1;       /* mark that the caller should destroy this data */
578                 ptr = NULL;
579                 p_delete(&expn.data);
580             }
581         } else
582         if (ch == '$' && (!qc || qc == '"')
583         && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
584         {
585             char *env = NULL, *var = NULL;
586
587             if (*tok->dptr == '{') {
588                 tok->dptr++;
589                 if ((pc = strchr (tok->dptr, '}'))) {
590                     var = p_dupstr(tok->dptr, pc - tok->dptr);
591                     tok->dptr = pc + 1;
592                 }
593             } else {
594                 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
595                      pc++);
596                 var = p_dupstr(tok->dptr, pc - tok->dptr);
597                 tok->dptr = pc;
598             }
599             if (var) {
600                 char tmp[STRING];
601                 if ((env = getenv (var))
602                 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
603                 {
604                     mutt_buffer_addstr (dest, env);
605                 }
606             }
607             p_delete(&var);
608         } else {
609             mutt_buffer_addch(dest, ch);
610         }
611     }
612     mutt_buffer_addch(dest, 0);  /* terminate the string */
613     tok->dptr = vskipspaces(tok->dptr);
614     return 0;
615 }