NeoMutt  2021-10-29-225-gb9986f
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 
39 static struct ReplaceList SubjectRegexList = STAILQ_HEAD_INITIALIZER(SubjectRegexList);
40 static struct Notify *SubjRxNotify = NULL;
41 
45 void subjrx_free(void)
46 {
49 }
50 
54 void subjrx_init(void)
55 {
56  if (SubjRxNotify)
57  return;
58 
61 }
62 
66 static enum CommandResult parse_unreplace_list(struct Buffer *buf, struct Buffer *s,
67  struct ReplaceList *list, struct Buffer *err)
68 {
69  /* First token is a regex. */
70  if (!MoreArgs(s))
71  {
72  mutt_buffer_printf(err, _("%s: too few arguments"), "unsubjectrx");
73  return MUTT_CMD_WARNING;
74  }
75 
77 
78  /* "*" is a special case. */
79  if (mutt_str_equal(buf->data, "*"))
80  {
82  return MUTT_CMD_SUCCESS;
83  }
84 
85  mutt_replacelist_remove(list, buf->data);
86  return MUTT_CMD_SUCCESS;
87 }
88 
92 static enum CommandResult parse_replace_list(struct Buffer *buf, struct Buffer *s,
93  struct ReplaceList *list, struct Buffer *err)
94 {
95  struct Buffer templ = mutt_buffer_make(0);
96 
97  /* First token is a regex. */
98  if (!MoreArgs(s))
99  {
100  mutt_buffer_printf(err, _("%s: too few arguments"), "subjectrx");
101  return MUTT_CMD_WARNING;
102  }
104 
105  /* Second token is a replacement template */
106  if (!MoreArgs(s))
107  {
108  mutt_buffer_printf(err, _("%s: too few arguments"), "subjectrx");
109  return MUTT_CMD_WARNING;
110  }
112 
113  if (mutt_replacelist_add(list, buf->data, templ.data, err) != 0)
114  {
115  FREE(&templ.data);
116  return MUTT_CMD_ERROR;
117  }
118  FREE(&templ.data);
119 
120  return MUTT_CMD_SUCCESS;
121 }
122 
128 bool subjrx_apply_mods(struct Envelope *env)
129 {
130  if (!env || !env->subject || (*env->subject == '\0'))
131  return false;
132 
133  if (env->disp_subj)
134  return true;
135 
137  return false;
138 
140  return true;
141 }
142 
146 void subjrx_clear_mods(struct Mailbox *m)
147 {
148  if (!m)
149  return;
150 
151  for (int i = 0; i < m->msg_count; i++)
152  {
153  struct Email *e = m->emails[i];
154  if (!e || !e->env)
155  continue;
156  FREE(&e->env->disp_subj);
157  }
158 }
159 
163 enum CommandResult parse_subjectrx_list(struct Buffer *buf, struct Buffer *s,
164  intptr_t data, struct Buffer *err)
165 {
166  enum CommandResult rc;
167 
168  rc = parse_replace_list(buf, s, &SubjectRegexList, err);
169  if (rc == MUTT_CMD_SUCCESS)
170  {
171  mutt_debug(LL_NOTIFY, "NT_SUBJRX_ADD: %s\n", buf->data);
173  }
174  return rc;
175 }
176 
180 enum CommandResult parse_unsubjectrx_list(struct Buffer *buf, struct Buffer *s,
181  intptr_t data, struct Buffer *err)
182 {
183  enum CommandResult rc;
184 
185  rc = parse_unreplace_list(buf, s, &SubjectRegexList, err);
186  if (rc == MUTT_CMD_SUCCESS)
187  {
188  mutt_debug(LL_NOTIFY, "NT_SUBJRX_DELETE: %s\n", buf->data);
190  }
191  return rc;
192 }
struct Buffer mutt_buffer_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:61
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
#define MoreArgs(buf)
Definition: buffer.h:40
CommandResult
Error codes for command_t parse functions.
Definition: command.h:34
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:37
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:35
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition: command.h:36
Convenience wrapper for the core headers.
Structs that make up an email.
enum CommandResult parse_unsubjectrx_list(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'unsubjectrx' command - Implements Command::parse() -.
Definition: subjectrx.c:180
enum CommandResult parse_subjectrx_list(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'subjectrx' command - Implements Command::parse() -.
Definition: subjectrx.c:163
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:92
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:66
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:399
Config/command parsing.
@ LL_NOTIFY
Log of notifications.
Definition: logging.h:45
#define FREE(x)
Definition: memory.h:40
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:60
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
void notify_set_parent(struct Notify *notify, struct Notify *parent)
Set the parent notification handler.
Definition: notify.c:93
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:73
int mutt_replacelist_remove(struct ReplaceList *rl, const char *pat)
Remove a pattern from a list.
Definition: regex.c:581
void mutt_replacelist_free(struct ReplaceList *rl)
Free a ReplaceList object.
Definition: regex.c:465
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:265
char * mutt_replacelist_apply(struct ReplaceList *rl, char *buf, size_t buflen, const char *str)
Apply replacements to a buffer.
Definition: regex.c:367
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:715
Many unsorted constants and some structs.
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:66
@ NT_SUBJRX
Subject Regex has changed, NotifySubjRx.
Definition: notify_type.h:52
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define STAILQ_EMPTY(head)
Definition: queue.h:348
String manipulation buffer.
Definition: buffer.h:34
char * data
Pointer to data.
Definition: buffer.h:35
The envelope/body of an email.
Definition: email.h:37
struct Envelope * env
Envelope information.
Definition: email.h:66
The header of an Email.
Definition: envelope.h:55
char * subject
Email's subject.
Definition: envelope.h:68
char * disp_subj
Display subject (modified copy of subject)
Definition: envelope.h:70
A mailbox.
Definition: mailbox.h:82
int msg_count
Total number of messages.
Definition: mailbox.h:91
struct Email ** emails
Array of Emails.
Definition: mailbox.h:99
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
Notification API.
Definition: notify.c:51
void subjrx_init(void)
Create new Subject Regex List.
Definition: subjectrx.c:54
static struct Notify * SubjRxNotify
Definition: subjectrx.c:40
void subjrx_clear_mods(struct Mailbox *m)
Clear out all modified email subjects.
Definition: subjectrx.c:146
bool subjrx_apply_mods(struct Envelope *env)
Apply regex modifications to the subject.
Definition: subjectrx.c:128
static struct ReplaceList SubjectRegexList
Definition: subjectrx.c:39
void subjrx_free(void)
Free the Subject Regex List.
Definition: subjectrx.c:45
Subject Regex handling.
@ NT_SUBJRX_DELETE
Subject Regex has been deleted.
Definition: subjectrx.h:41
@ NT_SUBJRX_ADD
Subject Regex has been added.
Definition: subjectrx.h:40