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_task.c 41KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  1. /*
  2. * Copyright (c) 2009-2012, Vsevolod Stakhov
  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 AUTHOR ''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 AUTHOR 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 "message.h"
  26. #include "expressions.h"
  27. #include "protocol.h"
  28. #include "filter.h"
  29. #include "dns.h"
  30. #include "util.h"
  31. #include "images.h"
  32. #include "cfg_file.h"
  33. #include "statfile.h"
  34. #include "tokenizers/tokenizers.h"
  35. #include "classifiers/classifiers.h"
  36. #include "binlog.h"
  37. #include "statfile_sync.h"
  38. #include "diff.h"
  39. /* Task creation */
  40. LUA_FUNCTION_DEF (task, create_empty);
  41. LUA_FUNCTION_DEF (task, create_from_buffer);
  42. /* Task methods */
  43. LUA_FUNCTION_DEF (task, get_message);
  44. LUA_FUNCTION_DEF (task, process_message);
  45. LUA_FUNCTION_DEF (task, get_cfg);
  46. LUA_FUNCTION_DEF (task, set_cfg);
  47. LUA_FUNCTION_DEF (task, destroy);
  48. LUA_FUNCTION_DEF (task, get_mempool);
  49. LUA_FUNCTION_DEF (task, get_session);
  50. LUA_FUNCTION_DEF (task, get_ev_base);
  51. LUA_FUNCTION_DEF (task, insert_result);
  52. LUA_FUNCTION_DEF (task, set_pre_result);
  53. LUA_FUNCTION_DEF (task, get_urls);
  54. LUA_FUNCTION_DEF (task, get_emails);
  55. LUA_FUNCTION_DEF (task, get_text_parts);
  56. LUA_FUNCTION_DEF (task, get_parts);
  57. LUA_FUNCTION_DEF (task, get_raw_headers);
  58. LUA_FUNCTION_DEF (task, get_raw_header);
  59. LUA_FUNCTION_DEF (task, get_raw_header_strong);
  60. LUA_FUNCTION_DEF (task, get_received_headers);
  61. LUA_FUNCTION_DEF (task, get_resolver);
  62. LUA_FUNCTION_DEF (task, inc_dns_req);
  63. LUA_FUNCTION_DEF (task, call_rspamd_function);
  64. LUA_FUNCTION_DEF (task, get_recipients);
  65. LUA_FUNCTION_DEF (task, get_from);
  66. LUA_FUNCTION_DEF (task, get_user);
  67. LUA_FUNCTION_DEF (task, set_user);
  68. LUA_FUNCTION_DEF (task, get_from_ip);
  69. LUA_FUNCTION_DEF (task, set_from_ip);
  70. LUA_FUNCTION_DEF (task, get_from_ip_num);
  71. LUA_FUNCTION_DEF (task, get_client_ip_num);
  72. LUA_FUNCTION_DEF (task, get_helo);
  73. LUA_FUNCTION_DEF (task, set_helo);
  74. LUA_FUNCTION_DEF (task, get_hostname);
  75. LUA_FUNCTION_DEF (task, set_hostname);
  76. LUA_FUNCTION_DEF (task, get_images);
  77. LUA_FUNCTION_DEF (task, get_symbol);
  78. LUA_FUNCTION_DEF (task, get_date);
  79. LUA_FUNCTION_DEF (task, get_message_id);
  80. LUA_FUNCTION_DEF (task, get_timeval);
  81. LUA_FUNCTION_DEF (task, get_metric_score);
  82. LUA_FUNCTION_DEF (task, get_metric_action);
  83. LUA_FUNCTION_DEF (task, learn);
  84. LUA_FUNCTION_DEF (task, set_settings);
  85. static const struct luaL_reg tasklib_f[] = {
  86. LUA_INTERFACE_DEF (task, create_empty),
  87. LUA_INTERFACE_DEF (task, create_from_buffer),
  88. {NULL, NULL}
  89. };
  90. static const struct luaL_reg tasklib_m[] = {
  91. LUA_INTERFACE_DEF (task, get_message),
  92. LUA_INTERFACE_DEF (task, destroy),
  93. LUA_INTERFACE_DEF (task, process_message),
  94. LUA_INTERFACE_DEF (task, set_cfg),
  95. LUA_INTERFACE_DEF (task, get_cfg),
  96. LUA_INTERFACE_DEF (task, get_mempool),
  97. LUA_INTERFACE_DEF (task, get_session),
  98. LUA_INTERFACE_DEF (task, get_ev_base),
  99. LUA_INTERFACE_DEF (task, insert_result),
  100. LUA_INTERFACE_DEF (task, set_pre_result),
  101. LUA_INTERFACE_DEF (task, get_urls),
  102. LUA_INTERFACE_DEF (task, get_emails),
  103. LUA_INTERFACE_DEF (task, get_text_parts),
  104. LUA_INTERFACE_DEF (task, get_parts),
  105. LUA_INTERFACE_DEF (task, get_raw_headers),
  106. LUA_INTERFACE_DEF (task, get_raw_header),
  107. LUA_INTERFACE_DEF (task, get_raw_header_strong),
  108. LUA_INTERFACE_DEF (task, get_received_headers),
  109. LUA_INTERFACE_DEF (task, get_resolver),
  110. LUA_INTERFACE_DEF (task, inc_dns_req),
  111. LUA_INTERFACE_DEF (task, call_rspamd_function),
  112. LUA_INTERFACE_DEF (task, get_recipients),
  113. LUA_INTERFACE_DEF (task, get_from),
  114. LUA_INTERFACE_DEF (task, get_user),
  115. LUA_INTERFACE_DEF (task, set_user),
  116. LUA_INTERFACE_DEF (task, get_from_ip),
  117. LUA_INTERFACE_DEF (task, set_from_ip),
  118. LUA_INTERFACE_DEF (task, get_from_ip_num),
  119. LUA_INTERFACE_DEF (task, get_client_ip_num),
  120. LUA_INTERFACE_DEF (task, get_helo),
  121. LUA_INTERFACE_DEF (task, set_helo),
  122. LUA_INTERFACE_DEF (task, get_hostname),
  123. LUA_INTERFACE_DEF (task, set_hostname),
  124. LUA_INTERFACE_DEF (task, get_images),
  125. LUA_INTERFACE_DEF (task, get_symbol),
  126. LUA_INTERFACE_DEF (task, get_date),
  127. LUA_INTERFACE_DEF (task, get_message_id),
  128. LUA_INTERFACE_DEF (task, get_timeval),
  129. LUA_INTERFACE_DEF (task, get_metric_score),
  130. LUA_INTERFACE_DEF (task, get_metric_action),
  131. LUA_INTERFACE_DEF (task, learn),
  132. LUA_INTERFACE_DEF (task, set_settings),
  133. {"__tostring", rspamd_lua_class_tostring},
  134. {NULL, NULL}
  135. };
  136. /* Textpart methods */
  137. LUA_FUNCTION_DEF (textpart, get_content);
  138. LUA_FUNCTION_DEF (textpart, get_length);
  139. LUA_FUNCTION_DEF (textpart, is_empty);
  140. LUA_FUNCTION_DEF (textpart, is_html);
  141. LUA_FUNCTION_DEF (textpart, get_fuzzy);
  142. LUA_FUNCTION_DEF (textpart, get_language);
  143. LUA_FUNCTION_DEF (textpart, compare_distance);
  144. static const struct luaL_reg textpartlib_m[] = {
  145. LUA_INTERFACE_DEF (textpart, get_content),
  146. LUA_INTERFACE_DEF (textpart, get_length),
  147. LUA_INTERFACE_DEF (textpart, is_empty),
  148. LUA_INTERFACE_DEF (textpart, is_html),
  149. LUA_INTERFACE_DEF (textpart, get_fuzzy),
  150. LUA_INTERFACE_DEF (textpart, get_language),
  151. LUA_INTERFACE_DEF (textpart, compare_distance),
  152. {"__tostring", rspamd_lua_class_tostring},
  153. {NULL, NULL}
  154. };
  155. /* Mimepart methods */
  156. LUA_FUNCTION_DEF (mimepart, get_content);
  157. LUA_FUNCTION_DEF (mimepart, get_length);
  158. LUA_FUNCTION_DEF (mimepart, get_type);
  159. LUA_FUNCTION_DEF (mimepart, get_filename);
  160. static const struct luaL_reg mimepartlib_m[] = {
  161. LUA_INTERFACE_DEF (mimepart, get_content),
  162. LUA_INTERFACE_DEF (mimepart, get_length),
  163. LUA_INTERFACE_DEF (mimepart, get_type),
  164. LUA_INTERFACE_DEF (mimepart, get_filename),
  165. {"__tostring", rspamd_lua_class_tostring},
  166. {NULL, NULL}
  167. };
  168. /* Image methods */
  169. LUA_FUNCTION_DEF (image, get_width);
  170. LUA_FUNCTION_DEF (image, get_height);
  171. LUA_FUNCTION_DEF (image, get_type);
  172. LUA_FUNCTION_DEF (image, get_filename);
  173. LUA_FUNCTION_DEF (image, get_size);
  174. static const struct luaL_reg imagelib_m[] = {
  175. LUA_INTERFACE_DEF (image, get_width),
  176. LUA_INTERFACE_DEF (image, get_height),
  177. LUA_INTERFACE_DEF (image, get_type),
  178. LUA_INTERFACE_DEF (image, get_filename),
  179. LUA_INTERFACE_DEF (image, get_size),
  180. {"__tostring", rspamd_lua_class_tostring},
  181. {NULL, NULL}
  182. };
  183. /* URL methods */
  184. LUA_FUNCTION_DEF (url, get_length);
  185. LUA_FUNCTION_DEF (url, get_host);
  186. LUA_FUNCTION_DEF (url, get_user);
  187. LUA_FUNCTION_DEF (url, get_path);
  188. LUA_FUNCTION_DEF (url, get_text);
  189. LUA_FUNCTION_DEF (url, is_phished);
  190. LUA_FUNCTION_DEF (url, get_phished);
  191. static const struct luaL_reg urllib_m[] = {
  192. LUA_INTERFACE_DEF (url, get_length),
  193. LUA_INTERFACE_DEF (url, get_host),
  194. LUA_INTERFACE_DEF (url, get_user),
  195. LUA_INTERFACE_DEF (url, get_path),
  196. LUA_INTERFACE_DEF (url, get_text),
  197. LUA_INTERFACE_DEF (url, is_phished),
  198. LUA_INTERFACE_DEF (url, get_phished),
  199. {"__tostring", rspamd_lua_class_tostring},
  200. {NULL, NULL}
  201. };
  202. /* Utility functions */
  203. static struct rspamd_task *
  204. lua_check_task (lua_State * L)
  205. {
  206. void *ud = luaL_checkudata (L, 1, "rspamd{task}");
  207. luaL_argcheck (L, ud != NULL, 1, "'task' expected");
  208. return ud ? *((struct rspamd_task **)ud) : NULL;
  209. }
  210. static struct mime_text_part *
  211. lua_check_textpart (lua_State * L)
  212. {
  213. void *ud = luaL_checkudata (L, 1, "rspamd{textpart}");
  214. luaL_argcheck (L, ud != NULL, 1, "'textpart' expected");
  215. return ud ? *((struct mime_text_part **)ud) : NULL;
  216. }
  217. static struct mime_part *
  218. lua_check_mimepart (lua_State * L)
  219. {
  220. void *ud = luaL_checkudata (L, 1, "rspamd{mimepart}");
  221. luaL_argcheck (L, ud != NULL, 1, "'mimepart' expected");
  222. return ud ? *((struct mime_part **)ud) : NULL;
  223. }
  224. static struct rspamd_image *
  225. lua_check_image (lua_State * L)
  226. {
  227. void *ud = luaL_checkudata (L, 1, "rspamd{image}");
  228. luaL_argcheck (L, ud != NULL, 1, "'image' expected");
  229. return ud ? *((struct rspamd_image **)ud) : NULL;
  230. }
  231. static struct uri *
  232. lua_check_url (lua_State * L)
  233. {
  234. void *ud = luaL_checkudata (L, 1, "rspamd{url}");
  235. luaL_argcheck (L, ud != NULL, 1, "'url' expected");
  236. return ud ? *((struct uri **)ud) : NULL;
  237. }
  238. /*** Task interface ***/
  239. static int
  240. lua_task_create_empty (lua_State *L)
  241. {
  242. struct rspamd_task **ptask, *task;
  243. task = rspamd_task_new (NULL);
  244. ptask = lua_newuserdata (L, sizeof (gpointer));
  245. rspamd_lua_setclass (L, "rspamd{task}", -1);
  246. *ptask = task;
  247. return 1;
  248. }
  249. static int
  250. lua_task_create_from_buffer (lua_State *L)
  251. {
  252. struct rspamd_task **ptask, *task;
  253. const gchar *data;
  254. size_t len;
  255. data = luaL_checklstring (L, 1, &len);
  256. if (data) {
  257. task = rspamd_task_new (NULL);
  258. ptask = lua_newuserdata (L, sizeof (gpointer));
  259. rspamd_lua_setclass (L, "rspamd{task}", -1);
  260. *ptask = task;
  261. task->msg = g_string_new_len (data, len);
  262. }
  263. return 1;
  264. }
  265. static int
  266. lua_task_process_message (lua_State *L)
  267. {
  268. struct rspamd_task *task = lua_check_task (L);
  269. if (task != NULL && task->msg != NULL && task->msg->len > 0) {
  270. if (process_message (task) == 0) {
  271. lua_pushboolean (L, TRUE);
  272. }
  273. else {
  274. lua_pushboolean (L, FALSE);
  275. }
  276. }
  277. else {
  278. lua_pushboolean (L, FALSE);
  279. }
  280. return 1;
  281. }
  282. static int
  283. lua_task_get_cfg (lua_State *L)
  284. {
  285. struct rspamd_task *task = lua_check_task (L);
  286. struct rspamd_config **pcfg;
  287. pcfg = lua_newuserdata (L, sizeof (gpointer));
  288. rspamd_lua_setclass (L, "rspamd{config}", -1);
  289. *pcfg = task->cfg;
  290. return 1;
  291. }
  292. static int
  293. lua_task_set_cfg (lua_State *L)
  294. {
  295. struct rspamd_task *task = lua_check_task (L);
  296. void *ud = luaL_checkudata (L, 2, "rspamd{config}");
  297. luaL_argcheck (L, ud != NULL, 1, "'config' expected");
  298. task->cfg = ud ? *((struct rspamd_config **)ud) : NULL;
  299. return 0;
  300. }
  301. static int
  302. lua_task_destroy (lua_State *L)
  303. {
  304. struct rspamd_task *task = lua_check_task (L);
  305. if (task != NULL) {
  306. rspamd_task_free (task, FALSE);
  307. }
  308. return 0;
  309. }
  310. static int
  311. lua_task_get_message (lua_State * L)
  312. {
  313. GMimeMessage **pmsg;
  314. struct rspamd_task *task = lua_check_task (L);
  315. if (task != NULL && task->message != NULL) {
  316. pmsg = lua_newuserdata (L, sizeof (GMimeMessage *));
  317. rspamd_lua_setclass (L, "rspamd{message}", -1);
  318. *pmsg = task->message;
  319. }
  320. else {
  321. lua_pushnil (L);
  322. }
  323. return 1;
  324. }
  325. static int
  326. lua_task_get_mempool (lua_State * L)
  327. {
  328. rspamd_mempool_t **ppool;
  329. struct rspamd_task *task = lua_check_task (L);
  330. if (task != NULL) {
  331. ppool = lua_newuserdata (L, sizeof (rspamd_mempool_t *));
  332. rspamd_lua_setclass (L, "rspamd{mempool}", -1);
  333. *ppool = task->task_pool;
  334. }
  335. else {
  336. lua_pushnil (L);
  337. }
  338. return 1;
  339. }
  340. static int
  341. lua_task_get_session (lua_State * L)
  342. {
  343. struct rspamd_async_session **psession;
  344. struct rspamd_task *task = lua_check_task (L);
  345. if (task != NULL) {
  346. psession = lua_newuserdata (L, sizeof (void *));
  347. rspamd_lua_setclass (L, "rspamd{session}", -1);
  348. *psession = task->s;
  349. }
  350. else {
  351. lua_pushnil (L);
  352. }
  353. return 1;
  354. }
  355. static int
  356. lua_task_get_ev_base (lua_State * L)
  357. {
  358. struct event_base **pbase;
  359. struct rspamd_task *task = lua_check_task (L);
  360. if (task != NULL) {
  361. pbase = lua_newuserdata (L, sizeof (struct event_base *));
  362. rspamd_lua_setclass (L, "rspamd{ev_base}", -1);
  363. *pbase = task->ev_base;
  364. }
  365. else {
  366. lua_pushnil (L);
  367. }
  368. return 1;
  369. }
  370. static gint
  371. lua_task_insert_result (lua_State * L)
  372. {
  373. struct rspamd_task *task = lua_check_task (L);
  374. const gchar *symbol_name, *param;
  375. double flag;
  376. GList *params = NULL;
  377. gint i, top;
  378. if (task != NULL) {
  379. symbol_name =
  380. rspamd_mempool_strdup (task->task_pool, luaL_checkstring (L, 2));
  381. flag = luaL_checknumber (L, 3);
  382. top = lua_gettop (L);
  383. /* Get additional options */
  384. for (i = 4; i <= top; i++) {
  385. param = luaL_checkstring (L, i);
  386. params =
  387. g_list_prepend (params,
  388. rspamd_mempool_strdup (task->task_pool, param));
  389. }
  390. insert_result (task, symbol_name, flag, params);
  391. }
  392. return 0;
  393. }
  394. static gint
  395. lua_task_set_pre_result (lua_State * L)
  396. {
  397. struct rspamd_task *task = lua_check_task (L);
  398. gchar *action_str;
  399. guint action;
  400. if (task != NULL) {
  401. action = luaL_checkinteger (L, 2);
  402. if (action < task->pre_result.action) {
  403. task->pre_result.action = action;
  404. if (lua_gettop (L) >= 3) {
  405. action_str = rspamd_mempool_strdup (task->task_pool,
  406. luaL_checkstring (L, 3));
  407. task->pre_result.str = action_str;
  408. }
  409. else {
  410. task->pre_result.str = NULL;
  411. }
  412. }
  413. }
  414. return 0;
  415. }
  416. struct lua_tree_cb_data {
  417. lua_State *L;
  418. int i;
  419. };
  420. static gboolean
  421. lua_tree_url_callback (gpointer key, gpointer value, gpointer ud)
  422. {
  423. struct uri **purl;
  424. struct lua_tree_cb_data *cb = ud;
  425. purl = lua_newuserdata (cb->L, sizeof (struct uri *));
  426. rspamd_lua_setclass (cb->L, "rspamd{url}", -1);
  427. *purl = value;
  428. lua_rawseti (cb->L, -2, cb->i++);
  429. return FALSE;
  430. }
  431. static gint
  432. lua_task_get_urls (lua_State * L)
  433. {
  434. struct rspamd_task *task = lua_check_task (L);
  435. struct lua_tree_cb_data cb;
  436. if (task) {
  437. lua_newtable (L);
  438. cb.i = 1;
  439. cb.L = L;
  440. g_tree_foreach (task->urls, lua_tree_url_callback, &cb);
  441. return 1;
  442. }
  443. lua_pushnil (L);
  444. return 1;
  445. }
  446. static gint
  447. lua_task_get_emails (lua_State * L)
  448. {
  449. struct rspamd_task *task = lua_check_task (L);
  450. struct lua_tree_cb_data cb;
  451. if (task) {
  452. lua_newtable (L);
  453. cb.i = 1;
  454. cb.L = L;
  455. g_tree_foreach (task->emails, lua_tree_url_callback, &cb);
  456. return 1;
  457. }
  458. lua_pushnil (L);
  459. return 1;
  460. }
  461. static gint
  462. lua_task_get_text_parts (lua_State * L)
  463. {
  464. gint i = 1;
  465. struct rspamd_task *task = lua_check_task (L);
  466. GList *cur;
  467. struct mime_text_part *part, **ppart;
  468. if (task != NULL) {
  469. lua_newtable (L);
  470. cur = task->text_parts;
  471. while (cur) {
  472. part = cur->data;
  473. ppart = lua_newuserdata (L, sizeof (struct mime_text_part *));
  474. *ppart = part;
  475. rspamd_lua_setclass (L, "rspamd{textpart}", -1);
  476. /* Make it array */
  477. lua_rawseti (L, -2, i++);
  478. cur = g_list_next (cur);
  479. }
  480. return 1;
  481. }
  482. lua_pushnil (L);
  483. return 1;
  484. }
  485. static gint
  486. lua_task_get_parts (lua_State * L)
  487. {
  488. gint i = 1;
  489. struct rspamd_task *task = lua_check_task (L);
  490. GList *cur;
  491. struct mime_part *part, **ppart;
  492. if (task != NULL) {
  493. lua_newtable (L);
  494. cur = task->parts;
  495. while (cur) {
  496. part = cur->data;
  497. ppart = lua_newuserdata (L, sizeof (struct mime_part *));
  498. *ppart = part;
  499. rspamd_lua_setclass (L, "rspamd{mimepart}", -1);
  500. /* Make it array */
  501. lua_rawseti (L, -2, i++);
  502. cur = g_list_next (cur);
  503. }
  504. return 1;
  505. }
  506. lua_pushnil (L);
  507. return 1;
  508. }
  509. static gint
  510. lua_task_get_raw_headers (lua_State * L)
  511. {
  512. struct rspamd_task *task = lua_check_task (L);
  513. if (task) {
  514. lua_pushstring (L, task->raw_headers_str);
  515. }
  516. else {
  517. lua_pushnil (L);
  518. }
  519. return 1;
  520. }
  521. static gint
  522. lua_task_get_raw_header_common (lua_State * L, gboolean strong)
  523. {
  524. struct rspamd_task *task = lua_check_task (L);
  525. struct raw_header *rh;
  526. gint i = 1;
  527. const gchar *name;
  528. if (task) {
  529. name = luaL_checkstring (L, 2);
  530. if (name == NULL) {
  531. lua_pushnil (L);
  532. return 1;
  533. }
  534. lua_newtable (L);
  535. rh = g_hash_table_lookup (task->raw_headers, name);
  536. if (rh == NULL) {
  537. return 1;
  538. }
  539. while (rh) {
  540. if (rh->name == NULL) {
  541. rh = rh->next;
  542. continue;
  543. }
  544. /* Check case sensivity */
  545. if (strong) {
  546. if (strcmp (rh->name, name) != 0) {
  547. rh = rh->next;
  548. continue;
  549. }
  550. }
  551. else {
  552. if (g_ascii_strcasecmp (rh->name, name) != 0) {
  553. rh = rh->next;
  554. continue;
  555. }
  556. }
  557. /* Create new associated table for a header */
  558. lua_newtable (L);
  559. rspamd_lua_table_set (L, "name", rh->name);
  560. rspamd_lua_table_set (L, "value", rh->value);
  561. lua_pushstring (L, "tab_separated");
  562. lua_pushboolean (L, rh->tab_separated);
  563. lua_settable (L, -3);
  564. lua_pushstring (L, "empty_separator");
  565. lua_pushboolean (L, rh->empty_separator);
  566. lua_settable (L, -3);
  567. rspamd_lua_table_set (L, "separator", rh->separator);
  568. lua_rawseti (L, -2, i++);
  569. /* Process next element */
  570. rh = rh->next;
  571. }
  572. }
  573. else {
  574. lua_pushnil (L);
  575. }
  576. return 1;
  577. }
  578. static gint
  579. lua_task_get_raw_header (lua_State * L)
  580. {
  581. return lua_task_get_raw_header_common (L, FALSE);
  582. }
  583. static gint
  584. lua_task_get_raw_header_strong (lua_State * L)
  585. {
  586. return lua_task_get_raw_header_common (L, TRUE);
  587. }
  588. static gint
  589. lua_task_get_received_headers (lua_State * L)
  590. {
  591. struct rspamd_task *task = lua_check_task (L);
  592. GList *cur;
  593. struct received_header *rh;
  594. gint i = 1;
  595. if (task) {
  596. lua_newtable (L);
  597. cur = g_list_first (task->received);
  598. while (cur) {
  599. rh = cur->data;
  600. if (rh->is_error || G_UNLIKELY (
  601. rh->from_ip == NULL &&
  602. rh->real_ip == NULL &&
  603. rh->real_hostname == NULL &&
  604. rh->by_hostname == NULL)) {
  605. cur = g_list_next (cur);
  606. continue;
  607. }
  608. lua_newtable (L);
  609. rspamd_lua_table_set (L, "from_hostname", rh->from_hostname);
  610. lua_pushstring (L, "from_ip");
  611. rspamd_lua_ip_push_fromstring (L, rh->from_ip);
  612. lua_settable (L, -3);
  613. rspamd_lua_table_set (L, "real_hostname", rh->real_hostname);
  614. lua_pushstring (L, "real_ip");
  615. rspamd_lua_ip_push_fromstring (L, rh->real_ip);
  616. lua_settable (L, -3);
  617. rspamd_lua_table_set (L, "by_hostname", rh->by_hostname);
  618. lua_rawseti (L, -2, i++);
  619. cur = g_list_next (cur);
  620. }
  621. }
  622. else {
  623. lua_pushnil (L);
  624. }
  625. return 1;
  626. }
  627. static gint
  628. lua_task_get_resolver (lua_State *L)
  629. {
  630. struct rspamd_task *task = lua_check_task (L);
  631. struct rspamd_dns_resolver **presolver;
  632. if (task != NULL && task->resolver != NULL) {
  633. presolver = lua_newuserdata (L, sizeof (void *));
  634. rspamd_lua_setclass (L, "rspamd{resolver}", -1);
  635. *presolver = task->resolver;
  636. }
  637. else {
  638. lua_pushnil (L);
  639. }
  640. return 1;
  641. }
  642. static gint
  643. lua_task_inc_dns_req (lua_State *L)
  644. {
  645. struct rspamd_task *task = lua_check_task (L);
  646. if (task != NULL) {
  647. task->dns_requests++;
  648. }
  649. return 0;
  650. }
  651. static gint
  652. lua_task_call_rspamd_function (lua_State * L)
  653. {
  654. struct rspamd_task *task = lua_check_task (L);
  655. struct expression_function f;
  656. gint i, top;
  657. gboolean res;
  658. gchar *arg;
  659. if (task) {
  660. f.name = (gchar *)luaL_checkstring (L, 2);
  661. if (f.name) {
  662. f.args = NULL;
  663. top = lua_gettop (L);
  664. /* Get arguments after function name */
  665. for (i = 3; i <= top; i++) {
  666. arg = (gchar *)luaL_checkstring (L, i);
  667. if (arg != NULL) {
  668. f.args = g_list_prepend (f.args, arg);
  669. }
  670. }
  671. res = call_expression_function (&f, task, L);
  672. lua_pushboolean (L, res);
  673. if (f.args) {
  674. g_list_free (f.args);
  675. }
  676. return 1;
  677. }
  678. }
  679. lua_pushnil (L);
  680. return 1;
  681. }
  682. static gboolean
  683. lua_push_internet_address (lua_State *L, InternetAddress *ia)
  684. {
  685. #ifndef GMIME24
  686. if (internet_address_get_type (ia) == INTERNET_ADDRESS_NAME) {
  687. lua_newtable (L);
  688. rspamd_lua_table_set (L, "name", internet_address_get_name (ia));
  689. rspamd_lua_table_set (L, "addr", internet_address_get_addr (ia));
  690. return TRUE;
  691. }
  692. return FALSE;
  693. #else
  694. InternetAddressMailbox *iamb;
  695. const char *addr, *at;
  696. if (ia && INTERNET_ADDRESS_IS_MAILBOX (ia)) {
  697. lua_newtable (L);
  698. iamb = INTERNET_ADDRESS_MAILBOX (ia);
  699. addr = internet_address_mailbox_get_addr (iamb);
  700. if (addr) {
  701. rspamd_lua_table_set (L, "name", internet_address_get_name (ia));
  702. rspamd_lua_table_set (L, "addr", addr);
  703. /* Set optional fields */
  704. at = strchr (addr, '@');
  705. if (at != NULL) {
  706. lua_pushstring(L, "user");
  707. lua_pushlstring(L, addr, at - addr);
  708. lua_settable (L, -3);
  709. lua_pushstring(L, "domain");
  710. lua_pushstring(L, at + 1);
  711. lua_settable (L, -3);
  712. }
  713. return TRUE;
  714. }
  715. }
  716. return FALSE;
  717. #endif
  718. }
  719. /*
  720. * Push internet addresses to lua as a table
  721. */
  722. static void
  723. lua_push_internet_address_list (lua_State *L, InternetAddressList *addrs)
  724. {
  725. InternetAddress *ia;
  726. gint idx = 1;
  727. #ifndef GMIME24
  728. /* Gmime 2.2 version */
  729. InternetAddressList *cur;
  730. lua_newtable (L);
  731. cur = addrs;
  732. while (cur) {
  733. ia = internet_address_list_get_address (cur);
  734. if (lua_push_internet_address (L, ia)) {
  735. lua_rawseti (L, -2, idx++);
  736. }
  737. cur = internet_address_list_next (cur);
  738. }
  739. #else
  740. /* Gmime 2.4 version */
  741. gsize len, i;
  742. lua_newtable (L);
  743. if (addrs != NULL) {
  744. len = internet_address_list_length (addrs);
  745. for (i = 0; i < len; i++) {
  746. ia = internet_address_list_get_address (addrs, i);
  747. if (lua_push_internet_address (L, ia)) {
  748. lua_rawseti (L, -2, idx++);
  749. }
  750. }
  751. }
  752. #endif
  753. }
  754. static gint
  755. lua_task_get_recipients (lua_State *L)
  756. {
  757. struct rspamd_task *task = lua_check_task (L);
  758. InternetAddressList *addrs;
  759. gint what = 0;
  760. if (task) {
  761. if (lua_gettop (L) == 2) {
  762. /* Get what value */
  763. what = lua_tonumber (L, 2);
  764. }
  765. switch (what) {
  766. case 1:
  767. /* Here we check merely envelope rcpt */
  768. addrs = task->rcpt_envelope;
  769. break;
  770. case 2:
  771. /* Here we check merely mime rcpt */
  772. addrs = task->rcpt_mime;
  773. break;
  774. case 0:
  775. default:
  776. if (task->rcpt_envelope) {
  777. addrs = task->rcpt_envelope;
  778. }
  779. else {
  780. addrs = task->rcpt_mime;
  781. }
  782. break;
  783. }
  784. if (addrs) {
  785. lua_push_internet_address_list (L, addrs);
  786. }
  787. else {
  788. lua_pushnil (L);
  789. }
  790. }
  791. else {
  792. lua_pushnil (L);
  793. }
  794. return 1;
  795. }
  796. static gint
  797. lua_task_get_from (lua_State *L)
  798. {
  799. struct rspamd_task *task = lua_check_task (L);
  800. InternetAddressList *addrs;
  801. gint what = 0;
  802. if (task) {
  803. if (lua_gettop (L) == 2) {
  804. /* Get what value */
  805. what = lua_tonumber (L, 2);
  806. }
  807. switch (what) {
  808. case 1:
  809. /* Here we check merely envelope rcpt */
  810. addrs = task->from_envelope;
  811. break;
  812. case 2:
  813. /* Here we check merely mime rcpt */
  814. addrs = task->from_mime;
  815. break;
  816. case 0:
  817. default:
  818. if (task->from_envelope) {
  819. addrs = task->from_envelope;
  820. }
  821. else {
  822. addrs = task->from_mime;
  823. }
  824. break;
  825. }
  826. if (addrs) {
  827. lua_push_internet_address_list (L, addrs);
  828. }
  829. else {
  830. lua_pushnil (L);
  831. }
  832. }
  833. else {
  834. lua_pushnil (L);
  835. }
  836. return 1;
  837. }
  838. static gint
  839. lua_task_get_user (lua_State *L)
  840. {
  841. struct rspamd_task *task = lua_check_task (L);
  842. if (task && task->user != NULL) {
  843. lua_pushstring (L, task->user);
  844. return 1;
  845. }
  846. lua_pushnil (L);
  847. return 1;
  848. }
  849. static gint
  850. lua_task_set_user (lua_State *L)
  851. {
  852. struct rspamd_task *task = lua_check_task (L);
  853. const gchar *new_user;
  854. if (task) {
  855. new_user = luaL_checkstring (L, 2);
  856. if (new_user) {
  857. task->user = rspamd_mempool_strdup (task->task_pool, new_user);
  858. }
  859. }
  860. return 0;
  861. }
  862. static gint
  863. lua_task_get_from_ip (lua_State *L)
  864. {
  865. struct rspamd_task *task = lua_check_task (L);
  866. if (task) {
  867. rspamd_lua_ip_push (L, &task->from_addr);
  868. }
  869. else {
  870. lua_pushnil (L);
  871. }
  872. return 1;
  873. }
  874. static gint
  875. lua_task_set_from_ip (lua_State *L)
  876. {
  877. msg_err ("this function is deprecated and should no longer be used");
  878. return 0;
  879. }
  880. static gint
  881. lua_task_get_from_ip_num (lua_State *L)
  882. {
  883. msg_err ("this function is deprecated and should no longer be used");
  884. lua_pushnil (L);
  885. return 1;
  886. }
  887. static gint
  888. lua_task_get_client_ip_num (lua_State *L)
  889. {
  890. struct rspamd_task *task = lua_check_task (L);
  891. if (task) {
  892. rspamd_lua_ip_push (L, &task->client_addr);
  893. }
  894. else {
  895. lua_pushnil (L);
  896. }
  897. return 1;
  898. }
  899. static gint
  900. lua_task_get_helo (lua_State *L)
  901. {
  902. struct rspamd_task *task = lua_check_task (L);
  903. if (task) {
  904. if (task->helo != NULL) {
  905. lua_pushstring (L, (gchar *)task->helo);
  906. return 1;
  907. }
  908. }
  909. lua_pushnil (L);
  910. return 1;
  911. }
  912. static gint
  913. lua_task_set_helo (lua_State *L)
  914. {
  915. struct rspamd_task *task = lua_check_task (L);
  916. const gchar *new_helo;
  917. if (task) {
  918. new_helo = luaL_checkstring (L, 2);
  919. if (new_helo) {
  920. task->helo = rspamd_mempool_strdup (task->task_pool, new_helo);
  921. }
  922. }
  923. return 0;
  924. }
  925. static gint
  926. lua_task_get_hostname (lua_State *L)
  927. {
  928. struct rspamd_task *task = lua_check_task (L);
  929. if (task) {
  930. if (task->hostname != NULL) {
  931. /* Check whether it looks like an IP address */
  932. if (*task->hostname == '[') {
  933. /*
  934. * From the milter documentation:
  935. * If the reverse lookup fails or if none of the IP
  936. * addresses of the resolved host name matches the
  937. * original IP address, hostname will contain the
  938. * message sender's IP address enclosed in square
  939. * brackets (e.g. `[a.b.c.d]')
  940. */
  941. lua_pushstring (L, "unknown");
  942. }
  943. else {
  944. lua_pushstring (L, task->hostname);
  945. }
  946. return 1;
  947. }
  948. }
  949. lua_pushnil (L);
  950. return 1;
  951. }
  952. static gint
  953. lua_task_set_hostname (lua_State *L)
  954. {
  955. struct rspamd_task *task = lua_check_task (L);
  956. const gchar *new_hostname;
  957. if (task) {
  958. new_hostname = luaL_checkstring (L, 2);
  959. if (new_hostname) {
  960. task->hostname = rspamd_mempool_strdup (task->task_pool,
  961. new_hostname);
  962. }
  963. }
  964. return 0;
  965. }
  966. static gint
  967. lua_task_get_images (lua_State *L)
  968. {
  969. struct rspamd_task *task = lua_check_task (L);
  970. gint i = 1;
  971. GList *cur;
  972. struct rspamd_image **pimg;
  973. if (task) {
  974. cur = task->images;
  975. if (cur != NULL) {
  976. lua_newtable (L);
  977. while (cur) {
  978. pimg = lua_newuserdata (L, sizeof (struct rspamd_image *));
  979. rspamd_lua_setclass (L, "rspamd{image}", -1);
  980. *pimg = cur->data;
  981. lua_rawseti (L, -2, i++);
  982. cur = g_list_next (cur);
  983. }
  984. return 1;
  985. }
  986. }
  987. lua_pushnil (L);
  988. return 1;
  989. }
  990. static inline gboolean
  991. lua_push_symbol_result (lua_State *L,
  992. struct rspamd_task *task,
  993. struct metric *metric,
  994. const gchar *symbol)
  995. {
  996. struct metric_result *metric_res;
  997. struct symbol *s;
  998. gint j;
  999. GList *opt;
  1000. metric_res = g_hash_table_lookup (task->results, metric->name);
  1001. if (metric_res) {
  1002. if ((s = g_hash_table_lookup (metric_res->symbols, symbol)) != NULL) {
  1003. j = 0;
  1004. lua_newtable (L);
  1005. lua_pushstring (L, "metric");
  1006. lua_pushstring (L, metric->name);
  1007. lua_settable (L, -3);
  1008. lua_pushstring (L, "score");
  1009. lua_pushnumber (L, s->score);
  1010. lua_settable (L, -3);
  1011. if (s->options) {
  1012. opt = s->options;
  1013. lua_pushstring (L, "options");
  1014. lua_newtable (L);
  1015. while (opt) {
  1016. lua_pushstring (L, opt->data);
  1017. lua_rawseti (L, -2, j++);
  1018. opt = g_list_next (opt);
  1019. }
  1020. lua_settable (L, -3);
  1021. }
  1022. return TRUE;
  1023. }
  1024. }
  1025. return FALSE;
  1026. }
  1027. static gint
  1028. lua_task_get_symbol (lua_State *L)
  1029. {
  1030. struct rspamd_task *task = lua_check_task (L);
  1031. const gchar *symbol;
  1032. struct metric *metric;
  1033. GList *cur = NULL, *metric_list;
  1034. gboolean found = FALSE;
  1035. gint i = 1;
  1036. symbol = luaL_checkstring (L, 2);
  1037. if (task && symbol) {
  1038. metric_list = g_hash_table_lookup (task->cfg->metrics_symbols, symbol);
  1039. if (metric_list) {
  1040. lua_newtable (L);
  1041. cur = metric_list;
  1042. }
  1043. else {
  1044. metric = task->cfg->default_metric;
  1045. }
  1046. if (!cur && metric) {
  1047. if ((found = lua_push_symbol_result (L, task, metric, symbol))) {
  1048. lua_newtable (L);
  1049. lua_rawseti (L, -2, i++);
  1050. }
  1051. }
  1052. else {
  1053. while (cur) {
  1054. metric = cur->data;
  1055. if (lua_push_symbol_result (L, task, metric, symbol)) {
  1056. lua_rawseti (L, -2, i++);
  1057. found = TRUE;
  1058. }
  1059. cur = g_list_next (cur);
  1060. }
  1061. }
  1062. }
  1063. if (!found) {
  1064. lua_pushnil (L);
  1065. }
  1066. return 1;
  1067. }
  1068. static gint
  1069. lua_task_get_date (lua_State *L)
  1070. {
  1071. struct rspamd_task *task = lua_check_task (L);
  1072. time_t task_time;
  1073. if (task != NULL) {
  1074. /* Get GMT date and store it to time_t */
  1075. task_time = task->tv.tv_sec;
  1076. lua_pushnumber (L, task_time);
  1077. }
  1078. else {
  1079. lua_pushnil (L);
  1080. }
  1081. return 1;
  1082. }
  1083. static gint
  1084. lua_task_get_message_id (lua_State *L)
  1085. {
  1086. struct rspamd_task *task = lua_check_task (L);
  1087. if (task != NULL && task->message_id != NULL) {
  1088. lua_pushstring (L, task->message_id);
  1089. }
  1090. else {
  1091. lua_pushnil (L);
  1092. }
  1093. return 1;
  1094. }
  1095. static gint
  1096. lua_task_get_timeval (lua_State *L)
  1097. {
  1098. struct rspamd_task *task = lua_check_task (L);
  1099. if (task != NULL) {
  1100. lua_newtable (L);
  1101. lua_pushstring (L, "tv_sec");
  1102. lua_pushnumber (L, (lua_Number)task->tv.tv_sec);
  1103. lua_settable (L, -3);
  1104. lua_pushstring (L, "tv_usec");
  1105. lua_pushnumber (L, (lua_Number)task->tv.tv_usec);
  1106. lua_settable (L, -3);
  1107. }
  1108. else {
  1109. lua_pushnil (L);
  1110. }
  1111. return 1;
  1112. }
  1113. static gint
  1114. lua_task_learn (lua_State *L)
  1115. {
  1116. struct rspamd_task *task = lua_check_task (L);
  1117. gboolean is_spam = FALSE;
  1118. const gchar *clname;
  1119. struct rspamd_classifier_config *cl;
  1120. GError *err = NULL;
  1121. int ret = 1;
  1122. is_spam = lua_toboolean(L, 2);
  1123. if (lua_gettop (L) > 2) {
  1124. clname = luaL_checkstring (L, 3);
  1125. }
  1126. else {
  1127. clname = "bayes";
  1128. }
  1129. cl = rspamd_config_find_classifier (task->cfg, clname);
  1130. if (cl == NULL) {
  1131. msg_warn ("classifier %s is not found", clname);
  1132. lua_pushboolean (L, FALSE);
  1133. lua_pushstring (L, "classifier not found");
  1134. ret = 2;
  1135. }
  1136. else {
  1137. if (!learn_task_spam (cl, task, is_spam, &err)) {
  1138. lua_pushboolean (L, FALSE);
  1139. if (err != NULL) {
  1140. lua_pushstring (L, err->message);
  1141. ret = 2;
  1142. }
  1143. }
  1144. else {
  1145. lua_pushboolean (L, TRUE);
  1146. }
  1147. }
  1148. return ret;
  1149. }
  1150. static gint
  1151. lua_task_set_settings (lua_State *L)
  1152. {
  1153. struct rspamd_task *task = lua_check_task (L);
  1154. ucl_object_t *settings;
  1155. settings = ucl_object_lua_import (L, 2);
  1156. if (settings != NULL) {
  1157. task->settings = settings;
  1158. }
  1159. return 0;
  1160. }
  1161. static gint
  1162. lua_task_get_metric_score (lua_State *L)
  1163. {
  1164. struct rspamd_task *task = lua_check_task (L);
  1165. const gchar *metric_name;
  1166. struct metric_result *metric_res;
  1167. metric_name = luaL_checkstring (L, 2);
  1168. if (task && metric_name) {
  1169. if ((metric_res =
  1170. g_hash_table_lookup (task->results, metric_name)) != NULL) {
  1171. lua_newtable (L);
  1172. lua_pushnumber (L, metric_res->score);
  1173. lua_rawseti (L, -2, 1);
  1174. lua_pushnumber (L,
  1175. metric_res->metric->actions[METRIC_ACTION_REJECT].score);
  1176. lua_rawseti (L, -2, 2);
  1177. lua_pushnumber (L,
  1178. metric_res->metric->actions[METRIC_ACTION_REJECT].score);
  1179. lua_rawseti (L, -2, 3);
  1180. }
  1181. else {
  1182. lua_pushnil (L);
  1183. }
  1184. return 1;
  1185. }
  1186. return 0;
  1187. }
  1188. static gint
  1189. lua_task_get_metric_action (lua_State *L)
  1190. {
  1191. struct rspamd_task *task = lua_check_task (L);
  1192. const gchar *metric_name;
  1193. struct metric_result *metric_res;
  1194. enum rspamd_metric_action action;
  1195. metric_name = luaL_checkstring (L, 2);
  1196. if (task && metric_name) {
  1197. if ((metric_res =
  1198. g_hash_table_lookup (task->results, metric_name)) != NULL) {
  1199. action = check_metric_action (task, metric_res->score,
  1200. NULL,
  1201. metric_res->metric);
  1202. lua_pushstring (L, str_action_metric (action));
  1203. }
  1204. else {
  1205. lua_pushnil (L);
  1206. }
  1207. return 1;
  1208. }
  1209. return 0;
  1210. }
  1211. /**** Textpart implementation *****/
  1212. static gint
  1213. lua_textpart_get_content (lua_State * L)
  1214. {
  1215. struct mime_text_part *part = lua_check_textpart (L);
  1216. if (part == NULL || part->is_empty) {
  1217. lua_pushnil (L);
  1218. return 1;
  1219. }
  1220. lua_pushlstring (L, (const gchar *)part->content->data, part->content->len);
  1221. return 1;
  1222. }
  1223. static gint
  1224. lua_textpart_get_length (lua_State * L)
  1225. {
  1226. struct mime_text_part *part = lua_check_textpart (L);
  1227. if (part == NULL) {
  1228. lua_pushnil (L);
  1229. return 1;
  1230. }
  1231. if (part->is_empty) {
  1232. lua_pushnumber (L, 0);
  1233. }
  1234. else {
  1235. lua_pushnumber (L, part->content->len);
  1236. }
  1237. return 1;
  1238. }
  1239. static gint
  1240. lua_textpart_is_empty (lua_State * L)
  1241. {
  1242. struct mime_text_part *part = lua_check_textpart (L);
  1243. if (part == NULL) {
  1244. lua_pushnil (L);
  1245. return 1;
  1246. }
  1247. lua_pushboolean (L, part->is_empty);
  1248. return 1;
  1249. }
  1250. static gint
  1251. lua_textpart_is_html (lua_State * L)
  1252. {
  1253. struct mime_text_part *part = lua_check_textpart (L);
  1254. if (part == NULL) {
  1255. lua_pushnil (L);
  1256. return 1;
  1257. }
  1258. lua_pushboolean (L, part->is_html);
  1259. return 1;
  1260. }
  1261. static gint
  1262. lua_textpart_get_fuzzy (lua_State * L)
  1263. {
  1264. struct mime_text_part *part = lua_check_textpart (L);
  1265. if (part == NULL || part->is_empty) {
  1266. lua_pushnil (L);
  1267. return 1;
  1268. }
  1269. lua_pushlstring (L, part->fuzzy->hash_pipe,
  1270. sizeof (part->fuzzy->hash_pipe));
  1271. return 1;
  1272. }
  1273. static gint
  1274. lua_textpart_get_language (lua_State * L)
  1275. {
  1276. struct mime_text_part *part = lua_check_textpart (L);
  1277. static const gchar languages[][4] = {
  1278. "", /* G_UNICODE_SCRIPT_COMMON */
  1279. "", /* G_UNICODE_SCRIPT_INHERITED */
  1280. "ar", /* G_UNICODE_SCRIPT_ARABIC */
  1281. "hy", /* G_UNICODE_SCRIPT_ARMENIAN */
  1282. "bn", /* G_UNICODE_SCRIPT_BENGALI */
  1283. /* Used primarily in Taiwan, but not part of the standard
  1284. * zh-tw orthography */
  1285. "", /* G_UNICODE_SCRIPT_BOPOMOFO */
  1286. "chr", /* G_UNICODE_SCRIPT_CHEROKEE */
  1287. "cop", /* G_UNICODE_SCRIPT_COPTIC */
  1288. "ru", /* G_UNICODE_SCRIPT_CYRILLIC */
  1289. /* Deseret was used to write English */
  1290. "", /* G_UNICODE_SCRIPT_DESERET */
  1291. "hi", /* G_UNICODE_SCRIPT_DEVANAGARI */
  1292. "am", /* G_UNICODE_SCRIPT_ETHIOPIC */
  1293. "ka", /* G_UNICODE_SCRIPT_GEORGIAN */
  1294. "", /* G_UNICODE_SCRIPT_GOTHIC */
  1295. "el", /* G_UNICODE_SCRIPT_GREEK */
  1296. "gu", /* G_UNICODE_SCRIPT_GUJARATI */
  1297. "pa", /* G_UNICODE_SCRIPT_GURMUKHI */
  1298. "", /* G_UNICODE_SCRIPT_HAN */
  1299. "ko", /* G_UNICODE_SCRIPT_HANGUL */
  1300. "he", /* G_UNICODE_SCRIPT_HEBREW */
  1301. "ja", /* G_UNICODE_SCRIPT_HIRAGANA */
  1302. "kn", /* G_UNICODE_SCRIPT_KANNADA */
  1303. "ja", /* G_UNICODE_SCRIPT_KATAKANA */
  1304. "km", /* G_UNICODE_SCRIPT_KHMER */
  1305. "lo", /* G_UNICODE_SCRIPT_LAO */
  1306. "en", /* G_UNICODE_SCRIPT_LATIN */
  1307. "ml", /* G_UNICODE_SCRIPT_MALAYALAM */
  1308. "mn", /* G_UNICODE_SCRIPT_MONGOLIAN */
  1309. "my", /* G_UNICODE_SCRIPT_MYANMAR */
  1310. /* Ogham was used to write old Irish */
  1311. "", /* G_UNICODE_SCRIPT_OGHAM */
  1312. "", /* G_UNICODE_SCRIPT_OLD_ITALIC */
  1313. "or", /* G_UNICODE_SCRIPT_ORIYA */
  1314. "", /* G_UNICODE_SCRIPT_RUNIC */
  1315. "si", /* G_UNICODE_SCRIPT_SINHALA */
  1316. "syr", /* G_UNICODE_SCRIPT_SYRIAC */
  1317. "ta", /* G_UNICODE_SCRIPT_TAMIL */
  1318. "te", /* G_UNICODE_SCRIPT_TELUGU */
  1319. "dv", /* G_UNICODE_SCRIPT_THAANA */
  1320. "th", /* G_UNICODE_SCRIPT_THAI */
  1321. "bo", /* G_UNICODE_SCRIPT_TIBETAN */
  1322. "iu", /* G_UNICODE_SCRIPT_CANADIAN_ABORIGINAL */
  1323. "", /* G_UNICODE_SCRIPT_YI */
  1324. "tl", /* G_UNICODE_SCRIPT_TAGALOG */
  1325. /* Phillipino languages/scripts */
  1326. "hnn", /* G_UNICODE_SCRIPT_HANUNOO */
  1327. "bku", /* G_UNICODE_SCRIPT_BUHID */
  1328. "tbw", /* G_UNICODE_SCRIPT_TAGBANWA */
  1329. "", /* G_UNICODE_SCRIPT_BRAILLE */
  1330. "", /* G_UNICODE_SCRIPT_CYPRIOT */
  1331. "", /* G_UNICODE_SCRIPT_LIMBU */
  1332. /* Used for Somali (so) in the past */
  1333. "", /* G_UNICODE_SCRIPT_OSMANYA */
  1334. /* The Shavian alphabet was designed for English */
  1335. "", /* G_UNICODE_SCRIPT_SHAVIAN */
  1336. "", /* G_UNICODE_SCRIPT_LINEAR_B */
  1337. "", /* G_UNICODE_SCRIPT_TAI_LE */
  1338. "uga", /* G_UNICODE_SCRIPT_UGARITIC */
  1339. "", /* G_UNICODE_SCRIPT_NEW_TAI_LUE */
  1340. "bug", /* G_UNICODE_SCRIPT_BUGINESE */
  1341. /* The original script for Old Church Slavonic (chu), later
  1342. * written with Cyrillic */
  1343. "", /* G_UNICODE_SCRIPT_GLAGOLITIC */
  1344. /* Used for for Berber (ber), but Arabic script is more common */
  1345. "", /* G_UNICODE_SCRIPT_TIFINAGH */
  1346. "syl", /* G_UNICODE_SCRIPT_SYLOTI_NAGRI */
  1347. "peo", /* G_UNICODE_SCRIPT_OLD_PERSIAN */
  1348. "", /* G_UNICODE_SCRIPT_KHAROSHTHI */
  1349. "", /* G_UNICODE_SCRIPT_UNKNOWN */
  1350. "", /* G_UNICODE_SCRIPT_BALINESE */
  1351. "", /* G_UNICODE_SCRIPT_CUNEIFORM */
  1352. "", /* G_UNICODE_SCRIPT_PHOENICIAN */
  1353. "", /* G_UNICODE_SCRIPT_PHAGS_PA */
  1354. "nqo" /* G_UNICODE_SCRIPT_NKO */
  1355. };
  1356. const gchar *sel;
  1357. if (part != NULL && part->script > 0 && part->script <
  1358. (gint)G_N_ELEMENTS (languages)) {
  1359. sel = languages[part->script];
  1360. if (*sel != '\0') {
  1361. lua_pushstring (L, sel);
  1362. return 1;
  1363. }
  1364. }
  1365. lua_pushnil (L);
  1366. return 1;
  1367. }
  1368. static gint
  1369. lua_textpart_compare_distance (lua_State * L)
  1370. {
  1371. struct mime_text_part *part = lua_check_textpart (L), *other;
  1372. void *ud = luaL_checkudata (L, 2, "rspamd{textpart}");
  1373. gint diff = -1;
  1374. GMimeObject *parent;
  1375. const GMimeContentType *ct;
  1376. luaL_argcheck (L, ud != NULL, 2, "'textpart' expected");
  1377. other = ud ? *((struct mime_text_part **)ud) : NULL;
  1378. if (other != NULL && part->parent && part->parent == other->parent) {
  1379. parent = part->parent;
  1380. ct = g_mime_object_get_content_type (parent);
  1381. #ifndef GMIME24
  1382. if (ct == NULL ||
  1383. !g_mime_content_type_is_type (ct, "multipart", "alternative")) {
  1384. #else
  1385. if (ct == NULL ||
  1386. !g_mime_content_type_is_type ((GMimeContentType *)ct, "multipart",
  1387. "alternative")) {
  1388. #endif
  1389. diff = -1;
  1390. }
  1391. else {
  1392. if (!part->is_empty && !other->is_empty) {
  1393. if (part->diff_str != NULL && other->diff_str != NULL) {
  1394. diff = compare_diff_distance (part->diff_str,
  1395. other->diff_str);
  1396. }
  1397. else {
  1398. diff = fuzzy_compare_parts (part, other);
  1399. }
  1400. }
  1401. else if ((part->is_empty &&
  1402. !other->is_empty) || (!part->is_empty && other->is_empty)) {
  1403. /* Empty and non empty parts are different */
  1404. diff = 0;
  1405. }
  1406. }
  1407. }
  1408. else {
  1409. diff = -1;
  1410. }
  1411. lua_pushinteger (L, diff);
  1412. return 1;
  1413. }
  1414. /* Mimepart implementation */
  1415. static gint
  1416. lua_mimepart_get_content (lua_State * L)
  1417. {
  1418. struct mime_part *part = lua_check_mimepart (L);
  1419. if (part == NULL) {
  1420. lua_pushnil (L);
  1421. return 1;
  1422. }
  1423. lua_pushlstring (L, (const gchar *)part->content->data, part->content->len);
  1424. return 1;
  1425. }
  1426. static gint
  1427. lua_mimepart_get_length (lua_State * L)
  1428. {
  1429. struct mime_part *part = lua_check_mimepart (L);
  1430. if (part == NULL) {
  1431. lua_pushnil (L);
  1432. return 1;
  1433. }
  1434. lua_pushinteger (L, part->content->len);
  1435. return 1;
  1436. }
  1437. static gint
  1438. lua_mimepart_get_type (lua_State * L)
  1439. {
  1440. struct mime_part *part = lua_check_mimepart (L);
  1441. if (part == NULL) {
  1442. lua_pushnil (L);
  1443. lua_pushnil (L);
  1444. return 2;
  1445. }
  1446. #ifndef GMIME24
  1447. lua_pushstring (L, part->type->type);
  1448. lua_pushstring (L, part->type->subtype);
  1449. #else
  1450. lua_pushstring (L, g_mime_content_type_get_media_type (part->type));
  1451. lua_pushstring (L, g_mime_content_type_get_media_subtype (part->type));
  1452. #endif
  1453. return 2;
  1454. }
  1455. static gint
  1456. lua_mimepart_get_filename (lua_State * L)
  1457. {
  1458. struct mime_part *part = lua_check_mimepart (L);
  1459. if (part == NULL || part->filename == NULL) {
  1460. lua_pushnil (L);
  1461. return 1;
  1462. }
  1463. lua_pushstring (L, part->filename);
  1464. return 1;
  1465. }
  1466. /* Image functions */
  1467. static gint
  1468. lua_image_get_width (lua_State *L)
  1469. {
  1470. struct rspamd_image *img = lua_check_image (L);
  1471. if (img != NULL) {
  1472. lua_pushnumber (L, img->width);
  1473. }
  1474. else {
  1475. lua_pushnumber (L, 0);
  1476. }
  1477. return 1;
  1478. }
  1479. static gint
  1480. lua_image_get_height (lua_State *L)
  1481. {
  1482. struct rspamd_image *img = lua_check_image (L);
  1483. if (img != NULL) {
  1484. lua_pushnumber (L, img->height);
  1485. }
  1486. else {
  1487. lua_pushnumber (L, 0);
  1488. }
  1489. return 1;
  1490. }
  1491. static gint
  1492. lua_image_get_type (lua_State *L)
  1493. {
  1494. struct rspamd_image *img = lua_check_image (L);
  1495. if (img != NULL) {
  1496. lua_pushstring (L, image_type_str (img->type));
  1497. }
  1498. else {
  1499. lua_pushnil (L);
  1500. }
  1501. return 1;
  1502. }
  1503. static gint
  1504. lua_image_get_size (lua_State *L)
  1505. {
  1506. struct rspamd_image *img = lua_check_image (L);
  1507. if (img != NULL) {
  1508. lua_pushinteger (L, img->data->len);
  1509. }
  1510. else {
  1511. lua_pushnil (L);
  1512. }
  1513. return 1;
  1514. }
  1515. static gint
  1516. lua_image_get_filename (lua_State *L)
  1517. {
  1518. struct rspamd_image *img = lua_check_image (L);
  1519. if (img != NULL && img->filename != NULL) {
  1520. lua_pushstring (L, img->filename);
  1521. }
  1522. else {
  1523. lua_pushnil (L);
  1524. }
  1525. return 1;
  1526. }
  1527. /* URL part */
  1528. static gint
  1529. lua_url_get_length (lua_State *L)
  1530. {
  1531. struct uri *url = lua_check_url (L);
  1532. if (url != NULL) {
  1533. lua_pushinteger (L, strlen (struri (url)));
  1534. }
  1535. else {
  1536. lua_pushnil (L);
  1537. }
  1538. return 1;
  1539. }
  1540. static gint
  1541. lua_url_get_host (lua_State *L)
  1542. {
  1543. struct uri *url = lua_check_url (L);
  1544. if (url != NULL) {
  1545. lua_pushlstring (L, url->host, url->hostlen);
  1546. }
  1547. else {
  1548. lua_pushnil (L);
  1549. }
  1550. return 1;
  1551. }
  1552. static gint
  1553. lua_url_get_user (lua_State *L)
  1554. {
  1555. struct uri *url = lua_check_url (L);
  1556. if (url != NULL) {
  1557. lua_pushlstring (L, url->user, url->userlen);
  1558. }
  1559. else {
  1560. lua_pushnil (L);
  1561. }
  1562. return 1;
  1563. }
  1564. static gint
  1565. lua_url_get_path (lua_State *L)
  1566. {
  1567. struct uri *url = lua_check_url (L);
  1568. if (url != NULL) {
  1569. lua_pushlstring (L, url->data, url->datalen);
  1570. }
  1571. else {
  1572. lua_pushnil (L);
  1573. }
  1574. return 1;
  1575. }
  1576. static gint
  1577. lua_url_get_text (lua_State *L)
  1578. {
  1579. struct uri *url = lua_check_url (L);
  1580. if (url != NULL) {
  1581. lua_pushstring (L, struri (url));
  1582. }
  1583. else {
  1584. lua_pushnil (L);
  1585. }
  1586. return 1;
  1587. }
  1588. static gint
  1589. lua_url_is_phished (lua_State *L)
  1590. {
  1591. struct uri *url = lua_check_url (L);
  1592. if (url != NULL) {
  1593. lua_pushboolean (L, url->is_phished);
  1594. }
  1595. else {
  1596. lua_pushnil (L);
  1597. }
  1598. return 1;
  1599. }
  1600. static gint
  1601. lua_url_get_phished (lua_State *L)
  1602. {
  1603. struct uri **purl, *url = lua_check_url (L);
  1604. if (url) {
  1605. if (url->is_phished && url->phished_url != NULL) {
  1606. purl = lua_newuserdata (L, sizeof (struct uri *));
  1607. rspamd_lua_setclass (L, "rspamd{url}", -1);
  1608. *purl = url->phished_url;
  1609. return 1;
  1610. }
  1611. }
  1612. lua_pushnil (L);
  1613. return 1;
  1614. }
  1615. /* Init part */
  1616. static gint
  1617. lua_load_task (lua_State * L)
  1618. {
  1619. lua_newtable (L);
  1620. luaL_register (L, NULL, tasklib_f);
  1621. return 1;
  1622. }
  1623. void
  1624. luaopen_task (lua_State * L)
  1625. {
  1626. rspamd_lua_new_class (L, "rspamd{task}", tasklib_m);
  1627. lua_pop (L, 1); /* remove metatable from stack */
  1628. rspamd_lua_add_preload (L, "rspamd_task", lua_load_task);
  1629. }
  1630. void
  1631. luaopen_textpart (lua_State * L)
  1632. {
  1633. rspamd_lua_new_class (L, "rspamd{textpart}", textpartlib_m);
  1634. luaL_register (L, "rspamd_textpart", null_reg);
  1635. lua_pop (L, 1); /* remove metatable from stack */
  1636. }
  1637. void
  1638. luaopen_mimepart (lua_State * L)
  1639. {
  1640. rspamd_lua_new_class (L, "rspamd{mimepart}", mimepartlib_m);
  1641. lua_pop (L, 1); /* remove metatable from stack */
  1642. }
  1643. void
  1644. luaopen_image (lua_State * L)
  1645. {
  1646. rspamd_lua_new_class (L, "rspamd{image}", imagelib_m);
  1647. lua_pop (L, 1); /* remove metatable from stack */
  1648. }
  1649. void
  1650. luaopen_url (lua_State * L)
  1651. {
  1652. rspamd_lua_new_class (L, "rspamd{url}", urllib_m);
  1653. lua_pop (L, 1); /* remove metatable from stack */
  1654. }