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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. */
  150. void rspamd_lua_setclass(lua_State *L, const gchar *classname, gint objidx);
  151. /**
  152. * Pushes the metatable for specific class on top of the stack
  153. * @param L
  154. * @param classname
  155. */
  156. void rspamd_lua_class_metatable(lua_State *L, const gchar *classname);
  157. /**
  158. * Adds a new field to the class (metatable) identified by `classname`
  159. * @param L
  160. * @param classname
  161. * @param meth
  162. */
  163. void rspamd_lua_add_metamethod(lua_State *L, const gchar *classname,
  164. luaL_Reg *meth);
  165. /**
  166. * Set index of table to value (like t['index'] = value)
  167. */
  168. void rspamd_lua_table_set(lua_State *L, const gchar *index, const gchar *value);
  169. /**
  170. * Get string value of index in a table (return t['index'])
  171. */
  172. const gchar *rspamd_lua_table_get(lua_State *L, const gchar *index);
  173. /**
  174. * Convert classname to string
  175. */
  176. gint rspamd_lua_class_tostring(lua_State *L);
  177. /**
  178. * Check whether the argument at specified index is of the specified class
  179. */
  180. gpointer rspamd_lua_check_class(lua_State *L, gint index, const gchar *name);
  181. /**
  182. * Initialize lua and bindings
  183. */
  184. lua_State *rspamd_lua_init(bool wipe_mem);
  185. /**
  186. * Close lua_state and free remainders
  187. * @param L
  188. */
  189. void rspamd_lua_close(lua_State *L);
  190. void rspamd_lua_start_gc(struct rspamd_config *cfg);
  191. /**
  192. * Sets field in a global variable
  193. * @param L
  194. * @param global_name
  195. * @param field_name
  196. * @param new_elt
  197. */
  198. void rspamd_plugins_table_push_elt(lua_State *L, const gchar *field_name,
  199. const gchar *new_elt);
  200. /**
  201. * Load and initialize lua plugins
  202. */
  203. gboolean
  204. rspamd_init_lua_filters(struct rspamd_config *cfg, bool force_load, bool strict);
  205. /**
  206. * Push lua ip address
  207. */
  208. void rspamd_lua_ip_push(lua_State *L, rspamd_inet_addr_t *addr);
  209. /**
  210. * Push rspamd task structure to lua
  211. */
  212. void rspamd_lua_task_push(lua_State *L, struct rspamd_task *task);
  213. /**
  214. * Return lua ip structure at the specified address
  215. */
  216. struct rspamd_lua_ip *lua_check_ip(lua_State *L, gint pos);
  217. struct rspamd_lua_text *lua_check_text(lua_State *L, gint pos);
  218. /**
  219. * Checks for a text or a string. In case of string a pointer to static structure is returned.
  220. * So it should not be reused or placed to Lua stack anyhow!
  221. * However, you can use this function up to 4 times and have distinct static structures
  222. * @param L
  223. * @param pos
  224. * @return
  225. */
  226. struct rspamd_lua_text *lua_check_text_or_string(lua_State *L, gint pos);
  227. /**
  228. * Create new text object
  229. * @param L
  230. * @param start
  231. * @param len
  232. * @param own
  233. * @return
  234. */
  235. struct rspamd_lua_text *lua_new_text(lua_State *L, const gchar *start,
  236. gsize len, gboolean own);
  237. /**
  238. * Create new text object from task pool if allocation is needed
  239. * @param task
  240. * @param L
  241. * @param start
  242. * @param len
  243. * @param own
  244. * @return
  245. */
  246. struct rspamd_lua_text *lua_new_text_task(lua_State *L, struct rspamd_task *task,
  247. const gchar *start, gsize len, gboolean own);
  248. /**
  249. * Checks if a text has binary characters (non ascii and non-utf8 characters)
  250. * @param t
  251. * @return
  252. */
  253. bool lua_is_text_binary(struct rspamd_lua_text *t);
  254. struct rspamd_lua_regexp *lua_check_regexp(lua_State *L, gint pos);
  255. struct rspamd_lua_upstream *lua_check_upstream(lua_State *L, int pos);
  256. enum rspamd_lua_task_header_type {
  257. RSPAMD_TASK_HEADER_PUSH_SIMPLE = 0,
  258. RSPAMD_TASK_HEADER_PUSH_RAW,
  259. RSPAMD_TASK_HEADER_PUSH_FULL,
  260. RSPAMD_TASK_HEADER_PUSH_COUNT,
  261. RSPAMD_TASK_HEADER_PUSH_HAS,
  262. };
  263. gint rspamd_lua_push_header(lua_State *L,
  264. struct rspamd_mime_header *h,
  265. enum rspamd_lua_task_header_type how);
  266. /**
  267. * Push specific header to lua
  268. */
  269. gint rspamd_lua_push_header_array(lua_State *L,
  270. const gchar *name,
  271. struct rspamd_mime_header *rh,
  272. enum rspamd_lua_task_header_type how,
  273. gboolean strong);
  274. /**
  275. * Check for task at the specified position
  276. */
  277. struct rspamd_task *lua_check_task(lua_State *L, gint pos);
  278. struct rspamd_task *lua_check_task_maybe(lua_State *L, gint pos);
  279. struct rspamd_lua_map *lua_check_map(lua_State *L, gint pos);
  280. /**
  281. * Push ip address from a string (nil is pushed if a string cannot be converted)
  282. */
  283. void rspamd_lua_ip_push_fromstring(lua_State *L, const gchar *ip_str);
  284. /**
  285. * Create type error
  286. */
  287. int rspamd_lua_typerror(lua_State *L, int narg, const char *tname);
  288. /**
  289. * Open libraries functions
  290. */
  291. /**
  292. * Add preload function
  293. */
  294. void rspamd_lua_add_preload(lua_State *L, const gchar *name, lua_CFunction func);
  295. void luaopen_task(lua_State *L);
  296. void luaopen_config(lua_State *L);
  297. void luaopen_map(lua_State *L);
  298. void luaopen_trie(lua_State *L);
  299. void luaopen_textpart(lua_State *L);
  300. void luaopen_mimepart(lua_State *L);
  301. void luaopen_image(lua_State *L);
  302. void luaopen_url(lua_State *L);
  303. void luaopen_classifier(lua_State *L);
  304. void luaopen_statfile(lua_State *L);
  305. void luaopen_regexp(lua_State *L);
  306. void luaopen_cdb(lua_State *L);
  307. void luaopen_xmlrpc(lua_State *L);
  308. void luaopen_http(lua_State *L);
  309. void luaopen_redis(lua_State *L);
  310. void luaopen_upstream(lua_State *L);
  311. void luaopen_mempool(lua_State *L);
  312. void luaopen_dns_resolver(lua_State *L);
  313. void luaopen_rsa(lua_State *L);
  314. void luaopen_ip(lua_State *L);
  315. void luaopen_expression(lua_State *L);
  316. void luaopen_logger(lua_State *L);
  317. void luaopen_text(lua_State *L);
  318. void luaopen_util(lua_State *L);
  319. void luaopen_tcp(lua_State *L);
  320. void luaopen_html(lua_State *L);
  321. void luaopen_sqlite3(lua_State *L);
  322. void luaopen_cryptobox(lua_State *L);
  323. void luaopen_dns(lua_State *L);
  324. void luaopen_udp(lua_State *L);
  325. void luaopen_worker(lua_State *L);
  326. void luaopen_kann(lua_State *L);
  327. void luaopen_spf(lua_State *L);
  328. void luaopen_tensor(lua_State *L);
  329. void luaopen_parsers(lua_State *L);
  330. void rspamd_lua_dostring(const gchar *line);
  331. double rspamd_lua_normalize(struct rspamd_config *cfg,
  332. long double score,
  333. void *params);
  334. /* Config file functions */
  335. void rspamd_lua_post_load_config(struct rspamd_config *cfg);
  336. void rspamd_lua_dumpstack(lua_State *L);
  337. /* Set lua path according to the configuration */
  338. void rspamd_lua_set_path(lua_State *L, const ucl_object_t *cfg_obj,
  339. GHashTable *vars);
  340. /* Set some lua globals */
  341. gboolean rspamd_lua_set_env(lua_State *L, GHashTable *vars, char **lua_env,
  342. GError **err);
  343. void rspamd_lua_set_globals(struct rspamd_config *cfg, lua_State *L);
  344. struct memory_pool_s *rspamd_lua_check_mempool(lua_State *L, gint pos);
  345. struct rspamd_config *lua_check_config(lua_State *L, gint pos);
  346. struct rspamd_async_session *lua_check_session(lua_State *L, gint pos);
  347. struct ev_loop *lua_check_ev_base(lua_State *L, gint pos);
  348. struct rspamd_dns_resolver *lua_check_dns_resolver(lua_State *L, gint pos);
  349. struct rspamd_lua_url *lua_check_url(lua_State *L, gint pos);
  350. enum rspamd_lua_parse_arguments_flags {
  351. RSPAMD_LUA_PARSE_ARGUMENTS_DEFAULT = 0,
  352. RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING,
  353. };
  354. /**
  355. * Extract an arguments from lua table according to format string. Supported arguments are:
  356. * [*]key=S|I|N|B|V|U{a-z};[key=...]
  357. * - S - const char *
  358. * - I - gint64_t
  359. * - i - int32_t
  360. * - N - double
  361. * - B - gboolean
  362. * - V - size_t + const char *
  363. * - U{classname} - userdata of the following class (stored in gpointer)
  364. * - F - function
  365. * - O - ucl_object_t *
  366. * - D - same as N but argument is set to NAN not to 0.0
  367. * - u{classname} - userdata of the following class (stored directly)
  368. *
  369. * If any of keys is prefixed with `*` then it is treated as required argument
  370. * @param L lua state
  371. * @param pos at which pos start extraction
  372. * @param err error pointer
  373. * @param how extraction type (IGNORE_MISSING means that default values will not be set)
  374. * @param extraction_pattern static pattern
  375. * @return TRUE if a table has been parsed
  376. */
  377. gboolean rspamd_lua_parse_table_arguments(lua_State *L, gint pos,
  378. GError **err,
  379. enum rspamd_lua_parse_arguments_flags how,
  380. const gchar *extraction_pattern, ...);
  381. gint rspamd_lua_traceback(lua_State *L);
  382. /**
  383. * Returns stack trace as a string. Caller should clear memory.
  384. * @param L
  385. * @return
  386. */
  387. void rspamd_lua_get_traceback_string(lua_State *L, luaL_Buffer *buf);
  388. /**
  389. * Returns size of table at position `tbl_pos`
  390. */
  391. guint rspamd_lua_table_size(lua_State *L, gint tbl_pos);
  392. void lua_push_emails_address_list(lua_State *L, GPtrArray *addrs, int flags);
  393. #define TRACE_POINTS 6
  394. struct lua_logger_trace {
  395. gint cur_level;
  396. gconstpointer traces[TRACE_POINTS];
  397. };
  398. enum lua_logger_escape_type {
  399. LUA_ESCAPE_NONE = (0u),
  400. LUA_ESCAPE_UNPRINTABLE = (1u << 0u),
  401. LUA_ESCAPE_NEWLINES = (1u << 1u),
  402. LUA_ESCAPE_8BIT = (1u << 2u),
  403. };
  404. #define LUA_ESCAPE_LOG (LUA_ESCAPE_UNPRINTABLE | LUA_ESCAPE_NEWLINES)
  405. #define LUA_ESCAPE_ALL (LUA_ESCAPE_UNPRINTABLE | LUA_ESCAPE_NEWLINES | LUA_ESCAPE_8BIT)
  406. /**
  407. * Log lua object to string
  408. * @param L
  409. * @param pos
  410. * @param outbuf
  411. * @param len
  412. * @return
  413. */
  414. gsize lua_logger_out_type(lua_State *L, gint pos, gchar *outbuf,
  415. gsize len, struct lua_logger_trace *trace,
  416. enum lua_logger_escape_type esc_type);
  417. /**
  418. * Safely checks userdata to match specified class
  419. * @param L
  420. * @param pos
  421. * @param classname
  422. */
  423. void *rspamd_lua_check_udata(lua_State *L, gint pos, const gchar *classname);
  424. #define RSPAMD_LUA_CHECK_UDATA_PTR_OR_RETURN(L, pos, classname, type, dest) \
  425. do { \
  426. type **_maybe_ptr = (type **) rspamd_lua_check_udata((L), (pos), (classname)); \
  427. if (_maybe_ptr == NULL) { \
  428. return luaL_error(L, "%s: invalid arguments; pos = %d; expected = %s", G_STRFUNC, (pos), (classname)); \
  429. } \
  430. (dest) = *(_maybe_ptr); \
  431. } while (0)
  432. /**
  433. * Safely checks userdata to match specified class
  434. * @param L
  435. * @param pos
  436. * @param classname
  437. */
  438. void *rspamd_lua_check_udata_maybe(lua_State *L, gint pos, const gchar *classname);
  439. /**
  440. * Call finishing script with the specified task
  441. * @param sc
  442. * @param task
  443. */
  444. void lua_call_finish_script(struct rspamd_config_cfg_lua_script *sc,
  445. struct rspamd_task *task);
  446. /**
  447. * Run post-load operations
  448. * @param L
  449. * @param cfg
  450. * @param ev_base
  451. */
  452. void rspamd_lua_run_postloads(lua_State *L, struct rspamd_config *cfg,
  453. struct ev_loop *ev_base, struct rspamd_worker *w);
  454. void rspamd_lua_run_config_post_init(lua_State *L, struct rspamd_config *cfg);
  455. void rspamd_lua_run_config_unload(lua_State *L, struct rspamd_config *cfg);
  456. /**
  457. * Adds new destructor for a local function for specific pool
  458. * @param L
  459. * @param pool
  460. * @param ref
  461. */
  462. void rspamd_lua_add_ref_dtor(lua_State *L, rspamd_mempool_t *pool,
  463. gint ref);
  464. /**
  465. * Returns a lua reference from a function like string, e.g. `return function(...) end`
  466. * @param L
  467. * @param str
  468. * @return
  469. */
  470. gint rspamd_lua_function_ref_from_str(lua_State *L, const gchar *str, gsize slen,
  471. const gchar *modname, GError **err);
  472. /**
  473. * Tries to load some module using `require` and get some method from it
  474. * @param L
  475. * @param modname
  476. * @param funcname
  477. * @return TRUE if function exists in that module, the function is pushed in stack, otherwise stack is unchanged and FALSE is returned
  478. */
  479. gboolean rspamd_lua_require_function(lua_State *L, const gchar *modname,
  480. const gchar *funcname);
  481. /**
  482. * Tries to load redis server definition from ucl object specified
  483. * @param L
  484. * @param obj
  485. * @param cfg
  486. * @return
  487. */
  488. gboolean rspamd_lua_try_load_redis(lua_State *L, const ucl_object_t *obj,
  489. struct rspamd_config *cfg, gint *ref_id);
  490. struct rspamd_stat_token_s;
  491. /**
  492. * Pushes a single word into Lua
  493. * @param L
  494. * @param word
  495. */
  496. void rspamd_lua_push_full_word(lua_State *L, struct rspamd_stat_token_s *word);
  497. enum rspamd_lua_words_type {
  498. RSPAMD_LUA_WORDS_STEM = 0,
  499. RSPAMD_LUA_WORDS_NORM,
  500. RSPAMD_LUA_WORDS_RAW,
  501. RSPAMD_LUA_WORDS_FULL,
  502. RSPAMD_LUA_WORDS_MAX
  503. };
  504. /**
  505. * Pushes words (rspamd_stat_token_t) to Lua
  506. * @param L
  507. * @param words
  508. * @param how
  509. */
  510. gint rspamd_lua_push_words(lua_State *L, GArray *words,
  511. enum rspamd_lua_words_type how);
  512. /**
  513. * Returns newly allocated name for caller module name
  514. * @param L
  515. * @return
  516. */
  517. gchar *rspamd_lua_get_module_name(lua_State *L);
  518. /**
  519. * Call Lua function in a universal way. Arguments string:
  520. * - i - lua_integer, argument - gint64
  521. * - n - lua_number, argument - gdouble
  522. * - s - lua_string, argument - const gchar * (zero terminated)
  523. * - l - lua_lstring, argument - (size_t + const gchar *) pair
  524. * - u - lua_userdata, argument - (const char * + void *) - classname + pointer
  525. * - b - lua_boolean, argument - gboolean (not bool due to varargs promotion)
  526. * - f - lua_function, argument - int - position of the function on stack (not lua_registry)
  527. * - t - lua_text, argument - int - position of the lua_text on stack (not lua_registry)
  528. * @param L lua state
  529. * @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)
  530. * @param strloc where this function is called from
  531. * @param nret number of results (or LUA_MULTRET)
  532. * @param args arguments format string
  533. * @param err error to promote
  534. * @param ... arguments
  535. * @return true of pcall returned 0, false + err otherwise
  536. */
  537. bool rspamd_lua_universal_pcall(lua_State *L, gint cbref, const gchar *strloc,
  538. gint nret, const gchar *args, GError **err, ...);
  539. /**
  540. * Returns true if lua is initialised
  541. * @return
  542. */
  543. bool rspamd_lua_is_initialised(void);
  544. /**
  545. * Wrapper for lua_geti from lua 5.3
  546. * @param L
  547. * @param index
  548. * @param i
  549. * @return
  550. */
  551. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
  552. gint rspamd_lua_geti(lua_State *L, int index, int i);
  553. #else
  554. #define rspamd_lua_geti lua_geti
  555. #endif
  556. /* Paths defs */
  557. #define RSPAMD_CONFDIR_INDEX "CONFDIR"
  558. #define RSPAMD_LOCAL_CONFDIR_INDEX "LOCAL_CONFDIR"
  559. #define RSPAMD_RUNDIR_INDEX "RUNDIR"
  560. #define RSPAMD_DBDIR_INDEX "DBDIR"
  561. #define RSPAMD_LOGDIR_INDEX "LOGDIR"
  562. #define RSPAMD_PLUGINSDIR_INDEX "PLUGINSDIR"
  563. #define RSPAMD_SHAREDIR_INDEX "SHAREDIR"
  564. #define RSPAMD_RULESDIR_INDEX "RULESDIR"
  565. #define RSPAMD_LUALIBDIR_INDEX "LUALIBDIR"
  566. #define RSPAMD_WWWDIR_INDEX "WWWDIR"
  567. #define RSPAMD_PREFIX_INDEX "PREFIX"
  568. #define RSPAMD_VERSION_INDEX "VERSION"
  569. #ifdef WITH_LUA_TRACE
  570. extern ucl_object_t *lua_traces;
  571. #define LUA_TRACE_POINT \
  572. do { \
  573. ucl_object_t *func_obj; \
  574. if (lua_traces == NULL) { lua_traces = ucl_object_typed_new(UCL_OBJECT); } \
  575. func_obj = (ucl_object_t *) ucl_object_lookup(lua_traces, G_STRFUNC); \
  576. if (func_obj == NULL) { \
  577. func_obj = ucl_object_typed_new(UCL_INT); \
  578. ucl_object_insert_key(lua_traces, func_obj, G_STRFUNC, 0, false); \
  579. } \
  580. func_obj->value.iv++; \
  581. } while (0)
  582. #else
  583. #define LUA_TRACE_POINT \
  584. do { \
  585. } while (0)
  586. #endif
  587. #ifdef __cplusplus
  588. }
  589. #endif
  590. #endif /* RSPAMD_LUA_H */