We don't need a function for mutt_make_version().
[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   if (a || b)
373     return (0);
374
375   return (1);
376 }
377
378 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
379 {
380   while (a && b) {
381     if (m_strcmp(a->data, b->data))
382       return (0);
383
384     a = a->next;
385     b = b->next;
386   }
387   if (a || b)
388     return (0);
389
390   return (1);
391 }
392
393 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
394 {
395   if (e1 && e2) {
396     if (m_strcmp(e1->message_id, e2->message_id) ||
397         m_strcmp(e1->subject, e2->subject) ||
398         !mutt_cmp_list (e1->references, e2->references) ||
399         !mutt_cmp_addr (e1->from, e2->from) ||
400         !mutt_cmp_addr (e1->sender, e2->sender) ||
401         !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
402         !mutt_cmp_addr (e1->to, e2->to) ||
403         !mutt_cmp_addr (e1->cc, e2->cc) ||
404         !mutt_cmp_addr (e1->return_path, e2->return_path))
405       return (0);
406     else
407       return (1);
408   }
409   else {
410     if (e1 == NULL && e2 == NULL)
411       return (1);
412     else
413       return (0);
414   }
415 }
416
417 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
418 {
419   if (b1->type != b2->type ||
420       b1->encoding != b2->encoding ||
421       m_strcmp(b1->subtype, b2->subtype) ||
422       m_strcmp(b1->description, b2->description) ||
423       !parameter_equal(b1->parameter, b2->parameter) ||
424       b1->length != b2->length)
425     return (0);
426   return (1);
427 }
428 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
429   if (h1 && h2) {
430     if (h1->received != h2->received ||
431         h1->date_sent != h2->date_sent ||
432         h1->content->length != h2->content->length ||
433         h1->lines != h2->lines ||
434         h1->zhours != h2->zhours ||
435         h1->zminutes != h2->zminutes ||
436         h1->zoccident != h2->zoccident ||
437         h1->mime != h2->mime ||
438         !mutt_cmp_env (h1->env, h2->env) ||
439         !mutt_cmp_body (h1->content, h2->content))
440       return (0);
441     else
442       return (1);
443   }
444   else {
445     if (h1 == NULL && h2 == NULL)
446       return (1);
447     else
448       return (0);
449   }
450 }
451
452
453 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
454 {
455     char ch;
456     char qc = 0;                  /* quote char */
457     char *pc;
458
459     /* reset the destination pointer to the beginning of the buffer */
460     dest->dptr = dest->data;
461
462     tok->dptr = vskipspaces(tok->dptr);
463     while ((ch = *tok->dptr)) {
464         if (!qc) {
465             if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
466             || (ch == '#' && !(flags & M_TOKEN_COMMENT))
467             || (ch == '=' && (flags & M_TOKEN_EQUAL))
468             || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
469             {
470                 break;
471             }
472         }
473
474         tok->dptr++;
475
476         if (ch == qc) {
477             qc = 0;                     /* end of quote */
478         } else
479         if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
480             qc = ch;
481         } else
482         if (ch == '\\' && qc != '\'') {
483             if (!*tok->dptr)
484                 return -1;              /* premature end of token */
485
486             switch (ch = *tok->dptr++) {
487               case 'c':
488               case 'C':
489                 if (!*tok->dptr)
490                     return -1;          /* premature end of token */
491                 mutt_buffer_addch(dest,
492                                   (toupper((unsigned char)*tok->dptr) - 'A' + 1) & 0x7f);
493                 tok->dptr++;
494                 break;
495               case 'r':
496                 mutt_buffer_addch(dest, '\r');
497                 break;
498               case 'n':
499                 mutt_buffer_addch(dest, '\n');
500                 break;
501               case 't':
502                 mutt_buffer_addch(dest, '\t');
503                 break;
504               case 'f':
505                 mutt_buffer_addch(dest, '\f');
506                 break;
507               case 'e':
508                 mutt_buffer_addch(dest, '\033');
509                 break;
510               default:
511                 if (isdigit((unsigned char)ch)
512                 &&  isdigit((unsigned char)*tok->dptr)
513                 &&  isdigit((unsigned char)*(tok->dptr + 1)))
514                 {
515                     mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
516                                             *(tok->dptr + 1) - 3504);
517                     tok->dptr += 2;
518                 } else {
519                     mutt_buffer_addch(dest, ch);
520                 }
521             }
522         } else
523         if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
524             if (!*tok->dptr)
525                 return -1;              /* premature end of token */
526             ch = *tok->dptr++;
527             if (ch == '^') {
528                 mutt_buffer_addch(dest, ch);
529             } else
530             if (ch == '[') {
531                 mutt_buffer_addch(dest, '\033');
532             } else
533             if (isalpha((unsigned char)ch)) {
534                 mutt_buffer_addch(dest, toupper((unsigned char)ch) - 'A' + 1);
535             } else {
536                 mutt_buffer_addch(dest, '^');
537                 mutt_buffer_addch(dest, ch);
538             }
539         } else
540         if (ch == '`' && (!qc || qc == '"')) {
541             FILE *fp;
542             pid_t pid;
543             char *cmd, *ptr;
544             ssize_t expnlen;
545             BUFFER expn;
546             int line = 0;
547
548             pc = tok->dptr;
549             do {
550                 if ((pc = strpbrk(pc, "\\`"))) {
551                     /* skip any quoted chars */
552                     if (*pc == '\\')
553                         pc += 2;
554                 }
555             } while (pc && *pc != '`');
556             if (!pc) {
557                 return (-1);
558             }
559
560             cmd = p_dupstr(tok->dptr, pc - tok->dptr);
561             if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
562                 p_delete(&cmd);
563                 return -1;
564             }
565             p_delete(&cmd);
566
567             tok->dptr = pc + 1;
568
569             /* read line */
570             p_clear(&expn, 1);
571             expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
572             m_fclose(&fp);
573             mutt_wait_filter(pid);
574
575             /* if we got output, make a new string consiting of the shell ouptput
576                plus whatever else was left on the original line */
577             /* BUT: If this is inside a quoted string, directly add output to 
578              * the token */
579             if (expn.data && qc) {
580                 mutt_buffer_addstr(dest, expn.data);
581                 p_delete(&expn.data);
582             } else
583             if (expn.data) {
584                 expnlen = m_strlen(expn.data);
585                 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
586                 ptr = xmalloc(tok->dsize);
587                 memcpy(ptr, expn.data, expnlen);
588                 m_strcpy(ptr + expnlen, tok->dsize - expnlen, tok->dptr);
589                 if (tok->destroy)
590                     p_delete(&tok->data);
591                 tok->data = ptr;
592                 tok->dptr = ptr;
593                 tok->destroy = 1;       /* mark that the caller should destroy this data */
594                 ptr = NULL;
595                 p_delete(&expn.data);
596             }
597         } else
598         if (ch == '$' && (!qc || qc == '"')
599         && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
600         {
601             char *env = NULL, *var = NULL;
602
603             if (*tok->dptr == '{') {
604                 tok->dptr++;
605                 if ((pc = strchr (tok->dptr, '}'))) {
606                     var = p_dupstr(tok->dptr, pc - tok->dptr);
607                     tok->dptr = pc + 1;
608                 }
609             } else {
610                 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
611                      pc++);
612                 var = p_dupstr(tok->dptr, pc - tok->dptr);
613                 tok->dptr = pc;
614             }
615             if (var) {
616                 char tmp[STRING];
617                 if ((env = getenv (var))
618                 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
619                 {
620                     mutt_buffer_addstr (dest, env);
621                 }
622             }
623             p_delete(&var);
624         } else {
625             mutt_buffer_addch(dest, ch);
626         }
627     }
628     mutt_buffer_addch(dest, 0);  /* terminate the string */
629     tok->dptr = vskipspaces(tok->dptr);
630     return 0;
631 }