NeoMutt  2025-12-11-58-g09398d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
subjectrx.c
Go to the documentation of this file.
1
22
28
29#include "config.h"
30#include <stddef.h>
31#include "mutt/lib.h"
32#include "email/lib.h"
33#include "core/lib.h"
34#include "subjectrx.h"
35#include "parse/lib.h"
36#include "mview.h"
37
40static struct Notify *SubjRxNotify = NULL;
41
50
54void subjrx_init(void)
55{
56 if (SubjRxNotify)
57 return;
58
61}
62
71static enum CommandResult parse_unreplace_list(const struct Command *cmd,
72 struct Buffer *line,
73 struct ReplaceList *list, struct Buffer *err)
74{
75 if (!MoreArgs(line))
76 {
77 buf_printf(err, _("%s: too few arguments"), cmd->name);
78 return MUTT_CMD_WARNING;
79 }
80
81 struct Buffer *token = buf_pool_get();
82
83 /* First token is a regex. */
85
86 /* "*" is a special case. */
87 if (mutt_str_equal(buf_string(token), "*"))
88 {
90 buf_pool_release(&token);
91 return MUTT_CMD_SUCCESS;
92 }
93
95 buf_pool_release(&token);
96 return MUTT_CMD_SUCCESS;
97}
98
107static enum CommandResult parse_replace_list(const struct Command *cmd, struct Buffer *line,
108 struct ReplaceList *list, struct Buffer *err)
109{
110 struct Buffer *templ = buf_pool_get();
111 struct Buffer *regex = buf_pool_get();
112 int rc = MUTT_CMD_WARNING;
113
114 if (!MoreArgs(line))
115 {
116 buf_printf(err, _("%s: too few arguments"), cmd->name);
117 goto done;
118 }
119
120 /* First token is a regex. */
122
123 if (!MoreArgs(line))
124 {
125 buf_printf(err, _("%s: too few arguments"), cmd->name);
126 goto done;
127 }
128
129 /* Second token is a replacement template */
131
132 if (mutt_replacelist_add(list, buf_string(regex), buf_string(templ), err) != 0)
133 {
134 rc = MUTT_CMD_ERROR;
135 goto done;
136 }
137
138 rc = MUTT_CMD_SUCCESS;
139
140done:
141 buf_pool_release(&regex);
142 buf_pool_release(&templ);
143 return rc;
144}
145
152{
153 if (!env || !env->subject || (*env->subject == '\0'))
154 return false;
155
156 if (env->disp_subj)
157 return true;
158
160 return false;
161
163 return true;
164}
165
171{
172 if (!mv || !mv->mailbox)
173 return;
174
175 struct Mailbox *m = mv->mailbox;
176
177 for (int i = 0; i < m->msg_count; i++)
178 {
179 struct Email *e = m->emails[i];
180 if (!e || !e->env)
181 continue;
182 FREE(&e->env->disp_subj);
183 }
184}
185
193 struct Buffer *line, struct Buffer *err)
194{
195 if (!MoreArgs(line))
196 {
197 buf_printf(err, _("%s: too few arguments"), cmd->name);
198 return MUTT_CMD_WARNING;
199 }
200
201 enum CommandResult rc;
202
203 rc = parse_replace_list(cmd, line, &SubjectRegexList, err);
204 if (rc == MUTT_CMD_SUCCESS)
205 {
206 mutt_debug(LL_NOTIFY, "NT_SUBJRX_ADD: %s\n", cmd->name);
208 }
209 return rc;
210}
211
219 struct Buffer *line, struct Buffer *err)
220{
221 if (!MoreArgs(line))
222 {
223 buf_printf(err, _("%s: too few arguments"), cmd->name);
224 return MUTT_CMD_WARNING;
225 }
226
227 enum CommandResult rc;
228
229 rc = parse_unreplace_list(cmd, line, &SubjectRegexList, err);
230 if (rc == MUTT_CMD_SUCCESS)
231 {
232 mutt_debug(LL_NOTIFY, "NT_SUBJRX_DELETE: %s\n", cmd->name);
234 }
235 return rc;
236}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
CommandResult
Error codes for command_t parse functions.
Definition command.h:35
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition command.h:38
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition command.h:36
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition command.h:37
Convenience wrapper for the core headers.
Structs that make up an email.
int parse_extract_token(struct Buffer *dest, struct Buffer *line, TokenFlags flags)
Extract one token from a string.
Definition extract.c:48
#define MoreArgs(buf)
Definition extract.h:30
#define TOKEN_NO_FLAGS
No flags are set.
Definition extract.h:44
enum CommandResult parse_unsubjectrx_list(const struct Command *cmd, struct Buffer *line, struct Buffer *err)
Parse the 'unsubjectrx' command - Implements Command::parse() -.
Definition subjectrx.c:218
enum CommandResult parse_subjectrx_list(const struct Command *cmd, struct Buffer *line, struct Buffer *err)
Parse the 'subjectrx' command - Implements Command::parse() -.
Definition subjectrx.c:192
#define mutt_debug(LEVEL,...)
Definition logging2.h:90
@ LL_NOTIFY
Log of notifications.
Definition logging2.h:49
#define FREE(x)
Definition memory.h:62
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:62
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition notify.c:173
void notify_set_parent(struct Notify *notify, struct Notify *parent)
Set the parent notification handler.
Definition notify.c:95
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition notify.c:75
int mutt_replacelist_remove(struct ReplaceList *rl, const char *pat)
Remove a pattern from a list.
Definition regex.c:565
void mutt_replacelist_free(struct ReplaceList *rl)
Free a ReplaceList object.
Definition regex.c:449
char * mutt_replacelist_apply(struct ReplaceList *rl, const char *str)
Apply replacements to a buffer.
Definition regex.c:368
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:270
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:660
View of a Mailbox.
@ NT_SUBJRX
Subject Regex has changed, NotifySubjRx.
Definition notify_type.h:55
Text parsing functions.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:96
#define STAILQ_HEAD_INITIALIZER(head)
Definition queue.h:324
#define STAILQ_EMPTY(head)
Definition queue.h:382
String manipulation buffer.
Definition buffer.h:36
const char * name
Name of the command.
Definition command.h:59
The envelope/body of an email.
Definition email.h:39
struct Envelope * env
Envelope information.
Definition email.h:68
The header of an Email.
Definition envelope.h:57
char *const subject
Email's subject.
Definition envelope.h:70
char * disp_subj
Display subject (modified copy of subject)
Definition envelope.h:72
View of a Mailbox.
Definition mview.h:40
struct Mailbox * mailbox
Current Mailbox.
Definition mview.h:51
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:43
struct Notify * notify
Notifications handler.
Definition neomutt.h:44
Notification API.
Definition notify.c:53
static enum CommandResult parse_replace_list(const struct Command *cmd, struct Buffer *line, struct ReplaceList *list, struct Buffer *err)
Parse a string replacement rule.
Definition subjectrx.c:107
void subjrx_init(void)
Create new Subject Regex List.
Definition subjectrx.c:54
void subjrx_clear_mods(struct MailboxView *mv)
Clear out all modified email subjects.
Definition subjectrx.c:170
static enum CommandResult parse_unreplace_list(const struct Command *cmd, struct Buffer *line, struct ReplaceList *list, struct Buffer *err)
Remove a string replacement rule.
Definition subjectrx.c:71
static struct Notify * SubjRxNotify
Notifications: NotifySubjRx.
Definition subjectrx.c:40
void subjrx_cleanup(void)
Free the Subject Regex List.
Definition subjectrx.c:45
bool subjrx_apply_mods(struct Envelope *env)
Apply regex modifications to the subject.
Definition subjectrx.c:151
static struct ReplaceList SubjectRegexList
List of subjectrx rules for modifying the Subject:
Definition subjectrx.c:39
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