forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutt_lua.c
437 lines (391 loc) · 10.7 KB
/
mutt_lua.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/**
* @file
* Integrated Lua scripting
*
* @authors
* Copyright (C) 2016 Richard Russon <[email protected]>
* Copyright (C) 2016 Bernard Pratz <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <lauxlib.h>
#include <limits.h>
#include <lua.h>
#include <lualib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "mutt/mutt.h"
#include "mutt.h"
#include "mutt_lua.h"
#include "globals.h"
#include "mailbox.h"
#include "mbtable.h"
#include "mutt_commands.h"
#include "mutt_options.h"
#include "options.h"
#include "protos.h"
static int handle_panic(lua_State *l)
{
mutt_debug(1, "lua runtime panic: %s\n", lua_tostring(l, -1));
mutt_error("Lua runtime panic: %s\n", lua_tostring(l, -1));
lua_pop(l, 1);
return -1;
}
static int handle_error(lua_State *l)
{
mutt_debug(1, "lua runtime error: %s\n", lua_tostring(l, -1));
mutt_error("Lua runtime error: %s\n", lua_tostring(l, -1));
lua_pop(l, 1);
return -1;
}
static int lua_mutt_call(lua_State *l)
{
mutt_debug(2, " * lua_mutt_call()\n");
struct Buffer token, expn, err;
char buffer[LONG_STRING] = "";
const struct Command *command = NULL;
int rc = 0;
mutt_buffer_init(&token);
mutt_buffer_init(&expn);
mutt_buffer_init(&err);
err.dsize = STRING;
err.data = mutt_mem_malloc(err.dsize);
if (lua_gettop(l) == 0)
{
luaL_error(l, "Error command argument required.");
return -1;
}
command = mutt_command_get(lua_tostring(l, 1));
if (!command)
{
luaL_error(l, "Error command %s not found.", lua_tostring(l, 1));
return -1;
}
for (int i = 2; i <= lua_gettop(l); i++)
{
const char *s = lua_tostring(l, i);
mutt_str_strncat(buffer, sizeof(buffer), s, mutt_str_strlen(s));
mutt_str_strncat(buffer, sizeof(buffer), " ", 1);
}
expn.data = expn.dptr = buffer;
expn.dsize = mutt_str_strlen(buffer);
if (command->func(&token, &expn, command->data, &err))
{
luaL_error(l, "NeoMutt error: %s", err.data);
rc = -1;
}
else
{
if (lua_pushstring(l, err.data) == NULL)
handle_error(l);
else
rc++;
}
FREE(&err.data);
return rc;
}
static int lua_mutt_set(lua_State *l)
{
int rc = -1;
const char *param = lua_tostring(l, -2);
mutt_debug(2, " * lua_mutt_set(%s)\n", param);
struct Option opt;
char err_str[LONG_STRING];
struct Buffer err;
err.data = err_str;
err.dsize = sizeof(err_str);
if (mutt_str_strncmp("my_", param, 3) == 0)
{
const char *val = lua_tostring(l, -1);
myvar_set(param, val);
return 0;
}
else if (!mutt_option_get(param, &opt))
{
luaL_error(l, "Error getting parameter %s", param);
return -1;
}
rc = 0;
switch (DTYPE(opt.type))
{
case DT_ADDRESS:
case DT_MBTABLE:
case DT_REGEX:
case DT_PATH:
case DT_SORT:
case DT_STRING:
opt.var = mutt_str_strdup(lua_tostring(l, -1));
rc = mutt_option_set(&opt, &err);
FREE(&opt.var);
break;
case DT_QUAD:
{
long num = lua_tointeger(l, -1);
opt.var = (void *) num;
if ((num != MUTT_YES) && (num != MUTT_NO) && (num != MUTT_ASKYES) && (num != MUTT_ASKNO))
{
luaL_error(l,
"Invalid opt for quad option %s (one of "
"mutt.QUAD_YES, mutt.QUAD_NO, mutt.QUAD_ASKYES, "
"mutt.QUAD_ASKNO",
param);
rc = -1;
}
else
{
rc = mutt_option_set(&opt, &err);
}
break;
}
case DT_MAGIC:
if (mx_set_magic(lua_tostring(l, -1)))
{
luaL_error(l, "Invalid mailbox type: %s", opt.var);
rc = -1;
}
break;
case DT_NUMBER:
{
lua_Integer i = lua_tointeger(l, -1);
if ((i > SHRT_MIN) && (i < SHRT_MAX))
{
opt.var = (void *) lua_tointeger(l, -1);
rc = mutt_option_set(&opt, &err);
}
else
{
luaL_error(l, "Integer overflow of %d, not in %d-%d", i, SHRT_MIN, SHRT_MAX);
rc = -1;
}
break;
}
case DT_BOOL:
*(bool *) opt.var = lua_toboolean(l, -1);
break;
default:
luaL_error(l, "Unsupported NeoMutt parameter type %d for %s", opt.type, param);
rc = -1;
break;
}
return rc;
}
static int lua_mutt_get(lua_State *l)
{
const char *param = lua_tostring(l, -1);
mutt_debug(2, " * lua_mutt_get(%s)\n", param);
struct Option opt;
if (mutt_option_get(param, &opt))
{
switch (DTYPE(opt.type))
{
case DT_ADDRESS:
{
char value[LONG_STRING] = "";
mutt_addr_write(value, LONG_STRING, *((struct Address **) opt.var), false);
lua_pushstring(l, value);
return 1;
}
case DT_MBTABLE:
{
struct MbTable *tbl = *(struct MbTable **) opt.var;
lua_pushstring(l, tbl->orig_str);
return 1;
}
case DT_PATH:
case DT_STRING:
if (mutt_str_strncmp("my_", param, 3) == 0)
{
char *value = (char *) opt.initial;
lua_pushstring(l, value);
}
else
{
char *value = NONULL(*((char **) opt.var));
lua_pushstring(l, value);
}
return 1;
case DT_QUAD:
lua_pushinteger(l, *(unsigned char *) opt.var);
return 1;
case DT_REGEX:
case DT_MAGIC:
case DT_SORT:
{
char buf[LONG_STRING];
if (!mutt_option_to_string(&opt, buf, LONG_STRING))
{
luaL_error(l, "Couldn't load %s", param);
return -1;
}
lua_pushstring(l, buf);
return 1;
}
case DT_NUMBER:
lua_pushinteger(l, (signed short) *((unsigned long *) opt.var));
return 1;
case DT_BOOL:
lua_pushboolean(l, *((bool *) opt.var));
return 1;
default:
luaL_error(l, "NeoMutt parameter type %d unknown for %s", opt.type, param);
return -1;
}
}
mutt_debug(2, " * error\n");
luaL_error(l, "NeoMutt parameter not found %s", param);
return -1;
}
static int lua_mutt_enter(lua_State *l)
{
mutt_debug(2, " * lua_mutt_enter()\n");
struct Buffer token, err;
char *buffer = mutt_str_strdup(lua_tostring(l, -1));
int rc = 0;
mutt_buffer_init(&err);
mutt_buffer_init(&token);
err.dsize = STRING;
err.data = mutt_mem_malloc(err.dsize);
if (mutt_parse_rc_line(buffer, &token, &err))
{
luaL_error(l, "NeoMutt error: %s", err.data);
rc = -1;
}
else
{
if (lua_pushstring(l, err.data) == NULL)
handle_error(l);
else
rc++;
}
FREE(&buffer);
FREE(&err.data);
return rc;
}
static int lua_mutt_message(lua_State *l)
{
mutt_debug(2, " * lua_mutt_message()\n");
const char *msg = lua_tostring(l, -1);
if (msg)
mutt_message(msg);
return 0;
}
static int lua_mutt_error(lua_State *l)
{
mutt_debug(2, " * lua_mutt_error()\n");
const char *msg = lua_tostring(l, -1);
if (msg)
mutt_error(msg);
return 0;
}
static void lua_expose_command(void *p, const struct Command *cmd)
{
lua_State *l = (lua_State *) p;
char buf[LONG_STRING];
snprintf(buf, LONG_STRING, "mutt.command.%s = function (...); mutt.call('%s', ...); end",
cmd->name, cmd->name);
(void) luaL_dostring(l, buf);
}
static const luaL_Reg luaMuttDecl[] = {
{ "set", lua_mutt_set }, { "get", lua_mutt_get },
{ "call", lua_mutt_call }, { "enter", lua_mutt_enter },
{ "print", lua_mutt_message }, { "message", lua_mutt_message },
{ "error", lua_mutt_error }, { NULL, NULL },
};
#define lua_add_lib_member(LUA, TABLE, KEY, VALUE, DATATYPE_HANDLER) \
lua_pushstring(LUA, KEY); \
DATATYPE_HANDLER(LUA, VALUE); \
lua_settable(LUA, TABLE);
static int luaopen_mutt_decl(lua_State *l)
{
mutt_debug(2, " * luaopen_mutt()\n");
luaL_newlib(l, luaMuttDecl);
int lib_idx = lua_gettop(l);
/* table_idx, key value, value's type */
lua_add_lib_member(l, lib_idx, "VERSION", mutt_make_version(), lua_pushstring);
lua_add_lib_member(l, lib_idx, "QUAD_YES", MUTT_YES, lua_pushinteger);
lua_add_lib_member(l, lib_idx, "QUAD_NO", MUTT_NO, lua_pushinteger);
lua_add_lib_member(l, lib_idx, "QUAD_ASKYES", MUTT_ASKYES, lua_pushinteger);
lua_add_lib_member(l, lib_idx, "QUAD_ASKNO", MUTT_ASKNO, lua_pushinteger);
return 1;
}
static void luaopen_mutt(lua_State *l)
{
luaL_requiref(l, "mutt", luaopen_mutt_decl, 1);
(void) luaL_dostring(l, "mutt.command = {}");
mutt_commands_apply((void *) l, &lua_expose_command);
}
static bool lua_init(lua_State **l)
{
if (!*l)
{
mutt_debug(2, " * lua_init()\n");
*l = luaL_newstate();
if (!*l)
{
mutt_error("Error: Couldn't load the lua interpreter.");
return false;
}
lua_atpanic(*l, handle_panic);
/* load various Lua libraries */
luaL_openlibs(*l);
luaopen_mutt(*l);
}
return true;
}
/* Public API --------------------------------------------------------------- */
lua_State *Lua = NULL;
int mutt_lua_parse(struct Buffer *tmp, struct Buffer *s, unsigned long data, struct Buffer *err)
{
lua_init(&Lua);
mutt_debug(2, " * mutt_lua_parse(%s)\n", tmp->data);
if (luaL_dostring(Lua, s->dptr))
{
mutt_debug(2, " * %s -> failure\n", s->dptr);
snprintf(err->data, err->dsize, "%s: %s", s->dptr, lua_tostring(Lua, -1));
/* pop error message from the stack */
lua_pop(Lua, 1);
return -1;
}
mutt_debug(2, " * %s -> success\n", s->dptr);
return 2;
}
int mutt_lua_source_file(struct Buffer *tmp, struct Buffer *s,
unsigned long data, struct Buffer *err)
{
mutt_debug(2, " * mutt_lua_source()\n");
lua_init(&Lua);
char path[_POSIX_PATH_MAX];
if (mutt_extract_token(tmp, s, 0) != 0)
{
snprintf(err->data, err->dsize, _("source: error at %s"), s->dptr);
return -1;
}
if (MoreArgs(s))
{
mutt_buffer_printf(err, _("%s: too many arguments"), "source");
return -1;
}
mutt_str_strfcpy(path, tmp->data, sizeof(path));
mutt_expand_path(path, sizeof(path));
if (luaL_dofile(Lua, path))
{
mutt_error("Couldn't source lua source: %s", lua_tostring(Lua, -1));
lua_pop(Lua, 1);
return -1;
}
return 2;
}