NeoMutt  2024-04-16-36-g75b6fb
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 <limits.h>
44#include <lua.h>
45#include <lualib.h>
46#include <stdbool.h>
47#include <stdint.h>
48#include <stdio.h>
49#include "mutt/lib.h"
50#include "config/lib.h"
51#include "core/lib.h"
52#include "mutt_lua.h"
53#include "parse/lib.h"
54#include "muttlib.h"
55
57static lua_State *LuaState = NULL;
58
62static const struct Command LuaCommands[] = {
63 // clang-format off
64 { "lua", mutt_lua_parse, 0 },
65 { "lua-source", mutt_lua_source_file, 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 = command_get(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
129 if (cmd->parse(token, buf, cmd->data, err))
130 {
131 luaL_error(l, "NeoMutt error: %s", buf_string(err));
132 rc = -1;
133 }
134 else
135 {
136 if (!lua_pushstring(l, buf_string(err)))
137 handle_error(l);
138 else
139 rc++;
140 }
141
142 buf_pool_release(&buf);
143 buf_pool_release(&token);
144 buf_pool_release(&err);
145 return rc;
146}
147
154static int lua_mutt_set(lua_State *l)
155{
156 const char *param = lua_tostring(l, -2);
157 mutt_debug(LL_DEBUG2, " * lua_mutt_set(%s)\n", param);
158
159 struct Buffer *err = buf_pool_get();
160 struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
161 if (!he)
162 {
163 // In case it is a my_var, we have to create it
164 if (mutt_str_startswith(param, "my_"))
165 {
166 struct ConfigDef my_cdef = { 0 };
167 my_cdef.name = param;
168 my_cdef.type = DT_MYVAR;
169 he = cs_create_variable(NeoMutt->sub->cs, &my_cdef, err);
170 if (!he)
171 return -1;
172 }
173 else
174 {
175 luaL_error(l, "NeoMutt parameter not found %s", param);
176 return -1;
177 }
178 }
179
180 struct ConfigDef *cdef = he->data;
181
182 int rc = 0;
183
184 switch (DTYPE(cdef->type))
185 {
186 case DT_ADDRESS:
187 case DT_ENUM:
188 case DT_MBTABLE:
189 case DT_MYVAR:
190 case DT_PATH:
191 case DT_REGEX:
192 case DT_SLIST:
193 case DT_SORT:
194 case DT_STRING:
195 {
196 const char *value = lua_tostring(l, -1);
197 size_t val_size = lua_rawlen(l, -1);
198 struct Buffer *value_buf = buf_pool_get();
199 buf_strcpy_n(value_buf, value, val_size);
200 if (DTYPE(he->type) == DT_PATH)
201 buf_expand_path(value_buf);
202
203 int rv = cs_subset_he_string_set(NeoMutt->sub, he, buf_string(value_buf), err);
204 buf_pool_release(&value_buf);
205 if (CSR_RESULT(rv) != CSR_SUCCESS)
206 rc = -1;
207 break;
208 }
209 case DT_NUMBER:
210 case DT_QUAD:
211 {
212 const intptr_t value = lua_tointeger(l, -1);
213 int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, err);
214 if (CSR_RESULT(rv) != CSR_SUCCESS)
215 rc = -1;
216 break;
217 }
218 case DT_BOOL:
219 {
220 const intptr_t value = lua_toboolean(l, -1);
221 int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, err);
222 if (CSR_RESULT(rv) != CSR_SUCCESS)
223 rc = -1;
224 break;
225 }
226 default:
227 luaL_error(l, "Unsupported NeoMutt parameter type %d for %s", DTYPE(cdef->type), param);
228 rc = -1;
229 break;
230 }
231
232 buf_pool_release(&err);
233 return rc;
234}
235
242static int lua_mutt_get(lua_State *l)
243{
244 const char *param = lua_tostring(l, -1);
245 mutt_debug(LL_DEBUG2, " * lua_mutt_get(%s)\n", param);
246
247 struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
248 if (!he)
249 {
250 mutt_debug(LL_DEBUG2, " * error\n");
251 luaL_error(l, "NeoMutt parameter not found %s", param);
252 return -1;
253 }
254
255 struct ConfigDef *cdef = he->data;
256
257 switch (DTYPE(cdef->type))
258 {
259 case DT_ADDRESS:
260 case DT_ENUM:
261 case DT_MBTABLE:
262 case DT_MYVAR:
263 case DT_REGEX:
264 case DT_SLIST:
265 case DT_SORT:
266 case DT_STRING:
267 {
268 struct Buffer *value = buf_pool_get();
269 int rc = cs_subset_he_string_get(NeoMutt->sub, he, value);
270 if (CSR_RESULT(rc) != CSR_SUCCESS)
271 {
272 buf_pool_release(&value);
273 return -1;
274 }
275
276 struct Buffer *escaped = buf_pool_get();
277 escape_string(escaped, buf_string(value));
278 lua_pushstring(l, buf_string(escaped));
279 buf_pool_release(&value);
280 buf_pool_release(&escaped);
281 return 1;
282 }
283 case DT_QUAD:
284 lua_pushinteger(l, (unsigned char) cdef->var);
285 return 1;
286 case DT_NUMBER:
287 lua_pushinteger(l, (signed short) cdef->var);
288 return 1;
289 case DT_BOOL:
290 lua_pushboolean(l, (bool) cdef->var);
291 return 1;
292 default:
293 luaL_error(l, "NeoMutt parameter type %d unknown for %s", cdef->type, param);
294 return -1;
295 }
296}
297
304static int lua_mutt_enter(lua_State *l)
305{
306 mutt_debug(LL_DEBUG2, " * lua_mutt_enter()\n");
307 struct Buffer *err = buf_pool_get();
308 char *buf = mutt_str_dup(lua_tostring(l, -1));
309 int rc = 0;
310
311 if (parse_rc_line(buf, err))
312 {
313 luaL_error(l, "NeoMutt error: %s", buf_string(err));
314 rc = -1;
315 }
316 else
317 {
318 if (!lua_pushstring(l, buf_string(err)))
319 handle_error(l);
320 else
321 rc++;
322 }
323
324 FREE(&buf);
325 buf_pool_release(&err);
326
327 return rc;
328}
329
335static int lua_mutt_message(lua_State *l)
336{
337 mutt_debug(LL_DEBUG2, " * lua_mutt_message()\n");
338 const char *msg = lua_tostring(l, -1);
339 if (msg)
340 mutt_message("%s", msg);
341 return 0;
342}
343
349static int lua_mutt_error(lua_State *l)
350{
351 mutt_debug(LL_DEBUG2, " * lua_mutt_error()\n");
352 const char *msg = lua_tostring(l, -1);
353 if (msg)
354 mutt_error("%s", msg);
355 return 0;
356}
357
363static void lua_expose_command(lua_State *l, const struct Command *cmd)
364{
365 char buf[1024] = { 0 };
366 snprintf(buf, sizeof(buf), "mutt.command.%s = function (...); mutt.call('%s', ...); end",
367 cmd->name, cmd->name);
368 (void) luaL_dostring(l, buf);
369}
370
380static const luaL_Reg LuaMuttCommands[] = {
381 // clang-format off
382 { "set", lua_mutt_set },
383 { "get", lua_mutt_get },
384 { "call", lua_mutt_call },
385 { "enter", lua_mutt_enter },
386 { "print", lua_mutt_message },
387 { "message", lua_mutt_message },
388 { "error", lua_mutt_error },
389 { NULL, NULL },
390 // clang-format on
391};
392
398static int luaopen_mutt_decl(lua_State *l)
399{
400 mutt_debug(LL_DEBUG2, " * luaopen_mutt()\n");
401 luaL_newlib(l, LuaMuttCommands);
402 int lib_idx = lua_gettop(l);
403
404 // clang-format off
405 lua_pushstring(l, "VERSION"); lua_pushstring(l, mutt_make_version()); lua_settable(l, lib_idx);;
406 lua_pushstring(l, "QUAD_YES"); lua_pushinteger(l, MUTT_YES); lua_settable(l, lib_idx);;
407 lua_pushstring(l, "QUAD_NO"); lua_pushinteger(l, MUTT_NO); lua_settable(l, lib_idx);;
408 lua_pushstring(l, "QUAD_ASKYES"); lua_pushinteger(l, MUTT_ASKYES); lua_settable(l, lib_idx);;
409 lua_pushstring(l, "QUAD_ASKNO"); lua_pushinteger(l, MUTT_ASKNO); lua_settable(l, lib_idx);;
410 // clang-format on
411
412 return 1;
413}
414
419static void luaopen_mutt(lua_State *l)
420{
421 luaL_requiref(l, "mutt", luaopen_mutt_decl, 1);
422 (void) luaL_dostring(l, "mutt.command = {}");
423
424 struct Command *c = NULL;
425 for (size_t i = 0, size = commands_array(&c); i < size; i++)
426 {
427 lua_expose_command(l, &c[i]);
428 }
429}
430
436static bool lua_init(lua_State **l)
437{
438 if (!l)
439 return false;
440 if (*l)
441 return true;
442
443 mutt_debug(LL_DEBUG2, " * lua_init()\n");
444 *l = luaL_newstate();
445
446 if (!*l)
447 {
448 mutt_error(_("Error: Couldn't load the lua interpreter"));
449 return false;
450 }
451
452 lua_atpanic(*l, handle_panic);
453
454 /* load various Lua libraries */
455 luaL_openlibs(*l);
456 luaopen_mutt(*l);
457
458 return true;
459}
460
465{
467}
468
472enum CommandResult mutt_lua_parse(struct Buffer *buf, struct Buffer *s,
473 intptr_t data, struct Buffer *err)
474{
476 mutt_debug(LL_DEBUG2, " * mutt_lua_parse(%s)\n", buf->data);
477
478 if (luaL_dostring(LuaState, s->dptr))
479 {
480 mutt_debug(LL_DEBUG2, " * %s -> failure\n", s->dptr);
481 buf_printf(err, "%s: %s", s->dptr, lua_tostring(LuaState, -1));
482 /* pop error message from the stack */
483 lua_pop(LuaState, 1);
484 return MUTT_CMD_ERROR;
485 }
486 mutt_debug(LL_DEBUG2, " * %s -> success\n", s->dptr);
487 buf_reset(s); // Clear the rest of the line
488 return MUTT_CMD_SUCCESS;
489}
490
495 intptr_t data, struct Buffer *err)
496{
497 mutt_debug(LL_DEBUG2, " * mutt_lua_source()\n");
498
500
501 char path[PATH_MAX] = { 0 };
502
503 if (parse_extract_token(buf, s, TOKEN_NO_FLAGS) != 0)
504 {
505 buf_printf(err, _("source: error at %s"), s->dptr);
506 return MUTT_CMD_ERROR;
507 }
508 if (MoreArgs(s))
509 {
510 buf_printf(err, _("%s: too many arguments"), "source");
511 return MUTT_CMD_WARNING;
512 }
513 mutt_str_copy(path, buf->data, sizeof(path));
514 mutt_expand_path(path, sizeof(path));
515
516 if (luaL_dofile(LuaState, path))
517 {
518 mutt_error(_("Couldn't source lua source: %s"), lua_tostring(LuaState, -1));
519 lua_pop(LuaState, 1);
520 return MUTT_CMD_ERROR;
521 }
522 return MUTT_CMD_SUCCESS;
523}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:75
size_t buf_strcpy_n(struct Buffer *buf, const char *s, size_t len)
Copy a string into a Buffer.
Definition: buffer.c:415
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:240
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
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:494
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:472
#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:45
#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
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:575
#define PATH_MAX
Definition: mutt.h:42
static int lua_mutt_enter(lua_State *l)
Execute NeoMutt config from Lua.
Definition: mutt_lua.c:304
static int lua_mutt_message(lua_State *l)
Display a message in Neomutt.
Definition: mutt_lua.c:335
static void luaopen_mutt(lua_State *l)
Expose a 'Mutt' object to the Lua interpreter.
Definition: mutt_lua.c:419
void mutt_lua_init(void)
Setup feature commands.
Definition: mutt_lua.c:464
static void lua_expose_command(lua_State *l, const struct Command *cmd)
Expose a NeoMutt command to the Lua interpreter.
Definition: mutt_lua.c:363
static bool lua_init(lua_State **l)
Initialise a Lua State.
Definition: mutt_lua.c:436
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:242
static lua_State * LuaState
Global Lua State.
Definition: mutt_lua.c:57
static const luaL_Reg LuaMuttCommands[]
List of Lua commands to register.
Definition: mutt_lua.c:380
static int lua_mutt_error(lua_State *l)
Display an error in Neomutt.
Definition: mutt_lua.c:349
static const struct Command LuaCommands[]
List of NeoMutt commands to register.
Definition: mutt_lua.c:62
static int luaopen_mutt_decl(lua_State *l)
Declare some NeoMutt types to the Lua interpreter.
Definition: mutt_lua.c:398
static int lua_mutt_set(lua_State *l)
Set a NeoMutt variable.
Definition: mutt_lua.c:154
Integrated Lua scripting.
char * mutt_expand_path(char *buf, size_t buflen)
Create the canonical path.
Definition: muttlib.c:123
const char * mutt_make_version(void)
Generate the NeoMutt version string.
Definition: muttlib.c:893
void buf_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:328
Some miscellaneous functions.
Text parsing functions.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
@ 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:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
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