NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
hook.c File Reference

Parse and execute user-defined hooks. More...

#include "config.h"
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "alias/lib.h"
#include "mutt.h"
#include "hook.h"
#include "ncrypt/lib.h"
#include "pattern/lib.h"
#include "context.h"
#include "format_flags.h"
#include "hdrline.h"
#include "init.h"
#include "mutt_attach.h"
#include "mutt_commands.h"
#include "mutt_globals.h"
#include "muttlib.h"
#include "mx.h"
#include "compmbox/lib.h"
+ Include dependency graph for hook.c:

Go to the source code of this file.

Data Structures

struct  Hook
 A list of user hooks. More...
 

Functions

 TAILQ_HEAD (HookList, Hook)
 
enum CommandResult mutt_parse_hook (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'hook' family of commands - Implements Command::parse() -This is used by 'account-hook', 'append-hook' and many more. More...
 
static void delete_hook (struct Hook *h)
 Delete a Hook. More...
 
void mutt_delete_hooks (HookFlags type)
 Delete matching hooks. More...
 
static void idxfmt_hashelem_free (int type, void *obj, intptr_t data)
 Delete an index-format-hook from the Hash Table - Implements hash_hdata_free_t. More...
 
static void delete_idxfmt_hooks (void)
 Delete all the index-format-hooks. More...
 
enum CommandResult mutt_parse_idxfmt_hook (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'index-format-hook' command - Implements Command::parse() -. More...
 
enum CommandResult mutt_parse_unhook (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'unhook' command - Implements Command::parse() -. More...
 
void mutt_folder_hook (const char *path, const char *desc)
 Perform a folder hook. More...
 
char * mutt_find_hook (HookFlags type, const char *pat)
 Find a matching hook. More...
 
void mutt_message_hook (struct Mailbox *m, struct Email *e, HookFlags type)
 Perform a message hook. More...
 
static int addr_hook (char *path, size_t pathlen, HookFlags type, struct Mailbox *m, struct Email *e)
 Perform an address hook (get a path) More...
 
void mutt_default_save (char *path, size_t pathlen, struct Email *e)
 Find the default save path for an email. More...
 
void mutt_select_fcc (struct Buffer *path, struct Email *e)
 Select the FCC path for an email. More...
 
static void list_hook (struct ListHead *matches, const char *match, HookFlags hook)
 Find hook strings matching. More...
 
void mutt_crypt_hook (struct ListHead *list, struct Address *addr)
 Find crypto hooks for an Address. More...
 
void mutt_account_hook (const char *url)
 Perform an account hook. More...
 
void mutt_timeout_hook (void)
 Execute any timeout hooks. More...
 
void mutt_startup_shutdown_hook (HookFlags type)
 Execute any startup/shutdown hooks. More...
 
const char * mutt_idxfmt_hook (const char *name, struct Mailbox *m, struct Email *e)
 Get index-format-hook format string. More...
 

Variables

static struct HookList Hooks = TAILQ_HEAD_INITIALIZER(Hooks)
 
static struct HashTableIdxFmtHooks = NULL
 
static HookFlags current_hook_type = MUTT_HOOK_NO_FLAGS
 

Detailed Description

Parse and execute user-defined hooks.

Authors
  • Michael R. Elkins, and others
  • Pietro Cerutti

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, see http://www.gnu.org/licenses/.

Definition in file hook.c.

Function Documentation

◆ TAILQ_HEAD()

TAILQ_HEAD ( HookList  ,
Hook   
)

◆ delete_hook()

static void delete_hook ( struct Hook h)
static

Delete a Hook.

Parameters
hHook to delete

Definition at line 309 of file hook.c.

310 {
311  FREE(&h->command);
312  FREE(&h->regex.pattern);
313  if (h->regex.regex)
314  {
315  regfree(h->regex.regex);
316  FREE(&h->regex.regex);
317  }
319  FREE(&h);
320 }
regex_t * regex
compiled expression
Definition: regex3.h:92
char * command
Filename, command or pattern to execute.
Definition: hook.c:66
struct Regex regex
Regular expression.
Definition: hook.c:65
#define FREE(x)
Definition: memory.h:40
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition: compile.c:1038
char * pattern
printable version
Definition: regex3.h:91
struct PatternList * pattern
Used for fcc,save,send-hook.
Definition: hook.c:67
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_delete_hooks()

void mutt_delete_hooks ( HookFlags  type)

Delete matching hooks.

Parameters
typeHook type to delete, see HookFlags

If 0 is passed, all the hooks will be deleted.

Definition at line 328 of file hook.c.

329 {
330  struct Hook *h = NULL;
331  struct Hook *tmp = NULL;
332 
333  TAILQ_FOREACH_SAFE(h, &Hooks, entries, tmp)
334  {
335  if ((type == MUTT_HOOK_NO_FLAGS) || (type == h->type))
336  {
337  TAILQ_REMOVE(&Hooks, h, entries);
338  delete_hook(h);
339  }
340  }
341 }
static struct HookList Hooks
Definition: hook.c:72
HookFlags type
Hook type.
Definition: hook.c:64
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:735
A list of user hooks.
Definition: hook.c:62
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:841
#define MUTT_HOOK_NO_FLAGS
No flags are set.
Definition: hook.h:38
static void delete_hook(struct Hook *h)
Delete a Hook.
Definition: hook.c:309
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ idxfmt_hashelem_free()

static void idxfmt_hashelem_free ( int  type,
void *  obj,
intptr_t  data 
)
static

Delete an index-format-hook from the Hash Table - Implements hash_hdata_free_t.

Definition at line 346 of file hook.c.

347 {
348  struct HookList *hl = obj;
349  struct Hook *h = NULL;
350  struct Hook *tmp = NULL;
351 
352  TAILQ_FOREACH_SAFE(h, hl, entries, tmp)
353  {
354  TAILQ_REMOVE(hl, h, entries);
355  delete_hook(h);
356  }
357 
358  FREE(&hl);
359 }
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:735
A list of user hooks.
Definition: hook.c:62
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:841
#define FREE(x)
Definition: memory.h:40
static void delete_hook(struct Hook *h)
Delete a Hook.
Definition: hook.c:309
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ delete_idxfmt_hooks()

static void delete_idxfmt_hooks ( void  )
static

Delete all the index-format-hooks.

Definition at line 364 of file hook.c.

365 {
367 }
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:449
static struct HashTable * IdxFmtHooks
Definition: hook.c:74
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_folder_hook()

void mutt_folder_hook ( const char *  path,
const char *  desc 
)

Perform a folder hook.

Parameters
pathPath to potentially match
descDescription to potentially match

Definition at line 529 of file hook.c.

530 {
531  if (!path && !desc)
532  return;
533 
534  struct Hook *hook = NULL;
535  struct Buffer *err = mutt_buffer_pool_get();
536 
538 
539  TAILQ_FOREACH(hook, &Hooks, entries)
540  {
541  if (!hook->command)
542  continue;
543 
544  if (!(hook->type & MUTT_FOLDER_HOOK))
545  continue;
546 
547  const char *match = NULL;
548  if (mutt_regex_match(&hook->regex, path))
549  match = path;
550  else if (mutt_regex_match(&hook->regex, desc))
551  match = desc;
552 
553  if (match)
554  {
555  mutt_debug(LL_DEBUG1, "folder-hook '%s' matches '%s'\n", hook->regex.pattern, match);
556  mutt_debug(LL_DEBUG5, " %s\n", hook->command);
557  if (mutt_parse_rc_line(hook->command, err) == MUTT_CMD_ERROR)
558  {
559  mutt_error("%s", mutt_buffer_string(err));
560  break;
561  }
562  }
563  }
565 
567 }
static struct HookList Hooks
Definition: hook.c:72
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
Error: Can&#39;t help the user.
Definition: mutt_commands.h:36
#define mutt_error(...)
Definition: logging.h:88
HookFlags type
Hook type.
Definition: hook.c:64
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
static HookFlags current_hook_type
Definition: hook.c:75
String manipulation buffer.
Definition: buffer.h:33
A list of user hooks.
Definition: hook.c:62
enum CommandResult mutt_parse_rc_line(const char *line, struct Buffer *err)
Parse a line of user config.
Definition: init.c:1039
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
char * command
Filename, command or pattern to execute.
Definition: hook.c:66
struct Regex regex
Regular expression.
Definition: hook.c:65
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Log at debug level 1.
Definition: logging.h:40
bool mutt_regex_match(const struct Regex *regex, const char *str)
Shorthand to mutt_regex_capture()
Definition: regex.c:613
#define MUTT_HOOK_NO_FLAGS
No flags are set.
Definition: hook.h:38
char * pattern
printable version
Definition: regex3.h:91
Log at debug level 5.
Definition: logging.h:44
#define MUTT_FOLDER_HOOK
folder-hook: when entering a mailbox
Definition: hook.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_find_hook()

char* mutt_find_hook ( HookFlags  type,
const char *  pat 
)

Find a matching hook.

Parameters
typeHook type, see HookFlags
patPattern to match
Return values
ptrCommand string
Note
The returned string must not be freed.

Definition at line 577 of file hook.c.

578 {
579  struct Hook *tmp = NULL;
580 
581  TAILQ_FOREACH(tmp, &Hooks, entries)
582  {
583  if (tmp->type & type)
584  {
585  if (mutt_regex_match(&tmp->regex, pat))
586  return tmp->command;
587  }
588  }
589  return NULL;
590 }
static struct HookList Hooks
Definition: hook.c:72
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
HookFlags type
Hook type.
Definition: hook.c:64
A list of user hooks.
Definition: hook.c:62
char * command
Filename, command or pattern to execute.
Definition: hook.c:66
struct Regex regex
Regular expression.
Definition: hook.c:65
bool mutt_regex_match(const struct Regex *regex, const char *str)
Shorthand to mutt_regex_capture()
Definition: regex.c:613
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_message_hook()

void mutt_message_hook ( struct Mailbox m,
struct Email e,
HookFlags  type 
)

Perform a message hook.

Parameters
mMailbox Context
eEmail
typeHook type, see HookFlags

Definition at line 598 of file hook.c.

599 {
600  struct Hook *hook = NULL;
601  struct PatternCache cache = { 0 };
602  struct Buffer *err = mutt_buffer_pool_get();
603 
604  current_hook_type = type;
605 
606  TAILQ_FOREACH(hook, &Hooks, entries)
607  {
608  if (!hook->command)
609  continue;
610 
611  if (hook->type & type)
612  {
613  if ((mutt_pattern_exec(SLIST_FIRST(hook->pattern), 0, m, e, &cache) > 0) ^
614  hook->regex.pat_not)
615  {
616  if (mutt_parse_rc_line(hook->command, err) == MUTT_CMD_ERROR)
617  {
618  mutt_error("%s", mutt_buffer_string(err));
621 
622  return;
623  }
624  /* Executing arbitrary commands could affect the pattern results,
625  * so the cache has to be wiped */
626  memset(&cache, 0, sizeof(cache));
627  }
628  }
629  }
631 
633 }
static struct HookList Hooks
Definition: hook.c:72
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
Error: Can&#39;t help the user.
Definition: mutt_commands.h:36
#define mutt_error(...)
Definition: logging.h:88
HookFlags type
Hook type.
Definition: hook.c:64
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
static HookFlags current_hook_type
Definition: hook.c:75
String manipulation buffer.
Definition: buffer.h:33
bool pat_not
do not match
Definition: regex3.h:93
A list of user hooks.
Definition: hook.c:62
enum CommandResult mutt_parse_rc_line(const char *line, struct Buffer *err)
Parse a line of user config.
Definition: init.c:1039
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
char * command
Filename, command or pattern to execute.
Definition: hook.c:66
struct Regex regex
Regular expression.
Definition: hook.c:65
#define SLIST_FIRST(head)
Definition: queue.h:229
int mutt_pattern_exec(struct Pattern *pat, PatternExecFlags flags, struct Mailbox *m, struct Email *e, struct PatternCache *cache)
Match a pattern against an email header.
Definition: exec.c:1097
#define MUTT_HOOK_NO_FLAGS
No flags are set.
Definition: hook.h:38
Cache commonly-used patterns.
Definition: lib.h:105
struct PatternList * pattern
Used for fcc,save,send-hook.
Definition: hook.c:67
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addr_hook()

static int addr_hook ( char *  path,
size_t  pathlen,
HookFlags  type,
struct Mailbox m,
struct Email e 
)
static

Perform an address hook (get a path)

Parameters
pathBuffer for path
pathlenLength of buffer
typeHook type, see HookFlags
mMailbox
eEmail
Return values
0Success
-1Failure

Definition at line 645 of file hook.c.

647 {
648  struct Hook *hook = NULL;
649  struct PatternCache cache = { 0 };
650 
651  /* determine if a matching hook exists */
652  TAILQ_FOREACH(hook, &Hooks, entries)
653  {
654  if (!hook->command)
655  continue;
656 
657  if (hook->type & type)
658  {
659  if ((mutt_pattern_exec(SLIST_FIRST(hook->pattern), 0, m, e, &cache) > 0) ^
660  hook->regex.pat_not)
661  {
662  mutt_make_string(path, pathlen, 0, hook->command, m, -1, e, MUTT_FORMAT_PLAIN, NULL);
663  return 0;
664  }
665  }
666  }
667 
668  return -1;
669 }
static struct HookList Hooks
Definition: hook.c:72
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
HookFlags type
Hook type.
Definition: hook.c:64
bool pat_not
do not match
Definition: regex3.h:93
A list of user hooks.
Definition: hook.c:62
char * command
Filename, command or pattern to execute.
Definition: hook.c:66
void mutt_make_string(char *buf, size_t buflen, int cols, const char *s, struct Mailbox *m, int inpgr, struct Email *e, MuttFormatFlags flags, const char *progress)
Create formatted strings using mailbox expandos.
Definition: hdrline.c:1409
struct Regex regex
Regular expression.
Definition: hook.c:65
#define SLIST_FIRST(head)
Definition: queue.h:229
int mutt_pattern_exec(struct Pattern *pat, PatternExecFlags flags, struct Mailbox *m, struct Email *e, struct PatternCache *cache)
Match a pattern against an email header.
Definition: exec.c:1097
#define MUTT_FORMAT_PLAIN
Do not prepend DISP_TO, DISP_CC ...
Definition: format_flags.h:38
Cache commonly-used patterns.
Definition: lib.h:105
struct PatternList * pattern
Used for fcc,save,send-hook.
Definition: hook.c:67
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_default_save()

void mutt_default_save ( char *  path,
size_t  pathlen,
struct Email e 
)

Find the default save path for an email.

Parameters
pathBuffer for the path
pathlenLength of buffer
eEmail

Definition at line 677 of file hook.c.

678 {
679  *path = '\0';
680  if (addr_hook(path, pathlen, MUTT_SAVE_HOOK, ctx_mailbox(Context), e) == 0)
681  return;
682 
683  struct Envelope *env = e->env;
684  const struct Address *from = TAILQ_FIRST(&env->from);
685  const struct Address *reply_to = TAILQ_FIRST(&env->reply_to);
686  const struct Address *to = TAILQ_FIRST(&env->to);
687  const struct Address *cc = TAILQ_FIRST(&env->cc);
688  const struct Address *addr = NULL;
689  bool from_me = mutt_addr_is_user(from);
690 
691  if (!from_me && reply_to && reply_to->mailbox)
692  addr = reply_to;
693  else if (!from_me && from && from->mailbox)
694  addr = from;
695  else if (to && to->mailbox)
696  addr = to;
697  else if (cc && cc->mailbox)
698  addr = cc;
699  else
700  addr = NULL;
701  if (addr)
702  {
703  struct Buffer *tmp = mutt_buffer_pool_get();
704  mutt_safe_path(tmp, addr);
705  snprintf(path, pathlen, "=%s", mutt_buffer_string(tmp));
707  }
708 }
The "current" mailbox.
Definition: context.h:37
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
#define TAILQ_FIRST(head)
Definition: queue.h:723
struct Mailbox * ctx_mailbox(struct Context *ctx)
wrapper to get the mailbox in a Context, or NULL
Definition: context.c:444
struct AddressList reply_to
Email&#39;s &#39;reply-to&#39;.
Definition: envelope.h:62
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
String manipulation buffer.
Definition: buffer.h:33
An email address.
Definition: address.h:35
char * mailbox
Mailbox and host address.
Definition: address.h:38
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
struct AddressList from
Email&#39;s &#39;From&#39; list.
Definition: envelope.h:57
struct Envelope * env
Envelope information.
Definition: email.h:90
struct AddressList cc
Email&#39;s &#39;Cc&#39; list.
Definition: envelope.h:59
void mutt_safe_path(struct Buffer *dest, const struct Address *a)
Make a safe filename from an email address.
Definition: muttlib.c:761
#define MUTT_SAVE_HOOK
save-hook: set a default folder when saving an email
Definition: hook.h:43
bool mutt_addr_is_user(const struct Address *addr)
Does the address belong to the user.
Definition: alias.c:562
struct AddressList to
Email&#39;s &#39;To&#39; list.
Definition: envelope.h:58
static int addr_hook(char *path, size_t pathlen, HookFlags type, struct Mailbox *m, struct Email *e)
Perform an address hook (get a path)
Definition: hook.c:645
The header of an Email.
Definition: envelope.h:54
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_select_fcc()

void mutt_select_fcc ( struct Buffer path,
struct Email e 
)

Select the FCC path for an email.

Parameters
pathBuffer for the path
eEmail

Definition at line 715 of file hook.c.

716 {
718 
719  if (addr_hook(path->data, path->dsize, MUTT_FCC_HOOK, NULL, e) != 0)
720  {
721  const struct Address *to = TAILQ_FIRST(&e->env->to);
722  const struct Address *cc = TAILQ_FIRST(&e->env->cc);
723  const struct Address *bcc = TAILQ_FIRST(&e->env->bcc);
724  const bool c_save_name = cs_subset_bool(NeoMutt->sub, "save_name");
725  const bool c_force_name = cs_subset_bool(NeoMutt->sub, "force_name");
726  const char *const c_record = cs_subset_string(NeoMutt->sub, "record");
727  if ((c_save_name || c_force_name) && (to || cc || bcc))
728  {
729  const struct Address *addr = to ? to : (cc ? cc : bcc);
730  struct Buffer *buf = mutt_buffer_pool_get();
731  mutt_safe_path(buf, addr);
732  const char *const c_folder = cs_subset_string(NeoMutt->sub, "folder");
733  mutt_buffer_concat_path(path, NONULL(c_folder), mutt_buffer_string(buf));
735  if (!c_force_name && (mx_access(mutt_buffer_string(path), W_OK) != 0))
736  mutt_buffer_strcpy(path, c_record);
737  }
738  else
739  mutt_buffer_strcpy(path, c_record);
740  }
741  else
742  mutt_buffer_fix_dptr(path);
743 
745 }
#define NONULL(x)
Definition: string2.h:37
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
#define TAILQ_FIRST(head)
Definition: queue.h:723
struct AddressList bcc
Email&#39;s &#39;Bcc&#39; list.
Definition: envelope.h:60
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
String manipulation buffer.
Definition: buffer.h:33
An email address.
Definition: address.h:35
Container for Accounts, Notifications.
Definition: neomutt.h:36
void mutt_buffer_pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using &#39;~&#39; or &#39;=&#39;.
Definition: muttlib.c:603
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
size_t dsize
Length of data.
Definition: buffer.h:37
struct Envelope * env
Envelope information.
Definition: email.h:90
struct AddressList cc
Email&#39;s &#39;Cc&#39; list.
Definition: envelope.h:59
void mutt_buffer_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition: buffer.c:181
void mutt_safe_path(struct Buffer *dest, const struct Address *a)
Make a safe filename from an email address.
Definition: muttlib.c:761
#define PATH_MAX
Definition: mutt.h:40
char * data
Pointer to data.
Definition: buffer.h:35
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:312
size_t mutt_buffer_concat_path(struct Buffer *buf, const char *dir, const char *fname)
Join a directory name and a filename.
Definition: buffer.c:374
int mx_access(const char *path, int flags)
Wrapper for access, checks permissions on a given mailbox.
Definition: mx.c:184
struct AddressList to
Email&#39;s &#39;To&#39; list.
Definition: envelope.h:58
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
static int addr_hook(char *path, size_t pathlen, HookFlags type, struct Mailbox *m, struct Email *e)
Perform an address hook (get a path)
Definition: hook.c:645
#define MUTT_FCC_HOOK
fcc-hook: to save outgoing email
Definition: hook.h:42
void mutt_buffer_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition: buffer.c:265
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ list_hook()

static void list_hook ( struct ListHead *  matches,
const char *  match,
HookFlags  hook 
)
static

Find hook strings matching.

Parameters
[out]matchesList of hook strings
[in]matchString to match
[in]hookHook type, see HookFlags

Definition at line 753 of file hook.c.

754 {
755  struct Hook *tmp = NULL;
756 
757  TAILQ_FOREACH(tmp, &Hooks, entries)
758  {
759  if ((tmp->type & hook) && mutt_regex_match(&tmp->regex, match))
760  {
762  }
763  }
764 }
static struct HookList Hooks
Definition: hook.c:72
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
HookFlags type
Hook type.
Definition: hook.c:64
A list of user hooks.
Definition: hook.c:62
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
char * command
Filename, command or pattern to execute.
Definition: hook.c:66
struct Regex regex
Regular expression.
Definition: hook.c:65
struct ListNode * mutt_list_insert_tail(struct ListHead *h, char *s)
Append a string to the end of a List.
Definition: list.c:64
bool mutt_regex_match(const struct Regex *regex, const char *str)
Shorthand to mutt_regex_capture()
Definition: regex.c:613
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_crypt_hook()

void mutt_crypt_hook ( struct ListHead *  list,
struct Address addr 
)

Find crypto hooks for an Address.

Parameters
[out]listList of keys
[in]addrAddress to match

The crypt-hook associates keys with addresses.

Definition at line 773 of file hook.c.

774 {
775  list_hook(list, addr->mailbox, MUTT_CRYPT_HOOK);
776 }
char * mailbox
Mailbox and host address.
Definition: address.h:38
static void list_hook(struct ListHead *matches, const char *match, HookFlags hook)
Find hook strings matching.
Definition: hook.c:753
#define MUTT_CRYPT_HOOK
crypt-hook: automatically select a PGP/SMIME key
Definition: hook.h:47
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_account_hook()

void mutt_account_hook ( const char *  url)

Perform an account hook.

Parameters
urlAccount URL to match

Definition at line 782 of file hook.c.

783 {
784  /* parsing commands with URLs in an account hook can cause a recursive
785  * call. We just skip processing if this occurs. Typically such commands
786  * belong in a folder-hook -- perhaps we should warn the user. */
787  static bool inhook = false;
788  if (inhook)
789  return;
790 
791  struct Hook *hook = NULL;
792  struct Buffer *err = mutt_buffer_pool_get();
793 
794  TAILQ_FOREACH(hook, &Hooks, entries)
795  {
796  if (!(hook->command && (hook->type & MUTT_ACCOUNT_HOOK)))
797  continue;
798 
799  if (mutt_regex_match(&hook->regex, url))
800  {
801  inhook = true;
802  mutt_debug(LL_DEBUG1, "account-hook '%s' matches '%s'\n", hook->regex.pattern, url);
803  mutt_debug(LL_DEBUG5, " %s\n", hook->command);
804 
805  if (mutt_parse_rc_line(hook->command, err) == MUTT_CMD_ERROR)
806  {
807  mutt_error("%s", mutt_buffer_string(err));
809 
810  inhook = false;
811  goto done;
812  }
813 
814  inhook = false;
815  }
816  }
817 done:
819 }
static struct HookList Hooks
Definition: hook.c:72
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
Error: Can&#39;t help the user.
Definition: mutt_commands.h:36
#define mutt_error(...)
Definition: logging.h:88
HookFlags type
Hook type.
Definition: hook.c:64
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
String manipulation buffer.
Definition: buffer.h:33
A list of user hooks.
Definition: hook.c:62
enum CommandResult mutt_parse_rc_line(const char *line, struct Buffer *err)
Parse a line of user config.
Definition: init.c:1039
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
char * command
Filename, command or pattern to execute.
Definition: hook.c:66
struct Regex regex
Regular expression.
Definition: hook.c:65
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Log at debug level 1.
Definition: logging.h:40
bool mutt_regex_match(const struct Regex *regex, const char *str)
Shorthand to mutt_regex_capture()
Definition: regex.c:613
char * pattern
printable version
Definition: regex3.h:91
Log at debug level 5.
Definition: logging.h:44
#define MUTT_ACCOUNT_HOOK
account-hook: when changing between accounts
Definition: hook.h:48
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_timeout_hook()

void mutt_timeout_hook ( void  )

Execute any timeout hooks.

The user can configure hooks to be run on timeout. This function finds all the matching hooks and executes them.

Definition at line 827 of file hook.c.

828 {
829  struct Hook *hook = NULL;
830  struct Buffer err;
831  char buf[256];
832 
833  mutt_buffer_init(&err);
834  err.data = buf;
835  err.dsize = sizeof(buf);
836 
837  TAILQ_FOREACH(hook, &Hooks, entries)
838  {
839  if (!(hook->command && (hook->type & MUTT_TIMEOUT_HOOK)))
840  continue;
841 
842  if (mutt_parse_rc_line(hook->command, &err) == MUTT_CMD_ERROR)
843  {
844  mutt_error("%s", err.data);
845  mutt_buffer_reset(&err);
846 
847  /* The hooks should be independent of each other, so even though this on
848  * failed, we'll carry on with the others. */
849  }
850  }
851 
852  /* Delete temporary attachment files */
854 }
static struct HookList Hooks
Definition: hook.c:72
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
Error: Can&#39;t help the user.
Definition: mutt_commands.h:36
#define mutt_error(...)
Definition: logging.h:88
HookFlags type
Hook type.
Definition: hook.c:64
String manipulation buffer.
Definition: buffer.h:33
A list of user hooks.
Definition: hook.c:62
enum CommandResult mutt_parse_rc_line(const char *line, struct Buffer *err)
Parse a line of user config.
Definition: init.c:1039
void mutt_unlink_temp_attachments(void)
Delete all temporary attachments.
Definition: mutt_attach.c:1275
char * command
Filename, command or pattern to execute.
Definition: hook.c:66
#define MUTT_TIMEOUT_HOOK
timeout-hook: run a command periodically
Definition: hook.h:57
struct Buffer * mutt_buffer_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:46
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_startup_shutdown_hook()

void mutt_startup_shutdown_hook ( HookFlags  type)

Execute any startup/shutdown hooks.

Parameters
typeHook type: MUTT_STARTUP_HOOK or MUTT_SHUTDOWN_HOOK

The user can configure hooks to be run on startup/shutdown. This function finds all the matching hooks and executes them.

Definition at line 863 of file hook.c.

864 {
865  struct Hook *hook = NULL;
866  struct Buffer err = mutt_buffer_make(0);
867  char buf[256];
868 
869  err.data = buf;
870  err.dsize = sizeof(buf);
871 
872  TAILQ_FOREACH(hook, &Hooks, entries)
873  {
874  if (!(hook->command && (hook->type & type)))
875  continue;
876 
877  if (mutt_parse_rc_line(hook->command, &err) == MUTT_CMD_ERROR)
878  {
879  mutt_error("%s", err.data);
880  mutt_buffer_reset(&err);
881  }
882  }
883 }
static struct HookList Hooks
Definition: hook.c:72
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
Error: Can&#39;t help the user.
Definition: mutt_commands.h:36
#define mutt_error(...)
Definition: logging.h:88
HookFlags type
Hook type.
Definition: hook.c:64
struct Buffer mutt_buffer_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:61
String manipulation buffer.
Definition: buffer.h:33
A list of user hooks.
Definition: hook.c:62
enum CommandResult mutt_parse_rc_line(const char *line, struct Buffer *err)
Parse a line of user config.
Definition: init.c:1039
char * command
Filename, command or pattern to execute.
Definition: hook.c:66
size_t dsize
Length of data.
Definition: buffer.h:37
char * data
Pointer to data.
Definition: buffer.h:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_idxfmt_hook()

const char* mutt_idxfmt_hook ( const char *  name,
struct Mailbox m,
struct Email e 
)

Get index-format-hook format string.

Parameters
nameHook name
mMailbox
eEmail
Return values
ptrprintf(3)-like format string
NULLNo matching hook

Definition at line 893 of file hook.c.

894 {
895  if (!IdxFmtHooks)
896  return NULL;
897 
898  struct HookList *hl = mutt_hash_find(IdxFmtHooks, name);
899  if (!hl)
900  return NULL;
901 
903 
904  struct PatternCache cache = { 0 };
905  const char *fmtstring = NULL;
906  struct Hook *hook = NULL;
907 
908  TAILQ_FOREACH(hook, hl, entries)
909  {
910  struct Pattern *pat = SLIST_FIRST(hook->pattern);
911  if ((mutt_pattern_exec(pat, 0, m, e, &cache) > 0) ^ hook->regex.pat_not)
912  {
913  fmtstring = hook->command;
914  break;
915  }
916  }
917 
919 
920  return fmtstring;
921 }
void * mutt_hash_find(const struct HashTable *table, const char *strkey)
Find the HashElem data in a Hash Table element using a key.
Definition: hash.c:354
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
static HookFlags current_hook_type
Definition: hook.c:75
bool pat_not
do not match
Definition: regex3.h:93
A list of user hooks.
Definition: hook.c:62
#define MUTT_IDXFMTHOOK
index-format-hook: customise the format of the index
Definition: hook.h:56
A simple (non-regex) pattern.
Definition: lib.h:68
char * command
Filename, command or pattern to execute.
Definition: hook.c:66
struct Regex regex
Regular expression.
Definition: hook.c:65
#define SLIST_FIRST(head)
Definition: queue.h:229
int mutt_pattern_exec(struct Pattern *pat, PatternExecFlags flags, struct Mailbox *m, struct Email *e, struct PatternCache *cache)
Match a pattern against an email header.
Definition: exec.c:1097
static struct HashTable * IdxFmtHooks
Definition: hook.c:74
#define MUTT_HOOK_NO_FLAGS
No flags are set.
Definition: hook.h:38
Cache commonly-used patterns.
Definition: lib.h:105
struct PatternList * pattern
Used for fcc,save,send-hook.
Definition: hook.c:67
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ Hooks

struct HookList Hooks = TAILQ_HEAD_INITIALIZER(Hooks)
static

Definition at line 72 of file hook.c.

◆ IdxFmtHooks

struct HashTable* IdxFmtHooks = NULL
static

Definition at line 74 of file hook.c.

◆ current_hook_type

HookFlags current_hook_type = MUTT_HOOK_NO_FLAGS
static

Definition at line 75 of file hook.c.