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_config.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * Copyright (c) 2009, Rambler media
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY Rambler media ''AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL Rambler BE LIABLE FOR ANY
  17. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "lua_common.h"
  25. #include "../expressions.h"
  26. #include "../map.h"
  27. #include "../message.h"
  28. #include "../radix.h"
  29. #include "../trie.h"
  30. #include "../classifiers/classifiers.h"
  31. #include "../cfg_xml.h"
  32. /* Config file methods */
  33. LUA_FUNCTION_DEF (config, get_module_opt);
  34. LUA_FUNCTION_DEF (config, get_all_opt);
  35. LUA_FUNCTION_DEF (config, register_function);
  36. LUA_FUNCTION_DEF (config, add_radix_map);
  37. LUA_FUNCTION_DEF (config, add_hash_map);
  38. LUA_FUNCTION_DEF (config, get_classifier);
  39. LUA_FUNCTION_DEF (config, register_symbol);
  40. LUA_FUNCTION_DEF (config, register_virtual_symbol);
  41. LUA_FUNCTION_DEF (config, register_callback_symbol);
  42. LUA_FUNCTION_DEF (config, register_post_filter);
  43. LUA_FUNCTION_DEF (config, register_module_option);
  44. LUA_FUNCTION_DEF (config, get_api_version);
  45. static const struct luaL_reg configlib_m[] = {
  46. LUA_INTERFACE_DEF (config, get_module_opt),
  47. LUA_INTERFACE_DEF (config, get_all_opt),
  48. LUA_INTERFACE_DEF (config, register_function),
  49. LUA_INTERFACE_DEF (config, add_radix_map),
  50. LUA_INTERFACE_DEF (config, add_hash_map),
  51. LUA_INTERFACE_DEF (config, get_classifier),
  52. LUA_INTERFACE_DEF (config, register_symbol),
  53. LUA_INTERFACE_DEF (config, register_virtual_symbol),
  54. LUA_INTERFACE_DEF (config, register_callback_symbol),
  55. LUA_INTERFACE_DEF (config, register_module_option),
  56. LUA_INTERFACE_DEF (config, register_post_filter),
  57. LUA_INTERFACE_DEF (config, get_api_version),
  58. {"__tostring", lua_class_tostring},
  59. {NULL, NULL}
  60. };
  61. /* Radix tree */
  62. LUA_FUNCTION_DEF (radix, get_key);
  63. static const struct luaL_reg radixlib_m[] = {
  64. LUA_INTERFACE_DEF (radix, get_key),
  65. {"__tostring", lua_class_tostring},
  66. {NULL, NULL}
  67. };
  68. /* Hash table */
  69. LUA_FUNCTION_DEF (hash_table, get_key);
  70. static const struct luaL_reg hashlib_m[] = {
  71. LUA_INTERFACE_DEF (hash_table, get_key),
  72. {"__tostring", lua_class_tostring},
  73. {NULL, NULL}
  74. };
  75. /* Suffix trie */
  76. LUA_FUNCTION_DEF (trie, create);
  77. LUA_FUNCTION_DEF (trie, add_pattern);
  78. LUA_FUNCTION_DEF (trie, search_text);
  79. LUA_FUNCTION_DEF (trie, search_task);
  80. static const struct luaL_reg trielib_m[] = {
  81. LUA_INTERFACE_DEF (trie, add_pattern),
  82. LUA_INTERFACE_DEF (trie, search_text),
  83. LUA_INTERFACE_DEF (trie, search_task),
  84. {"__tostring", lua_class_tostring},
  85. {NULL, NULL}
  86. };
  87. static const struct luaL_reg trielib_f[] = {
  88. LUA_INTERFACE_DEF (trie, create),
  89. {NULL, NULL}
  90. };
  91. static struct config_file *
  92. lua_check_config (lua_State * L)
  93. {
  94. void *ud = luaL_checkudata (L, 1, "rspamd{config}");
  95. luaL_argcheck (L, ud != NULL, 1, "'config' expected");
  96. return *((struct config_file **)ud);
  97. }
  98. static radix_tree_t *
  99. lua_check_radix (lua_State * L)
  100. {
  101. void *ud = luaL_checkudata (L, 1, "rspamd{radix}");
  102. luaL_argcheck (L, ud != NULL, 1, "'radix' expected");
  103. return **((radix_tree_t ***)ud);
  104. }
  105. static GHashTable *
  106. lua_check_hash_table (lua_State * L)
  107. {
  108. void *ud = luaL_checkudata (L, 1, "rspamd{hash_table}");
  109. luaL_argcheck (L, ud != NULL, 1, "'hash_table' expected");
  110. return **((GHashTable ***)ud);
  111. }
  112. static rspamd_trie_t *
  113. lua_check_trie (lua_State * L)
  114. {
  115. void *ud = luaL_checkudata (L, 1, "rspamd{trie}");
  116. luaL_argcheck (L, ud != NULL, 1, "'trie' expected");
  117. return *((rspamd_trie_t **)ud);
  118. }
  119. /*** Config functions ***/
  120. static gint
  121. lua_config_get_api_version (lua_State *L)
  122. {
  123. lua_pushinteger (L, RSPAMD_LUA_API_VERSION);
  124. return 1;
  125. }
  126. static gint
  127. lua_config_get_module_opt (lua_State * L)
  128. {
  129. struct config_file *cfg = lua_check_config (L);
  130. const gchar *mname, *optname, *val;
  131. if (cfg) {
  132. mname = luaL_checkstring (L, 2);
  133. optname = luaL_checkstring (L, 3);
  134. if (mname && optname) {
  135. val = get_module_opt (cfg, (gchar *)mname, (gchar *)optname);
  136. if (val) {
  137. lua_pushstring (L, val);
  138. return 1;
  139. }
  140. }
  141. }
  142. lua_pushnil (L);
  143. return 1;
  144. }
  145. static gint
  146. opt_compare (gconstpointer a, gconstpointer b)
  147. {
  148. const struct module_opt *o1 = a,
  149. *o2 = b;
  150. return g_ascii_strcasecmp (o1->param, o2->param);
  151. }
  152. static gint
  153. lua_config_get_all_opt (lua_State * L)
  154. {
  155. struct config_file *cfg = lua_check_config (L);
  156. const gchar *mname;
  157. GList *cur_opt, *next_opt;
  158. struct module_opt *opt, *tmp;
  159. gint i;
  160. if (cfg) {
  161. mname = luaL_checkstring (L, 2);
  162. if (mname) {
  163. cur_opt = g_hash_table_lookup (cfg->modules_opts, mname);
  164. if (cur_opt == NULL) {
  165. lua_pushnil (L);
  166. return 1;
  167. }
  168. /* Sort options in alphabet order by param name */
  169. cur_opt = g_list_sort (cur_opt, opt_compare);
  170. g_hash_table_insert (cfg->modules_opts, (gpointer)mname, cur_opt);
  171. lua_newtable (L);
  172. while (cur_opt) {
  173. opt = cur_opt->data;
  174. next_opt = g_list_next (cur_opt);
  175. if (next_opt) {
  176. tmp = next_opt->data;
  177. if (g_ascii_strcasecmp (tmp->param, opt->param) == 0) {
  178. /* We have some common values */
  179. lua_pushstring (L, opt->param);
  180. lua_newtable (L);
  181. /* Now stack looks like:
  182. * table - parent associated table of options
  183. * key - string key of this option
  184. * table - array of values, beginig from 1
  185. */
  186. for (i = 1; ; i++) {
  187. lua_pushinteger (L, i);
  188. lua_pushstring (L, opt->value);
  189. lua_settable (L, -3);
  190. cur_opt = g_list_next (cur_opt);
  191. if (!cur_opt) {
  192. break;
  193. }
  194. tmp = cur_opt->data;
  195. if (g_ascii_strcasecmp (tmp->param, opt->param) != 0) {
  196. break;
  197. }
  198. opt = tmp;
  199. }
  200. /* Now set index in parent table */
  201. lua_settable (L, -3);
  202. /* Now continue in outter cycle */
  203. continue;
  204. }
  205. else {
  206. lua_set_table_index (L, opt->param, opt->value);
  207. }
  208. }
  209. else {
  210. lua_set_table_index (L, opt->param, opt->value);
  211. }
  212. cur_opt = next_opt;
  213. }
  214. return 1;
  215. }
  216. }
  217. lua_pushnil (L);
  218. return 1;
  219. }
  220. static gint
  221. lua_config_get_classifier (lua_State * L)
  222. {
  223. struct config_file *cfg = lua_check_config (L);
  224. struct classifier_config *clc = NULL, **pclc = NULL;
  225. const gchar *name;
  226. GList *cur;
  227. if (cfg) {
  228. name = luaL_checkstring (L, 2);
  229. cur = g_list_first (cfg->classifiers);
  230. while (cur) {
  231. clc = cur->data;
  232. if (g_ascii_strcasecmp (clc->classifier->name, name) == 0) {
  233. pclc = &clc;
  234. break;
  235. }
  236. cur = g_list_next (cur);
  237. }
  238. if (pclc) {
  239. pclc = lua_newuserdata (L, sizeof (struct classifier_config *));
  240. lua_setclass (L, "rspamd{classifier}", -1);
  241. *pclc = clc;
  242. return 1;
  243. }
  244. }
  245. lua_pushnil (L);
  246. return 1;
  247. }
  248. struct lua_callback_data {
  249. const gchar *name;
  250. lua_State *L;
  251. };
  252. static gboolean
  253. lua_config_function_callback (struct worker_task *task, GList *args, void *user_data)
  254. {
  255. struct lua_callback_data *cd = user_data;
  256. struct worker_task **ptask;
  257. gint i = 1;
  258. struct expression_argument *arg;
  259. GList *cur;
  260. gboolean res = FALSE;
  261. lua_getglobal (cd->L, cd->name);
  262. ptask = lua_newuserdata (cd->L, sizeof (struct worker_task *));
  263. lua_setclass (cd->L, "rspamd{task}", -1);
  264. *ptask = task;
  265. /* Now push all arguments */
  266. cur = args;
  267. while (cur) {
  268. arg = get_function_arg (cur->data, task, TRUE);
  269. lua_pushstring (cd->L, (const gchar *)arg->data);
  270. cur = g_list_next (cur);
  271. i ++;
  272. }
  273. if (lua_pcall (cd->L, i, 1, 0) != 0) {
  274. msg_warn ("error running function %s: %s", cd->name, lua_tostring (cd->L, -1));
  275. }
  276. else {
  277. if (lua_isboolean (cd->L, 1)) {
  278. res = lua_toboolean (cd->L, 1);
  279. }
  280. }
  281. return res;
  282. }
  283. static gint
  284. lua_config_register_function (lua_State *L)
  285. {
  286. struct config_file *cfg = lua_check_config (L);
  287. const gchar *name, *callback;
  288. struct lua_callback_data *cd;
  289. if (cfg) {
  290. name = g_strdup (luaL_checkstring (L, 2));
  291. callback = luaL_checkstring (L, 3);
  292. if (name) {
  293. cd = g_malloc (sizeof (struct lua_callback_data));
  294. cd->name = g_strdup (callback);
  295. cd->L = L;
  296. register_expression_function (name, lua_config_function_callback, cd);
  297. }
  298. }
  299. return 1;
  300. }
  301. static gint
  302. lua_config_register_module_option (lua_State *L)
  303. {
  304. struct config_file *cfg = lua_check_config (L);
  305. const gchar *mname, *optname, *stype;
  306. enum module_opt_type type;
  307. if (cfg) {
  308. mname = memory_pool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2));
  309. optname = memory_pool_strdup (cfg->cfg_pool, luaL_checkstring (L, 3));
  310. stype = memory_pool_strdup (cfg->cfg_pool, luaL_checkstring (L, 4));
  311. if (mname && optname) {
  312. if (stype == NULL) {
  313. stype = "string";
  314. }
  315. if (g_ascii_strcasecmp (stype, "string") == 0) {
  316. type = MODULE_OPT_TYPE_STRING;
  317. }
  318. else if (g_ascii_strcasecmp (stype, "int") == 0) {
  319. type = MODULE_OPT_TYPE_INT;
  320. }
  321. else if (g_ascii_strcasecmp (stype, "uint") == 0) {
  322. type = MODULE_OPT_TYPE_UINT;
  323. }
  324. else if (g_ascii_strcasecmp (stype, "time") == 0) {
  325. type = MODULE_OPT_TYPE_TIME;
  326. }
  327. else if (g_ascii_strcasecmp (stype, "size") == 0) {
  328. type = MODULE_OPT_TYPE_SIZE;
  329. }
  330. else if (g_ascii_strcasecmp (stype, "map") == 0) {
  331. type = MODULE_OPT_TYPE_MAP;
  332. }
  333. else if (g_ascii_strcasecmp (stype, "double") == 0) {
  334. type = MODULE_OPT_TYPE_DOUBLE;
  335. }
  336. else {
  337. msg_err ("unknown type '%s' for option: %s, for module: %s", stype, optname, mname);
  338. luaL_error (L, "unknown type '%s' for option: %s, for module: %s", stype, optname, mname);
  339. return 0;
  340. }
  341. register_module_opt (mname, optname, type);
  342. return 1;
  343. }
  344. luaL_error (L, "bad arguments for register module option, must be: register_module_option(modulename, optionname, optiontype)");
  345. }
  346. return 0;
  347. }
  348. void
  349. lua_call_post_filters (struct worker_task *task)
  350. {
  351. struct lua_callback_data *cd;
  352. struct worker_task **ptask;
  353. GList *cur;
  354. cur = task->cfg->post_filters;
  355. while (cur) {
  356. cd = cur->data;
  357. lua_getglobal (cd->L, cd->name);
  358. ptask = lua_newuserdata (cd->L, sizeof (struct worker_task *));
  359. lua_setclass (cd->L, "rspamd{task}", -1);
  360. *ptask = task;
  361. if (lua_pcall (cd->L, 1, 1, 0) != 0) {
  362. msg_warn ("error running function %s: %s", cd->name, lua_tostring (cd->L, -1));
  363. }
  364. cur = g_list_next (cur);
  365. }
  366. }
  367. static gint
  368. lua_config_register_post_filter (lua_State *L)
  369. {
  370. struct config_file *cfg = lua_check_config (L);
  371. const gchar *callback;
  372. struct lua_callback_data *cd;
  373. if (cfg) {
  374. callback = luaL_checkstring (L, 2);
  375. if (callback) {
  376. cd = g_malloc (sizeof (struct lua_callback_data));
  377. cd->name = g_strdup (callback);
  378. cd->L = L;
  379. cfg->post_filters = g_list_prepend (cfg->post_filters, cd);
  380. }
  381. }
  382. return 1;
  383. }
  384. static gint
  385. lua_config_add_radix_map (lua_State *L)
  386. {
  387. struct config_file *cfg = lua_check_config (L);
  388. const gchar *map_line;
  389. radix_tree_t **r, ***ud;
  390. if (cfg) {
  391. map_line = luaL_checkstring (L, 2);
  392. r = g_malloc (sizeof (radix_tree_t *));
  393. *r = radix_tree_create ();
  394. if (!add_map (map_line, read_radix_list, fin_radix_list, (void **)r)) {
  395. msg_warn ("invalid radix map %s", map_line);
  396. radix_tree_free (*r);
  397. g_free (r);
  398. lua_pushnil (L);
  399. return 1;
  400. }
  401. ud = lua_newuserdata (L, sizeof (radix_tree_t *));
  402. *ud = r;
  403. lua_setclass (L, "rspamd{radix}", -1);
  404. return 1;
  405. }
  406. lua_pushnil (L);
  407. return 1;
  408. }
  409. static gint
  410. lua_config_add_hash_map (lua_State *L)
  411. {
  412. struct config_file *cfg = lua_check_config (L);
  413. const gchar *map_line;
  414. GHashTable **r, ***ud;
  415. if (cfg) {
  416. map_line = luaL_checkstring (L, 2);
  417. r = g_malloc (sizeof (GHashTable *));
  418. *r = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
  419. if (!add_map (map_line, read_host_list, fin_host_list, (void **)r)) {
  420. msg_warn ("invalid hash map %s", map_line);
  421. g_hash_table_destroy (*r);
  422. g_free (r);
  423. lua_pushnil (L);
  424. return 1;
  425. }
  426. ud = lua_newuserdata (L, sizeof (GHashTable *));
  427. *ud = r;
  428. lua_setclass (L, "rspamd{hash_table}", -1);
  429. return 1;
  430. }
  431. lua_pushnil (L);
  432. return 1;
  433. }
  434. /*** Metric functions ***/
  435. static void
  436. lua_metric_symbol_callback (struct worker_task *task, gpointer ud)
  437. {
  438. struct lua_callback_data *cd = ud;
  439. struct worker_task **ptask;
  440. lua_getglobal (cd->L, cd->name);
  441. ptask = lua_newuserdata (cd->L, sizeof (struct worker_task *));
  442. lua_setclass (cd->L, "rspamd{task}", -1);
  443. *ptask = task;
  444. if (lua_pcall (cd->L, 1, 1, 0) != 0) {
  445. msg_warn ("error running function %s: %s", cd->name, lua_tostring (cd->L, -1));
  446. }
  447. }
  448. static gint
  449. lua_config_register_symbol (lua_State * L)
  450. {
  451. struct config_file *cfg = lua_check_config (L);
  452. const gchar *name, *callback;
  453. double weight;
  454. struct lua_callback_data *cd;
  455. if (cfg) {
  456. name = memory_pool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2));
  457. weight = luaL_checknumber (L, 3);
  458. callback = luaL_checkstring (L, 4);
  459. if (name) {
  460. cd = g_malloc (sizeof (struct lua_callback_data));
  461. cd->name = g_strdup (callback);
  462. cd->L = L;
  463. register_symbol (&cfg->cache, name, weight, lua_metric_symbol_callback, cd);
  464. }
  465. }
  466. return 1;
  467. }
  468. static gint
  469. lua_config_register_virtual_symbol (lua_State * L)
  470. {
  471. struct config_file *cfg = lua_check_config (L);
  472. const gchar *name;
  473. double weight;
  474. if (cfg) {
  475. name = memory_pool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2));
  476. weight = luaL_checknumber (L, 3);
  477. if (name) {
  478. register_virtual_symbol (&cfg->cache, name, weight);
  479. }
  480. }
  481. return 1;
  482. }
  483. static gint
  484. lua_config_register_callback_symbol (lua_State * L)
  485. {
  486. struct config_file *cfg = lua_check_config (L);
  487. const gchar *name, *callback;
  488. double weight;
  489. struct lua_callback_data *cd;
  490. if (cfg) {
  491. name = memory_pool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2));
  492. weight = luaL_checknumber (L, 3);
  493. callback = luaL_checkstring (L, 4);
  494. if (name) {
  495. cd = g_malloc (sizeof (struct lua_callback_data));
  496. cd->name = g_strdup (callback);
  497. cd->L = L;
  498. register_callback_symbol (&cfg->cache, name, weight, lua_metric_symbol_callback, cd);
  499. }
  500. }
  501. return 1;
  502. }
  503. /* Radix and hash table functions */
  504. static gint
  505. lua_radix_get_key (lua_State * L)
  506. {
  507. radix_tree_t *radix = lua_check_radix (L);
  508. guint32 key;
  509. if (radix) {
  510. key = luaL_checkint (L, 2);
  511. if (radix32tree_find (radix, key) != RADIX_NO_VALUE) {
  512. lua_pushboolean (L, 1);
  513. return 1;
  514. }
  515. }
  516. lua_pushboolean (L, 0);
  517. return 1;
  518. }
  519. static gint
  520. lua_hash_table_get_key (lua_State * L)
  521. {
  522. GHashTable *tbl = lua_check_hash_table (L);
  523. const gchar *key;
  524. if (tbl) {
  525. key = luaL_checkstring (L, 2);
  526. if (g_hash_table_lookup (tbl, key) != NULL) {
  527. lua_pushboolean (L, 1);
  528. return 1;
  529. }
  530. }
  531. lua_pushboolean (L, 0);
  532. return 1;
  533. }
  534. /* Trie functions */
  535. static gint
  536. lua_trie_create (lua_State *L)
  537. {
  538. rspamd_trie_t *trie, **ptrie;
  539. gboolean icase = FALSE;
  540. if (lua_gettop (L) == 1) {
  541. icase = lua_toboolean (L, 1);
  542. }
  543. trie = rspamd_trie_create (icase);
  544. ptrie = lua_newuserdata (L, sizeof (rspamd_trie_t *));
  545. lua_setclass (L, "rspamd{trie}", -1);
  546. *ptrie = trie;
  547. return 1;
  548. }
  549. static gint
  550. lua_trie_add_pattern (lua_State *L)
  551. {
  552. rspamd_trie_t *trie = lua_check_trie (L);
  553. const gchar *pattern;
  554. gint id;
  555. if (trie) {
  556. pattern = luaL_checkstring (L, 2);
  557. id = luaL_checknumber (L, 3);
  558. if (pattern != NULL) {
  559. rspamd_trie_insert (trie, pattern, id);
  560. lua_pushboolean (L, 1);
  561. }
  562. }
  563. lua_pushboolean (L, 0);
  564. return 1;
  565. }
  566. static gint
  567. lua_trie_search_text (lua_State *L)
  568. {
  569. rspamd_trie_t *trie = lua_check_trie (L);
  570. const gchar *text, *pos;
  571. gint id, i = 1;
  572. gsize len;
  573. if (trie) {
  574. text = luaL_checkstring (L, 2);
  575. len = strlen (text);
  576. if (text) {
  577. lua_newtable (L);
  578. pos = text;
  579. while (pos < text + len && (pos = rspamd_trie_lookup (trie, pos, len, &id)) != NULL) {
  580. lua_pushinteger (L, i);
  581. lua_pushinteger (L, id);
  582. lua_settable (L, -3);
  583. i ++;
  584. }
  585. return 1;
  586. }
  587. }
  588. lua_pushnil (L);
  589. return 1;
  590. }
  591. static gint
  592. lua_trie_search_task (lua_State *L)
  593. {
  594. rspamd_trie_t *trie = lua_check_trie (L);
  595. struct worker_task *task;
  596. struct mime_text_part *part;
  597. GList *cur;
  598. const gchar *pos, *end;
  599. gint id, i = 1;
  600. void *ud;
  601. if (trie) {
  602. ud = luaL_checkudata (L, 2, "rspamd{task}");
  603. luaL_argcheck (L, ud != NULL, 1, "'task' expected");
  604. task = *((struct worker_task **)ud);
  605. if (task) {
  606. lua_newtable (L);
  607. cur = task->text_parts;
  608. while (cur) {
  609. part = cur->data;
  610. if (!part->is_empty && part->content != NULL) {
  611. pos = (const gchar *)part->content->data;
  612. end = pos + part->content->len;
  613. while (pos < end && (pos = rspamd_trie_lookup (trie, pos, part->content->len, &id)) != NULL) {
  614. lua_pushinteger (L, i);
  615. lua_pushinteger (L, id);
  616. lua_settable (L, -3);
  617. i ++;
  618. }
  619. }
  620. cur = g_list_next (cur);
  621. }
  622. return 1;
  623. }
  624. }
  625. lua_pushnil (L);
  626. return 1;
  627. }
  628. /* Init functions */
  629. gint
  630. luaopen_config (lua_State * L)
  631. {
  632. lua_newclass (L, "rspamd{config}", configlib_m);
  633. luaL_openlib (L, "rspamd_config", null_reg, 0);
  634. return 1;
  635. }
  636. gint
  637. luaopen_radix (lua_State * L)
  638. {
  639. lua_newclass (L, "rspamd{radix}", radixlib_m);
  640. luaL_openlib (L, "rspamd_radix", null_reg, 0);
  641. return 1;
  642. }
  643. gint
  644. luaopen_hash_table (lua_State * L)
  645. {
  646. lua_newclass (L, "rspamd{hash_table}", hashlib_m);
  647. luaL_openlib (L, "rspamd_hash_table", null_reg, 0);
  648. return 1;
  649. }
  650. gint
  651. luaopen_trie (lua_State * L)
  652. {
  653. luaL_newmetatable (L, "rspamd{trie}");
  654. lua_pushstring (L, "__index");
  655. lua_pushvalue (L, -2);
  656. lua_settable (L, -3);
  657. lua_pushstring (L, "class");
  658. lua_pushstring (L, "rspamd{trie}");
  659. lua_rawset (L, -3);
  660. luaL_openlib (L, NULL, trielib_m, 0);
  661. luaL_openlib(L, "rspamd_trie", trielib_f, 0);
  662. return 1;
  663. }