NeoMutt  2022-04-29-249-gaae397
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
40static struct Notify *SubjRxNotify = NULL;
41
45void subjrx_free(void)
46{
49}
50
54void subjrx_init(void)
55{
56 if (SubjRxNotify)
57 return;
58
61}
62
66static 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
92static 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
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
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
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
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:67
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:168
#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:273
Config/command parsing.
@ LL_NOTIFY
Log of notifications.
Definition: logging.h:45
#define FREE(x)
Definition: memory.h:43
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:583
char * mutt_replacelist_apply(struct ReplaceList *rl, char *buf, size_t buflen, const char *str)
Apply replacements to a buffer.
Definition: regex.c:368
void mutt_replacelist_free(struct ReplaceList *rl)
Free a ReplaceList object.
Definition: regex.c:467
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:266
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
Many unsorted constants and some structs.
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:67
@ NT_SUBJRX
Subject Regex has changed, NotifySubjRx.
Definition: notify_type.h:54
#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:57
char * subject
Email's subject.
Definition: envelope.h:70
char * disp_subj
Display subject (modified copy of subject)
Definition: envelope.h:72
A mailbox.
Definition: mailbox.h:79
int msg_count
Total number of messages.
Definition: mailbox.h:88
struct Email ** emails
Array of Emails.
Definition: mailbox.h:96
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:43
@ NT_SUBJRX_ADD
Subject Regex has been added.
Definition: subjectrx.h:42