NeoMutt  2025-01-09-117-gace867
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
mutt_lua.c
Go to the documentation of this file.
1
34#ifndef LUA_COMPAT_ALL
35#define LUA_COMPAT_ALL
36#endif
37#ifndef LUA_COMPAT_5_1
38#define LUA_COMPAT_5_1
39#endif
40
41#include "config.h"
42#include <lauxlib.h>
43#include <lua.h>
44#include <lualib.h>
45#include <stdbool.h>
46#include <stdint.h>
47#include <stdio.h>
48#include "mutt/lib.h"
49#include "config/lib.h"
50#include "core/lib.h"
51#include "mutt_lua.h"
52#include "parse/lib.h"
53#include "muttlib.h"
54
56static lua_State *LuaState = NULL;
57
61static const struct Command LuaCommands[] = {
62 // clang-format off
63 { "lua", mutt_lua_parse, 0 },
64 { "lua-source", mutt_lua_source_file, 0 },
65 { NULL, NULL, 0 },
66 // clang-format on
67};
68
74static int handle_panic(lua_State *l)
75{
76 mutt_debug(LL_DEBUG1, "lua runtime panic: %s\n", lua_tostring(l, -1));
77 mutt_error("Lua runtime panic: %s", lua_tostring(l, -1));
78 lua_pop(l, 1);
79 return -1;
80}
81
87static int handle_error(lua_State *l)
88{
89 mutt_debug(LL_DEBUG1, "lua runtime error: %s\n", lua_tostring(l, -1));
90 mutt_error("Lua runtime error: %s", lua_tostring(l, -1));
91 lua_pop(l, 1);
92 return -1;
93}
94
101static int lua_mutt_call(lua_State *l)
102{
103 mutt_debug(LL_DEBUG2, " * lua_mutt_call()\n");
104 struct Buffer *err = buf_pool_get();
105 struct Buffer *token = buf_pool_get();
106 struct Buffer *buf = buf_pool_get();
107 const struct Command *cmd = NULL;
108 int rc = 0;
109
110 if (lua_gettop(l) == 0)
111 {
112 luaL_error(l, "Error command argument required.");
113 return -1;
114 }
115
116 cmd = commands_get(&NeoMutt->commands, lua_tostring(l, 1));
117 if (!cmd)
118 {
119 luaL_error(l, "Error command %s not found.", lua_tostring(l, 1));
120 return -1;
121 }
122
123 for (int i = 2; i <= lua_gettop(l); i++)
124 {
125 buf_addstr(buf, lua_tostring(l, i));
126 buf_addch(buf, ' ');
127 }
128 buf_seek(buf, 0);
129
130 if (cmd->parse(token, buf, cmd->data, err))
131 {
132 luaL_error(l, "NeoMutt error: %s", buf_string(err));
133 rc = -1;
134 }
135 else
136 {
137 if (!lua_pushstring(l, buf_string(err)))
138 handle_error(l);
139 else
140 rc++;
141 }
142
143 buf_pool_release(&buf);
144 buf_pool_release(&token);
145 buf_pool_release(&err);
146 return rc;
147}
148
155static int lua_mutt_set(lua_State *l)
156{
157 const char *param = lua_tostring(l, -2);
158 mutt_debug(LL_DEBUG2, " * lua_mutt_set(%s)\n", param);
159
160 struct Buffer *err = buf_pool_get();
161 struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
162 if (!he)
163 {
164 // In case it is a my_var, we have to create it
165 if (mutt_str_startswith(param, "my_"))
166 {
167 struct ConfigDef my_cdef = { 0 };
168 my_cdef.name = param;
169 my_cdef.type = DT_MYVAR;
170 he = cs_create_variable(NeoMutt->sub->cs, &my_cdef, err);
171 if (!he)
172 return -1;
173 }
174 else
175 {
176 luaL_error(l, "NeoMutt parameter not found %s", param);
177 return -1;
178 }
179 }
180
181 struct ConfigDef *cdef = he->data;
182
183 int rc = 0;
184
185 switch (CONFIG_TYPE(cdef->type))
186 {
187 case DT_ADDRESS:
188 case DT_ENUM:
189 case DT_MBTABLE:
190 case DT_MYVAR:
191 case DT_PATH:
192 case DT_REGEX:
193 case DT_SLIST:
194 case DT_SORT:
195 case DT_STRING:
196 {
197 const char *value = lua_tostring(l, -1);
198 size_t val_size = lua_rawlen(l, -1);
199 struct Buffer *value_buf = buf_pool_get();
200 buf_strcpy_n(value_buf, value, val_size);
201 if (CONFIG_TYPE(he->type) == DT_PATH)
202 buf_expand_path(value_buf);
203
204 int rv = cs_subset_he_string_set(NeoMutt->sub, he, buf_string(value_buf), err);
205 buf_pool_release(&value_buf);
206 if (CSR_RESULT(rv) != CSR_SUCCESS)
207 rc = -1;
208 break;
209 }
210 case DT_NUMBER:
211 case DT_QUAD:
212 {
213 const intptr_t value = lua_tointeger(l, -1);
214 int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, err);
215 if (CSR_RESULT(rv) != CSR_SUCCESS)
216 rc = -1;
217 break;
218 }
219 case DT_BOOL:
220 {
221 const intptr_t value = lua_toboolean(l, -1);
222 int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, err);
223 if (CSR_RESULT(rv) != CSR_SUCCESS)
224 rc = -1;
225 break;
226 }
227 default:
228 luaL_error(l, "Unsupported NeoMutt parameter type %d for %s",
229 CONFIG_TYPE(cdef->type), param);
230 rc = -1;
231 break;
232 }
233
234 buf_pool_release(&err);
235 return rc;
236}
237
244static int lua_mutt_get(lua_State *l)
245{
246 const char *param = lua_tostring(l, -1);
247 mutt_debug(LL_DEBUG2, " * lua_mutt_get(%s)\n", param);
248
249 struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
250 if (!he)
251 {
252 mutt_debug(LL_DEBUG2, " * error\n");
253 luaL_error(l, "NeoMutt parameter not found %s", param);
254 return -1;
255 }
256
257 struct ConfigDef *cdef = he->data;
258
259 switch (CONFIG_TYPE(cdef->type))
260 {
261 case DT_ADDRESS:
262 case DT_ENUM:
263 case DT_MBTABLE:
264 case DT_MYVAR:
265 case DT_REGEX:
266 case DT_SLIST:
267 case DT_SORT:
268 case DT_STRING:
269 {
270 struct Buffer *value = buf_pool_get();
271 int rc = cs_subset_he_string_get(NeoMutt->sub, he, value);
272 if (CSR_RESULT(rc) != CSR_SUCCESS)
273 {
274 buf_pool_release(&value);
275 return -1;
276 }
277
278 struct Buffer *escaped = buf_pool_get();
279 escape_string(escaped, buf_string(value));
280 lua_pushstring(l, buf_string(escaped));
281 buf_pool_release(&value);
282 buf_pool_release(&escaped);
283 return 1;
284 }
285 case DT_QUAD:
286 lua_pushinteger(l, (unsigned char) cdef->var);
287 return 1;
288 case DT_NUMBER:
289 lua_pushinteger(l, (signed short) cdef->var);
290 return 1;
291 case DT_BOOL:
292 lua_pushboolean(l, (bool) cdef->var);
293 return 1;
294 default:
295 luaL_error(l, "NeoMutt parameter type %d unknown for %s", cdef->type, param);
296 return -1;
297 }
298}
299
306static int lua_mutt_enter(lua_State *l)
307{
308 mutt_debug(LL_DEBUG2, " * lua_mutt_enter()\n");
309 struct Buffer *err = buf_pool_get();
310 char *buf = mutt_str_dup(lua_tostring(l, -1));
311 int rc = 0;
312
313 if (parse_rc_line(buf, err))
314 {
315 luaL_error(l, "NeoMutt error: %s", buf_string(err));
316 rc = -1;
317 }
318 else
319 {
320 if (!lua_pushstring(l, buf_string(err)))
321 handle_error(l);
322 else
323 rc++;
324 }
325
326 FREE(&buf);
327 buf_pool_release(&err);
328
329 return rc;
330}
331
337static int lua_mutt_message(lua_State *l)
338{
339 mutt_debug(LL_DEBUG2, " * lua_mutt_message()\n");
340 const char *msg = lua_tostring(l, -1);
341 if (msg)
342 mutt_message("%s", msg);
343 return 0;
344}
345
351static int lua_mutt_error(lua_State *l)
352{
353 mutt_debug(LL_DEBUG2, " * lua_mutt_error()\n");
354 const char *msg = lua_tostring(l, -1);
355 if (msg)
356 mutt_error("%s", msg);
357 return 0;
358}
359
365static void lua_expose_command(lua_State *l, const struct Command *cmd)
366{
367 char buf[1024] = { 0 };
368 snprintf(buf, sizeof(buf), "mutt.command.%s = function (...); mutt.call('%s', ...); end",
369 cmd->name, cmd->name);
370 (void) luaL_dostring(l, buf);
371}
372
382static const luaL_Reg LuaMuttCommands[] = {
383 // clang-format off
384 { "set", lua_mutt_set },
385 { "get", lua_mutt_get },
386 { "call", lua_mutt_call },
387 { "enter", lua_mutt_enter },
388 { "print", lua_mutt_message },
389 { "message", lua_mutt_message },
390 { "error", lua_mutt_error },
391 { NULL, NULL },
392 // clang-format on
393};
394
400static int luaopen_mutt_decl(lua_State *l)
401{
402 mutt_debug(LL_DEBUG2, " * luaopen_mutt()\n");
403 luaL_newlib(l, LuaMuttCommands);
404 int lib_idx = lua_gettop(l);
405
406 // clang-format off
407 lua_pushstring(l, "VERSION"); lua_pushstring(l, mutt_make_version()); lua_settable(l, lib_idx);;
408 lua_pushstring(l, "QUAD_YES"); lua_pushinteger(l, MUTT_YES); lua_settable(l, lib_idx);;
409 lua_pushstring(l, "QUAD_NO"); lua_pushinteger(l, MUTT_NO); lua_settable(l, lib_idx);;
410 lua_pushstring(l, "QUAD_ASKYES"); lua_pushinteger(l, MUTT_ASKYES); lua_settable(l, lib_idx);;
411 lua_pushstring(l, "QUAD_ASKNO"); lua_pushinteger(l, MUTT_ASKNO); lua_settable(l, lib_idx);;
412 // clang-format on
413
414 return 1;
415}
416
421static void luaopen_mutt(lua_State *l)
422{
423 luaL_requiref(l, "mutt", luaopen_mutt_decl, 1);
424 (void) luaL_dostring(l, "mutt.command = {}");
425
426 const struct Command **cp = NULL;
428 {
429 const struct Command *cmd = *cp;
430
431 lua_expose_command(l, cmd);
432 }
433}
434
440static bool lua_init(lua_State **l)
441{
442 if (!l)
443 return false;
444 if (*l)
445 return true;
446
447 mutt_debug(LL_DEBUG2, " * lua_init()\n");
448 *l = luaL_newstate();
449
450 if (!*l)
451 {
452 mutt_error(_("Error: Couldn't load the lua interpreter"));
453 return false;
454 }
455
456 lua_atpanic(*l, handle_panic);
457
458 /* load various Lua libraries */
459 luaL_openlibs(*l);
460 luaopen_mutt(*l);
461
462 return true;
463}
464
469{
471}
472
476enum CommandResult mutt_lua_parse(struct Buffer *buf, struct Buffer *s,
477 intptr_t data, struct Buffer *err)
478{
480 mutt_debug(LL_DEBUG2, " * mutt_lua_parse(%s)\n", buf->data);
481
482 if (luaL_dostring(LuaState, s->dptr))
483 {
484 mutt_debug(LL_DEBUG2, " * %s -> failure\n", s->dptr);
485 buf_printf(err, "%s: %s", s->dptr, lua_tostring(LuaState, -1));
486 /* pop error message from the stack */
487 lua_pop(LuaState, 1);
488 return MUTT_CMD_ERROR;
489 }
490 mutt_debug(LL_DEBUG2, " * %s -> success\n", s->dptr);
491 buf_reset(s); // Clear the rest of the line
492 return MUTT_CMD_SUCCESS;
493}
494
499 intptr_t data, struct Buffer *err)
500{
501 mutt_debug(LL_DEBUG2, " * mutt_lua_source()\n");
502
504
505 if (parse_extract_token(buf, s, TOKEN_NO_FLAGS) != 0)
506 {
507 buf_printf(err, _("source: error at %s"), s->dptr);
508 return MUTT_CMD_ERROR;
509 }
510 if (MoreArgs(s))
511 {
512 buf_printf(err, _("%s: too many arguments"), "source");
513 return MUTT_CMD_WARNING;
514 }
515
516 struct Buffer *path = buf_pool_get();
517 buf_copy(path, buf);
518 buf_expand_path(path);
519
520 if (luaL_dofile(LuaState, buf_string(path)))
521 {
522 mutt_error(_("Couldn't source lua source: %s"), lua_tostring(LuaState, -1));
523 lua_pop(LuaState, 1);
524 buf_pool_release(&path);
525 return MUTT_CMD_ERROR;
526 }
527
528 buf_pool_release(&path);
529 return MUTT_CMD_SUCCESS;
530}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:214
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
void buf_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition: buffer.c:622
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
size_t buf_strcpy_n(struct Buffer *buf, const char *s, size_t len)
Copy a string into a Buffer.
Definition: buffer.c:416
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_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition: buffer.c:601
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:35
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:38
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:36
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition: command.h:37
size_t escape_string(struct Buffer *buf, const char *src)
Write a string to a buffer, escaping special characters.
Definition: dump.c:48
Convenience wrapper for the config headers.
struct HashElem * cs_create_variable(const struct ConfigSet *cs, struct ConfigDef *cdef, struct Buffer *err)
Create and register one config item.
Definition: set.c:326
#define CSR_RESULT(x)
Definition: set.h:50
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:33
bool commands_register(struct CommandArray *ca, const struct Command *cmds)
Add commands to Commands array.
Definition: command.c:51
const struct Command * commands_get(struct CommandArray *ca, const char *name)
Get a Command by its name.
Definition: command.c:82
Convenience wrapper for the core headers.
int parse_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: extract.c:49
#define MoreArgs(buf)
Definition: extract.h:32
#define TOKEN_NO_FLAGS
No flags are set.
Definition: extract.h:46
enum CommandResult mutt_lua_source_file(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'lua-source' command - Implements Command::parse() -.
Definition: mutt_lua.c:498
enum CommandResult mutt_lua_parse(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'lua' command - Implements Command::parse() -.
Definition: mutt_lua.c:476
#define mutt_error(...)
Definition: logging2.h:93
#define mutt_message(...)
Definition: logging2.h:92
#define mutt_debug(LEVEL,...)
Definition: logging2.h:90
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:45
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:44
#define FREE(x)
Definition: memory.h:55
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:254
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:231
static int lua_mutt_enter(lua_State *l)
Execute NeoMutt config from Lua.
Definition: mutt_lua.c:306
static int lua_mutt_message(lua_State *l)
Display a message in Neomutt.
Definition: mutt_lua.c:337
static void luaopen_mutt(lua_State *l)
Expose a 'Mutt' object to the Lua interpreter.
Definition: mutt_lua.c:421
void mutt_lua_init(void)
Setup feature commands.
Definition: mutt_lua.c:468
static void lua_expose_command(lua_State *l, const struct Command *cmd)
Expose a NeoMutt command to the Lua interpreter.
Definition: mutt_lua.c:365
static bool lua_init(lua_State **l)
Initialise a Lua State.
Definition: mutt_lua.c:440
static int handle_panic(lua_State *l)
Handle a panic in the Lua interpreter.
Definition: mutt_lua.c:74
static int lua_mutt_call(lua_State *l)
Call a NeoMutt command by name.
Definition: mutt_lua.c:101
static int handle_error(lua_State *l)
Handle an error in the Lua interpreter.
Definition: mutt_lua.c:87
static int lua_mutt_get(lua_State *l)
Get a NeoMutt variable.
Definition: mutt_lua.c:244
static lua_State * LuaState
Global Lua State.
Definition: mutt_lua.c:56
static const luaL_Reg LuaMuttCommands[]
List of Lua commands to register.
Definition: mutt_lua.c:382
static int lua_mutt_error(lua_State *l)
Display an error in Neomutt.
Definition: mutt_lua.c:351
static const struct Command LuaCommands[]
List of NeoMutt commands to register.
Definition: mutt_lua.c:61
static int luaopen_mutt_decl(lua_State *l)
Declare some NeoMutt types to the Lua interpreter.
Definition: mutt_lua.c:400
static int lua_mutt_set(lua_State *l)
Set a NeoMutt variable.
Definition: mutt_lua.c:155
Integrated Lua scripting.
const char * mutt_make_version(void)
Generate the NeoMutt version string.
Definition: muttlib.c:857
void buf_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:315
Some miscellaneous functions.
Text parsing functions.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
@ MUTT_ASKNO
Ask the user, defaulting to 'No'.
Definition: quad.h:40
@ MUTT_NO
User answered 'No', or assume 'No'.
Definition: quad.h:38
@ MUTT_ASKYES
Ask the user, defaulting to 'Yes'.
Definition: quad.h:41
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition: quad.h:39
enum CommandResult parse_rc_line(const char *line, struct Buffer *err)
Parse a line of user config.
Definition: rc.c:109
String manipulation buffer.
Definition: buffer.h:36
char * dptr
Current read/write position.
Definition: buffer.h:38
char * data
Pointer to data.
Definition: buffer.h:37
enum CommandResult(* parse)(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Definition: command.h:64
intptr_t data
Data or flags to pass to the command.
Definition: command.h:66
const char * name
Name of the command.
Definition: command.h:51
Definition: set.h:62
const char * name
User-visible name.
Definition: set.h:63
intptr_t var
Storage for the variable.
Definition: set.h:82
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:64
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:50
The item stored in a Hash Table.
Definition: hash.h:44
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition: hash.h:45
void * data
User-supplied data.
Definition: hash.h:47
Container for Accounts, Notifications.
Definition: neomutt.h:43
struct CommandArray commands
NeoMutt commands.
Definition: neomutt.h:51
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:47
int cs_subset_he_string_get(const struct ConfigSubset *sub, struct HashElem *he, struct Buffer *result)
Get a config item as a string.
Definition: subset.c:334
int cs_subset_he_native_set(const struct ConfigSubset *sub, struct HashElem *he, intptr_t value, struct Buffer *err)
Natively set the value of a HashElem config item.
Definition: subset.c:277
int cs_subset_he_string_set(const struct ConfigSubset *sub, struct HashElem *he, const char *value, struct Buffer *err)
Set a config item by string.
Definition: subset.c:366
struct HashElem * cs_subset_lookup(const struct ConfigSubset *sub, const char *name)
Find an inherited config item.
Definition: subset.c:189
#define CONFIG_TYPE(t)
Definition: types.h:49
@ DT_NUMBER
a number
Definition: types.h:38
@ DT_SLIST
a list of strings
Definition: types.h:42
@ DT_BOOL
boolean option
Definition: types.h:32
@ DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:40
@ DT_STRING
a string
Definition: types.h:44
@ DT_SORT
sorting methods
Definition: types.h:43
@ DT_MYVAR
a user-defined variable (my_foo)
Definition: types.h:37
@ DT_MBTABLE
multibyte char table
Definition: types.h:36
@ DT_ADDRESS
e-mail address
Definition: types.h:31
@ DT_ENUM
an enumeration
Definition: types.h:33
@ DT_REGEX
regular expressions
Definition: types.h:41
@ DT_PATH
a path to a file/directory
Definition: types.h:39