NeoMutt  2025-12-11-435-g4ac674
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
lua.c
Go to the documentation of this file.
1
27
33
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 "parse/lib.h"
52#include "muttlib.h"
53#include "version.h"
54
56lua_State *LuaState = NULL;
57
63static int lua_handle_panic(lua_State *l)
64{
65 mutt_debug(LL_DEBUG1, "lua runtime panic: %s\n", lua_tostring(l, -1));
66 mutt_error("Lua runtime panic: %s", lua_tostring(l, -1));
67 lua_pop(l, 1);
68 return -1;
69}
70
76static int lua_handle_error(lua_State *l)
77{
78 mutt_debug(LL_DEBUG1, "lua runtime error: %s\n", lua_tostring(l, -1));
79 mutt_error("Lua runtime error: %s", lua_tostring(l, -1));
80 lua_pop(l, 1);
81 return -1;
82}
83
90static int lua_cb_global_call(lua_State *l)
91{
92 mutt_debug(LL_DEBUG2, "enter\n");
93 struct Buffer *buf = buf_pool_get();
94 struct ParseContext *pc = parse_context_new();
95 struct ParseError *pe = parse_error_new();
96 const struct Command *cmd = NULL;
97 int rc = 0;
98
99 if (lua_gettop(l) == 0)
100 {
101 luaL_error(l, "Error command argument required");
102 return -1;
103 }
104
105 cmd = commands_get(&NeoMutt->commands, lua_tostring(l, 1));
106 if (!cmd)
107 {
108 luaL_error(l, "Error command %s not found", lua_tostring(l, 1));
109 return -1;
110 }
111
112 for (int i = 2; i <= lua_gettop(l); i++)
113 {
114 buf_addstr(buf, lua_tostring(l, i));
115 buf_addch(buf, ' ');
116 }
117 buf_seek(buf, 0);
118
119 if (cmd->parse(cmd, buf, pc, pe))
120 {
121 luaL_error(l, "NeoMutt error: %s", buf_string(pe->message));
122 rc = -1;
123 }
124 else
125 {
126 if (!lua_pushstring(l, buf_string(pe->message)))
128 else
129 rc++;
130 }
131
132 buf_pool_release(&buf);
134 parse_error_free(&pe);
135 return rc;
136}
137
144static int lua_cb_global_set(lua_State *l)
145{
146 const char *param = lua_tostring(l, -2);
147 mutt_debug(LL_DEBUG2, "%s\n", param);
148
149 struct Buffer *err = buf_pool_get();
150 struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
151 if (!he)
152 {
153 // In case it is a my_var, we have to create it
154 if (mutt_str_startswith(param, "my_"))
155 {
156 struct ConfigDef my_cdef = { 0 };
157 my_cdef.name = param;
158 my_cdef.type = DT_MYVAR;
159 he = cs_create_variable(NeoMutt->sub->cs, &my_cdef, err);
160 if (!he)
161 return -1;
162 }
163 else
164 {
165 luaL_error(l, "NeoMutt parameter not found %s", param);
166 return -1;
167 }
168 }
169
170 struct ConfigDef *cdef = he->data;
171
172 int rc = 0;
173
174 switch (CONFIG_TYPE(cdef->type))
175 {
176 case DT_ADDRESS:
177 case DT_ENUM:
178 case DT_EXPANDO:
179 case DT_MBTABLE:
180 case DT_MYVAR:
181 case DT_PATH:
182 case DT_REGEX:
183 case DT_SLIST:
184 case DT_SORT:
185 case DT_STRING:
186 {
187 const char *value = lua_tostring(l, -1);
188 size_t val_size = lua_rawlen(l, -1);
189 struct Buffer *value_buf = buf_pool_get();
190 buf_strcpy_n(value_buf, value, val_size);
191 if (CONFIG_TYPE(he->type) == DT_PATH)
192 expand_path(value_buf, false);
193
194 int rv = cs_subset_he_string_set(NeoMutt->sub, he, buf_string(value_buf), err);
195 buf_pool_release(&value_buf);
196 if (CSR_RESULT(rv) != CSR_SUCCESS)
197 rc = -1;
198 break;
199 }
200 case DT_LONG:
201 case DT_NUMBER:
202 case DT_QUAD:
203 {
204 const intptr_t value = lua_tointeger(l, -1);
205 int rv = cs_subset_he_native_set(NeoMutt->sub, he, value, err);
206 if (CSR_RESULT(rv) != CSR_SUCCESS)
207 rc = -1;
208 break;
209 }
210 case DT_BOOL:
211 {
212 const intptr_t value = lua_toboolean(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 default:
219 luaL_error(l, "Unsupported NeoMutt parameter type %d for %s",
220 CONFIG_TYPE(cdef->type), param);
221 rc = -1;
222 break;
223 }
224
225 buf_pool_release(&err);
226 return rc;
227}
228
235static int lua_cb_global_get(lua_State *l)
236{
237 const char *param = lua_tostring(l, -1);
238 mutt_debug(LL_DEBUG2, "%s\n", param);
239
240 struct HashElem *he = cs_subset_lookup(NeoMutt->sub, param);
241 if (!he)
242 {
243 mutt_debug(LL_DEBUG2, "error\n");
244 luaL_error(l, "NeoMutt parameter not found %s", param);
245 return -1;
246 }
247
248 struct ConfigDef *cdef = he->data;
249
250 switch (CONFIG_TYPE(cdef->type))
251 {
252 case DT_ADDRESS:
253 case DT_ENUM:
254 case DT_EXPANDO:
255 case DT_MBTABLE:
256 case DT_MYVAR:
257 case DT_PATH:
258 case DT_REGEX:
259 case DT_SLIST:
260 case DT_SORT:
261 case DT_STRING:
262 {
263 struct Buffer *value = buf_pool_get();
264 int rc = cs_subset_he_string_get(NeoMutt->sub, he, value);
265 if (CSR_RESULT(rc) != CSR_SUCCESS)
266 {
267 buf_pool_release(&value);
268 return -1;
269 }
270
271 struct Buffer *escaped = buf_pool_get();
272 escape_string(escaped, buf_string(value));
273 lua_pushstring(l, buf_string(escaped));
274 buf_pool_release(&value);
275 buf_pool_release(&escaped);
276 return 1;
277 }
278 case DT_QUAD:
279 lua_pushinteger(l, (unsigned char) cdef->var);
280 return 1;
281 case DT_LONG:
282 lua_pushinteger(l, (signed long) cdef->var);
283 return 1;
284 case DT_NUMBER:
285 lua_pushinteger(l, (signed short) cdef->var);
286 return 1;
287 case DT_BOOL:
288 lua_pushboolean(l, (bool) cdef->var);
289 return 1;
290 default:
291 luaL_error(l, "NeoMutt parameter type %d unknown for %s", cdef->type, param);
292 return -1;
293 }
294}
295
302static int lua_cb_global_enter(lua_State *l)
303{
304 mutt_debug(LL_DEBUG2, "enter\n");
305 struct Buffer *line = buf_pool_get();
306 struct ParseContext *pc = parse_context_new();
307 struct ParseError *pe = parse_error_new();
308
309 buf_strcpy(line, lua_tostring(l, -1));
310 int rc = 0;
311
312 if (parse_rc_line(line, pc, pe))
313 {
314 luaL_error(l, "NeoMutt error: %s", buf_string(pe->message));
315 rc = -1;
316 }
317 else
318 {
319 if (!lua_pushstring(l, buf_string(pe->message)))
321 else
322 rc++;
323 }
324
325 buf_pool_release(&line);
327 parse_error_free(&pe);
328
329 return rc;
330}
331
337static int lua_cb_global_message(lua_State *l)
338{
339 mutt_debug(LL_DEBUG2, "enter\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_cb_global_error(lua_State *l)
352{
353 mutt_debug(LL_DEBUG2, "enter\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_cb_global_set },
385 { "get", lua_cb_global_get },
386 { "call", lua_cb_global_call },
387 { "enter", lua_cb_global_enter },
388 { "print", lua_cb_global_message },
389 { "message", lua_cb_global_message },
390 { "error", lua_cb_global_error },
391 { NULL, NULL },
392 // clang-format on
393};
394
400static int lua_expose_commands(lua_State *l)
401{
402 mutt_debug(LL_DEBUG2, "enter\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 lua_expose_mutt(lua_State *l)
422{
423 luaL_requiref(l, "mutt", lua_expose_commands, 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
440bool lua_init_state(lua_State **l)
441{
442 if (!l)
443 return false;
444 if (*l)
445 return true;
446
447 mutt_debug(LL_DEBUG2, "enter\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, lua_handle_panic);
457
458 /* load various Lua libraries */
459 luaL_openlibs(*l);
460 lua_expose_mutt(*l);
461
462 return true;
463}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:223
void buf_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition buffer.c:622
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_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
size_t escape_string(struct Buffer *buf, const char *src)
Write a string to a buffer, escaping special characters.
Definition dump.c:47
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:327
#define CSR_RESULT(x)
Extract the result code from CSR_* flags.
Definition set.h:52
#define CSR_SUCCESS
Action completed successfully.
Definition set.h:33
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.
#define mutt_error(...)
Definition logging2.h:94
#define mutt_message(...)
Definition logging2.h:93
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:46
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
lua_State * LuaState
Global Lua State.
Definition lua.c:56
bool lua_init_state(lua_State **l)
Initialise a Lua State.
Definition lua.c:440
static int lua_expose_commands(lua_State *l)
Declare some NeoMutt types to the Lua interpreter.
Definition lua.c:400
static int lua_cb_global_set(lua_State *l)
Set a NeoMutt variable.
Definition lua.c:144
static void lua_expose_mutt(lua_State *l)
Expose a 'Mutt' object to the Lua interpreter.
Definition lua.c:421
static void lua_expose_command(lua_State *l, const struct Command *cmd)
Expose a NeoMutt command to the Lua interpreter.
Definition lua.c:365
static int lua_cb_global_message(lua_State *l)
Display a message in NeoMutt.
Definition lua.c:337
static int lua_handle_panic(lua_State *l)
Handle a panic in the Lua interpreter.
Definition lua.c:63
static int lua_cb_global_error(lua_State *l)
Display an error in NeoMutt.
Definition lua.c:351
static const luaL_Reg LuaMuttCommands[]
List of Lua commands to register.
Definition lua.c:382
static int lua_cb_global_enter(lua_State *l)
Execute NeoMutt config from Lua.
Definition lua.c:302
static int lua_cb_global_get(lua_State *l)
Get a NeoMutt variable.
Definition lua.c:235
static int lua_handle_error(lua_State *l)
Handle an error in the Lua interpreter.
Definition lua.c:76
static int lua_cb_global_call(lua_State *l)
Call a NeoMutt command by name.
Definition lua.c:90
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition string.c:234
void expand_path(struct Buffer *buf, bool regex)
Create the canonical path.
Definition muttlib.c:121
Some miscellaneous functions.
Text parsing functions.
void parse_context_free(struct ParseContext **pptr)
Free a ParseContext.
Definition pcontext.c:48
struct ParseContext * parse_context_new(void)
Create a new ParseContext.
Definition pcontext.c:37
void parse_error_free(struct ParseError **pptr)
Free a ParseError.
Definition perror.c:50
struct ParseError * parse_error_new(void)
Create a new ParseError.
Definition perror.c:37
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
@ 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(struct Buffer *line, struct ParseContext *pc, struct ParseError *pe)
Parse a line of user config.
Definition rc.c:45
String manipulation buffer.
Definition buffer.h:36
enum CommandResult(* parse)(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Definition command.h:178
const char * name
Name of the Command.
Definition command.h:159
const char * name
User-visible name.
Definition set.h:65
intptr_t var
Storage for the variable.
Definition set.h:84
uint32_t type
Variable type, e.g. DT_STRING.
Definition set.h:66
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:41
struct CommandArray commands
NeoMutt commands.
Definition neomutt.h:53
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
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
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:338
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:281
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:370
struct HashElem * cs_subset_lookup(const struct ConfigSubset *sub, const char *name)
Find an inherited config item.
Definition subset.c:193
#define CONFIG_TYPE(t)
Extract the type from the flags.
Definition types.h:50
@ 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_LONG
a number (long)
Definition types.h:35
@ DT_EXPANDO
an expando
Definition types.h:34
@ 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
const char * mutt_make_version(void)
Generate the NeoMutt version string.
Definition version.c:293
Display version and copyright about NeoMutt.