NeoMutt  2025-12-11-435-g4ac674
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
my_header.c
Go to the documentation of this file.
1
30
36
37#include "config.h"
38#include <string.h>
39#include "mutt/lib.h"
40#include "email/lib.h"
41#include "core/lib.h"
42#include "my_header.h"
43#include "parse/lib.h"
44#include "module_data.h"
45
52enum CommandResult parse_my_header(const struct Command *cmd, struct Buffer *line,
53 const struct ParseContext *pc, struct ParseError *pe)
54{
55 struct Buffer *err = pe->message;
56
57 if (!MoreArgs(line))
58 {
59 buf_printf(err, _("%s: too few arguments"), cmd->name);
60 return MUTT_CMD_WARNING;
61 }
62
63 struct Buffer *token = buf_pool_get();
65
67 char *p = strpbrk(buf_string(token), ": \t");
68 if (!p || (*p != ':'))
69 {
70 buf_strcpy(err, _("invalid header field"));
71 goto done;
72 }
73
75 ASSERT(md);
76
77 struct EventHeader ev_h = { token->data };
78 struct ListNode *node = header_find(&md->user_header, buf_string(token));
79
80 if (node)
81 {
82 header_update(node, buf_string(token));
83 mutt_debug(LL_NOTIFY, "NT_HEADER_CHANGE: %s\n", buf_string(token));
85 }
86 else
87 {
88 header_add(&md->user_header, buf_string(token));
89 mutt_debug(LL_NOTIFY, "NT_HEADER_ADD: %s\n", buf_string(token));
91 }
92
94
95done:
96 buf_pool_release(&token);
97 return rc;
98}
99
106enum CommandResult parse_unmy_header(const struct Command *cmd, struct Buffer *line,
107 const struct ParseContext *pc, struct ParseError *pe)
108{
109 struct Buffer *err = pe->message;
110
111 if (!MoreArgs(line))
112 {
113 buf_printf(err, _("%s: too few arguments"), cmd->name);
114 return MUTT_CMD_WARNING;
115 }
116
117 struct Buffer *token = buf_pool_get();
118
119 struct ListNode *np = NULL, *tmp = NULL;
120 size_t l;
121
123 ASSERT(md);
124
125 do
126 {
128 if (mutt_str_equal("*", buf_string(token)))
129 {
130 /* Clear all headers, send a notification for each header */
131 STAILQ_FOREACH(np, &md->user_header, entries)
132 {
133 mutt_debug(LL_NOTIFY, "NT_HEADER_DELETE: %s\n", np->data);
134 struct EventHeader ev_h = { np->data };
136 }
138 continue;
139 }
140
141 l = mutt_str_len(buf_string(token));
142 if (buf_at(token, l - 1) == ':')
143 l--;
144
145 STAILQ_FOREACH_SAFE(np, &md->user_header, entries, tmp)
146 {
147 if (mutt_istrn_equal(buf_string(token), np->data, l) && (np->data[l] == ':'))
148 {
149 mutt_debug(LL_NOTIFY, "NT_HEADER_DELETE: %s\n", np->data);
150 struct EventHeader ev_h = { np->data };
152
153 header_free(&md->user_header, np);
154 }
155 }
156 } while (MoreArgs(line));
157 buf_pool_release(&token);
158 return MUTT_CMD_SUCCESS;
159}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
char buf_at(const struct Buffer *buf, size_t offset)
Return the character at the given offset.
Definition buffer.c:668
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:395
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_WARNING
Warning: Help given to the user.
Definition command.h:39
Convenience wrapper for the core headers.
void header_free(struct ListHead *hdrlist, struct ListNode *target)
Free and remove a header from a header list.
Definition email.c:202
struct ListNode * header_add(struct ListHead *hdrlist, const char *header)
Add a header to a list.
Definition email.c:160
struct ListNode * header_update(struct ListNode *hdr, const char *header)
Update an existing header.
Definition email.c:174
struct ListNode * header_find(const struct ListHead *hdrlist, const char *header)
Find a header, matching on its field, in a list of headers.
Definition email.c:137
Structs that make up an email.
@ NT_HEADER_CHANGE
An existing header has been changed.
Definition email.h:210
@ NT_HEADER_ADD
Header has been added.
Definition email.h:208
@ NT_HEADER_DELETE
Header has been removed.
Definition email.h:209
int parse_extract_token(struct Buffer *dest, struct Buffer *line, TokenFlags flags)
Extract one token from a string.
Definition extract.c:49
#define TOKEN_SPACE
Don't treat whitespace as a term.
Definition extract.h:48
#define TOKEN_QUOTE
Don't interpret quotes.
Definition extract.h:49
#define MoreArgs(buf)
Definition extract.h:31
#define TOKEN_NO_FLAGS
No flags are set.
Definition extract.h:45
enum CommandResult parse_my_header(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'my-header' command - Implements Command::parse() -.
Definition my_header.c:52
enum CommandResult parse_unmy_header(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'unmy-header' command - Implements Command::parse() -.
Definition my_header.c:106
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition list.c:123
@ LL_NOTIFY
Log of notifications.
Definition logging2.h:50
@ MODULE_ID_SEND
ModuleSend, Send
Definition module_api.h:90
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
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
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:662
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:500
bool mutt_istrn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings ignoring case (to a maximum), safely.
Definition string.c:457
Parse My-header Commands.
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:585
@ NT_HEADER
A header has changed, NotifyHeader EventHeader.
Definition notify_type.h:47
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_FOREACH(var, head, field)
Definition queue.h:390
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition queue.h:400
Send private Module data.
#define ASSERT(COND)
Definition signal2.h:59
String manipulation buffer.
Definition buffer.h:36
char * data
Pointer to data.
Definition buffer.h:37
const char * name
Name of the Command.
Definition command.h:159
An event that happened to a header.
Definition email.h:217
A List node for strings.
Definition list.h:37
char * data
String.
Definition list.h:38
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
Send private Module data.
Definition module_data.h:32
struct ListHead user_header
Custom headers to add to outgoing emails.
Definition module_data.h:33