Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*-
  2. * Copyright 2016 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. #include "lua_common.h"
  17. #include "regexp.h"
  18. /***
  19. * @module rspamd_regexp
  20. * Rspamd regexp is an utility module that handles rspamd perl compatible
  21. * regular expressions
  22. * @example
  23. * local rspamd_regexp = require "rspamd_regexp"
  24. *
  25. * local re = rspamd_regexp.create_cached('/^\\s*some_string\\s*$/i')
  26. * re:match('some_string')
  27. * local re = rspamd_regexp.create_cached('/\\s+/i')
  28. * re:split('word word word') -- returns ['word', 'word', 'word']
  29. */
  30. LUA_FUNCTION_DEF (regexp, create);
  31. LUA_FUNCTION_DEF (regexp, create_cached);
  32. LUA_FUNCTION_DEF (regexp, get_cached);
  33. LUA_FUNCTION_DEF (regexp, get_pattern);
  34. LUA_FUNCTION_DEF (regexp, set_limit);
  35. LUA_FUNCTION_DEF (regexp, set_max_hits);
  36. LUA_FUNCTION_DEF (regexp, get_max_hits);
  37. LUA_FUNCTION_DEF (regexp, search);
  38. LUA_FUNCTION_DEF (regexp, match);
  39. LUA_FUNCTION_DEF (regexp, matchn);
  40. LUA_FUNCTION_DEF (regexp, split);
  41. LUA_FUNCTION_DEF (regexp, destroy);
  42. LUA_FUNCTION_DEF (regexp, gc);
  43. static const struct luaL_reg regexplib_m[] = {
  44. LUA_INTERFACE_DEF (regexp, get_pattern),
  45. LUA_INTERFACE_DEF (regexp, set_limit),
  46. LUA_INTERFACE_DEF (regexp, set_max_hits),
  47. LUA_INTERFACE_DEF (regexp, get_max_hits),
  48. LUA_INTERFACE_DEF (regexp, match),
  49. LUA_INTERFACE_DEF (regexp, matchn),
  50. LUA_INTERFACE_DEF (regexp, search),
  51. LUA_INTERFACE_DEF (regexp, split),
  52. LUA_INTERFACE_DEF (regexp, destroy),
  53. {"__tostring", lua_regexp_get_pattern},
  54. {"__gc", lua_regexp_gc},
  55. {NULL, NULL}
  56. };
  57. static const struct luaL_reg regexplib_f[] = {
  58. LUA_INTERFACE_DEF (regexp, create),
  59. LUA_INTERFACE_DEF (regexp, get_cached),
  60. LUA_INTERFACE_DEF (regexp, create_cached),
  61. {NULL, NULL}
  62. };
  63. #define LUA_REGEXP_FLAG_DESTROYED (1 << 0)
  64. #define IS_DESTROYED(re) ((re)->re_flags & LUA_REGEXP_FLAG_DESTROYED)
  65. rspamd_mempool_t *regexp_static_pool = NULL;
  66. static struct rspamd_lua_regexp *
  67. lua_check_regexp (lua_State * L)
  68. {
  69. void *ud = rspamd_lua_check_udata (L, 1, "rspamd{regexp}");
  70. luaL_argcheck (L, ud != NULL, 1, "'regexp' expected");
  71. return ud ? *((struct rspamd_lua_regexp **)ud) : NULL;
  72. }
  73. static gchar *
  74. rspamd_lua_get_module_name (lua_State *L)
  75. {
  76. lua_Debug d;
  77. gchar *p;
  78. gchar func_buf[128];
  79. if (lua_getstack (L, 1, &d) == 1) {
  80. (void) lua_getinfo (L, "Sl", &d);
  81. if ((p = strrchr (d.short_src, '/')) == NULL) {
  82. p = d.short_src;
  83. }
  84. else {
  85. p++;
  86. }
  87. if (strlen (p) > 20) {
  88. rspamd_snprintf (func_buf, sizeof (func_buf), "%10s...]:%d", p,
  89. d.currentline);
  90. }
  91. else {
  92. rspamd_snprintf (func_buf, sizeof (func_buf), "%s:%d", p,
  93. d.currentline);
  94. }
  95. return g_strdup (func_buf);
  96. }
  97. return NULL;
  98. }
  99. /***
  100. * @function rspamd_regexp.create(pattern[, flags])
  101. * Creates new rspamd_regexp
  102. * @param {string} pattern pattern to build regexp. If this pattern is enclosed in `//` then it is possible to specify flags after it
  103. * @param {string} flags optional flags to create regular expression
  104. * @return {regexp} regexp argument that is *not* automatically destroyed
  105. * @example
  106. * local regexp = require "rspamd_regexp"
  107. *
  108. * local re = regexp.create('/^test.*[0-9]\\s*$/i')
  109. */
  110. static int
  111. lua_regexp_create (lua_State *L)
  112. {
  113. rspamd_regexp_t *re;
  114. struct rspamd_lua_regexp *new, **pnew;
  115. const gchar *string, *flags_str = NULL;
  116. GError *err = NULL;
  117. string = luaL_checkstring (L, 1);
  118. if (lua_gettop (L) == 2) {
  119. flags_str = luaL_checkstring (L, 2);
  120. }
  121. re = rspamd_regexp_new (string, flags_str, &err);
  122. if (re == NULL) {
  123. lua_pushnil (L);
  124. msg_info ("cannot parse regexp: %s, error: %s",
  125. string,
  126. err == NULL ? "undefined" : err->message);
  127. g_error_free (err);
  128. }
  129. else {
  130. new = g_slice_alloc0 (sizeof (struct rspamd_lua_regexp));
  131. new->re = re;
  132. new->re_pattern = g_strdup (string);
  133. new->module = rspamd_lua_get_module_name (L);
  134. pnew = lua_newuserdata (L, sizeof (struct rspamd_lua_regexp *));
  135. rspamd_lua_setclass (L, "rspamd{regexp}", -1);
  136. *pnew = new;
  137. }
  138. return 1;
  139. }
  140. /***
  141. * @function rspamd_regexp.get_cached(pattern)
  142. * This function gets cached and pre-compiled regexp created by either `create`
  143. * or `create_cached` methods. If no cached regexp is found then `nil` is returned.
  144. *
  145. * @param {string} pattern regexp pattern
  146. * @return {regexp} cached regexp structure or `nil`
  147. */
  148. static int
  149. lua_regexp_get_cached (lua_State *L)
  150. {
  151. rspamd_regexp_t *re;
  152. struct rspamd_lua_regexp *new, **pnew;
  153. const gchar *string, *flags_str = NULL;
  154. string = luaL_checkstring (L, 1);
  155. if (lua_gettop (L) == 2) {
  156. flags_str = luaL_checkstring (L, 2);
  157. }
  158. re = rspamd_regexp_cache_query (NULL, string, flags_str);
  159. if (re) {
  160. new = g_slice_alloc0 (sizeof (struct rspamd_lua_regexp));
  161. new->re = rspamd_regexp_ref (re);
  162. new->re_pattern = g_strdup (string);
  163. new->module = rspamd_lua_get_module_name (L);
  164. pnew = lua_newuserdata (L, sizeof (struct rspamd_lua_regexp *));
  165. rspamd_lua_setclass (L, "rspamd{regexp}", -1);
  166. *pnew = new;
  167. }
  168. else {
  169. lua_pushnil (L);
  170. }
  171. return 1;
  172. }
  173. /***
  174. * @function rspamd_regexp.create_cached(pattern[, flags])
  175. * This function is similar to `create` but it tries to search for regexp in the
  176. * cache first.
  177. * @param {string} pattern pattern to build regexp. If this pattern is enclosed in `//` then it is possible to specify flags after it
  178. * @param {string} flags optional flags to create regular expression
  179. * @return {regexp} regexp argument that is *not* automatically destroyed
  180. * @example
  181. * local regexp = require "rspamd_regexp"
  182. *
  183. * local re = regexp.create_cached('/^test.*[0-9]\\s*$/i')
  184. * ...
  185. * -- This doesn't create new regexp object
  186. * local other_re = regexp.create_cached('/^test.*[0-9]\\s*$/i')
  187. */
  188. static int
  189. lua_regexp_create_cached (lua_State *L)
  190. {
  191. rspamd_regexp_t *re;
  192. struct rspamd_lua_regexp *new, **pnew;
  193. const gchar *string, *flags_str = NULL;
  194. GError *err = NULL;
  195. string = luaL_checkstring (L, 1);
  196. if (lua_gettop (L) == 2) {
  197. flags_str = luaL_checkstring (L, 2);
  198. }
  199. re = rspamd_regexp_cache_query (NULL, string, flags_str);
  200. if (re) {
  201. new = g_slice_alloc0 (sizeof (struct rspamd_lua_regexp));
  202. new->re = rspamd_regexp_ref (re);
  203. new->re_pattern = g_strdup (string);
  204. new->module = rspamd_lua_get_module_name (L);
  205. pnew = lua_newuserdata (L, sizeof (struct rspamd_lua_regexp *));
  206. rspamd_lua_setclass (L, "rspamd{regexp}", -1);
  207. *pnew = new;
  208. }
  209. else {
  210. re = rspamd_regexp_cache_create (NULL, string, flags_str, &err);
  211. if (re == NULL) {
  212. lua_pushnil (L);
  213. msg_info ("cannot parse regexp: %s, error: %s",
  214. string,
  215. err == NULL ? "undefined" : err->message);
  216. g_error_free (err);
  217. }
  218. else {
  219. new = g_slice_alloc0 (sizeof (struct rspamd_lua_regexp));
  220. new->re = rspamd_regexp_ref (re);
  221. new->re_pattern = g_strdup (string);
  222. new->module = rspamd_lua_get_module_name (L);
  223. pnew = lua_newuserdata (L, sizeof (struct rspamd_lua_regexp *));
  224. rspamd_lua_setclass (L, "rspamd{regexp}", -1);
  225. *pnew = new;
  226. }
  227. }
  228. return 1;
  229. }
  230. /***
  231. * @method re:get_pattern()
  232. * Get a pattern for specified regexp object
  233. * @return {string} pattern line
  234. */
  235. static int
  236. lua_regexp_get_pattern (lua_State *L)
  237. {
  238. struct rspamd_lua_regexp *re = lua_check_regexp (L);
  239. if (re && re->re && !IS_DESTROYED (re)) {
  240. lua_pushstring (L, rspamd_regexp_get_pattern (re->re));
  241. }
  242. else {
  243. lua_pushnil (L);
  244. }
  245. return 1;
  246. }
  247. /***
  248. * @method re:set_limit(lim)
  249. * Set maximum size of text length to be matched with this regexp (if `lim` is
  250. * less or equal to zero then all texts are checked)
  251. * @param {number} lim limit in bytes
  252. */
  253. static int
  254. lua_regexp_set_limit (lua_State *L)
  255. {
  256. struct rspamd_lua_regexp *re = lua_check_regexp (L);
  257. gint64 lim;
  258. lim = luaL_checknumber (L, 2);
  259. if (re && re->re && !IS_DESTROYED (re)) {
  260. if (lim > 0) {
  261. re->match_limit = lim;
  262. }
  263. else {
  264. re->match_limit = 0;
  265. }
  266. }
  267. return 0;
  268. }
  269. /***
  270. * @method re:set_max_hits(lim)
  271. * Set maximum number of hits returned by a regexp
  272. * @param {number} lim limit in hits count
  273. * @return {number} old number of max hits
  274. */
  275. static int
  276. lua_regexp_set_max_hits (lua_State *L)
  277. {
  278. struct rspamd_lua_regexp *re = lua_check_regexp (L);
  279. guint lim;
  280. lim = luaL_checknumber (L, 2);
  281. if (re && re->re && !IS_DESTROYED (re)) {
  282. lua_pushnumber (L, rspamd_regexp_set_maxhits (re->re, lim));
  283. }
  284. else {
  285. lua_pushnil (L);
  286. }
  287. return 1;
  288. }
  289. /***
  290. * @method re:get_max_hits(lim)
  291. * Get maximum number of hits returned by a regexp
  292. * @return {number} number of max hits
  293. */
  294. static int
  295. lua_regexp_get_max_hits (lua_State *L)
  296. {
  297. struct rspamd_lua_regexp *re = lua_check_regexp (L);
  298. if (re && re->re && !IS_DESTROYED (re)) {
  299. lua_pushnumber (L, rspamd_regexp_get_maxhits (re->re));
  300. }
  301. else {
  302. lua_pushnumber (L, 1);
  303. }
  304. return 1;
  305. }
  306. /***
  307. * @method re:search(line[, raw[, capture]])
  308. * Search line in regular expression object. If line matches then this
  309. * function returns the table of captured strings. Otherwise, nil is returned.
  310. * If `raw` is specified, then input is treated as raw data not encoded in `utf-8`.
  311. * If `capture` is true, then this function saves all captures to the table of
  312. * values, so the first element is the whole matched string and the
  313. * subsequent elements are ordered captures defined within pattern.
  314. *
  315. * @param {string} line match the specified line against regexp object
  316. * @param {bool} match raw regexp instead of utf8 one
  317. * @param {bool} capture perform subpatterns capturing
  318. * @return {table or nil} table of strings or tables (if `capture` is true) or nil if not matched
  319. * @example
  320. * local re = regexp.create_cached('/^\s*([0-9]+)\s*$/')
  321. * -- returns nil
  322. * local m1 = re:search('blah')
  323. * local m2 = re:search(' 190 ')
  324. * -- prints ' 190 '
  325. * print(m2[1])
  326. *
  327. * local m3 = re:search(' 100500 ')
  328. * -- prints ' 100500 '
  329. * print(m3[1][1])
  330. * -- prints '100500' capture
  331. * print(m3[1][2])
  332. */
  333. static int
  334. lua_regexp_search (lua_State *L)
  335. {
  336. struct rspamd_lua_regexp *re = lua_check_regexp (L);
  337. const gchar *data = NULL;
  338. struct rspamd_lua_text *t;
  339. const gchar *start = NULL, *end = NULL;
  340. gint i;
  341. gsize len, capn;
  342. gboolean matched = FALSE, capture = FALSE, raw = FALSE;
  343. GArray *captures = NULL;
  344. struct rspamd_re_capture *cap;
  345. if (re && !IS_DESTROYED (re)) {
  346. if (lua_type (L, 2) == LUA_TSTRING) {
  347. data = luaL_checklstring (L, 2, &len);
  348. }
  349. else if (lua_type (L, 2) == LUA_TUSERDATA) {
  350. t = lua_check_text (L, 2);
  351. if (t != NULL) {
  352. data = t->start;
  353. len = t->len;
  354. }
  355. }
  356. if (lua_gettop (L) >= 3) {
  357. raw = lua_toboolean (L, 3);
  358. }
  359. if (data) {
  360. if (lua_gettop (L) >= 4) {
  361. capture = TRUE;
  362. captures = g_array_new (FALSE, TRUE,
  363. sizeof (struct rspamd_re_capture));
  364. }
  365. lua_newtable (L);
  366. i = 0;
  367. if (re->match_limit > 0) {
  368. len = MIN (len, re->match_limit);
  369. }
  370. while (rspamd_regexp_search (re->re, data, len, &start, &end, raw,
  371. captures)) {
  372. if (capture) {
  373. lua_createtable (L, captures->len, 0);
  374. for (capn = 0; capn < captures->len; capn ++) {
  375. cap = &g_array_index (captures, struct rspamd_re_capture,
  376. capn);
  377. lua_pushlstring (L, cap->p, cap->len);
  378. lua_rawseti (L, -2, capn + 1);
  379. }
  380. lua_rawseti (L, -2, ++i);
  381. }
  382. else {
  383. lua_pushlstring (L, start, end - start);
  384. lua_rawseti (L, -2, ++i);
  385. }
  386. matched = TRUE;
  387. }
  388. if (!matched) {
  389. lua_pop (L, 1);
  390. lua_pushnil (L);
  391. }
  392. if (capture) {
  393. g_array_free (captures, TRUE);
  394. }
  395. return 1;
  396. }
  397. }
  398. lua_pushnil (L);
  399. return 1;
  400. }
  401. /***
  402. * @method re:match(line[, raw_match])
  403. * Matches line against the regular expression and return true if line matches
  404. * (partially or completely)
  405. *
  406. * @param {string} line match the specified line against regexp object
  407. * @param {bool} match raw regexp instead of utf8 one
  408. * @return {bool} true if `line` matches
  409. */
  410. static int
  411. lua_regexp_match (lua_State *L)
  412. {
  413. struct rspamd_lua_regexp *re = lua_check_regexp (L);
  414. struct rspamd_lua_text *t;
  415. const gchar *data = NULL;
  416. gsize len = 0;
  417. gboolean raw = FALSE;
  418. if (re && !IS_DESTROYED (re)) {
  419. if (lua_type (L, 2) == LUA_TSTRING) {
  420. data = luaL_checklstring (L, 2, &len);
  421. }
  422. else if (lua_type (L, 2) == LUA_TUSERDATA) {
  423. t = lua_check_text (L, 2);
  424. if (t != NULL) {
  425. data = t->start;
  426. len = t->len;
  427. }
  428. }
  429. if (lua_gettop (L) == 3) {
  430. raw = lua_toboolean (L, 3);
  431. }
  432. if (data) {
  433. if (re->match_limit > 0) {
  434. len = MIN (len, re->match_limit);
  435. }
  436. if (rspamd_regexp_search (re->re, data, len, NULL, NULL, raw, NULL)) {
  437. lua_pushboolean (L, TRUE);
  438. }
  439. else {
  440. lua_pushboolean (L, FALSE);
  441. }
  442. return 1;
  443. }
  444. }
  445. lua_pushnil (L);
  446. return 1;
  447. }
  448. /***
  449. * @method re:matchn(line, max_matches, [, raw_match])
  450. * Matches line against the regular expression and return number of matches if line matches
  451. * (partially or completely). This process stop when `max_matches` is reached.
  452. * If `max_matches` is zero, then only a single match is counted which is equal to
  453. * @see re:match If `max_matches` is negative, then all matches are considered.
  454. *
  455. * @param {string} line match the specified line against regexp object
  456. * @param {number} max_matches maximum number of matches
  457. * @param {bool} match raw regexp instead of utf8 one
  458. * @return {number} number of matches found in the `line` argument
  459. */
  460. static int
  461. lua_regexp_matchn (lua_State *L)
  462. {
  463. struct rspamd_lua_regexp *re = lua_check_regexp (L);
  464. struct rspamd_lua_text *t;
  465. const gchar *data = NULL, *start = NULL, *end = NULL;
  466. gint max_matches, matches;
  467. gsize len = 0;
  468. gboolean raw = FALSE;
  469. if (re && !IS_DESTROYED (re)) {
  470. if (lua_type (L, 2) == LUA_TSTRING) {
  471. data = luaL_checklstring (L, 2, &len);
  472. }
  473. else if (lua_type (L, 2) == LUA_TUSERDATA) {
  474. t = lua_check_text (L, 2);
  475. if (t != NULL) {
  476. data = t->start;
  477. len = t->len;
  478. }
  479. }
  480. max_matches = lua_tonumber (L, 3);
  481. if (lua_gettop (L) == 4) {
  482. raw = lua_toboolean (L, 4);
  483. }
  484. if (data) {
  485. matches = 0;
  486. if (re->match_limit > 0) {
  487. len = MIN (len, re->match_limit);
  488. }
  489. for (;;) {
  490. if (rspamd_regexp_search (re->re, data, len, &start, &end, raw,
  491. NULL)) {
  492. matches ++;
  493. }
  494. else {
  495. break;
  496. }
  497. if (max_matches >= 0 && matches >= max_matches) {
  498. break;
  499. }
  500. }
  501. lua_pushnumber (L, matches);
  502. return 1;
  503. }
  504. }
  505. lua_pushnil (L);
  506. return 1;
  507. }
  508. /***
  509. * @method re:split(line)
  510. * Split line using the specified regular expression.
  511. * Breaks the string on the pattern, and returns an array of the tokens.
  512. * If the pattern contains capturing parentheses, then the text for each
  513. * of the substrings will also be returned. If the pattern does not match
  514. * anywhere in the string, then the whole string is returned as the first
  515. * token.
  516. * @param {string/text} line line to split
  517. * @return {table} table of split line portions (if text was the input, then text is used for return parts)
  518. */
  519. static int
  520. lua_regexp_split (lua_State *L)
  521. {
  522. struct rspamd_lua_regexp *re = lua_check_regexp (L);
  523. const gchar *data = NULL;
  524. struct rspamd_lua_text *t;
  525. gboolean matched = FALSE, is_text = FALSE;
  526. gsize len = 0;
  527. const gchar *start = NULL, *end = NULL, *old_start;
  528. gint i;
  529. if (re && !IS_DESTROYED (re)) {
  530. if (lua_type (L, 2) == LUA_TSTRING) {
  531. data = luaL_checklstring (L, 2, &len);
  532. }
  533. else if (lua_type (L, 2) == LUA_TUSERDATA) {
  534. t = lua_check_text (L, 2);
  535. if (t == NULL) {
  536. lua_error (L);
  537. return 0;
  538. }
  539. data = t->start;
  540. len = t->len;
  541. is_text = TRUE;
  542. }
  543. if (re->match_limit > 0) {
  544. len = MIN (len, re->match_limit);
  545. }
  546. if (data) {
  547. lua_newtable (L);
  548. i = 0;
  549. old_start = data;
  550. while (rspamd_regexp_search (re->re, data, len, &start, &end, FALSE,
  551. NULL)) {
  552. if (start - old_start > 0) {
  553. if (!is_text) {
  554. lua_pushlstring (L, old_start, start - old_start);
  555. }
  556. else {
  557. t = lua_newuserdata (L, sizeof (*t));
  558. rspamd_lua_setclass (L, "rspamd{text}", -1);
  559. t->start = old_start;
  560. t->len = start - old_start;
  561. t->flags = 0;
  562. }
  563. lua_rawseti (L, -2, ++i);
  564. matched = TRUE;
  565. }
  566. else if (start == end) {
  567. break;
  568. }
  569. old_start = end;
  570. }
  571. if (len > 0 && (end == NULL || end < data + len)) {
  572. if (end == NULL) {
  573. end = data;
  574. }
  575. if (!is_text) {
  576. lua_pushlstring (L, end, (data + len) - end);
  577. }
  578. else {
  579. t = lua_newuserdata (L, sizeof (*t));
  580. rspamd_lua_setclass (L, "rspamd{text}", -1);
  581. t->start = end;
  582. t->len = (data + len) - end;
  583. t->flags = 0;
  584. }
  585. lua_rawseti (L, -2, ++i);
  586. matched = TRUE;
  587. }
  588. if (!matched) {
  589. lua_pop (L, 1);
  590. lua_pushnil (L);
  591. }
  592. return 1;
  593. }
  594. }
  595. lua_pushnil (L);
  596. return 1;
  597. }
  598. /***
  599. * @method re:destroy()
  600. * Destroy regexp from caches if needed (the pointer is removed by garbadge collector)
  601. */
  602. static gint
  603. lua_regexp_destroy (lua_State *L)
  604. {
  605. struct rspamd_lua_regexp *to_del = lua_check_regexp (L);
  606. if (to_del) {
  607. rspamd_regexp_cache_remove (NULL, to_del->re);
  608. rspamd_regexp_unref (to_del->re);
  609. to_del->re = NULL;
  610. to_del->re_flags |= LUA_REGEXP_FLAG_DESTROYED;
  611. }
  612. return 0;
  613. }
  614. static gint
  615. lua_regexp_gc (lua_State *L)
  616. {
  617. struct rspamd_lua_regexp *to_del = lua_check_regexp (L);
  618. if (to_del) {
  619. if (!IS_DESTROYED (to_del)) {
  620. rspamd_regexp_unref (to_del->re);
  621. }
  622. g_free (to_del->re_pattern);
  623. g_free (to_del->module);
  624. g_slice_free1 (sizeof (*to_del), to_del);
  625. }
  626. return 0;
  627. }
  628. static gint
  629. lua_load_regexp (lua_State * L)
  630. {
  631. lua_newtable (L);
  632. luaL_register (L, NULL, regexplib_f);
  633. return 1;
  634. }
  635. void
  636. luaopen_regexp (lua_State * L)
  637. {
  638. luaL_newmetatable (L, "rspamd{regexp}");
  639. lua_pushstring (L, "__index");
  640. lua_pushvalue (L, -2);
  641. lua_settable (L, -3);
  642. lua_pushstring (L, "class");
  643. lua_pushstring (L, "rspamd{regexp}");
  644. lua_rawset (L, -3);
  645. luaL_register (L, NULL, regexplib_m);
  646. rspamd_lua_add_preload (L, "rspamd_regexp", lua_load_regexp);
  647. if (regexp_static_pool == NULL) {
  648. regexp_static_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (),
  649. "regexp_lua_pool");
  650. }
  651. lua_settop (L, 0);
  652. }