You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_common.h 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * Copyright 2024 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef RSPAMD_LUA_H
  17. #define RSPAMD_LUA_H
  18. #include "config.h"
  19. /* Lua headers do not have __cplusplus guards... */
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #include <lua.h>
  24. #include <lauxlib.h>
  25. #include <lualib.h>
  26. #ifdef WITH_LUAJIT
  27. #include <luajit.h>
  28. #endif
  29. #ifdef __cplusplus
  30. }
  31. #endif
  32. #include <stdbool.h>
  33. #include "rspamd.h"
  34. #include "ucl.h"
  35. #include "lua_ucl.h"
  36. #include "lua_classnames.h"
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. #ifndef lua_open
  41. #define lua_open() luaL_newstate()
  42. #endif
  43. #ifndef luaL_reg
  44. #define luaL_reg luaL_Reg
  45. #endif
  46. #define LUA_ENUM(L, name, val) \
  47. lua_pushlstring(L, #name, sizeof(#name) - 1); \
  48. lua_pushinteger(L, val); \
  49. lua_settable(L, -3);
  50. #if LUA_VERSION_NUM > 501 && !defined LUA_COMPAT_MODULE
  51. static inline void
  52. luaL_register(lua_State *L, const gchar *name, const struct luaL_reg *methods)
  53. {
  54. if (name != NULL) {
  55. lua_newtable(L);
  56. }
  57. luaL_setfuncs(L, methods, 0);
  58. if (name != NULL) {
  59. lua_pushvalue(L, -1);
  60. lua_setglobal(L, name);
  61. }
  62. }
  63. #endif
  64. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
  65. /* Special hack to work with moonjit of specific version */
  66. #if !defined(MOONJIT_VERSION) && (!defined(LUAJIT_VERSION_NUM) || LUAJIT_VERSION_NUM != 20200)
  67. static inline int lua_absindex(lua_State *L, int i)
  68. {
  69. if (i < 0 && i > LUA_REGISTRYINDEX)
  70. i += lua_gettop(L) + 1;
  71. return i;
  72. }
  73. #endif
  74. #endif
  75. /* Interface definitions */
  76. #define LUA_FUNCTION_DEF(class, name) static int lua_##class##_##name(lua_State *L)
  77. #define LUA_PUBLIC_FUNCTION_DEF(class, name) int lua_##class##_##name(lua_State *L)
  78. #define LUA_INTERFACE_DEF(class, name) \
  79. { \
  80. #name, lua_##class##_##name \
  81. }
  82. extern const luaL_reg null_reg[];
  83. #define RSPAMD_LUA_CFG_STATE(cfg) ((lua_State *) ((cfg)->lua_state))
  84. /**
  85. * Lua IP address structure
  86. */
  87. struct rspamd_lua_ip {
  88. rspamd_inet_addr_t *addr;
  89. };
  90. #define RSPAMD_TEXT_FLAG_OWN (1u << 0u)
  91. #define RSPAMD_TEXT_FLAG_MMAPED (1u << 1u)
  92. #define RSPAMD_TEXT_FLAG_WIPE (1u << 2u)
  93. #define RSPAMD_TEXT_FLAG_SYSMALLOC (1u << 3u)
  94. #define RSPAMD_TEXT_FLAG_FAKE (1u << 4u)
  95. #define RSPAMD_TEXT_FLAG_BINARY (1u << 5u)
  96. struct rspamd_lua_text {
  97. const gchar *start;
  98. guint len;
  99. guint flags;
  100. };
  101. struct rspamd_lua_url {
  102. struct rspamd_url *url;
  103. };
  104. struct rspamd_lua_regexp {
  105. rspamd_regexp_t *re;
  106. gchar *module;
  107. gchar *re_pattern;
  108. gint re_flags;
  109. };
  110. struct rspamd_map;
  111. struct lua_map_callback_data;
  112. struct radix_tree_compressed;
  113. struct rspamd_mime_header;
  114. enum rspamd_lua_map_type {
  115. RSPAMD_LUA_MAP_RADIX = 0,
  116. RSPAMD_LUA_MAP_SET,
  117. RSPAMD_LUA_MAP_HASH,
  118. RSPAMD_LUA_MAP_REGEXP,
  119. RSPAMD_LUA_MAP_REGEXP_MULTIPLE,
  120. RSPAMD_LUA_MAP_CALLBACK,
  121. RSPAMD_LUA_MAP_CDB,
  122. RSPAMD_LUA_MAP_UNKNOWN,
  123. };
  124. struct rspamd_lua_map {
  125. struct rspamd_map *map;
  126. enum rspamd_lua_map_type type;
  127. guint flags;
  128. union {
  129. struct rspamd_radix_map_helper *radix;
  130. struct rspamd_hash_map_helper *hash;
  131. struct rspamd_regexp_map_helper *re_map;
  132. struct rspamd_cdb_map_helper *cdb_map;
  133. struct lua_map_callback_data *cbdata;
  134. } data;
  135. };
  136. struct rspamd_lua_upstream {
  137. struct upstream *up;
  138. gint upref;
  139. };
  140. /* Common utility functions */
  141. /**
  142. * Create and register new class
  143. */
  144. void rspamd_lua_new_class(lua_State *L,
  145. const gchar *classname,
  146. const struct luaL_reg *methods);
  147. /**
  148. * Set class name for object at @param objidx position
  149. * @param L
  150. * @param classname **MUST BE STATIC**, direct address is used for comparisons!
  151. */
  152. void rspamd_lua_setclass(lua_State *L, const gchar *classname, gint objidx);
  153. /**
  154. * Pushes the metatable for specific class on top of the stack
  155. * @param L
  156. * @param classname
  157. */
  158. void rspamd_lua_class_metatable(lua_State *L, const gchar *classname);
  159. /**
  160. * Adds a new field to the class (metatable) identified by `classname`
  161. * @param L
  162. * @param classname
  163. * @param meth
  164. */
  165. void rspamd_lua_add_metamethod(lua_State *L, const gchar *classname,
  166. luaL_Reg *meth);
  167. /**
  168. * Set index of table to value (like t['index'] = value)
  169. */
  170. void rspamd_lua_table_set(lua_State *L, const gchar *index, const gchar *value);
  171. /**
  172. * Get string value of index in a table (return t['index'])
  173. */
  174. const gchar *rspamd_lua_table_get(lua_State *L, const gchar *index);
  175. /**
  176. * Convert classname to string
  177. */
  178. gint rspamd_lua_class_tostring(lua_State *L);
  179. /**
  180. * Check whether the argument at specified index is of the specified class
  181. */
  182. gpointer rspamd_lua_check_class(lua_State *L, gint index, const gchar *name);
  183. /**
  184. * Initialize lua and bindings
  185. */
  186. lua_State *rspamd_lua_init(bool wipe_mem);
  187. /**
  188. * Close lua_state and free remainders
  189. * @param L
  190. */
  191. void rspamd_lua_close(lua_State *L);
  192. void rspamd_lua_start_gc(struct rspamd_config *cfg);
  193. /**
  194. * Sets field in a global variable
  195. * @param L
  196. * @param global_name
  197. * @param field_name
  198. * @param new_elt
  199. */
  200. void rspamd_plugins_table_push_elt(lua_State *L, const gchar *field_name,
  201. const gchar *new_elt);
  202. /**
  203. * Load and initialize lua plugins
  204. */
  205. gboolean
  206. rspamd_init_lua_filters(struct rspamd_config *cfg, bool force_load, bool strict);
  207. /**
  208. * Push lua ip address
  209. */
  210. void rspamd_lua_ip_push(lua_State *L, rspamd_inet_addr_t *addr);
  211. /**
  212. * Push rspamd task structure to lua
  213. */
  214. void rspamd_lua_task_push(lua_State *L, struct rspamd_task *task);
  215. /**
  216. * Return lua ip structure at the specified address
  217. */
  218. struct rspamd_lua_ip *lua_check_ip(lua_State *L, gint pos);
  219. struct rspamd_lua_text *lua_check_text(lua_State *L, gint pos);
  220. /**
  221. * Checks for a text or a string. In case of string a pointer to static structure is returned.
  222. * So it should not be reused or placed to Lua stack anyhow!
  223. * However, you can use this function up to 4 times and have distinct static structures
  224. * @param L
  225. * @param pos
  226. * @return
  227. */
  228. struct rspamd_lua_text *lua_check_text_or_string(lua_State *L, gint pos);
  229. /**
  230. * Create new text object
  231. * @param L
  232. * @param start
  233. * @param len
  234. * @param own
  235. * @return
  236. */
  237. struct rspamd_lua_text *lua_new_text(lua_State *L, const gchar *start,
  238. gsize len, gboolean own);
  239. /**
  240. * Create new text object from task pool if allocation is needed
  241. * @param task
  242. * @param L
  243. * @param start
  244. * @param len
  245. * @param own
  246. * @return
  247. */
  248. struct rspamd_lua_text *lua_new_text_task(lua_State *L, struct rspamd_task *task,
  249. const gchar *start, gsize len, gboolean own);
  250. /**
  251. * Checks if a text has binary characters (non ascii and non-utf8 characters)
  252. * @param t
  253. * @return
  254. */
  255. bool lua_is_text_binary(struct rspamd_lua_text *t);
  256. struct rspamd_lua_regexp *lua_check_regexp(lua_State *L, gint pos);
  257. struct rspamd_lua_upstream *lua_check_upstream(lua_State *L, int pos);
  258. enum rspamd_lua_task_header_type {
  259. RSPAMD_TASK_HEADER_PUSH_SIMPLE = 0,
  260. RSPAMD_TASK_HEADER_PUSH_RAW,
  261. RSPAMD_TASK_HEADER_PUSH_FULL,
  262. RSPAMD_TASK_HEADER_PUSH_COUNT,
  263. RSPAMD_TASK_HEADER_PUSH_HAS,
  264. };
  265. gint rspamd_lua_push_header(lua_State *L,
  266. struct rspamd_mime_header *h,
  267. enum rspamd_lua_task_header_type how);
  268. /**
  269. * Push specific header to lua
  270. */
  271. gint rspamd_lua_push_header_array(lua_State *L,
  272. const gchar *name,
  273. struct rspamd_mime_header *rh,
  274. enum rspamd_lua_task_header_type how,
  275. gboolean strong);
  276. /**
  277. * Check for task at the specified position
  278. */
  279. struct rspamd_task *lua_check_task(lua_State *L, gint pos);
  280. struct rspamd_task *lua_check_task_maybe(lua_State *L, gint pos);
  281. struct rspamd_lua_map *lua_check_map(lua_State *L, gint pos);
  282. /**
  283. * Push ip address from a string (nil is pushed if a string cannot be converted)
  284. */
  285. void rspamd_lua_ip_push_fromstring(lua_State *L, const gchar *ip_str);
  286. /**
  287. * Create type error
  288. */
  289. int rspamd_lua_typerror(lua_State *L, int narg, const char *tname);
  290. /**
  291. * Open libraries functions
  292. */
  293. /**
  294. * Add preload function
  295. */
  296. void rspamd_lua_add_preload(lua_State *L, const gchar *name, lua_CFunction func);
  297. void luaopen_task(lua_State *L);
  298. void luaopen_config(lua_State *L);
  299. void luaopen_map(lua_State *L);
  300. void luaopen_trie(lua_State *L);
  301. void luaopen_textpart(lua_State *L);
  302. void luaopen_mimepart(lua_State *L);
  303. void luaopen_image(lua_State *L);
  304. void luaopen_url(lua_State *L);
  305. void luaopen_classifier(lua_State *L);
  306. void luaopen_statfile(lua_State *L);
  307. void luaopen_regexp(lua_State *L);
  308. void luaopen_cdb(lua_State *L);
  309. void luaopen_xmlrpc(lua_State *L);
  310. void luaopen_http(lua_State *L);
  311. void luaopen_redis(lua_State *L);
  312. void luaopen_upstream(lua_State *L);
  313. void luaopen_mempool(lua_State *L);
  314. void luaopen_dns_resolver(lua_State *L);
  315. void luaopen_rsa(lua_State *L);
  316. void luaopen_ip(lua_State *L);
  317. void luaopen_expression(lua_State *L);
  318. void luaopen_logger(lua_State *L);
  319. void luaopen_text(lua_State *L);
  320. void luaopen_util(lua_State *L);
  321. void luaopen_tcp(lua_State *L);
  322. void luaopen_html(lua_State *L);
  323. void luaopen_sqlite3(lua_State *L);
  324. void luaopen_cryptobox(lua_State *L);
  325. void luaopen_dns(lua_State *L);
  326. void luaopen_udp(lua_State *L);
  327. void luaopen_worker(lua_State *L);
  328. void luaopen_kann(lua_State *L);
  329. void luaopen_spf(lua_State *L);
  330. void luaopen_tensor(lua_State *L);
  331. void luaopen_parsers(lua_State *L);
  332. void rspamd_lua_dostring(const gchar *line);
  333. double rspamd_lua_normalize(struct rspamd_config *cfg,
  334. long double score,
  335. void *params);
  336. /* Config file functions */
  337. void rspamd_lua_post_load_config(struct rspamd_config *cfg);
  338. void rspamd_lua_dumpstack(lua_State *L);
  339. /* Set lua path according to the configuration */
  340. void rspamd_lua_set_path(lua_State *L, const ucl_object_t *cfg_obj,
  341. GHashTable *vars);
  342. /* Set some lua globals */
  343. gboolean rspamd_lua_set_env(lua_State *L, GHashTable *vars, char **lua_env,
  344. GError **err);
  345. void rspamd_lua_set_globals(struct rspamd_config *cfg, lua_State *L);
  346. struct memory_pool_s *rspamd_lua_check_mempool(lua_State *L, gint pos);
  347. struct rspamd_config *lua_check_config(lua_State *L, gint pos);
  348. struct rspamd_async_session *lua_check_session(lua_State *L, gint pos);
  349. struct ev_loop *lua_check_ev_base(lua_State *L, gint pos);
  350. struct rspamd_dns_resolver *lua_check_dns_resolver(lua_State *L, gint pos);
  351. struct rspamd_lua_url *lua_check_url(lua_State *L, gint pos);
  352. enum rspamd_lua_parse_arguments_flags {
  353. RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT = 0,
  354. RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING,
  355. };
  356. /**
  357. * Extract an arguments from lua table according to format string. Supported arguments are:
  358. * [*]key=S|I|N|B|V|U{a-z};[key=...]
  359. * - S - const char *
  360. * - I - int64_t_t
  361. * - i - int32_t
  362. * - N - double
  363. * - B - gboolean
  364. * - V - size_t + const char *
  365. * - U{classname} - userdata of the following class (stored in gpointer)
  366. * - F - function
  367. * - O - ucl_object_t *
  368. * - D - same as N but argument is set to NAN not to 0.0
  369. * - u{classname} - userdata of the following class (stored directly)
  370. *
  371. * If any of keys is prefixed with `*` then it is treated as required argument
  372. * @param L lua state
  373. * @param pos at which pos start extraction
  374. * @param err error pointer
  375. * @param how extraction type (IGNORE_MISSING means that default values will not be set)
  376. * @param extraction_pattern static pattern
  377. * @return TRUE if a table has been parsed
  378. */
  379. gboolean rspamd_lua_parse_table_arguments(lua_State *L, gint pos,
  380. GError **err,
  381. enum rspamd_lua_parse_arguments_flags how,
  382. const gchar *extraction_pattern, ...);
  383. gint rspamd_lua_traceback(lua_State *L);
  384. /**
  385. * Returns stack trace as a string. Caller should clear memory.
  386. * @param L
  387. * @return
  388. */
  389. void rspamd_lua_get_traceback_string(lua_State *L, luaL_Buffer *buf);
  390. /**
  391. * Returns size of table at position `tbl_pos`
  392. */
  393. guint rspamd_lua_table_size(lua_State *L, gint tbl_pos);
  394. void lua_push_emails_address_list(lua_State *L, GPtrArray *addrs, int flags);
  395. #define TRACE_POINTS 6
  396. struct lua_logger_trace {
  397. gint cur_level;
  398. gconstpointer traces[TRACE_POINTS];
  399. };
  400. enum lua_logger_escape_type {
  401. LUA_ESCAPE_NONE = (0u),
  402. LUA_ESCAPE_UNPRINTABLE = (1u << 0u),
  403. LUA_ESCAPE_NEWLINES = (1u << 1u),
  404. LUA_ESCAPE_8BIT = (1u << 2u),
  405. };
  406. #define LUA_ESCAPE_LOG (LUA_ESCAPE_UNPRINTABLE | LUA_ESCAPE_NEWLINES)
  407. #define LUA_ESCAPE_ALL (LUA_ESCAPE_UNPRINTABLE | LUA_ESCAPE_NEWLINES | LUA_ESCAPE_8BIT)
  408. /**
  409. * Log lua object to string
  410. * @param L
  411. * @param pos
  412. * @param outbuf
  413. * @param len
  414. * @return
  415. */
  416. gsize lua_logger_out_type(lua_State *L, gint pos, gchar *outbuf,
  417. gsize len, struct lua_logger_trace *trace,
  418. enum lua_logger_escape_type esc_type);
  419. /**
  420. * Safely checks userdata to match specified class
  421. * @param L
  422. * @param pos
  423. * @param classname **MUST BE STATIC**, direct address is used for comparisons!
  424. */
  425. void *rspamd_lua_check_udata(lua_State *L, gint pos, const gchar *classname);
  426. #define RSPAMD_LUA_CHECK_UDATA_PTR_OR_RETURN(L, pos, classname, type, dest) \
  427. do { \
  428. type **_maybe_ptr = (type **) rspamd_lua_check_udata((L), (pos), (classname)); \
  429. if (_maybe_ptr == NULL) { \
  430. return luaL_error(L, "%s: invalid arguments; pos = %d; expected = %s", G_STRFUNC, (pos), (classname)); \
  431. } \
  432. (dest) = *(_maybe_ptr); \
  433. } while (0)
  434. /**
  435. * Safely checks userdata to match specified class
  436. * @param L
  437. * @param pos
  438. * @param classname **MUST BE STATIC**, direct address is used for comparisons!
  439. */
  440. void *rspamd_lua_check_udata_maybe(lua_State *L, gint pos, const gchar *classname);
  441. /**
  442. * Call finishing script with the specified task
  443. * @param sc
  444. * @param task
  445. */
  446. void lua_call_finish_script(struct rspamd_config_cfg_lua_script *sc,
  447. struct rspamd_task *task);
  448. /**
  449. * Run post-load operations
  450. * @param L
  451. * @param cfg
  452. * @param ev_base
  453. */
  454. void rspamd_lua_run_postloads(lua_State *L, struct rspamd_config *cfg,
  455. struct ev_loop *ev_base, struct rspamd_worker *w);
  456. void rspamd_lua_run_config_post_init(lua_State *L, struct rspamd_config *cfg);
  457. void rspamd_lua_run_config_unload(lua_State *L, struct rspamd_config *cfg);
  458. /**
  459. * Adds new destructor for a local function for specific pool
  460. * @param L
  461. * @param pool
  462. * @param ref
  463. */
  464. void rspamd_lua_add_ref_dtor(lua_State *L, rspamd_mempool_t *pool,
  465. gint ref);
  466. /**
  467. * Returns a lua reference from a function like string, e.g. `return function(...) end`
  468. * @param L
  469. * @param str
  470. * @return
  471. */
  472. gint rspamd_lua_function_ref_from_str(lua_State *L, const gchar *str, gsize slen,
  473. const gchar *modname, GError **err);
  474. /**
  475. * Tries to load some module using `require` and get some method from it
  476. * @param L
  477. * @param modname
  478. * @param funcname
  479. * @return TRUE if function exists in that module, the function is pushed in stack, otherwise stack is unchanged and FALSE is returned
  480. */
  481. gboolean rspamd_lua_require_function(lua_State *L, const gchar *modname,
  482. const gchar *funcname);
  483. /**
  484. * Tries to load redis server definition from ucl object specified
  485. * @param L
  486. * @param obj
  487. * @param cfg
  488. * @return
  489. */
  490. gboolean rspamd_lua_try_load_redis(lua_State *L, const ucl_object_t *obj,
  491. struct rspamd_config *cfg, gint *ref_id);
  492. struct rspamd_stat_token_s;
  493. /**
  494. * Pushes a single word into Lua
  495. * @param L
  496. * @param word
  497. */
  498. void rspamd_lua_push_full_word(lua_State *L, struct rspamd_stat_token_s *word);
  499. enum rspamd_lua_words_type {
  500. RSPAMD_LUA_WORDS_STEM = 0,
  501. RSPAMD_LUA_WORDS_NORM,
  502. RSPAMD_LUA_WORDS_RAW,
  503. RSPAMD_LUA_WORDS_FULL,
  504. RSPAMD_LUA_WORDS_MAX
  505. };
  506. /**
  507. * Pushes words (rspamd_stat_token_t) to Lua
  508. * @param L
  509. * @param words
  510. * @param how
  511. */
  512. gint rspamd_lua_push_words(lua_State *L, GArray *words,
  513. enum rspamd_lua_words_type how);
  514. /**
  515. * Returns newly allocated name for caller module name
  516. * @param L
  517. * @return
  518. */
  519. gchar *rspamd_lua_get_module_name(lua_State *L);
  520. /**
  521. * Call Lua function in a universal way. Arguments string:
  522. * - i - lua_integer, argument - int64_t
  523. * - n - lua_number, argument - gdouble
  524. * - s - lua_string, argument - const gchar * (zero terminated)
  525. * - l - lua_lstring, argument - (size_t + const gchar *) pair
  526. * - u - lua_userdata, argument - (const char * + void *) - classname + pointer
  527. * - b - lua_boolean, argument - gboolean (not bool due to varargs promotion)
  528. * - f - lua_function, argument - int - position of the function on stack (not lua_registry)
  529. * - t - lua_text, argument - int - position of the lua_text on stack (not lua_registry)
  530. * @param L lua state
  531. * @param cbref LUA_REGISTRY reference (if it is -1 then a function on top of the stack is called - it must be removed by caller manually)
  532. * @param strloc where this function is called from
  533. * @param nret number of results (or LUA_MULTRET)
  534. * @param args arguments format string
  535. * @param err error to promote
  536. * @param ... arguments
  537. * @return true of pcall returned 0, false + err otherwise
  538. */
  539. bool rspamd_lua_universal_pcall(lua_State *L, gint cbref, const gchar *strloc,
  540. gint nret, const gchar *args, GError **err, ...);
  541. /**
  542. * Returns true if lua is initialised
  543. * @return
  544. */
  545. bool rspamd_lua_is_initialised(void);
  546. /**
  547. * Wrapper for lua_geti from lua 5.3
  548. * @param L
  549. * @param index
  550. * @param i
  551. * @return
  552. */
  553. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
  554. gint rspamd_lua_geti(lua_State *L, int index, int i);
  555. #else
  556. #define rspamd_lua_geti lua_geti
  557. #endif
  558. /* Paths defs */
  559. #define RSPAMD_CONFDIR_INDEX "CONFDIR"
  560. #define RSPAMD_LOCAL_CONFDIR_INDEX "LOCAL_CONFDIR"
  561. #define RSPAMD_RUNDIR_INDEX "RUNDIR"
  562. #define RSPAMD_DBDIR_INDEX "DBDIR"
  563. #define RSPAMD_LOGDIR_INDEX "LOGDIR"
  564. #define RSPAMD_PLUGINSDIR_INDEX "PLUGINSDIR"
  565. #define RSPAMD_SHAREDIR_INDEX "SHAREDIR"
  566. #define RSPAMD_RULESDIR_INDEX "RULESDIR"
  567. #define RSPAMD_LUALIBDIR_INDEX "LUALIBDIR"
  568. #define RSPAMD_WWWDIR_INDEX "WWWDIR"
  569. #define RSPAMD_PREFIX_INDEX "PREFIX"
  570. #define RSPAMD_VERSION_INDEX "VERSION"
  571. #ifdef WITH_LUA_TRACE
  572. extern ucl_object_t *lua_traces;
  573. #define LUA_TRACE_POINT \
  574. do { \
  575. ucl_object_t *func_obj; \
  576. if (lua_traces == NULL) { lua_traces = ucl_object_typed_new(UCL_OBJECT); } \
  577. func_obj = (ucl_object_t *) ucl_object_lookup(lua_traces, G_STRFUNC); \
  578. if (func_obj == NULL) { \
  579. func_obj = ucl_object_typed_new(UCL_INT); \
  580. ucl_object_insert_key(lua_traces, func_obj, G_STRFUNC, 0, false); \
  581. } \
  582. func_obj->value.iv++; \
  583. } while (0)
  584. #else
  585. #define LUA_TRACE_POINT \
  586. do { \
  587. } while (0)
  588. #endif
  589. #ifdef __cplusplus
  590. }
  591. #endif
  592. #endif /* RSPAMD_LUA_H */