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