NeoMutt  2025-12-11-435-g4ac674
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 "gui/lib.h"
35#include "subjectrx.h"
36#include "parse/lib.h"
37#include "module_data.h"
38
49
59
68static enum CommandResult parse_unreplace_list(const struct Command *cmd,
69 struct Buffer *line,
70 struct ReplaceList *list, struct Buffer *err)
71{
72 if (!MoreArgs(line))
73 {
74 buf_printf(err, _("%s: too few arguments"), cmd->name);
75 return MUTT_CMD_WARNING;
76 }
77
78 struct Buffer *token = buf_pool_get();
79
80 /* First token is a regex. */
82
83 /* "*" is a special case. */
84 if (mutt_str_equal(buf_string(token), "*"))
85 {
87 buf_pool_release(&token);
88 return MUTT_CMD_SUCCESS;
89 }
90
92 buf_pool_release(&token);
93 return MUTT_CMD_SUCCESS;
94}
95
104static enum CommandResult parse_replace_list(const struct Command *cmd, struct Buffer *line,
105 struct ReplaceList *list, struct Buffer *err)
106{
107 struct Buffer *templ = buf_pool_get();
108 struct Buffer *regex = buf_pool_get();
109 int rc = MUTT_CMD_WARNING;
110
111 if (!MoreArgs(line))
112 {
113 buf_printf(err, _("%s: too few arguments"), cmd->name);
114 goto done;
115 }
116
117 /* First token is a regex. */
119
120 if (!MoreArgs(line))
121 {
122 buf_printf(err, _("%s: too few arguments"), cmd->name);
123 goto done;
124 }
125
126 /* Second token is a replacement template */
128
129 if (mutt_replacelist_add(list, buf_string(regex), buf_string(templ), err) != 0)
130 {
131 rc = MUTT_CMD_ERROR;
132 goto done;
133 }
134
135 rc = MUTT_CMD_SUCCESS;
136
137done:
138 buf_pool_release(&regex);
139 buf_pool_release(&templ);
140 return rc;
141}
142
149{
150 if (!env || !env->subject || (*env->subject == '\0'))
151 return false;
152
153 if (env->disp_subj)
154 return true;
155
157 ASSERT(md);
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
192enum CommandResult parse_subjectrx_list(const struct Command *cmd, struct Buffer *line,
193 const struct ParseContext *pc,
194 struct ParseError *pe)
195{
196 struct Buffer *err = pe->message;
197
198 if (!MoreArgs(line))
199 {
200 buf_printf(err, _("%s: too few arguments"), cmd->name);
201 return MUTT_CMD_WARNING;
202 }
203
205 ASSERT(md);
206
207 enum CommandResult rc;
208
209 rc = parse_replace_list(cmd, line, &md->subject_rx_list, err);
210 if (rc == MUTT_CMD_SUCCESS)
211 {
212 mutt_debug(LL_NOTIFY, "NT_SUBJECTRX_ADD: %s\n", cmd->name);
214 }
215 return rc;
216}
217
224enum CommandResult parse_unsubjectrx_list(const struct Command *cmd, struct Buffer *line,
225 const struct ParseContext *pc,
226 struct ParseError *pe)
227{
228 struct Buffer *err = pe->message;
229
230 if (!MoreArgs(line))
231 {
232 buf_printf(err, _("%s: too few arguments"), cmd->name);
233 return MUTT_CMD_WARNING;
234 }
235
237 ASSERT(md);
238
239 enum CommandResult rc;
240
241 rc = parse_unreplace_list(cmd, line, &md->subject_rx_list, err);
242 if (rc == MUTT_CMD_SUCCESS)
243 {
244 mutt_debug(LL_NOTIFY, "NT_SUBJECTRX_DELETE: %s\n", cmd->name);
246 }
247 return rc;
248}
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:37
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition command.h:40
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition command.h:38
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition command.h:39
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:49
#define MoreArgs(buf)
Definition extract.h:31
#define TOKEN_NO_FLAGS
No flags are set.
Definition extract.h:45
enum CommandResult parse_unsubjectrx_list(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'unsubject-regex' command - Implements Command::parse() -.
Definition subjectrx.c:224
enum CommandResult parse_subjectrx_list(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'subject-regex' command - Implements Command::parse() -.
Definition subjectrx.c:192
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
Convenience wrapper for the gui headers.
Index private Module data.
@ LL_NOTIFY
Log of notifications.
Definition logging2.h:50
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
@ MODULE_ID_INDEX
ModuleIndex, Index
Definition module_api.h:72
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:566
void mutt_replacelist_free(struct ReplaceList *rl)
Free a ReplaceList object.
Definition regex.c:450
char * mutt_replacelist_apply(struct ReplaceList *rl, const char *str)
Apply replacements to a buffer.
Definition regex.c:369
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:271
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:662
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:585
@ NT_SUBJECTRX
Subject Regex has changed, NotifySubjectRx.
Definition notify_type.h:55
Text parsing functions.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
#define STAILQ_INIT(head)
Definition queue.h:410
#define STAILQ_EMPTY(head)
Definition queue.h:382
#define ASSERT(COND)
Definition signal2.h:59
String manipulation buffer.
Definition buffer.h:36
const char * name
Name of the Command.
Definition command.h:159
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
Index private Module data.
Definition module_data.h:32
struct Notify * subject_rx_notify
List of subject-regex rules for modifying the Subject:
Definition module_data.h:34
struct ReplaceList subject_rx_list
Definition module_data.h:33
View of a Mailbox.
Definition mview.h:40
struct Mailbox * mailbox
Current Mailbox.
Definition mview.h:51
A mailbox.
Definition mailbox.h:78
int msg_count
Total number of messages.
Definition mailbox.h:87
struct Email ** emails
Array of Emails.
Definition mailbox.h:95
Container for Accounts, Notifications.
Definition neomutt.h:41
struct Notify * notify
Notifications handler.
Definition neomutt.h:45
Context for config parsing (history/backtrace)
Definition pcontext.h:34
Detailed error information from config parsing.
Definition perror.h:34
struct Buffer * message
Error message.
Definition perror.h:35
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:104
void subjectrx_init(struct NeoMutt *n, struct IndexModuleData *md)
Create new Subject Regex List.
Definition subjectrx.c:42
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:68
void subjectrx_cleanup(struct IndexModuleData *md)
Free the Subject Regex List.
Definition subjectrx.c:53
void subjectrx_clear_mods(struct MailboxView *mv)
Clear out all modified email subjects.
Definition subjectrx.c:170
bool subjectrx_apply_mods(struct Envelope *env)
Apply regex modifications to the subject.
Definition subjectrx.c:148
Parse Subject-regex Commands.
@ NT_SUBJECTRX_DELETE
Subject Regex has been deleted.
Definition subjectrx.h:46
@ NT_SUBJECTRX_ADD
Subject Regex has been added.
Definition subjectrx.h:45