Remove most of the hooks
[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 #include <lib-lib/lib-lib.h>
11 #include <lib-lua/lib-lua.h>
12 #include <lib-ui/lib-ui.h>
13 #include <lib-mx/mx.h>
14 #include <lib-mx/compress.h>
15
16 #include "alias.h"
17 #include "crypt.h"
18 #include "pattern.h"
19
20 typedef struct hook_t {
21   int type;                     /* hook type */
22   rx_t rx;                      /* regular expression */
23   char *command;                /* filename, command or pattern to execute */
24   pattern_t *pattern;           /* used for fcc,save,send-hook */
25   struct hook_t *next;
26 } hook_t;
27
28 DO_INIT(hook_t, hook);
29 static inline void hook_wipe(hook_t *hk) {
30     p_delete(&hk->command);
31     pattern_list_wipe(&hk->pattern);
32 }
33 DO_NEW(hook_t, hook);
34 DO_DELETE(hook_t, hook);
35 DO_SLIST(hook_t, hook, hook_delete);
36
37 static hook_t *Hooks = NULL;
38 static unsigned long current_hook_type = 0;
39
40 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
41 void mutt_folder_hook (char *path)
42 {
43     lua_State *L = luaM_getruntime();
44     lua_getglobal(L, "mod_core");             /* push mod_core        1 */
45     lua_getfield(L, -1, "folder_hook");       /* push folder_hook()   2 */
46     if (lua_isfunction(L, -1)) {
47         lua_pushstring(L, LastFolder);
48         lua_pushstring(L, CurrentFolder);
49         if (lua_pcall(L, 2, 0, 0)) {
50             lua_pop(L, 3);
51             return;
52         }
53     }
54     lua_pop(L, 2);
55 }
56
57 void mutt_message_hook (CONTEXT * ctx, HEADER * hdr, int type)
58 {
59   BUFFER err, token;
60   hook_t *hook;
61   char buf[STRING];
62
63   current_hook_type = type;
64
65   err.data = buf;
66   err.dsize = sizeof (buf);
67   p_clear(&token, 1);
68   for (hook = Hooks; hook; hook = hook->next) {
69     if (!hook->command)
70       continue;
71
72     if (hook->type & type)
73       if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.neg)
74         if (mutt_parse_rc_line (hook->command, &token, &err) != 0) {
75           mutt_error ("%s", err.data);
76           mutt_sleep (1);
77         }
78   }
79   p_delete(&token.data);
80   current_hook_type = 0;
81 }
82
83 void mutt_default_save (char *path, ssize_t pathlen, HEADER * hdr)
84 {
85   char tmp[_POSIX_PATH_MAX];
86   address_t *adr;
87   ENVELOPE *env = hdr->env;
88   int fromMe = mutt_addr_is_user (env->from);
89
90   if (!fromMe && env->reply_to && env->reply_to->mailbox)
91     adr = env->reply_to;
92   else if (!fromMe && env->from && env->from->mailbox)
93     adr = env->from;
94   else if (env->to && env->to->mailbox)
95     adr = env->to;
96   else if (env->cc && env->cc->mailbox)
97     adr = env->cc;
98   else
99     adr = NULL;
100   if (adr) {
101     mutt_safe_path (tmp, sizeof (tmp), adr);
102     snprintf (path, pathlen, "=%s", tmp);
103   } else {
104     *path = 0;
105   }
106 }
107
108 void mutt_select_fcc (char *path, ssize_t pathlen, HEADER * hdr)
109 {
110     m_strcpy(path, pathlen, NONULL(MAlias.record));
111     mutt_pretty_mailbox(path);
112 }