NeoMutt  2025-12-11-435-g4ac674
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
spam.c
Go to the documentation of this file.
1
30
36
37#include "config.h"
38#include <stdio.h>
39#include "mutt/lib.h"
40#include "core/lib.h"
41#include "spam.h"
42#include "parse/lib.h"
43#include "module_data.h"
44
51enum CommandResult parse_nospam(const struct Command *cmd, struct Buffer *line,
52 const struct ParseContext *pc, struct ParseError *pe)
53{
54 struct Buffer *err = pe->message;
55
56 if (!MoreArgs(line))
57 {
58 buf_printf(err, _("%s: too few arguments"), cmd->name);
59 return MUTT_CMD_WARNING;
60 }
61
62 struct Buffer *token = buf_pool_get();
64
65 // Extract the first token, a regex or "*"
67
68 if (MoreArgs(line))
69 {
70 buf_printf(err, _("%s: too many arguments"), cmd->name);
71 goto done;
72 }
73
75 ASSERT(md);
76
77 // "*" is special - clear both spam and nospam lists
78 if (mutt_str_equal(buf_string(token), "*"))
79 {
83 goto done;
84 }
85
86 // If it's on the spam list, just remove it
87 if (mutt_replacelist_remove(&md->spam, buf_string(token)) != 0)
88 {
90 goto done;
91 }
92
93 // Otherwise, add it to the nospam list
94 if (mutt_regexlist_add(&md->no_spam, buf_string(token), REG_ICASE, err) != 0)
95 {
96 rc = MUTT_CMD_ERROR;
97 goto done;
98 }
99
100 rc = MUTT_CMD_SUCCESS;
101
102done:
103 buf_pool_release(&token);
104 return rc;
105}
106
113enum CommandResult parse_spam(const struct Command *cmd, struct Buffer *line,
114 const struct ParseContext *pc, struct ParseError *pe)
115{
116 struct Buffer *err = pe->message;
117
118 if (!MoreArgs(line))
119 {
120 buf_printf(err, _("%s: too few arguments"), cmd->name);
121 return MUTT_CMD_WARNING;
122 }
123
124 struct Buffer *token = buf_pool_get();
125 struct Buffer *templ = NULL;
127
128 // Extract the first token, a regex
130
132 ASSERT(md);
133
134 // If there's a second parameter, it's a template for the spam tag
135 if (MoreArgs(line))
136 {
137 templ = buf_pool_get();
139
140 // Add to the spam list
141 if (mutt_replacelist_add(&md->spam, buf_string(token), buf_string(templ), err) != 0)
142 goto done;
143 }
144 else
145 {
146 // If not, try to remove from the nospam list
148 }
149
150 rc = MUTT_CMD_SUCCESS;
151
152done:
153 buf_pool_release(&templ);
154 buf_pool_release(&token);
155 return rc;
156}
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.
Email private Module data.
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_nospam(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'nospam' command - Implements Command::parse() -.
Definition spam.c:51
enum CommandResult parse_spam(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'spam' command - Implements Command::parse() -.
Definition spam.c:113
@ MODULE_ID_EMAIL
ModuleEmail, Email code
Definition module_api.h:64
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
int mutt_replacelist_remove(struct ReplaceList *rl, const char *pat)
Remove a pattern from a list.
Definition regex.c:566
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition regex.c:179
int mutt_regexlist_add(struct RegexList *rl, const char *str, uint16_t flags, struct Buffer *err)
Compile a regex string and add it to a list.
Definition regex.c:140
void mutt_replacelist_free(struct ReplaceList *rl)
Free a ReplaceList object.
Definition regex.c:450
int mutt_regexlist_remove(struct RegexList *rl, const char *str)
Remove a Regex from a list.
Definition regex.c:235
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
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 ASSERT(COND)
Definition signal2.h:59
Parse Spam Commands.
String manipulation buffer.
Definition buffer.h:36
const char * name
Name of the Command.
Definition command.h:159
Email private Module data.
Definition module_data.h:32
struct ReplaceList spam
Regexes and patterns to match spam emails.
Definition module_data.h:41
struct RegexList no_spam
Regexes to identify non-spam emails.
Definition module_data.h:40
Container for Accounts, Notifications.
Definition neomutt.h:41
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