NeoMutt
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_make(0);
97
98 /* First token is a regex. */
99 if (!MoreArgs(s))
100 {
101 buf_printf(err, _("%s: too few arguments"), "subjectrx");
102 return MUTT_CMD_WARNING;
103 }
105
106 /* Second token is a replacement template */
107 if (!MoreArgs(s))
108 {
109 buf_printf(err, _("%s: too few arguments"), "subjectrx");
110 return MUTT_CMD_WARNING;
111 }
113
114 if (mutt_replacelist_add(list, buf->data, templ.data, err) != 0)
115 {
116 FREE(&templ.data);
117 return MUTT_CMD_ERROR;
118 }
119 FREE(&templ.data);
120
121 return MUTT_CMD_SUCCESS;
122}
123
130{
131 if (!env || !env->subject || (*env->subject == '\0'))
132 return false;
133
134 if (env->disp_subj)
135 return true;
136
138 return false;
139
141 return true;
142}
143
149{
150 if (!mv || !mv->mailbox)
151 return;
152
153 struct Mailbox *m = mv->mailbox;
154
155 for (int i = 0; i < m->msg_count; i++)
156 {
157 struct Email *e = m->emails[i];
158 if (!e || !e->env)
159 continue;
160 FREE(&e->env->disp_subj);
161 }
162}
163
168 intptr_t data, struct Buffer *err)
169{
170 enum CommandResult rc;
171
172 rc = parse_replace_list(buf, s, &SubjectRegexList, err);
173 if (rc == MUTT_CMD_SUCCESS)
174 {
175 mutt_debug(LL_NOTIFY, "NT_SUBJRX_ADD: %s\n", buf->data);
177 }
178 return rc;
179}
180
185 intptr_t data, struct Buffer *err)
186{
187 enum CommandResult rc;
188
189 rc = parse_unreplace_list(buf, s, &SubjectRegexList, err);
190 if (rc == MUTT_CMD_SUCCESS)
191 {
192 mutt_debug(LL_NOTIFY, "NT_SUBJRX_DELETE: %s\n", buf->data);
194 }
195 return rc;
196}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:173
struct Buffer buf_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:70
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:48
#define MoreArgs(buf)
Definition: extract.h:31
#define TOKEN_NO_FLAGS
No flags are set.
Definition: extract.h:45
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:184
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:167
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:588
char * mutt_replacelist_apply(struct ReplaceList *rl, char *buf, size_t buflen, const char *str)
Apply replacements to a buffer.
Definition: regex.c:371
void mutt_replacelist_free(struct ReplaceList *rl)
Free a ReplaceList object.
Definition: regex.c:472
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:267
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
View of a Mailbox.
@ NT_SUBJRX
Subject Regex has changed, NotifySubjRx.
Definition: notify_type.h:55
Text parsing functions.
#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
View of a Mailbox.
Definition: mview.h:39
struct Mailbox * mailbox
Current Mailbox.
Definition: mview.h:50
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:148
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:129
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