NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
subjectrx.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stddef.h>
31 #include <stdint.h>
32 #include "mutt/lib.h"
33 #include "email/lib.h"
34 #include "core/lib.h"
35 #include "mutt.h"
36 #include "subjectrx.h"
37 #include "init.h"
38 #include "mutt_commands.h"
39 
41 static struct Notify *SubjRxNotify = NULL;
42 
46 void subjrx_free(void)
47 {
48  notify_free(&SubjRxNotify);
50 }
51 
55 void subjrx_init(void)
56 {
57  if (SubjRxNotify)
58  return;
59 
60  SubjRxNotify = notify_new();
61  notify_set_parent(SubjRxNotify, NeoMutt->notify);
62 }
63 
67 static enum CommandResult parse_unreplace_list(struct Buffer *buf, struct Buffer *s,
68  struct ReplaceList *list, struct Buffer *err)
69 {
70  /* First token is a regex. */
71  if (!MoreArgs(s))
72  {
73  mutt_buffer_printf(err, _("%s: too few arguments"), "unsubjectrx");
74  return MUTT_CMD_WARNING;
75  }
76 
78 
79  /* "*" is a special case. */
80  if (mutt_str_equal(buf->data, "*"))
81  {
83  return MUTT_CMD_SUCCESS;
84  }
85 
86  mutt_replacelist_remove(list, buf->data);
87  return MUTT_CMD_SUCCESS;
88 }
89 
93 static enum CommandResult parse_replace_list(struct Buffer *buf, struct Buffer *s,
94  struct ReplaceList *list, struct Buffer *err)
95 {
96  struct Buffer templ = mutt_buffer_make(0);
97 
98  /* First token is a regex. */
99  if (!MoreArgs(s))
100  {
101  mutt_buffer_printf(err, _("%s: too few arguments"), "subjectrx");
102  return MUTT_CMD_WARNING;
103  }
105 
106  /* Second token is a replacement template */
107  if (!MoreArgs(s))
108  {
109  mutt_buffer_printf(err, _("%s: too few arguments"), "subjectrx");
110  return MUTT_CMD_WARNING;
111  }
113 
114  if (mutt_replacelist_add(list, buf->data, templ.data, err) != 0)
115  {
116  FREE(&templ.data);
117  return MUTT_CMD_ERROR;
118  }
119  FREE(&templ.data);
120 
121  return MUTT_CMD_SUCCESS;
122 }
123 
129 bool subjrx_apply_mods(struct Envelope *env)
130 {
131  if (!env || !env->subject || (*env->subject == '\0'))
132  return false;
133 
134  if (env->disp_subj)
135  return true;
136 
138  return false;
139 
141  return true;
142 }
143 
147 void subjrx_clear_mods(struct Mailbox *m)
148 {
149  if (!m)
150  return;
151 
152  for (int i = 0; i < m->msg_count; i++)
153  {
154  struct Email *e = m->emails[i];
155  if (!e || !e->env)
156  continue;
157  FREE(&e->env->disp_subj);
158  }
159 }
160 
164 enum CommandResult parse_subjectrx_list(struct Buffer *buf, struct Buffer *s,
165  intptr_t data, struct Buffer *err)
166 {
167  enum CommandResult rc;
168 
169  rc = parse_replace_list(buf, s, &SubjectRegexList, err);
170  if (rc == MUTT_CMD_SUCCESS)
171  {
172  mutt_debug(LL_NOTIFY, "NT_SUBJRX_ADD: %s\n", buf->data);
173  notify_send(SubjRxNotify, NT_SUBJRX, NT_SUBJRX_ADD, NULL);
174  }
175  return rc;
176 }
177 
181 enum CommandResult parse_unsubjectrx_list(struct Buffer *buf, struct Buffer *s,
182  intptr_t data, struct Buffer *err)
183 {
184  enum CommandResult rc;
185 
186  rc = parse_unreplace_list(buf, s, &SubjectRegexList, err);
187  if (rc == MUTT_CMD_SUCCESS)
188  {
189  mutt_debug(LL_NOTIFY, "NT_SUBJRX_DELETE: %s\n", buf->data);
190  notify_send(SubjRxNotify, NT_SUBJRX, NT_SUBJRX_DELETE, NULL);
191  }
192  return rc;
193 }
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
void subjrx_clear_mods(struct Mailbox *m)
Clear out all modified email subjects.
Definition: subjectrx.c:147
static struct ReplaceList SubjectRegexList
Definition: subjectrx.c:40
int msg_count
Total number of messages.
Definition: mailbox.h:91
int mutt_replacelist_add(struct ReplaceList *rl, const char *pat, const char *templ, struct Buffer *err)
Add a pattern and a template to a list.
Definition: regex.c:263
The envelope/body of an email.
Definition: email.h:37
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.
char * disp_subj
Display subject (modified copy of subject)
Definition: envelope.h:68
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:73
enum CommandResult parse_subjectrx_list(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the &#39;subjectrx&#39; command - Implements Command::parse() -.
Definition: subjectrx.c:164
bool subjrx_apply_mods(struct Envelope *env)
Apply regex modifications to the subject.
Definition: subjectrx.c:129
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
Subject Regex has changed, NotifySubjRx.
Definition: notify_type.h:52
#define _(a)
Definition: message.h:28
static enum CommandResult parse_unreplace_list(struct Buffer *buf, struct Buffer *s, struct ReplaceList *list, struct Buffer *err)
Remove a string replacement rule - Implements Command::parse() -.
Definition: subjectrx.c:67
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
void mutt_replacelist_free(struct ReplaceList *rl)
Free a ReplaceList object.
Definition: regex.c:448
Container for Accounts, Notifications.
Definition: neomutt.h:36
Subject Regex has been deleted.
Definition: subjectrx.h:42
#define MoreArgs(buf)
Definition: buffer.h:40
Many unsorted constants and some structs.
struct Envelope * env
Envelope information.
Definition: email.h:90
Convenience wrapper for the core headers.
static enum CommandResult parse_replace_list(struct Buffer *buf, struct Buffer *s, struct ReplaceList *list, struct Buffer *err)
Parse a string replacement rule - Implements Command::parse() -.
Definition: subjectrx.c:93
Subject Regex handling.
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:60
Subject Regex has been added.
Definition: subjectrx.h:41
A mailbox.
Definition: mailbox.h:81
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
void subjrx_init(void)
Create new Subject Regex List.
Definition: subjectrx.c:55
Definitions of NeoMutt commands.
Notification API.
Definition: notify.c:50
char * mutt_replacelist_apply(struct ReplaceList *rl, char *buf, size_t buflen, const char *str)
Apply replacements to a buffer.
Definition: regex.c:353
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
enum CommandResult parse_unsubjectrx_list(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the &#39;unsubjectrx&#39; command - Implements Command::parse() -.
Definition: subjectrx.c:181
Success: Command worked.
Definition: mutt_commands.h:38
char * subject
Email&#39;s subject.
Definition: envelope.h:66
Warning: Help given to the user.
Definition: mutt_commands.h:37
int mutt_replacelist_remove(struct ReplaceList *rl, const char *pat)
Remove a pattern from a list.
Definition: regex.c:565
#define FREE(x)
Definition: memory.h:40
#define STAILQ_EMPTY(head)
Definition: queue.h:348
void subjrx_free(void)
Free the Subject Regex List.
Definition: subjectrx.c:46
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
void notify_set_parent(struct Notify *notify, struct Notify *parent)
Set the parent notification handler.
Definition: notify.c:93
Convenience wrapper for the library headers.
The header of an Email.
Definition: envelope.h:54
#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