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