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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "lua_common.h"
  17. #include "message.h"
  18. #include "images.h"
  19. #include "archives.h"
  20. #include "utlist.h"
  21. #include "unix-std.h"
  22. #include "libmime/smtp_parsers.h"
  23. #include "libserver/mempool_vars_internal.h"
  24. #include <math.h>
  25. /***
  26. * @module rspamd_task
  27. * This module provides routines for tasks manipulation in rspamd. Tasks usually
  28. * represent messages being scanned, and this API provides access to such elements
  29. * as headers, symbols, metrics and so on and so forth. Normally, task objects
  30. * are passed to the lua callbacks allowing to check specific properties of messages
  31. * and add the corresponding symbols to the scan's results.
  32. @example
  33. rspamd_config.DATE_IN_PAST = function(task)
  34. local dm = task:get_date{format = 'message', gmt = true}
  35. local dt = task:get_date{format = 'connect', gmt = true}
  36. -- A day
  37. if dt - dm > 86400 then
  38. return true
  39. end
  40. return false
  41. end
  42. */
  43. /* Task methods */
  44. LUA_FUNCTION_DEF (task, get_message);
  45. LUA_FUNCTION_DEF (task, process_message);
  46. /***
  47. * @method task:get_cfg()
  48. * Get configuration object for a task.
  49. * @return {rspamd_config} (config.md)[configuration object] for the task
  50. */
  51. LUA_FUNCTION_DEF (task, get_cfg);
  52. LUA_FUNCTION_DEF (task, set_cfg);
  53. LUA_FUNCTION_DEF (task, destroy);
  54. /***
  55. * @method task:get_mempool()
  56. * Returns memory pool valid for a lifetime of task. It is used internally by
  57. * many rspamd routines.
  58. * @return {rspamd_mempool} memory pool object
  59. */
  60. LUA_FUNCTION_DEF (task, get_mempool);
  61. /***
  62. * @method task:get_session()
  63. * Returns asynchronous session object that is used by many rspamd asynchronous
  64. * utilities internally.
  65. * @return {rspamd_session} session object
  66. */
  67. LUA_FUNCTION_DEF (task, get_session);
  68. /***
  69. * @method task:get_ev_base()
  70. * Return asynchronous event base for using in callbacks and resolver.
  71. * @return {rspamd_ev_base} event base
  72. */
  73. LUA_FUNCTION_DEF (task, get_ev_base);
  74. /***
  75. * @method task:insert_result(symbol, weigth[, option1, ...])
  76. * Insert specific symbol to the tasks scanning results assigning the initial
  77. * weight to it.
  78. * @param {string} symbol symbol to insert
  79. * @param {number} weight initial weight (this weight is multiplied by the metric weight)
  80. * @param {string} options list of optional options attached to a symbol inserted
  81. @example
  82. local function cb(task)
  83. if task:get_header('Some header') then
  84. task:insert_result('SOME_HEADER', 1.0, 'Got some header')
  85. end
  86. end
  87. */
  88. LUA_FUNCTION_DEF (task, insert_result);
  89. /***
  90. * @method task:set_pre_result(action, description)
  91. * Sets pre-result for a task. It is used in pre-filters to specify early result
  92. * of the task scanned. If a pre-filter sets some result, then further processing
  93. * may be skipped. For selecting action it is possible to use global table
  94. * `rspamd_actions` or a string value:
  95. *
  96. * - `reject`: reject message permanently
  97. * - `add header`: add spam header
  98. * - `rewrite subject`: rewrite subject to spam subject
  99. * - `greylist`: greylist message
  100. * - `accept` or `no action`: whitelist message
  101. * @param {rspamd_action or string} action a numeric or string action value
  102. * @param {string} description optional descripton
  103. @example
  104. local function cb(task)
  105. local gr = task:get_header('Greylist')
  106. if gr and gr == 'greylist' then
  107. task:set_pre_result(rspamd_actions['greylist'], 'Greylisting required')
  108. end
  109. end
  110. */
  111. LUA_FUNCTION_DEF (task, set_pre_result);
  112. /***
  113. * @method task:append_message(message)
  114. * Adds a message to scanning output.
  115. * @param {string} message
  116. @example
  117. local function cb(task)
  118. task:append_message('Example message')
  119. end
  120. */
  121. LUA_FUNCTION_DEF (task, append_message);
  122. /***
  123. * @method task:get_urls([need_emails])
  124. * Get all URLs found in a message.
  125. * @param {boolean} need_emails if `true` then reutrn also email urls
  126. * @return {table rspamd_url} list of all urls found
  127. @example
  128. local function phishing_cb(task)
  129. local urls = task:get_urls();
  130. if urls then
  131. for _,url in ipairs(urls) do
  132. if url:is_phished() then
  133. return true
  134. end
  135. end
  136. end
  137. return false
  138. end
  139. */
  140. LUA_FUNCTION_DEF (task, get_urls);
  141. /***
  142. * @method task:has_urls([need_emails])
  143. * Returns 'true' if a task has urls listed
  144. * @param {boolean} need_emails if `true` then reutrn also email urls
  145. * @return {boolean} true if a task has urls (urls or emails if `need_emails` is true)
  146. */
  147. LUA_FUNCTION_DEF (task, has_urls);
  148. /***
  149. * @method task:get_content()
  150. * Get raw content for the specified task
  151. * @return {text} the data contained in the task
  152. */
  153. LUA_FUNCTION_DEF (task, get_content);
  154. /***
  155. * @method task:get_rawbody()
  156. * Get raw body for the specified task
  157. * @return {text} the data contained in the task
  158. */
  159. LUA_FUNCTION_DEF (task, get_rawbody);
  160. /***
  161. * @method task:get_emails()
  162. * Get all email addresses found in a message.
  163. * @return {table rspamd_url} list of all email addresses found
  164. */
  165. LUA_FUNCTION_DEF (task, get_emails);
  166. /***
  167. * @method task:get_text_parts()
  168. * Get all text (and HTML) parts found in a message
  169. * @return {table rspamd_text_part} list of text parts
  170. */
  171. LUA_FUNCTION_DEF (task, get_text_parts);
  172. /***
  173. * @method task:get_parts()
  174. * Get all mime parts found in a message
  175. * @return {table rspamd_mime_part} list of mime parts
  176. */
  177. LUA_FUNCTION_DEF (task, get_parts);
  178. /***
  179. * @method task:get_request_header(name)
  180. * Get value of a HTTP request header.
  181. * @param {string} name name of header to get
  182. * @return {rspamd_text} value of an HTTP header
  183. */
  184. LUA_FUNCTION_DEF (task, get_request_header);
  185. /***
  186. * @method task:set_request_header(name, value)
  187. * Set value of a HTTP request header. If value is omitted, then a header is removed
  188. * @param {string} name name of header to get
  189. * @param {rspamd_text/string} value new header's value
  190. */
  191. LUA_FUNCTION_DEF (task, set_request_header);
  192. /***
  193. * @method task:get_subject()
  194. * Returns task subject (either from the protocol override or from a header)
  195. * @return {string} value of a subject (decoded)
  196. */
  197. LUA_FUNCTION_DEF (task, get_subject);
  198. /***
  199. * @method task:get_header(name[, case_sensitive])
  200. * Get decoded value of a header specified with optional case_sensitive flag.
  201. * By default headers are searched in caseless matter.
  202. * @param {string} name name of header to get
  203. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  204. * @return {string} decoded value of a header
  205. */
  206. LUA_FUNCTION_DEF (task, get_header);
  207. /***
  208. * @method task:get_header_raw(name[, case_sensitive])
  209. * Get raw value of a header specified with optional case_sensitive flag.
  210. * By default headers are searched in caseless matter.
  211. * @param {string} name name of header to get
  212. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  213. * @return {string} raw value of a header
  214. */
  215. LUA_FUNCTION_DEF (task, get_header_raw);
  216. /***
  217. * @method task:get_header_full(name[, case_sensitive])
  218. * Get raw value of a header specified with optional case_sensitive flag.
  219. * By default headers are searched in caseless matter. This method returns more
  220. * information about the header as a list of tables with the following structure:
  221. *
  222. * - `name` - name of a header
  223. * - `value` - raw value of a header
  224. * - `decoded` - decoded value of a header
  225. * - `tab_separated` - `true` if a header and a value are separated by `tab` character
  226. * - `empty_separator` - `true` if there are no separator between a header and a value
  227. * @param {string} name name of header to get
  228. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  229. * @return {list of tables} all values of a header as specified above
  230. @example
  231. function check_header_delimiter_tab(task, header_name)
  232. for _,rh in ipairs(task:get_header_full(header_name)) do
  233. if rh['tab_separated'] then return true end
  234. end
  235. return false
  236. end
  237. */
  238. LUA_FUNCTION_DEF (task, get_header_full);
  239. /***
  240. * @method task:get_raw_headers()
  241. * Get all undecoded headers of a message as a string
  242. * @return {rspamd_text} all raw headers for a message as opaque text
  243. */
  244. LUA_FUNCTION_DEF (task, get_raw_headers);
  245. /***
  246. * @method task:get_received_headers()
  247. * Returns a list of tables of parsed received headers. A tables returned have
  248. * the following structure:
  249. *
  250. * - `from_hostname` - string that represents hostname provided by a peer
  251. * - `from_ip` - string representation of IP address as provided by a peer
  252. * - `real_hostname` - hostname as resolved by MTA
  253. * - `real_ip` - string representation of IP as resolved by PTR request of MTA
  254. * - `by_hostname` - MTA hostname
  255. * - `proto` - protocol, e.g. ESMTP or ESMTPS
  256. * - `timestamp` - received timetamp
  257. * - `for` - for value (unparsed mailbox)
  258. *
  259. * Please note that in some situations rspamd cannot parse all the fields of received headers.
  260. * In that case you should check all strings for validity.
  261. * @return {table of tables} list of received headers described above
  262. */
  263. LUA_FUNCTION_DEF (task, get_received_headers);
  264. /***
  265. * @method task:get_queue_id()
  266. * Returns queue ID of the message being processed.
  267. */
  268. LUA_FUNCTION_DEF (task, get_queue_id);
  269. /***
  270. * @method task:get_uid()
  271. * Returns ID of the task being processed.
  272. */
  273. LUA_FUNCTION_DEF (task, get_uid);
  274. /***
  275. * @method task:get_resolver()
  276. * Returns ready to use rspamd_resolver object suitable for making asynchronous DNS requests.
  277. * @return {rspamd_resolver} resolver object associated with the task's session
  278. * @example
  279. local logger = require "rspamd_logger"
  280. local function task_cb(task)
  281. local function dns_cb(resolver, to_resolve, results, err)
  282. -- task object is available due to closure
  283. task:inc_dns_req()
  284. if results then
  285. logger.info(string.format('<%s> [%s] resolved for symbol: %s',
  286. task:get_message_id(), to_resolve, 'EXAMPLE_SYMBOL'))
  287. task:insert_result('EXAMPLE_SYMBOL', 1)
  288. end
  289. end
  290. local r = task:get_resolver()
  291. r:resolve_a(task:get_session(), task:get_mempool(), 'example.com', dns_cb)
  292. end
  293. */
  294. LUA_FUNCTION_DEF (task, get_resolver);
  295. /***
  296. * @method task:inc_dns_req()
  297. * Increment number of DNS requests for the task. Is used just for logging purposes.
  298. */
  299. LUA_FUNCTION_DEF (task, inc_dns_req);
  300. /***
  301. * @method task:get_dns_req()
  302. * Get number of dns requests being sent in the task
  303. * @return {number} number of DNS requests
  304. */
  305. LUA_FUNCTION_DEF (task, get_dns_req);
  306. /***
  307. * @method task:has_recipients([type])
  308. * Return true if there are SMTP or MIME recipients for a task.
  309. * @param {integer|string} type if specified has the following meaning: `0` or `any` means try SMTP recipients and fallback to MIME if failed, `1` or `smtp` means checking merely SMTP recipients and `2` or `mime` means MIME recipients only
  310. * @return {bool} `true` if there are recipients of the following type
  311. */
  312. LUA_FUNCTION_DEF (task, has_recipients);
  313. /***
  314. * @method task:get_recipients([type])
  315. * Return SMTP or MIME recipients for a task. This function returns list of internet addresses each one is a table with the following structure:
  316. *
  317. * - `name` - name of internet address in UTF8, e.g. for `Vsevolod Stakhov <blah@foo.com>` it returns `Vsevolod Stakhov`
  318. * - `addr` - address part of the address
  319. * - `user` - user part (if present) of the address, e.g. `blah`
  320. * - `domain` - domain part (if present), e.g. `foo.com`
  321. * @param {integer|string} type if specified has the following meaning: `0` or `any` means try SMTP recipients and fallback to MIME if failed, `1` or `smtp` means checking merely SMTP recipients and `2` or `mime` means MIME recipients only
  322. * @return {list of addresses} list of recipients or `nil`
  323. */
  324. LUA_FUNCTION_DEF (task, get_recipients);
  325. /***
  326. * @method task:set_recipients([type], {rcpt1, rcpt2...})
  327. * Sets sender for a task. This function accepts table that will be converted to the address.
  328. * If some fields are missing they are subsequently reconstructed by this function. E.g. if you
  329. * specify 'user' and 'domain', then address and raw string will be reconstructed
  330. *
  331. * - `name` - name of internet address in UTF8, e.g. for `Vsevolod Stakhov <blah@foo.com>` it returns `Vsevolod Stakhov`
  332. * - `addr` - address part of the address
  333. * - `user` - user part (if present) of the address, e.g. `blah`
  334. * - `domain` - domain part (if present), e.g. `foo.com`
  335. * @param {integer|string} type if specified has the following meaning: `0` or `any` means try SMTP recipients and fallback to MIME if failed, `1` or `smtp` means checking merely SMTP recipients and `2` or `mime` means MIME recipients only
  336. * @param {list of tables} recipients recipients to set
  337. * @return {boolean} result of the operation
  338. */
  339. LUA_FUNCTION_DEF (task, set_recipients);
  340. /***
  341. * @method task:has_from([type])
  342. * Return true if there is SMTP or MIME sender for a task.
  343. * @param {integer|string} type if specified has the following meaning: `0` or `any` means try SMTP recipients and fallback to MIME if failed, `1` or `smtp` means checking merely SMTP recipients and `2` or `mime` means MIME recipients only
  344. * @return {bool} `true` if there is sender of the following type
  345. */
  346. LUA_FUNCTION_DEF (task, has_from);
  347. /***
  348. * @method task:get_from([type])
  349. * Return SMTP or MIME sender for a task. This function returns an internet address which one is a table with the following structure:
  350. *
  351. * - `name` - name of internet address in UTF8, e.g. for `Vsevolod Stakhov <blah@foo.com>` it returns `Vsevolod Stakhov`
  352. * - `addr` - address part of the address
  353. * - `user` - user part (if present) of the address, e.g. `blah`
  354. * - `domain` - domain part (if present), e.g. `foo.com`
  355. * @param {integer|string} type if specified has the following meaning: `0` or `any` means try SMTP sender and fallback to MIME if failed, `1` or `smtp` means checking merely SMTP sender and `2` or `mime` means MIME `From:` only
  356. * @return {address} sender or `nil`
  357. */
  358. LUA_FUNCTION_DEF (task, get_from);
  359. /***
  360. * @method task:set_from(type, addr)
  361. * Sets sender for a task. This function accepts table that will be converted to the address.
  362. * If some fields are missing they are subsequently reconstructed by this function. E.g. if you
  363. * specify 'user' and 'domain', then address and raw string will be reconstructed
  364. *
  365. * - `name` - name of internet address in UTF8, e.g. for `Vsevolod Stakhov <blah@foo.com>` it returns `Vsevolod Stakhov`
  366. * - `addr` - address part of the address
  367. * - `user` - user part (if present) of the address, e.g. `blah`
  368. * - `domain` - domain part (if present), e.g. `foo.com`
  369. * @param {integer|string} type if specified has the following meaning: `0` or `any` means try SMTP sender and fallback to MIME if failed, `1` or `smtp` means checking merely SMTP sender and `2` or `mime` means MIME `From:` only
  370. * @param {table
  371. * @return {boolean} success or not
  372. */
  373. LUA_FUNCTION_DEF (task, set_from);
  374. /***
  375. * @method task:get_user()
  376. * Returns authenticated user name for this task if specified by an MTA.
  377. * @return {string} username or nil
  378. */
  379. LUA_FUNCTION_DEF (task, get_user);
  380. LUA_FUNCTION_DEF (task, set_user);
  381. /***
  382. * @method task:get_from_ip()
  383. * Returns [ip_addr](ip.md) object of a sender that is provided by MTA
  384. * @return {rspamd_ip} ip address object
  385. */
  386. LUA_FUNCTION_DEF (task, get_from_ip);
  387. /***
  388. * @method task:set_from_ip(str)
  389. * Set tasks's IP address based on the passed string
  390. * @param {string} str string representation of ip
  391. */
  392. LUA_FUNCTION_DEF (task, set_from_ip);
  393. LUA_FUNCTION_DEF (task, get_from_ip_num);
  394. /***
  395. * @method task:get_client_ip()
  396. * Returns [ip_addr](ip.md) object of a client connected to rspamd (normally, it is an IP address of MTA)
  397. * @return {rspamd_ip} ip address object
  398. */
  399. LUA_FUNCTION_DEF (task, get_client_ip);
  400. /***
  401. * @method task:get_helo()
  402. * Returns the value of SMTP helo provided by MTA.
  403. * @return {string} HELO value
  404. */
  405. LUA_FUNCTION_DEF (task, get_helo);
  406. LUA_FUNCTION_DEF (task, set_helo);
  407. /***
  408. * @method task:get_hostname()
  409. * Returns the value of sender's hostname provided by MTA
  410. * @return {string} hostname value
  411. */
  412. LUA_FUNCTION_DEF (task, get_hostname);
  413. LUA_FUNCTION_DEF (task, set_hostname);
  414. /***
  415. * @method task:get_images()
  416. * Returns list of all images found in a task as a table of `rspamd_image`.
  417. * Each image has the following methods:
  418. *
  419. * * `get_width` - return width of an image in pixels
  420. * * `get_height` - return height of an image in pixels
  421. * * `get_type` - return string representation of image's type (e.g. 'jpeg')
  422. * * `get_filename` - return string with image's file name
  423. * * `get_size` - return size in bytes
  424. * @return {list of rspamd_image} images found in a message
  425. */
  426. LUA_FUNCTION_DEF (task, get_images);
  427. /***
  428. * @method task:get_archives()
  429. * Returns list of all archives found in a task as a table of `rspamd_archive`.
  430. * Each archive has the following methods available:
  431. *
  432. * * `get_files` - return list of strings with filenames inside archive
  433. * * `get_files_full` - return list of tables with all information about files
  434. * * `is_encrypted` - return true if an archive is encrypted
  435. * * `get_type` - return string representation of image's type (e.g. 'zip')
  436. * * `get_filename` - return string with archive's file name
  437. * * `get_size` - return size in bytes
  438. * @return {list of rspamd_archive} archives found in a message
  439. */
  440. LUA_FUNCTION_DEF (task, get_archives);
  441. /***
  442. * @method task:get_symbol(name)
  443. * Searches for a symbol `name` in all metrics results and returns a list of tables
  444. * one per metric that describes the symbol inserted. Please note that this function
  445. * is intended to return values for **inserted** symbols, so if this symbol was not
  446. * inserted it won't be in the function's output. This method is useful for post-filters mainly.
  447. * The symbols are returned as the list of the following tables:
  448. *
  449. * - `metric` - name of metric
  450. * - `score` - score of a symbol in that metric
  451. * - `options` - a table of strings representing options of a symbol
  452. * - `group` - a group of symbol (or 'ungrouped')
  453. * @param {string} name symbol's name
  454. * @return {list of tables} list of tables or nil if symbol was not found in any metric
  455. */
  456. LUA_FUNCTION_DEF (task, get_symbol);
  457. /***
  458. * @method task:get_symbols_all()
  459. * Returns array of symbols matched in default metric with all metadata
  460. * @return {table} table of tables formatted as in `task:get_symbol()` except that `metric` is absent and `name` is added
  461. */
  462. LUA_FUNCTION_DEF (task, get_symbols_all);
  463. /***
  464. * @method task:get_symbols()
  465. * Returns array of all symbols matched for this task
  466. * @return {table, table} table of strings with symbols names + table of theirs scores
  467. */
  468. LUA_FUNCTION_DEF (task, get_symbols);
  469. /***
  470. * @method task:get_symbols_numeric()
  471. * Returns array of all symbols matched for this task
  472. * @return {table|number, table|number} table of numbers with symbols ids + table of theirs scores
  473. */
  474. LUA_FUNCTION_DEF (task, get_symbols_numeric);
  475. /***
  476. * @method task:has_symbol(name)
  477. * Fast path to check if a specified symbol is in the task's results
  478. * @param {string} name symbol's name
  479. * @return {boolean} `true` if symbol has been found
  480. */
  481. LUA_FUNCTION_DEF (task, has_symbol);
  482. /***
  483. * @method task:get_date(type[, gmt])
  484. * Returns timestamp for a connection or for a MIME message. This function can be called with a
  485. * single table arguments with the following fields:
  486. *
  487. * * `format` - a format of date returned:
  488. * - `message` - returns a mime date as integer (unix timestamp)
  489. * - `message_str` - returns a mime date as string (UTC format)
  490. * - `connect` - returns a unix timestamp of a connection to rspamd
  491. * - `connect_str` - returns connection time in UTC format
  492. * * `gmt` - returns date in `GMT` timezone (normal for unix timestamps)
  493. *
  494. * By default this function returns connection time in numeric format.
  495. * @param {string} type date format as described above
  496. * @param {boolean} gmt gmt flag as described above
  497. * @return {string/number} date representation according to format
  498. * @example
  499. rspamd_config.DATE_IN_PAST = function(task)
  500. local dm = task:get_date{format = 'message', gmt = true}
  501. local dt = task:get_date{format = 'connect', gmt = true}
  502. -- A day
  503. if dt - dm > 86400 then
  504. return true
  505. end
  506. return false
  507. end
  508. */
  509. LUA_FUNCTION_DEF (task, get_date);
  510. /***
  511. * @method task:get_message_id()
  512. * Returns message id of the specified task
  513. * @return {string} if of a message
  514. */
  515. LUA_FUNCTION_DEF (task, get_message_id);
  516. LUA_FUNCTION_DEF (task, get_timeval);
  517. /***
  518. * @method task:get_metric_score(name)
  519. * Get the current score of metric `name`. Should be used in post-filters only.
  520. * @param {string} name name of a metric
  521. * @return {table} table containing the current score and required score of the metric
  522. */
  523. LUA_FUNCTION_DEF (task, get_metric_score);
  524. /***
  525. * @method task:get_metric_action(name)
  526. * Get the current action of metric `name`. Should be used in post-filters only.
  527. * @param {string} name name of a metric
  528. * @return {string} the current action of the metric as a string
  529. */
  530. LUA_FUNCTION_DEF (task, get_metric_action);
  531. /***
  532. * @method task:set_metric_score(name, score)
  533. * Set the current score of metric `name`. Should be used in post-filters only.
  534. * @param {string} name name of a metric
  535. * @param {number} score the current score of the metric
  536. */
  537. LUA_FUNCTION_DEF (task, set_metric_score);
  538. /***
  539. * @method task:set_metric_action(name, action)
  540. * Set the current action of metric `name`. Should be used in post-filters only.
  541. * @param {string} name name of a metric
  542. * @param {string} action name to set
  543. */
  544. LUA_FUNCTION_DEF (task, set_metric_action);
  545. /***
  546. * @method task:set_metric_subject(subject)
  547. * Set the subject in the default metric
  548. * @param {string} subject subject to set
  549. */
  550. LUA_FUNCTION_DEF (task, set_metric_subject);
  551. /***
  552. * @method task:learn(is_spam[, classifier)
  553. * Learn classifier `classifier` with the task. If `is_spam` is true then message
  554. * is learnt as spam. Otherwise HAM is learnt. By default, this function learns
  555. * `bayes` classifier.
  556. * @param {boolean} is_spam learn spam or ham
  557. * @param {string} classifier classifier's name
  558. * @return {boolean} `true` if classifier has been learnt successfully
  559. */
  560. LUA_FUNCTION_DEF (task, learn);
  561. /***
  562. * @method task:set_settings(obj)
  563. * Set users settings object for a task. The format of this object is described
  564. * [here](https://rspamd.com/doc/configuration/settings.html).
  565. * @param {any} obj any lua object that corresponds to the settings format
  566. */
  567. LUA_FUNCTION_DEF (task, set_settings);
  568. /***
  569. * @method task:get_settings()
  570. * Gets users settings object for a task. The format of this object is described
  571. * [here](https://rspamd.com/doc/configuration/settings.html).
  572. * @return {lua object} lua object generated from UCL
  573. */
  574. LUA_FUNCTION_DEF (task, get_settings);
  575. /***
  576. * @method task:lookup_settings(key)
  577. * Gets users settings object with the specified key for a task.
  578. * @param {string} key key to lookup
  579. * @return {lua object} lua object generated from UCL
  580. */
  581. LUA_FUNCTION_DEF (task, lookup_settings);
  582. /***
  583. * @method task:get_settings_id()
  584. * Get numeric hash of settings id if specified for this task. 0 is returned otherwise.
  585. * @return {number} settings-id hash
  586. */
  587. LUA_FUNCTION_DEF (task, get_settings_id);
  588. /***
  589. * @method task:set_rmilter_reply(obj)
  590. * Set special reply for rmilter
  591. * @param {any} obj any lua object that corresponds to the settings format
  592. * @example
  593. task:set_rmilter_reply({
  594. add_headers = {['X-Lua'] = 'test'},
  595. -- 1 is the position of header to remove
  596. remove_headers = {['DKIM-Signature'] = 1},
  597. })
  598. */
  599. LUA_FUNCTION_DEF (task, set_rmilter_reply);
  600. /***
  601. * @method task:process_re(params)
  602. * Processes the specified regexp and returns number of captures (cached or new)
  603. * Params is the table with the follwoing fields (mandatory fields are marked with `*`):
  604. * - `re`* : regular expression object
  605. * - `type`*: type of regular expression:
  606. * + `mime`: mime regexp
  607. * + `header`: header regexp
  608. * + `rawheader`: raw header expression
  609. * + `rawmime`: raw mime regexp
  610. * + `body`: raw body regexp
  611. * + `url`: url regexp
  612. * - `header`: for header and rawheader regexp means the name of header
  613. * - `strong`: case sensitive match for headers
  614. * @return {number} number of regexp occurences in the task (limited by 255 so far)
  615. */
  616. LUA_FUNCTION_DEF (task, process_regexp);
  617. /***
  618. * @method task:cache_set(key, value)
  619. * Store some value to the task cache
  620. * @param {string} key key to use
  621. * @param {any} value any value (including functions and tables)
  622. */
  623. LUA_FUNCTION_DEF (task, cache_set);
  624. /***
  625. * @method task:cache_get(key)
  626. * Returns cached value or nil if nothing is cached
  627. * @param {string} key key to use
  628. * @return {any} cached value
  629. */
  630. LUA_FUNCTION_DEF (task, cache_get);
  631. /***
  632. * @method task:get_size()
  633. * Returns size of the task in bytes (that includes headers + parts size)
  634. * @return {number} size in bytes
  635. */
  636. LUA_FUNCTION_DEF (task, get_size);
  637. /***
  638. * @method task:set_flag(flag_name[, set])
  639. * Set specific flag for task:
  640. *
  641. * - `no_log`: do not log task summary
  642. * - `no_stat`: do not include task into scanned stats
  643. * - `pass_all`: check all filters for task
  644. * - `extended_urls`: output extended info about urls
  645. * - `skip`: skip task processing
  646. * - `learn_spam`: learn message as spam
  647. * - `learn_ham`: learn message as ham
  648. * - `broken_headers`: header data is broken for a message
  649. * @param {string} flag to set
  650. * @param {boolean} set set or clear flag (default is set)
  651. @example
  652. --[[
  653. For messages with undefined queue ID (scanned with rspamc or WebUI)
  654. do not include results into statistics and do not log task summary
  655. (it will not appear in the WebUI history as well).
  656. ]]--
  657. -- Callback function to set flags
  658. local function no_log_stat_cb(task)
  659. if not task:get_queue_id() then
  660. task:set_flag('no_log')
  661. task:set_flag('no_stat')
  662. end
  663. end
  664. rspamd_config:register_symbol({
  665. name = 'LOCAL_NO_LOG_STAT',
  666. type = 'postfilter',
  667. callback = no_log_stat_cb
  668. })
  669. */
  670. LUA_FUNCTION_DEF (task, set_flag);
  671. /***
  672. * @method task:has_flag(flag_name)
  673. * Checks for a specific flag in task:
  674. *
  675. * - `no_log`: do not log task summary
  676. * - `no_stat`: do not include task into scanned stats
  677. * - `pass_all`: check all filters for task
  678. * - `extended_urls`: output extended info about urls
  679. * - `skip`: skip task processing
  680. * - `learn_spam`: learn message as spam
  681. * - `learn_ham`: learn message as ham
  682. * - `broken_headers`: header data is broken for a message
  683. * @param {string} flag to check
  684. * @return {boolean} true if flags is set
  685. */
  686. LUA_FUNCTION_DEF (task, has_flag);
  687. /***
  688. * @method task:get_flags()
  689. * Get list of flags for task:
  690. *
  691. * - `no_log`: do not log task summary
  692. * - `no_stat`: do not include task into scanned stats
  693. * - `pass_all`: check all filters for task
  694. * - `extended_urls`: output extended info about urls
  695. * - `skip`: skip task processing
  696. * - `learn_spam`: learn message as spam
  697. * - `learn_ham`: learn message as ham
  698. * - `broken_headers`: header data is broken for a message
  699. * @return {array of strings} table with all flags as strings
  700. */
  701. LUA_FUNCTION_DEF (task, get_flags);
  702. /***
  703. * @method task:get_digest()
  704. * Returns message's unique digest (32 hex symbols)
  705. * @return {string} hex digest
  706. */
  707. LUA_FUNCTION_DEF (task, get_digest);
  708. /***
  709. * @method task:store_in_file([mode])
  710. * If task was loaded using file scan, then this method just returns its name,
  711. * otherwise, a fresh temporary file is created and its name is returned. Default
  712. * mode is 0600. To convert lua number to the octal mode you can use the following
  713. * trick: `tonumber("0644", 8)`. The file is automatically removed when task is
  714. * destroyed.
  715. *
  716. * @param {number} mode mode for new file
  717. * @return {string} file name with task content
  718. */
  719. LUA_FUNCTION_DEF (task, store_in_file);
  720. /***
  721. * @method task:get_protocol_reply([flags])
  722. * This method being called from a **postfilter** will return reply for a message
  723. * as it is returned to a client. This method returns the Lua table corresponding
  724. * to the UCL object. Flags is a table that specify which information should be
  725. * there in a reply:
  726. *
  727. * - `basic`: basic info, such as message-id
  728. * - `metrics`: metrics and symbols
  729. * - `messages`: messages
  730. * - `dkim`: dkim signature
  731. * - `rmilter`: rmilter control block
  732. * - `extra`: extra data, such as profiling
  733. * - `urls`: list of all urls in a message
  734. *
  735. * @param {table} flags table of flags (default is all flags but `urls`)
  736. * @return {table} ucl object corresponding to the reply
  737. */
  738. LUA_FUNCTION_DEF (task, get_protocol_reply);
  739. /***
  740. * @method task:headers_foreach(callback, [params])
  741. * This method calls `callback` for each header that satisfies some condition.
  742. * By default, all headers are iterated unless `callback` returns `true`. Nil or
  743. * false means continue of iterations.
  744. * Params could be as following:
  745. *
  746. * - `full`: header value is full table of all attributes @see task:get_header_full for details
  747. * - `regexp`: return headers that satisfies the specified regexp
  748. * @param {function} callback function from header name and header value
  749. * @param {table} params optional parameters
  750. */
  751. LUA_FUNCTION_DEF (task, headers_foreach);
  752. static const struct luaL_reg tasklib_f[] = {
  753. {NULL, NULL}
  754. };
  755. static const struct luaL_reg tasklib_m[] = {
  756. LUA_INTERFACE_DEF (task, get_message),
  757. LUA_INTERFACE_DEF (task, destroy),
  758. LUA_INTERFACE_DEF (task, process_message),
  759. LUA_INTERFACE_DEF (task, set_cfg),
  760. LUA_INTERFACE_DEF (task, get_cfg),
  761. LUA_INTERFACE_DEF (task, get_mempool),
  762. LUA_INTERFACE_DEF (task, get_session),
  763. LUA_INTERFACE_DEF (task, get_ev_base),
  764. LUA_INTERFACE_DEF (task, insert_result),
  765. LUA_INTERFACE_DEF (task, set_pre_result),
  766. LUA_INTERFACE_DEF (task, append_message),
  767. LUA_INTERFACE_DEF (task, has_urls),
  768. LUA_INTERFACE_DEF (task, get_urls),
  769. LUA_INTERFACE_DEF (task, get_content),
  770. LUA_INTERFACE_DEF (task, get_rawbody),
  771. LUA_INTERFACE_DEF (task, get_emails),
  772. LUA_INTERFACE_DEF (task, get_text_parts),
  773. LUA_INTERFACE_DEF (task, get_parts),
  774. LUA_INTERFACE_DEF (task, get_request_header),
  775. LUA_INTERFACE_DEF (task, set_request_header),
  776. LUA_INTERFACE_DEF (task, get_header),
  777. LUA_INTERFACE_DEF (task, get_header_raw),
  778. LUA_INTERFACE_DEF (task, get_header_full),
  779. LUA_INTERFACE_DEF (task, get_raw_headers),
  780. LUA_INTERFACE_DEF (task, get_received_headers),
  781. LUA_INTERFACE_DEF (task, get_queue_id),
  782. LUA_INTERFACE_DEF (task, get_uid),
  783. LUA_INTERFACE_DEF (task, get_resolver),
  784. LUA_INTERFACE_DEF (task, inc_dns_req),
  785. LUA_INTERFACE_DEF (task, get_dns_req),
  786. LUA_INTERFACE_DEF (task, has_recipients),
  787. LUA_INTERFACE_DEF (task, get_recipients),
  788. LUA_INTERFACE_DEF (task, set_recipients),
  789. LUA_INTERFACE_DEF (task, has_from),
  790. LUA_INTERFACE_DEF (task, get_from),
  791. LUA_INTERFACE_DEF (task, set_from),
  792. LUA_INTERFACE_DEF (task, get_user),
  793. LUA_INTERFACE_DEF (task, set_user),
  794. {"get_addr", lua_task_get_from_ip},
  795. {"get_ip", lua_task_get_from_ip},
  796. {"get_from_addr", lua_task_get_from_ip},
  797. LUA_INTERFACE_DEF (task, get_from_ip),
  798. LUA_INTERFACE_DEF (task, set_from_ip),
  799. LUA_INTERFACE_DEF (task, get_from_ip_num),
  800. LUA_INTERFACE_DEF (task, get_client_ip),
  801. LUA_INTERFACE_DEF (task, get_subject),
  802. LUA_INTERFACE_DEF (task, get_helo),
  803. LUA_INTERFACE_DEF (task, set_helo),
  804. LUA_INTERFACE_DEF (task, get_hostname),
  805. LUA_INTERFACE_DEF (task, set_hostname),
  806. LUA_INTERFACE_DEF (task, get_images),
  807. LUA_INTERFACE_DEF (task, get_archives),
  808. LUA_INTERFACE_DEF (task, get_symbol),
  809. LUA_INTERFACE_DEF (task, get_symbols),
  810. LUA_INTERFACE_DEF (task, get_symbols_all),
  811. LUA_INTERFACE_DEF (task, get_symbols_numeric),
  812. LUA_INTERFACE_DEF (task, has_symbol),
  813. LUA_INTERFACE_DEF (task, get_date),
  814. LUA_INTERFACE_DEF (task, get_message_id),
  815. LUA_INTERFACE_DEF (task, get_timeval),
  816. LUA_INTERFACE_DEF (task, get_metric_score),
  817. LUA_INTERFACE_DEF (task, get_metric_action),
  818. LUA_INTERFACE_DEF (task, set_metric_score),
  819. LUA_INTERFACE_DEF (task, set_metric_action),
  820. LUA_INTERFACE_DEF (task, set_metric_subject),
  821. LUA_INTERFACE_DEF (task, learn),
  822. LUA_INTERFACE_DEF (task, set_settings),
  823. LUA_INTERFACE_DEF (task, get_settings),
  824. LUA_INTERFACE_DEF (task, lookup_settings),
  825. LUA_INTERFACE_DEF (task, get_settings_id),
  826. LUA_INTERFACE_DEF (task, cache_get),
  827. LUA_INTERFACE_DEF (task, cache_set),
  828. LUA_INTERFACE_DEF (task, process_regexp),
  829. LUA_INTERFACE_DEF (task, get_size),
  830. LUA_INTERFACE_DEF (task, set_flag),
  831. LUA_INTERFACE_DEF (task, get_flags),
  832. LUA_INTERFACE_DEF (task, has_flag),
  833. LUA_INTERFACE_DEF (task, set_rmilter_reply),
  834. LUA_INTERFACE_DEF (task, get_digest),
  835. LUA_INTERFACE_DEF (task, store_in_file),
  836. LUA_INTERFACE_DEF (task, get_protocol_reply),
  837. LUA_INTERFACE_DEF (task, headers_foreach),
  838. {"__tostring", rspamd_lua_class_tostring},
  839. {NULL, NULL}
  840. };
  841. /* Image methods */
  842. LUA_FUNCTION_DEF (image, get_width);
  843. LUA_FUNCTION_DEF (image, get_height);
  844. LUA_FUNCTION_DEF (image, get_type);
  845. LUA_FUNCTION_DEF (image, get_filename);
  846. LUA_FUNCTION_DEF (image, get_size);
  847. static const struct luaL_reg imagelib_m[] = {
  848. LUA_INTERFACE_DEF (image, get_width),
  849. LUA_INTERFACE_DEF (image, get_height),
  850. LUA_INTERFACE_DEF (image, get_type),
  851. LUA_INTERFACE_DEF (image, get_filename),
  852. LUA_INTERFACE_DEF (image, get_size),
  853. {"__tostring", rspamd_lua_class_tostring},
  854. {NULL, NULL}
  855. };
  856. /* Archive methods */
  857. LUA_FUNCTION_DEF (archive, get_type);
  858. LUA_FUNCTION_DEF (archive, get_files);
  859. LUA_FUNCTION_DEF (archive, get_files_full);
  860. LUA_FUNCTION_DEF (archive, is_encrypted);
  861. LUA_FUNCTION_DEF (archive, get_filename);
  862. LUA_FUNCTION_DEF (archive, get_size);
  863. static const struct luaL_reg archivelib_m[] = {
  864. LUA_INTERFACE_DEF (archive, get_type),
  865. LUA_INTERFACE_DEF (archive, get_files),
  866. LUA_INTERFACE_DEF (archive, get_files_full),
  867. LUA_INTERFACE_DEF (archive, is_encrypted),
  868. LUA_INTERFACE_DEF (archive, get_filename),
  869. LUA_INTERFACE_DEF (archive, get_size),
  870. {"__tostring", rspamd_lua_class_tostring},
  871. {NULL, NULL}
  872. };
  873. /* Blob methods */
  874. LUA_FUNCTION_DEF (text, len);
  875. LUA_FUNCTION_DEF (text, str);
  876. LUA_FUNCTION_DEF (text, ptr);
  877. LUA_FUNCTION_DEF (text, save_in_file);
  878. LUA_FUNCTION_DEF (text, take_ownership);
  879. LUA_FUNCTION_DEF (text, gc);
  880. static const struct luaL_reg textlib_m[] = {
  881. LUA_INTERFACE_DEF (text, len),
  882. LUA_INTERFACE_DEF (text, str),
  883. LUA_INTERFACE_DEF (text, ptr),
  884. LUA_INTERFACE_DEF (text, take_ownership),
  885. LUA_INTERFACE_DEF (text, save_in_file),
  886. {"__len", lua_text_len},
  887. {"__tostring", lua_text_str},
  888. {"__gc", lua_text_gc},
  889. {NULL, NULL}
  890. };
  891. /* Utility functions */
  892. struct rspamd_task *
  893. lua_check_task (lua_State * L, gint pos)
  894. {
  895. void *ud = rspamd_lua_check_udata (L, pos, "rspamd{task}");
  896. luaL_argcheck (L, ud != NULL, pos, "'task' expected");
  897. return ud ? *((struct rspamd_task **)ud) : NULL;
  898. }
  899. struct rspamd_task *
  900. lua_check_task_maybe (lua_State * L, gint pos)
  901. {
  902. void *ud = rspamd_lua_check_udata_maybe (L, pos, "rspamd{task}");
  903. return ud ? *((struct rspamd_task **)ud) : NULL;
  904. }
  905. static struct rspamd_image *
  906. lua_check_image (lua_State * L)
  907. {
  908. void *ud = rspamd_lua_check_udata (L, 1, "rspamd{image}");
  909. luaL_argcheck (L, ud != NULL, 1, "'image' expected");
  910. return ud ? *((struct rspamd_image **)ud) : NULL;
  911. }
  912. static struct rspamd_archive *
  913. lua_check_archive (lua_State * L)
  914. {
  915. void *ud = rspamd_lua_check_udata (L, 1, "rspamd{archive}");
  916. luaL_argcheck (L, ud != NULL, 1, "'archive' expected");
  917. return ud ? *((struct rspamd_archive **)ud) : NULL;
  918. }
  919. struct rspamd_lua_text *
  920. lua_check_text (lua_State * L, gint pos)
  921. {
  922. void *ud = rspamd_lua_check_udata (L, pos, "rspamd{text}");
  923. luaL_argcheck (L, ud != NULL, pos, "'text' expected");
  924. return ud ? (struct rspamd_lua_text *)ud : NULL;
  925. }
  926. static void
  927. lua_task_set_cached (lua_State *L, struct rspamd_task *task, const gchar *key,
  928. gint pos)
  929. {
  930. gpointer elt;
  931. gint lua_ref;
  932. lua_pushvalue (L, pos);
  933. elt = g_hash_table_lookup (task->lua_cache, key);
  934. if (G_UNLIKELY (elt != NULL)) {
  935. /* Unref previous value */
  936. lua_ref = GPOINTER_TO_INT (elt);
  937. luaL_unref (L, LUA_REGISTRYINDEX, lua_ref);
  938. }
  939. lua_ref = luaL_ref (L, LUA_REGISTRYINDEX);
  940. g_hash_table_insert (task->lua_cache, (void *)key, GINT_TO_POINTER (lua_ref));
  941. }
  942. static gboolean
  943. lua_task_get_cached (lua_State *L, struct rspamd_task *task, const gchar *key)
  944. {
  945. gpointer elt;
  946. elt = g_hash_table_lookup (task->lua_cache, key);
  947. if (elt != NULL) {
  948. lua_rawgeti (L, LUA_REGISTRYINDEX, GPOINTER_TO_INT (elt));
  949. return TRUE;
  950. }
  951. return FALSE;
  952. }
  953. /* Task methods */
  954. static int
  955. lua_task_process_message (lua_State *L)
  956. {
  957. struct rspamd_task *task = lua_check_task (L, 1);
  958. if (task != NULL) {
  959. if (task->msg.len > 0) {
  960. if (rspamd_message_parse (task) == 0) {
  961. lua_pushboolean (L, TRUE);
  962. }
  963. else {
  964. lua_pushboolean (L, FALSE);
  965. }
  966. }
  967. else {
  968. lua_pushnil (L);
  969. }
  970. }
  971. else {
  972. return luaL_error (L, "invalid arguments");
  973. }
  974. return 1;
  975. }
  976. static int
  977. lua_task_get_cfg (lua_State *L)
  978. {
  979. struct rspamd_task *task = lua_check_task (L, 1);
  980. struct rspamd_config **pcfg;
  981. if (task) {
  982. pcfg = lua_newuserdata (L, sizeof (gpointer));
  983. rspamd_lua_setclass (L, "rspamd{config}", -1);
  984. *pcfg = task->cfg;
  985. }
  986. else {
  987. return luaL_error (L, "invalid arguments");
  988. }
  989. return 1;
  990. }
  991. static int
  992. lua_task_set_cfg (lua_State *L)
  993. {
  994. struct rspamd_task *task = lua_check_task (L, 1);
  995. void *ud = rspamd_lua_check_udata (L, 2, "rspamd{config}");
  996. if (task) {
  997. luaL_argcheck (L, ud != NULL, 1, "'config' expected");
  998. task->cfg = ud ? *((struct rspamd_config **)ud) : NULL;
  999. }
  1000. else {
  1001. return luaL_error (L, "invalid arguments");
  1002. }
  1003. return 0;
  1004. }
  1005. static int
  1006. lua_task_destroy (lua_State *L)
  1007. {
  1008. struct rspamd_task *task = lua_check_task (L, 1);
  1009. if (task != NULL) {
  1010. rspamd_task_free (task);
  1011. }
  1012. return 0;
  1013. }
  1014. static int
  1015. lua_task_get_message (lua_State * L)
  1016. {
  1017. return luaL_error (L, "task:get_message is no longer supported");
  1018. }
  1019. static int
  1020. lua_task_get_mempool (lua_State * L)
  1021. {
  1022. rspamd_mempool_t **ppool;
  1023. struct rspamd_task *task = lua_check_task (L, 1);
  1024. if (task != NULL) {
  1025. ppool = lua_newuserdata (L, sizeof (rspamd_mempool_t *));
  1026. rspamd_lua_setclass (L, "rspamd{mempool}", -1);
  1027. *ppool = task->task_pool;
  1028. }
  1029. else {
  1030. return luaL_error (L, "invalid arguments");
  1031. }
  1032. return 1;
  1033. }
  1034. static int
  1035. lua_task_get_session (lua_State * L)
  1036. {
  1037. struct rspamd_async_session **psession;
  1038. struct rspamd_task *task = lua_check_task (L, 1);
  1039. if (task != NULL) {
  1040. psession = lua_newuserdata (L, sizeof (void *));
  1041. rspamd_lua_setclass (L, "rspamd{session}", -1);
  1042. *psession = task->s;
  1043. }
  1044. else {
  1045. return luaL_error (L, "invalid arguments");
  1046. }
  1047. return 1;
  1048. }
  1049. static int
  1050. lua_task_get_ev_base (lua_State * L)
  1051. {
  1052. struct event_base **pbase;
  1053. struct rspamd_task *task = lua_check_task (L, 1);
  1054. if (task != NULL) {
  1055. pbase = lua_newuserdata (L, sizeof (struct event_base *));
  1056. rspamd_lua_setclass (L, "rspamd{ev_base}", -1);
  1057. *pbase = task->ev_base;
  1058. }
  1059. else {
  1060. return luaL_error (L, "invalid arguments");
  1061. }
  1062. return 1;
  1063. }
  1064. static gint
  1065. lua_task_insert_result (lua_State * L)
  1066. {
  1067. struct rspamd_task *task = lua_check_task (L, 1);
  1068. const gchar *symbol_name, *param;
  1069. double flag;
  1070. struct rspamd_symbol_result *s;
  1071. gint i, top;
  1072. if (task != NULL) {
  1073. symbol_name =
  1074. rspamd_mempool_strdup (task->task_pool, luaL_checkstring (L, 2));
  1075. flag = luaL_checknumber (L, 3);
  1076. top = lua_gettop (L);
  1077. s = rspamd_task_insert_result (task, symbol_name, flag, NULL);
  1078. /* Get additional options */
  1079. if (s) {
  1080. for (i = 4; i <= top; i++) {
  1081. if (lua_type (L, i) == LUA_TSTRING) {
  1082. param = luaL_checkstring (L, i);
  1083. rspamd_task_add_result_option (task, s, param);
  1084. }
  1085. else if (lua_type (L, i) == LUA_TTABLE) {
  1086. lua_pushvalue (L, i);
  1087. lua_pushnil (L);
  1088. while (lua_next (L, -2)) {
  1089. param = lua_tostring (L, -1);
  1090. rspamd_task_add_result_option (task, s, param);
  1091. lua_pop (L, 1);
  1092. }
  1093. lua_pop (L, 1);
  1094. }
  1095. }
  1096. }
  1097. }
  1098. else {
  1099. return luaL_error (L, "invalid arguments");
  1100. }
  1101. return 0;
  1102. }
  1103. static gint
  1104. lua_task_set_pre_result (lua_State * L)
  1105. {
  1106. struct rspamd_task *task = lua_check_task (L, 1);
  1107. struct rspamd_metric_result *mres;
  1108. gchar *action_str;
  1109. gint action = METRIC_ACTION_MAX;
  1110. if (task != NULL) {
  1111. if (lua_type (L, 2) == LUA_TNUMBER) {
  1112. action = lua_tointeger (L, 2);
  1113. }
  1114. else if (lua_type (L, 2) == LUA_TSTRING) {
  1115. rspamd_action_from_str (lua_tostring (L, 2), &action);
  1116. }
  1117. if (action < METRIC_ACTION_MAX && action >= METRIC_ACTION_REJECT) {
  1118. /* We also need to set the default metric to that result */
  1119. mres = rspamd_create_metric_result (task);
  1120. if (mres != NULL) {
  1121. mres->score = mres->metric->actions[action].score;
  1122. mres->action = action;
  1123. }
  1124. task->pre_result.action = action;
  1125. if (lua_gettop (L) >= 3) {
  1126. action_str = rspamd_mempool_strdup (task->task_pool,
  1127. luaL_checkstring (L, 3));
  1128. task->pre_result.str = action_str;
  1129. ucl_object_insert_key (task->messages,
  1130. ucl_object_fromstring (action_str), "smtp_message", 0,
  1131. false);
  1132. }
  1133. else {
  1134. task->pre_result.str = "unknown";
  1135. }
  1136. msg_info_task ("<%s>: set pre-result to %s: '%s'",
  1137. task->message_id, rspamd_action_to_str (action),
  1138. task->pre_result.str);
  1139. /* Don't classify or filter message if pre-filter sets results */
  1140. task->processed_stages |= (RSPAMD_TASK_STAGE_FILTERS |
  1141. RSPAMD_TASK_STAGE_CLASSIFIERS |
  1142. RSPAMD_TASK_STAGE_CLASSIFIERS_PRE |
  1143. RSPAMD_TASK_STAGE_CLASSIFIERS_POST);
  1144. }
  1145. else {
  1146. return luaL_error (L, "invalid arguments");
  1147. }
  1148. }
  1149. else {
  1150. return luaL_error (L, "invalid arguments");
  1151. }
  1152. return 0;
  1153. }
  1154. static gint
  1155. lua_task_append_message (lua_State * L)
  1156. {
  1157. struct rspamd_task *task = lua_check_task (L, 1);
  1158. const gchar *message = luaL_checkstring (L, 2), *category;
  1159. if (task != NULL) {
  1160. if (lua_type (L, 3) == LUA_TSTRING) {
  1161. category = luaL_checkstring (L, 3);
  1162. }
  1163. else {
  1164. category = "unknown";
  1165. }
  1166. ucl_object_insert_key (task->messages,
  1167. ucl_object_fromstring (message), category, 0,
  1168. true);
  1169. }
  1170. else {
  1171. return luaL_error (L, "invalid arguments");
  1172. }
  1173. return 0;
  1174. }
  1175. struct lua_tree_cb_data {
  1176. lua_State *L;
  1177. int i;
  1178. };
  1179. static void
  1180. lua_tree_url_callback (gpointer key, gpointer value, gpointer ud)
  1181. {
  1182. struct rspamd_lua_url *url;
  1183. struct lua_tree_cb_data *cb = ud;
  1184. url = lua_newuserdata (cb->L, sizeof (struct rspamd_lua_url));
  1185. rspamd_lua_setclass (cb->L, "rspamd{url}", -1);
  1186. url->url = value;
  1187. lua_rawseti (cb->L, -2, cb->i++);
  1188. }
  1189. static gint
  1190. lua_task_get_urls (lua_State * L)
  1191. {
  1192. struct rspamd_task *task = lua_check_task (L, 1);
  1193. struct lua_tree_cb_data cb;
  1194. gboolean need_emails = FALSE;
  1195. gsize sz;
  1196. if (task) {
  1197. if (lua_gettop (L) >= 2) {
  1198. need_emails = lua_toboolean (L, 2);
  1199. }
  1200. if (need_emails) {
  1201. if (!lua_task_get_cached (L, task, "emails+urls")) {
  1202. sz = g_hash_table_size (task->urls);
  1203. sz += g_hash_table_size (task->emails);
  1204. lua_createtable (L, sz, 0);
  1205. cb.i = 1;
  1206. cb.L = L;
  1207. g_hash_table_foreach (task->urls, lua_tree_url_callback, &cb);
  1208. g_hash_table_foreach (task->emails, lua_tree_url_callback, &cb);
  1209. lua_task_set_cached (L, task, "emails+urls", -1);
  1210. }
  1211. }
  1212. else {
  1213. if (!lua_task_get_cached (L, task, "urls")) {
  1214. sz = g_hash_table_size (task->urls);
  1215. lua_createtable (L, sz, 0);
  1216. cb.i = 1;
  1217. cb.L = L;
  1218. g_hash_table_foreach (task->urls, lua_tree_url_callback, &cb);
  1219. lua_task_set_cached (L, task, "urls", -1);
  1220. }
  1221. }
  1222. }
  1223. else {
  1224. return luaL_error (L, "invalid arguments");
  1225. }
  1226. return 1;
  1227. }
  1228. static gint
  1229. lua_task_has_urls (lua_State * L)
  1230. {
  1231. struct rspamd_task *task = lua_check_task (L, 1);
  1232. gboolean need_emails = FALSE, ret = FALSE;
  1233. if (task) {
  1234. if (lua_gettop (L) >= 2) {
  1235. need_emails = lua_toboolean (L, 2);
  1236. }
  1237. if (g_hash_table_size (task->urls) > 0) {
  1238. ret = TRUE;
  1239. }
  1240. if (need_emails && g_hash_table_size (task->emails) > 0) {
  1241. ret = TRUE;
  1242. }
  1243. }
  1244. else {
  1245. return luaL_error (L, "invalid arguments");
  1246. }
  1247. lua_pushboolean (L, ret);
  1248. return 1;
  1249. }
  1250. static gint
  1251. lua_task_get_content (lua_State * L)
  1252. {
  1253. struct rspamd_task *task = lua_check_task (L, 1);
  1254. struct rspamd_lua_text *t;
  1255. if (task) {
  1256. t = lua_newuserdata (L, sizeof (*t));
  1257. rspamd_lua_setclass (L, "rspamd{text}", -1);
  1258. t->len = task->msg.len;
  1259. t->start = task->msg.begin;
  1260. t->flags = 0;
  1261. }
  1262. else {
  1263. return luaL_error (L, "invalid arguments");
  1264. }
  1265. return 1;
  1266. }
  1267. static gint
  1268. lua_task_get_rawbody (lua_State * L)
  1269. {
  1270. struct rspamd_task *task = lua_check_task (L, 1);
  1271. struct rspamd_lua_text *t;
  1272. if (task) {
  1273. t = lua_newuserdata (L, sizeof (*t));
  1274. rspamd_lua_setclass (L, "rspamd{text}", -1);
  1275. if (task->raw_headers_content.len > 0) {
  1276. g_assert (task->raw_headers_content.len <= task->msg.len);
  1277. t->start = task->msg.begin + task->raw_headers_content.len;
  1278. t->len = task->msg.len - task->raw_headers_content.len;
  1279. }
  1280. else {
  1281. t->len = task->msg.len;
  1282. t->start = task->msg.begin;
  1283. }
  1284. t->flags = 0;
  1285. }
  1286. else {
  1287. return luaL_error (L, "invalid arguments");
  1288. }
  1289. return 1;
  1290. }
  1291. static gint
  1292. lua_task_get_emails (lua_State * L)
  1293. {
  1294. struct rspamd_task *task = lua_check_task (L, 1);
  1295. struct lua_tree_cb_data cb;
  1296. if (task) {
  1297. lua_createtable (L, g_hash_table_size (task->emails), 0);
  1298. cb.i = 1;
  1299. cb.L = L;
  1300. g_hash_table_foreach (task->emails, lua_tree_url_callback, &cb);
  1301. }
  1302. else {
  1303. return luaL_error (L, "invalid arguments");
  1304. }
  1305. return 1;
  1306. }
  1307. static gint
  1308. lua_task_get_text_parts (lua_State * L)
  1309. {
  1310. guint i;
  1311. struct rspamd_task *task = lua_check_task (L, 1);
  1312. struct rspamd_mime_text_part *part, **ppart;
  1313. if (task != NULL) {
  1314. if (!lua_task_get_cached (L, task, "text_parts")) {
  1315. lua_createtable (L, task->text_parts->len, 0);
  1316. for (i = 0; i < task->text_parts->len; i ++) {
  1317. part = g_ptr_array_index (task->text_parts, i);
  1318. ppart = lua_newuserdata (L, sizeof (struct rspamd_mime_text_part *));
  1319. *ppart = part;
  1320. rspamd_lua_setclass (L, "rspamd{textpart}", -1);
  1321. /* Make it array */
  1322. lua_rawseti (L, -2, i + 1);
  1323. }
  1324. lua_task_set_cached (L, task, "text_parts", -1);
  1325. }
  1326. }
  1327. else {
  1328. return luaL_error (L, "invalid arguments");
  1329. }
  1330. return 1;
  1331. }
  1332. static gint
  1333. lua_task_get_parts (lua_State * L)
  1334. {
  1335. guint i;
  1336. struct rspamd_task *task = lua_check_task (L, 1);
  1337. struct rspamd_mime_part *part, **ppart;
  1338. if (task != NULL) {
  1339. if (!lua_task_get_cached (L, task, "mime_parts")) {
  1340. lua_createtable (L, task->parts->len, 0);
  1341. for (i = 0; i < task->parts->len; i ++) {
  1342. part = g_ptr_array_index (task->parts, i);
  1343. ppart = lua_newuserdata (L, sizeof (struct rspamd_mime_part *));
  1344. *ppart = part;
  1345. rspamd_lua_setclass (L, "rspamd{mimepart}", -1);
  1346. /* Make it array */
  1347. lua_rawseti (L, -2, i + 1);
  1348. }
  1349. lua_task_set_cached (L, task, "mime_parts", -1);
  1350. }
  1351. }
  1352. else {
  1353. return luaL_error (L, "invalid arguments");
  1354. }
  1355. return 1;
  1356. }
  1357. static gint
  1358. lua_task_get_request_header (lua_State *L)
  1359. {
  1360. rspamd_ftok_t *hdr;
  1361. struct rspamd_task *task = lua_check_task (L, 1);
  1362. const gchar *s;
  1363. struct rspamd_lua_text *t;
  1364. s = luaL_checkstring (L, 2);
  1365. if (s && task) {
  1366. hdr = rspamd_task_get_request_header (task, s);
  1367. if (hdr) {
  1368. t = lua_newuserdata (L, sizeof (*t));
  1369. rspamd_lua_setclass (L, "rspamd{text}", -1);
  1370. t->start = hdr->begin;
  1371. t->len = hdr->len;
  1372. t->flags = 0;
  1373. return 1;
  1374. }
  1375. else {
  1376. lua_pushnil (L);
  1377. }
  1378. }
  1379. else {
  1380. return luaL_error (L, "invalid arguments");
  1381. }
  1382. return 1;
  1383. }
  1384. static gint
  1385. lua_task_set_request_header (lua_State *L)
  1386. {
  1387. struct rspamd_task *task = lua_check_task (L, 1);
  1388. const gchar *s, *v = NULL;
  1389. rspamd_fstring_t *buf;
  1390. struct rspamd_lua_text *t;
  1391. rspamd_ftok_t *hdr, *new_name;
  1392. gsize len, vlen;
  1393. s = luaL_checklstring (L, 2, &len);
  1394. if (s && task) {
  1395. if (lua_type (L, 3) == LUA_TSTRING) {
  1396. v = luaL_checklstring (L, 2, &vlen);
  1397. }
  1398. else if (lua_type (L, 3) == LUA_TUSERDATA) {
  1399. t = lua_check_text (L, 3);
  1400. if (t != NULL) {
  1401. v = t->start;
  1402. vlen = t->len;
  1403. }
  1404. }
  1405. if (v != NULL) {
  1406. buf = rspamd_fstring_new_init (v, vlen);
  1407. hdr = rspamd_ftok_map (buf);
  1408. buf = rspamd_fstring_new_init (s, len);
  1409. new_name = rspamd_ftok_map (buf);
  1410. rspamd_task_add_request_header (task, new_name, hdr);
  1411. }
  1412. }
  1413. else {
  1414. return luaL_error (L, "invalid arguments");
  1415. }
  1416. return 0;
  1417. }
  1418. gint
  1419. rspamd_lua_push_header (lua_State *L, struct rspamd_mime_header *rh,
  1420. gboolean full, gboolean raw)
  1421. {
  1422. const gchar *val;
  1423. if (full) {
  1424. /* Create new associated table for a header */
  1425. lua_createtable (L, 0, 7);
  1426. rspamd_lua_table_set (L, "name", rh->name);
  1427. if (rh->value) {
  1428. rspamd_lua_table_set (L, "value", rh->value);
  1429. }
  1430. if (rh->decoded) {
  1431. rspamd_lua_table_set (L, "decoded", rh->decoded);
  1432. }
  1433. lua_pushstring (L, "tab_separated");
  1434. lua_pushboolean (L, rh->tab_separated);
  1435. lua_settable (L, -3);
  1436. lua_pushstring (L, "empty_separator");
  1437. lua_pushboolean (L, rh->empty_separator);
  1438. lua_settable (L, -3);
  1439. rspamd_lua_table_set (L, "separator", rh->separator);
  1440. lua_pushstring (L, "order");
  1441. lua_pushnumber (L, rh->order);
  1442. lua_settable (L, -3);
  1443. }
  1444. else {
  1445. if (!raw) {
  1446. val = rh->decoded;
  1447. }
  1448. else {
  1449. val = rh->value;
  1450. }
  1451. if (val) {
  1452. lua_pushstring (L, val);
  1453. }
  1454. else {
  1455. lua_pushnil (L);
  1456. }
  1457. }
  1458. return 1;
  1459. }
  1460. gint
  1461. rspamd_lua_push_header_array (lua_State * L,
  1462. GPtrArray *ar,
  1463. gboolean full,
  1464. gboolean raw)
  1465. {
  1466. struct rspamd_mime_header *rh;
  1467. guint i;
  1468. if (ar == NULL || ar->len == 0) {
  1469. lua_pushnil (L);
  1470. return 1;
  1471. }
  1472. if (full) {
  1473. lua_createtable (L, ar->len, 0);
  1474. }
  1475. PTR_ARRAY_FOREACH (ar, i, rh) {
  1476. if (full) {
  1477. rspamd_lua_push_header (L, rh, full, raw);
  1478. lua_rawseti (L, -2, i + 1);
  1479. }
  1480. else {
  1481. return rspamd_lua_push_header (L, rh, full, raw);
  1482. }
  1483. }
  1484. return 1;
  1485. }
  1486. static gint
  1487. lua_task_get_header_common (lua_State *L, gboolean full, gboolean raw)
  1488. {
  1489. gboolean strong = FALSE;
  1490. struct rspamd_task *task = lua_check_task (L, 1);
  1491. const gchar *name;
  1492. GPtrArray *ar;
  1493. name = luaL_checkstring (L, 2);
  1494. if (name && task) {
  1495. if (lua_gettop (L) == 3) {
  1496. strong = lua_toboolean (L, 3);
  1497. }
  1498. ar = rspamd_message_get_header_array (task, name, strong);
  1499. return rspamd_lua_push_header_array (L, ar, full, raw);
  1500. }
  1501. else {
  1502. return luaL_error (L, "invalid arguments");
  1503. }
  1504. }
  1505. static gint
  1506. lua_task_get_header_full (lua_State * L)
  1507. {
  1508. return lua_task_get_header_common (L, TRUE, TRUE);
  1509. }
  1510. static gint
  1511. lua_task_get_header (lua_State * L)
  1512. {
  1513. return lua_task_get_header_common (L, FALSE, FALSE);
  1514. }
  1515. static gint
  1516. lua_task_get_header_raw (lua_State * L)
  1517. {
  1518. return lua_task_get_header_common (L, FALSE, TRUE);
  1519. }
  1520. static gint
  1521. lua_task_get_raw_headers (lua_State *L)
  1522. {
  1523. struct rspamd_task *task = lua_check_task (L, 1);
  1524. struct rspamd_lua_text *t;
  1525. if (task) {
  1526. t = lua_newuserdata (L, sizeof (*t));
  1527. rspamd_lua_setclass (L, "rspamd{text}", -1);
  1528. t->start = task->raw_headers_content.begin;
  1529. t->len = task->raw_headers_content.len;
  1530. t->flags = 0;
  1531. }
  1532. else {
  1533. return luaL_error (L, "invalid arguments");
  1534. }
  1535. return 1;
  1536. }
  1537. static gint
  1538. lua_task_get_received_headers (lua_State * L)
  1539. {
  1540. struct rspamd_task *task = lua_check_task (L, 1);
  1541. struct received_header *rh;
  1542. const gchar *proto;
  1543. guint i, k = 1;
  1544. if (task) {
  1545. if (!lua_task_get_cached (L, task, "received")) {
  1546. lua_createtable (L, task->received->len, 0);
  1547. for (i = 0; i < task->received->len; i ++) {
  1548. rh = g_ptr_array_index (task->received, i);
  1549. lua_createtable (L, 0, 10);
  1550. if (rh->hdr && rh->hdr->decoded) {
  1551. rspamd_lua_table_set (L, "raw", rh->hdr->decoded);
  1552. }
  1553. lua_pushstring (L, "flags");
  1554. lua_createtable (L, 0, 3);
  1555. lua_pushstring (L, "artificial");
  1556. if (rh->flags & RSPAMD_RECEIVED_FLAG_ARTIFICIAL) {
  1557. lua_pushboolean (L, true);
  1558. }
  1559. else {
  1560. lua_pushboolean (L, false);
  1561. }
  1562. lua_settable (L, -3);
  1563. lua_pushstring (L, "authenticated");
  1564. if (rh->flags & RSPAMD_RECEIVED_FLAG_AUTHENTICATED) {
  1565. lua_pushboolean (L, true);
  1566. }
  1567. else {
  1568. lua_pushboolean (L, false);
  1569. }
  1570. lua_settable (L, -3);
  1571. lua_pushstring (L, "ssl");
  1572. if (rh->flags & RSPAMD_RECEIVED_FLAG_SSL) {
  1573. lua_pushboolean (L, true);
  1574. }
  1575. else {
  1576. lua_pushboolean (L, false);
  1577. }
  1578. lua_settable (L, -3);
  1579. lua_settable (L, -3);
  1580. if (G_UNLIKELY (rh->from_ip == NULL &&
  1581. rh->real_ip == NULL &&
  1582. rh->real_hostname == NULL &&
  1583. rh->by_hostname == NULL && rh->timestamp == 0 &&
  1584. rh->for_mbox == NULL)) {
  1585. lua_rawseti (L, -2, k ++);
  1586. continue;
  1587. }
  1588. rspamd_lua_table_set (L, "from_hostname", rh->from_hostname);
  1589. rspamd_lua_table_set (L, "from_ip", rh->from_ip);
  1590. rspamd_lua_table_set (L, "real_hostname", rh->real_hostname);
  1591. lua_pushstring (L, "real_ip");
  1592. rspamd_lua_ip_push (L, rh->addr);
  1593. lua_settable (L, -3);
  1594. lua_pushstring (L, "proto");
  1595. switch (rh->type) {
  1596. case RSPAMD_RECEIVED_SMTP:
  1597. proto = "smtp";
  1598. break;
  1599. case RSPAMD_RECEIVED_ESMTP:
  1600. proto = "esmtp";
  1601. break;
  1602. case RSPAMD_RECEIVED_ESMTPS:
  1603. proto = "esmtps";
  1604. break;
  1605. case RSPAMD_RECEIVED_ESMTPA:
  1606. proto = "esmtpa";
  1607. break;
  1608. case RSPAMD_RECEIVED_ESMTPSA:
  1609. proto = "esmtpsa";
  1610. break;
  1611. case RSPAMD_RECEIVED_LMTP:
  1612. proto = "lmtp";
  1613. break;
  1614. case RSPAMD_RECEIVED_IMAP:
  1615. proto = "imap";
  1616. break;
  1617. case RSPAMD_RECEIVED_UNKNOWN:
  1618. default:
  1619. proto = "unknown";
  1620. break;
  1621. }
  1622. lua_pushstring (L, proto);
  1623. lua_settable (L, -3);
  1624. lua_pushstring (L, "timestamp");
  1625. lua_pushnumber (L, rh->timestamp);
  1626. lua_settable (L, -3);
  1627. rspamd_lua_table_set (L, "by_hostname", rh->by_hostname);
  1628. rspamd_lua_table_set (L, "for", rh->for_mbox);
  1629. lua_rawseti (L, -2, k ++);
  1630. }
  1631. lua_task_set_cached (L, task, "received", -1);
  1632. }
  1633. }
  1634. else {
  1635. return luaL_error (L, "invalid arguments");
  1636. }
  1637. return 1;
  1638. }
  1639. static gint
  1640. lua_task_get_queue_id (lua_State *L)
  1641. {
  1642. struct rspamd_task *task = lua_check_task (L, 1);
  1643. if (task) {
  1644. if (task->queue_id != NULL && strcmp (task->queue_id, "undef") != 0) {
  1645. lua_pushstring (L, task->queue_id);
  1646. }
  1647. else {
  1648. lua_pushnil (L);
  1649. }
  1650. }
  1651. else {
  1652. return luaL_error (L, "invalid arguments");
  1653. }
  1654. return 1;
  1655. }
  1656. static gint
  1657. lua_task_get_uid (lua_State *L)
  1658. {
  1659. struct rspamd_task *task = lua_check_task (L, 1);
  1660. if (task) {
  1661. lua_pushstring (L, task->task_pool->tag.uid);
  1662. }
  1663. else {
  1664. return luaL_error (L, "invalid arguments");
  1665. }
  1666. return 1;
  1667. }
  1668. static gint
  1669. lua_task_get_resolver (lua_State *L)
  1670. {
  1671. struct rspamd_task *task = lua_check_task (L, 1);
  1672. struct rspamd_dns_resolver **presolver;
  1673. if (task != NULL && task->resolver != NULL) {
  1674. presolver = lua_newuserdata (L, sizeof (void *));
  1675. rspamd_lua_setclass (L, "rspamd{resolver}", -1);
  1676. *presolver = task->resolver;
  1677. }
  1678. else {
  1679. return luaL_error (L, "invalid arguments");
  1680. }
  1681. return 1;
  1682. }
  1683. static gint
  1684. lua_task_inc_dns_req (lua_State *L)
  1685. {
  1686. struct rspamd_task *task = lua_check_task (L, 1);
  1687. if (task != NULL) {
  1688. task->dns_requests++;
  1689. }
  1690. else {
  1691. return luaL_error (L, "invalid arguments");
  1692. }
  1693. return 0;
  1694. }
  1695. static gint
  1696. lua_task_get_dns_req (lua_State *L)
  1697. {
  1698. struct rspamd_task *task = lua_check_task (L, 1);
  1699. if (task != NULL) {
  1700. lua_pushnumber (L, task->dns_requests);
  1701. }
  1702. else {
  1703. return luaL_error (L, "invalid arguments");
  1704. }
  1705. return 1;
  1706. }
  1707. enum rspamd_address_type {
  1708. RSPAMD_ADDRESS_ANY = 0,
  1709. RSPAMD_ADDRESS_SMTP = 1,
  1710. RSPAMD_ADDRESS_MIME = 2,
  1711. RSPAMD_ADDRESS_RAW_ANY = 3,
  1712. RSPAMD_ADDRESS_RAW_SMTP = 4,
  1713. RSPAMD_ADDRESS_RAW_MIME = 5,
  1714. RSPAMD_ADDRESS_MAX
  1715. };
  1716. /*
  1717. * Convert element at the specified position to the type
  1718. * for get_from/get_recipients
  1719. */
  1720. static enum rspamd_address_type
  1721. lua_task_str_to_get_type (lua_State *L, gint pos)
  1722. {
  1723. const gchar *type = NULL;
  1724. gint ret = RSPAMD_ADDRESS_ANY;
  1725. guint64 h;
  1726. gsize sz;
  1727. /* Get what value */
  1728. if (lua_type (L, pos) == LUA_TNUMBER) {
  1729. ret = lua_tonumber (L, pos);
  1730. if (ret >= RSPAMD_ADDRESS_ANY && ret < RSPAMD_ADDRESS_MAX) {
  1731. return ret;
  1732. }
  1733. return RSPAMD_ADDRESS_ANY;
  1734. }
  1735. else if (lua_type (L, pos) == LUA_TSTRING) {
  1736. type = lua_tolstring (L, pos, &sz);
  1737. if (type && sz > 0) {
  1738. h = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64,
  1739. type, sz, 0xdeadbabe);
  1740. switch (h) {
  1741. case 0xDA081341FB600389ULL: /* mime */
  1742. ret = RSPAMD_ADDRESS_MIME;
  1743. break;
  1744. case 0xEEC8A7832F8C43ACULL: /* any */
  1745. ret = RSPAMD_ADDRESS_ANY;
  1746. break;
  1747. case 0x472274D5193B2A80ULL: /* smtp */
  1748. case 0xEFE0F586CC9F14A9ULL: /* envelope */
  1749. ret = RSPAMD_ADDRESS_SMTP;
  1750. break;
  1751. case 0x9DA887501690DE20ULL: /* raw_mime */
  1752. ret = RSPAMD_ADDRESS_RAW_MIME;
  1753. break;
  1754. case 0x6B54FE02DEB595A4ULL: /* raw_smtp */
  1755. case 0xE0E596C861777B02ULL: /* raw_envelope */
  1756. ret = RSPAMD_ADDRESS_RAW_SMTP;
  1757. break;
  1758. case 0x2C49DBE3A10A0197ULL: /* raw_any */
  1759. ret = RSPAMD_ADDRESS_RAW_ANY;
  1760. break;
  1761. }
  1762. }
  1763. }
  1764. return ret;
  1765. }
  1766. #define EMAIL_CHECK_FLAG(fl, str) do { \
  1767. if (addr->flags & (fl)) { \
  1768. lua_pushstring (L, (str)); \
  1769. lua_pushboolean (L, true); \
  1770. lua_settable (L, -3); \
  1771. } \
  1772. } while(0)
  1773. static void
  1774. lua_push_email_address (lua_State *L, struct rspamd_email_address *addr)
  1775. {
  1776. if (addr) {
  1777. lua_createtable (L, 0, 4);
  1778. if (addr->addr_len > 0) {
  1779. lua_pushstring (L, "addr");
  1780. lua_pushlstring (L, addr->addr, addr->addr_len);
  1781. lua_settable (L, -3);
  1782. }
  1783. else {
  1784. lua_pushstring (L, "addr");
  1785. lua_pushstring (L, "");
  1786. lua_settable (L, -3);
  1787. }
  1788. if (addr->domain_len > 0) {
  1789. lua_pushstring (L, "domain");
  1790. lua_pushlstring (L, addr->domain, addr->domain_len);
  1791. lua_settable (L, -3);
  1792. }
  1793. else {
  1794. lua_pushstring (L, "domain");
  1795. lua_pushstring (L, "");
  1796. lua_settable (L, -3);
  1797. }
  1798. if (addr->user_len > 0) {
  1799. lua_pushstring (L, "user");
  1800. lua_pushlstring (L, addr->user, addr->user_len);
  1801. lua_settable (L, -3);
  1802. }
  1803. else {
  1804. lua_pushstring (L, "user");
  1805. lua_pushstring (L, "");
  1806. lua_settable (L, -3);
  1807. }
  1808. if (addr->name_len > 0) {
  1809. lua_pushstring (L, "name");
  1810. lua_pushlstring (L, addr->name, addr->name_len);
  1811. lua_settable (L, -3);
  1812. }
  1813. else {
  1814. lua_pushstring (L, "name");
  1815. lua_pushstring (L, "");
  1816. lua_settable (L, -3);
  1817. }
  1818. lua_pushstring (L, "flags");
  1819. lua_createtable (L, 0, 7);
  1820. EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_VALID, "valid");
  1821. EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_IP, "ip");
  1822. EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_BRACED, "braced");
  1823. EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_QUOTED, "quoted");
  1824. EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_EMPTY, "empty");
  1825. EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_HAS_BACKSLASH, "backslash");
  1826. EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_HAS_8BIT, "8bit");
  1827. lua_settable (L, -3);
  1828. }
  1829. }
  1830. void
  1831. lua_push_emails_address_list (lua_State *L, GPtrArray *addrs)
  1832. {
  1833. struct rspamd_email_address *addr;
  1834. guint i;
  1835. lua_createtable (L, addrs->len, 0);
  1836. for (i = 0; i < addrs->len; i ++) {
  1837. addr = g_ptr_array_index (addrs, i);
  1838. lua_push_email_address (L, addr);
  1839. lua_rawseti (L, -2, i + 1);
  1840. }
  1841. }
  1842. static gboolean
  1843. lua_import_email_address (lua_State *L, struct rspamd_task *task,
  1844. gint pos,
  1845. struct rspamd_email_address **paddr)
  1846. {
  1847. struct rspamd_email_address *addr;
  1848. const gchar *p;
  1849. gsize len;
  1850. g_assert (paddr != NULL);
  1851. if (!lua_istable (L, pos)) {
  1852. return FALSE;
  1853. }
  1854. addr = rspamd_mempool_alloc0 (task->task_pool, sizeof (*addr));
  1855. lua_pushstring (L, "name");
  1856. lua_gettable (L, pos);
  1857. if (lua_type (L, -1) == LUA_TSTRING) {
  1858. p = lua_tolstring (L, -1, &len);
  1859. addr->name = (const gchar *)rspamd_mempool_alloc (task->task_pool, len);
  1860. memcpy ((gchar *)addr->name, p, len);
  1861. addr->name_len = len;
  1862. }
  1863. lua_pop (L, 1);
  1864. lua_pushstring (L, "user");
  1865. lua_gettable (L, pos);
  1866. if (lua_type (L, -1) == LUA_TSTRING) {
  1867. p = lua_tolstring (L, -1, &len);
  1868. addr->user = (const gchar *)rspamd_mempool_alloc (task->task_pool, len);
  1869. memcpy ((gchar *)addr->user, p, len);
  1870. addr->user_len = len;
  1871. }
  1872. lua_pop (L, 1);
  1873. lua_pushstring (L, "domain");
  1874. lua_gettable (L, pos);
  1875. if (lua_type (L, -1) == LUA_TSTRING) {
  1876. p = lua_tolstring (L, -1, &len);
  1877. addr->domain = (const gchar *)rspamd_mempool_alloc (task->task_pool, len);
  1878. memcpy ((gchar *)addr->domain, p, len);
  1879. addr->domain_len = len;
  1880. }
  1881. lua_pop (L, 1);
  1882. lua_pushstring (L, "addr");
  1883. lua_gettable (L, pos);
  1884. if (lua_type (L, -1) == LUA_TSTRING) {
  1885. p = lua_tolstring (L, -1, &len);
  1886. addr->addr = (const gchar *)rspamd_mempool_alloc (task->task_pool, len);
  1887. memcpy ((gchar *)addr->addr, p, len);
  1888. addr->addr_len = len;
  1889. }
  1890. else {
  1891. /* Construct addr */
  1892. len = addr->domain_len + addr->user_len + 1;
  1893. addr->addr = (const gchar *)rspamd_mempool_alloc (task->task_pool, len);
  1894. addr->addr_len = rspamd_snprintf ((gchar *)addr->addr, len, "%*s@%*s",
  1895. (int)addr->user_len, addr->user,
  1896. (int)addr->domain_len, addr->domain);
  1897. }
  1898. lua_pop (L, 1);
  1899. lua_pushstring (L, "raw");
  1900. lua_gettable (L, pos);
  1901. if (lua_type (L, -1) == LUA_TSTRING) {
  1902. p = lua_tolstring (L, -1, &len);
  1903. addr->raw = (const gchar *)rspamd_mempool_alloc (task->task_pool, len);
  1904. memcpy ((gchar *)addr->raw, p, len);
  1905. addr->raw_len = len;
  1906. }
  1907. else {
  1908. /* Construct raw addr */
  1909. len = addr->addr_len + addr->name_len + 3;
  1910. addr->raw = (const gchar *)rspamd_mempool_alloc (task->task_pool, len);
  1911. if (addr->name_len > 0) {
  1912. addr->raw_len = rspamd_snprintf ((gchar *)addr->raw, len, "%*s <%*s>",
  1913. (int)addr->name_len, addr->name,
  1914. (int)addr->addr_len, addr->addr);
  1915. }
  1916. else {
  1917. addr->raw_len = rspamd_snprintf ((gchar *)addr->raw, len, "<%*s@%*s>",
  1918. (int)addr->name_len, addr->name,
  1919. (int)addr->addr_len, addr->addr);
  1920. }
  1921. }
  1922. lua_pop (L, 1);
  1923. addr->flags = RSPAMD_EMAIL_ADDR_VALID;
  1924. *paddr = addr;
  1925. return TRUE;
  1926. }
  1927. static gint
  1928. lua_task_get_recipients (lua_State *L)
  1929. {
  1930. struct rspamd_task *task = lua_check_task (L, 1);
  1931. GPtrArray *ptrs = NULL;
  1932. gint what = 0;
  1933. if (task) {
  1934. if (lua_gettop (L) == 2) {
  1935. /* Get what value */
  1936. what = lua_task_str_to_get_type (L, 2);
  1937. }
  1938. switch (what) {
  1939. case RSPAMD_ADDRESS_SMTP:
  1940. /* Here we check merely envelope rcpt */
  1941. ptrs = task->rcpt_envelope;
  1942. break;
  1943. case RSPAMD_ADDRESS_MIME:
  1944. /* Here we check merely mime rcpt */
  1945. ptrs = task->rcpt_mime;
  1946. break;
  1947. case RSPAMD_ADDRESS_ANY:
  1948. default:
  1949. if (task->rcpt_envelope) {
  1950. ptrs = task->rcpt_envelope;
  1951. }
  1952. else {
  1953. ptrs = task->rcpt_mime;
  1954. }
  1955. break;
  1956. }
  1957. if (ptrs) {
  1958. lua_push_emails_address_list (L, ptrs);
  1959. }
  1960. else {
  1961. lua_pushnil (L);
  1962. }
  1963. }
  1964. else {
  1965. return luaL_error (L, "invalid arguments");
  1966. }
  1967. return 1;
  1968. }
  1969. static gint
  1970. lua_task_set_recipients (lua_State *L)
  1971. {
  1972. struct rspamd_task *task = lua_check_task (L, 1);
  1973. GPtrArray *ptrs = NULL;
  1974. struct rspamd_email_address *addr = NULL;
  1975. gint what = 0, pos = 3;
  1976. if (task) {
  1977. if (lua_isstring (L, 2) || lua_isnumber (L, 2)) {
  1978. /* Get what value */
  1979. what = lua_task_str_to_get_type (L, 2);
  1980. }
  1981. else if (lua_istable (L, 2)) {
  1982. pos = 2;
  1983. }
  1984. switch (what) {
  1985. case RSPAMD_ADDRESS_SMTP:
  1986. /* Here we check merely envelope rcpt */
  1987. ptrs = task->rcpt_envelope;
  1988. break;
  1989. case RSPAMD_ADDRESS_MIME:
  1990. /* Here we check merely mime rcpt */
  1991. ptrs = task->rcpt_mime;
  1992. break;
  1993. case RSPAMD_ADDRESS_ANY:
  1994. default:
  1995. if (task->rcpt_envelope) {
  1996. ptrs = task->rcpt_envelope;
  1997. }
  1998. else {
  1999. ptrs = task->rcpt_mime;
  2000. }
  2001. break;
  2002. }
  2003. if (ptrs) {
  2004. lua_pushvalue (L, pos);
  2005. g_ptr_array_set_size (ptrs, 0);
  2006. for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
  2007. if (lua_import_email_address (L, task, lua_gettop (L), &addr)) {
  2008. g_ptr_array_add (ptrs, addr);
  2009. addr = NULL;
  2010. }
  2011. }
  2012. lua_pop (L, 1);
  2013. lua_pushboolean (L, true);
  2014. }
  2015. else {
  2016. lua_pushboolean (L, false);
  2017. }
  2018. }
  2019. else {
  2020. return luaL_error (L, "invalid arguments");
  2021. }
  2022. return 1;
  2023. }
  2024. #define CHECK_EMAIL_ADDR(addr) do { \
  2025. if (addr == NULL) { \
  2026. ret = 0; \
  2027. } \
  2028. else { \
  2029. ret = addr->flags & RSPAMD_EMAIL_ADDR_VALID; \
  2030. } \
  2031. } while (0)
  2032. #define CHECK_EMAIL_ADDR_LIST(addr) do { \
  2033. if (addr == NULL) { \
  2034. ret = 0; \
  2035. } \
  2036. else { \
  2037. ret = addr->len > 0; \
  2038. } \
  2039. } while (0)
  2040. static gint
  2041. lua_task_has_from (lua_State *L)
  2042. {
  2043. struct rspamd_task *task = lua_check_task (L, 1);
  2044. gint what = 0;
  2045. gboolean ret = FALSE;
  2046. if (task) {
  2047. if (lua_gettop (L) == 2) {
  2048. /* Get what value */
  2049. what = lua_task_str_to_get_type (L, 2);
  2050. }
  2051. switch (what) {
  2052. case RSPAMD_ADDRESS_SMTP:
  2053. /* Here we check merely envelope rcpt */
  2054. CHECK_EMAIL_ADDR (task->from_envelope);
  2055. break;
  2056. case RSPAMD_ADDRESS_MIME:
  2057. /* Here we check merely mime rcpt */
  2058. CHECK_EMAIL_ADDR_LIST (task->from_mime);
  2059. break;
  2060. case RSPAMD_ADDRESS_ANY:
  2061. default:
  2062. CHECK_EMAIL_ADDR (task->from_envelope);
  2063. if (!ret) {
  2064. CHECK_EMAIL_ADDR_LIST (task->from_mime);
  2065. }
  2066. break;
  2067. }
  2068. }
  2069. else {
  2070. return luaL_error (L, "invalid arguments");
  2071. }
  2072. lua_pushboolean (L, ret);
  2073. return 1;
  2074. }
  2075. static gint
  2076. lua_task_has_recipients (lua_State *L)
  2077. {
  2078. struct rspamd_task *task = lua_check_task (L, 1);
  2079. gint what = 0;
  2080. gboolean ret = FALSE;
  2081. if (task) {
  2082. if (lua_gettop (L) == 2) {
  2083. /* Get what value */
  2084. what = lua_task_str_to_get_type (L, 2);
  2085. }
  2086. switch (what) {
  2087. case RSPAMD_ADDRESS_SMTP:
  2088. /* Here we check merely envelope rcpt */
  2089. CHECK_EMAIL_ADDR_LIST (task->rcpt_envelope);
  2090. break;
  2091. case RSPAMD_ADDRESS_MIME:
  2092. /* Here we check merely mime rcpt */
  2093. CHECK_EMAIL_ADDR_LIST (task->rcpt_mime);
  2094. break;
  2095. case RSPAMD_ADDRESS_ANY:
  2096. default:
  2097. CHECK_EMAIL_ADDR_LIST (task->rcpt_envelope);
  2098. if (!ret) {
  2099. CHECK_EMAIL_ADDR_LIST (task->rcpt_mime);
  2100. }
  2101. break;
  2102. }
  2103. }
  2104. else {
  2105. return luaL_error (L, "invalid arguments");
  2106. }
  2107. lua_pushboolean (L, ret);
  2108. return 1;
  2109. }
  2110. static gint
  2111. lua_task_get_from (lua_State *L)
  2112. {
  2113. struct rspamd_task *task = lua_check_task (L, 1);
  2114. GPtrArray *addrs = NULL;
  2115. struct rspamd_email_address *addr = NULL;
  2116. gint what = 0;
  2117. if (task) {
  2118. if (lua_gettop (L) == 2) {
  2119. /* Get what value */
  2120. what = lua_task_str_to_get_type (L, 2);
  2121. }
  2122. switch (what) {
  2123. case RSPAMD_ADDRESS_SMTP:
  2124. /* Here we check merely envelope rcpt */
  2125. addr = task->from_envelope;
  2126. break;
  2127. case RSPAMD_ADDRESS_MIME:
  2128. /* Here we check merely mime rcpt */
  2129. addrs = task->from_mime;
  2130. break;
  2131. case RSPAMD_ADDRESS_ANY:
  2132. default:
  2133. if (task->from_envelope) {
  2134. addr = task->from_envelope;
  2135. }
  2136. else {
  2137. addrs = task->from_mime;
  2138. }
  2139. break;
  2140. }
  2141. if (addrs) {
  2142. lua_push_emails_address_list (L, addrs);
  2143. }
  2144. else if (addr) {
  2145. /* Create table to preserve compatibility */
  2146. if (addr->addr) {
  2147. lua_createtable (L, 1, 0);
  2148. lua_push_email_address (L, addr);
  2149. lua_rawseti (L, -2, 1);
  2150. }
  2151. else {
  2152. lua_pushnil (L);
  2153. }
  2154. }
  2155. else {
  2156. lua_pushnil (L);
  2157. }
  2158. }
  2159. else {
  2160. return luaL_error (L, "invalid arguments");
  2161. }
  2162. return 1;
  2163. }
  2164. static gint
  2165. lua_task_set_from (lua_State *L)
  2166. {
  2167. struct rspamd_task *task = lua_check_task (L, 1);
  2168. GPtrArray *addrs = NULL;
  2169. struct rspamd_email_address **paddr = NULL, *addr;
  2170. gint what = 0, pos = 3;
  2171. if (task) {
  2172. if (lua_isstring (L, 2) || lua_isnumber (L, 2)) {
  2173. /* Get what value */
  2174. what = lua_task_str_to_get_type (L, 2);
  2175. }
  2176. else if (lua_istable (L, 2)) {
  2177. pos = 2;
  2178. }
  2179. switch (what) {
  2180. case RSPAMD_ADDRESS_SMTP:
  2181. /* Here we check merely envelope rcpt */
  2182. paddr = &task->from_envelope;
  2183. break;
  2184. case RSPAMD_ADDRESS_MIME:
  2185. /* Here we check merely mime rcpt */
  2186. addrs = task->from_mime;
  2187. break;
  2188. case RSPAMD_ADDRESS_ANY:
  2189. default:
  2190. if (task->from_envelope) {
  2191. paddr = &task->from_envelope;
  2192. }
  2193. else {
  2194. addrs = task->from_mime;
  2195. }
  2196. break;
  2197. }
  2198. if (addrs) {
  2199. if (lua_import_email_address (L, task, pos, &addr)) {
  2200. g_ptr_array_set_size (addrs, 0);
  2201. g_ptr_array_add (addrs, addr);
  2202. lua_pushboolean (L, true);
  2203. }
  2204. else {
  2205. lua_pushboolean (L, false);
  2206. }
  2207. }
  2208. else if (paddr) {
  2209. if (lua_import_email_address (L, task, pos, paddr)) {
  2210. lua_pushboolean (L, true);
  2211. }
  2212. else {
  2213. lua_pushboolean (L, false);
  2214. }
  2215. }
  2216. else {
  2217. lua_pushboolean (L, false);
  2218. }
  2219. }
  2220. else {
  2221. return luaL_error (L, "invalid arguments");
  2222. }
  2223. return 1;
  2224. }
  2225. static gint
  2226. lua_task_get_user (lua_State *L)
  2227. {
  2228. struct rspamd_task *task = lua_check_task (L, 1);
  2229. if (task) {
  2230. if (task->user != NULL) {
  2231. lua_pushstring (L, task->user);
  2232. }
  2233. else {
  2234. lua_pushnil (L);
  2235. }
  2236. }
  2237. else {
  2238. return luaL_error (L, "invalid arguments");
  2239. }
  2240. return 1;
  2241. }
  2242. static gint
  2243. lua_task_set_user (lua_State *L)
  2244. {
  2245. struct rspamd_task *task = lua_check_task (L, 1);
  2246. const gchar *new_user;
  2247. if (task) {
  2248. new_user = luaL_checkstring (L, 2);
  2249. if (new_user) {
  2250. task->user = rspamd_mempool_strdup (task->task_pool, new_user);
  2251. }
  2252. }
  2253. else {
  2254. return luaL_error (L, "invalid arguments");
  2255. }
  2256. return 0;
  2257. }
  2258. static gint
  2259. lua_task_get_from_ip (lua_State *L)
  2260. {
  2261. struct rspamd_task *task = lua_check_task (L, 1);
  2262. if (task) {
  2263. rspamd_lua_ip_push (L, task->from_addr);
  2264. }
  2265. else {
  2266. return luaL_error (L, "invalid arguments");
  2267. }
  2268. return 1;
  2269. }
  2270. static gint
  2271. lua_task_set_from_ip (lua_State *L)
  2272. {
  2273. struct rspamd_task *task = lua_check_task (L, 1);
  2274. const gchar *ip_str = luaL_checkstring (L, 2);
  2275. rspamd_inet_addr_t *addr = NULL;
  2276. if (!task || !ip_str) {
  2277. lua_pushstring (L, "invalid parameters");
  2278. return lua_error (L);
  2279. }
  2280. else {
  2281. if (!rspamd_parse_inet_address (&addr,
  2282. ip_str,
  2283. 0)) {
  2284. msg_warn_task ("cannot get IP from received header: '%s'",
  2285. ip_str);
  2286. }
  2287. else {
  2288. if (task->from_addr) {
  2289. rspamd_inet_address_free (task->from_addr);
  2290. }
  2291. task->from_addr = addr;
  2292. }
  2293. }
  2294. return 0;
  2295. }
  2296. static gint
  2297. lua_task_get_from_ip_num (lua_State *L)
  2298. {
  2299. msg_err ("this function is deprecated and should no longer be used");
  2300. lua_pushnil (L);
  2301. return 1;
  2302. }
  2303. static gint
  2304. lua_task_get_client_ip (lua_State *L)
  2305. {
  2306. struct rspamd_task *task = lua_check_task (L, 1);
  2307. if (task) {
  2308. rspamd_lua_ip_push (L, task->client_addr);
  2309. }
  2310. else {
  2311. return luaL_error (L, "invalid arguments");
  2312. }
  2313. return 1;
  2314. }
  2315. static gint
  2316. lua_task_get_helo (lua_State *L)
  2317. {
  2318. struct rspamd_task *task = lua_check_task (L, 1);
  2319. if (task) {
  2320. if (task->helo != NULL) {
  2321. lua_pushstring (L, task->helo);
  2322. return 1;
  2323. }
  2324. else {
  2325. lua_pushnil (L);
  2326. }
  2327. }
  2328. else {
  2329. return luaL_error (L, "invalid arguments");
  2330. }
  2331. return 1;
  2332. }
  2333. static gint
  2334. lua_task_get_subject (lua_State *L)
  2335. {
  2336. struct rspamd_task *task = lua_check_task (L, 1);
  2337. if (task) {
  2338. if (task->subject != NULL) {
  2339. lua_pushstring (L, task->subject);
  2340. return 1;
  2341. }
  2342. else {
  2343. lua_pushnil (L);
  2344. }
  2345. }
  2346. else {
  2347. return luaL_error (L, "invalid arguments");
  2348. }
  2349. return 1;
  2350. }
  2351. static gint
  2352. lua_task_set_helo (lua_State *L)
  2353. {
  2354. struct rspamd_task *task = lua_check_task (L, 1);
  2355. const gchar *new_helo;
  2356. if (task) {
  2357. new_helo = luaL_checkstring (L, 2);
  2358. if (new_helo) {
  2359. task->helo = rspamd_mempool_strdup (task->task_pool, new_helo);
  2360. }
  2361. }
  2362. else {
  2363. return luaL_error (L, "invalid arguments");
  2364. }
  2365. return 0;
  2366. }
  2367. static gint
  2368. lua_task_get_hostname (lua_State *L)
  2369. {
  2370. struct rspamd_task *task = lua_check_task (L, 1);
  2371. if (task) {
  2372. if (task->hostname != NULL) {
  2373. /* Check whether it looks like an IP address */
  2374. if (*task->hostname == '[') {
  2375. /*
  2376. * From the milter documentation:
  2377. * If the reverse lookup fails or if none of the IP
  2378. * addresses of the resolved host name matches the
  2379. * original IP address, hostname will contain the
  2380. * message sender's IP address enclosed in square
  2381. * brackets (e.g. `[a.b.c.d]')
  2382. */
  2383. lua_pushstring (L, "unknown");
  2384. }
  2385. else {
  2386. lua_pushstring (L, task->hostname);
  2387. }
  2388. }
  2389. else {
  2390. lua_pushnil (L);
  2391. }
  2392. }
  2393. else {
  2394. return luaL_error (L, "invalid arguments");
  2395. }
  2396. return 1;
  2397. }
  2398. static gint
  2399. lua_task_set_hostname (lua_State *L)
  2400. {
  2401. struct rspamd_task *task = lua_check_task (L, 1);
  2402. const gchar *new_hostname;
  2403. if (task) {
  2404. new_hostname = luaL_checkstring (L, 2);
  2405. if (new_hostname) {
  2406. task->hostname = rspamd_mempool_strdup (task->task_pool,
  2407. new_hostname);
  2408. }
  2409. }
  2410. else {
  2411. return luaL_error (L, "invalid arguments");
  2412. }
  2413. return 0;
  2414. }
  2415. static gint
  2416. lua_task_get_images (lua_State *L)
  2417. {
  2418. struct rspamd_task *task = lua_check_task (L, 1);
  2419. guint nelt = 0, i;
  2420. struct rspamd_mime_part *part;
  2421. struct rspamd_image **pimg;
  2422. if (task) {
  2423. if (!lua_task_get_cached (L, task, "images")) {
  2424. lua_createtable (L, task->parts->len, 0);
  2425. for (i = 0; i < task->parts->len; i ++) {
  2426. part = g_ptr_array_index (task->parts, i);
  2427. if (part->flags & RSPAMD_MIME_PART_IMAGE) {
  2428. pimg = lua_newuserdata (L, sizeof (struct rspamd_image *));
  2429. rspamd_lua_setclass (L, "rspamd{image}", -1);
  2430. *pimg = part->specific.img;
  2431. lua_rawseti (L, -2, ++nelt);
  2432. }
  2433. }
  2434. lua_task_set_cached (L, task, "images", -1);
  2435. }
  2436. }
  2437. else {
  2438. return luaL_error (L, "invalid arguments");
  2439. }
  2440. return 1;
  2441. }
  2442. static gint
  2443. lua_task_get_archives (lua_State *L)
  2444. {
  2445. struct rspamd_task *task = lua_check_task (L, 1);
  2446. guint nelt = 0, i;
  2447. struct rspamd_mime_part *part;
  2448. struct rspamd_archive **parch;
  2449. if (task) {
  2450. if (!lua_task_get_cached (L, task, "archives")) {
  2451. lua_createtable (L, task->parts->len, 0);
  2452. for (i = 0; i < task->parts->len; i ++) {
  2453. part = g_ptr_array_index (task->parts, i);
  2454. if (part->flags & RSPAMD_MIME_PART_ARCHIVE) {
  2455. parch = lua_newuserdata (L, sizeof (struct rspamd_archive *));
  2456. rspamd_lua_setclass (L, "rspamd{archive}", -1);
  2457. *parch = part->specific.arch;
  2458. lua_rawseti (L, -2, ++nelt);
  2459. }
  2460. }
  2461. lua_task_set_cached (L, task, "archives", -1);
  2462. }
  2463. }
  2464. else {
  2465. return luaL_error (L, "invalid arguments");
  2466. }
  2467. return 1;
  2468. }
  2469. static inline gboolean
  2470. lua_push_symbol_result (lua_State *L,
  2471. struct rspamd_task *task,
  2472. const gchar *symbol,
  2473. struct rspamd_symbol_result *symbol_result,
  2474. gboolean add_metric,
  2475. gboolean add_name)
  2476. {
  2477. struct rspamd_metric_result *metric_res;
  2478. struct rspamd_symbol_result *s = NULL;
  2479. struct rspamd_symbol_option *opt;
  2480. gint j = 1, e = 4;
  2481. if (!symbol_result) {
  2482. metric_res = task->result;
  2483. if (metric_res) {
  2484. s = g_hash_table_lookup (metric_res->symbols, symbol);
  2485. }
  2486. }
  2487. else {
  2488. s = symbol_result;
  2489. }
  2490. if (s) {
  2491. if (add_metric) {
  2492. e++;
  2493. }
  2494. if (add_name) {
  2495. e++;
  2496. }
  2497. lua_createtable (L, 0, e);
  2498. if (add_name) {
  2499. lua_pushstring (L, "name");
  2500. lua_pushstring (L, symbol);
  2501. lua_settable (L, -3);
  2502. }
  2503. lua_pushstring (L, "score");
  2504. lua_pushnumber (L, s->score);
  2505. lua_settable (L, -3);
  2506. if (s->sym && s->sym->gr) {
  2507. lua_pushstring (L, "group");
  2508. lua_pushstring (L, s->sym->gr->name);
  2509. lua_settable (L, -3);
  2510. }
  2511. else {
  2512. lua_pushstring (L, "group");
  2513. lua_pushstring (L, "ungrouped");
  2514. lua_settable (L, -3);
  2515. }
  2516. if (s->options) {
  2517. lua_pushstring (L, "options");
  2518. lua_createtable (L, g_hash_table_size (s->options), 0);
  2519. DL_FOREACH (s->opts_head, opt) {
  2520. lua_pushstring (L, (const char*)opt->option);
  2521. lua_rawseti (L, -2, j++);
  2522. }
  2523. lua_settable (L, -3);
  2524. }
  2525. return TRUE;
  2526. }
  2527. return FALSE;
  2528. }
  2529. static gint
  2530. lua_task_get_symbol (lua_State *L)
  2531. {
  2532. struct rspamd_task *task = lua_check_task (L, 1);
  2533. const gchar *symbol;
  2534. gboolean found = FALSE;
  2535. gint i = 1;
  2536. symbol = luaL_checkstring (L, 2);
  2537. if (task && symbol) {
  2538. lua_createtable (L, 1, 0);
  2539. if ((found = lua_push_symbol_result (L, task, symbol,
  2540. NULL, TRUE, FALSE))) {
  2541. lua_rawseti (L, -2, i++);
  2542. }
  2543. else {
  2544. /* Pop table */
  2545. lua_pop (L, 1);
  2546. }
  2547. }
  2548. else {
  2549. return luaL_error (L, "invalid arguments");
  2550. }
  2551. if (!found) {
  2552. lua_pushnil (L);
  2553. }
  2554. return 1;
  2555. }
  2556. static gint
  2557. lua_task_has_symbol (lua_State *L)
  2558. {
  2559. struct rspamd_task *task = lua_check_task (L, 1);
  2560. const gchar *symbol;
  2561. struct rspamd_metric_result *mres;
  2562. gboolean found = FALSE;
  2563. symbol = luaL_checkstring (L, 2);
  2564. if (task && symbol) {
  2565. mres = task->result;
  2566. if (mres) {
  2567. found = g_hash_table_lookup (mres->symbols, symbol) != NULL;
  2568. }
  2569. lua_pushboolean (L, found);
  2570. }
  2571. else {
  2572. return luaL_error (L, "invalid arguments");
  2573. }
  2574. return 1;
  2575. }
  2576. static gint
  2577. lua_task_get_symbols (lua_State *L)
  2578. {
  2579. struct rspamd_task *task = lua_check_task (L, 1);
  2580. struct rspamd_metric_result *mres;
  2581. gint i = 1;
  2582. GHashTableIter it;
  2583. gpointer k, v;
  2584. struct rspamd_symbol_result *s;
  2585. if (task) {
  2586. mres = task->result;
  2587. if (mres) {
  2588. lua_createtable (L, g_hash_table_size (mres->symbols), 0);
  2589. lua_createtable (L, g_hash_table_size (mres->symbols), 0);
  2590. g_hash_table_iter_init (&it, mres->symbols);
  2591. while (g_hash_table_iter_next (&it, &k, &v)) {
  2592. s = v;
  2593. lua_pushstring (L, k);
  2594. lua_rawseti (L, -3, i);
  2595. lua_pushnumber (L, s->score);
  2596. lua_rawseti (L, -2, i);
  2597. i ++;
  2598. }
  2599. }
  2600. else {
  2601. lua_createtable (L, 0, 0);
  2602. lua_createtable (L, 0, 0);
  2603. }
  2604. }
  2605. else {
  2606. return luaL_error (L, "invalid arguments");
  2607. }
  2608. return 2;
  2609. }
  2610. static gint
  2611. lua_task_get_symbols_all (lua_State *L)
  2612. {
  2613. struct rspamd_task *task = lua_check_task (L, 1);
  2614. struct rspamd_metric_result *mres;
  2615. GHashTableIter it;
  2616. gpointer k, v;
  2617. gboolean found = FALSE;
  2618. gint i = 1;
  2619. if (task) {
  2620. mres = task->result;
  2621. if (mres) {
  2622. found = TRUE;
  2623. lua_createtable (L, g_hash_table_size (mres->symbols), 0);
  2624. g_hash_table_iter_init (&it, mres->symbols);
  2625. while (g_hash_table_iter_next (&it, &k, &v)) {
  2626. lua_push_symbol_result (L, task, k, v, FALSE, TRUE);
  2627. lua_rawseti (L, -2, i++);
  2628. }
  2629. }
  2630. }
  2631. else {
  2632. return luaL_error (L, "invalid arguments");
  2633. }
  2634. if (!found) {
  2635. lua_pushnil (L);
  2636. }
  2637. return 1;
  2638. }
  2639. static gint
  2640. lua_task_get_symbols_numeric (lua_State *L)
  2641. {
  2642. struct rspamd_task *task = lua_check_task (L, 1);
  2643. struct rspamd_metric_result *mres;
  2644. gint i = 1, id;
  2645. GHashTableIter it;
  2646. gpointer k, v;
  2647. struct rspamd_symbol_result *s;
  2648. if (task) {
  2649. mres = task->result;
  2650. if (mres) {
  2651. lua_createtable (L, g_hash_table_size (mres->symbols), 0);
  2652. lua_createtable (L, g_hash_table_size (mres->symbols), 0);
  2653. g_hash_table_iter_init (&it, mres->symbols);
  2654. while (g_hash_table_iter_next (&it, &k, &v)) {
  2655. id = rspamd_symbols_cache_find_symbol (task->cfg->cache,
  2656. k);
  2657. s = v;
  2658. lua_pushnumber (L, id);
  2659. lua_rawseti (L, -3, i);
  2660. lua_pushnumber (L, s->score);
  2661. lua_rawseti (L, -2, i);
  2662. i ++;
  2663. }
  2664. }
  2665. else {
  2666. lua_createtable (L, 0, 0);
  2667. lua_createtable (L, 0, 0);
  2668. }
  2669. }
  2670. else {
  2671. return luaL_error (L, "invalid arguments");
  2672. }
  2673. return 2;
  2674. }
  2675. enum lua_date_type {
  2676. DATE_CONNECT = 0,
  2677. DATE_MESSAGE,
  2678. DATE_CONNECT_STRING,
  2679. DATE_MESSAGE_STRING
  2680. };
  2681. static enum lua_date_type
  2682. lua_task_detect_date_type (struct rspamd_task *task,
  2683. lua_State *L, gint idx, gboolean *gmt)
  2684. {
  2685. enum lua_date_type type = DATE_CONNECT;
  2686. if (lua_type (L, idx) == LUA_TNUMBER) {
  2687. gint num = lua_tonumber (L, idx);
  2688. if (num >= DATE_CONNECT && num <= DATE_MESSAGE_STRING) {
  2689. return num;
  2690. }
  2691. }
  2692. else if (lua_type (L, idx) == LUA_TTABLE) {
  2693. const gchar *str;
  2694. lua_pushvalue (L, idx);
  2695. lua_pushstring (L, "format");
  2696. lua_gettable (L, -2);
  2697. str = lua_tostring (L, -1);
  2698. if (str) {
  2699. if (g_ascii_strcasecmp (str, "message") == 0) {
  2700. type = DATE_MESSAGE;
  2701. } else if (g_ascii_strcasecmp (str, "connect_str") == 0) {
  2702. type = DATE_CONNECT_STRING;
  2703. } else if (g_ascii_strcasecmp (str, "message_str") == 0) {
  2704. type = DATE_MESSAGE_STRING;
  2705. }
  2706. }
  2707. else {
  2708. msg_warn_task ("date format has not been specified");
  2709. }
  2710. lua_pop (L, 1);
  2711. lua_pushstring (L, "gmt");
  2712. lua_gettable (L, -2);
  2713. if (lua_type (L, -1) == LUA_TBOOLEAN) {
  2714. *gmt = lua_toboolean (L, -1);
  2715. }
  2716. /* Value and table */
  2717. lua_pop (L, 2);
  2718. }
  2719. return type;
  2720. }
  2721. static gint
  2722. lua_task_get_date (lua_State *L)
  2723. {
  2724. struct rspamd_task *task = lua_check_task (L, 1);
  2725. GPtrArray *hdrs;
  2726. gdouble tim;
  2727. enum lua_date_type type = DATE_CONNECT;
  2728. gboolean gmt = TRUE;
  2729. if (task != NULL) {
  2730. if (lua_gettop (L) > 1) {
  2731. type = lua_task_detect_date_type (task, L, 2, &gmt);
  2732. }
  2733. /* Get GMT date and store it to time_t */
  2734. if (type == DATE_CONNECT || type == DATE_CONNECT_STRING) {
  2735. tim = (tv_to_msec (&task->tv)) / 1000.;
  2736. if (!gmt) {
  2737. struct tm t;
  2738. time_t tt;
  2739. tt = tim;
  2740. localtime_r (&tt, &t);
  2741. #if !defined(__sun)
  2742. t.tm_gmtoff = 0;
  2743. #endif
  2744. t.tm_isdst = 0;
  2745. tim = mktime (&t);
  2746. }
  2747. }
  2748. else {
  2749. hdrs = rspamd_message_get_header_array (task, "Date",
  2750. FALSE);
  2751. if (hdrs && hdrs->len > 0) {
  2752. time_t tt;
  2753. struct tm t;
  2754. struct rspamd_mime_header *h;
  2755. h = g_ptr_array_index (hdrs, 0);
  2756. tt = rspamd_parse_smtp_date (h->decoded, strlen (h->decoded));
  2757. if (!gmt) {
  2758. localtime_r (&tt, &t);
  2759. #if !defined(__sun)
  2760. t.tm_gmtoff = 0;
  2761. #endif
  2762. t.tm_isdst = 0;
  2763. tim = mktime (&t);
  2764. }
  2765. else {
  2766. tim = tt;
  2767. }
  2768. }
  2769. else {
  2770. tim = 0.0;
  2771. }
  2772. }
  2773. if (type == DATE_CONNECT || type == DATE_MESSAGE) {
  2774. lua_pushnumber (L, tim);
  2775. }
  2776. else {
  2777. GTimeVal tv;
  2778. gchar *out;
  2779. double_to_tv (tim, &tv);
  2780. out = g_time_val_to_iso8601 (&tv);
  2781. lua_pushstring (L, out);
  2782. g_free (out);
  2783. }
  2784. }
  2785. else {
  2786. return luaL_error (L, "invalid arguments");
  2787. }
  2788. return 1;
  2789. }
  2790. static gint
  2791. lua_task_get_message_id (lua_State *L)
  2792. {
  2793. struct rspamd_task *task = lua_check_task (L, 1);
  2794. if (task != NULL) {
  2795. if (task->message_id != NULL) {
  2796. lua_pushstring (L, task->message_id);
  2797. }
  2798. else {
  2799. lua_pushnil (L);
  2800. }
  2801. }
  2802. else {
  2803. return luaL_error (L, "invalid arguments");
  2804. }
  2805. return 1;
  2806. }
  2807. static gint
  2808. lua_task_get_timeval (lua_State *L)
  2809. {
  2810. struct rspamd_task *task = lua_check_task (L, 1);
  2811. if (task != NULL) {
  2812. lua_createtable (L, 0, 2);
  2813. lua_pushstring (L, "tv_sec");
  2814. lua_pushnumber (L, (lua_Number)task->tv.tv_sec);
  2815. lua_settable (L, -3);
  2816. lua_pushstring (L, "tv_usec");
  2817. lua_pushnumber (L, (lua_Number)task->tv.tv_usec);
  2818. lua_settable (L, -3);
  2819. }
  2820. else {
  2821. return luaL_error (L, "invalid arguments");
  2822. }
  2823. return 1;
  2824. }
  2825. static gint
  2826. lua_task_get_size (lua_State *L)
  2827. {
  2828. struct rspamd_task *task = lua_check_task (L, 1);
  2829. if (task != NULL) {
  2830. lua_pushnumber (L, task->msg.len);
  2831. }
  2832. else {
  2833. return luaL_error (L, "invalid arguments");
  2834. }
  2835. return 1;
  2836. }
  2837. /**
  2838. * - `no_log`: do not log task summary
  2839. * - `no_stat`: do not include task into scanned stats
  2840. * - `pass_all`: check all filters for task
  2841. * - `extended_urls`: output extended info about urls
  2842. * - `skip`: skip task processing
  2843. */
  2844. #define LUA_TASK_FLAG_WRITE(flag, set) do { \
  2845. task->flags = (set) ? (task->flags | (flag)) : (task->flags & ~(flag)); \
  2846. } while(0)
  2847. #define LUA_TASK_SET_FLAG(flag, strname, macro, set) do { \
  2848. if (!found && strcmp ((flag), strname) == 0) { \
  2849. LUA_TASK_FLAG_WRITE((macro), set); \
  2850. found = TRUE; \
  2851. } \
  2852. } while(0)
  2853. #define LUA_TASK_FLAG_READ(flag) do { \
  2854. lua_pushboolean(L, !!(task->flags & (flag))); \
  2855. } while(0)
  2856. #define LUA_TASK_GET_FLAG(flag, strname, macro) do { \
  2857. if (!found && strcmp ((flag), strname) == 0) { \
  2858. LUA_TASK_FLAG_READ((macro)); \
  2859. found = TRUE; \
  2860. } \
  2861. } while(0)
  2862. static gint
  2863. lua_task_set_flag (lua_State *L)
  2864. {
  2865. struct rspamd_task *task = lua_check_task (L, 1);
  2866. const gchar *flag = luaL_checkstring (L, 2);
  2867. gboolean set = TRUE, found = FALSE;
  2868. if (lua_gettop (L) >= 3) {
  2869. set = lua_toboolean (L, 3);
  2870. }
  2871. if (task != NULL && flag != NULL) {
  2872. LUA_TASK_SET_FLAG (flag, "pass_all", RSPAMD_TASK_FLAG_PASS_ALL, set);
  2873. LUA_TASK_SET_FLAG (flag, "no_log", RSPAMD_TASK_FLAG_NO_LOG, set);
  2874. LUA_TASK_SET_FLAG (flag, "no_stat", RSPAMD_TASK_FLAG_NO_STAT, set);
  2875. LUA_TASK_SET_FLAG (flag, "skip", RSPAMD_TASK_FLAG_SKIP, set);
  2876. LUA_TASK_SET_FLAG (flag, "extended_urls", RSPAMD_TASK_FLAG_EXT_URLS, set);
  2877. LUA_TASK_SET_FLAG (flag, "learn_spam", RSPAMD_TASK_FLAG_LEARN_SPAM, set);
  2878. LUA_TASK_SET_FLAG (flag, "learn_ham", RSPAMD_TASK_FLAG_LEARN_HAM, set);
  2879. LUA_TASK_SET_FLAG (flag, "broken_headers",
  2880. RSPAMD_TASK_FLAG_BROKEN_HEADERS, set);
  2881. LUA_TASK_SET_FLAG (flag, "greylisted", RSPAMD_TASK_FLAG_GREYLISTED, set);
  2882. if (!found) {
  2883. msg_warn_task ("unknown flag requested: %s", flag);
  2884. }
  2885. }
  2886. else {
  2887. return luaL_error (L, "invalid arguments");
  2888. }
  2889. return 0;
  2890. }
  2891. static gint
  2892. lua_task_has_flag (lua_State *L)
  2893. {
  2894. struct rspamd_task *task = lua_check_task (L, 1);
  2895. const gchar *flag = luaL_checkstring (L, 2);
  2896. gboolean found = FALSE;
  2897. if (task != NULL && flag != NULL) {
  2898. LUA_TASK_GET_FLAG (flag, "pass_all", RSPAMD_TASK_FLAG_PASS_ALL);
  2899. LUA_TASK_GET_FLAG (flag, "no_log", RSPAMD_TASK_FLAG_NO_LOG);
  2900. LUA_TASK_GET_FLAG (flag, "no_stat", RSPAMD_TASK_FLAG_NO_STAT);
  2901. LUA_TASK_GET_FLAG (flag, "skip", RSPAMD_TASK_FLAG_SKIP);
  2902. LUA_TASK_GET_FLAG (flag, "extended_urls", RSPAMD_TASK_FLAG_EXT_URLS);
  2903. LUA_TASK_GET_FLAG (flag, "learn_spam", RSPAMD_TASK_FLAG_LEARN_SPAM);
  2904. LUA_TASK_GET_FLAG (flag, "learn_ham", RSPAMD_TASK_FLAG_LEARN_HAM);
  2905. LUA_TASK_GET_FLAG (flag, "greylisted", RSPAMD_TASK_FLAG_GREYLISTED);
  2906. LUA_TASK_GET_FLAG (flag, "broken_headers",
  2907. RSPAMD_TASK_FLAG_BROKEN_HEADERS);
  2908. if (!found) {
  2909. msg_warn_task ("unknown flag requested: %s", flag);
  2910. lua_pushboolean (L, 0);
  2911. }
  2912. }
  2913. else {
  2914. return luaL_error (L, "invalid arguments");
  2915. }
  2916. return 1;
  2917. }
  2918. static gint
  2919. lua_task_get_flags (lua_State *L)
  2920. {
  2921. struct rspamd_task *task = lua_check_task (L, 1);
  2922. gint idx = 1;
  2923. guint flags, bit, i;
  2924. if (task) {
  2925. lua_createtable (L, 8, 0);
  2926. flags = task->flags;
  2927. for (i = 0; i < sizeof (task->flags) * NBBY; i ++) {
  2928. bit = (1U << i);
  2929. if (flags & bit) {
  2930. switch (bit) {
  2931. case RSPAMD_TASK_FLAG_PASS_ALL:
  2932. lua_pushstring (L, "pass_all");
  2933. lua_rawseti (L, -2, idx ++);
  2934. break;
  2935. case RSPAMD_TASK_FLAG_NO_LOG:
  2936. lua_pushstring (L, "no_log");
  2937. lua_rawseti (L, -2, idx++);
  2938. break;
  2939. case RSPAMD_TASK_FLAG_NO_STAT:
  2940. lua_pushstring (L, "no_stat");
  2941. lua_rawseti (L, -2, idx++);
  2942. break;
  2943. case RSPAMD_TASK_FLAG_SKIP:
  2944. lua_pushstring (L, "skip");
  2945. lua_rawseti (L, -2, idx++);
  2946. break;
  2947. case RSPAMD_TASK_FLAG_EXT_URLS:
  2948. lua_pushstring (L, "extended_urls");
  2949. lua_rawseti (L, -2, idx++);
  2950. break;
  2951. case RSPAMD_TASK_FLAG_BROKEN_HEADERS:
  2952. lua_pushstring (L, "broken_headers");
  2953. lua_rawseti (L, -2, idx++);
  2954. break;
  2955. case RSPAMD_TASK_FLAG_LEARN_SPAM:
  2956. lua_pushstring (L, "learn_spam");
  2957. lua_rawseti (L, -2, idx++);
  2958. break;
  2959. case RSPAMD_TASK_FLAG_LEARN_HAM:
  2960. lua_pushstring (L, "learn_ham");
  2961. lua_rawseti (L, -2, idx++);
  2962. break;
  2963. case RSPAMD_TASK_FLAG_GREYLISTED:
  2964. lua_pushstring (L, "greylisted");
  2965. lua_rawseti (L, -2, idx++);
  2966. break;
  2967. default:
  2968. break;
  2969. }
  2970. }
  2971. }
  2972. }
  2973. else {
  2974. return luaL_error (L, "invalid arguments");
  2975. }
  2976. return 1;
  2977. }
  2978. static gint
  2979. lua_task_get_digest (lua_State *L)
  2980. {
  2981. struct rspamd_task *task = lua_check_task (L, 1);
  2982. gchar hexbuf[33];
  2983. gint r;
  2984. if (task) {
  2985. r = rspamd_encode_hex_buf (task->digest, sizeof (task->digest),
  2986. hexbuf, sizeof (hexbuf) - 1);
  2987. if (r > 0) {
  2988. hexbuf[r] = '\0';
  2989. lua_pushstring (L, hexbuf);
  2990. }
  2991. else {
  2992. lua_pushnil (L);
  2993. }
  2994. }
  2995. else {
  2996. return luaL_error (L, "invalid arguments");
  2997. }
  2998. return 1;
  2999. }
  3000. static gint
  3001. lua_task_learn (lua_State *L)
  3002. {
  3003. struct rspamd_task *task = lua_check_task (L, 1);
  3004. gboolean is_spam = FALSE;
  3005. const gchar *clname = NULL;
  3006. GError *err = NULL;
  3007. int ret = 1;
  3008. if (task == NULL) {
  3009. return luaL_error (L, "invalid arguments");
  3010. }
  3011. is_spam = lua_toboolean(L, 2);
  3012. if (lua_gettop (L) > 2) {
  3013. clname = luaL_checkstring (L, 3);
  3014. }
  3015. if (!rspamd_learn_task_spam (task, is_spam, clname, &err)) {
  3016. lua_pushboolean (L, FALSE);
  3017. if (err != NULL) {
  3018. lua_pushstring (L, err->message);
  3019. ret = 2;
  3020. }
  3021. }
  3022. else {
  3023. lua_pushboolean (L, TRUE);
  3024. }
  3025. return ret;
  3026. }
  3027. static gint
  3028. lua_task_set_settings (lua_State *L)
  3029. {
  3030. struct rspamd_task *task = lua_check_task (L, 1);
  3031. ucl_object_t *settings;
  3032. const ucl_object_t *act, *elt, *metric_elt;
  3033. struct rspamd_metric_result *mres;
  3034. guint i;
  3035. settings = ucl_object_lua_import (L, 2);
  3036. if (settings != NULL && task != NULL) {
  3037. metric_elt = ucl_object_lookup (settings, DEFAULT_METRIC);
  3038. if (metric_elt) {
  3039. task->settings = ucl_object_ref (metric_elt);
  3040. ucl_object_unref (settings);
  3041. }
  3042. else {
  3043. task->settings = settings;
  3044. }
  3045. act = ucl_object_lookup (task->settings, "actions");
  3046. if (act) {
  3047. /* Adjust desired actions */
  3048. mres = task->result;
  3049. if (mres == NULL) {
  3050. mres = rspamd_create_metric_result (task);
  3051. }
  3052. for (i = 0; i < METRIC_ACTION_MAX; i++) {
  3053. elt = ucl_object_lookup_any (act, rspamd_action_to_str (i),
  3054. rspamd_action_to_str_alt (i), NULL);
  3055. if (elt) {
  3056. mres->actions_limits[i] = ucl_object_todouble (elt);
  3057. msg_debug_task ("adjusted action %s to %.2f",
  3058. ucl_object_key (elt), mres->actions_limits[i]);
  3059. }
  3060. }
  3061. }
  3062. rspamd_symbols_cache_process_settings (task, task->cfg->cache);
  3063. }
  3064. else {
  3065. return luaL_error (L, "invalid arguments");
  3066. }
  3067. return 0;
  3068. }
  3069. static gint
  3070. lua_task_set_rmilter_reply (lua_State *L)
  3071. {
  3072. struct rspamd_task *task = lua_check_task (L, 1);
  3073. ucl_object_t *reply;
  3074. reply = ucl_object_lua_import (L, 2);
  3075. if (reply != NULL && task != NULL) {
  3076. rspamd_mempool_set_variable (task->task_pool,
  3077. RSPAMD_MEMPOOL_RMILTER_REPLY,
  3078. reply, (rspamd_mempool_destruct_t)ucl_object_unref);
  3079. }
  3080. else {
  3081. return luaL_error (L, "invalid arguments");
  3082. }
  3083. return 0;
  3084. }
  3085. static gint
  3086. lua_task_get_settings (lua_State *L)
  3087. {
  3088. struct rspamd_task *task = lua_check_task (L, 1);
  3089. if (task != NULL) {
  3090. if (task->settings) {
  3091. return ucl_object_push_lua (L, task->settings, true);
  3092. }
  3093. else {
  3094. lua_pushnil (L);
  3095. }
  3096. }
  3097. else {
  3098. return luaL_error (L, "invalid arguments");
  3099. }
  3100. return 1;
  3101. }
  3102. static gint
  3103. lua_task_lookup_settings (lua_State *L)
  3104. {
  3105. struct rspamd_task *task = lua_check_task (L, 1);
  3106. const gchar *key = NULL;
  3107. const ucl_object_t *elt;
  3108. if (task != NULL) {
  3109. if (lua_isstring (L, 2)) {
  3110. key = lua_tostring (L, 2);
  3111. }
  3112. if (task->settings) {
  3113. if (key == NULL) {
  3114. return ucl_object_push_lua (L, task->settings, true);
  3115. }
  3116. else {
  3117. elt = ucl_object_lookup (task->settings, key);
  3118. if (elt) {
  3119. return ucl_object_push_lua (L, elt, true);
  3120. }
  3121. else {
  3122. lua_pushnil (L);
  3123. }
  3124. }
  3125. }
  3126. else {
  3127. lua_pushnil (L);
  3128. }
  3129. }
  3130. else {
  3131. return luaL_error (L, "invalid arguments");
  3132. }
  3133. return 1;
  3134. }
  3135. static gint
  3136. lua_task_get_settings_id (lua_State *L)
  3137. {
  3138. struct rspamd_task *task = lua_check_task (L, 1);
  3139. guint32 *hp;
  3140. if (task != NULL) {
  3141. hp = rspamd_mempool_get_variable (task->task_pool, "settings_hash");
  3142. if (hp) {
  3143. lua_pushnumber (L, *hp);
  3144. }
  3145. else {
  3146. lua_pushnil (L);
  3147. }
  3148. }
  3149. else {
  3150. return luaL_error (L, "invalid arguments");
  3151. }
  3152. return 1;
  3153. }
  3154. static gint
  3155. lua_task_cache_get (lua_State *L)
  3156. {
  3157. struct rspamd_task *task = lua_check_task (L, 1);
  3158. const gchar *key = luaL_checkstring (L, 2);
  3159. if (task && key) {
  3160. if (!lua_task_get_cached (L, task, key)) {
  3161. lua_pushnil (L);
  3162. }
  3163. }
  3164. else {
  3165. luaL_error (L, "invalid arguments");
  3166. }
  3167. return 1;
  3168. }
  3169. static gint
  3170. lua_task_cache_set (lua_State *L)
  3171. {
  3172. struct rspamd_task *task = lua_check_task (L, 1);
  3173. const gchar *key = luaL_checkstring (L, 2);
  3174. if (task && key && lua_gettop (L) >= 3) {
  3175. lua_task_set_cached (L, task, key, 3);
  3176. }
  3177. else {
  3178. luaL_error (L, "invalid arguments");
  3179. }
  3180. return 0;
  3181. }
  3182. struct lua_file_cbdata {
  3183. gchar *fname;
  3184. gint fd;
  3185. };
  3186. static void
  3187. lua_tmp_file_dtor (gpointer p)
  3188. {
  3189. struct lua_file_cbdata *cbdata = p;
  3190. unlink (cbdata->fname);
  3191. close (cbdata->fd);
  3192. }
  3193. static gint
  3194. lua_task_store_in_file (lua_State *L)
  3195. {
  3196. struct rspamd_task *task = lua_check_task (L, 1);
  3197. gchar fpath[PATH_MAX];
  3198. guint mode = 00600;
  3199. gint fd;
  3200. struct lua_file_cbdata *cbdata;
  3201. if (task) {
  3202. if ((task->flags & RSPAMD_TASK_FLAG_FILE) && task->msg.fpath) {
  3203. lua_pushstring (L, task->msg.fpath);
  3204. }
  3205. else {
  3206. if (lua_isnumber (L, 2)) {
  3207. mode = lua_tonumber (L, 2);
  3208. }
  3209. rspamd_snprintf (fpath, sizeof (fpath), "%s%c%s", task->cfg->temp_dir,
  3210. G_DIR_SEPARATOR, "rmsg-XXXXXXXXXX");
  3211. fd = mkstemp (fpath);
  3212. if (fd == -1) {
  3213. msg_err_task ("cannot save file: %s", strerror (errno));
  3214. lua_pushnil (L);
  3215. }
  3216. else {
  3217. fchmod (fd, mode);
  3218. if (write (fd, task->msg.begin, task->msg.len) == -1) {
  3219. msg_err_task ("cannot write file %s: %s", fpath,
  3220. strerror (errno));
  3221. unlink (fpath);
  3222. close (fd);
  3223. lua_pushnil (L);
  3224. return 1;
  3225. }
  3226. cbdata = rspamd_mempool_alloc (task->task_pool, sizeof (*cbdata));
  3227. cbdata->fd = fd;
  3228. cbdata->fname = rspamd_mempool_strdup (task->task_pool, fpath);
  3229. lua_pushstring (L, cbdata->fname);
  3230. rspamd_mempool_add_destructor (task->task_pool,
  3231. lua_tmp_file_dtor, cbdata);
  3232. }
  3233. }
  3234. }
  3235. else {
  3236. luaL_error (L, "invalid arguments");
  3237. }
  3238. return 1;
  3239. }
  3240. static gint
  3241. lua_task_process_regexp (lua_State *L)
  3242. {
  3243. struct rspamd_task *task = lua_check_task (L, 1);
  3244. struct rspamd_lua_regexp *re = NULL;
  3245. gboolean strong = FALSE;
  3246. const gchar *type_str = NULL, *header_str = NULL;
  3247. gsize header_len = 0;
  3248. GError *err = NULL;
  3249. gint ret = 0;
  3250. enum rspamd_re_type type = RSPAMD_RE_BODY;
  3251. /*
  3252. * - `re`* : regular expression object
  3253. * - `type`*: type of regular expression:
  3254. * + `mime`: mime regexp
  3255. * + `rawmime`: raw mime regexp
  3256. * + `header`: header regexp
  3257. * + `rawheader`: raw header expression
  3258. * + `body`: raw body regexp
  3259. * + `url`: url regexp
  3260. * - `header`: for header and rawheader regexp means the name of header
  3261. * - `strong`: case sensitive match for headers
  3262. */
  3263. if (task != NULL) {
  3264. if (!rspamd_lua_parse_table_arguments (L, 2, &err,
  3265. "*re=U{regexp};*type=S;header=V;strong=B",
  3266. &re, &type_str, &header_len, &header_str,
  3267. &strong)) {
  3268. msg_err_task ("cannot get parameters list: %e", err);
  3269. if (err) {
  3270. g_error_free (err);
  3271. }
  3272. }
  3273. else {
  3274. type = rspamd_re_cache_type_from_string (type_str);
  3275. if ((type == RSPAMD_RE_HEADER || type == RSPAMD_RE_RAWHEADER)
  3276. && header_str == NULL) {
  3277. msg_err_task (
  3278. "header argument is mandatory for header/rawheader regexps");
  3279. }
  3280. else {
  3281. ret = rspamd_re_cache_process (task, re->re, type,
  3282. (gpointer) header_str, header_len, strong);
  3283. }
  3284. }
  3285. }
  3286. else {
  3287. return luaL_error (L, "invalid arguments");
  3288. }
  3289. lua_pushnumber (L, ret);
  3290. return 1;
  3291. }
  3292. static gint
  3293. lua_task_get_metric_score (lua_State *L)
  3294. {
  3295. struct rspamd_task *task = lua_check_task (L, 1);
  3296. gdouble rs;
  3297. struct rspamd_metric_result *metric_res;
  3298. if (task) {
  3299. if ((metric_res = task->result) != NULL) {
  3300. lua_createtable (L, 2, 0);
  3301. lua_pushnumber (L, isnan (metric_res->score) ? 0.0 : metric_res->score);
  3302. rs = rspamd_task_get_required_score (task, metric_res);
  3303. lua_rawseti (L, -2, 1);
  3304. lua_pushnumber (L, rs);
  3305. lua_rawseti (L, -2, 2);
  3306. }
  3307. else {
  3308. lua_pushnil (L);
  3309. }
  3310. }
  3311. else {
  3312. return luaL_error (L, "invalid arguments");
  3313. }
  3314. return 1;
  3315. }
  3316. static gint
  3317. lua_task_get_metric_action (lua_State *L)
  3318. {
  3319. struct rspamd_task *task = lua_check_task (L, 1);
  3320. struct rspamd_metric_result *metric_res;
  3321. enum rspamd_metric_action action;
  3322. if (task) {
  3323. if ((metric_res = task->result) != NULL) {
  3324. action = rspamd_check_action_metric (task, metric_res);
  3325. lua_pushstring (L, rspamd_action_to_str (action));
  3326. }
  3327. else {
  3328. lua_pushnil (L);
  3329. }
  3330. }
  3331. else {
  3332. return luaL_error (L, "invalid arguments");
  3333. }
  3334. return 1;
  3335. }
  3336. static gint
  3337. lua_task_set_metric_score (lua_State *L)
  3338. {
  3339. struct rspamd_task *task = lua_check_task (L, 1);
  3340. struct rspamd_metric_result *metric_res;
  3341. gdouble nscore;
  3342. if (lua_isnumber (L, 2)) {
  3343. nscore = luaL_checknumber (L, 2);
  3344. }
  3345. else {
  3346. nscore = luaL_checknumber (L, 3);
  3347. }
  3348. if (task) {
  3349. if ((metric_res = task->result) != NULL) {
  3350. msg_debug_task ("set metric score from %.2f to %.2f",
  3351. metric_res->score, nscore);
  3352. metric_res->score = nscore;
  3353. lua_pushboolean (L, true);
  3354. }
  3355. else {
  3356. lua_pushboolean (L, false);
  3357. }
  3358. }
  3359. else {
  3360. return luaL_error (L, "invalid arguments");
  3361. }
  3362. return 1;
  3363. }
  3364. static gint
  3365. lua_task_set_metric_action (lua_State *L)
  3366. {
  3367. struct rspamd_task *task = lua_check_task (L, 1);
  3368. const gchar *metric_name, *action_name;
  3369. struct rspamd_metric_result *metric_res;
  3370. gint action;
  3371. metric_name = luaL_checkstring (L, 2);
  3372. if (metric_name == NULL) {
  3373. metric_name = DEFAULT_METRIC;
  3374. }
  3375. action_name = luaL_checkstring (L, 3);
  3376. if (task && metric_name && action_name) {
  3377. if ((metric_res = task->result) != NULL) {
  3378. if (rspamd_action_from_str (action_name, &action)) {
  3379. metric_res->action = action;
  3380. lua_pushboolean (L, true);
  3381. }
  3382. else {
  3383. lua_pushboolean (L, false);
  3384. }
  3385. }
  3386. else {
  3387. lua_pushboolean (L, false);
  3388. }
  3389. }
  3390. else {
  3391. return luaL_error (L, "invalid arguments");
  3392. }
  3393. return 1;
  3394. }
  3395. static gint
  3396. lua_task_set_metric_subject (lua_State *L)
  3397. {
  3398. struct rspamd_task *task = lua_check_task (L, 1);
  3399. const gchar *subject;
  3400. struct rspamd_metric *metric;
  3401. metric = task->cfg->default_metric;
  3402. subject = luaL_checkstring (L, 2);
  3403. if (task && metric && subject) {
  3404. metric->subject = rspamd_mempool_strdup (task->task_pool, subject);
  3405. lua_pushboolean (L, true);
  3406. }
  3407. else {
  3408. return luaL_error (L, "invalid arguments");
  3409. }
  3410. return 1;
  3411. }
  3412. static gint
  3413. lua_task_get_protocol_reply (lua_State *L)
  3414. {
  3415. struct rspamd_task *task = lua_check_task (L, 1);
  3416. guint flags = 0;
  3417. ucl_object_t *obj;
  3418. if (!task) {
  3419. return luaL_error (L, "invalid arguments");
  3420. }
  3421. if (!(task->processed_stages & (RSPAMD_TASK_STAGE_POST_FILTERS >> 1))) {
  3422. return luaL_error (L, "must not be called before post-filters");
  3423. }
  3424. if (lua_istable (L, 2)) {
  3425. for (lua_pushnil (L); lua_next (L, 2); lua_pop (L, 1)) {
  3426. if (lua_isstring (L, -1)) {
  3427. const gchar *str = lua_tostring (L, -1);
  3428. if (strcmp (str, "default") == 0) {
  3429. flags |= RSPAMD_PROTOCOL_DEFAULT;
  3430. }
  3431. else if (strcmp (str, "basic") == 0) {
  3432. flags |= RSPAMD_PROTOCOL_BASIC;
  3433. }
  3434. else if (strcmp (str, "metrics") == 0) {
  3435. flags |= RSPAMD_PROTOCOL_METRICS;
  3436. }
  3437. else if (strcmp (str, "messages") == 0) {
  3438. flags |= RSPAMD_PROTOCOL_MESSAGES;
  3439. }
  3440. else if (strcmp (str, "rmilter") == 0) {
  3441. flags |= RSPAMD_PROTOCOL_RMILTER;
  3442. }
  3443. else if (strcmp (str, "dkim") == 0) {
  3444. flags |= RSPAMD_PROTOCOL_DKIM;
  3445. }
  3446. else if (strcmp (str, "extra") == 0) {
  3447. flags |= RSPAMD_PROTOCOL_EXTRA;
  3448. }
  3449. else {
  3450. msg_err_task ("invalid protocol flag: %s", str);
  3451. }
  3452. }
  3453. }
  3454. }
  3455. else {
  3456. flags = RSPAMD_PROTOCOL_DEFAULT;
  3457. }
  3458. obj = rspamd_protocol_write_ucl (task, flags);
  3459. if (obj) {
  3460. ucl_object_push_lua (L, obj, true);
  3461. }
  3462. else {
  3463. lua_pushnil (L);
  3464. }
  3465. return 1;
  3466. }
  3467. static gint
  3468. lua_task_headers_foreach (lua_State *L)
  3469. {
  3470. struct rspamd_task *task = lua_check_task (L, 1);
  3471. gboolean full = FALSE, raw = FALSE;
  3472. struct rspamd_lua_regexp *re = NULL;
  3473. GList *cur;
  3474. struct rspamd_mime_header *hdr;
  3475. gint old_top;
  3476. if (task && lua_isfunction (L, 2)) {
  3477. if (lua_istable (L, 3)) {
  3478. lua_pushstring (L, "full");
  3479. lua_gettable (L, 3);
  3480. if (lua_isboolean (L, -1)) {
  3481. full = lua_toboolean (L, -1);
  3482. }
  3483. lua_pop (L, 1);
  3484. lua_pushstring (L, "raw");
  3485. lua_gettable (L, 3);
  3486. if (lua_isboolean (L, -1)) {
  3487. raw = lua_toboolean (L, -1);
  3488. }
  3489. lua_pop (L, 1);
  3490. lua_pushstring (L, "regexp");
  3491. lua_gettable (L, 3);
  3492. if (lua_isuserdata (L, -1)) {
  3493. re = *(struct rspamd_lua_regexp **)
  3494. rspamd_lua_check_udata (L, -1, "rspamd{regexp}");
  3495. }
  3496. lua_pop (L, 1);
  3497. }
  3498. if (task->headers_order) {
  3499. cur = task->headers_order->head;
  3500. while (cur) {
  3501. hdr = cur->data;
  3502. if (re && re->re) {
  3503. if (!rspamd_regexp_match (re->re, hdr->name,
  3504. strlen (hdr->name),FALSE)) {
  3505. cur = g_list_next (cur);
  3506. continue;
  3507. }
  3508. }
  3509. old_top = lua_gettop (L);
  3510. lua_pushvalue (L, 2);
  3511. lua_pushstring (L, hdr->name);
  3512. rspamd_lua_push_header (L, hdr, full, raw);
  3513. if (lua_pcall (L, 2, LUA_MULTRET, 0) != 0) {
  3514. msg_err ("call to header_foreach failed: %s",
  3515. lua_tostring (L, -1));
  3516. lua_settop (L, old_top);
  3517. break;
  3518. }
  3519. else {
  3520. if (lua_gettop (L) > old_top) {
  3521. if (lua_isboolean (L, old_top + 1)) {
  3522. if (lua_toboolean (L, old_top + 1)) {
  3523. lua_settop (L, old_top);
  3524. break;
  3525. }
  3526. }
  3527. }
  3528. }
  3529. lua_settop (L, old_top);
  3530. cur = g_list_next (cur);
  3531. }
  3532. }
  3533. }
  3534. return 0;
  3535. }
  3536. /* Image functions */
  3537. static gint
  3538. lua_image_get_width (lua_State *L)
  3539. {
  3540. struct rspamd_image *img = lua_check_image (L);
  3541. if (img != NULL) {
  3542. lua_pushnumber (L, img->width);
  3543. }
  3544. else {
  3545. return luaL_error (L, "invalid arguments");
  3546. }
  3547. return 1;
  3548. }
  3549. static gint
  3550. lua_image_get_height (lua_State *L)
  3551. {
  3552. struct rspamd_image *img = lua_check_image (L);
  3553. if (img != NULL) {
  3554. lua_pushnumber (L, img->height);
  3555. }
  3556. else {
  3557. return luaL_error (L, "invalid arguments");
  3558. }
  3559. return 1;
  3560. }
  3561. static gint
  3562. lua_image_get_type (lua_State *L)
  3563. {
  3564. struct rspamd_image *img = lua_check_image (L);
  3565. if (img != NULL) {
  3566. lua_pushstring (L, rspamd_image_type_str (img->type));
  3567. }
  3568. else {
  3569. return luaL_error (L, "invalid arguments");
  3570. }
  3571. return 1;
  3572. }
  3573. static gint
  3574. lua_image_get_size (lua_State *L)
  3575. {
  3576. struct rspamd_image *img = lua_check_image (L);
  3577. if (img != NULL) {
  3578. lua_pushnumber (L, img->data->len);
  3579. }
  3580. else {
  3581. return luaL_error (L, "invalid arguments");
  3582. }
  3583. return 1;
  3584. }
  3585. static gint
  3586. lua_image_get_filename (lua_State *L)
  3587. {
  3588. struct rspamd_image *img = lua_check_image (L);
  3589. if (img != NULL && img->filename != NULL) {
  3590. lua_pushlstring (L, img->filename->begin, img->filename->len);
  3591. }
  3592. else {
  3593. return luaL_error (L, "invalid arguments");
  3594. }
  3595. return 1;
  3596. }
  3597. /* Arvhive methods */
  3598. static gint
  3599. lua_archive_get_type (lua_State *L)
  3600. {
  3601. struct rspamd_archive *arch = lua_check_archive (L);
  3602. if (arch != NULL) {
  3603. lua_pushstring (L, rspamd_archive_type_str (arch->type));
  3604. }
  3605. else {
  3606. return luaL_error (L, "invalid arguments");
  3607. }
  3608. return 1;
  3609. }
  3610. static gint
  3611. lua_archive_get_files (lua_State *L)
  3612. {
  3613. struct rspamd_archive *arch = lua_check_archive (L);
  3614. guint i;
  3615. struct rspamd_archive_file *f;
  3616. if (arch != NULL) {
  3617. lua_createtable (L, arch->files->len, 0);
  3618. for (i = 0; i < arch->files->len; i ++) {
  3619. f = g_ptr_array_index (arch->files, i);
  3620. lua_pushlstring (L, f->fname->str, f->fname->len);
  3621. lua_rawseti (L, -2, i + 1);
  3622. }
  3623. }
  3624. else {
  3625. return luaL_error (L, "invalid arguments");
  3626. }
  3627. return 1;
  3628. }
  3629. static gint
  3630. lua_archive_get_files_full (lua_State *L)
  3631. {
  3632. struct rspamd_archive *arch = lua_check_archive (L);
  3633. guint i;
  3634. struct rspamd_archive_file *f;
  3635. if (arch != NULL) {
  3636. lua_createtable (L, arch->files->len, 0);
  3637. for (i = 0; i < arch->files->len; i ++) {
  3638. f = g_ptr_array_index (arch->files, i);
  3639. lua_createtable (L, 0, 4);
  3640. lua_pushstring (L, "name");
  3641. lua_pushlstring (L, f->fname->str, f->fname->len);
  3642. lua_settable (L, -3);
  3643. lua_pushstring (L, "compressed_size");
  3644. lua_pushnumber (L, f->compressed_size);
  3645. lua_settable (L, -3);
  3646. lua_pushstring (L, "uncompressed_size");
  3647. lua_pushnumber (L, f->uncompressed_size);
  3648. lua_settable (L, -3);
  3649. lua_pushstring (L, "encrypted");
  3650. lua_pushboolean (L, (f->flags & RSPAMD_ARCHIVE_FILE_ENCRYPTED) ? true : false);
  3651. lua_settable (L, -3);
  3652. lua_rawseti (L, -2, i + 1);
  3653. }
  3654. }
  3655. else {
  3656. return luaL_error (L, "invalid arguments");
  3657. }
  3658. return 1;
  3659. }
  3660. static gint
  3661. lua_archive_is_encrypted (lua_State *L)
  3662. {
  3663. struct rspamd_archive *arch = lua_check_archive (L);
  3664. if (arch != NULL) {
  3665. lua_pushboolean (L, (arch->flags & RSPAMD_ARCHIVE_ENCRYPTED) ? true : false);
  3666. }
  3667. else {
  3668. return luaL_error (L, "invalid arguments");
  3669. }
  3670. return 1;
  3671. }
  3672. static gint
  3673. lua_archive_get_size (lua_State *L)
  3674. {
  3675. struct rspamd_archive *arch = lua_check_archive (L);
  3676. if (arch != NULL) {
  3677. lua_pushnumber (L, arch->size);
  3678. }
  3679. else {
  3680. return luaL_error (L, "invalid arguments");
  3681. }
  3682. return 1;
  3683. }
  3684. static gint
  3685. lua_archive_get_filename (lua_State *L)
  3686. {
  3687. struct rspamd_archive *arch = lua_check_archive (L);
  3688. if (arch != NULL) {
  3689. lua_pushlstring (L, arch->archive_name->begin, arch->archive_name->len);
  3690. }
  3691. else {
  3692. return luaL_error (L, "invalid arguments");
  3693. }
  3694. return 1;
  3695. }
  3696. /* Text methods */
  3697. static gint
  3698. lua_text_len (lua_State *L)
  3699. {
  3700. struct rspamd_lua_text *t = lua_check_text (L, 1);
  3701. gsize l = 0;
  3702. if (t != NULL) {
  3703. l = t->len;
  3704. }
  3705. else {
  3706. return luaL_error (L, "invalid arguments");
  3707. }
  3708. lua_pushnumber (L, l);
  3709. return 1;
  3710. }
  3711. static gint
  3712. lua_text_str (lua_State *L)
  3713. {
  3714. struct rspamd_lua_text *t = lua_check_text (L, 1);
  3715. if (t != NULL) {
  3716. lua_pushlstring (L, t->start, t->len);
  3717. }
  3718. else {
  3719. return luaL_error (L, "invalid arguments");
  3720. }
  3721. return 1;
  3722. }
  3723. static gint
  3724. lua_text_ptr (lua_State *L)
  3725. {
  3726. struct rspamd_lua_text *t = lua_check_text (L, 1);
  3727. if (t != NULL) {
  3728. lua_pushlightuserdata (L, (gpointer)t->start);
  3729. }
  3730. else {
  3731. return luaL_error (L, "invalid arguments");
  3732. }
  3733. return 1;
  3734. }
  3735. static gint
  3736. lua_text_take_ownership (lua_State *L)
  3737. {
  3738. struct rspamd_lua_text *t = lua_check_text (L, 1);
  3739. gchar *dest;
  3740. if (t != NULL) {
  3741. if (t->flags & RSPAMD_TEXT_FLAG_OWN) {
  3742. /* We already own it */
  3743. lua_pushboolean (L, true);
  3744. }
  3745. else {
  3746. dest = g_malloc (t->len);
  3747. memcpy (dest, t->start, t->len);
  3748. t->start = dest;
  3749. t->flags |= RSPAMD_TEXT_FLAG_OWN;
  3750. lua_pushboolean (L, true);
  3751. }
  3752. }
  3753. else {
  3754. return luaL_error (L, "invalid arguments");
  3755. }
  3756. return 1;
  3757. }
  3758. static gint
  3759. lua_text_save_in_file (lua_State *L)
  3760. {
  3761. struct rspamd_lua_text *t = lua_check_text (L, 1);
  3762. const gchar *fname = luaL_checkstring (L, 2);
  3763. guint mode = 00644;
  3764. gint fd;
  3765. if (t != NULL && fname != NULL) {
  3766. if (lua_type (L, 3) == LUA_TNUMBER) {
  3767. mode = lua_tonumber (L, 3);
  3768. }
  3769. fd = rspamd_file_xopen (fname, O_CREAT | O_WRONLY | O_EXCL, mode, 0);
  3770. if (fd == -1) {
  3771. lua_pushboolean (L, false);
  3772. lua_pushstring (L, strerror (errno));
  3773. return 2;
  3774. }
  3775. if (write (fd, t->start, t->len) == -1) {
  3776. close (fd);
  3777. lua_pushboolean (L, false);
  3778. lua_pushstring (L, strerror (errno));
  3779. return 2;
  3780. }
  3781. close (fd);
  3782. lua_pushboolean (L, true);
  3783. }
  3784. else {
  3785. return luaL_error (L, "invalid arguments");
  3786. }
  3787. return 1;
  3788. }
  3789. static gint
  3790. lua_text_gc (lua_State *L)
  3791. {
  3792. struct rspamd_lua_text *t = lua_check_text (L, 1);
  3793. if (t != NULL) {
  3794. if (t->flags & RSPAMD_TEXT_FLAG_OWN) {
  3795. if (t->flags & RSPAMD_TEXT_FLAG_MMAPED) {
  3796. munmap ((gpointer)t->start, t->len);
  3797. }
  3798. else {
  3799. g_free ((gpointer)t->start);
  3800. }
  3801. }
  3802. }
  3803. return 0;
  3804. }
  3805. /* Init part */
  3806. static gint
  3807. lua_load_task (lua_State * L)
  3808. {
  3809. lua_newtable (L);
  3810. luaL_register (L, NULL, tasklib_f);
  3811. return 1;
  3812. }
  3813. static void
  3814. luaopen_archive (lua_State * L)
  3815. {
  3816. rspamd_lua_new_class (L, "rspamd{archive}", archivelib_m);
  3817. lua_pop (L, 1);
  3818. }
  3819. void
  3820. luaopen_task (lua_State * L)
  3821. {
  3822. rspamd_lua_new_class (L, "rspamd{task}", tasklib_m);
  3823. lua_pop (L, 1);
  3824. rspamd_lua_add_preload (L, "rspamd_task", lua_load_task);
  3825. luaopen_archive (L);
  3826. }
  3827. void
  3828. luaopen_image (lua_State * L)
  3829. {
  3830. rspamd_lua_new_class (L, "rspamd{image}", imagelib_m);
  3831. lua_pop (L, 1);
  3832. }
  3833. void
  3834. luaopen_text (lua_State *L)
  3835. {
  3836. rspamd_lua_new_class (L, "rspamd{text}", textlib_m);
  3837. lua_pop (L, 1);
  3838. }
  3839. void
  3840. rspamd_lua_task_push (lua_State *L, struct rspamd_task *task)
  3841. {
  3842. struct rspamd_task **ptask;
  3843. ptask = lua_newuserdata (L, sizeof (gpointer));
  3844. rspamd_lua_setclass (L, "rspamd{task}", -1);
  3845. *ptask = task;
  3846. }