Rocco Rutte:
[apps/madmutt.git] / hook.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>, and others
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include "mutt.h"
15 #include "mx.h"
16 #include "mutt_crypt.h"
17
18 #ifdef USE_COMPRESSED
19 #include "compress.h"
20 #endif
21
22 #include "lib/mem.h"
23 #include "lib/intl.h"
24 #include "lib/str.h"
25 #include "lib/rx.h"
26
27 #include <limits.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <ctype.h>
31 #include <unistd.h>
32
33 typedef struct hook {
34   int type;                     /* hook type */
35   rx_t rx;                      /* regular expression */
36   char *command;                /* filename, command or pattern to execute */
37   pattern_t *pattern;           /* used for fcc,save,send-hook */
38   struct hook *next;
39 } HOOK;
40
41 static HOOK *Hooks = NULL;
42
43 static int current_hook_type = 0;
44
45 int mutt_parse_hook (BUFFER * buf, BUFFER * s, unsigned long data,
46                      BUFFER * err)
47 {
48   HOOK *ptr;
49   BUFFER command, pattern;
50   int rc, not = 0;
51   regex_t *rx = NULL;
52   pattern_t *pat = NULL;
53   char path[_POSIX_PATH_MAX];
54
55   memset (&pattern, 0, sizeof (pattern));
56   memset (&command, 0, sizeof (command));
57
58   if (*s->dptr == '!') {
59     s->dptr++;
60     SKIPWS (s->dptr);
61     not = 1;
62   }
63
64   mutt_extract_token (&pattern, s, 0);
65
66   if (!MoreArgs (s)) {
67     strfcpy (err->data, _("too few arguments"), err->dsize);
68     goto error;
69   }
70
71   mutt_extract_token (&command, s,
72                       (data &
73                        (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK |
74                         M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
75
76   if (!command.data) {
77     strfcpy (err->data, _("too few arguments"), err->dsize);
78     goto error;
79   }
80
81   if (MoreArgs (s)) {
82     strfcpy (err->data, _("too many arguments"), err->dsize);
83     goto error;
84   }
85
86   if (data & (M_FOLDERHOOK | M_MBOXHOOK)) {
87     strfcpy (path, pattern.data, sizeof (path));
88     _mutt_expand_path (path, sizeof (path), 1);
89     FREE (&pattern.data);
90     memset (&pattern, 0, sizeof (pattern));
91     pattern.data = safe_strdup (path);
92   }
93 #ifdef USE_COMPRESSED
94   else if (data & (M_APPENDHOOK | M_OPENHOOK | M_CLOSEHOOK)) {
95     if (mutt_test_compress_command (command.data)) {
96       strfcpy (err->data, _("bad formatted command string"), err->dsize);
97       return (-1);
98     }
99   }
100 #endif
101   else if (DefaultHook && !(data & (M_CHARSETHOOK | M_ACCOUNTHOOK))
102            && (!WithCrypto || !(data & M_CRYPTHOOK))
103     ) {
104     char tmp[HUGE_STRING];
105
106     strfcpy (tmp, pattern.data, sizeof (tmp));
107     mutt_check_simple (tmp, sizeof (tmp), DefaultHook);
108     FREE (&pattern.data);
109     memset (&pattern, 0, sizeof (pattern));
110     pattern.data = safe_strdup (tmp);
111   }
112
113   if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK)) {
114     strfcpy (path, command.data, sizeof (path));
115     mutt_expand_path (path, sizeof (path));
116     FREE (&command.data);
117     memset (&command, 0, sizeof (command));
118     command.data = safe_strdup (path);
119   }
120
121   /* check to make sure that a matching hook doesn't already exist */
122   for (ptr = Hooks; ptr; ptr = ptr->next) {
123     if (ptr->type == data &&
124         ptr->rx.not == not && !safe_strcmp (pattern.data, ptr->rx.pattern)) {
125       if (data &
126           (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK | M_MESSAGEHOOK |
127            M_ACCOUNTHOOK | M_REPLYHOOK)) {
128         /* these hooks allow multiple commands with the same
129          * pattern, so if we've already seen this pattern/command pair, just
130          * ignore it instead of creating a duplicate */
131         if (!safe_strcmp (ptr->command, command.data)) {
132           FREE (&command.data);
133           FREE (&pattern.data);
134           return 0;
135         }
136       }
137       else {
138         /* other hooks only allow one command per pattern, so update the
139          * entry with the new command.  this currently does not change the
140          * order of execution of the hooks, which i think is desirable since
141          * a common action to perform is to change the default (.) entry
142          * based upon some other information. */
143         FREE (&ptr->command);
144         ptr->command = command.data;
145         FREE (&pattern.data);
146         return 0;
147       }
148     }
149     if (!ptr->next)
150       break;
151   }
152
153   if (data &
154       (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK | M_MESSAGEHOOK |
155        M_REPLYHOOK)) {
156     if ((pat =
157          mutt_pattern_comp (pattern.data,
158                             (data & (M_SENDHOOK | M_SEND2HOOK | M_FCCHOOK)) ?
159                             0 : M_FULL_MSG, err)) == NULL)
160       goto error;
161   }
162   else {
163     rx = safe_malloc (sizeof (regex_t));
164 #ifdef M_CRYPTHOOK
165     if ((rc =
166          REGCOMP (rx, NONULL (pattern.data),
167                   ((data & (M_CRYPTHOOK | M_CHARSETHOOK)) ? REG_ICASE : 0)))
168         != 0)
169 #else
170     if ((rc =
171          REGCOMP (rx, NONULL (pattern.data),
172                   (data & (M_CHARSETHOOK | M_ICONVHOOK)) ? REG_ICASE : 0)) !=
173         0)
174 #endif /* M_CRYPTHOOK */
175     {
176       regerror (rc, rx, err->data, err->dsize);
177       regfree (rx);
178       FREE (&rx);
179       goto error;
180     }
181   }
182
183   if (ptr) {
184     ptr->next = safe_calloc (1, sizeof (HOOK));
185     ptr = ptr->next;
186   }
187   else
188     Hooks = ptr = safe_calloc (1, sizeof (HOOK));
189   ptr->type = data;
190   ptr->command = command.data;
191   ptr->pattern = pat;
192   ptr->rx.pattern = pattern.data;
193   ptr->rx.rx = rx;
194   ptr->rx.not = not;
195   return 0;
196
197 error:
198   FREE (&pattern.data);
199   FREE (&command.data);
200   return (-1);
201 }
202
203 static void delete_hook (HOOK * h)
204 {
205   FREE (&h->command);
206   FREE (&h->rx.pattern);
207   if (h->rx.rx) {
208     regfree (h->rx.rx);
209   }
210   mutt_pattern_free (&h->pattern);
211   FREE (&h);
212 }
213
214 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
215 static void delete_hooks (int type)
216 {
217   HOOK *h;
218   HOOK *prev;
219
220   while (h = Hooks, h && (type == 0 || type == h->type)) {
221     Hooks = h->next;
222     delete_hook (h);
223   }
224
225   prev = h;                     /* Unused assignment to avoid compiler warnings */
226
227   while (h) {
228     if (type == h->type) {
229       prev->next = h->next;
230       delete_hook (h);
231     }
232     else
233       prev = h;
234     h = prev->next;
235   }
236 }
237
238 int mutt_parse_unhook (BUFFER * buf, BUFFER * s, unsigned long data,
239                        BUFFER * err)
240 {
241   while (MoreArgs (s)) {
242     mutt_extract_token (buf, s, 0);
243     if (safe_strcmp ("*", buf->data) == 0) {
244       if (current_hook_type) {
245         snprintf (err->data, err->dsize,
246                   _("unhook: Can't do unhook * from within a hook."));
247         return -1;
248       }
249       delete_hooks (0);
250     }
251     else {
252       int type = mutt_get_hook_type (buf->data);
253
254       if (!type) {
255         snprintf (err->data, err->dsize,
256                   _("unhook: unknown hook type: %s"), buf->data);
257         return (-1);
258       }
259       if (current_hook_type == type) {
260         snprintf (err->data, err->dsize,
261                   _("unhook: Can't delete a %s from within a %s."), buf->data,
262                   buf->data);
263         return -1;
264       }
265       delete_hooks (type);
266     }
267   }
268   return 0;
269 }
270
271 void mutt_folder_hook (char *path)
272 {
273   HOOK *tmp = Hooks;
274   BUFFER err, token;
275   char buf[STRING];
276
277   current_hook_type = M_FOLDERHOOK;
278
279   err.data = buf;
280   err.dsize = sizeof (buf);
281   memset (&token, 0, sizeof (token));
282   for (; tmp; tmp = tmp->next) {
283     if (!tmp->command)
284       continue;
285
286     if (tmp->type & M_FOLDERHOOK) {
287       if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.not) {
288         if (mutt_parse_rc_line (tmp->command, &token, &err) == -1) {
289           mutt_error ("%s", err.data);
290           FREE (&token.data);
291           mutt_sleep (1);       /* pause a moment to let the user see the error */
292           current_hook_type = 0;
293           return;
294         }
295       }
296     }
297   }
298   FREE (&token.data);
299
300   current_hook_type = 0;
301 }
302
303 char *mutt_find_hook (int type, const char *pat)
304 {
305   HOOK *tmp = Hooks;
306
307   for (; tmp; tmp = tmp->next)
308     if (tmp->type & type) {
309       if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
310         return (tmp->command);
311     }
312   return (NULL);
313 }
314
315 void mutt_message_hook (CONTEXT * ctx, HEADER * hdr, int type)
316 {
317   BUFFER err, token;
318   HOOK *hook;
319   char buf[STRING];
320
321   current_hook_type = type;
322
323   err.data = buf;
324   err.dsize = sizeof (buf);
325   memset (&token, 0, sizeof (token));
326   for (hook = Hooks; hook; hook = hook->next) {
327     if (!hook->command)
328       continue;
329
330     if (hook->type & type)
331       if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
332         if (mutt_parse_rc_line (hook->command, &token, &err) != 0) {
333           FREE (&token.data);
334           mutt_error ("%s", err.data);
335           mutt_sleep (1);
336           current_hook_type = 0;
337           return;
338         }
339   }
340   FREE (&token.data);
341   current_hook_type = 0;
342 }
343
344 static int
345 mutt_addr_hook (char *path, size_t pathlen, int type, CONTEXT * ctx,
346                 HEADER * hdr)
347 {
348   HOOK *hook;
349
350   /* determine if a matching hook exists */
351   for (hook = Hooks; hook; hook = hook->next) {
352     if (!hook->command)
353       continue;
354
355     if (hook->type & type)
356       if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not) {
357         mutt_make_string (path, pathlen, hook->command, ctx, hdr);
358         return 0;
359       }
360   }
361
362   return -1;
363 }
364
365 void mutt_default_save (char *path, size_t pathlen, HEADER * hdr)
366 {
367   *path = 0;
368   if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0) {
369     char tmp[_POSIX_PATH_MAX];
370     ADDRESS *adr;
371     ENVELOPE *env = hdr->env;
372     int fromMe = mutt_addr_is_user (env->from);
373
374     if (!fromMe && env->reply_to && env->reply_to->mailbox)
375       adr = env->reply_to;
376     else if (!fromMe && env->from && env->from->mailbox)
377       adr = env->from;
378     else if (env->to && env->to->mailbox)
379       adr = env->to;
380     else if (env->cc && env->cc->mailbox)
381       adr = env->cc;
382     else
383       adr = NULL;
384     if (adr) {
385       mutt_safe_path (tmp, sizeof (tmp), adr);
386       snprintf (path, pathlen, "=%s", tmp);
387     }
388   }
389 }
390
391 void mutt_select_fcc (char *path, size_t pathlen, HEADER * hdr)
392 {
393   ADDRESS *adr;
394   char buf[_POSIX_PATH_MAX];
395   ENVELOPE *env = hdr->env;
396
397   if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0) {
398     if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
399         (env->to || env->cc || env->bcc)) {
400       adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
401       mutt_safe_path (buf, sizeof (buf), adr);
402       mutt_concat_path (path, NONULL (Maildir), buf, pathlen);
403       if (!option (OPTFORCENAME) && mx_access (path, W_OK) != 0)
404         strfcpy (path, NONULL (Outbox), pathlen);
405     }
406     else
407       strfcpy (path, NONULL (Outbox), pathlen);
408   }
409   mutt_pretty_mailbox (path);
410 }
411
412 static char *_mutt_string_hook (const char *match, int hook)
413 {
414   HOOK *tmp = Hooks;
415
416   for (; tmp; tmp = tmp->next) {
417     if ((tmp->type & hook) && ((match &&
418                                 regexec (tmp->rx.rx, match, 0, NULL,
419                                          0) == 0) ^ tmp->rx.not))
420       return (tmp->command);
421   }
422   return (NULL);
423 }
424
425 char *mutt_charset_hook (const char *chs)
426 {
427   return _mutt_string_hook (chs, M_CHARSETHOOK);
428 }
429
430 char *mutt_iconv_hook (const char *chs)
431 {
432   return _mutt_string_hook (chs, M_ICONVHOOK);
433 }
434
435 char *mutt_crypt_hook (ADDRESS * adr)
436 {
437   return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
438 }
439
440 #ifdef USE_SOCKET
441 void mutt_account_hook (const char *url)
442 {
443   HOOK *hook;
444   BUFFER token;
445   BUFFER err;
446   char buf[STRING];
447
448   err.data = buf;
449   err.dsize = sizeof (buf);
450   memset (&token, 0, sizeof (token));
451
452   for (hook = Hooks; hook; hook = hook->next) {
453     if (!(hook->command && (hook->type & M_ACCOUNTHOOK)))
454       continue;
455
456     if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.not) {
457       if (mutt_parse_rc_line (hook->command, &token, &err) == -1) {
458         FREE (&token.data);
459         mutt_error ("%s", err.data);
460         mutt_sleep (1);
461
462         return;
463       }
464     }
465   }
466
467   FREE (&token.data);
468 }
469 #endif