NeoMutt  2024-03-23-147-g885fbc
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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 "subjectrx.h"
36#include "parse/lib.h"
37#include "mview.h"
38
41static struct Notify *SubjRxNotify = NULL;
42
47{
50}
51
55void subjrx_init(void)
56{
57 if (SubjRxNotify)
58 return;
59
62}
63
67static 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 buf_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
93static enum CommandResult parse_replace_list(struct Buffer *buf, struct Buffer *s,
94 struct ReplaceList *list, struct Buffer *err)
95{
96 struct Buffer *templ = buf_pool_get();
97 int rc = MUTT_CMD_WARNING;
98
99 /* First token is a regex. */
100 if (!MoreArgs(s))
101 {
102 buf_printf(err, _("%s: too few arguments"), "subjectrx");
103 goto done;
104 }
106
107 /* Second token is a replacement template */
108 if (!MoreArgs(s))
109 {
110 buf_printf(err, _("%s: too few arguments"), "subjectrx");
111 goto done;
112 }
114
115 if (mutt_replacelist_add(list, buf->data, buf_string(templ), err) != 0)
116 {
117 rc = MUTT_CMD_ERROR;
118 goto done;
119 }
120
121 rc = MUTT_CMD_SUCCESS;
122
123done:
124 buf_pool_release(&templ);
125 return rc;
126}
127
134{
135 if (!env || !env->subject || (*env->subject == '\0'))
136 return false;
137
138 if (env->disp_subj)
139 return true;
140
142 return false;
143
145 return true;
146}
147
153{
154 if (!mv || !mv->mailbox)
155 return;
156
157 struct Mailbox *m = mv->mailbox;
158
159 for (int i = 0; i < m->msg_count; i++)
160 {
161 struct Email *e = m->emails[i];
162 if (!e || !e->env)
163 continue;
164 FREE(&e->env->disp_subj);
165 }
166}
167
172 intptr_t data, struct Buffer *err)
173{
174 enum CommandResult rc;
175
176 rc = parse_replace_list(buf, s, &SubjectRegexList, err);
177 if (rc == MUTT_CMD_SUCCESS)
178 {
179 mutt_debug(LL_NOTIFY, "NT_SUBJRX_ADD: %s\n", buf->data);
181 }
182 return rc;
183}
184
189 intptr_t data, struct Buffer *err)
190{
191 enum CommandResult rc;
192
193 rc = parse_unreplace_list(buf, s, &SubjectRegexList, err);
194 if (rc == MUTT_CMD_SUCCESS)
195 {
196 mutt_debug(LL_NOTIFY, "NT_SUBJRX_DELETE: %s\n", buf->data);
198 }
199 return rc;
200}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
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:36
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:39
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:37
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition: command.h:38
Convenience wrapper for the core headers.
Structs that make up an email.
int parse_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: extract.c:50
#define MoreArgs(buf)
Definition: extract.h:32
#define TOKEN_NO_FLAGS
No flags are set.
Definition: extract.h:46
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:188
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:171
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
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
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_NOTIFY
Log of notifications.
Definition: logging2.h:48
#define FREE(x)
Definition: memory.h:45
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:709
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:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define STAILQ_EMPTY(head)
Definition: queue.h:348
String manipulation buffer.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:37
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:41
struct Notify * notify
Notifications handler.
Definition: neomutt.h:42
Notification API.
Definition: notify.c:53
void subjrx_init(void)
Create new Subject Regex List.
Definition: subjectrx.c:55
void subjrx_clear_mods(struct MailboxView *mv)
Clear out all modified email subjects.
Definition: subjectrx.c:152
static struct Notify * SubjRxNotify
Notifications: NotifySubjRx.
Definition: subjectrx.c:41
void subjrx_cleanup(void)
Free the Subject Regex List.
Definition: subjectrx.c:46
bool subjrx_apply_mods(struct Envelope *env)
Apply regex modifications to the subject.
Definition: subjectrx.c:133
static struct ReplaceList SubjectRegexList
List of subjectrx rules for modifying the Subject:
Definition: subjectrx.c:40
Subject Regex handling.
@ NT_SUBJRX_DELETE
Subject Regex has been deleted.
Definition: subjectrx.h:44
@ NT_SUBJRX_ADD
Subject Regex has been added.
Definition: subjectrx.h:43