NeoMutt  2024-12-12-14-g7b49f7
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 // clang-format on
66};
67
73static int handle_panic(lua_State *l)
74{
75 mutt_debug(LL_DEBUG1, "lua runtime panic: %s\n", lua_tostring(l, -1));
76 mutt_error("Lua runtime panic: %s", lua_tostring(l, -1));
77 lua_pop(l, 1);
78 return -1;
79}
80
86static int handle_error(lua_State *l)
87{
88 mutt_debug(LL_DEBUG1, "lua runtime error: %s\n", lua_tostring(l, -1));
89 mutt_error("Lua runtime error: %s", lua_tostring(l, -1));
90 lua_pop(l, 1);
91 return -1;
92}
93
100static int lua_mutt_call(lua_State *l)
101{
102 mutt_debug(LL_DEBUG2, " * lua_mutt_call()\n");
103 struct Buffer *err = buf_pool_get();
104 struct Buffer *token = buf_pool_get();
105 struct Buffer *buf = buf_pool_get();
106 const struct Command *cmd = NULL;
107 int rc = 0;
108
109 if (lua_gettop(l) == 0)
110 {
111 luaL_error(l, "Error command argument required.");
112 return -1;
113 }
114
115 cmd = command_get(lua_tostring(l, 1));
116 if (!cmd)
117 {
118 luaL_error(l, "Error command %s not found.", lua_tostring(l, 1));
119 return -1;
120 }
121
122 for (int i = 2; i <= lua_gettop(l); i++)
123 {
124 buf_addstr(buf, lua_tostring(l, i));
125 buf_addch(buf, ' ');
126 }
127
128 if (cmd->parse(token, buf, cmd->data, err))
129 {
130 luaL_error(l, "NeoMutt error: %s", buf_string(err));
131 rc = -1;
132 }
133 else
134 {
135 if (!lua_pushstring(l, buf_string(err)))
136 handle_error(l);
137 else
138 rc++;
139 }
140
141 buf_pool_release(&buf);
142 buf_pool_release(&token);
143 buf_pool_release(&err);
144 return rc;
145}
146
153static int lua_mutt_set(lua_State *l)
154{
155 const char *param = lua_tostring(l, -2);
156 mutt_debug(LL_DEBUG2, " * lua_mutt_set(%s)\n", param);
157
158 struct Buffer *err = buf_pool_get();
159 struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
160 if (!he)
161 {
162 // In case it is a my_var, we have to create it
163 if (mutt_str_startswith(param, "my_"))
164 {
165 struct ConfigDef my_cdef = { 0 };
166 my_cdef.name = param;
167 my_cdef.type = DT_MYVAR;
168 he = cs_create_variable(NeoMutt->sub->cs, &my_cdef, err);
169 if (!he)
170 return -1;
171 }
172 else
173 {
174 luaL_error(l, "NeoMutt parameter not found %s", param);
175 return -1;
176 }
177 }
178
179 struct ConfigDef *cdef = he->data;
180
181 int rc = 0;
182
183 switch (DTYPE(cdef->type))
184 {
185 case DT_ADDRESS:
186 case DT_ENUM:
187 case DT_MBTABLE:
188 case DT_MYVAR:
189 case DT_PATH:
190 case DT_REGEX:
191 case DT_SLIST:
192 case DT_SORT:
193 case DT_STRING:
194 {
195 const char *value = lua_tostring(l, -1);
196 size_t val_size = lua_rawlen(l, -1);
197 struct Buffer *value_buf = buf_pool_get();
198 buf_strcpy_n(value_buf, value, val_size);
199 if (DTYPE(he->type) == DT_PATH)
200 buf_expand_path(value_buf);
201
202 int rv = cs_subset_he_string_set(NeoMutt->sub, he, buf_string(value_buf), err);
203 buf_pool_release(&value_buf);
204 if (CSR_RESULT(rv) != CSR_SUCCESS)
205 rc = -1;
206 break;
207 }
208 case DT_NUMBER:
209 case DT_QUAD:
210 {
211 const intptr_t value = lua_tointeger(l, -1);
212 int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, err);
213 if (CSR_RESULT(rv) != CSR_SUCCESS)
214 rc = -1;
215 break;
216 }
217 case DT_BOOL:
218 {
219 const intptr_t value = lua_toboolean(l, -1);
220 int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, err);
221 if (CSR_RESULT(rv) != CSR_SUCCESS)
222 rc = -1;
223 break;
224 }
225 default:
226 luaL_error(l, "Unsupported NeoMutt parameter type %d for %s", DTYPE(cdef->type), param);
227 rc = -1;
228 break;
229 }
230
231 buf_pool_release(&err);
232 return rc;
233}
234
241static int lua_mutt_get(lua_State *l)
242{
243 const char *param = lua_tostring(l, -1);
244 mutt_debug(LL_DEBUG2, " * lua_mutt_get(%s)\n", param);
245
246 struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
247 if (!he)
248 {
249 mutt_debug(LL_DEBUG2, " * error\n");
250 luaL_error(l, "NeoMutt parameter not found %s", param);
251 return -1;
252 }
253
254 struct ConfigDef *cdef = he->data;
255
256 switch (DTYPE(cdef->type))
257 {
258 case DT_ADDRESS:
259 case DT_ENUM:
260 case DT_MBTABLE:
261 case DT_MYVAR:
262 case DT_REGEX:
263 case DT_SLIST:
264 case DT_SORT:
265 case DT_STRING:
266 {
267 struct Buffer *value = buf_pool_get();
268 int rc = cs_subset_he_string_get(NeoMutt->sub, he, value);
269 if (CSR_RESULT(rc) != CSR_SUCCESS)
270 {
271 buf_pool_release(&value);
272 return -1;
273 }
274
275 struct Buffer *escaped = buf_pool_get();
276 escape_string(escaped, buf_string(value));
277 lua_pushstring(l, buf_string(escaped));
278 buf_pool_release(&value);
279 buf_pool_release(&escaped);
280 return 1;
281 }
282 case DT_QUAD:
283 lua_pushinteger(l, (unsigned char) cdef->var);
284 return 1;
285 case DT_NUMBER:
286 lua_pushinteger(l, (signed short) cdef->var);
287 return 1;
288 case DT_BOOL:
289 lua_pushboolean(l, (bool) cdef->var);
290 return 1;
291 default:
292 luaL_error(l, "NeoMutt parameter type %d unknown for %s", cdef->type, param);
293 return -1;
294 }
295}
296
303static int lua_mutt_enter(lua_State *l)
304{
305 mutt_debug(LL_DEBUG2, " * lua_mutt_enter()\n");
306 struct Buffer *err = buf_pool_get();
307 char *buf = mutt_str_dup(lua_tostring(l, -1));
308 int rc = 0;
309
310 if (parse_rc_line(buf, err))
311 {
312 luaL_error(l, "NeoMutt error: %s", buf_string(err));
313 rc = -1;
314 }
315 else
316 {
317 if (!lua_pushstring(l, buf_string(err)))
318 handle_error(l);
319 else
320 rc++;
321 }
322
323 FREE(&buf);
324 buf_pool_release(&err);
325
326 return rc;
327}
328
334static int lua_mutt_message(lua_State *l)
335{
336 mutt_debug(LL_DEBUG2, " * lua_mutt_message()\n");
337 const char *msg = lua_tostring(l, -1);
338 if (msg)
339 mutt_message("%s", msg);
340 return 0;
341}
342
348static int lua_mutt_error(lua_State *l)
349{
350 mutt_debug(LL_DEBUG2, " * lua_mutt_error()\n");
351 const char *msg = lua_tostring(l, -1);
352 if (msg)
353 mutt_error("%s", msg);
354 return 0;
355}
356
362static void lua_expose_command(lua_State *l, const struct Command *cmd)
363{
364 char buf[1024] = { 0 };
365 snprintf(buf, sizeof(buf), "mutt.command.%s = function (...); mutt.call('%s', ...); end",
366 cmd->name, cmd->name);
367 (void) luaL_dostring(l, buf);
368}
369
379static const luaL_Reg LuaMuttCommands[] = {
380 // clang-format off
381 { "set", lua_mutt_set },
382 { "get", lua_mutt_get },
383 { "call", lua_mutt_call },
384 { "enter", lua_mutt_enter },
385 { "print", lua_mutt_message },
386 { "message", lua_mutt_message },
387 { "error", lua_mutt_error },
388 { NULL, NULL },
389 // clang-format on
390};
391
397static int luaopen_mutt_decl(lua_State *l)
398{
399 mutt_debug(LL_DEBUG2, " * luaopen_mutt()\n");
400 luaL_newlib(l, LuaMuttCommands);
401 int lib_idx = lua_gettop(l);
402
403 // clang-format off
404 lua_pushstring(l, "VERSION"); lua_pushstring(l, mutt_make_version()); lua_settable(l, lib_idx);;
405 lua_pushstring(l, "QUAD_YES"); lua_pushinteger(l, MUTT_YES); lua_settable(l, lib_idx);;
406 lua_pushstring(l, "QUAD_NO"); lua_pushinteger(l, MUTT_NO); lua_settable(l, lib_idx);;
407 lua_pushstring(l, "QUAD_ASKYES"); lua_pushinteger(l, MUTT_ASKYES); lua_settable(l, lib_idx);;
408 lua_pushstring(l, "QUAD_ASKNO"); lua_pushinteger(l, MUTT_ASKNO); lua_settable(l, lib_idx);;
409 // clang-format on
410
411 return 1;
412}
413
418static void luaopen_mutt(lua_State *l)
419{
420 luaL_requiref(l, "mutt", luaopen_mutt_decl, 1);
421 (void) luaL_dostring(l, "mutt.command = {}");
422
423 struct Command *c = NULL;
424 for (size_t i = 0, size = commands_array(&c); i < size; i++)
425 {
426 lua_expose_command(l, &c[i]);
427 }
428}
429
435static bool lua_init(lua_State **l)
436{
437 if (!l)
438 return false;
439 if (*l)
440 return true;
441
442 mutt_debug(LL_DEBUG2, " * lua_init()\n");
443 *l = luaL_newstate();
444
445 if (!*l)
446 {
447 mutt_error(_("Error: Couldn't load the lua interpreter"));
448 return false;
449 }
450
451 lua_atpanic(*l, handle_panic);
452
453 /* load various Lua libraries */
454 luaL_openlibs(*l);
455 luaopen_mutt(*l);
456
457 return true;
458}
459
464{
466}
467
471enum CommandResult mutt_lua_parse(struct Buffer *buf, struct Buffer *s,
472 intptr_t data, struct Buffer *err)
473{
475 mutt_debug(LL_DEBUG2, " * mutt_lua_parse(%s)\n", buf->data);
476
477 if (luaL_dostring(LuaState, s->dptr))
478 {
479 mutt_debug(LL_DEBUG2, " * %s -> failure\n", s->dptr);
480 buf_printf(err, "%s: %s", s->dptr, lua_tostring(LuaState, -1));
481 /* pop error message from the stack */
482 lua_pop(LuaState, 1);
483 return MUTT_CMD_ERROR;
484 }
485 mutt_debug(LL_DEBUG2, " * %s -> success\n", s->dptr);
486 buf_reset(s); // Clear the rest of the line
487 return MUTT_CMD_SUCCESS;
488}
489
494 intptr_t data, struct Buffer *err)
495{
496 mutt_debug(LL_DEBUG2, " * mutt_lua_source()\n");
497
499
500 if (parse_extract_token(buf, s, TOKEN_NO_FLAGS) != 0)
501 {
502 buf_printf(err, _("source: error at %s"), s->dptr);
503 return MUTT_CMD_ERROR;
504 }
505 if (MoreArgs(s))
506 {
507 buf_printf(err, _("%s: too many arguments"), "source");
508 return MUTT_CMD_WARNING;
509 }
510
511 struct Buffer *path = buf_pool_get();
512 buf_copy(path, buf);
513 buf_expand_path(path);
514
515 if (luaL_dofile(LuaState, buf_string(path)))
516 {
517 mutt_error(_("Couldn't source lua source: %s"), lua_tostring(LuaState, -1));
518 lua_pop(LuaState, 1);
519 buf_pool_release(&path);
520 return MUTT_CMD_ERROR;
521 }
522
523 buf_pool_release(&path);
524 return MUTT_CMD_SUCCESS;
525}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
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:36
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:39
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:37
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition: command.h:38
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:318
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
void commands_register(const struct Command *cmds, const size_t num_cmds)
Add commands to Commands array.
Definition: command.c:53
size_t commands_array(struct Command **first)
Get Commands array.
Definition: command.c:75
struct Command * command_get(const char *s)
Get a Command by its name.
Definition: command.c:87
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:50
#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:493
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:471
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_message(...)
Definition: logging2.h:91
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
#define FREE(x)
Definition: memory.h:55
#define mutt_array_size(x)
Definition: memory.h:38
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:253
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:230
static int lua_mutt_enter(lua_State *l)
Execute NeoMutt config from Lua.
Definition: mutt_lua.c:303
static int lua_mutt_message(lua_State *l)
Display a message in Neomutt.
Definition: mutt_lua.c:334
static void luaopen_mutt(lua_State *l)
Expose a 'Mutt' object to the Lua interpreter.
Definition: mutt_lua.c:418
void mutt_lua_init(void)
Setup feature commands.
Definition: mutt_lua.c:463
static void lua_expose_command(lua_State *l, const struct Command *cmd)
Expose a NeoMutt command to the Lua interpreter.
Definition: mutt_lua.c:362
static bool lua_init(lua_State **l)
Initialise a Lua State.
Definition: mutt_lua.c:435
static int handle_panic(lua_State *l)
Handle a panic in the Lua interpreter.
Definition: mutt_lua.c:73
static int lua_mutt_call(lua_State *l)
Call a NeoMutt command by name.
Definition: mutt_lua.c:100
static int handle_error(lua_State *l)
Handle an error in the Lua interpreter.
Definition: mutt_lua.c:86
static int lua_mutt_get(lua_State *l)
Get a NeoMutt variable.
Definition: mutt_lua.c:241
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:379
static int lua_mutt_error(lua_State *l)
Display an error in Neomutt.
Definition: mutt_lua.c:348
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:397
static int lua_mutt_set(lua_State *l)
Set a NeoMutt variable.
Definition: mutt_lua.c:153
Integrated Lua scripting.
const char * mutt_make_version(void)
Generate the NeoMutt version string.
Definition: muttlib.c:858
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:104
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:65
intptr_t data
Data or flags to pass to the command.
Definition: command.h:67
const char * name
Name of the command.
Definition: command.h:52
Definition: set.h:64
const char * name
User-visible name.
Definition: set.h:65
intptr_t var
Storage for the variable.
Definition: set.h:85
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:66
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
The item stored in a Hash Table.
Definition: hash.h:43
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition: hash.h:44
void * data
User-supplied data.
Definition: hash.h:46
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
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:332
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:275
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:364
struct HashElem * cs_subset_lookup(const struct ConfigSubset *sub, const char *name)
Find an inherited config item.
Definition: subset.c:187
#define DTYPE(t)
Definition: types.h:50
@ DT_NUMBER
a number
Definition: types.h:39
@ DT_SLIST
a list of strings
Definition: types.h:43
@ DT_BOOL
boolean option
Definition: types.h:32
@ DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:41
@ DT_STRING
a string
Definition: types.h:45
@ DT_SORT
sorting methods
Definition: types.h:44
@ DT_MYVAR
a user-defined variable (my_foo)
Definition: types.h:38
@ DT_MBTABLE
multibyte char table
Definition: types.h:37
@ DT_ADDRESS
e-mail address
Definition: types.h:31
@ DT_ENUM
an enumeration
Definition: types.h:33
@ DT_REGEX
regular expressions
Definition: types.h:42
@ DT_PATH
a path to a file/directory
Definition: types.h:40