NeoMutt  2025-12-11-911-gd8d604
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
commands.c
Go to the documentation of this file.
1
22
28
29#include "config.h"
30#include <stdbool.h>
31#include <stdio.h>
32#include "mutt/lib.h"
33#include "core/lib.h"
34#include "commands/lib.h"
35#include "parse/lib.h"
36#include "ignore.h"
37#include "module_data.h"
38#include "score.h"
39#include "spam.h"
40
50enum CommandResult parse_list(const struct Command *cmd, struct Buffer *line,
51 const struct ParseContext *pc, struct ParseError *pe)
52{
54 ASSERT(mod_data);
55
56 switch (cmd->id)
57 {
59 return parse_stailq(cmd, line, &mod_data->alternative_order, pc, pe);
60
61 case CMD_AUTO_VIEW:
62 return parse_stailq(cmd, line, &mod_data->auto_view, pc, pe);
63
65 return parse_stailq(cmd, line, &mod_data->header_order, pc, pe);
66
68 return parse_stailq(cmd, line, &mod_data->mail_to_allow, pc, pe);
69
70 default:
71 ASSERT(false);
72 }
73
74 return MUTT_CMD_ERROR;
75}
76
86enum CommandResult parse_unlist(const struct Command *cmd, struct Buffer *line,
87 const struct ParseContext *pc, struct ParseError *pe)
88{
90 ASSERT(mod_data);
91
92 switch (cmd->id)
93 {
95 return parse_unstailq(cmd, line, &mod_data->alternative_order, pc, pe);
96
97 case CMD_UNAUTO_VIEW:
98 return parse_unstailq(cmd, line, &mod_data->auto_view, pc, pe);
99
101 return parse_unstailq(cmd, line, &mod_data->header_order, pc, pe);
102
104 return parse_unstailq(cmd, line, &mod_data->mail_to_allow, pc, pe);
105
106 default:
107 ASSERT(false);
108 }
109
110 return MUTT_CMD_ERROR;
111}
112
123enum CommandResult parse_tag_formats(const struct Command *cmd, struct Buffer *line,
124 const struct ParseContext *pc, struct ParseError *pe)
125{
126 struct Buffer *err = pe->message;
127
128 if (!MoreArgs(line))
129 {
130 buf_printf(err, _("%s: too few arguments"), cmd->name);
131 return MUTT_CMD_WARNING;
132 }
133
134 struct Buffer *tag = buf_pool_get();
135 struct Buffer *fmt = buf_pool_get();
136
138 ASSERT(mod_data);
139
140 while (MoreArgs(line))
141 {
143 if (buf_is_empty(tag))
144 continue;
145
147
148 /* avoid duplicates */
149 const char *tmp = mutt_hash_find(mod_data->tag_formats, buf_string(fmt));
150 if (tmp)
151 {
152 mutt_warning(_("tag format '%s' already registered as '%s'"), buf_string(fmt), tmp);
153 continue;
154 }
155
156 mutt_hash_insert(mod_data->tag_formats, buf_string(fmt), buf_strdup(tag));
157 }
158
159 buf_pool_release(&tag);
160 buf_pool_release(&fmt);
161 return MUTT_CMD_SUCCESS;
162}
163
174enum CommandResult parse_tag_transforms(const struct Command *cmd, struct Buffer *line,
175 const struct ParseContext *pc,
176 struct ParseError *pe)
177{
178 struct Buffer *err = pe->message;
179
180 if (!MoreArgs(line))
181 {
182 buf_printf(err, _("%s: too few arguments"), cmd->name);
183 return MUTT_CMD_WARNING;
184 }
185
186 struct Buffer *tag = buf_pool_get();
187 struct Buffer *trans = buf_pool_get();
188
190 ASSERT(mod_data);
191
192 while (MoreArgs(line))
193 {
195 if (buf_is_empty(tag))
196 continue;
197
198 parse_extract_token(trans, line, TOKEN_NONE);
199 const char *trn = buf_string(trans);
200
201 /* avoid duplicates */
202 const char *tmp = mutt_hash_find(mod_data->tag_transforms, buf_string(tag));
203 if (tmp)
204 {
205 mutt_warning(_("tag transform '%s' already registered as '%s'"),
206 buf_string(tag), tmp);
207 continue;
208 }
209
211 }
212
213 buf_pool_release(&tag);
214 buf_pool_release(&trans);
215 return MUTT_CMD_SUCCESS;
216}
217
221const struct Command EmailCommands[] = {
222 // clang-format off
223 { "alternative-order", CMD_ALTERNATIVE_ORDER, parse_list,
224 N_("Set preference order for multipart alternatives"),
225 N_("alternative-order <mime-type>[/<mime-subtype> ] [ ... ]"),
226 "mimesupport.html#alternative-order" },
227 { "auto-view", CMD_AUTO_VIEW, parse_list,
228 N_("Automatically display specified MIME types inline"),
229 N_("auto-view <mime-type>[/<mime-subtype> ] [ ... ]"),
230 "mimesupport.html#auto-view" },
231 { "header-order", CMD_HEADER_ORDER, parse_list,
232 N_("Define custom order of headers displayed"),
233 N_("header-order <header> [ <header> ... ]"),
234 "configuration.html#header-order" },
235 { "ignore", CMD_IGNORE, parse_ignore,
236 N_("Hide specified headers when displaying messages"),
237 N_("ignore <string> [ <string> ...]"),
238 "configuration.html#ignore" },
239 { "mailto-allow", CMD_MAILTO_ALLOW, parse_list,
240 N_("Permit specific header-fields in mailto URL processing"),
241 N_("mailto-allow { * | <header-field> ... }"),
242 "configuration.html#mailto-allow" },
243 { "nospam", CMD_NOSPAM, parse_nospam,
244 N_("Remove a spam detection rule"),
245 N_("nospam { * | <regex> }"),
246 "configuration.html#spam" },
247 { "score", CMD_SCORE, parse_score,
248 N_("Set a score value on emails matching a pattern"),
249 N_("score <pattern> <value>"),
250 "configuration.html#score-command" },
251 { "spam", CMD_SPAM, parse_spam,
252 N_("Define rules to parse spam detection headers"),
253 N_("spam <regex> [ <format> ]"),
254 "configuration.html#spam" },
255 { "tag-formats", CMD_TAG_FORMATS, parse_tag_formats,
256 N_("Define expandos tags"),
257 N_("tag-formats <tag> <format-string> [ ... ] }"),
258 "optionalfeatures.html#custom-tags" },
259 { "tag-transforms", CMD_TAG_TRANSFORMS, parse_tag_transforms,
260 N_("Rules to transform tags into icons"),
261 N_("tag-transforms <tag> <transformed-string> [ ... ]"),
262 "optionalfeatures.html#custom-tags" },
263 { "unalternative-order", CMD_UNALTERNATIVE_ORDER, parse_unlist,
264 N_("Remove MIME types from preference order"),
265 N_("unalternative-order { * | [ <mime-type>[/<mime-subtype> ] ... ] }"),
266 "mimesupport.html#alternative-order" },
267 { "unauto-view", CMD_UNAUTO_VIEW, parse_unlist,
268 N_("Remove MIME types from `auto-view` list"),
269 N_("unauto-view { * | [ <mime-type>[/<mime-subtype> ] ... ] }"),
270 "mimesupport.html#auto-view" },
271 { "unheader-order", CMD_UNHEADER_ORDER, parse_unlist,
272 N_("Remove header from `header-order` list"),
273 N_("unheader-order { * | <header> ... }"),
274 "configuration.html#header-order" },
275 { "unignore", CMD_UNIGNORE, parse_unignore,
276 N_("Remove a header from the `header-order` list"),
277 N_("unignore { * | <string> ... }"),
278 "configuration.html#ignore" },
279 { "unmailto-allow", CMD_UNMAILTO_ALLOW, parse_unlist,
280 N_("Disallow header-fields in mailto processing"),
281 N_("unmailto-allow { * | <header-field> ... }"),
282 "configuration.html#mailto-allow" },
283 { "unscore", CMD_UNSCORE, parse_unscore,
284 N_("Remove scoring rules for matching patterns"),
285 N_("unscore { * | <pattern> ... }"),
286 "configuration.html#score-command" },
287
288 // Deprecated
289 { "alternative_order", CMD_NONE, NULL, "alternative-order", NULL, NULL, CF_SYNONYM },
290 { "auto_view", CMD_NONE, NULL, "auto-view", NULL, NULL, CF_SYNONYM },
291 { "hdr_order", CMD_NONE, NULL, "header-order", NULL, NULL, CF_SYNONYM },
292 { "mailto_allow", CMD_NONE, NULL, "mailto-allow", NULL, NULL, CF_SYNONYM },
293 { "unalternative_order", CMD_NONE, NULL, "unalternative-order", NULL, NULL, CF_SYNONYM },
294 { "unauto_view", CMD_NONE, NULL, "unauto-view", NULL, NULL, CF_SYNONYM },
295 { "unhdr_order", CMD_NONE, NULL, "unheader-order", NULL, NULL, CF_SYNONYM },
296 { "unmailto_allow", CMD_NONE, NULL, "unmailto-allow", NULL, NULL, CF_SYNONYM },
297
298 { NULL, CMD_NONE, NULL, NULL, NULL, NULL, CF_NONE },
299 // clang-format on
300};
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition buffer.c:291
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
@ CF_SYNONYM
Command is a synonym for another command.
Definition command.h:50
@ CF_NONE
No flags are set.
Definition command.h:49
@ CMD_SPAM
:spam
Definition command.h:117
@ CMD_IGNORE
:ignore
Definition command.h:88
@ CMD_TAG_TRANSFORMS
:tag-transforms
Definition command.h:123
@ CMD_SCORE
:score
Definition command.h:108
@ CMD_MAILTO_ALLOW
:mailto-allow
Definition command.h:95
@ CMD_AUTO_VIEW
:auto-view
Definition command.h:69
@ CMD_UNALTERNATIVE_ORDER
:unalternative-order
Definition command.h:128
@ CMD_UNAUTO_VIEW
:unauto-view
Definition command.h:130
@ CMD_UNHEADER_ORDER
:unheader-order
Definition command.h:134
@ CMD_NOSPAM
:nospam
Definition command.h:102
@ CMD_NONE
No Command.
Definition command.h:62
@ CMD_TAG_FORMATS
:tag-formats
Definition command.h:122
@ CMD_UNIGNORE
:unignore
Definition command.h:136
@ CMD_ALTERNATIVE_ORDER
:alternative-order
Definition command.h:66
@ CMD_UNSCORE
:unscore
Definition command.h:144
@ CMD_HEADER_ORDER
:header-order
Definition command.h:83
@ CMD_UNMAILTO_ALLOW
:unmailto-allow
Definition command.h:140
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
NeoMutt Commands.
Convenience wrapper for the core headers.
const struct Command EmailCommands[]
Email Commands.
Definition commands.c:221
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
@ TOKEN_NONE
No flags are set.
Definition extract.h:49
enum CommandResult parse_tag_transforms(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'tag-transforms' command - Implements Command::parse() -.
Definition commands.c:174
enum CommandResult parse_stailq(const struct Command *cmd, struct Buffer *line, struct ListHead *list, const struct ParseContext *pc, struct ParseError *pe)
Parse a list command - Implements Command::parse() -.
Definition stailq.c:52
enum CommandResult parse_unstailq(const struct Command *cmd, struct Buffer *line, struct ListHead *list, const struct ParseContext *pc, struct ParseError *pe)
Parse an unlist command - Implements Command::parse() -.
Definition stailq.c:85
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_unlist(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse an unlist command - Implements Command::parse() -.
Definition commands.c:86
enum CommandResult parse_tag_formats(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'tag-formats' command - Implements Command::parse() -.
Definition commands.c:123
enum CommandResult parse_unscore(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'unscore' command - Implements Command::parse() -.
Definition score.c:166
enum CommandResult parse_unignore(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'unignore' command - Implements Command::parse() -.
Definition ignore.c:83
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
enum CommandResult parse_ignore(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'ignore' command - Implements Command::parse() -.
Definition ignore.c:50
enum CommandResult parse_score(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'score' command - Implements Command::parse() -.
Definition score.c:76
enum CommandResult parse_list(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse a list command - Implements Command::parse() -.
Definition commands.c:50
#define mutt_warning(...)
Definition logging2.h:92
struct HashElem * mutt_hash_insert(struct HashTable *table, const char *strkey, void *data)
Add a new element to the Hash Table (with string keys)
Definition hash.c:337
void * mutt_hash_find(const struct HashTable *table, const char *strkey)
Find the HashElem data in a Hash Table element using a key.
Definition hash.c:364
Parse Ignore Commands.
@ MODULE_ID_EMAIL
ModuleEmail, Email code
Definition module_api.h:64
Convenience wrapper for the library headers.
#define N_(a)
Definition message.h:32
#define _(a)
Definition message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:663
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
Routines for adding user scores to emails.
#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:162
enum CommandId id
ID of the Command.
Definition command.h:163
Email private Module data.
Definition module_data.h:32
struct HashTable * tag_formats
Hash Table: "inbox" -> "GI" - Tag format strings.
Definition module_data.h:41
struct ListHead auto_view
List of mime types to auto view.
Definition module_data.h:35
struct ListHead mail_to_allow
Permitted fields in a mailto: url.
Definition module_data.h:38
struct ListHead alternative_order
List of preferred mime types to display.
Definition module_data.h:34
struct HashTable * tag_transforms
Hash Table: "inbox" -> "i" - Alternative tag names.
Definition module_data.h:42
struct ListHead header_order
List of header fields in the order they should be displayed.
Definition module_data.h:36
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