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 59KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509
  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.h"
  35. #include "classifiers.h"
  36. #include "binlog.h"
  37. #include "statfile_sync.h"
  38. #include "diff.h"
  39. /***
  40. * @module rspamd_task
  41. * This module provides routines for tasks manipulation in rspamd. Tasks usually
  42. * represent messages being scanned, and this API provides access to such elements
  43. * as headers, symbols, metrics and so on and so forth. Normally, task objects
  44. * are passed to the lua callbacks allowing to check specific properties of messages
  45. * and add the corresponding symbols to the scan's results.
  46. @example
  47. rspamd_config.DATE_IN_PAST = function(task)
  48. if rspamd_config:get_api_version() >= 5 then
  49. local dm = task:get_date{format = 'message', gmt = true}
  50. local dt = task:get_date{format = 'connect', gmt = true}
  51. -- A day
  52. if dt - dm > 86400 then
  53. return true
  54. end
  55. end
  56. return false
  57. end
  58. */
  59. /* Task creation */
  60. /***
  61. * @function rspamd_task.create_empty()
  62. * Creates new empty task object.
  63. * @return {rspamd_task} task object
  64. */
  65. LUA_FUNCTION_DEF (task, create_empty);
  66. /***
  67. * @function rspamd_task.create_from_buffer(input)
  68. * Creates new task object and load its content from the string provided.
  69. * @param {string} input string that contains MIME message
  70. * @return {rspamd_task} task object
  71. */
  72. LUA_FUNCTION_DEF (task, create_from_buffer);
  73. /* Task methods */
  74. LUA_FUNCTION_DEF (task, get_message);
  75. LUA_FUNCTION_DEF (task, process_message);
  76. /***
  77. * @method task:get_cfg()
  78. * Get configuration object for a task.
  79. * @return {rspamd_config} (config.md)[configuration object] for the task
  80. */
  81. LUA_FUNCTION_DEF (task, get_cfg);
  82. LUA_FUNCTION_DEF (task, set_cfg);
  83. LUA_FUNCTION_DEF (task, destroy);
  84. /***
  85. * @method task:get_mempool()
  86. * Returns memory pool valid for a lifetime of task. It is used internally by
  87. * many rspamd routines.
  88. * @return {rspamd_mempool} memory pool object
  89. */
  90. LUA_FUNCTION_DEF (task, get_mempool);
  91. /***
  92. * @method task:get_session()
  93. * Returns asynchronous session object that is used by many rspamd asynchronous
  94. * utilities internally.
  95. * @return {rspamd_session} session object
  96. */
  97. LUA_FUNCTION_DEF (task, get_session);
  98. /***
  99. * @method task:get_ev_base()
  100. * Return asynchronous event base for using in callbacks and resolver.
  101. * @return {rspamd_ev_base} event base
  102. */
  103. LUA_FUNCTION_DEF (task, get_ev_base);
  104. /***
  105. * @method task:insert_result(symbol, weigth[, option1, ...])
  106. * Insert specific symbol to the tasks scanning results assigning the initial
  107. * weight to it.
  108. * @param {string} symbol symbol to insert
  109. * @param {number} weight initial weight (this weight is multiplied by the metric weight)
  110. * @param {string} options list of optional options attached to a symbol inserted
  111. @example
  112. local function cb(task)
  113. if task:get_header('Some header') then
  114. task:insert_result('SOME_HEADER', 1.0, 'Got some header')
  115. end
  116. end
  117. */
  118. LUA_FUNCTION_DEF (task, insert_result);
  119. /***
  120. * @method task:set_pre_results(action, description)
  121. * Sets pre-result for a task. It is used in pre-filters to specify early results
  122. * of the task scanned. If a pre-filter sets some result, then further processing
  123. * may be skipped. For selecting action it is possible to use global table
  124. * `rspamd_actions` or a string value:
  125. *
  126. * - `reject`: reject message permanently
  127. * - `add header`: add spam header
  128. * - `rewrite subject`: rewrite subject to spam subject
  129. * - `greylist`: greylist message
  130. * @param {rspamd_action or string} action a numeric or string action value
  131. * @param {string} description optional descripton
  132. @example
  133. local function cb(task)
  134. local gr = task:get_header('Greylist')
  135. if gr and gr == 'greylist' then
  136. task:set_pre_result(rspamd_actions['greylist'], 'Greylisting required')
  137. end
  138. end
  139. */
  140. LUA_FUNCTION_DEF (task, set_pre_result);
  141. /***
  142. * @method task:get_urls()
  143. * Get all URLs found in a message.
  144. * @return {table rspamd_url} list of all urls found
  145. @example
  146. local function phishing_cb(task)
  147. local urls = task:get_urls();
  148. if urls then
  149. for _,url in ipairs(urls) do
  150. if url:is_phished() then
  151. return true
  152. end
  153. end
  154. end
  155. return false
  156. end
  157. */
  158. LUA_FUNCTION_DEF (task, get_urls);
  159. /***
  160. * @method task:get_urls()
  161. * Get all email addresses found in a message.
  162. * @return {table rspamd_url} list of all email addresses found
  163. */
  164. LUA_FUNCTION_DEF (task, get_emails);
  165. /***
  166. * @method task:get_text_parts()
  167. * Get all text (and HTML) parts found in a message
  168. * @return {table rspamd_text_part} list of text parts
  169. */
  170. LUA_FUNCTION_DEF (task, get_text_parts);
  171. /***
  172. * @method task:get_parts()
  173. * Get all mime parts found in a message
  174. * @return {table rspamd_mime_part} list of mime parts
  175. */
  176. LUA_FUNCTION_DEF (task, get_parts);
  177. /***
  178. * @method task:get_header(name[, case_sensitive])
  179. * Get decoded value of a header specified with optional case_sensitive flag.
  180. * By default headers are searched in caseless matter.
  181. * @param {string} name name of header to get
  182. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  183. * @return {string} decoded value of a header
  184. */
  185. LUA_FUNCTION_DEF (task, get_header);
  186. /***
  187. * @method task:get_header_raw(name[, case_sensitive])
  188. * Get raw value of a header specified with optional case_sensitive flag.
  189. * By default headers are searched in caseless matter.
  190. * @param {string} name name of header to get
  191. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  192. * @return {string} raw value of a header
  193. */
  194. LUA_FUNCTION_DEF (task, get_header_raw);
  195. /***
  196. * @method task:get_header_full(name[, case_sensitive])
  197. * Get raw value of a header specified with optional case_sensitive flag.
  198. * By default headers are searched in caseless matter. This method returns more
  199. * information about the header as a list of tables with the following structure:
  200. *
  201. * - `name` - name of a header
  202. * - `value` - raw value of a header
  203. * - `decoded` - decoded value of a header
  204. * - `tab_separated` - `true` if a header and a value are separated by `tab` character
  205. * - `empty_separator` - `true` if there are no separator between a header and a value
  206. * @param {string} name name of header to get
  207. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  208. * @return {list of tables} all values of a header as specified above
  209. @example
  210. function check_header_delimiter_tab(task, header_name)
  211. for _,rh in ipairs(task:get_header_full(header_name)) do
  212. if rh['tab_separated'] then return true end
  213. end
  214. return false
  215. end
  216. */
  217. LUA_FUNCTION_DEF (task, get_header_full);
  218. /***
  219. * @method task:get_received_headers()
  220. * Returns a list of tables of parsed received headers. A tables returned have
  221. * the following structure:
  222. *
  223. * - `from_hostname` - string that represents hostname provided by a peer
  224. * - `from_ip` - string representation of IP address as provided by a peer
  225. * - `real_hostname` - hostname as resolved by MTA
  226. * - `real_ip` - string representation of IP as resolved by PTR request of MTA
  227. * - `by_hostname` - MTA hostname
  228. *
  229. * Please note that in some situations rspamd cannot parse all the fields of received headers.
  230. * In that case you should check all strings for validity.
  231. * @return {table of tables} list of received headers described above
  232. */
  233. LUA_FUNCTION_DEF (task, get_received_headers);
  234. /***
  235. * @method task:get_resolver()
  236. * Returns ready to use rspamd_resolver object suitable for making asynchronous DNS requests.
  237. * @return {rspamd_resolver} resolver object associated with the task's session
  238. * @example
  239. local logger = require "rspamd_logger"
  240. local function task_cb(task)
  241. local function dns_cb(resolver, to_resolve, results, err)
  242. -- task object is available due to closure
  243. task:inc_dns_req()
  244. if results then
  245. logger.info(string.format('<%s> [%s] resolved for symbol: %s',
  246. task:get_message_id(), to_resolve, 'EXAMPLE_SYMBOL'))
  247. task:insert_result('EXAMPLE_SYMBOL', 1)
  248. end
  249. end
  250. local r = task:get_resolver()
  251. r:resolve_a(task:get_session(), task:get_mempool(), 'example.com', dns_cb)
  252. end
  253. */
  254. LUA_FUNCTION_DEF (task, get_resolver);
  255. /***
  256. * @method task:inc_dns_req()
  257. * Increment number of DNS requests for the task. Is used just for logging purposes.
  258. */
  259. LUA_FUNCTION_DEF (task, inc_dns_req);
  260. /***
  261. * @method task:call_rspamd_function(function[, param, param...])
  262. * Calls rspamd expression function `func` with the specified parameters.
  263. * It returns the boolean result of function invocation.
  264. * @param {string} function name of internal or registered lua function to call
  265. * @param {list of strings} params parameters for a function
  266. * @return {bool} true or false returned by expression function
  267. */
  268. LUA_FUNCTION_DEF (task, call_rspamd_function);
  269. /***
  270. * @method task:get_recipients([type])
  271. * Return SMTP or MIME recipients for a task. This function returns list of internet addresses each one is a table with the following structure:
  272. *
  273. * - `name` - name of internet address in UTF8, e.g. for `Vsevolod Stakhov <blah@foo.com>` it returns `Vsevolod Stakhov`
  274. * - `addr` - address part of the address
  275. * - `user` - user part (if present) of the address, e.g. `blah`
  276. * - `domain` - domain part (if present), e.g. `foo.com`
  277. * @param {integer} type if specified has the following meaning: `0` means try SMTP recipients and fallback to MIME if failed, `1` means checking merely SMTP recipients and `2` means MIME recipients only
  278. * @return {list of addresses} list of recipients or `nil`
  279. */
  280. LUA_FUNCTION_DEF (task, get_recipients);
  281. /***
  282. * @method task:get_from([type])
  283. * Return SMTP or MIME sender for a task. This function returns list of internet addresses each one is a table with the following structure:
  284. *
  285. * - `name` - name of internet address in UTF8, e.g. for `Vsevolod Stakhov <blah@foo.com>` it returns `Vsevolod Stakhov`
  286. * - `addr` - address part of the address
  287. * - `user` - user part (if present) of the address, e.g. `blah`
  288. * - `domain` - domain part (if present), e.g. `foo.com`
  289. * @param {integer} type if specified has the following meaning: `0` means try SMTP sender and fallback to MIME if failed, `1` means checking merely SMTP sender and `2` means MIME `From:` only
  290. * @return {list of addresses} list of recipients or `nil`
  291. */
  292. LUA_FUNCTION_DEF (task, get_from);
  293. /***
  294. * @method task:get_user()
  295. * Returns authenticated user name for this task if specified by an MTA.
  296. * @return {string} username or nil
  297. */
  298. LUA_FUNCTION_DEF (task, get_user);
  299. LUA_FUNCTION_DEF (task, set_user);
  300. /***
  301. * @method task:get_from_ip()
  302. * Returns [ip_addr](ip.md) object of a sender that is provided by MTA
  303. * @return {rspamd_ip} ip address object
  304. */
  305. LUA_FUNCTION_DEF (task, get_from_ip);
  306. LUA_FUNCTION_DEF (task, set_from_ip);
  307. LUA_FUNCTION_DEF (task, get_from_ip_num);
  308. /***
  309. * @method task:get_client_ip()
  310. * Returns [ip_addr](ip.md) object of a client connected to rspamd (normally, it is an IP address of MTA)
  311. * @return {rspamd_ip} ip address object
  312. */
  313. LUA_FUNCTION_DEF (task, get_client_ip);
  314. /***
  315. * @method task:get_helo()
  316. * Returns the value of SMTP helo provided by MTA.
  317. * @return {string} HELO value
  318. */
  319. LUA_FUNCTION_DEF (task, get_helo);
  320. LUA_FUNCTION_DEF (task, set_helo);
  321. /***
  322. * @method task:get_hostname()
  323. * Returns the value of sender's hostname provided by MTA
  324. * @return {string} hostname value
  325. */
  326. LUA_FUNCTION_DEF (task, get_hostname);
  327. LUA_FUNCTION_DEF (task, set_hostname);
  328. /***
  329. * @method task:get_images()
  330. * Returns list of all images found in a task as a table of `rspamd_image`.
  331. * @return {list of rspamd_image} images found in a message
  332. */
  333. LUA_FUNCTION_DEF (task, get_images);
  334. /***
  335. * @method task:get_symbol(name)
  336. * Searches for a symbol `name` in all metrics results and returns a list of tables
  337. * one per metric that describes the symbol inserted. Please note that this function
  338. * is intended to return values for **inserted** symbols, so if this symbol was not
  339. * inserted it won't be in the function's output. This method is useful for post-filters mainly.
  340. * The symbols are returned as the list of the following tables:
  341. *
  342. * - `metric` - name of metric
  343. * - `score` - score of a symbol in that metric
  344. * - `options` - a table of strings representing options of a symbol
  345. * @param {string} name symbol's name
  346. * @return {list of tables} list of tables or nil if symbol was not found in any metric
  347. */
  348. LUA_FUNCTION_DEF (task, get_symbol);
  349. /***
  350. * @method task:get_date(type[, gmt])
  351. * Returns timestamp for a connection or for a MIME message. This function can be called with a
  352. * single table arguments with the following fields:
  353. *
  354. * * `format` - a format of date returned:
  355. * - `message` - returns a mime date as integer (unix timestamp)
  356. * - `message_str` - returns a mime date as string (UTC format)
  357. * - `connect` - returns a unix timestamp of a connection to rspamd
  358. * - `connect_str` - returns connection time in UTC format
  359. * * `gmt` - returns date in `GMT` timezone (normal for unix timestamps)
  360. *
  361. * By default this function returns connection time in numeric format.
  362. * @param {string} type date format as described above
  363. * @param {boolean} gmt gmt flag as described above
  364. * @return {string/number} date representation according to format
  365. * @example
  366. rspamd_config.DATE_IN_PAST = function(task)
  367. local dm = task:get_date{format = 'message', gmt = true}
  368. local dt = task:get_date{format = 'connect', gmt = true}
  369. -- A day
  370. if dt - dm > 86400 then
  371. return true
  372. end
  373. return false
  374. end
  375. */
  376. LUA_FUNCTION_DEF (task, get_date);
  377. /***
  378. * @method task:get_message_id()
  379. * Returns message id of the specified task
  380. * @return {string} if of a message
  381. */
  382. LUA_FUNCTION_DEF (task, get_message_id);
  383. LUA_FUNCTION_DEF (task, get_timeval);
  384. /***
  385. * @method task:get_metric_score(name)
  386. * Get the current score of metric `name`. Should be used in post-filters only.
  387. * @param {string} name name of a metric
  388. * @return {number} the current score of the metric
  389. */
  390. LUA_FUNCTION_DEF (task, get_metric_score);
  391. /***
  392. * @method task:get_metric_action(name)
  393. * Get the current action of metric `name`. Should be used in post-filters only.
  394. * @param {string} name name of a metric
  395. * @return {string} the current action of the metric as a string
  396. */
  397. LUA_FUNCTION_DEF (task, get_metric_action);
  398. /***
  399. * @method task:learn(is_spam[, classifier)
  400. * Learn classifier `classifier` with the task. If `is_spam` is true then message
  401. * is learnt as spam. Otherwise HAM is learnt. By default, this function learns
  402. * `bayes` classifier.
  403. * @param {boolean} is_spam learn spam or ham
  404. * @param {string} classifier classifier's name
  405. * @return {boolean} `true` if classifier has been learnt successfully
  406. */
  407. LUA_FUNCTION_DEF (task, learn);
  408. /***
  409. * @method task:set_settings(obj)
  410. * Set users settings object for a task. The format of this object is described
  411. * [here](https://rspamd.com/doc/configuration/settings.html).
  412. * @param {any} obj any lua object that corresponds to the settings format
  413. */
  414. LUA_FUNCTION_DEF (task, set_settings);
  415. static const struct luaL_reg tasklib_f[] = {
  416. LUA_INTERFACE_DEF (task, create_empty),
  417. LUA_INTERFACE_DEF (task, create_from_buffer),
  418. {NULL, NULL}
  419. };
  420. static const struct luaL_reg tasklib_m[] = {
  421. LUA_INTERFACE_DEF (task, get_message),
  422. LUA_INTERFACE_DEF (task, destroy),
  423. LUA_INTERFACE_DEF (task, process_message),
  424. LUA_INTERFACE_DEF (task, set_cfg),
  425. LUA_INTERFACE_DEF (task, get_cfg),
  426. LUA_INTERFACE_DEF (task, get_mempool),
  427. LUA_INTERFACE_DEF (task, get_session),
  428. LUA_INTERFACE_DEF (task, get_ev_base),
  429. LUA_INTERFACE_DEF (task, insert_result),
  430. LUA_INTERFACE_DEF (task, set_pre_result),
  431. LUA_INTERFACE_DEF (task, get_urls),
  432. LUA_INTERFACE_DEF (task, get_emails),
  433. LUA_INTERFACE_DEF (task, get_text_parts),
  434. LUA_INTERFACE_DEF (task, get_parts),
  435. LUA_INTERFACE_DEF (task, get_header),
  436. LUA_INTERFACE_DEF (task, get_header_raw),
  437. LUA_INTERFACE_DEF (task, get_header_full),
  438. LUA_INTERFACE_DEF (task, get_received_headers),
  439. LUA_INTERFACE_DEF (task, get_resolver),
  440. LUA_INTERFACE_DEF (task, inc_dns_req),
  441. LUA_INTERFACE_DEF (task, call_rspamd_function),
  442. LUA_INTERFACE_DEF (task, get_recipients),
  443. LUA_INTERFACE_DEF (task, get_from),
  444. LUA_INTERFACE_DEF (task, get_user),
  445. LUA_INTERFACE_DEF (task, set_user),
  446. LUA_INTERFACE_DEF (task, get_from_ip),
  447. LUA_INTERFACE_DEF (task, set_from_ip),
  448. LUA_INTERFACE_DEF (task, get_from_ip_num),
  449. LUA_INTERFACE_DEF (task, get_client_ip),
  450. LUA_INTERFACE_DEF (task, get_helo),
  451. LUA_INTERFACE_DEF (task, set_helo),
  452. LUA_INTERFACE_DEF (task, get_hostname),
  453. LUA_INTERFACE_DEF (task, set_hostname),
  454. LUA_INTERFACE_DEF (task, get_images),
  455. LUA_INTERFACE_DEF (task, get_symbol),
  456. LUA_INTERFACE_DEF (task, get_date),
  457. LUA_INTERFACE_DEF (task, get_message_id),
  458. LUA_INTERFACE_DEF (task, get_timeval),
  459. LUA_INTERFACE_DEF (task, get_metric_score),
  460. LUA_INTERFACE_DEF (task, get_metric_action),
  461. LUA_INTERFACE_DEF (task, learn),
  462. LUA_INTERFACE_DEF (task, set_settings),
  463. {"__tostring", rspamd_lua_class_tostring},
  464. {NULL, NULL}
  465. };
  466. /* Textpart methods */
  467. /***
  468. * @module rspamd_textpart
  469. * This module provides access to text parts found in a message
  470. */
  471. /***
  472. * @method textpart:get_content()
  473. * Get parsed content of a part
  474. * @return {string} text contained in the part
  475. */
  476. LUA_FUNCTION_DEF (textpart, get_content);
  477. /***
  478. * @method textpart:get_length()
  479. * Get length of the text inside the part
  480. * @return {integer} the lenght of text part in bytes
  481. */
  482. LUA_FUNCTION_DEF (textpart, get_length);
  483. /***
  484. * @method textpart:is_empty()
  485. * Check whether a part is empty
  486. * @return {bool} `true` if a part is empty
  487. */
  488. LUA_FUNCTION_DEF (textpart, is_empty);
  489. /***
  490. * @method textpart:is_html()
  491. * Check whether a part contains HTML
  492. * @return {bool} `true` if a part html part
  493. */
  494. LUA_FUNCTION_DEF (textpart, is_html);
  495. /***
  496. * @method textpart:get_fuzzy()
  497. * Get fuzzy hash for a message
  498. * @return {string} fuzzy hash of the part
  499. */
  500. LUA_FUNCTION_DEF (textpart, get_fuzzy);
  501. /***
  502. * @method textpart:get_language()
  503. * Return short language code detected for the part
  504. * @return {string} language code for the part or nil if language has not been detected
  505. */
  506. LUA_FUNCTION_DEF (textpart, get_language);
  507. /***
  508. * @method textpart:compare_distance(part)
  509. * Compares two parts
  510. * @param {textpart} another text part
  511. * @return {number} similarity rate from 0.0 (different parts) to 1.0 (equal parts)
  512. */
  513. LUA_FUNCTION_DEF (textpart, compare_distance);
  514. static const struct luaL_reg textpartlib_m[] = {
  515. LUA_INTERFACE_DEF (textpart, get_content),
  516. LUA_INTERFACE_DEF (textpart, get_length),
  517. LUA_INTERFACE_DEF (textpart, is_empty),
  518. LUA_INTERFACE_DEF (textpart, is_html),
  519. LUA_INTERFACE_DEF (textpart, get_fuzzy),
  520. LUA_INTERFACE_DEF (textpart, get_language),
  521. LUA_INTERFACE_DEF (textpart, compare_distance),
  522. {"__tostring", rspamd_lua_class_tostring},
  523. {NULL, NULL}
  524. };
  525. /* Mimepart methods */
  526. /***
  527. * @module rspamd_mimepart
  528. * This module provides access to mime parts found in a message
  529. @example
  530. rspamd_config.MISSING_CONTENT_TYPE = function(task)
  531. local parts = task:get_parts()
  532. if parts and table.maxn(parts) > 1 then
  533. -- We have more than one part
  534. for _,p in ipairs(parts) do
  535. local ct = p:get_header('Content-Type')
  536. -- And some parts have no Content-Type header
  537. if not ct then
  538. return true
  539. end
  540. end
  541. end
  542. return false
  543. end
  544. */
  545. /***
  546. * @method mimepart:get_header(name[, case_sensitive])
  547. * Get decoded value of a header specified with optional case_sensitive flag.
  548. * By default headers are searched in caseless matter.
  549. * @param {string} name name of header to get
  550. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  551. * @return {string} decoded value of a header
  552. */
  553. LUA_FUNCTION_DEF (mimepart, get_header);
  554. /***
  555. * @method mimepart:get_header_raw(name[, case_sensitive])
  556. * Get raw value of a header specified with optional case_sensitive flag.
  557. * By default headers are searched in caseless matter.
  558. * @param {string} name name of header to get
  559. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  560. * @return {string} raw value of a header
  561. */
  562. LUA_FUNCTION_DEF (mimepart, get_header_raw);
  563. /***
  564. * @method mimepart:get_header_full(name[, case_sensitive])
  565. * Get raw value of a header specified with optional case_sensitive flag.
  566. * By default headers are searched in caseless matter. This method returns more
  567. * information about the header as a list of tables with the following structure:
  568. *
  569. * - `name` - name of a header
  570. * - `value` - raw value of a header
  571. * - `decoded` - decoded value of a header
  572. * - `tab_separated` - `true` if a header and a value are separated by `tab` character
  573. * - `empty_separator` - `true` if there are no separator between a header and a value
  574. * @param {string} name name of header to get
  575. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  576. * @return {list of tables} all values of a header as specified above
  577. @example
  578. function check_header_delimiter_tab(task, header_name)
  579. for _,rh in ipairs(task:get_header_full(header_name)) do
  580. if rh['tab_separated'] then return true end
  581. end
  582. return false
  583. end
  584. */
  585. LUA_FUNCTION_DEF (mimepart, get_header_full);
  586. LUA_FUNCTION_DEF (mimepart, get_content);
  587. LUA_FUNCTION_DEF (mimepart, get_length);
  588. LUA_FUNCTION_DEF (mimepart, get_type);
  589. LUA_FUNCTION_DEF (mimepart, get_filename);
  590. static const struct luaL_reg mimepartlib_m[] = {
  591. LUA_INTERFACE_DEF (mimepart, get_content),
  592. LUA_INTERFACE_DEF (mimepart, get_length),
  593. LUA_INTERFACE_DEF (mimepart, get_type),
  594. LUA_INTERFACE_DEF (mimepart, get_filename),
  595. LUA_INTERFACE_DEF (mimepart, get_header),
  596. LUA_INTERFACE_DEF (mimepart, get_header_raw),
  597. LUA_INTERFACE_DEF (mimepart, get_header_full),
  598. {"__tostring", rspamd_lua_class_tostring},
  599. {NULL, NULL}
  600. };
  601. /* Image methods */
  602. LUA_FUNCTION_DEF (image, get_width);
  603. LUA_FUNCTION_DEF (image, get_height);
  604. LUA_FUNCTION_DEF (image, get_type);
  605. LUA_FUNCTION_DEF (image, get_filename);
  606. LUA_FUNCTION_DEF (image, get_size);
  607. static const struct luaL_reg imagelib_m[] = {
  608. LUA_INTERFACE_DEF (image, get_width),
  609. LUA_INTERFACE_DEF (image, get_height),
  610. LUA_INTERFACE_DEF (image, get_type),
  611. LUA_INTERFACE_DEF (image, get_filename),
  612. LUA_INTERFACE_DEF (image, get_size),
  613. {"__tostring", rspamd_lua_class_tostring},
  614. {NULL, NULL}
  615. };
  616. /* URL methods */
  617. LUA_FUNCTION_DEF (url, get_length);
  618. LUA_FUNCTION_DEF (url, get_host);
  619. LUA_FUNCTION_DEF (url, get_user);
  620. LUA_FUNCTION_DEF (url, get_path);
  621. LUA_FUNCTION_DEF (url, get_text);
  622. LUA_FUNCTION_DEF (url, is_phished);
  623. LUA_FUNCTION_DEF (url, get_phished);
  624. static const struct luaL_reg urllib_m[] = {
  625. LUA_INTERFACE_DEF (url, get_length),
  626. LUA_INTERFACE_DEF (url, get_host),
  627. LUA_INTERFACE_DEF (url, get_user),
  628. LUA_INTERFACE_DEF (url, get_path),
  629. LUA_INTERFACE_DEF (url, get_text),
  630. LUA_INTERFACE_DEF (url, is_phished),
  631. LUA_INTERFACE_DEF (url, get_phished),
  632. {"__tostring", rspamd_lua_class_tostring},
  633. {NULL, NULL}
  634. };
  635. /* Utility functions */
  636. static struct rspamd_task *
  637. lua_check_task (lua_State * L)
  638. {
  639. void *ud = luaL_checkudata (L, 1, "rspamd{task}");
  640. luaL_argcheck (L, ud != NULL, 1, "'task' expected");
  641. return ud ? *((struct rspamd_task **)ud) : NULL;
  642. }
  643. static struct mime_text_part *
  644. lua_check_textpart (lua_State * L)
  645. {
  646. void *ud = luaL_checkudata (L, 1, "rspamd{textpart}");
  647. luaL_argcheck (L, ud != NULL, 1, "'textpart' expected");
  648. return ud ? *((struct mime_text_part **)ud) : NULL;
  649. }
  650. static struct mime_part *
  651. lua_check_mimepart (lua_State * L)
  652. {
  653. void *ud = luaL_checkudata (L, 1, "rspamd{mimepart}");
  654. luaL_argcheck (L, ud != NULL, 1, "'mimepart' expected");
  655. return ud ? *((struct mime_part **)ud) : NULL;
  656. }
  657. static struct rspamd_image *
  658. lua_check_image (lua_State * L)
  659. {
  660. void *ud = luaL_checkudata (L, 1, "rspamd{image}");
  661. luaL_argcheck (L, ud != NULL, 1, "'image' expected");
  662. return ud ? *((struct rspamd_image **)ud) : NULL;
  663. }
  664. static struct uri *
  665. lua_check_url (lua_State * L)
  666. {
  667. void *ud = luaL_checkudata (L, 1, "rspamd{url}");
  668. luaL_argcheck (L, ud != NULL, 1, "'url' expected");
  669. return ud ? *((struct uri **)ud) : NULL;
  670. }
  671. static int
  672. lua_task_create_empty (lua_State *L)
  673. {
  674. struct rspamd_task **ptask, *task;
  675. task = rspamd_task_new (NULL);
  676. ptask = lua_newuserdata (L, sizeof (gpointer));
  677. rspamd_lua_setclass (L, "rspamd{task}", -1);
  678. *ptask = task;
  679. return 1;
  680. }
  681. static int
  682. lua_task_create_from_buffer (lua_State *L)
  683. {
  684. struct rspamd_task **ptask, *task;
  685. const gchar *data;
  686. size_t len;
  687. data = luaL_checklstring (L, 1, &len);
  688. if (data) {
  689. task = rspamd_task_new (NULL);
  690. ptask = lua_newuserdata (L, sizeof (gpointer));
  691. rspamd_lua_setclass (L, "rspamd{task}", -1);
  692. *ptask = task;
  693. task->msg = g_string_new_len (data, len);
  694. }
  695. return 1;
  696. }
  697. static int
  698. lua_task_process_message (lua_State *L)
  699. {
  700. struct rspamd_task *task = lua_check_task (L);
  701. if (task != NULL && task->msg != NULL && task->msg->len > 0) {
  702. if (process_message (task) == 0) {
  703. lua_pushboolean (L, TRUE);
  704. }
  705. else {
  706. lua_pushboolean (L, FALSE);
  707. }
  708. }
  709. else {
  710. lua_pushboolean (L, FALSE);
  711. }
  712. return 1;
  713. }
  714. static int
  715. lua_task_get_cfg (lua_State *L)
  716. {
  717. struct rspamd_task *task = lua_check_task (L);
  718. struct rspamd_config **pcfg;
  719. pcfg = lua_newuserdata (L, sizeof (gpointer));
  720. rspamd_lua_setclass (L, "rspamd{config}", -1);
  721. *pcfg = task->cfg;
  722. return 1;
  723. }
  724. static int
  725. lua_task_set_cfg (lua_State *L)
  726. {
  727. struct rspamd_task *task = lua_check_task (L);
  728. void *ud = luaL_checkudata (L, 2, "rspamd{config}");
  729. luaL_argcheck (L, ud != NULL, 1, "'config' expected");
  730. task->cfg = ud ? *((struct rspamd_config **)ud) : NULL;
  731. return 0;
  732. }
  733. static int
  734. lua_task_destroy (lua_State *L)
  735. {
  736. struct rspamd_task *task = lua_check_task (L);
  737. if (task != NULL) {
  738. rspamd_task_free (task, FALSE);
  739. }
  740. return 0;
  741. }
  742. static int
  743. lua_task_get_message (lua_State * L)
  744. {
  745. GMimeMessage **pmsg;
  746. struct rspamd_task *task = lua_check_task (L);
  747. if (task != NULL && task->message != NULL) {
  748. pmsg = lua_newuserdata (L, sizeof (GMimeMessage *));
  749. rspamd_lua_setclass (L, "rspamd{message}", -1);
  750. *pmsg = task->message;
  751. }
  752. else {
  753. lua_pushnil (L);
  754. }
  755. return 1;
  756. }
  757. static int
  758. lua_task_get_mempool (lua_State * L)
  759. {
  760. rspamd_mempool_t **ppool;
  761. struct rspamd_task *task = lua_check_task (L);
  762. if (task != NULL) {
  763. ppool = lua_newuserdata (L, sizeof (rspamd_mempool_t *));
  764. rspamd_lua_setclass (L, "rspamd{mempool}", -1);
  765. *ppool = task->task_pool;
  766. }
  767. else {
  768. lua_pushnil (L);
  769. }
  770. return 1;
  771. }
  772. static int
  773. lua_task_get_session (lua_State * L)
  774. {
  775. struct rspamd_async_session **psession;
  776. struct rspamd_task *task = lua_check_task (L);
  777. if (task != NULL) {
  778. psession = lua_newuserdata (L, sizeof (void *));
  779. rspamd_lua_setclass (L, "rspamd{session}", -1);
  780. *psession = task->s;
  781. }
  782. else {
  783. lua_pushnil (L);
  784. }
  785. return 1;
  786. }
  787. static int
  788. lua_task_get_ev_base (lua_State * L)
  789. {
  790. struct event_base **pbase;
  791. struct rspamd_task *task = lua_check_task (L);
  792. if (task != NULL) {
  793. pbase = lua_newuserdata (L, sizeof (struct event_base *));
  794. rspamd_lua_setclass (L, "rspamd{ev_base}", -1);
  795. *pbase = task->ev_base;
  796. }
  797. else {
  798. lua_pushnil (L);
  799. }
  800. return 1;
  801. }
  802. static gint
  803. lua_task_insert_result (lua_State * L)
  804. {
  805. struct rspamd_task *task = lua_check_task (L);
  806. const gchar *symbol_name, *param;
  807. double flag;
  808. GList *params = NULL;
  809. gint i, top;
  810. if (task != NULL) {
  811. symbol_name =
  812. rspamd_mempool_strdup (task->task_pool, luaL_checkstring (L, 2));
  813. flag = luaL_checknumber (L, 3);
  814. top = lua_gettop (L);
  815. /* Get additional options */
  816. for (i = 4; i <= top; i++) {
  817. param = luaL_checkstring (L, i);
  818. params =
  819. g_list_prepend (params,
  820. rspamd_mempool_strdup (task->task_pool, param));
  821. }
  822. rspamd_task_insert_result (task, symbol_name, flag, params);
  823. }
  824. return 0;
  825. }
  826. static gint
  827. lua_task_set_pre_result (lua_State * L)
  828. {
  829. struct rspamd_task *task = lua_check_task (L);
  830. struct metric_result *mres;
  831. gchar *action_str;
  832. gint action = METRIC_ACTION_MAX;
  833. if (task != NULL) {
  834. if (lua_type (L, 2) == LUA_TNUMBER) {
  835. action = lua_tointeger (L, 2);
  836. }
  837. else if (lua_type (L, 2) == LUA_TSTRING) {
  838. rspamd_action_from_str (lua_tostring (L, 2), &action);
  839. }
  840. if (action < (gint)task->pre_result.action &&
  841. action < METRIC_ACTION_MAX &&
  842. action >= METRIC_ACTION_REJECT) {
  843. /* We also need to set the default metric to that result */
  844. mres = rspamd_create_metric_result (task, DEFAULT_METRIC);
  845. if (mres != NULL) {
  846. mres->score = mres->metric->actions[action].score;
  847. mres->action = action;
  848. }
  849. task->pre_result.action = action;
  850. if (lua_gettop (L) >= 3) {
  851. action_str = rspamd_mempool_strdup (task->task_pool,
  852. luaL_checkstring (L, 3));
  853. task->pre_result.str = action_str;
  854. task->messages = g_list_prepend (task->messages, action_str);
  855. }
  856. else {
  857. task->pre_result.str = "unknown";
  858. }
  859. msg_info ("<%s>: set pre-result to %s: '%s'",
  860. task->message_id, rspamd_action_to_str (action),
  861. task->pre_result.str);
  862. }
  863. }
  864. return 0;
  865. }
  866. struct lua_tree_cb_data {
  867. lua_State *L;
  868. int i;
  869. };
  870. static gboolean
  871. lua_tree_url_callback (gpointer key, gpointer value, gpointer ud)
  872. {
  873. struct uri **purl;
  874. struct lua_tree_cb_data *cb = ud;
  875. purl = lua_newuserdata (cb->L, sizeof (struct uri *));
  876. rspamd_lua_setclass (cb->L, "rspamd{url}", -1);
  877. *purl = value;
  878. lua_rawseti (cb->L, -2, cb->i++);
  879. return FALSE;
  880. }
  881. static gint
  882. lua_task_get_urls (lua_State * L)
  883. {
  884. struct rspamd_task *task = lua_check_task (L);
  885. struct lua_tree_cb_data cb;
  886. if (task) {
  887. lua_newtable (L);
  888. cb.i = 1;
  889. cb.L = L;
  890. g_tree_foreach (task->urls, lua_tree_url_callback, &cb);
  891. return 1;
  892. }
  893. lua_pushnil (L);
  894. return 1;
  895. }
  896. static gint
  897. lua_task_get_emails (lua_State * L)
  898. {
  899. struct rspamd_task *task = lua_check_task (L);
  900. struct lua_tree_cb_data cb;
  901. if (task) {
  902. lua_newtable (L);
  903. cb.i = 1;
  904. cb.L = L;
  905. g_tree_foreach (task->emails, lua_tree_url_callback, &cb);
  906. return 1;
  907. }
  908. lua_pushnil (L);
  909. return 1;
  910. }
  911. static gint
  912. lua_task_get_text_parts (lua_State * L)
  913. {
  914. gint i = 1;
  915. struct rspamd_task *task = lua_check_task (L);
  916. GList *cur;
  917. struct mime_text_part *part, **ppart;
  918. if (task != NULL) {
  919. lua_newtable (L);
  920. cur = task->text_parts;
  921. while (cur) {
  922. part = cur->data;
  923. ppart = lua_newuserdata (L, sizeof (struct mime_text_part *));
  924. *ppart = part;
  925. rspamd_lua_setclass (L, "rspamd{textpart}", -1);
  926. /* Make it array */
  927. lua_rawseti (L, -2, i++);
  928. cur = g_list_next (cur);
  929. }
  930. return 1;
  931. }
  932. lua_pushnil (L);
  933. return 1;
  934. }
  935. static gint
  936. lua_task_get_parts (lua_State * L)
  937. {
  938. gint i = 1;
  939. struct rspamd_task *task = lua_check_task (L);
  940. GList *cur;
  941. struct mime_part *part, **ppart;
  942. if (task != NULL) {
  943. lua_newtable (L);
  944. cur = task->parts;
  945. while (cur) {
  946. part = cur->data;
  947. ppart = lua_newuserdata (L, sizeof (struct mime_part *));
  948. *ppart = part;
  949. rspamd_lua_setclass (L, "rspamd{mimepart}", -1);
  950. /* Make it array */
  951. lua_rawseti (L, -2, i++);
  952. cur = g_list_next (cur);
  953. }
  954. return 1;
  955. }
  956. lua_pushnil (L);
  957. return 1;
  958. }
  959. static gint
  960. lua_push_header (lua_State * L,
  961. GHashTable *hdrs,
  962. const gchar *name,
  963. gboolean strong,
  964. gboolean full,
  965. gboolean raw)
  966. {
  967. struct raw_header *rh;
  968. gint i = 1;
  969. const gchar *val;
  970. rh = g_hash_table_lookup (hdrs, name);
  971. if (rh == NULL) {
  972. lua_pushnil (L);
  973. return 1;
  974. }
  975. if (full) {
  976. lua_newtable (L);
  977. }
  978. while (rh) {
  979. if (rh->name == NULL) {
  980. rh = rh->next;
  981. continue;
  982. }
  983. /* Check case sensivity */
  984. if (strong) {
  985. if (strcmp (rh->name, name) != 0) {
  986. rh = rh->next;
  987. continue;
  988. }
  989. }
  990. if (full) {
  991. /* Create new associated table for a header */
  992. lua_newtable (L);
  993. rspamd_lua_table_set (L, "name", rh->name);
  994. if (rh->value) {
  995. rspamd_lua_table_set (L, "value", rh->value);
  996. }
  997. if (rh->decoded) {
  998. rspamd_lua_table_set (L, "decoded", rh->value);
  999. }
  1000. lua_pushstring (L, "tab_separated");
  1001. lua_pushboolean (L, rh->tab_separated);
  1002. lua_settable (L, -3);
  1003. lua_pushstring (L, "empty_separator");
  1004. lua_pushboolean (L, rh->empty_separator);
  1005. lua_settable (L, -3);
  1006. rspamd_lua_table_set (L, "separator", rh->separator);
  1007. lua_rawseti (L, -2, i++);
  1008. /* Process next element */
  1009. rh = rh->next;
  1010. }
  1011. else {
  1012. if (raw) {
  1013. val = rh->decoded;
  1014. }
  1015. else {
  1016. val = rh->value;
  1017. }
  1018. if (val) {
  1019. lua_pushstring (L, val);
  1020. }
  1021. else {
  1022. lua_pushnil (L);
  1023. }
  1024. return 1;
  1025. }
  1026. }
  1027. return 1;
  1028. }
  1029. static gint
  1030. lua_task_get_header_common (lua_State *L, gboolean full, gboolean raw)
  1031. {
  1032. gboolean strong = FALSE;
  1033. struct rspamd_task *task = lua_check_task (L);
  1034. const gchar *name;
  1035. name = luaL_checkstring (L, 2);
  1036. if (name && task) {
  1037. if (lua_gettop (L) == 3) {
  1038. strong = lua_toboolean (L, 3);
  1039. }
  1040. return lua_push_header (L, task->raw_headers, name, strong, full, raw);
  1041. }
  1042. lua_pushnil (L);
  1043. return 1;
  1044. }
  1045. static gint
  1046. lua_task_get_header_full (lua_State * L)
  1047. {
  1048. return lua_task_get_header_common (L, TRUE, TRUE);
  1049. }
  1050. static gint
  1051. lua_task_get_header (lua_State * L)
  1052. {
  1053. return lua_task_get_header_common (L, FALSE, FALSE);
  1054. }
  1055. static gint
  1056. lua_task_get_header_raw (lua_State * L)
  1057. {
  1058. return lua_task_get_header_common (L, FALSE, TRUE);
  1059. }
  1060. static gint
  1061. lua_task_get_received_headers (lua_State * L)
  1062. {
  1063. struct rspamd_task *task = lua_check_task (L);
  1064. GList *cur;
  1065. struct received_header *rh;
  1066. gint i = 1;
  1067. if (task) {
  1068. lua_newtable (L);
  1069. cur = g_list_first (task->received);
  1070. while (cur) {
  1071. rh = cur->data;
  1072. if (rh->is_error || G_UNLIKELY (
  1073. rh->from_ip == NULL &&
  1074. rh->real_ip == NULL &&
  1075. rh->real_hostname == NULL &&
  1076. rh->by_hostname == NULL)) {
  1077. cur = g_list_next (cur);
  1078. continue;
  1079. }
  1080. lua_newtable (L);
  1081. rspamd_lua_table_set (L, "from_hostname", rh->from_hostname);
  1082. lua_pushstring (L, "from_ip");
  1083. rspamd_lua_ip_push_fromstring (L, rh->from_ip);
  1084. lua_settable (L, -3);
  1085. rspamd_lua_table_set (L, "real_hostname", rh->real_hostname);
  1086. lua_pushstring (L, "real_ip");
  1087. rspamd_lua_ip_push_fromstring (L, rh->real_ip);
  1088. lua_settable (L, -3);
  1089. rspamd_lua_table_set (L, "by_hostname", rh->by_hostname);
  1090. lua_rawseti (L, -2, i++);
  1091. cur = g_list_next (cur);
  1092. }
  1093. }
  1094. else {
  1095. lua_pushnil (L);
  1096. }
  1097. return 1;
  1098. }
  1099. static gint
  1100. lua_task_get_resolver (lua_State *L)
  1101. {
  1102. struct rspamd_task *task = lua_check_task (L);
  1103. struct rspamd_dns_resolver **presolver;
  1104. if (task != NULL && task->resolver != NULL) {
  1105. presolver = lua_newuserdata (L, sizeof (void *));
  1106. rspamd_lua_setclass (L, "rspamd{resolver}", -1);
  1107. *presolver = task->resolver;
  1108. }
  1109. else {
  1110. lua_pushnil (L);
  1111. }
  1112. return 1;
  1113. }
  1114. static gint
  1115. lua_task_inc_dns_req (lua_State *L)
  1116. {
  1117. struct rspamd_task *task = lua_check_task (L);
  1118. if (task != NULL) {
  1119. task->dns_requests++;
  1120. }
  1121. return 0;
  1122. }
  1123. static gint
  1124. lua_task_call_rspamd_function (lua_State * L)
  1125. {
  1126. struct rspamd_task *task = lua_check_task (L);
  1127. struct expression_function f;
  1128. gint i, top;
  1129. gboolean res;
  1130. gchar *arg;
  1131. if (task) {
  1132. f.name = (gchar *)luaL_checkstring (L, 2);
  1133. if (f.name) {
  1134. f.args = NULL;
  1135. top = lua_gettop (L);
  1136. /* Get arguments after function name */
  1137. for (i = 3; i <= top; i++) {
  1138. arg = (gchar *)luaL_checkstring (L, i);
  1139. if (arg != NULL) {
  1140. f.args = g_list_prepend (f.args, arg);
  1141. }
  1142. }
  1143. res = call_expression_function (&f, task, L);
  1144. lua_pushboolean (L, res);
  1145. if (f.args) {
  1146. g_list_free (f.args);
  1147. }
  1148. return 1;
  1149. }
  1150. }
  1151. lua_pushnil (L);
  1152. return 1;
  1153. }
  1154. static gboolean
  1155. lua_push_internet_address (lua_State *L, InternetAddress *ia)
  1156. {
  1157. #ifndef GMIME24
  1158. if (internet_address_get_type (ia) == INTERNET_ADDRESS_NAME) {
  1159. lua_newtable (L);
  1160. rspamd_lua_table_set (L, "name", internet_address_get_name (ia));
  1161. rspamd_lua_table_set (L, "addr", internet_address_get_addr (ia));
  1162. return TRUE;
  1163. }
  1164. return FALSE;
  1165. #else
  1166. InternetAddressMailbox *iamb;
  1167. const char *addr, *at;
  1168. if (ia && INTERNET_ADDRESS_IS_MAILBOX (ia)) {
  1169. lua_newtable (L);
  1170. iamb = INTERNET_ADDRESS_MAILBOX (ia);
  1171. addr = internet_address_mailbox_get_addr (iamb);
  1172. if (addr) {
  1173. rspamd_lua_table_set (L, "name", internet_address_get_name (ia));
  1174. rspamd_lua_table_set (L, "addr", addr);
  1175. /* Set optional fields */
  1176. at = strchr (addr, '@');
  1177. if (at != NULL) {
  1178. lua_pushstring(L, "user");
  1179. lua_pushlstring(L, addr, at - addr);
  1180. lua_settable (L, -3);
  1181. lua_pushstring(L, "domain");
  1182. lua_pushstring(L, at + 1);
  1183. lua_settable (L, -3);
  1184. }
  1185. return TRUE;
  1186. }
  1187. }
  1188. return FALSE;
  1189. #endif
  1190. }
  1191. /*
  1192. * Push internet addresses to lua as a table
  1193. */
  1194. static void
  1195. lua_push_internet_address_list (lua_State *L, InternetAddressList *addrs)
  1196. {
  1197. InternetAddress *ia;
  1198. gint idx = 1;
  1199. #ifndef GMIME24
  1200. /* Gmime 2.2 version */
  1201. InternetAddressList *cur;
  1202. lua_newtable (L);
  1203. cur = addrs;
  1204. while (cur) {
  1205. ia = internet_address_list_get_address (cur);
  1206. if (lua_push_internet_address (L, ia)) {
  1207. lua_rawseti (L, -2, idx++);
  1208. }
  1209. cur = internet_address_list_next (cur);
  1210. }
  1211. #else
  1212. /* Gmime 2.4 version */
  1213. gsize len, i;
  1214. lua_newtable (L);
  1215. if (addrs != NULL) {
  1216. len = internet_address_list_length (addrs);
  1217. for (i = 0; i < len; i++) {
  1218. ia = internet_address_list_get_address (addrs, i);
  1219. if (lua_push_internet_address (L, ia)) {
  1220. lua_rawseti (L, -2, idx++);
  1221. }
  1222. }
  1223. }
  1224. #endif
  1225. }
  1226. static gint
  1227. lua_task_get_recipients (lua_State *L)
  1228. {
  1229. struct rspamd_task *task = lua_check_task (L);
  1230. InternetAddressList *addrs;
  1231. gint what = 0;
  1232. if (task) {
  1233. if (lua_gettop (L) == 2) {
  1234. /* Get what value */
  1235. what = lua_tonumber (L, 2);
  1236. }
  1237. switch (what) {
  1238. case 1:
  1239. /* Here we check merely envelope rcpt */
  1240. addrs = task->rcpt_envelope;
  1241. break;
  1242. case 2:
  1243. /* Here we check merely mime rcpt */
  1244. addrs = task->rcpt_mime;
  1245. break;
  1246. case 0:
  1247. default:
  1248. if (task->rcpt_envelope) {
  1249. addrs = task->rcpt_envelope;
  1250. }
  1251. else {
  1252. addrs = task->rcpt_mime;
  1253. }
  1254. break;
  1255. }
  1256. if (addrs) {
  1257. lua_push_internet_address_list (L, addrs);
  1258. }
  1259. else {
  1260. lua_pushnil (L);
  1261. }
  1262. }
  1263. else {
  1264. lua_pushnil (L);
  1265. }
  1266. return 1;
  1267. }
  1268. static gint
  1269. lua_task_get_from (lua_State *L)
  1270. {
  1271. struct rspamd_task *task = lua_check_task (L);
  1272. InternetAddressList *addrs;
  1273. gint what = 0;
  1274. if (task) {
  1275. if (lua_gettop (L) == 2) {
  1276. /* Get what value */
  1277. what = lua_tonumber (L, 2);
  1278. }
  1279. switch (what) {
  1280. case 1:
  1281. /* Here we check merely envelope rcpt */
  1282. addrs = task->from_envelope;
  1283. break;
  1284. case 2:
  1285. /* Here we check merely mime rcpt */
  1286. addrs = task->from_mime;
  1287. break;
  1288. case 0:
  1289. default:
  1290. if (task->from_envelope) {
  1291. addrs = task->from_envelope;
  1292. }
  1293. else {
  1294. addrs = task->from_mime;
  1295. }
  1296. break;
  1297. }
  1298. if (addrs) {
  1299. lua_push_internet_address_list (L, addrs);
  1300. }
  1301. else {
  1302. lua_pushnil (L);
  1303. }
  1304. }
  1305. else {
  1306. lua_pushnil (L);
  1307. }
  1308. return 1;
  1309. }
  1310. static gint
  1311. lua_task_get_user (lua_State *L)
  1312. {
  1313. struct rspamd_task *task = lua_check_task (L);
  1314. if (task && task->user != NULL) {
  1315. lua_pushstring (L, task->user);
  1316. return 1;
  1317. }
  1318. lua_pushnil (L);
  1319. return 1;
  1320. }
  1321. static gint
  1322. lua_task_set_user (lua_State *L)
  1323. {
  1324. struct rspamd_task *task = lua_check_task (L);
  1325. const gchar *new_user;
  1326. if (task) {
  1327. new_user = luaL_checkstring (L, 2);
  1328. if (new_user) {
  1329. task->user = rspamd_mempool_strdup (task->task_pool, new_user);
  1330. }
  1331. }
  1332. return 0;
  1333. }
  1334. static gint
  1335. lua_task_get_from_ip (lua_State *L)
  1336. {
  1337. struct rspamd_task *task = lua_check_task (L);
  1338. if (task) {
  1339. rspamd_lua_ip_push (L, &task->from_addr);
  1340. }
  1341. else {
  1342. lua_pushnil (L);
  1343. }
  1344. return 1;
  1345. }
  1346. static gint
  1347. lua_task_set_from_ip (lua_State *L)
  1348. {
  1349. msg_err ("this function is deprecated and should no longer be used");
  1350. return 0;
  1351. }
  1352. static gint
  1353. lua_task_get_from_ip_num (lua_State *L)
  1354. {
  1355. msg_err ("this function is deprecated and should no longer be used");
  1356. lua_pushnil (L);
  1357. return 1;
  1358. }
  1359. static gint
  1360. lua_task_get_client_ip (lua_State *L)
  1361. {
  1362. struct rspamd_task *task = lua_check_task (L);
  1363. if (task) {
  1364. rspamd_lua_ip_push (L, &task->client_addr);
  1365. }
  1366. else {
  1367. lua_pushnil (L);
  1368. }
  1369. return 1;
  1370. }
  1371. static gint
  1372. lua_task_get_helo (lua_State *L)
  1373. {
  1374. struct rspamd_task *task = lua_check_task (L);
  1375. if (task) {
  1376. if (task->helo != NULL) {
  1377. lua_pushstring (L, (gchar *)task->helo);
  1378. return 1;
  1379. }
  1380. }
  1381. lua_pushnil (L);
  1382. return 1;
  1383. }
  1384. static gint
  1385. lua_task_set_helo (lua_State *L)
  1386. {
  1387. struct rspamd_task *task = lua_check_task (L);
  1388. const gchar *new_helo;
  1389. if (task) {
  1390. new_helo = luaL_checkstring (L, 2);
  1391. if (new_helo) {
  1392. task->helo = rspamd_mempool_strdup (task->task_pool, new_helo);
  1393. }
  1394. }
  1395. return 0;
  1396. }
  1397. static gint
  1398. lua_task_get_hostname (lua_State *L)
  1399. {
  1400. struct rspamd_task *task = lua_check_task (L);
  1401. if (task) {
  1402. if (task->hostname != NULL) {
  1403. /* Check whether it looks like an IP address */
  1404. if (*task->hostname == '[') {
  1405. /*
  1406. * From the milter documentation:
  1407. * If the reverse lookup fails or if none of the IP
  1408. * addresses of the resolved host name matches the
  1409. * original IP address, hostname will contain the
  1410. * message sender's IP address enclosed in square
  1411. * brackets (e.g. `[a.b.c.d]')
  1412. */
  1413. lua_pushstring (L, "unknown");
  1414. }
  1415. else {
  1416. lua_pushstring (L, task->hostname);
  1417. }
  1418. return 1;
  1419. }
  1420. }
  1421. lua_pushnil (L);
  1422. return 1;
  1423. }
  1424. static gint
  1425. lua_task_set_hostname (lua_State *L)
  1426. {
  1427. struct rspamd_task *task = lua_check_task (L);
  1428. const gchar *new_hostname;
  1429. if (task) {
  1430. new_hostname = luaL_checkstring (L, 2);
  1431. if (new_hostname) {
  1432. task->hostname = rspamd_mempool_strdup (task->task_pool,
  1433. new_hostname);
  1434. }
  1435. }
  1436. return 0;
  1437. }
  1438. static gint
  1439. lua_task_get_images (lua_State *L)
  1440. {
  1441. struct rspamd_task *task = lua_check_task (L);
  1442. gint i = 1;
  1443. GList *cur;
  1444. struct rspamd_image **pimg;
  1445. if (task) {
  1446. cur = task->images;
  1447. if (cur != NULL) {
  1448. lua_newtable (L);
  1449. while (cur) {
  1450. pimg = lua_newuserdata (L, sizeof (struct rspamd_image *));
  1451. rspamd_lua_setclass (L, "rspamd{image}", -1);
  1452. *pimg = cur->data;
  1453. lua_rawseti (L, -2, i++);
  1454. cur = g_list_next (cur);
  1455. }
  1456. return 1;
  1457. }
  1458. }
  1459. lua_pushnil (L);
  1460. return 1;
  1461. }
  1462. static inline gboolean
  1463. lua_push_symbol_result (lua_State *L,
  1464. struct rspamd_task *task,
  1465. struct metric *metric,
  1466. const gchar *symbol)
  1467. {
  1468. struct metric_result *metric_res;
  1469. struct symbol *s;
  1470. gint j;
  1471. GList *opt;
  1472. metric_res = g_hash_table_lookup (task->results, metric->name);
  1473. if (metric_res) {
  1474. if ((s = g_hash_table_lookup (metric_res->symbols, symbol)) != NULL) {
  1475. j = 0;
  1476. lua_newtable (L);
  1477. lua_pushstring (L, "metric");
  1478. lua_pushstring (L, metric->name);
  1479. lua_settable (L, -3);
  1480. lua_pushstring (L, "score");
  1481. lua_pushnumber (L, s->score);
  1482. lua_settable (L, -3);
  1483. if (s->options) {
  1484. opt = s->options;
  1485. lua_pushstring (L, "options");
  1486. lua_newtable (L);
  1487. while (opt) {
  1488. lua_pushstring (L, opt->data);
  1489. lua_rawseti (L, -2, j++);
  1490. opt = g_list_next (opt);
  1491. }
  1492. lua_settable (L, -3);
  1493. }
  1494. return TRUE;
  1495. }
  1496. }
  1497. return FALSE;
  1498. }
  1499. static gint
  1500. lua_task_get_symbol (lua_State *L)
  1501. {
  1502. struct rspamd_task *task = lua_check_task (L);
  1503. const gchar *symbol;
  1504. struct metric *metric;
  1505. GList *cur = NULL, *metric_list;
  1506. gboolean found = FALSE;
  1507. gint i = 1;
  1508. symbol = luaL_checkstring (L, 2);
  1509. if (task && symbol) {
  1510. metric_list = g_hash_table_lookup (task->cfg->metrics_symbols, symbol);
  1511. if (metric_list) {
  1512. lua_newtable (L);
  1513. cur = metric_list;
  1514. }
  1515. else {
  1516. metric = task->cfg->default_metric;
  1517. }
  1518. if (!cur && metric) {
  1519. if ((found = lua_push_symbol_result (L, task, metric, symbol))) {
  1520. lua_newtable (L);
  1521. lua_rawseti (L, -2, i++);
  1522. }
  1523. }
  1524. else {
  1525. while (cur) {
  1526. metric = cur->data;
  1527. if (lua_push_symbol_result (L, task, metric, symbol)) {
  1528. lua_rawseti (L, -2, i++);
  1529. found = TRUE;
  1530. }
  1531. cur = g_list_next (cur);
  1532. }
  1533. }
  1534. }
  1535. if (!found) {
  1536. lua_pushnil (L);
  1537. }
  1538. return 1;
  1539. }
  1540. enum lua_date_type {
  1541. DATE_CONNECT = 0,
  1542. DATE_MESSAGE,
  1543. DATE_CONNECT_STRING,
  1544. DATE_MESSAGE_STRING
  1545. };
  1546. static enum lua_date_type
  1547. lua_task_detect_date_type (lua_State *L, gint idx, gboolean *gmt)
  1548. {
  1549. enum lua_date_type type = DATE_CONNECT;
  1550. if (lua_type (L, idx) == LUA_TNUMBER) {
  1551. gint num = lua_tonumber (L, idx);
  1552. if (num >= DATE_CONNECT && num <= DATE_MESSAGE_STRING) {
  1553. return num;
  1554. }
  1555. }
  1556. else if (lua_type (L, idx) == LUA_TTABLE) {
  1557. const gchar *str;
  1558. lua_pushvalue (L, idx);
  1559. lua_pushstring (L, "format");
  1560. lua_gettable (L, -2);
  1561. str = lua_tostring (L, -1);
  1562. if (g_ascii_strcasecmp (str, "message") == 0) {
  1563. type = DATE_MESSAGE;
  1564. }
  1565. else if (g_ascii_strcasecmp (str, "connect_str") == 0) {
  1566. type = DATE_CONNECT_STRING;
  1567. }
  1568. else if (g_ascii_strcasecmp (str, "message_str") == 0) {
  1569. type = DATE_MESSAGE_STRING;
  1570. }
  1571. lua_pop (L, 1);
  1572. lua_pushstring (L, "gmt");
  1573. lua_gettable (L, -2);
  1574. if (lua_type (L, -1) == LUA_TBOOLEAN) {
  1575. *gmt = lua_toboolean (L, -1);
  1576. }
  1577. /* Value and table */
  1578. lua_pop (L, 2);
  1579. }
  1580. return type;
  1581. }
  1582. static gint
  1583. lua_task_get_date (lua_State *L)
  1584. {
  1585. struct rspamd_task *task = lua_check_task (L);
  1586. gdouble tim;
  1587. enum lua_date_type type = DATE_CONNECT;
  1588. gboolean gmt = TRUE;
  1589. if (task != NULL) {
  1590. if (lua_gettop (L) > 1) {
  1591. type = lua_task_detect_date_type (L, 2, &gmt);
  1592. }
  1593. /* Get GMT date and store it to time_t */
  1594. if (type == DATE_CONNECT || type == DATE_CONNECT_STRING) {
  1595. tim = (tv_to_msec (&task->tv)) / 1000.;
  1596. if (!gmt) {
  1597. struct tm t;
  1598. time_t tt;
  1599. tt = tim;
  1600. localtime_r (&tt, &t);
  1601. t.tm_gmtoff = 0;
  1602. t.tm_isdst = 0;
  1603. tim = mktime (&t);
  1604. }
  1605. }
  1606. else {
  1607. if (task->message) {
  1608. time_t tt;
  1609. gint offset;
  1610. g_mime_message_get_date (task->message, &tt, &offset);
  1611. if (!gmt) {
  1612. tt += (offset * 60 * 60) / 100 + (offset * 60 * 60) % 100;
  1613. }
  1614. tim = tt;
  1615. }
  1616. else {
  1617. tim = 0.0;
  1618. }
  1619. }
  1620. if (type == DATE_CONNECT || type == DATE_MESSAGE) {
  1621. lua_pushnumber (L, tim);
  1622. }
  1623. else {
  1624. GTimeVal tv;
  1625. gchar *out;
  1626. double_to_tv (tim, &tv);
  1627. out = g_time_val_to_iso8601 (&tv);
  1628. lua_pushstring (L, out);
  1629. g_free (out);
  1630. }
  1631. }
  1632. else {
  1633. lua_pushnil (L);
  1634. }
  1635. return 1;
  1636. }
  1637. static gint
  1638. lua_task_get_message_id (lua_State *L)
  1639. {
  1640. struct rspamd_task *task = lua_check_task (L);
  1641. if (task != NULL && task->message_id != NULL) {
  1642. lua_pushstring (L, task->message_id);
  1643. }
  1644. else {
  1645. lua_pushnil (L);
  1646. }
  1647. return 1;
  1648. }
  1649. static gint
  1650. lua_task_get_timeval (lua_State *L)
  1651. {
  1652. struct rspamd_task *task = lua_check_task (L);
  1653. if (task != NULL) {
  1654. lua_newtable (L);
  1655. lua_pushstring (L, "tv_sec");
  1656. lua_pushnumber (L, (lua_Number)task->tv.tv_sec);
  1657. lua_settable (L, -3);
  1658. lua_pushstring (L, "tv_usec");
  1659. lua_pushnumber (L, (lua_Number)task->tv.tv_usec);
  1660. lua_settable (L, -3);
  1661. }
  1662. else {
  1663. lua_pushnil (L);
  1664. }
  1665. return 1;
  1666. }
  1667. static gint
  1668. lua_task_learn (lua_State *L)
  1669. {
  1670. struct rspamd_task *task = lua_check_task (L);
  1671. gboolean is_spam = FALSE;
  1672. const gchar *clname;
  1673. struct rspamd_classifier_config *cl;
  1674. GError *err = NULL;
  1675. int ret = 1;
  1676. is_spam = lua_toboolean(L, 2);
  1677. if (lua_gettop (L) > 2) {
  1678. clname = luaL_checkstring (L, 3);
  1679. }
  1680. else {
  1681. clname = "bayes";
  1682. }
  1683. cl = rspamd_config_find_classifier (task->cfg, clname);
  1684. if (cl == NULL) {
  1685. msg_warn ("classifier %s is not found", clname);
  1686. lua_pushboolean (L, FALSE);
  1687. lua_pushstring (L, "classifier not found");
  1688. ret = 2;
  1689. }
  1690. else {
  1691. if (!rspamd_learn_task_spam (cl, task, is_spam, &err)) {
  1692. lua_pushboolean (L, FALSE);
  1693. if (err != NULL) {
  1694. lua_pushstring (L, err->message);
  1695. ret = 2;
  1696. }
  1697. }
  1698. else {
  1699. lua_pushboolean (L, TRUE);
  1700. }
  1701. }
  1702. return ret;
  1703. }
  1704. static gint
  1705. lua_task_set_settings (lua_State *L)
  1706. {
  1707. struct rspamd_task *task = lua_check_task (L);
  1708. ucl_object_t *settings;
  1709. settings = ucl_object_lua_import (L, 2);
  1710. if (settings != NULL) {
  1711. task->settings = settings;
  1712. }
  1713. return 0;
  1714. }
  1715. static gint
  1716. lua_task_get_metric_score (lua_State *L)
  1717. {
  1718. struct rspamd_task *task = lua_check_task (L);
  1719. const gchar *metric_name;
  1720. struct metric_result *metric_res;
  1721. metric_name = luaL_checkstring (L, 2);
  1722. if (task && metric_name) {
  1723. if ((metric_res =
  1724. g_hash_table_lookup (task->results, metric_name)) != NULL) {
  1725. lua_newtable (L);
  1726. lua_pushnumber (L, metric_res->score);
  1727. lua_rawseti (L, -2, 1);
  1728. lua_pushnumber (L,
  1729. metric_res->metric->actions[METRIC_ACTION_REJECT].score);
  1730. lua_rawseti (L, -2, 2);
  1731. lua_pushnumber (L,
  1732. metric_res->metric->actions[METRIC_ACTION_REJECT].score);
  1733. lua_rawseti (L, -2, 3);
  1734. }
  1735. else {
  1736. lua_pushnil (L);
  1737. }
  1738. return 1;
  1739. }
  1740. return 0;
  1741. }
  1742. static gint
  1743. lua_task_get_metric_action (lua_State *L)
  1744. {
  1745. struct rspamd_task *task = lua_check_task (L);
  1746. const gchar *metric_name;
  1747. struct metric_result *metric_res;
  1748. enum rspamd_metric_action action;
  1749. metric_name = luaL_checkstring (L, 2);
  1750. if (task && metric_name) {
  1751. if ((metric_res =
  1752. g_hash_table_lookup (task->results, metric_name)) != NULL) {
  1753. action = rspamd_check_action_metric (task, metric_res->score,
  1754. NULL,
  1755. metric_res->metric);
  1756. lua_pushstring (L, rspamd_action_to_str (action));
  1757. }
  1758. else {
  1759. lua_pushnil (L);
  1760. }
  1761. return 1;
  1762. }
  1763. return 0;
  1764. }
  1765. /***
  1766. * @module mime_textpart
  1767. * This module provides different methods to manipulate text parts data. Text parts
  1768. * could be obtained from the `rspamd_task` by using of method `task:get_text_parts()`
  1769. @example
  1770. rspamd_config.R_EMPTY_IMAGE = function (task)
  1771. parts = task:get_text_parts()
  1772. if parts then
  1773. for _,part in ipairs(parts) do
  1774. if part:is_empty() then
  1775. images = task:get_images()
  1776. if images then
  1777. return true
  1778. end
  1779. return false
  1780. end
  1781. end
  1782. end
  1783. return false
  1784. end
  1785. */
  1786. /***
  1787. * @method text_part:get_content()
  1788. * Get the text of the part
  1789. * @return {string} `UTF8` encoded content of the part
  1790. */
  1791. static gint
  1792. lua_textpart_get_content (lua_State * L)
  1793. {
  1794. struct mime_text_part *part = lua_check_textpart (L);
  1795. if (part == NULL || part->is_empty) {
  1796. lua_pushnil (L);
  1797. return 1;
  1798. }
  1799. lua_pushlstring (L, (const gchar *)part->content->data, part->content->len);
  1800. return 1;
  1801. }
  1802. /***
  1803. * @method text_part:get_length()
  1804. * Get length of the text of the part
  1805. * @return {integer} length of part in **bytes**
  1806. */
  1807. static gint
  1808. lua_textpart_get_length (lua_State * L)
  1809. {
  1810. struct mime_text_part *part = lua_check_textpart (L);
  1811. if (part == NULL) {
  1812. lua_pushnil (L);
  1813. return 1;
  1814. }
  1815. if (part->is_empty) {
  1816. lua_pushnumber (L, 0);
  1817. }
  1818. else {
  1819. lua_pushnumber (L, part->content->len);
  1820. }
  1821. return 1;
  1822. }
  1823. /***
  1824. * @method text_part:is_empty()
  1825. * Returns `true` if the specified part is empty
  1826. * @return {bool} whether a part is empty
  1827. */
  1828. static gint
  1829. lua_textpart_is_empty (lua_State * L)
  1830. {
  1831. struct mime_text_part *part = lua_check_textpart (L);
  1832. if (part == NULL) {
  1833. lua_pushnil (L);
  1834. return 1;
  1835. }
  1836. lua_pushboolean (L, part->is_empty);
  1837. return 1;
  1838. }
  1839. /***
  1840. * @method text_part:is_html()
  1841. * Returns `true` if the specified part has HTML content
  1842. * @return {bool} whether a part is HTML part
  1843. */
  1844. static gint
  1845. lua_textpart_is_html (lua_State * L)
  1846. {
  1847. struct mime_text_part *part = lua_check_textpart (L);
  1848. if (part == NULL) {
  1849. lua_pushnil (L);
  1850. return 1;
  1851. }
  1852. lua_pushboolean (L, part->is_html);
  1853. return 1;
  1854. }
  1855. /***
  1856. * @method text_part:get_fuzzy()
  1857. * Returns base32 encoded value of fuzzy hash of the specified part
  1858. * @return {string} fuzzy hash value
  1859. */
  1860. static gint
  1861. lua_textpart_get_fuzzy (lua_State * L)
  1862. {
  1863. struct mime_text_part *part = lua_check_textpart (L);
  1864. gchar *out;
  1865. if (part == NULL || part->is_empty) {
  1866. lua_pushnil (L);
  1867. return 1;
  1868. }
  1869. out = rspamd_encode_base32 (part->fuzzy->hash_pipe,
  1870. strlen (part->fuzzy->hash_pipe));
  1871. lua_pushstring (L, out);
  1872. g_free (out);
  1873. return 1;
  1874. }
  1875. /***
  1876. * @method text_part:is_language()
  1877. * Returns the code of the most used unicode script in the text part. Does not work with raw parts
  1878. * @return {string} short abbreviation (such as `ru`) for the script's language
  1879. */
  1880. static gint
  1881. lua_textpart_get_language (lua_State * L)
  1882. {
  1883. struct mime_text_part *part = lua_check_textpart (L);
  1884. if (part != NULL) {
  1885. if (part->lang_code != NULL && part->lang_code[0] != '\0') {
  1886. lua_pushstring (L, part->lang_code);
  1887. return 1;
  1888. }
  1889. }
  1890. lua_pushnil (L);
  1891. return 1;
  1892. }
  1893. /***
  1894. * @method text_part:compare_distance(other)
  1895. * Calculates the difference to another text part. This function is intended to work with
  1896. * the parts of `multipart/alternative` container only. If the two parts are not the parts of the
  1897. * same `multipart/alternative` container, then they are considered as unrelated and
  1898. * `-1` is returned.
  1899. * @param {text_part} other text part to compare
  1900. * @return {integer} commodity percentage (e.g. the same strings give `100`, different give `0` and unrelated give `-1`)
  1901. */
  1902. static gint
  1903. lua_textpart_compare_distance (lua_State * L)
  1904. {
  1905. struct mime_text_part *part = lua_check_textpart (L), *other;
  1906. void *ud = luaL_checkudata (L, 2, "rspamd{textpart}");
  1907. gint diff = -1;
  1908. GMimeObject *parent;
  1909. const GMimeContentType *ct;
  1910. luaL_argcheck (L, ud != NULL, 2, "'textpart' expected");
  1911. other = ud ? *((struct mime_text_part **)ud) : NULL;
  1912. if (other != NULL && part->parent && part->parent == other->parent) {
  1913. parent = part->parent;
  1914. ct = g_mime_object_get_content_type (parent);
  1915. #ifndef GMIME24
  1916. if (ct == NULL ||
  1917. !g_mime_content_type_is_type (ct, "multipart", "alternative")) {
  1918. #else
  1919. if (ct == NULL ||
  1920. !g_mime_content_type_is_type ((GMimeContentType *)ct, "multipart",
  1921. "alternative")) {
  1922. #endif
  1923. diff = -1;
  1924. }
  1925. else {
  1926. if (!part->is_empty && !other->is_empty) {
  1927. if (part->diff_str != NULL && other->diff_str != NULL) {
  1928. diff = rspamd_diff_distance (part->diff_str,
  1929. other->diff_str);
  1930. }
  1931. else {
  1932. diff = rspamd_fuzzy_compare_parts (part, other);
  1933. }
  1934. }
  1935. else if ((part->is_empty &&
  1936. !other->is_empty) || (!part->is_empty && other->is_empty)) {
  1937. /* Empty and non empty parts are different */
  1938. diff = 0;
  1939. }
  1940. }
  1941. }
  1942. else {
  1943. diff = -1;
  1944. }
  1945. lua_pushinteger (L, diff);
  1946. return 1;
  1947. }
  1948. /* Mimepart implementation */
  1949. static gint
  1950. lua_mimepart_get_content (lua_State * L)
  1951. {
  1952. struct mime_part *part = lua_check_mimepart (L);
  1953. if (part == NULL) {
  1954. lua_pushnil (L);
  1955. return 1;
  1956. }
  1957. lua_pushlstring (L, (const gchar *)part->content->data, part->content->len);
  1958. return 1;
  1959. }
  1960. static gint
  1961. lua_mimepart_get_length (lua_State * L)
  1962. {
  1963. struct mime_part *part = lua_check_mimepart (L);
  1964. if (part == NULL) {
  1965. lua_pushnil (L);
  1966. return 1;
  1967. }
  1968. lua_pushinteger (L, part->content->len);
  1969. return 1;
  1970. }
  1971. static gint
  1972. lua_mimepart_get_type (lua_State * L)
  1973. {
  1974. struct mime_part *part = lua_check_mimepart (L);
  1975. if (part == NULL) {
  1976. lua_pushnil (L);
  1977. lua_pushnil (L);
  1978. return 2;
  1979. }
  1980. #ifndef GMIME24
  1981. lua_pushstring (L, part->type->type);
  1982. lua_pushstring (L, part->type->subtype);
  1983. #else
  1984. lua_pushstring (L, g_mime_content_type_get_media_type (part->type));
  1985. lua_pushstring (L, g_mime_content_type_get_media_subtype (part->type));
  1986. #endif
  1987. return 2;
  1988. }
  1989. static gint
  1990. lua_mimepart_get_filename (lua_State * L)
  1991. {
  1992. struct mime_part *part = lua_check_mimepart (L);
  1993. if (part == NULL || part->filename == NULL) {
  1994. lua_pushnil (L);
  1995. return 1;
  1996. }
  1997. lua_pushstring (L, part->filename);
  1998. return 1;
  1999. }
  2000. static gint
  2001. lua_mimepart_get_header_common (lua_State *L, gboolean full, gboolean raw)
  2002. {
  2003. gboolean strong = FALSE;
  2004. struct mime_part *part = lua_check_mimepart (L);
  2005. const gchar *name;
  2006. name = luaL_checkstring (L, 2);
  2007. if (name && part) {
  2008. if (lua_gettop (L) == 3) {
  2009. strong = lua_toboolean (L, 3);
  2010. }
  2011. return lua_push_header (L, part->raw_headers, name, strong, full, raw);
  2012. }
  2013. lua_pushnil (L);
  2014. return 1;
  2015. }
  2016. static gint
  2017. lua_mimepart_get_header_full (lua_State * L)
  2018. {
  2019. return lua_mimepart_get_header_common (L, TRUE, TRUE);
  2020. }
  2021. static gint
  2022. lua_mimepart_get_header (lua_State * L)
  2023. {
  2024. return lua_mimepart_get_header_common (L, FALSE, FALSE);
  2025. }
  2026. static gint
  2027. lua_mimepart_get_header_raw (lua_State * L)
  2028. {
  2029. return lua_mimepart_get_header_common (L, FALSE, TRUE);
  2030. }
  2031. /* Image functions */
  2032. static gint
  2033. lua_image_get_width (lua_State *L)
  2034. {
  2035. struct rspamd_image *img = lua_check_image (L);
  2036. if (img != NULL) {
  2037. lua_pushnumber (L, img->width);
  2038. }
  2039. else {
  2040. lua_pushnumber (L, 0);
  2041. }
  2042. return 1;
  2043. }
  2044. static gint
  2045. lua_image_get_height (lua_State *L)
  2046. {
  2047. struct rspamd_image *img = lua_check_image (L);
  2048. if (img != NULL) {
  2049. lua_pushnumber (L, img->height);
  2050. }
  2051. else {
  2052. lua_pushnumber (L, 0);
  2053. }
  2054. return 1;
  2055. }
  2056. static gint
  2057. lua_image_get_type (lua_State *L)
  2058. {
  2059. struct rspamd_image *img = lua_check_image (L);
  2060. if (img != NULL) {
  2061. lua_pushstring (L, image_type_str (img->type));
  2062. }
  2063. else {
  2064. lua_pushnil (L);
  2065. }
  2066. return 1;
  2067. }
  2068. static gint
  2069. lua_image_get_size (lua_State *L)
  2070. {
  2071. struct rspamd_image *img = lua_check_image (L);
  2072. if (img != NULL) {
  2073. lua_pushinteger (L, img->data->len);
  2074. }
  2075. else {
  2076. lua_pushnil (L);
  2077. }
  2078. return 1;
  2079. }
  2080. static gint
  2081. lua_image_get_filename (lua_State *L)
  2082. {
  2083. struct rspamd_image *img = lua_check_image (L);
  2084. if (img != NULL && img->filename != NULL) {
  2085. lua_pushstring (L, img->filename);
  2086. }
  2087. else {
  2088. lua_pushnil (L);
  2089. }
  2090. return 1;
  2091. }
  2092. /* URL part */
  2093. static gint
  2094. lua_url_get_length (lua_State *L)
  2095. {
  2096. struct uri *url = lua_check_url (L);
  2097. if (url != NULL) {
  2098. lua_pushinteger (L, strlen (struri (url)));
  2099. }
  2100. else {
  2101. lua_pushnil (L);
  2102. }
  2103. return 1;
  2104. }
  2105. static gint
  2106. lua_url_get_host (lua_State *L)
  2107. {
  2108. struct uri *url = lua_check_url (L);
  2109. if (url != NULL) {
  2110. lua_pushlstring (L, url->host, url->hostlen);
  2111. }
  2112. else {
  2113. lua_pushnil (L);
  2114. }
  2115. return 1;
  2116. }
  2117. static gint
  2118. lua_url_get_user (lua_State *L)
  2119. {
  2120. struct uri *url = lua_check_url (L);
  2121. if (url != NULL && url->user != NULL) {
  2122. lua_pushlstring (L, url->user, url->userlen);
  2123. }
  2124. else {
  2125. lua_pushnil (L);
  2126. }
  2127. return 1;
  2128. }
  2129. static gint
  2130. lua_url_get_path (lua_State *L)
  2131. {
  2132. struct uri *url = lua_check_url (L);
  2133. if (url != NULL) {
  2134. lua_pushlstring (L, url->data, url->datalen);
  2135. }
  2136. else {
  2137. lua_pushnil (L);
  2138. }
  2139. return 1;
  2140. }
  2141. static gint
  2142. lua_url_get_text (lua_State *L)
  2143. {
  2144. struct uri *url = lua_check_url (L);
  2145. if (url != NULL) {
  2146. lua_pushstring (L, struri (url));
  2147. }
  2148. else {
  2149. lua_pushnil (L);
  2150. }
  2151. return 1;
  2152. }
  2153. static gint
  2154. lua_url_is_phished (lua_State *L)
  2155. {
  2156. struct uri *url = lua_check_url (L);
  2157. if (url != NULL) {
  2158. lua_pushboolean (L, url->is_phished);
  2159. }
  2160. else {
  2161. lua_pushnil (L);
  2162. }
  2163. return 1;
  2164. }
  2165. static gint
  2166. lua_url_get_phished (lua_State *L)
  2167. {
  2168. struct uri **purl, *url = lua_check_url (L);
  2169. if (url) {
  2170. if (url->is_phished && url->phished_url != NULL) {
  2171. purl = lua_newuserdata (L, sizeof (struct uri *));
  2172. rspamd_lua_setclass (L, "rspamd{url}", -1);
  2173. *purl = url->phished_url;
  2174. return 1;
  2175. }
  2176. }
  2177. lua_pushnil (L);
  2178. return 1;
  2179. }
  2180. /* Init part */
  2181. static gint
  2182. lua_load_task (lua_State * L)
  2183. {
  2184. lua_newtable (L);
  2185. luaL_register (L, NULL, tasklib_f);
  2186. return 1;
  2187. }
  2188. void
  2189. luaopen_task (lua_State * L)
  2190. {
  2191. rspamd_lua_new_class (L, "rspamd{task}", tasklib_m);
  2192. lua_pop (L, 1); /* remove metatable from stack */
  2193. rspamd_lua_add_preload (L, "rspamd_task", lua_load_task);
  2194. }
  2195. void
  2196. luaopen_textpart (lua_State * L)
  2197. {
  2198. rspamd_lua_new_class (L, "rspamd{textpart}", textpartlib_m);
  2199. luaL_register (L, "rspamd_textpart", null_reg);
  2200. lua_pop (L, 1); /* remove metatable from stack */
  2201. }
  2202. void
  2203. luaopen_mimepart (lua_State * L)
  2204. {
  2205. rspamd_lua_new_class (L, "rspamd{mimepart}", mimepartlib_m);
  2206. lua_pop (L, 1); /* remove metatable from stack */
  2207. }
  2208. void
  2209. luaopen_image (lua_State * L)
  2210. {
  2211. rspamd_lua_new_class (L, "rspamd{image}", imagelib_m);
  2212. lua_pop (L, 1); /* remove metatable from stack */
  2213. }
  2214. void
  2215. luaopen_url (lua_State * L)
  2216. {
  2217. rspamd_lua_new_class (L, "rspamd{url}", urllib_m);
  2218. lua_pop (L, 1); /* remove metatable from stack */
  2219. }