1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/*
* Copyright (C) 1996-8 Michael R. Elkins <me@cs.hmc.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "mutt.h"
#include "mutt_regex.h"
#include "pattern.h"
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
typedef struct hook
{
int type; /* hook type */
REGEXP rx; /* regular expression */
char * command; /* filename, command or pattern to execute */
pattern_t *pattern; /* used for fcc,save,send-hook */
struct hook *next;
} HOOK;
static HOOK *Hooks = NULL;
int mutt_parse_hook (const char *s, unsigned long data, char *err, size_t errlen)
{
HOOK *ptr;
char expn[LONG_STRING];
char command[LONG_STRING];
char pattern[STRING];
int type = data;
regex_t *rx = NULL;
pattern_t *pat = NULL;
int rc, not = 0;
if (*s == '!')
{
s++;
SKIPWS (s);
not = 1;
}
s = mutt_extract_token (pattern, sizeof (pattern), s, expn, sizeof (expn), 0);
if (!s)
{
strfcpy (err, "too few arguments", errlen);
return (-1);
}
s = mutt_extract_token (command, sizeof (command), s, expn, sizeof (expn),
(type & (M_FOLDERHOOK | M_SENDHOOK)) ? M_SPACE : 0);
if (!*command)
{
strfcpy (err, "too few arguments", errlen);
return (-1);
}
if (s)
{
strfcpy (err, "too many arguments", errlen);
return (-1);
}
if (type & (M_FOLDERHOOK | M_MBOXHOOK))
mutt_expand_path (pattern, sizeof (pattern));
if (type & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK))
mutt_expand_path (command, sizeof (command));
/* check to make sure that a matching hook doesn't already exist */
for (ptr = Hooks; ptr; ptr = ptr->next)
{
if (ptr->type == type && ptr->rx.not == not &&
!strcmp (pattern, ptr->rx.pattern) &&
!strcmp (command, ptr->command))
return 0; /* skip it */
if (!ptr->next)
break;
}
if (type & (M_SENDHOOK | M_SAVEHOOK | M_FCCHOOK))
{
mutt_check_simple (pattern, sizeof (pattern), DefaultHook);
if ((pat = mutt_pattern_comp (pattern, 0, err, errlen)) == NULL)
{
return (-1);
}
}
else
{
rx = safe_malloc (sizeof (regex_t));
if ((rc = REGCOMP (rx, pattern, 0)) != 0)
{
regerror (rc, rx, err, errlen);
regfree (rx);
safe_free ((void **) &rx);
return (-1);
}
}
if (ptr)
{
ptr->next = safe_calloc (1, sizeof (HOOK));
ptr = ptr->next;
}
else
Hooks = ptr = safe_calloc (1, sizeof (HOOK));
ptr->type = type;
ptr->command = safe_strdup (command);
ptr->pattern = pat;
ptr->rx.pattern = safe_strdup (pattern);
ptr->rx.rx = rx;
ptr->rx.not = not;
return 0;
}
void mutt_folder_hook (char *path)
{
HOOK *tmp = Hooks;
char err[SHORT_STRING];
for (; tmp; tmp = tmp->next)
if (tmp->type & M_FOLDERHOOK)
{
if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.not)
{
if (mutt_parse_rc_line (tmp->command, err, sizeof (err)) == -1)
{
mutt_error ("%s", err);
sleep (1); /* pause a moment to let the user see the error */
return;
}
}
}
}
char *mutt_find_hook (int type, const char *pat)
{
HOOK *tmp = Hooks;
for (; tmp; tmp = tmp->next)
if (tmp->type & type)
{
if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
return (tmp->command);
}
return (NULL);
}
void mutt_send_hook (HEADER *hdr)
{
char err[SHORT_STRING];
HOOK *hook;
for (hook = Hooks; hook; hook = hook->next)
if (hook->type & M_SENDHOOK)
if ((mutt_pattern_exec (hook->pattern, 0, NULL, hdr) > 0) ^ hook->rx.not)
if (mutt_parse_rc_line (hook->command, err, sizeof (err)) != 0)
{
mutt_error ("%s", err);
sleep (1);
return;
}
}
static int mutt_addr_hook (char *path,
size_t pathlen,
int type,
CONTEXT *ctx,
HEADER *hdr)
{
HOOK *hook;
/* determine if a matching hook exists */
for (hook = Hooks; hook; hook = hook->next)
if (hook->type & type)
if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
{
strfcpy (path, hook->command, pathlen);
return 0;
}
return -1;
}
void mutt_default_save (char *path, size_t pathlen, HEADER *hdr)
{
char tmp[_POSIX_PATH_MAX];
ADDRESS *adr;
ENVELOPE *env = hdr->env;
if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) == 0)
return;
/* check to see if this is a mailing list */
for (adr = env->to; adr; adr = adr->next)
if (mutt_is_mail_list (adr))
break;
/* check the CC: list */
if (!adr)
{
for (adr = env->cc; adr; adr = adr->next)
if (mutt_is_mail_list (adr))
break;
}
/* pick default based upon sender */
if (!adr)
{
int fromMe = mutt_addr_is_user (env->from);
if (!fromMe && env->reply_to && env->reply_to->mailbox)
adr = env->reply_to;
else if (!fromMe && env->from && env->from->mailbox)
adr = env->from;
else if (env->to && env->to->mailbox)
adr = env->to;
else if (env->cc && env->cc->mailbox)
adr = env->cc;
}
mutt_safe_path (tmp, sizeof (tmp), adr);
snprintf (path, pathlen, "=%s", tmp);
}
void mutt_select_fcc (char *path, size_t pathlen, HEADER *hdr)
{
ADDRESS *adr;
char buf[_POSIX_PATH_MAX];
ENVELOPE *env = hdr->env;
if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0)
{
if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
(env->to || env->cc || env->bcc))
{
adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
mutt_safe_path (buf, sizeof (buf), adr);
snprintf (path, pathlen, "%s/%s", Maildir, buf);
if (!option (OPTFORCENAME) && access (path, W_OK) != 0)
strfcpy (path, Outbox, pathlen);
}
else
strfcpy (path, Outbox, pathlen);
}
mutt_pretty_mailbox (path);
}
|