NeoMutt  2025-12-11-435-g4ac674
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
dump.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 "config/lib.h"
34#include "core/lib.h"
35#include "mutt.h"
36#include "commands/lib.h"
37#include "expando/lib.h"
38#include "pager/lib.h"
39#include "parse/lib.h"
40#include "hook.h"
41#include "muttlib.h"
42#include "parse.h"
43
50static void hooks_dump_one(struct Hook *hook, const struct Command *cmd, struct Buffer *buf)
51{
52 struct Buffer *pretty = buf_pool_get();
53
54 buf_add_printf(buf, "%s ", cmd->name);
55
56 if (hook->regex.pattern)
57 {
58 if (hook->regex.pat_not)
59 buf_addch(pretty, '!');
60
61 if ((hook->id == CMD_FOLDER_HOOK) || (hook->id == CMD_MBOX_HOOK))
62 {
63 buf_strcpy(pretty, hook->regex.pattern);
64 pretty_mailbox(pretty);
65 buf_add_printf(buf, "\"%s\" ", buf_string(pretty));
66 }
67 else
68 {
69 buf_addstr(pretty, hook->regex.pattern);
70 pretty_var(buf_string(pretty), buf);
71 buf_addch(buf, ' ');
72 }
73 }
74
75 if ((hook->id == CMD_FCC_HOOK) || (hook->id == CMD_MBOX_HOOK) || (hook->id == CMD_SAVE_HOOK))
76 {
77 buf_strcpy(pretty, hook->command);
78 pretty_mailbox(pretty);
79 buf_add_printf(buf, "\"%s\"\n", buf_string(pretty));
80 }
81 else
82 {
83 pretty_var(hook->command, buf);
84 buf_addch(buf, '\n');
85 }
86
87 buf_pool_release(&pretty);
88}
89
94static void hooks_dump_simple(struct Buffer *buf)
95{
96 // Dump all the Hooks, except: CMD_CHARSET_HOOK, CMD_ICONV_HOOK, CMD_INDEX_FORMAT_HOOK
97 static const enum CommandId hook_ids[] = {
102 };
103
104 for (size_t i = 0; i < countof(hook_ids); i++)
105 {
106 const struct Command *hook_cmd = command_find_by_id(&NeoMutt->commands, hook_ids[i]);
107 enum CommandId id = hook_ids[i];
108 bool found_header = false;
109 struct Hook *hook = NULL;
110
111 TAILQ_FOREACH(hook, &Hooks, entries)
112 {
113 if (hook->id != id)
114 continue;
115
116 if (!found_header)
117 {
118 buf_add_printf(buf, "# %s\n", hook_cmd->help);
119 found_header = true;
120 }
121
122 hooks_dump_one(hook, hook_cmd, buf);
123 }
124
125 if (found_header)
126 buf_addstr(buf, "\n");
127 }
128}
129
134static void hooks_dump_index(struct Buffer *buf)
135{
136 if (!IdxFmtHooks)
137 return;
138
139 struct HashWalkState hws = { 0 };
140 struct HashElem *he = NULL;
141 bool found_header = false;
142
143 while ((he = mutt_hash_walk(IdxFmtHooks, &hws)))
144 {
145 const char *name = he->key.strkey;
146 struct HookList *hl = he->data;
147 struct Hook *hook = NULL;
148
149 TAILQ_FOREACH(hook, hl, entries)
150 {
151 if (!found_header)
152 {
153 const struct Command *hook_cmd = command_find_by_id(&NeoMutt->commands,
155 buf_add_printf(buf, "# %s\n", hook_cmd->help);
156 found_header = true;
157 }
158
159 buf_add_printf(buf, "index-format-hook '%s' %s'%s' '%s'\n", name,
160 hook->regex.pat_not ? "! " : "",
161 NONULL(hook->regex.pattern), NONULL(hook->expando->string));
162 }
163 }
164
165 if (found_header)
166 buf_addstr(buf, "\n");
167}
168
173static void hooks_dump_charset(struct Buffer *buf)
174{
175 struct Lookup *l = NULL;
176 struct Buffer *charset = buf_pool_get();
177 struct Buffer *iconv = buf_pool_get();
178
179 TAILQ_FOREACH(l, &Lookups, entries)
180 {
181 if (l->type == MUTT_LOOKUP_CHARSET)
182 {
183 buf_addstr(charset, "charset-hook ");
184 pretty_var(l->regex.pattern, charset);
185 buf_addch(charset, ' ');
186 pretty_var(l->replacement, charset);
187 buf_addch(charset, '\n');
188 }
189 else if (l->type == MUTT_LOOKUP_ICONV)
190 {
191 buf_addstr(iconv, "iconv-hook ");
192 pretty_var(l->regex.pattern, iconv);
193 buf_addch(iconv, ' ');
194 pretty_var(l->replacement, iconv);
195 buf_addch(iconv, '\n');
196 }
197 }
198
199 const struct Command *cmd = NULL;
200 if (!buf_is_empty(charset))
201 {
202 cmd = command_find_by_name(&NeoMutt->commands, "charset-hook");
203 buf_add_printf(buf, "# %s\n", cmd->help);
204 buf_add_printf(buf, "%s\n", buf_string(charset));
205 }
206
207 if (!buf_is_empty(iconv))
208 {
209 cmd = command_find_by_name(&NeoMutt->commands, "iconv-hook");
210 buf_add_printf(buf, "# %s\n", cmd->help);
211 buf_add_printf(buf, "%s\n", buf_string(iconv));
212 }
213
214 buf_pool_release(&iconv);
215 buf_pool_release(&charset);
216}
217
224enum CommandResult parse_hooks(const struct Command *cmd, struct Buffer *line,
225 const struct ParseContext *pc, struct ParseError *pe)
226{
227 struct Buffer *err = pe->message;
228
229 if (MoreArgs(line))
230 {
231 buf_printf(err, _("%s: too many arguments"), cmd->name);
232 return MUTT_CMD_WARNING;
233 }
234
235 if (!StartupComplete)
236 return MUTT_CMD_SUCCESS;
237
238 if (TAILQ_EMPTY(&Hooks))
239 {
240 buf_printf(err, _("%s: No Hooks are configured"), cmd->name);
241 return MUTT_CMD_WARNING;
242 }
243
244 struct Buffer *tempfile = buf_pool_get();
245 buf_mktemp(tempfile);
246
247 FILE *fp = mutt_file_fopen(buf_string(tempfile), "w");
248 if (!fp)
249 {
250 mutt_error(_("Could not create temporary file %s"), buf_string(tempfile));
251 buf_pool_release(&tempfile);
252 return MUTT_CMD_ERROR;
253 }
254
255 struct Buffer *buf = buf_pool_get();
256
258 hooks_dump_index(buf);
260
262 mutt_file_fclose(&fp);
263 buf_pool_release(&buf);
264
265 struct PagerData pdata = { 0 };
266 struct PagerView pview = { &pdata };
267
268 pdata.fname = buf_string(tempfile);
269
270 pview.banner = "hooks";
272 pview.mode = PAGER_MODE_OTHER;
273
274 mutt_do_pager(&pview, NULL);
275 buf_pool_release(&tempfile);
276 return MUTT_CMD_SUCCESS;
277}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:204
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition buffer.c:291
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:241
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:226
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
CommandId
ID of Command.
Definition command.h:58
@ CMD_CLOSE_HOOK
:close-hook
Definition command.h:70
@ CMD_SEND_HOOK
:send-hook
Definition command.h:107
@ CMD_INDEX_FORMAT_HOOK
:index-format-hook
Definition command.h:86
@ CMD_SHUTDOWN_HOOK
:shutdown-hook
Definition command.h:110
@ CMD_FCC_HOOK
:fcc-hook
Definition command.h:75
@ CMD_MESSAGE_HOOK
:message-hook
Definition command.h:94
@ CMD_SEND2_HOOK
:send2-hook
Definition command.h:106
@ CMD_REPLY_HOOK
:reply-hook
Definition command.h:102
@ CMD_STARTUP_HOOK
:startup-hook
Definition command.h:115
@ CMD_SAVE_HOOK
:save-hook
Definition command.h:104
@ CMD_TIMEOUT_HOOK
:timeout-hook
Definition command.h:121
@ CMD_ACCOUNT_HOOK
:account-hook
Definition command.h:60
@ CMD_CRYPT_HOOK
:crypt-hook
Definition command.h:72
@ CMD_MBOX_HOOK
:mbox-hook
Definition command.h:93
@ CMD_FOLDER_HOOK
:folder-hook
Definition command.h:78
@ CMD_OPEN_HOOK
:open-hook
Definition command.h:100
@ CMD_APPEND_HOOK
:append-hook
Definition command.h:64
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
const struct Command * command_find_by_name(const struct CommandArray *ca, const char *name)
Find a NeoMutt Command by its name.
Definition commands.c:171
const struct Command * command_find_by_id(const struct CommandArray *ca, enum CommandId id)
Find a NeoMutt Command by its CommandId.
Definition commands.c:146
NeoMutt Commands.
size_t pretty_var(const char *str, struct Buffer *buf)
Escape and stringify a config item value.
Definition dump.c:87
Convenience wrapper for the config headers.
bool StartupComplete
When the config has been read.
Definition address.c:11
Convenience wrapper for the core headers.
int mutt_do_pager(struct PagerView *pview, struct Email *e)
Display some page-able text to the user (help or attachment)
Definition do_pager.c:122
Parse Expando string.
#define MoreArgs(buf)
Definition extract.h:31
size_t mutt_file_save_str(FILE *fp, const char *str)
Save a string to a file.
Definition file.c:1569
#define mutt_file_fclose(FP)
Definition file.h:139
#define mutt_file_fopen(PATH, MODE)
Definition file.h:138
enum CommandResult parse_hooks(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'hooks' command - Implements Command::parse() -.
Definition dump.c:224
#define mutt_error(...)
Definition logging2.h:94
struct HashElem * mutt_hash_walk(const struct HashTable *table, struct HashWalkState *state)
Iterate through all the HashElem's in a Hash Table.
Definition hash.c:491
User-defined Hooks.
static void hooks_dump_charset(struct Buffer *buf)
Dump the Charset Hooks.
Definition dump.c:173
static void hooks_dump_index(struct Buffer *buf)
Dump the Index Format Hooks.
Definition dump.c:134
static void hooks_dump_one(struct Hook *hook, const struct Command *cmd, struct Buffer *buf)
Dump a single hook to the buffer.
Definition dump.c:50
static void hooks_dump_simple(struct Buffer *buf)
Dump the simple Hooks.
Definition dump.c:94
struct HashTable * IdxFmtHooks
All Index Format hooks.
Definition parse.c:52
struct HookList Hooks
All simple hooks, e.g. CMD_FOLDER_HOOK.
Definition parse.c:49
Parse user-defined Hooks.
#define countof(x)
Definition memory.h:49
struct LookupList Lookups
Lookup table of preferred character set names.
Definition charset.c:69
@ MUTT_LOOKUP_ICONV
Character set conversion.
Definition charset.h:63
@ MUTT_LOOKUP_CHARSET
Alias for another character set.
Definition charset.h:62
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
Many unsorted constants and some structs.
void pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using '~' or '='.
Definition muttlib.c:427
Some miscellaneous functions.
GUI display a file/email/help in a viewport with paging.
#define MUTT_PAGER_NO_FLAGS
No flags are set.
Definition lib.h:62
@ PAGER_MODE_OTHER
Pager is invoked via 3rd path. Non-email content is likely to be shown.
Definition lib.h:142
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 TAILQ_FOREACH(var, head, field)
Definition queue.h:782
#define TAILQ_EMPTY(head)
Definition queue.h:778
#define NONULL(x)
Definition string2.h:44
String manipulation buffer.
Definition buffer.h:36
const char * help
One-line description of the Command.
Definition command.h:180
const char * name
Name of the Command.
Definition command.h:159
const char * string
Pointer to the parsed string.
Definition expando.h:42
The item stored in a Hash Table.
Definition hash.h:44
union HashKey key
Key representing the data.
Definition hash.h:46
void * data
User-supplied data.
Definition hash.h:47
Cursor to iterate through a Hash Table.
Definition hash.h:134
A list of user hooks.
Definition hook.h:33
struct Regex regex
Regular expression.
Definition hook.h:35
char * command
Filename, command or pattern to execute.
Definition hook.h:36
struct Expando * expando
Used for format hooks.
Definition hook.h:39
enum CommandId id
Hook CommandId, e.g. CMD_FOLDER_HOOK.
Definition hook.h:34
Regex to String lookup table.
Definition charset.h:75
char * replacement
Alternative charset to use.
Definition charset.h:78
enum LookupType type
Lookup type.
Definition charset.h:76
struct Regex regex
Regular expression.
Definition charset.h:77
Container for Accounts, Notifications.
Definition neomutt.h:41
struct CommandArray commands
NeoMutt commands.
Definition neomutt.h:53
Data to be displayed by PagerView.
Definition lib.h:161
const char * fname
Name of the file to read.
Definition lib.h:165
Paged view into some data.
Definition lib.h:172
struct PagerData * pdata
Data that pager displays. NOTNULL.
Definition lib.h:173
enum PagerMode mode
Pager mode.
Definition lib.h:174
PagerFlags flags
Additional settings to tweak pager's function.
Definition lib.h:175
const char * banner
Title to display in status bar.
Definition lib.h:176
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
char * pattern
printable version
Definition regex3.h:86
bool pat_not
do not match
Definition regex3.h:88
#define buf_mktemp(buf)
Definition tmp.h:33
const char * strkey
String key.
Definition hash.h:36