NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
alternates.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stdbool.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include "mutt/lib.h"
34 #include "address/lib.h"
35 #include "email/lib.h"
36 #include "core/lib.h"
37 #include "mutt.h"
38 #include "alternates.h"
39 #include "command_parse.h"
40 #include "init.h"
41 #include "mutt_commands.h"
42 
45 static struct Notify *AlternatesNotify = NULL;
46 
50 void alternates_free(void)
51 {
52  notify_free(&AlternatesNotify);
53 
56 }
57 
61 void alternates_init(void)
62 {
63  if (AlternatesNotify)
64  return;
65 
66  AlternatesNotify = notify_new();
67  notify_set_parent(AlternatesNotify, NeoMutt->notify);
68 }
69 
74 {
75  if (!m)
76  return;
77 
78  for (int i = 0; i < m->msg_count; i++)
79  {
80  struct Email *e = m->emails[i];
81  if (!e)
82  break;
83  e->recip_valid = false;
84  }
85 }
86 
90 enum CommandResult parse_alternates(struct Buffer *buf, struct Buffer *s,
91  intptr_t data, struct Buffer *err)
92 {
93  struct GroupList gl = STAILQ_HEAD_INITIALIZER(gl);
94 
95  do
96  {
98 
99  if (parse_grouplist(&gl, buf, s, err) == -1)
100  goto bail;
101 
103 
104  if (mutt_regexlist_add(&Alternates, buf->data, REG_ICASE, err) != 0)
105  goto bail;
106 
107  if (mutt_grouplist_add_regex(&gl, buf->data, REG_ICASE, err) != 0)
108  goto bail;
109  } while (MoreArgs(s));
110 
112 
113  mutt_debug(LL_NOTIFY, "NT_ALTERN_ADD: %s\n", buf->data);
114  notify_send(AlternatesNotify, NT_ALTERN, NT_ALTERN_ADD, NULL);
115 
116  return MUTT_CMD_SUCCESS;
117 
118 bail:
120  return MUTT_CMD_ERROR;
121 }
122 
126 enum CommandResult parse_unalternates(struct Buffer *buf, struct Buffer *s,
127  intptr_t data, struct Buffer *err)
128 {
129  do
130  {
133 
134  if (!mutt_str_equal(buf->data, "*") &&
135  (mutt_regexlist_add(&UnAlternates, buf->data, REG_ICASE, err) != 0))
136  {
137  return MUTT_CMD_ERROR;
138  }
139 
140  } while (MoreArgs(s));
141 
142  mutt_debug(LL_NOTIFY, "NT_ALTERN_DELETE: %s\n", buf->data);
143  notify_send(AlternatesNotify, NT_ALTERN, NT_ALTERN_DELETE, NULL);
144 
145  return MUTT_CMD_SUCCESS;
146 }
147 
153 bool mutt_alternates_match(const char *addr)
154 {
155  if (!addr)
156  return false;
157 
158  if (mutt_regexlist_match(&Alternates, addr))
159  {
160  mutt_debug(LL_DEBUG5, "yes, %s matched by alternates\n", addr);
162  mutt_debug(LL_DEBUG5, "but, %s matched by unalternates\n", addr);
163  else
164  return true;
165  }
166 
167  return false;
168 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:904
struct Email ** emails
Array of Emails.
Definition: mailbox.h:99
int parse_grouplist(struct GroupList *gl, struct Buffer *buf, struct Buffer *s, struct Buffer *err)
Parse a group context.
struct RegexList UnAlternates
List of regexes to blacklist false matches in Alternates.
Definition: alternates.c:44
int msg_count
Total number of messages.
Definition: mailbox.h:91
The envelope/body of an email.
Definition: email.h:37
int mutt_regexlist_remove(struct RegexList *rl, const char *str)
Remove a Regex from a list.
Definition: regex.c:227
Alternate address has been added.
Definition: alternates.h:40
CommandResult
Error codes for command_t parse functions.
Definition: mutt_commands.h:34
Config/command parsing.
Error: Can&#39;t help the user.
Definition: mutt_commands.h:36
Log of notifications.
Definition: logging.h:45
Structs that make up an email.
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:73
int mutt_regexlist_add(struct RegexList *rl, const char *str, uint16_t flags, struct Buffer *err)
Compile a regex string and add it to a list.
Definition: regex.c:132
bool mutt_alternates_match(const char *addr)
Compare an Address to the Un/Alternates lists.
Definition: alternates.c:153
String manipulation buffer.
Definition: buffer.h:33
Alternates command changed, NotifyAlternates.
Definition: notify_type.h:36
Alternate address handling.
Alternate address has been deleted.
Definition: alternates.h:41
Container for Accounts, Notifications.
Definition: neomutt.h:36
Email Address Handling.
#define MoreArgs(buf)
Definition: buffer.h:40
Many unsorted constants and some structs.
Convenience wrapper for the core headers.
int mutt_grouplist_add_regex(struct GroupList *gl, const char *s, uint16_t flags, struct Buffer *err)
Add matching Addresses to a GroupList.
Definition: group.c:276
bool mutt_regexlist_match(struct RegexList *rl, const char *str)
Does a string match any Regex in the list?
Definition: regex.c:192
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:60
A mailbox.
Definition: mailbox.h:81
Functions to parse commands in a config file.
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:395
char * data
Pointer to data.
Definition: buffer.h:35
Definitions of NeoMutt commands.
struct RegexList Alternates
List of regexes to match the user&#39;s alternate email addresses.
Definition: alternates.c:43
void mutt_alternates_reset(struct Mailbox *m)
Clear the recipient valid flag of all emails.
Definition: alternates.c:73
Notification API.
Definition: notify.c:50
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Success: Command worked.
Definition: mutt_commands.h:38
void alternates_init(void)
Set up the alternates lists.
Definition: alternates.c:61
enum CommandResult parse_unalternates(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the &#39;unalternates&#39; command - Implements Command::parse() -.
Definition: alternates.c:126
void mutt_grouplist_destroy(struct GroupList *gl)
Free a GroupList.
Definition: group.c:157
enum CommandResult parse_alternates(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the &#39;alternates&#39; command - Implements Command::parse() -.
Definition: alternates.c:90
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
bool recip_valid
Is_recipient is valid.
Definition: email.h:58
void notify_set_parent(struct Notify *notify, struct Notify *parent)
Set the parent notification handler.
Definition: notify.c:93
void alternates_free(void)
Free the alternates lists.
Definition: alternates.c:50
Log at debug level 5.
Definition: logging.h:44
Convenience wrapper for the library headers.
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition: regex.c:171
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:66
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:171