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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996
  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 "libserver/dkim.h"
  25. #include "libserver/task.h"
  26. #include "libserver/cfg_file_private.h"
  27. #include "libmime/filter_private.h"
  28. #include "libstat/stat_api.h"
  29. #include "libutil/map_helpers.h"
  30. #include <math.h>
  31. /***
  32. * @module rspamd_task
  33. * This module provides routines for tasks manipulation in rspamd. Tasks usually
  34. * represent messages being scanned, and this API provides access to such elements
  35. * as headers, symbols, metrics and so on and so forth. Normally, task objects
  36. * are passed to the lua callbacks allowing to check specific properties of messages
  37. * and add the corresponding symbols to the scan's results.
  38. @example
  39. rspamd_config.DATE_IN_PAST = function(task)
  40. local dm = task:get_date{format = 'message', gmt = true}
  41. local dt = task:get_date{format = 'connect', gmt = true}
  42. -- A day
  43. if dt - dm > 86400 then
  44. return true
  45. end
  46. return false
  47. end
  48. */
  49. /* Task methods */
  50. /***
  51. * @function rspamd_task.create([cfg])
  52. * Create a new empty task
  53. * @return {rspamd_task} new task
  54. */
  55. LUA_FUNCTION_DEF (task, create);
  56. /***
  57. * @function rspamd_task.load_from_file(filename[, cfg])
  58. * Loads a message from specific file
  59. * @return {boolean,rspamd_task|error} status + new task or error message
  60. */
  61. LUA_FUNCTION_DEF (task, load_from_file);
  62. /***
  63. * @function rspamd_task.load_from_string(message[, cfg])
  64. * Loads a message from specific file
  65. * @return {boolean,rspamd_task|error} status + new task or error message
  66. */
  67. LUA_FUNCTION_DEF (task, load_from_string);
  68. LUA_FUNCTION_DEF (task, get_message);
  69. /***
  70. * @method task:process_message()
  71. * Parses message
  72. */
  73. LUA_FUNCTION_DEF (task, process_message);
  74. /***
  75. * @method task:get_cfg()
  76. * Get configuration object for a task.
  77. * @return {rspamd_config} (config.md)[configuration object] for the task
  78. */
  79. LUA_FUNCTION_DEF (task, get_cfg);
  80. LUA_FUNCTION_DEF (task, set_cfg);
  81. LUA_FUNCTION_DEF (task, destroy);
  82. /***
  83. * @method task:get_mempool()
  84. * Returns memory pool valid for a lifetime of task. It is used internally by
  85. * many rspamd routines.
  86. * @return {rspamd_mempool} memory pool object
  87. */
  88. LUA_FUNCTION_DEF (task, get_mempool);
  89. /***
  90. * @method task:get_session()
  91. * Returns asynchronous session object that is used by many rspamd asynchronous
  92. * utilities internally.
  93. * @return {rspamd_session} session object
  94. */
  95. LUA_FUNCTION_DEF (task, get_session);
  96. /***
  97. * @method task:set_session(session)
  98. * Sets new async session for a task
  99. */
  100. LUA_FUNCTION_DEF (task, set_session);
  101. /***
  102. * @method task:get_ev_base()
  103. * Return asynchronous event base for using in callbacks and resolver.
  104. * @return {rspamd_ev_base} event base
  105. */
  106. LUA_FUNCTION_DEF (task, get_ev_base);
  107. /***
  108. * @method task:get_worker()
  109. * Returns a worker object associated with the task
  110. * @return {rspamd_worker} worker object
  111. */
  112. LUA_FUNCTION_DEF (task, get_worker);
  113. /***
  114. * @method task:insert_result([enforce_symbol,]symbol, weight[, option1, ...])
  115. * Insert specific symbol to the tasks scanning results assigning the initial
  116. * weight to it.
  117. * @param {boolean} enforce_symbol if represented and true, then insert symbol even if it is not registered in the metric
  118. * @param {string} symbol symbol to insert
  119. * @param {number} weight initial weight (this weight is multiplied by the metric weight)
  120. * @param {string} options list of optional options attached to a symbol inserted
  121. @example
  122. local function cb(task)
  123. if task:get_header('Some header') then
  124. task:insert_result('SOME_HEADER', 1.0, 'Got some header')
  125. end
  126. end
  127. */
  128. LUA_FUNCTION_DEF (task, insert_result);
  129. /***
  130. * @method task:adjust_result(symbol, score[, option1, ...])
  131. * Alters the existing symbol's score to a new score. It is not affected by
  132. * metric score or grow factor. You can also add new options
  133. * using this method. Symbol must be already inserted into metric or an error
  134. * will be emitted.
  135. * @param {string} symbol symbol to adjust
  136. * @param {number} score this value is NOT multiplied by the metric score
  137. * @param {string/table} options list of optional options attached to a symbol adjusted
  138. */
  139. LUA_FUNCTION_DEF (task, adjust_result);
  140. /***
  141. * @method task:set_pre_result(action, description)
  142. * Sets pre-result for a task. It is used in pre-filters to specify early result
  143. * of the task scanned. If a pre-filter sets some result, then further processing
  144. * may be skipped. For selecting action it is possible to use global table
  145. * `rspamd_actions` or a string value:
  146. *
  147. * - `reject`: reject message permanently
  148. * - `add header`: add spam header
  149. * - `rewrite subject`: rewrite subject to spam subject
  150. * - `greylist`: greylist message
  151. * - `accept` or `no action`: whitelist message
  152. * @param {rspamd_action or string} action a numeric or string action value
  153. * @param {string} description optional description
  154. @example
  155. local function cb(task)
  156. local gr = task:get_header('Greylist')
  157. if gr and gr == 'greylist' then
  158. task:set_pre_result(rspamd_actions['greylist'], 'Greylisting required')
  159. end
  160. end
  161. */
  162. LUA_FUNCTION_DEF (task, set_pre_result);
  163. /***
  164. * @method task:has_pre_result()
  165. * Returns true if task has some pre-result being set
  166. *
  167. * @return {boolean} true if task has some pre-result being set
  168. */
  169. LUA_FUNCTION_DEF (task, has_pre_result);
  170. /***
  171. * @method task:append_message(message, [category])
  172. * Adds a message to scanning output.
  173. * @param {string} message
  174. * @param {category} message category
  175. @example
  176. local function cb(task)
  177. task:append_message('Example message')
  178. end
  179. */
  180. LUA_FUNCTION_DEF (task, append_message);
  181. /***
  182. * @method task:get_urls([need_emails|list_protos])
  183. * Get all URLs found in a message. Telephone urls and emails are not included unless explicitly asked in `list_protos`
  184. * @param {boolean} need_emails if `true` then reutrn also email urls, this can be a comma separated string of protocols desired or a table (e.g. `mailto` or `telephone`)
  185. * @return {table rspamd_url} list of all urls found
  186. @example
  187. local function phishing_cb(task)
  188. local urls = task:get_urls({'https', 'http'});
  189. if urls then
  190. for _,url in ipairs(urls) do
  191. if url:is_phished() then
  192. return true
  193. end
  194. end
  195. end
  196. return false
  197. end
  198. */
  199. LUA_FUNCTION_DEF (task, get_urls);
  200. /***
  201. * @method task:has_urls([need_emails])
  202. * Returns 'true' if a task has urls listed
  203. * @param {boolean} need_emails if `true` then reutrn also email urls
  204. * @return {boolean} true if a task has urls (urls or emails if `need_emails` is true)
  205. */
  206. LUA_FUNCTION_DEF (task, has_urls);
  207. /***
  208. * @method task:get_content()
  209. * Get raw content for the specified task
  210. * @return {text} the data contained in the task
  211. */
  212. LUA_FUNCTION_DEF (task, get_content);
  213. /***
  214. * @method task:get_filename()
  215. * Returns filename for a specific task
  216. * @return {string|nil} filename or nil if unknown
  217. */
  218. LUA_FUNCTION_DEF (task, get_filename);
  219. /***
  220. * @method task:get_rawbody()
  221. * Get raw body for the specified task
  222. * @return {text} the data contained in the task
  223. */
  224. LUA_FUNCTION_DEF (task, get_rawbody);
  225. /***
  226. * @method task:get_emails()
  227. * Get all email addresses found in a message.
  228. * @return {table rspamd_url} list of all email addresses found
  229. */
  230. LUA_FUNCTION_DEF (task, get_emails);
  231. /***
  232. * @method task:get_text_parts()
  233. * Get all text (and HTML) parts found in a message
  234. * @return {table rspamd_text_part} list of text parts
  235. */
  236. LUA_FUNCTION_DEF (task, get_text_parts);
  237. /***
  238. * @method task:get_parts()
  239. * Get all mime parts found in a message
  240. * @return {table rspamd_mime_part} list of mime parts
  241. */
  242. LUA_FUNCTION_DEF (task, get_parts);
  243. /***
  244. * @method task:get_meta_words([how='stem'])
  245. * Get meta words from task (subject and displayed names)
  246. * - `stem`: stemmed words (default)
  247. * - `norm`: normalised words (utf normalised + lowercased)
  248. * - `raw`: raw words in utf (if possible)
  249. * - `full`: list of tables, each table has the following fields:
  250. * - [1] - stemmed word
  251. * - [2] - normalised word
  252. * - [3] - raw word
  253. * - [4] - flags (table of strings)
  254. */
  255. LUA_FUNCTION_DEF (task, get_meta_words);
  256. /***
  257. * @method task:get_request_header(name)
  258. * Get value of a HTTP request header.
  259. * @param {string} name name of header to get
  260. * @return {rspamd_text} value of an HTTP header
  261. */
  262. LUA_FUNCTION_DEF (task, get_request_header);
  263. /***
  264. * @method task:set_request_header(name, value)
  265. * Set value of a HTTP request header. If value is omitted, then a header is removed
  266. * @param {string} name name of header to get
  267. * @param {rspamd_text/string} value new header's value
  268. */
  269. LUA_FUNCTION_DEF (task, set_request_header);
  270. /***
  271. * @method task:get_subject()
  272. * Returns task subject (either from the protocol override or from a header)
  273. * @return {string} value of a subject (decoded)
  274. */
  275. LUA_FUNCTION_DEF (task, get_subject);
  276. /***
  277. * @method task:get_header(name[, case_sensitive])
  278. * Get decoded value of a header specified with optional case_sensitive flag.
  279. * By default headers are searched in caseless matter.
  280. * @param {string} name name of header to get
  281. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  282. * @return {string} decoded value of a header
  283. */
  284. LUA_FUNCTION_DEF (task, get_header);
  285. /***
  286. * @method task:get_header_raw(name[, case_sensitive])
  287. * Get raw value of a header specified with optional case_sensitive flag.
  288. * By default headers are searched in caseless matter.
  289. * @param {string} name name of header to get
  290. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  291. * @return {string} raw value of a header
  292. */
  293. LUA_FUNCTION_DEF (task, get_header_raw);
  294. /***
  295. * @method task:get_header_full(name[, case_sensitive])
  296. * Get raw value of a header specified with optional case_sensitive flag.
  297. * By default headers are searched in caseless matter. This method returns more
  298. * information about the header as a list of tables with the following structure:
  299. *
  300. * - `name` - name of a header
  301. * - `value` - raw value of a header
  302. * - `decoded` - decoded value of a header
  303. * - `tab_separated` - `true` if a header and a value are separated by `tab` character
  304. * - `empty_separator` - `true` if there are no separator between a header and a value
  305. * @param {string} name name of header to get
  306. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  307. * @return {list of tables} all values of a header as specified above
  308. @example
  309. function check_header_delimiter_tab(task, header_name)
  310. for _,rh in ipairs(task:get_header_full(header_name)) do
  311. if rh['tab_separated'] then return true end
  312. end
  313. return false
  314. end
  315. */
  316. LUA_FUNCTION_DEF (task, get_header_full);
  317. /***
  318. * @method task:get_header_count(name[, case_sensitive])
  319. * Lightweight version if you need just a header's count
  320. * * By default headers are searched in caseless matter.
  321. * @param {string} name name of header to get
  322. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  323. * @return {number} number of header's occurrencies or 0 if not found
  324. */
  325. LUA_FUNCTION_DEF (task, get_header_count);
  326. /***
  327. * @method task:get_raw_headers()
  328. * Get all undecoded headers of a message as a string
  329. * @return {rspamd_text} all raw headers for a message as opaque text
  330. */
  331. LUA_FUNCTION_DEF (task, get_raw_headers);
  332. /***
  333. * @method task:get_received_headers()
  334. * Returns a list of tables of parsed received headers. A tables returned have
  335. * the following structure:
  336. *
  337. * - `from_hostname` - string that represents hostname provided by a peer
  338. * - `from_ip` - string representation of IP address as provided by a peer
  339. * - `real_hostname` - hostname as resolved by MTA
  340. * - `real_ip` - string representation of IP as resolved by PTR request of MTA
  341. * - `by_hostname` - MTA hostname
  342. * - `proto` - protocol, e.g. ESMTP or ESMTPS
  343. * - `timestamp` - received timestamp
  344. * - `for` - for value (unparsed mailbox)
  345. *
  346. * Please note that in some situations rspamd cannot parse all the fields of received headers.
  347. * In that case you should check all strings for validity.
  348. * @return {table of tables} list of received headers described above
  349. */
  350. LUA_FUNCTION_DEF (task, get_received_headers);
  351. /***
  352. * @method task:get_queue_id()
  353. * Returns queue ID of the message being processed.
  354. */
  355. LUA_FUNCTION_DEF (task, get_queue_id);
  356. /***
  357. * @method task:get_uid()
  358. * Returns ID of the task being processed.
  359. */
  360. LUA_FUNCTION_DEF (task, get_uid);
  361. /***
  362. * @method task:get_resolver()
  363. * Returns ready to use rspamd_resolver object suitable for making asynchronous DNS requests.
  364. * @return {rspamd_resolver} resolver object associated with the task's session
  365. * @example
  366. local logger = require "rspamd_logger"
  367. local function task_cb(task)
  368. local function dns_cb(resolver, to_resolve, results, err)
  369. -- task object is available due to closure
  370. task:inc_dns_req()
  371. if results then
  372. logger.info(string.format('<%s> [%s] resolved for symbol: %s',
  373. task:get_message_id(), to_resolve, 'EXAMPLE_SYMBOL'))
  374. task:insert_result('EXAMPLE_SYMBOL', 1)
  375. end
  376. end
  377. local r = task:get_resolver()
  378. r:resolve_a(task:get_session(), task:get_mempool(), 'example.com', dns_cb)
  379. end
  380. */
  381. LUA_FUNCTION_DEF (task, get_resolver);
  382. /***
  383. * @method task:set_resolver(resolver)
  384. * Sets rspamd_resolver for a specified task.
  385. */
  386. LUA_FUNCTION_DEF (task, set_resolver);
  387. /***
  388. * @method task:inc_dns_req()
  389. * Increment number of DNS requests for the task. Is used just for logging purposes.
  390. */
  391. LUA_FUNCTION_DEF (task, inc_dns_req);
  392. /***
  393. * @method task:get_dns_req()
  394. * Get number of dns requests being sent in the task
  395. * @return {number} number of DNS requests
  396. */
  397. LUA_FUNCTION_DEF (task, get_dns_req);
  398. /***
  399. * @method task:has_recipients([type])
  400. * Return true if there are SMTP or MIME recipients for a task.
  401. * @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
  402. * @return {bool} `true` if there are recipients of the following type
  403. */
  404. LUA_FUNCTION_DEF (task, has_recipients);
  405. /***
  406. * @method task:get_recipients([type])
  407. * Return SMTP or MIME recipients for a task. This function returns list of internet addresses each one is a table with the following structure:
  408. *
  409. * - `name` - name of internet address in UTF8, e.g. for `Vsevolod Stakhov <blah@foo.com>` it returns `Vsevolod Stakhov`
  410. * - `addr` - address part of the address
  411. * - `user` - user part (if present) of the address, e.g. `blah`
  412. * - `domain` - domain part (if present), e.g. `foo.com`
  413. * @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
  414. * @return {list of addresses} list of recipients or `nil`
  415. */
  416. LUA_FUNCTION_DEF (task, get_recipients);
  417. /***
  418. * @method task:get_principal_recipient()
  419. * Returns a single string with so called `principal recipient` for a message. The order
  420. * of check is the following:
  421. *
  422. * - deliver-to request header
  423. * - the first recipient (envelope)
  424. * - the first recipient (mime)
  425. * @return {string} principal recipient
  426. */
  427. LUA_FUNCTION_DEF (task, get_principal_recipient);
  428. /***
  429. * @method task:get_reply_sender()
  430. * Returns a single string with address that should be used to reply on a message
  431. *
  432. * - reply-to header
  433. * - from header
  434. * - smtp from as a last resort
  435. * @return {address} email address
  436. */
  437. LUA_FUNCTION_DEF (task, get_reply_sender);
  438. /***
  439. * @method task:set_recipients([type], {rcpt1, rcpt2...})
  440. * Sets sender for a task. This function accepts table that will be converted to the address.
  441. * If some fields are missing they are subsequently reconstructed by this function. E.g. if you
  442. * specify 'user' and 'domain', then address and raw string will be reconstructed
  443. *
  444. * - `name` - name of internet address in UTF8, e.g. for `Vsevolod Stakhov <blah@foo.com>` it returns `Vsevolod Stakhov`
  445. * - `addr` - address part of the address
  446. * - `user` - user part (if present) of the address, e.g. `blah`
  447. * - `domain` - domain part (if present), e.g. `foo.com`
  448. * @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
  449. * @param {list of tables} recipients recipients to set
  450. * @return {boolean} result of the operation
  451. */
  452. LUA_FUNCTION_DEF (task, set_recipients);
  453. /***
  454. * @method task:has_from([type])
  455. * Return true if there is SMTP or MIME sender for a task.
  456. * @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
  457. * @return {bool} `true` if there is sender of the following type
  458. */
  459. LUA_FUNCTION_DEF (task, has_from);
  460. /***
  461. * @method task:get_from([type])
  462. * Return SMTP or MIME sender for a task. This function returns an internet address which one is a table with the following structure:
  463. *
  464. * - `name` - name of internet address in UTF8, e.g. for `Vsevolod Stakhov <blah@foo.com>` it returns `Vsevolod Stakhov`
  465. * - `addr` - address part of the address
  466. * - `user` - user part (if present) of the address, e.g. `blah`
  467. * - `domain` - domain part (if present), e.g. `foo.com`
  468. * @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
  469. * @return {address} sender or `nil`
  470. */
  471. LUA_FUNCTION_DEF (task, get_from);
  472. /***
  473. * @method task:set_from(type, addr)
  474. * Sets sender for a task. This function accepts table that will be converted to the address.
  475. * If some fields are missing they are subsequently reconstructed by this function. E.g. if you
  476. * specify 'user' and 'domain', then address and raw string will be reconstructed
  477. *
  478. * - `name` - name of internet address in UTF8, e.g. for `Vsevolod Stakhov <blah@foo.com>` it returns `Vsevolod Stakhov`
  479. * - `addr` - address part of the address
  480. * - `user` - user part (if present) of the address, e.g. `blah`
  481. * - `domain` - domain part (if present), e.g. `foo.com`
  482. * @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
  483. * @param {table
  484. * @return {boolean} success or not
  485. */
  486. LUA_FUNCTION_DEF (task, set_from);
  487. /***
  488. * @method task:get_user()
  489. * Returns authenticated user name for this task if specified by an MTA.
  490. * @return {string} username or nil
  491. */
  492. LUA_FUNCTION_DEF (task, get_user);
  493. /***
  494. * @method task:set_user([username])
  495. * Sets or resets (if username is not specified) authenticated user name for this task.
  496. * @return {string} the previously set username or nil
  497. */
  498. LUA_FUNCTION_DEF (task, set_user);
  499. /***
  500. * @method task:get_from_ip()
  501. * Returns [ip_addr](ip.md) object of a sender that is provided by MTA
  502. * @return {rspamd_ip} ip address object
  503. */
  504. LUA_FUNCTION_DEF (task, get_from_ip);
  505. /***
  506. * @method task:set_from_ip(str)
  507. * Set tasks's IP address based on the passed string
  508. * @param {string} str string representation of ip
  509. */
  510. LUA_FUNCTION_DEF (task, set_from_ip);
  511. LUA_FUNCTION_DEF (task, get_from_ip_num);
  512. /***
  513. * @method task:get_client_ip()
  514. * Returns [ip_addr](ip.md) object of a client connected to rspamd (normally, it is an IP address of MTA)
  515. * @return {rspamd_ip} ip address object
  516. */
  517. LUA_FUNCTION_DEF (task, get_client_ip);
  518. /***
  519. * @method task:get_helo()
  520. * Returns the value of SMTP helo provided by MTA.
  521. * @return {string} HELO value
  522. */
  523. LUA_FUNCTION_DEF (task, get_helo);
  524. LUA_FUNCTION_DEF (task, set_helo);
  525. /***
  526. * @method task:get_hostname()
  527. * Returns the value of sender's hostname provided by MTA
  528. * @return {string} hostname value
  529. */
  530. LUA_FUNCTION_DEF (task, get_hostname);
  531. LUA_FUNCTION_DEF (task, set_hostname);
  532. /***
  533. * @method task:get_images()
  534. * Returns list of all images found in a task as a table of `rspamd_image`.
  535. * Each image has the following methods:
  536. *
  537. * * `get_width` - return width of an image in pixels
  538. * * `get_height` - return height of an image in pixels
  539. * * `get_type` - return string representation of image's type (e.g. 'jpeg')
  540. * * `get_filename` - return string with image's file name
  541. * * `get_size` - return size in bytes
  542. * @return {list of rspamd_image} images found in a message
  543. */
  544. LUA_FUNCTION_DEF (task, get_images);
  545. /***
  546. * @method task:get_archives()
  547. * Returns list of all archives found in a task as a table of `rspamd_archive`.
  548. * Each archive has the following methods available:
  549. *
  550. * * `get_files` - return list of strings with filenames inside archive
  551. * * `get_files_full` - return list of tables with all information about files
  552. * * `is_encrypted` - return true if an archive is encrypted
  553. * * `get_type` - return string representation of image's type (e.g. 'zip')
  554. * * `get_filename` - return string with archive's file name
  555. * * `get_size` - return size in bytes
  556. * @return {list of rspamd_archive} archives found in a message
  557. */
  558. LUA_FUNCTION_DEF (task, get_archives);
  559. /***
  560. * @method task:get_dkim_results()
  561. * Returns list of all dkim check results as table of maps. Callee must ensure that
  562. * dkim checks have been completed by adding dependency on `DKIM_TRACE` symbol.
  563. * Fields in map:
  564. *
  565. * * `result` - string result of check:
  566. * - `reject`
  567. * - `allow`
  568. * - `tempfail`
  569. * - `permfail`
  570. * - `not found`
  571. * - `bad record`
  572. * * `domain` - dkim domain
  573. * * `selector` - dkim selector
  574. * * `bhash` - short version of b tag (8 base64 symbols)
  575. * * `fail_reason` - reason of failure (if applicable)
  576. * @return {list of maps} dkim check results
  577. */
  578. LUA_FUNCTION_DEF (task, get_dkim_results);
  579. /***
  580. * @method task:get_symbol(name)
  581. * Searches for a symbol `name` in all metrics results and returns a list of tables
  582. * one per metric that describes the symbol inserted. Please note that this function
  583. * is intended to return values for **inserted** symbols, so if this symbol was not
  584. * inserted it won't be in the function's output. This method is useful for post-filters mainly.
  585. * The symbols are returned as the list of the following tables:
  586. *
  587. * - `metric` - name of metric
  588. * - `score` - score of a symbol in that metric
  589. * - `options` - a table of strings representing options of a symbol
  590. * - `group` - a group of symbol (or 'ungrouped')
  591. * @param {string} name symbol's name
  592. * @return {list of tables} list of tables or nil if symbol was not found in any metric
  593. */
  594. LUA_FUNCTION_DEF (task, get_symbol);
  595. /***
  596. * @method task:get_symbols_all()
  597. * Returns array of symbols matched in default metric with all metadata
  598. * @return {table} table of tables formatted as in `task:get_symbol()` except that `metric` is absent and `name` is added
  599. */
  600. LUA_FUNCTION_DEF (task, get_symbols_all);
  601. /***
  602. * @method task:get_symbols()
  603. * Returns array of all symbols matched for this task
  604. * @return {table, table} table of strings with symbols names + table of theirs scores
  605. */
  606. LUA_FUNCTION_DEF (task, get_symbols);
  607. /***
  608. * @method task:get_symbols_numeric()
  609. * Returns array of all symbols matched for this task
  610. * @return {table|number, table|number} table of numbers with symbols ids + table of theirs scores
  611. */
  612. LUA_FUNCTION_DEF (task, get_symbols_numeric);
  613. /***
  614. * @method task:get_symbols_tokens()
  615. * Returns array of all symbols as statistical tokens
  616. * @return {table|number} table of numbers
  617. */
  618. LUA_FUNCTION_DEF (task, get_symbols_tokens);
  619. /***
  620. * @method task:has_symbol(name)
  621. * Fast path to check if a specified symbol is in the task's results
  622. * @param {string} name symbol's name
  623. * @return {boolean} `true` if symbol has been found
  624. */
  625. LUA_FUNCTION_DEF (task, has_symbol);
  626. /***
  627. * @method task:enable_symbol(name)
  628. * Enable specified symbol for this particular task
  629. * @param {string} name symbol's name
  630. * @return {boolean} `true` if symbol has been found
  631. */
  632. LUA_FUNCTION_DEF (task, enable_symbol);
  633. /***
  634. * @method task:disable_symbol(name)
  635. * Disable specified symbol for this particular task
  636. * @param {string} name symbol's name
  637. * @return {boolean} `true` if symbol has been found
  638. */
  639. LUA_FUNCTION_DEF (task, disable_symbol);
  640. /***
  641. * @method task:get_date(type[, gmt])
  642. * Returns timestamp for a connection or for a MIME message. This function can be called with a
  643. * single table arguments with the following fields:
  644. *
  645. * * `format` - a format of date returned:
  646. * - `message` - returns a mime date as integer (unix timestamp)
  647. * - `message_str` - returns a mime date as string (UTC format)
  648. * - `connect` - returns a unix timestamp of a connection to rspamd
  649. * - `connect_str` - returns connection time in UTC format
  650. * * `gmt` - returns date in `GMT` timezone (normal for unix timestamps)
  651. *
  652. * By default this function returns connection time in numeric format.
  653. * @param {string} type date format as described above
  654. * @param {boolean} gmt gmt flag as described above
  655. * @return {string/number} date representation according to format
  656. * @example
  657. rspamd_config.DATE_IN_PAST = function(task)
  658. local dm = task:get_date{format = 'message', gmt = true}
  659. local dt = task:get_date{format = 'connect', gmt = true}
  660. -- A day
  661. if dt - dm > 86400 then
  662. return true
  663. end
  664. return false
  665. end
  666. */
  667. LUA_FUNCTION_DEF (task, get_date);
  668. /***
  669. * @method task:get_message_id()
  670. * Returns message id of the specified task
  671. * @return {string} if of a message
  672. */
  673. LUA_FUNCTION_DEF (task, get_message_id);
  674. /***
  675. * @method task:get_timeval()
  676. * Returns the timestamp for a task start processing time.
  677. * @return {table} table with fields as described in `struct timeval` in C
  678. */
  679. LUA_FUNCTION_DEF (task, get_timeval);
  680. /***
  681. * @method task:get_scan_time([set])
  682. * Returns 2 floating point numbers: scan real time and scan virtual time.
  683. * If `set` is `true`, then the finishing time is also set (enabled by default).
  684. * This function should be normally called on idempotent phase.
  685. * @return {number,number} real and virtual times in seconds with floating point
  686. */
  687. LUA_FUNCTION_DEF (task, get_scan_time);
  688. /***
  689. * @method task:get_metric_result()
  690. * Get full result of a metric as a table:
  691. * - `score`: current score
  692. * - `action`: current action as a string
  693. * - `nnegative`: number of negative rules matched
  694. * - `npositive`: number of positive rules matched
  695. * - `positive_score`: total score for positive rules
  696. * - `negative_score`: total score for negative rules
  697. * - `passthrough`: set to true if message has a passthrough result
  698. * @return {table} metric result
  699. */
  700. LUA_FUNCTION_DEF (task, get_metric_result);
  701. /***
  702. * @method task:get_metric_score(name)
  703. * Get the current score of metric `name` (must be nil or 'default') . Should be used in idempotent filters only.
  704. * @param {string} name name of a metric
  705. * @return {number,number} 2 numbers containing the current score and required score of the metric
  706. */
  707. LUA_FUNCTION_DEF (task, get_metric_score);
  708. /***
  709. * @method task:get_metric_action(name)
  710. * Get the current action of metric `name` (must be nil or 'default'). Should be used in idempotent filters only.
  711. * @param {string} name name of a metric
  712. * @return {string} the current action of the metric as a string
  713. */
  714. LUA_FUNCTION_DEF (task, get_metric_action);
  715. /***
  716. * @method task:set_metric_score(name, score)
  717. * Set the current score of metric `name`. Should be used in post-filters only.
  718. * @param {string} name name of a metric
  719. * @param {number} score the current score of the metric
  720. */
  721. LUA_FUNCTION_DEF (task, set_metric_score);
  722. /***
  723. * @method task:set_metric_subject(subject)
  724. * Set the subject in the default metric
  725. * @param {string} subject subject to set
  726. */
  727. LUA_FUNCTION_DEF (task, set_metric_subject);
  728. /***
  729. * @method task:learn(is_spam[, classifier)
  730. * Learn classifier `classifier` with the task. If `is_spam` is true then message
  731. * is learnt as spam. Otherwise HAM is learnt. By default, this function learns
  732. * `bayes` classifier.
  733. * @param {boolean} is_spam learn spam or ham
  734. * @param {string} classifier classifier's name
  735. * @return {boolean} `true` if classifier has been learnt successfully
  736. */
  737. LUA_FUNCTION_DEF (task, learn);
  738. /***
  739. * @method task:set_settings(obj)
  740. * Set users settings object for a task. The format of this object is described
  741. * [here](https://rspamd.com/doc/configuration/settings.html).
  742. * @param {any} obj any lua object that corresponds to the settings format
  743. */
  744. LUA_FUNCTION_DEF (task, set_settings);
  745. /***
  746. * @method task:get_settings()
  747. * Gets users settings object for a task. The format of this object is described
  748. * [here](https://rspamd.com/doc/configuration/settings.html).
  749. * @return {lua object} lua object generated from UCL
  750. */
  751. LUA_FUNCTION_DEF (task, get_settings);
  752. /***
  753. * @method task:lookup_settings(key)
  754. * Gets users settings object with the specified key for a task.
  755. * @param {string} key key to lookup
  756. * @return {lua object} lua object generated from UCL
  757. */
  758. LUA_FUNCTION_DEF (task, lookup_settings);
  759. /***
  760. * @method task:get_settings_id()
  761. * Get numeric hash of settings id if specified for this task. 0 is returned otherwise.
  762. * @return {number} settings-id hash
  763. */
  764. LUA_FUNCTION_DEF (task, get_settings_id);
  765. /***
  766. * @method task:set_milter_reply(obj)
  767. * Set special reply for milter
  768. * @param {any} obj any lua object that corresponds to the settings format
  769. * @example
  770. task:set_milter_reply({
  771. add_headers = {['X-Lua'] = 'test'},
  772. -- 1 is the position of header to remove
  773. remove_headers = {['DKIM-Signature'] = 1},
  774. })
  775. */
  776. LUA_FUNCTION_DEF (task, set_milter_reply);
  777. /***
  778. * @method task:process_re(params)
  779. * Processes the specified regexp and returns number of captures (cached or new)
  780. * Params is the table with the following fields (mandatory fields are marked with `*`):
  781. * - `re`* : regular expression object
  782. * - `type`*: type of regular expression:
  783. * + `mime`: mime regexp
  784. * + `header`: header regexp
  785. * + `rawheader`: raw header expression
  786. * + `rawmime`: raw mime regexp
  787. * + `body`: raw body regexp
  788. * + `url`: url regexp
  789. * - `header`: for header and rawheader regexp means the name of header
  790. * - `strong`: case sensitive match for headers
  791. * @return {number} number of regexp occurrences in the task (limited by 255 so far)
  792. */
  793. LUA_FUNCTION_DEF (task, process_regexp);
  794. /***
  795. * @method task:cache_set(key, value)
  796. * Store some value to the task cache
  797. * @param {string} key key to use
  798. * @param {any} value any value (including functions and tables)
  799. */
  800. LUA_FUNCTION_DEF (task, cache_set);
  801. /***
  802. * @method task:cache_get(key)
  803. * Returns cached value or nil if nothing is cached
  804. * @param {string} key key to use
  805. * @return {any} cached value
  806. */
  807. LUA_FUNCTION_DEF (task, cache_get);
  808. /***
  809. * @method task:get_size()
  810. * Returns size of the task in bytes (that includes headers + parts size)
  811. * @return {number} size in bytes
  812. */
  813. LUA_FUNCTION_DEF (task, get_size);
  814. /***
  815. * @method task:set_flag(flag_name[, set])
  816. * Set specific flag for task:
  817. *
  818. * - `no_log`: do not log task summary
  819. * - `no_stat`: do not include task into scanned stats
  820. * - `pass_all`: check all filters for task
  821. * - `extended_urls`: output extended info about urls
  822. * - `skip`: skip task processing
  823. * - `learn_spam`: learn message as spam
  824. * - `learn_ham`: learn message as ham
  825. * - `broken_headers`: header data is broken for a message
  826. * @param {string} flag to set
  827. * @param {boolean} set set or clear flag (default is set)
  828. @example
  829. --[[
  830. For messages with undefined queue ID (scanned with rspamc or WebUI)
  831. do not include results into statistics and do not log task summary
  832. (it will not appear in the WebUI history as well).
  833. ]]--
  834. -- Callback function to set flags
  835. local function no_log_stat_cb(task)
  836. if not task:get_queue_id() then
  837. task:set_flag('no_log')
  838. task:set_flag('no_stat')
  839. end
  840. end
  841. rspamd_config:register_symbol({
  842. name = 'LOCAL_NO_LOG_STAT',
  843. type = 'postfilter',
  844. callback = no_log_stat_cb
  845. })
  846. */
  847. LUA_FUNCTION_DEF (task, set_flag);
  848. /***
  849. * @method task:has_flag(flag_name)
  850. * Checks for a specific flag in task:
  851. *
  852. * - `no_log`: do not log task summary
  853. * - `no_stat`: do not include task into scanned stats
  854. * - `pass_all`: check all filters for task
  855. * - `extended_urls`: output extended info about urls
  856. * - `skip`: skip task processing
  857. * - `learn_spam`: learn message as spam
  858. * - `learn_ham`: learn message as ham
  859. * - `broken_headers`: header data is broken for a message
  860. * @param {string} flag to check
  861. * @return {boolean} true if flags is set
  862. */
  863. LUA_FUNCTION_DEF (task, has_flag);
  864. /***
  865. * @method task:get_flags()
  866. * Get list of flags for task:
  867. *
  868. * - `no_log`: do not log task summary
  869. * - `no_stat`: do not include task into scanned stats
  870. * - `pass_all`: check all filters for task
  871. * - `extended_urls`: output extended info about urls
  872. * - `skip`: skip task processing
  873. * - `learn_spam`: learn message as spam
  874. * - `learn_ham`: learn message as ham
  875. * - `broken_headers`: header data is broken for a message
  876. * - `milter`: task is initiated by milter connection
  877. * @return {array of strings} table with all flags as strings
  878. */
  879. LUA_FUNCTION_DEF (task, get_flags);
  880. /***
  881. * @method task:get_digest()
  882. * Returns message's unique digest (32 hex symbols)
  883. * @return {string} hex digest
  884. */
  885. LUA_FUNCTION_DEF (task, get_digest);
  886. /***
  887. * @method task:store_in_file([mode|table])
  888. * If task was loaded using file scan, then this method just returns its name,
  889. * otherwise, a fresh temporary file is created and its name is returned. Default
  890. * mode is 0600. To convert lua number to the octal mode you can use the following
  891. * trick: `tonumber("0644", 8)`. The file is automatically removed when task is
  892. * destroyed.
  893. *
  894. * If table argument is specified, the following extra fields are allowed:
  895. *
  896. * - `mode`: same as mode argument
  897. * - `force_new`: always create a new file
  898. * - `filename`: use specific filename instead of a temporary one
  899. * - `tmpmask`: use specific tempmask, e.g. '/tmp/file-XXXXX', where XXXX will be replaced by some random letters
  900. * - `keep`: do not remove file after task is processed
  901. *
  902. * @param {number} mode mode for new file
  903. * @return {string} file name with task content
  904. */
  905. LUA_FUNCTION_DEF (task, store_in_file);
  906. /***
  907. * @method task:get_protocol_reply([flags])
  908. * This method being called from a **postfilter** will return reply for a message
  909. * as it is returned to a client. This method returns the Lua table corresponding
  910. * to the UCL object. Flags is a table that specify which information should be
  911. * there in a reply:
  912. *
  913. * - `basic`: basic info, such as message-id
  914. * - `metrics`: metrics and symbols
  915. * - `messages`: messages
  916. * - `dkim`: dkim signature
  917. * - `milter`: milter control block
  918. * - `extra`: extra data, such as profiling
  919. * - `urls`: list of all urls in a message
  920. *
  921. * @param {table} flags table of flags (default is all flags but `urls`)
  922. * @return {table} ucl object corresponding to the reply
  923. */
  924. LUA_FUNCTION_DEF (task, get_protocol_reply);
  925. /***
  926. * @method task:headers_foreach(callback, [params])
  927. * This method calls `callback` for each header that satisfies some condition.
  928. * By default, all headers are iterated unless `callback` returns `true`. Nil or
  929. * false means continue of iterations.
  930. * Params could be as following:
  931. *
  932. * - `full`: header value is full table of all attributes @see task:get_header_full for details
  933. * - `regexp`: return headers that satisfies the specified regexp
  934. * @param {function} callback function from header name and header value
  935. * @param {table} params optional parameters
  936. */
  937. LUA_FUNCTION_DEF (task, headers_foreach);
  938. /***
  939. * @method task:disable_action(action)
  940. * Disables some action for this task (e.g. 'greylist')
  941. *
  942. * @param {string} action action to disable
  943. * @return {boolean} true if an action was enabled and is disabled after the method call
  944. */
  945. LUA_FUNCTION_DEF (task, disable_action);
  946. /***
  947. * @method task:get_newlines_type()
  948. * Returns the most frequent newlines type met in a task
  949. *
  950. * @return {string} "cr" for \r, "lf" for \n, "crlf" for \r\n
  951. */
  952. LUA_FUNCTION_DEF (task, get_newlines_type);
  953. /***
  954. * @method task:get_stat_tokens()
  955. * Returns list of tables the statistical tokens:
  956. * - `data`: 64 bit number encoded as a string
  957. * - `t1`: the first token (if any)
  958. * - `t2`: the second token (if any)
  959. * - `win`: window index
  960. * - `flag`: table of strings:
  961. * - `text`: text token
  962. * - `meta`: meta token
  963. * - `lua`: lua meta token
  964. * - `exception`: exception
  965. * - `subject`: subject token
  966. * - `unigram`: unigram token
  967. *
  968. * @return {table of tables}
  969. */
  970. LUA_FUNCTION_DEF (task, get_stat_tokens);
  971. /***
  972. * @method task:lookup_words(map, function({o, n, s, f}) ... end)
  973. * Matches words in a task (including meta words) against some map (set, regexp and so on)
  974. * and call the specified function with a table containing 4 values:
  975. * - [1] - stemmed word
  976. * - [2] - normalised word
  977. * - [3] - raw word
  978. * - [4] - flags (table of strings)
  979. */
  980. LUA_FUNCTION_DEF (task, lookup_words);
  981. /**
  982. * @method task:topointer()
  983. *
  984. * Returns raw C pointer (lightuserdata) associated with task
  985. */
  986. LUA_FUNCTION_DEF (task, topointer);
  987. static const struct luaL_reg tasklib_f[] = {
  988. LUA_INTERFACE_DEF (task, create),
  989. LUA_INTERFACE_DEF (task, load_from_file),
  990. LUA_INTERFACE_DEF (task, load_from_string),
  991. {NULL, NULL}
  992. };
  993. static const struct luaL_reg tasklib_m[] = {
  994. LUA_INTERFACE_DEF (task, get_message),
  995. LUA_INTERFACE_DEF (task, destroy),
  996. LUA_INTERFACE_DEF (task, process_message),
  997. LUA_INTERFACE_DEF (task, set_cfg),
  998. LUA_INTERFACE_DEF (task, get_cfg),
  999. LUA_INTERFACE_DEF (task, get_mempool),
  1000. LUA_INTERFACE_DEF (task, get_session),
  1001. LUA_INTERFACE_DEF (task, set_session),
  1002. LUA_INTERFACE_DEF (task, get_ev_base),
  1003. LUA_INTERFACE_DEF (task, get_worker),
  1004. LUA_INTERFACE_DEF (task, insert_result),
  1005. LUA_INTERFACE_DEF (task, adjust_result),
  1006. LUA_INTERFACE_DEF (task, set_pre_result),
  1007. LUA_INTERFACE_DEF (task, has_pre_result),
  1008. LUA_INTERFACE_DEF (task, append_message),
  1009. LUA_INTERFACE_DEF (task, has_urls),
  1010. LUA_INTERFACE_DEF (task, get_urls),
  1011. LUA_INTERFACE_DEF (task, get_content),
  1012. LUA_INTERFACE_DEF (task, get_filename),
  1013. LUA_INTERFACE_DEF (task, get_rawbody),
  1014. LUA_INTERFACE_DEF (task, get_emails),
  1015. LUA_INTERFACE_DEF (task, get_text_parts),
  1016. LUA_INTERFACE_DEF (task, get_parts),
  1017. LUA_INTERFACE_DEF (task, get_request_header),
  1018. LUA_INTERFACE_DEF (task, set_request_header),
  1019. LUA_INTERFACE_DEF (task, get_header),
  1020. LUA_INTERFACE_DEF (task, get_header_raw),
  1021. LUA_INTERFACE_DEF (task, get_header_full),
  1022. LUA_INTERFACE_DEF (task, get_header_count),
  1023. LUA_INTERFACE_DEF (task, get_raw_headers),
  1024. LUA_INTERFACE_DEF (task, get_received_headers),
  1025. LUA_INTERFACE_DEF (task, get_queue_id),
  1026. LUA_INTERFACE_DEF (task, get_uid),
  1027. LUA_INTERFACE_DEF (task, get_resolver),
  1028. LUA_INTERFACE_DEF (task, set_resolver),
  1029. LUA_INTERFACE_DEF (task, inc_dns_req),
  1030. LUA_INTERFACE_DEF (task, get_dns_req),
  1031. LUA_INTERFACE_DEF (task, has_recipients),
  1032. LUA_INTERFACE_DEF (task, get_recipients),
  1033. LUA_INTERFACE_DEF (task, set_recipients),
  1034. LUA_INTERFACE_DEF (task, get_principal_recipient),
  1035. LUA_INTERFACE_DEF (task, get_reply_sender),
  1036. LUA_INTERFACE_DEF (task, has_from),
  1037. LUA_INTERFACE_DEF (task, get_from),
  1038. LUA_INTERFACE_DEF (task, set_from),
  1039. LUA_INTERFACE_DEF (task, get_user),
  1040. LUA_INTERFACE_DEF (task, set_user),
  1041. {"get_addr", lua_task_get_from_ip},
  1042. {"get_ip", lua_task_get_from_ip},
  1043. {"get_from_addr", lua_task_get_from_ip},
  1044. LUA_INTERFACE_DEF (task, get_from_ip),
  1045. LUA_INTERFACE_DEF (task, set_from_ip),
  1046. LUA_INTERFACE_DEF (task, get_from_ip_num),
  1047. LUA_INTERFACE_DEF (task, get_client_ip),
  1048. LUA_INTERFACE_DEF (task, get_subject),
  1049. LUA_INTERFACE_DEF (task, get_helo),
  1050. LUA_INTERFACE_DEF (task, set_helo),
  1051. LUA_INTERFACE_DEF (task, get_hostname),
  1052. LUA_INTERFACE_DEF (task, set_hostname),
  1053. LUA_INTERFACE_DEF (task, get_images),
  1054. LUA_INTERFACE_DEF (task, get_archives),
  1055. LUA_INTERFACE_DEF (task, get_dkim_results),
  1056. LUA_INTERFACE_DEF (task, get_symbol),
  1057. LUA_INTERFACE_DEF (task, get_symbols),
  1058. LUA_INTERFACE_DEF (task, get_symbols_all),
  1059. LUA_INTERFACE_DEF (task, get_symbols_numeric),
  1060. LUA_INTERFACE_DEF (task, get_symbols_tokens),
  1061. LUA_INTERFACE_DEF (task, has_symbol),
  1062. LUA_INTERFACE_DEF (task, enable_symbol),
  1063. LUA_INTERFACE_DEF (task, disable_symbol),
  1064. LUA_INTERFACE_DEF (task, get_date),
  1065. LUA_INTERFACE_DEF (task, get_message_id),
  1066. LUA_INTERFACE_DEF (task, get_timeval),
  1067. LUA_INTERFACE_DEF (task, get_scan_time),
  1068. LUA_INTERFACE_DEF (task, get_metric_result),
  1069. LUA_INTERFACE_DEF (task, get_metric_score),
  1070. LUA_INTERFACE_DEF (task, get_metric_action),
  1071. LUA_INTERFACE_DEF (task, set_metric_score),
  1072. LUA_INTERFACE_DEF (task, set_metric_subject),
  1073. LUA_INTERFACE_DEF (task, learn),
  1074. LUA_INTERFACE_DEF (task, set_settings),
  1075. LUA_INTERFACE_DEF (task, get_settings),
  1076. LUA_INTERFACE_DEF (task, lookup_settings),
  1077. LUA_INTERFACE_DEF (task, get_settings_id),
  1078. LUA_INTERFACE_DEF (task, cache_get),
  1079. LUA_INTERFACE_DEF (task, cache_set),
  1080. LUA_INTERFACE_DEF (task, process_regexp),
  1081. LUA_INTERFACE_DEF (task, get_size),
  1082. LUA_INTERFACE_DEF (task, set_flag),
  1083. LUA_INTERFACE_DEF (task, get_flags),
  1084. LUA_INTERFACE_DEF (task, has_flag),
  1085. {"set_rmilter_reply", lua_task_set_milter_reply},
  1086. LUA_INTERFACE_DEF (task, set_milter_reply),
  1087. LUA_INTERFACE_DEF (task, get_digest),
  1088. LUA_INTERFACE_DEF (task, store_in_file),
  1089. LUA_INTERFACE_DEF (task, get_protocol_reply),
  1090. LUA_INTERFACE_DEF (task, headers_foreach),
  1091. LUA_INTERFACE_DEF (task, disable_action),
  1092. LUA_INTERFACE_DEF (task, get_newlines_type),
  1093. LUA_INTERFACE_DEF (task, get_stat_tokens),
  1094. LUA_INTERFACE_DEF (task, get_meta_words),
  1095. LUA_INTERFACE_DEF (task, lookup_words),
  1096. LUA_INTERFACE_DEF (task, topointer),
  1097. {"__tostring", rspamd_lua_class_tostring},
  1098. {NULL, NULL}
  1099. };
  1100. /* Image methods */
  1101. LUA_FUNCTION_DEF (image, get_width);
  1102. LUA_FUNCTION_DEF (image, get_height);
  1103. LUA_FUNCTION_DEF (image, get_type);
  1104. LUA_FUNCTION_DEF (image, get_filename);
  1105. LUA_FUNCTION_DEF (image, get_size);
  1106. static const struct luaL_reg imagelib_m[] = {
  1107. LUA_INTERFACE_DEF (image, get_width),
  1108. LUA_INTERFACE_DEF (image, get_height),
  1109. LUA_INTERFACE_DEF (image, get_type),
  1110. LUA_INTERFACE_DEF (image, get_filename),
  1111. LUA_INTERFACE_DEF (image, get_size),
  1112. {"__tostring", rspamd_lua_class_tostring},
  1113. {NULL, NULL}
  1114. };
  1115. /* Archive methods */
  1116. LUA_FUNCTION_DEF (archive, get_type);
  1117. LUA_FUNCTION_DEF (archive, get_files);
  1118. LUA_FUNCTION_DEF (archive, get_files_full);
  1119. LUA_FUNCTION_DEF (archive, is_encrypted);
  1120. LUA_FUNCTION_DEF (archive, get_filename);
  1121. LUA_FUNCTION_DEF (archive, get_size);
  1122. static const struct luaL_reg archivelib_m[] = {
  1123. LUA_INTERFACE_DEF (archive, get_type),
  1124. LUA_INTERFACE_DEF (archive, get_files),
  1125. LUA_INTERFACE_DEF (archive, get_files_full),
  1126. LUA_INTERFACE_DEF (archive, is_encrypted),
  1127. LUA_INTERFACE_DEF (archive, get_filename),
  1128. LUA_INTERFACE_DEF (archive, get_size),
  1129. {"__tostring", rspamd_lua_class_tostring},
  1130. {NULL, NULL}
  1131. };
  1132. /* Utility functions */
  1133. struct rspamd_task *
  1134. lua_check_task (lua_State * L, gint pos)
  1135. {
  1136. void *ud = rspamd_lua_check_udata (L, pos, "rspamd{task}");
  1137. luaL_argcheck (L, ud != NULL, pos, "'task' expected");
  1138. return ud ? *((struct rspamd_task **)ud) : NULL;
  1139. }
  1140. struct rspamd_task *
  1141. lua_check_task_maybe (lua_State * L, gint pos)
  1142. {
  1143. void *ud = rspamd_lua_check_udata_maybe (L, pos, "rspamd{task}");
  1144. return ud ? *((struct rspamd_task **)ud) : NULL;
  1145. }
  1146. static struct rspamd_image *
  1147. lua_check_image (lua_State * L)
  1148. {
  1149. void *ud = rspamd_lua_check_udata (L, 1, "rspamd{image}");
  1150. luaL_argcheck (L, ud != NULL, 1, "'image' expected");
  1151. return ud ? *((struct rspamd_image **)ud) : NULL;
  1152. }
  1153. static struct rspamd_archive *
  1154. lua_check_archive (lua_State * L)
  1155. {
  1156. void *ud = rspamd_lua_check_udata (L, 1, "rspamd{archive}");
  1157. luaL_argcheck (L, ud != NULL, 1, "'archive' expected");
  1158. return ud ? *((struct rspamd_archive **)ud) : NULL;
  1159. }
  1160. struct rspamd_lua_text *
  1161. lua_check_text (lua_State * L, gint pos)
  1162. {
  1163. void *ud = rspamd_lua_check_udata (L, pos, "rspamd{text}");
  1164. luaL_argcheck (L, ud != NULL, pos, "'text' expected");
  1165. return ud ? (struct rspamd_lua_text *)ud : NULL;
  1166. }
  1167. static void
  1168. lua_task_set_cached (lua_State *L, struct rspamd_task *task, const gchar *key,
  1169. gint pos, guint id)
  1170. {
  1171. LUA_TRACE_POINT;
  1172. struct rspamd_lua_cached_entry *entry;
  1173. lua_pushvalue (L, pos);
  1174. entry = g_hash_table_lookup (task->lua_cache, key);
  1175. if (G_UNLIKELY (entry != NULL)) {
  1176. /* Unref previous value */
  1177. luaL_unref (L, LUA_REGISTRYINDEX, entry->ref);
  1178. }
  1179. else {
  1180. entry = rspamd_mempool_alloc (task->task_pool, sizeof (*entry));
  1181. g_hash_table_insert (task->lua_cache,
  1182. rspamd_mempool_strdup (task->task_pool, key), entry);
  1183. }
  1184. entry->ref = luaL_ref (L, LUA_REGISTRYINDEX);
  1185. entry->id = id;
  1186. }
  1187. static gboolean
  1188. lua_task_get_cached (lua_State *L, struct rspamd_task *task, const gchar *key,
  1189. guint id)
  1190. {
  1191. LUA_TRACE_POINT;
  1192. struct rspamd_lua_cached_entry *entry;
  1193. entry = g_hash_table_lookup (task->lua_cache, key);
  1194. if (entry != NULL && entry->id == id) {
  1195. lua_rawgeti (L, LUA_REGISTRYINDEX, entry->ref);
  1196. return TRUE;
  1197. }
  1198. return FALSE;
  1199. }
  1200. /* Task methods */
  1201. static int
  1202. lua_task_process_message (lua_State *L)
  1203. {
  1204. LUA_TRACE_POINT;
  1205. struct rspamd_task *task = lua_check_task (L, 1);
  1206. if (task != NULL) {
  1207. if (task->msg.len > 0) {
  1208. if (rspamd_message_parse (task)) {
  1209. lua_pushboolean (L, TRUE);
  1210. rspamd_message_process (task);
  1211. }
  1212. else {
  1213. lua_pushboolean (L, FALSE);
  1214. }
  1215. }
  1216. else {
  1217. lua_pushnil (L);
  1218. }
  1219. }
  1220. else {
  1221. return luaL_error (L, "invalid arguments");
  1222. }
  1223. return 1;
  1224. }
  1225. static int
  1226. lua_task_get_cfg (lua_State *L)
  1227. {
  1228. LUA_TRACE_POINT;
  1229. struct rspamd_task *task = lua_check_task (L, 1);
  1230. struct rspamd_config **pcfg;
  1231. if (task) {
  1232. pcfg = lua_newuserdata (L, sizeof (gpointer));
  1233. rspamd_lua_setclass (L, "rspamd{config}", -1);
  1234. *pcfg = task->cfg;
  1235. }
  1236. else {
  1237. return luaL_error (L, "invalid arguments");
  1238. }
  1239. return 1;
  1240. }
  1241. static int
  1242. lua_task_set_cfg (lua_State *L)
  1243. {
  1244. LUA_TRACE_POINT;
  1245. struct rspamd_task *task = lua_check_task (L, 1);
  1246. void *ud = rspamd_lua_check_udata (L, 2, "rspamd{config}");
  1247. if (task) {
  1248. luaL_argcheck (L, ud != NULL, 1, "'config' expected");
  1249. task->cfg = ud ? *((struct rspamd_config **)ud) : NULL;
  1250. }
  1251. else {
  1252. return luaL_error (L, "invalid arguments");
  1253. }
  1254. return 0;
  1255. }
  1256. static int
  1257. lua_task_destroy (lua_State *L)
  1258. {
  1259. LUA_TRACE_POINT;
  1260. struct rspamd_task *task = lua_check_task (L, 1);
  1261. if (task != NULL) {
  1262. rspamd_task_free (task);
  1263. }
  1264. return 0;
  1265. }
  1266. static int
  1267. lua_task_get_message (lua_State * L)
  1268. {
  1269. LUA_TRACE_POINT;
  1270. return luaL_error (L, "task:get_message is no longer supported");
  1271. }
  1272. static void
  1273. lua_task_unmap_dtor (gpointer p)
  1274. {
  1275. struct rspamd_task *task = (struct rspamd_task *)p;
  1276. if (task->msg.begin) {
  1277. munmap ((gpointer)task->msg.begin, task->msg.len);
  1278. }
  1279. }
  1280. static void
  1281. lua_task_free_dtor (gpointer p)
  1282. {
  1283. struct rspamd_task *task = (struct rspamd_task *)p;
  1284. if (task->msg.begin) {
  1285. g_free ((gpointer)task->msg.begin);
  1286. }
  1287. }
  1288. static gint
  1289. lua_task_load_from_file (lua_State * L)
  1290. {
  1291. LUA_TRACE_POINT;
  1292. struct rspamd_task *task = NULL, **ptask;
  1293. const gchar *fname = luaL_checkstring (L, 1), *err = NULL;
  1294. struct rspamd_config *cfg = NULL;
  1295. gboolean res = FALSE;
  1296. gpointer map;
  1297. gsize sz;
  1298. if (fname) {
  1299. if (lua_type (L, 2) == LUA_TUSERDATA) {
  1300. gpointer p;
  1301. p = rspamd_lua_check_udata_maybe (L, 2, "rspamd{config}");
  1302. if (p) {
  1303. cfg = *(struct rspamd_config **)p;
  1304. }
  1305. }
  1306. if (strcmp (fname, "-") == 0) {
  1307. /* Read from stdin */
  1308. gint fd = STDIN_FILENO;
  1309. GString *data = g_string_sized_new (BUFSIZ);
  1310. gchar buf[BUFSIZ];
  1311. gssize r;
  1312. for (;;) {
  1313. r = read (fd, buf, sizeof (buf));
  1314. if (r == -1) {
  1315. err = strerror (errno);
  1316. break;
  1317. }
  1318. else if (r == 0) {
  1319. break;
  1320. }
  1321. else {
  1322. g_string_append_len (data, buf, r);
  1323. }
  1324. }
  1325. task = rspamd_task_new (NULL, cfg, NULL, NULL, NULL);
  1326. task->msg.begin = data->str;
  1327. task->msg.len = data->len;
  1328. rspamd_mempool_add_destructor (task->task_pool,
  1329. lua_task_free_dtor, task);
  1330. res = TRUE;
  1331. g_string_free (data, FALSE); /* Buffer is still valid */
  1332. }
  1333. else {
  1334. map = rspamd_file_xmap (fname, PROT_READ, &sz, TRUE);
  1335. if (!map) {
  1336. err = strerror (errno);
  1337. } else {
  1338. task = rspamd_task_new (NULL, cfg, NULL, NULL, NULL);
  1339. task->msg.begin = map;
  1340. task->msg.len = sz;
  1341. rspamd_mempool_add_destructor (task->task_pool,
  1342. lua_task_unmap_dtor, task);
  1343. res = TRUE;
  1344. }
  1345. }
  1346. }
  1347. else {
  1348. return luaL_error (L, "invalid arguments");
  1349. }
  1350. lua_pushboolean (L, res);
  1351. if (res) {
  1352. ptask = lua_newuserdata (L, sizeof (*ptask));
  1353. *ptask = task;
  1354. rspamd_lua_setclass (L, "rspamd{task}", -1);
  1355. }
  1356. else {
  1357. if (err) {
  1358. lua_pushstring (L, err);
  1359. }
  1360. else {
  1361. lua_pushnil (L);
  1362. }
  1363. }
  1364. return 2;
  1365. }
  1366. static gint
  1367. lua_task_load_from_string (lua_State * L)
  1368. {
  1369. LUA_TRACE_POINT;
  1370. struct rspamd_task *task = NULL, **ptask;
  1371. const gchar *str_message;
  1372. gsize message_len;
  1373. struct rspamd_config *cfg = NULL;
  1374. str_message = luaL_checklstring (L, 1, &message_len);
  1375. if (str_message) {
  1376. if (lua_type (L, 2) == LUA_TUSERDATA) {
  1377. gpointer p;
  1378. p = rspamd_lua_check_udata_maybe (L, 2, "rspamd{config}");
  1379. if (p) {
  1380. cfg = *(struct rspamd_config **)p;
  1381. }
  1382. }
  1383. task = rspamd_task_new (NULL, cfg, NULL, NULL, NULL);
  1384. task->msg.begin = g_strdup (str_message);
  1385. task->msg.len = message_len;
  1386. rspamd_mempool_add_destructor (task->task_pool, lua_task_free_dtor, task);
  1387. }
  1388. else {
  1389. return luaL_error (L, "invalid arguments");
  1390. }
  1391. lua_pushboolean (L, true);
  1392. ptask = lua_newuserdata (L, sizeof (*ptask));
  1393. *ptask = task;
  1394. rspamd_lua_setclass (L, "rspamd{task}", -1);
  1395. return 2;
  1396. }
  1397. static gint
  1398. lua_task_create (lua_State * L)
  1399. {
  1400. LUA_TRACE_POINT;
  1401. struct rspamd_task *task = NULL, **ptask;
  1402. struct rspamd_config *cfg = NULL;
  1403. struct event_base *ev_base = NULL;
  1404. if (lua_type (L, 1) == LUA_TUSERDATA) {
  1405. gpointer p;
  1406. p = rspamd_lua_check_udata_maybe (L, 2, "rspamd{config}");
  1407. if (p) {
  1408. cfg = *(struct rspamd_config **)p;
  1409. }
  1410. }
  1411. if (lua_type (L, 2) == LUA_TUSERDATA) {
  1412. gpointer p;
  1413. p = rspamd_lua_check_udata_maybe (L, 2, "rspamd{ev_base}");
  1414. if (p) {
  1415. ev_base = *(struct event_base **)p;
  1416. }
  1417. }
  1418. task = rspamd_task_new (NULL, cfg, NULL, NULL, ev_base);
  1419. task->flags |= RSPAMD_TASK_FLAG_EMPTY;
  1420. ptask = lua_newuserdata (L, sizeof (*ptask));
  1421. *ptask = task;
  1422. rspamd_lua_setclass (L, "rspamd{task}", -1);
  1423. return 1;
  1424. }
  1425. static int
  1426. lua_task_get_mempool (lua_State * L)
  1427. {
  1428. LUA_TRACE_POINT;
  1429. rspamd_mempool_t **ppool;
  1430. struct rspamd_task *task = lua_check_task (L, 1);
  1431. if (task != NULL) {
  1432. ppool = lua_newuserdata (L, sizeof (rspamd_mempool_t *));
  1433. rspamd_lua_setclass (L, "rspamd{mempool}", -1);
  1434. *ppool = task->task_pool;
  1435. }
  1436. else {
  1437. return luaL_error (L, "invalid arguments");
  1438. }
  1439. return 1;
  1440. }
  1441. static int
  1442. lua_task_get_session (lua_State * L)
  1443. {
  1444. LUA_TRACE_POINT;
  1445. struct rspamd_async_session **psession;
  1446. struct rspamd_task *task = lua_check_task (L, 1);
  1447. if (task != NULL) {
  1448. psession = lua_newuserdata (L, sizeof (void *));
  1449. rspamd_lua_setclass (L, "rspamd{session}", -1);
  1450. *psession = task->s;
  1451. }
  1452. else {
  1453. return luaL_error (L, "invalid arguments");
  1454. }
  1455. return 1;
  1456. }
  1457. static int
  1458. lua_task_set_session (lua_State * L)
  1459. {
  1460. LUA_TRACE_POINT;
  1461. struct rspamd_async_session *session = lua_check_session (L, 2);
  1462. struct rspamd_task *task = lua_check_task (L, 1);
  1463. if (task != NULL && session != NULL) {
  1464. task->s = session;
  1465. }
  1466. else {
  1467. return luaL_error (L, "invalid arguments");
  1468. }
  1469. return 1;
  1470. }
  1471. static int
  1472. lua_task_get_ev_base (lua_State * L)
  1473. {
  1474. LUA_TRACE_POINT;
  1475. struct event_base **pbase;
  1476. struct rspamd_task *task = lua_check_task (L, 1);
  1477. if (task != NULL) {
  1478. pbase = lua_newuserdata (L, sizeof (struct event_base *));
  1479. rspamd_lua_setclass (L, "rspamd{ev_base}", -1);
  1480. *pbase = task->ev_base;
  1481. }
  1482. else {
  1483. return luaL_error (L, "invalid arguments");
  1484. }
  1485. return 1;
  1486. }
  1487. static int
  1488. lua_task_get_worker (lua_State * L)
  1489. {
  1490. LUA_TRACE_POINT;
  1491. struct rspamd_worker **pworker;
  1492. struct rspamd_task *task = lua_check_task (L, 1);
  1493. if (task != NULL) {
  1494. if (task->worker) {
  1495. pworker = lua_newuserdata (L, sizeof (struct rspamd_worker *));
  1496. rspamd_lua_setclass (L, "rspamd{worker}", -1);
  1497. *pworker = task->worker;
  1498. }
  1499. else {
  1500. lua_pushnil (L);
  1501. }
  1502. }
  1503. else {
  1504. return luaL_error (L, "invalid arguments");
  1505. }
  1506. return 1;
  1507. }
  1508. static gint
  1509. lua_task_insert_result (lua_State * L)
  1510. {
  1511. LUA_TRACE_POINT;
  1512. struct rspamd_task *task = lua_check_task (L, 1);
  1513. const gchar *symbol_name, *param;
  1514. double weight;
  1515. struct rspamd_symbol_result *s;
  1516. enum rspamd_symbol_insert_flags flags = RSPAMD_SYMBOL_INSERT_DEFAULT;
  1517. gint i, top, args_start;
  1518. if (task != NULL) {
  1519. if (lua_isboolean (L, 2)) {
  1520. args_start = 3;
  1521. if (lua_toboolean (L, 2)) {
  1522. flags |= RSPAMD_SYMBOL_INSERT_ENFORCE;
  1523. }
  1524. }
  1525. else {
  1526. args_start = 2;
  1527. }
  1528. symbol_name = rspamd_mempool_strdup (task->task_pool,
  1529. luaL_checkstring (L, args_start));
  1530. weight = luaL_checknumber (L, args_start + 1);
  1531. top = lua_gettop (L);
  1532. s = rspamd_task_insert_result_full (task, symbol_name, weight, NULL, flags);
  1533. /* Get additional options */
  1534. if (s) {
  1535. for (i = args_start + 2; i <= top; i++) {
  1536. if (lua_type (L, i) == LUA_TSTRING) {
  1537. param = luaL_checkstring (L, i);
  1538. rspamd_task_add_result_option (task, s, param);
  1539. }
  1540. else if (lua_type (L, i) == LUA_TTABLE) {
  1541. lua_pushvalue (L, i);
  1542. lua_pushnil (L);
  1543. while (lua_next (L, -2)) {
  1544. param = lua_tostring (L, -1);
  1545. rspamd_task_add_result_option (task, s, param);
  1546. lua_pop (L, 1);
  1547. }
  1548. lua_pop (L, 1);
  1549. }
  1550. }
  1551. }
  1552. }
  1553. else {
  1554. return luaL_error (L, "invalid arguments");
  1555. }
  1556. return 0;
  1557. }
  1558. static gint
  1559. lua_task_adjust_result (lua_State * L)
  1560. {
  1561. LUA_TRACE_POINT;
  1562. struct rspamd_task *task = lua_check_task (L, 1);
  1563. const gchar *symbol_name, *param;
  1564. struct rspamd_metric_result *metric_res;
  1565. struct rspamd_symbol_result *s = NULL;
  1566. double weight;
  1567. gint i, top;
  1568. if (task != NULL) {
  1569. symbol_name = luaL_checkstring (L, 2);
  1570. weight = luaL_checknumber (L, 3);
  1571. top = lua_gettop (L);
  1572. metric_res = task->result;
  1573. if (metric_res) {
  1574. s = rspamd_task_find_symbol_result (task, symbol_name);
  1575. }
  1576. else {
  1577. return luaL_error (L, "no metric result");
  1578. }
  1579. if (s) {
  1580. metric_res->score -= s->score;
  1581. s->score = weight;
  1582. metric_res->score += s->score;
  1583. }
  1584. else {
  1585. return luaL_error (L, "symbol not found: %s", symbol_name);
  1586. }
  1587. /* Get additional options */
  1588. if (s) {
  1589. for (i = 4; i <= top; i++) {
  1590. if (lua_type (L, i) == LUA_TSTRING) {
  1591. param = luaL_checkstring (L, i);
  1592. rspamd_task_add_result_option (task, s, param);
  1593. }
  1594. else if (lua_type (L, i) == LUA_TTABLE) {
  1595. lua_pushvalue (L, i);
  1596. lua_pushnil (L);
  1597. while (lua_next (L, -2)) {
  1598. param = lua_tostring (L, -1);
  1599. rspamd_task_add_result_option (task, s, param);
  1600. lua_pop (L, 1);
  1601. }
  1602. lua_pop (L, 1);
  1603. }
  1604. }
  1605. }
  1606. }
  1607. else {
  1608. return luaL_error (L, "invalid arguments");
  1609. }
  1610. return 0;
  1611. }
  1612. static gint
  1613. lua_task_set_pre_result (lua_State * L)
  1614. {
  1615. LUA_TRACE_POINT;
  1616. struct rspamd_task *task = lua_check_task (L, 1);
  1617. const gchar *message = NULL, *module = NULL;
  1618. gdouble score = NAN;
  1619. struct rspamd_action *action;
  1620. guint priority = RSPAMD_PASSTHROUGH_NORMAL;
  1621. if (task != NULL) {
  1622. if (RSPAMD_TASK_IS_SKIPPED (task)) {
  1623. /* Do not set pre-result for a skipped task */
  1624. return 0;
  1625. }
  1626. if (lua_type (L, 2) == LUA_TSTRING) {
  1627. const gchar *act_name = lua_tostring (L, 2);
  1628. gint internal_type;
  1629. if (strcmp (act_name, "accept") == 0) {
  1630. /* Compatibility! */
  1631. act_name = "no action";
  1632. }
  1633. else if (rspamd_action_from_str (act_name, &internal_type)) {
  1634. /* Compatibility! */
  1635. act_name = rspamd_action_to_str (internal_type);
  1636. }
  1637. action = rspamd_config_get_action (task->cfg, act_name);
  1638. }
  1639. else {
  1640. return luaL_error (L, "invalid arguments");
  1641. }
  1642. if (action == NULL) {
  1643. struct rspamd_action *tmp;
  1644. HASH_ITER (hh, task->cfg->actions, action, tmp) {
  1645. msg_err_task ("known defined action: %s = %f",
  1646. action->name, action->threshold);
  1647. }
  1648. return luaL_error (L, "unknown action %s", lua_tostring (L, 2));
  1649. }
  1650. if (lua_type (L, 3) == LUA_TSTRING) {
  1651. message = lua_tostring (L, 3);
  1652. /* Keep compatibility here :( */
  1653. ucl_object_replace_key (task->messages,
  1654. ucl_object_fromstring_common (message, 0, UCL_STRING_RAW),
  1655. "smtp_message", 0,
  1656. false);
  1657. }
  1658. else {
  1659. message = "unknown reason";
  1660. }
  1661. if (lua_type (L, 4) == LUA_TSTRING) {
  1662. module = lua_tostring (L, 4);
  1663. }
  1664. else {
  1665. module = "Unknown lua";
  1666. }
  1667. if (lua_type (L, 5) == LUA_TNUMBER) {
  1668. score = lua_tonumber (L, 5);
  1669. }
  1670. if (lua_type (L, 6) == LUA_TNUMBER) {
  1671. priority = lua_tonumber (L, 6);
  1672. }
  1673. rspamd_add_passthrough_result (task, action, priority,
  1674. score, rspamd_mempool_strdup (task->task_pool, message),
  1675. rspamd_mempool_strdup (task->task_pool, module));
  1676. /* Don't classify or filter message if pre-filter sets results */
  1677. task->processed_stages |= (RSPAMD_TASK_STAGE_CLASSIFIERS |
  1678. RSPAMD_TASK_STAGE_CLASSIFIERS_PRE |
  1679. RSPAMD_TASK_STAGE_CLASSIFIERS_POST);
  1680. rspamd_symcache_disable_all_symbols (task, task->cfg->cache,
  1681. SYMBOL_TYPE_IDEMPOTENT|SYMBOL_TYPE_IGNORE_PASSTHROUGH|
  1682. SYMBOL_TYPE_POSTFILTER);
  1683. }
  1684. else {
  1685. return luaL_error (L, "invalid arguments");
  1686. }
  1687. return 0;
  1688. }
  1689. static gint
  1690. lua_task_has_pre_result (lua_State * L)
  1691. {
  1692. LUA_TRACE_POINT;
  1693. struct rspamd_task *task = lua_check_task (L, 1);
  1694. if (task) {
  1695. lua_pushboolean (L, task->result->passthrough_result != NULL);
  1696. }
  1697. else {
  1698. return luaL_error (L, "invalid arguments");
  1699. }
  1700. return 1;
  1701. }
  1702. static gint
  1703. lua_task_append_message (lua_State * L)
  1704. {
  1705. LUA_TRACE_POINT;
  1706. struct rspamd_task *task = lua_check_task (L, 1);
  1707. const gchar *message = luaL_checkstring (L, 2), *category;
  1708. if (task != NULL) {
  1709. if (lua_type (L, 3) == LUA_TSTRING) {
  1710. category = luaL_checkstring (L, 3);
  1711. }
  1712. else {
  1713. category = "unknown";
  1714. }
  1715. ucl_object_insert_key (task->messages,
  1716. ucl_object_fromstring_common (message, 0, UCL_STRING_RAW),
  1717. category, 0,
  1718. true);
  1719. }
  1720. else {
  1721. return luaL_error (L, "invalid arguments");
  1722. }
  1723. return 0;
  1724. }
  1725. struct lua_tree_cb_data {
  1726. lua_State *L;
  1727. int i;
  1728. gint mask;
  1729. };
  1730. static void
  1731. lua_tree_url_callback (gpointer key, gpointer value, gpointer ud)
  1732. {
  1733. struct rspamd_lua_url *lua_url;
  1734. struct rspamd_url *url = (struct rspamd_url *)value;
  1735. struct lua_tree_cb_data *cb = ud;
  1736. if (url->protocol & cb->mask) {
  1737. lua_url = lua_newuserdata (cb->L, sizeof (struct rspamd_lua_url));
  1738. rspamd_lua_setclass (cb->L, "rspamd{url}", -1);
  1739. lua_url->url = url;
  1740. lua_rawseti (cb->L, -2, cb->i++);
  1741. }
  1742. }
  1743. static gint
  1744. lua_task_get_urls (lua_State * L)
  1745. {
  1746. LUA_TRACE_POINT;
  1747. struct rspamd_task *task = lua_check_task (L, 1);
  1748. struct lua_tree_cb_data cb;
  1749. gint protocols_mask = 0;
  1750. static const gint default_mask = PROTOCOL_HTTP|PROTOCOL_HTTPS|
  1751. PROTOCOL_FILE|PROTOCOL_FTP;
  1752. gsize sz;
  1753. if (task) {
  1754. if (lua_gettop (L) >= 2) {
  1755. if (lua_type (L, 2) == LUA_TBOOLEAN) {
  1756. protocols_mask = default_mask;
  1757. if (lua_toboolean (L, 2)) {
  1758. protocols_mask |= PROTOCOL_MAILTO;
  1759. }
  1760. }
  1761. else if (lua_type (L, 2) == LUA_TTABLE) {
  1762. for (lua_pushnil (L); lua_next (L, 2); lua_pop (L, 1)) {
  1763. int nmask;
  1764. const gchar *pname = lua_tostring (L, -1);
  1765. nmask = rspamd_url_protocol_from_string (pname);
  1766. if (nmask != PROTOCOL_UNKNOWN) {
  1767. protocols_mask |= nmask;
  1768. }
  1769. else {
  1770. msg_info ("bad url protocol: %s", pname);
  1771. }
  1772. }
  1773. }
  1774. else if (lua_type (L, 2) == LUA_TSTRING) {
  1775. const gchar *plist = lua_tostring (L, 2);
  1776. gchar **strvec;
  1777. gchar * const *cvec;
  1778. strvec = g_strsplit_set (plist, ",;", -1);
  1779. cvec = strvec;
  1780. while (*cvec) {
  1781. int nmask;
  1782. nmask = rspamd_url_protocol_from_string (*cvec);
  1783. if (nmask != PROTOCOL_UNKNOWN) {
  1784. protocols_mask |= nmask;
  1785. }
  1786. else {
  1787. msg_info ("bad url protocol: %s", *cvec);
  1788. }
  1789. cvec ++;
  1790. }
  1791. g_strfreev (strvec);
  1792. }
  1793. else {
  1794. protocols_mask = default_mask;
  1795. }
  1796. }
  1797. else {
  1798. protocols_mask = default_mask;
  1799. }
  1800. cb.i = 1;
  1801. cb.L = L;
  1802. cb.mask = protocols_mask;
  1803. if (protocols_mask & PROTOCOL_MAILTO) {
  1804. sz = g_hash_table_size (task->urls) + g_hash_table_size (task->emails);
  1805. if (protocols_mask == (default_mask|PROTOCOL_MAILTO)) {
  1806. /* Can use cached version */
  1807. if (!lua_task_get_cached (L, task, "emails+urls", sz)) {
  1808. lua_createtable (L, sz, 0);
  1809. g_hash_table_foreach (task->urls, lua_tree_url_callback, &cb);
  1810. g_hash_table_foreach (task->emails, lua_tree_url_callback, &cb);
  1811. lua_task_set_cached (L, task, "emails+urls", -1, sz);
  1812. }
  1813. }
  1814. else {
  1815. lua_createtable (L, sz, 0);
  1816. g_hash_table_foreach (task->urls, lua_tree_url_callback, &cb);
  1817. g_hash_table_foreach (task->emails, lua_tree_url_callback, &cb);
  1818. }
  1819. }
  1820. else {
  1821. sz = g_hash_table_size (task->urls);
  1822. if (protocols_mask == (default_mask)) {
  1823. if (!lua_task_get_cached (L, task, "urls", sz)) {
  1824. lua_createtable (L, sz, 0);
  1825. g_hash_table_foreach (task->urls, lua_tree_url_callback, &cb);
  1826. lua_task_set_cached (L, task, "urls", -1, sz);
  1827. }
  1828. }
  1829. else {
  1830. lua_createtable (L, sz, 0);
  1831. g_hash_table_foreach (task->urls, lua_tree_url_callback, &cb);
  1832. }
  1833. }
  1834. }
  1835. else {
  1836. return luaL_error (L, "invalid arguments");
  1837. }
  1838. return 1;
  1839. }
  1840. static gint
  1841. lua_task_has_urls (lua_State * L)
  1842. {
  1843. LUA_TRACE_POINT;
  1844. struct rspamd_task *task = lua_check_task (L, 1);
  1845. gboolean need_emails = FALSE, ret = FALSE;
  1846. if (task) {
  1847. if (lua_gettop (L) >= 2) {
  1848. need_emails = lua_toboolean (L, 2);
  1849. }
  1850. if (g_hash_table_size (task->urls) > 0) {
  1851. ret = TRUE;
  1852. }
  1853. if (need_emails && g_hash_table_size (task->emails) > 0) {
  1854. ret = TRUE;
  1855. }
  1856. }
  1857. else {
  1858. return luaL_error (L, "invalid arguments");
  1859. }
  1860. lua_pushboolean (L, ret);
  1861. return 1;
  1862. }
  1863. static gint
  1864. lua_task_get_content (lua_State * L)
  1865. {
  1866. LUA_TRACE_POINT;
  1867. struct rspamd_task *task = lua_check_task (L, 1);
  1868. struct rspamd_lua_text *t;
  1869. if (task) {
  1870. t = lua_newuserdata (L, sizeof (*t));
  1871. rspamd_lua_setclass (L, "rspamd{text}", -1);
  1872. t->len = task->msg.len;
  1873. t->start = task->msg.begin;
  1874. t->flags = 0;
  1875. }
  1876. else {
  1877. return luaL_error (L, "invalid arguments");
  1878. }
  1879. return 1;
  1880. }
  1881. static gint
  1882. lua_task_get_filename (lua_State * L)
  1883. {
  1884. LUA_TRACE_POINT;
  1885. struct rspamd_task *task = lua_check_task (L, 1);
  1886. if (task) {
  1887. if (task->msg.fpath) {
  1888. lua_pushstring (L, task->msg.fpath);
  1889. }
  1890. else {
  1891. lua_pushnil (L);
  1892. }
  1893. }
  1894. else {
  1895. return luaL_error (L, "invalid arguments");
  1896. }
  1897. return 1;
  1898. }
  1899. static gint
  1900. lua_task_get_rawbody (lua_State * L)
  1901. {
  1902. LUA_TRACE_POINT;
  1903. struct rspamd_task *task = lua_check_task (L, 1);
  1904. struct rspamd_lua_text *t;
  1905. if (task) {
  1906. t = lua_newuserdata (L, sizeof (*t));
  1907. rspamd_lua_setclass (L, "rspamd{text}", -1);
  1908. if (task->raw_headers_content.len > 0) {
  1909. g_assert (task->raw_headers_content.len <= task->msg.len);
  1910. t->start = task->msg.begin + task->raw_headers_content.len;
  1911. t->len = task->msg.len - task->raw_headers_content.len;
  1912. }
  1913. else {
  1914. t->len = task->msg.len;
  1915. t->start = task->msg.begin;
  1916. }
  1917. t->flags = 0;
  1918. }
  1919. else {
  1920. return luaL_error (L, "invalid arguments");
  1921. }
  1922. return 1;
  1923. }
  1924. static gint
  1925. lua_task_get_emails (lua_State * L)
  1926. {
  1927. LUA_TRACE_POINT;
  1928. struct rspamd_task *task = lua_check_task (L, 1);
  1929. struct lua_tree_cb_data cb;
  1930. if (task) {
  1931. lua_createtable (L, g_hash_table_size (task->emails), 0);
  1932. cb.i = 1;
  1933. cb.L = L;
  1934. cb.mask = PROTOCOL_MAILTO;
  1935. g_hash_table_foreach (task->emails, lua_tree_url_callback, &cb);
  1936. }
  1937. else {
  1938. return luaL_error (L, "invalid arguments");
  1939. }
  1940. return 1;
  1941. }
  1942. static gint
  1943. lua_task_get_text_parts (lua_State * L)
  1944. {
  1945. LUA_TRACE_POINT;
  1946. guint i;
  1947. struct rspamd_task *task = lua_check_task (L, 1);
  1948. struct rspamd_mime_text_part *part, **ppart;
  1949. if (task != NULL) {
  1950. if (!lua_task_get_cached (L, task, "text_parts", task->text_parts->len)) {
  1951. lua_createtable (L, task->text_parts->len, 0);
  1952. for (i = 0; i < task->text_parts->len; i ++) {
  1953. part = g_ptr_array_index (task->text_parts, i);
  1954. ppart = lua_newuserdata (L, sizeof (struct rspamd_mime_text_part *));
  1955. *ppart = part;
  1956. rspamd_lua_setclass (L, "rspamd{textpart}", -1);
  1957. /* Make it array */
  1958. lua_rawseti (L, -2, i + 1);
  1959. }
  1960. lua_task_set_cached (L, task, "text_parts", -1, task->text_parts->len);
  1961. }
  1962. }
  1963. else {
  1964. return luaL_error (L, "invalid arguments");
  1965. }
  1966. return 1;
  1967. }
  1968. static gint
  1969. lua_task_get_parts (lua_State * L)
  1970. {
  1971. LUA_TRACE_POINT;
  1972. guint i;
  1973. struct rspamd_task *task = lua_check_task (L, 1);
  1974. struct rspamd_mime_part *part, **ppart;
  1975. if (task != NULL) {
  1976. if (!lua_task_get_cached (L, task, "mime_parts", task->parts->len)) {
  1977. lua_createtable (L, task->parts->len, 0);
  1978. for (i = 0; i < task->parts->len; i ++) {
  1979. part = g_ptr_array_index (task->parts, i);
  1980. ppart = lua_newuserdata (L, sizeof (struct rspamd_mime_part *));
  1981. *ppart = part;
  1982. rspamd_lua_setclass (L, "rspamd{mimepart}", -1);
  1983. /* Make it array */
  1984. lua_rawseti (L, -2, i + 1);
  1985. }
  1986. lua_task_set_cached (L, task, "mime_parts", -1, task->parts->len);
  1987. }
  1988. }
  1989. else {
  1990. return luaL_error (L, "invalid arguments");
  1991. }
  1992. return 1;
  1993. }
  1994. static gint
  1995. lua_task_get_request_header (lua_State *L)
  1996. {
  1997. LUA_TRACE_POINT;
  1998. rspamd_ftok_t *hdr;
  1999. struct rspamd_task *task = lua_check_task (L, 1);
  2000. const gchar *s;
  2001. struct rspamd_lua_text *t;
  2002. s = luaL_checkstring (L, 2);
  2003. if (s && task) {
  2004. hdr = rspamd_task_get_request_header (task, s);
  2005. if (hdr) {
  2006. t = lua_newuserdata (L, sizeof (*t));
  2007. rspamd_lua_setclass (L, "rspamd{text}", -1);
  2008. t->start = hdr->begin;
  2009. t->len = hdr->len;
  2010. t->flags = 0;
  2011. return 1;
  2012. }
  2013. else {
  2014. lua_pushnil (L);
  2015. }
  2016. }
  2017. else {
  2018. return luaL_error (L, "invalid arguments");
  2019. }
  2020. return 1;
  2021. }
  2022. static gint
  2023. lua_task_set_request_header (lua_State *L)
  2024. {
  2025. LUA_TRACE_POINT;
  2026. struct rspamd_task *task = lua_check_task (L, 1);
  2027. const gchar *s, *v = NULL;
  2028. rspamd_fstring_t *buf;
  2029. struct rspamd_lua_text *t;
  2030. rspamd_ftok_t *hdr, *new_name;
  2031. gsize len, vlen;
  2032. s = luaL_checklstring (L, 2, &len);
  2033. if (s && task) {
  2034. if (lua_type (L, 3) == LUA_TSTRING) {
  2035. v = luaL_checklstring (L, 3, &vlen);
  2036. }
  2037. else if (lua_type (L, 3) == LUA_TUSERDATA) {
  2038. t = lua_check_text (L, 3);
  2039. if (t != NULL) {
  2040. v = t->start;
  2041. vlen = t->len;
  2042. }
  2043. }
  2044. if (v != NULL) {
  2045. buf = rspamd_fstring_new_init (v, vlen);
  2046. hdr = rspamd_ftok_map (buf);
  2047. buf = rspamd_fstring_new_init (s, len);
  2048. new_name = rspamd_ftok_map (buf);
  2049. rspamd_task_add_request_header (task, new_name, hdr);
  2050. }
  2051. }
  2052. else {
  2053. return luaL_error (L, "invalid arguments");
  2054. }
  2055. return 0;
  2056. }
  2057. gint
  2058. rspamd_lua_push_header (lua_State *L, struct rspamd_mime_header *rh,
  2059. enum rspamd_lua_task_header_type how)
  2060. {
  2061. LUA_TRACE_POINT;
  2062. switch (how) {
  2063. case RSPAMD_TASK_HEADER_PUSH_FULL:
  2064. /* Create new associated table for a header */
  2065. lua_createtable (L, 0, 7);
  2066. rspamd_lua_table_set (L, "name", rh->name);
  2067. if (rh->value) {
  2068. rspamd_lua_table_set (L, "value", rh->value);
  2069. }
  2070. if (rh->raw_len > 0) {
  2071. lua_pushstring (L, "raw");
  2072. lua_pushlstring (L, rh->raw_value, rh->raw_len);
  2073. lua_settable (L, -3);
  2074. }
  2075. if (rh->decoded) {
  2076. rspamd_lua_table_set (L, "decoded", rh->decoded);
  2077. }
  2078. lua_pushstring (L, "tab_separated");
  2079. lua_pushboolean (L, rh->tab_separated);
  2080. lua_settable (L, -3);
  2081. lua_pushstring (L, "empty_separator");
  2082. lua_pushboolean (L, rh->empty_separator);
  2083. lua_settable (L, -3);
  2084. rspamd_lua_table_set (L, "separator", rh->separator);
  2085. lua_pushstring (L, "order");
  2086. lua_pushinteger (L, rh->order);
  2087. lua_settable (L, -3);
  2088. break;
  2089. case RSPAMD_TASK_HEADER_PUSH_RAW:
  2090. if (rh->value) {
  2091. lua_pushstring (L, rh->value);
  2092. }
  2093. else {
  2094. lua_pushnil (L);
  2095. }
  2096. break;
  2097. case RSPAMD_TASK_HEADER_PUSH_SIMPLE:
  2098. if (rh->decoded) {
  2099. lua_pushstring (L, rh->decoded);
  2100. }
  2101. else {
  2102. lua_pushnil (L);
  2103. }
  2104. break;
  2105. case RSPAMD_TASK_HEADER_PUSH_COUNT:
  2106. default:
  2107. g_assert_not_reached ();
  2108. break;
  2109. }
  2110. return 1;
  2111. }
  2112. gint
  2113. rspamd_lua_push_header_array (lua_State * L,
  2114. GPtrArray *ar,
  2115. enum rspamd_lua_task_header_type how)
  2116. {
  2117. LUA_TRACE_POINT;
  2118. struct rspamd_mime_header *rh;
  2119. guint i;
  2120. if (ar == NULL || ar->len == 0) {
  2121. if (how == RSPAMD_TASK_HEADER_PUSH_COUNT) {
  2122. lua_pushnumber (L, 0);
  2123. }
  2124. else {
  2125. lua_pushnil (L);
  2126. }
  2127. return 1;
  2128. }
  2129. if (how == RSPAMD_TASK_HEADER_PUSH_FULL) {
  2130. lua_createtable (L, ar->len, 0);
  2131. PTR_ARRAY_FOREACH (ar, i, rh) {
  2132. rspamd_lua_push_header (L, rh, how);
  2133. lua_rawseti (L, -2, i + 1);
  2134. }
  2135. }
  2136. else if (how == RSPAMD_TASK_HEADER_PUSH_COUNT) {
  2137. lua_pushinteger (L, ar->len);
  2138. }
  2139. else {
  2140. rh = g_ptr_array_index (ar, 0);
  2141. return rspamd_lua_push_header (L, rh, how);
  2142. }
  2143. return 1;
  2144. }
  2145. static gint
  2146. lua_task_get_header_common (lua_State *L, enum rspamd_lua_task_header_type how)
  2147. {
  2148. LUA_TRACE_POINT;
  2149. gboolean strong = FALSE;
  2150. struct rspamd_task *task = lua_check_task (L, 1);
  2151. const gchar *name;
  2152. GPtrArray *ar;
  2153. name = luaL_checkstring (L, 2);
  2154. if (name && task) {
  2155. if (lua_gettop (L) == 3) {
  2156. strong = lua_toboolean (L, 3);
  2157. }
  2158. ar = rspamd_message_get_header_array (task, name, strong);
  2159. return rspamd_lua_push_header_array (L, ar, how);
  2160. }
  2161. else {
  2162. return luaL_error (L, "invalid arguments");
  2163. }
  2164. }
  2165. static gint
  2166. lua_task_get_header_full (lua_State * L)
  2167. {
  2168. return lua_task_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_FULL);
  2169. }
  2170. static gint
  2171. lua_task_get_header (lua_State * L)
  2172. {
  2173. return lua_task_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_SIMPLE);
  2174. }
  2175. static gint
  2176. lua_task_get_header_raw (lua_State * L)
  2177. {
  2178. return lua_task_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_RAW);
  2179. }
  2180. static gint
  2181. lua_task_get_header_count (lua_State * L)
  2182. {
  2183. return lua_task_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_COUNT);
  2184. }
  2185. static gint
  2186. lua_task_get_raw_headers (lua_State *L)
  2187. {
  2188. LUA_TRACE_POINT;
  2189. struct rspamd_task *task = lua_check_task (L, 1);
  2190. struct rspamd_lua_text *t;
  2191. if (task) {
  2192. t = lua_newuserdata (L, sizeof (*t));
  2193. rspamd_lua_setclass (L, "rspamd{text}", -1);
  2194. t->start = task->raw_headers_content.begin;
  2195. t->len = task->raw_headers_content.len;
  2196. t->flags = 0;
  2197. }
  2198. else {
  2199. return luaL_error (L, "invalid arguments");
  2200. }
  2201. return 1;
  2202. }
  2203. static gint
  2204. lua_task_get_received_headers (lua_State * L)
  2205. {
  2206. LUA_TRACE_POINT;
  2207. struct rspamd_task *task = lua_check_task (L, 1);
  2208. struct received_header *rh;
  2209. const gchar *proto;
  2210. guint i, k = 1;
  2211. if (task) {
  2212. if (!lua_task_get_cached (L, task, "received", task->received->len)) {
  2213. lua_createtable (L, task->received->len, 0);
  2214. for (i = 0; i < task->received->len; i ++) {
  2215. rh = g_ptr_array_index (task->received, i);
  2216. lua_createtable (L, 0, 10);
  2217. if (rh->hdr && rh->hdr->decoded) {
  2218. rspamd_lua_table_set (L, "raw", rh->hdr->decoded);
  2219. }
  2220. lua_pushstring (L, "flags");
  2221. lua_createtable (L, 0, 3);
  2222. lua_pushstring (L, "artificial");
  2223. if (rh->flags & RSPAMD_RECEIVED_FLAG_ARTIFICIAL) {
  2224. lua_pushboolean (L, true);
  2225. }
  2226. else {
  2227. lua_pushboolean (L, false);
  2228. }
  2229. lua_settable (L, -3);
  2230. lua_pushstring (L, "authenticated");
  2231. if (rh->flags & RSPAMD_RECEIVED_FLAG_AUTHENTICATED) {
  2232. lua_pushboolean (L, true);
  2233. }
  2234. else {
  2235. lua_pushboolean (L, false);
  2236. }
  2237. lua_settable (L, -3);
  2238. lua_pushstring (L, "ssl");
  2239. if (rh->flags & RSPAMD_RECEIVED_FLAG_SSL) {
  2240. lua_pushboolean (L, true);
  2241. }
  2242. else {
  2243. lua_pushboolean (L, false);
  2244. }
  2245. lua_settable (L, -3);
  2246. lua_settable (L, -3);
  2247. if (G_UNLIKELY (rh->from_ip == NULL &&
  2248. rh->real_ip == NULL &&
  2249. rh->real_hostname == NULL &&
  2250. rh->by_hostname == NULL && rh->timestamp == 0 &&
  2251. rh->for_mbox == NULL)) {
  2252. lua_rawseti (L, -2, k ++);
  2253. continue;
  2254. }
  2255. rspamd_lua_table_set (L, "from_hostname", rh->from_hostname);
  2256. rspamd_lua_table_set (L, "from_ip", rh->from_ip);
  2257. rspamd_lua_table_set (L, "real_hostname", rh->real_hostname);
  2258. lua_pushstring (L, "real_ip");
  2259. rspamd_lua_ip_push (L, rh->addr);
  2260. lua_settable (L, -3);
  2261. lua_pushstring (L, "proto");
  2262. switch (rh->type) {
  2263. case RSPAMD_RECEIVED_SMTP:
  2264. proto = "smtp";
  2265. break;
  2266. case RSPAMD_RECEIVED_ESMTP:
  2267. proto = "esmtp";
  2268. break;
  2269. case RSPAMD_RECEIVED_ESMTPS:
  2270. proto = "esmtps";
  2271. break;
  2272. case RSPAMD_RECEIVED_ESMTPA:
  2273. proto = "esmtpa";
  2274. break;
  2275. case RSPAMD_RECEIVED_ESMTPSA:
  2276. proto = "esmtpsa";
  2277. break;
  2278. case RSPAMD_RECEIVED_LMTP:
  2279. proto = "lmtp";
  2280. break;
  2281. case RSPAMD_RECEIVED_IMAP:
  2282. proto = "imap";
  2283. break;
  2284. case RSPAMD_RECEIVED_HTTP:
  2285. proto = "http";
  2286. break;
  2287. case RSPAMD_RECEIVED_LOCAL:
  2288. proto = "local";
  2289. break;
  2290. case RSPAMD_RECEIVED_MAPI:
  2291. proto = "mapi";
  2292. break;
  2293. case RSPAMD_RECEIVED_UNKNOWN:
  2294. default:
  2295. proto = "unknown";
  2296. break;
  2297. }
  2298. lua_pushstring (L, proto);
  2299. lua_settable (L, -3);
  2300. lua_pushstring (L, "timestamp");
  2301. lua_pushinteger (L, rh->timestamp);
  2302. lua_settable (L, -3);
  2303. rspamd_lua_table_set (L, "by_hostname", rh->by_hostname);
  2304. rspamd_lua_table_set (L, "for", rh->for_mbox);
  2305. lua_rawseti (L, -2, k ++);
  2306. }
  2307. lua_task_set_cached (L, task, "received", -1, task->received->len);
  2308. }
  2309. }
  2310. else {
  2311. return luaL_error (L, "invalid arguments");
  2312. }
  2313. return 1;
  2314. }
  2315. static gint
  2316. lua_task_get_queue_id (lua_State *L)
  2317. {
  2318. LUA_TRACE_POINT;
  2319. struct rspamd_task *task = lua_check_task (L, 1);
  2320. if (task) {
  2321. if (task->queue_id != NULL && strcmp (task->queue_id, "undef") != 0) {
  2322. lua_pushstring (L, task->queue_id);
  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_uid (lua_State *L)
  2335. {
  2336. LUA_TRACE_POINT;
  2337. struct rspamd_task *task = lua_check_task (L, 1);
  2338. if (task) {
  2339. lua_pushstring (L, task->task_pool->tag.uid);
  2340. }
  2341. else {
  2342. return luaL_error (L, "invalid arguments");
  2343. }
  2344. return 1;
  2345. }
  2346. static gint
  2347. lua_task_get_resolver (lua_State *L)
  2348. {
  2349. LUA_TRACE_POINT;
  2350. struct rspamd_task *task = lua_check_task (L, 1);
  2351. struct rspamd_dns_resolver **presolver;
  2352. if (task != NULL && task->resolver != NULL) {
  2353. presolver = lua_newuserdata (L, sizeof (void *));
  2354. rspamd_lua_setclass (L, "rspamd{resolver}", -1);
  2355. *presolver = task->resolver;
  2356. }
  2357. else {
  2358. return luaL_error (L, "invalid arguments");
  2359. }
  2360. return 1;
  2361. }
  2362. static gint
  2363. lua_task_set_resolver (lua_State *L)
  2364. {
  2365. LUA_TRACE_POINT;
  2366. struct rspamd_task *task = lua_check_task (L, 1);
  2367. struct rspamd_dns_resolver *resolver = lua_check_dns_resolver (L, 2);
  2368. if (task != NULL && resolver != NULL) {
  2369. task->resolver = resolver;
  2370. }
  2371. else {
  2372. return luaL_error (L, "invalid arguments");
  2373. }
  2374. return 0;
  2375. }
  2376. static gint
  2377. lua_task_inc_dns_req (lua_State *L)
  2378. {
  2379. LUA_TRACE_POINT;
  2380. struct rspamd_task *task = lua_check_task (L, 1);
  2381. static guint warning_shown = 0;
  2382. if (warning_shown < 100) {
  2383. warning_shown ++;
  2384. msg_warn_task_check ("task:inc_dns_req is deprecated and should not be used");
  2385. }
  2386. if (task != NULL) {
  2387. /* Deprecation: already done in rspamd_dns_resolver_request */
  2388. }
  2389. else {
  2390. return luaL_error (L, "invalid arguments");
  2391. }
  2392. return 0;
  2393. }
  2394. static gint
  2395. lua_task_get_dns_req (lua_State *L)
  2396. {
  2397. LUA_TRACE_POINT;
  2398. struct rspamd_task *task = lua_check_task (L, 1);
  2399. if (task != NULL) {
  2400. lua_pushinteger (L, task->dns_requests);
  2401. }
  2402. else {
  2403. return luaL_error (L, "invalid arguments");
  2404. }
  2405. return 1;
  2406. }
  2407. enum rspamd_address_type {
  2408. RSPAMD_ADDRESS_ANY = 0,
  2409. RSPAMD_ADDRESS_SMTP = 1,
  2410. RSPAMD_ADDRESS_MIME = 2,
  2411. RSPAMD_ADDRESS_MASK = 0x3FF,
  2412. RSPAMD_ADDRESS_RAW = (1u << 10),
  2413. RSPAMD_ADDRESS_ORIGINAL = (1u << 11),
  2414. RSPAMD_ADDRESS_MAX = RSPAMD_ADDRESS_MASK,
  2415. };
  2416. /*
  2417. * Convert element at the specified position to the type
  2418. * for get_from/get_recipients
  2419. */
  2420. static enum rspamd_address_type
  2421. lua_task_str_to_get_type (lua_State *L, struct rspamd_task *task, gint pos)
  2422. {
  2423. const gchar *type = NULL;
  2424. gint ret = RSPAMD_ADDRESS_ANY;
  2425. guint64 h;
  2426. gsize sz;
  2427. /* Get what value */
  2428. if (lua_type (L, pos) == LUA_TNUMBER) {
  2429. ret = lua_tonumber (L, pos);
  2430. if (ret >= RSPAMD_ADDRESS_ANY && ret < RSPAMD_ADDRESS_MAX) {
  2431. return ret;
  2432. }
  2433. return RSPAMD_ADDRESS_ANY;
  2434. }
  2435. else if (lua_type (L, pos) == LUA_TSTRING) {
  2436. type = lua_tolstring (L, pos, &sz);
  2437. if (type && sz > 0) {
  2438. h = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64,
  2439. type, sz, 0xdeadbabe);
  2440. switch (h) {
  2441. case 0xDA081341FB600389ULL: /* mime */
  2442. ret = RSPAMD_ADDRESS_MIME;
  2443. break;
  2444. case 0xEEC8A7832F8C43ACULL: /* any */
  2445. ret = RSPAMD_ADDRESS_ANY;
  2446. break;
  2447. case 0x472274D5193B2A80ULL: /* smtp */
  2448. case 0xEFE0F586CC9F14A9ULL: /* envelope */
  2449. ret = RSPAMD_ADDRESS_SMTP;
  2450. break;
  2451. default:
  2452. msg_err_task ("invalid email type: %*s", (gint)sz, type);
  2453. break;
  2454. }
  2455. }
  2456. }
  2457. else if (lua_type (L, pos) == LUA_TTABLE) {
  2458. for (lua_pushnil (L); lua_next (L, pos); lua_pop (L, 1)) {
  2459. type = lua_tolstring (L, pos, &sz);
  2460. if (type && sz > 0) {
  2461. h = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64,
  2462. type, sz, 0xdeadbabe);
  2463. switch (h) {
  2464. case 0xDA081341FB600389ULL: /* mime */
  2465. ret |= RSPAMD_ADDRESS_MIME;
  2466. break;
  2467. case 0xEEC8A7832F8C43ACULL: /* any */
  2468. ret |= RSPAMD_ADDRESS_ANY;
  2469. break;
  2470. case 0x472274D5193B2A80ULL: /* smtp */
  2471. case 0xEFE0F586CC9F14A9ULL: /* envelope */
  2472. ret |= RSPAMD_ADDRESS_SMTP;
  2473. break;
  2474. case 0xAF4DE083D9AD0132: /* raw */
  2475. ret |= RSPAMD_ADDRESS_RAW;
  2476. break;
  2477. case 0xC7AB6C7B7B0F5A8A: /* orig */
  2478. case 0x1778AE905589E431: /* original */
  2479. ret |= RSPAMD_ADDRESS_ORIGINAL;
  2480. break;
  2481. default:
  2482. msg_err_task ("invalid email type: %*s", (gint)sz, type);
  2483. break;
  2484. }
  2485. }
  2486. }
  2487. }
  2488. return ret;
  2489. }
  2490. #define EMAIL_CHECK_FLAG(fl, str) do { \
  2491. if (addr->flags & (fl)) { \
  2492. lua_pushstring (L, (str)); \
  2493. lua_pushboolean (L, true); \
  2494. lua_settable (L, -3); \
  2495. } \
  2496. } while(0)
  2497. static void
  2498. lua_push_email_address (lua_State *L, struct rspamd_email_address *addr)
  2499. {
  2500. if (addr) {
  2501. lua_createtable (L, 0, 4);
  2502. if (addr->addr_len > 0) {
  2503. lua_pushstring (L, "addr");
  2504. lua_pushlstring (L, addr->addr, addr->addr_len);
  2505. lua_settable (L, -3);
  2506. }
  2507. else {
  2508. lua_pushstring (L, "addr");
  2509. lua_pushstring (L, "");
  2510. lua_settable (L, -3);
  2511. }
  2512. if (addr->domain_len > 0) {
  2513. lua_pushstring (L, "domain");
  2514. lua_pushlstring (L, addr->domain, addr->domain_len);
  2515. lua_settable (L, -3);
  2516. }
  2517. else {
  2518. lua_pushstring (L, "domain");
  2519. lua_pushstring (L, "");
  2520. lua_settable (L, -3);
  2521. }
  2522. if (addr->user_len > 0) {
  2523. lua_pushstring (L, "user");
  2524. lua_pushlstring (L, addr->user, addr->user_len);
  2525. lua_settable (L, -3);
  2526. }
  2527. else {
  2528. lua_pushstring (L, "user");
  2529. lua_pushstring (L, "");
  2530. lua_settable (L, -3);
  2531. }
  2532. if (addr->name) {
  2533. lua_pushstring (L, "name");
  2534. lua_pushstring (L, addr->name);
  2535. lua_settable (L, -3);
  2536. }
  2537. else {
  2538. lua_pushstring (L, "name");
  2539. lua_pushstring (L, "");
  2540. lua_settable (L, -3);
  2541. }
  2542. lua_pushstring (L, "flags");
  2543. lua_createtable (L, 0, 7);
  2544. EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_VALID, "valid");
  2545. EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_IP, "ip");
  2546. EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_BRACED, "braced");
  2547. EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_QUOTED, "quoted");
  2548. EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_EMPTY, "empty");
  2549. EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_HAS_BACKSLASH, "backslash");
  2550. EMAIL_CHECK_FLAG (RSPAMD_EMAIL_ADDR_HAS_8BIT, "8bit");
  2551. lua_settable (L, -3);
  2552. }
  2553. }
  2554. void
  2555. lua_push_emails_address_list (lua_State *L, GPtrArray *addrs, int flags)
  2556. {
  2557. struct rspamd_email_address *addr;
  2558. guint i, pos = 1;
  2559. lua_createtable (L, addrs->len, 0);
  2560. for (i = 0; i < addrs->len; i ++) {
  2561. addr = g_ptr_array_index (addrs, i);
  2562. if (addr->flags & RSPAMD_EMAIL_ADDR_ORIGINAL) {
  2563. if (flags & RSPAMD_ADDRESS_ORIGINAL) {
  2564. lua_push_email_address (L, addr);
  2565. lua_rawseti (L, -2, pos);
  2566. pos++;
  2567. }
  2568. }
  2569. else {
  2570. lua_push_email_address (L, addr);
  2571. lua_rawseti (L, -2, pos);
  2572. pos++;
  2573. }
  2574. }
  2575. }
  2576. static gboolean
  2577. lua_import_email_address (lua_State *L, struct rspamd_task *task,
  2578. gint pos,
  2579. struct rspamd_email_address **paddr)
  2580. {
  2581. struct rspamd_email_address *addr;
  2582. const gchar *p;
  2583. gchar *dst;
  2584. gsize len;
  2585. g_assert (paddr != NULL);
  2586. if (!lua_istable (L, pos)) {
  2587. return FALSE;
  2588. }
  2589. addr = g_malloc0 (sizeof (*addr));
  2590. lua_pushstring (L, "name");
  2591. lua_gettable (L, pos);
  2592. if (lua_type (L, -1) == LUA_TSTRING) {
  2593. p = lua_tolstring (L, -1, &len);
  2594. dst = rspamd_mempool_alloc (task->task_pool, len + 1);
  2595. rspamd_strlcpy (dst, p, len + 1);
  2596. addr->name = dst;
  2597. }
  2598. lua_pop (L, 1);
  2599. lua_pushstring (L, "user");
  2600. lua_gettable (L, pos);
  2601. if (lua_type (L, -1) == LUA_TSTRING) {
  2602. p = lua_tolstring (L, -1, &len);
  2603. addr->user = (const gchar *)rspamd_mempool_alloc (task->task_pool, len);
  2604. memcpy ((gchar *)addr->user, p, len);
  2605. addr->user_len = len;
  2606. }
  2607. lua_pop (L, 1);
  2608. lua_pushstring (L, "domain");
  2609. lua_gettable (L, pos);
  2610. if (lua_type (L, -1) == LUA_TSTRING) {
  2611. p = lua_tolstring (L, -1, &len);
  2612. addr->domain = (const gchar *)rspamd_mempool_alloc (task->task_pool, len);
  2613. memcpy ((gchar *)addr->domain, p, len);
  2614. addr->domain_len = len;
  2615. }
  2616. lua_pop (L, 1);
  2617. lua_pushstring (L, "addr");
  2618. lua_gettable (L, pos);
  2619. if (lua_type (L, -1) == LUA_TSTRING) {
  2620. p = lua_tolstring (L, -1, &len);
  2621. addr->addr = (const gchar *)rspamd_mempool_alloc (task->task_pool, len);
  2622. memcpy ((gchar *)addr->addr, p, len);
  2623. addr->addr_len = len;
  2624. }
  2625. else {
  2626. /* Construct addr */
  2627. len = addr->domain_len + addr->user_len + 1;
  2628. addr->addr = (const gchar *)rspamd_mempool_alloc (task->task_pool, len);
  2629. addr->addr_len = rspamd_snprintf ((gchar *)addr->addr, len, "%*s@%*s",
  2630. (int)addr->user_len, addr->user,
  2631. (int)addr->domain_len, addr->domain);
  2632. }
  2633. lua_pop (L, 1);
  2634. lua_pushstring (L, "raw");
  2635. lua_gettable (L, pos);
  2636. if (lua_type (L, -1) == LUA_TSTRING) {
  2637. gchar *cpy;
  2638. p = lua_tolstring (L, -1, &len);
  2639. cpy = rspamd_mempool_alloc (task->task_pool, len + 1);
  2640. memcpy (cpy, p, len);
  2641. cpy[len] = '\0';
  2642. addr->raw_len = len;
  2643. addr->raw = cpy;
  2644. }
  2645. else {
  2646. /* Construct raw addr */
  2647. len = addr->addr_len + 3;
  2648. if (addr->name) {
  2649. len += strlen (addr->name) + 1;
  2650. dst = rspamd_mempool_alloc (task->task_pool, len + 1);
  2651. addr->raw_len = rspamd_snprintf (dst, len, "%s <%*s>",
  2652. addr->name,
  2653. (int)addr->addr_len, addr->addr);
  2654. }
  2655. else {
  2656. dst = rspamd_mempool_alloc (task->task_pool, len + 1);
  2657. addr->raw_len = rspamd_snprintf (dst, len, "<%*s@%*s>",
  2658. (int)addr->user_len, addr->user,
  2659. (int)addr->domain_len, addr->domain);
  2660. }
  2661. addr->raw = dst;
  2662. }
  2663. lua_pop (L, 1);
  2664. addr->flags = RSPAMD_EMAIL_ADDR_VALID;
  2665. *paddr = addr;
  2666. return TRUE;
  2667. }
  2668. static gint
  2669. lua_task_get_recipients (lua_State *L)
  2670. {
  2671. LUA_TRACE_POINT;
  2672. struct rspamd_task *task = lua_check_task (L, 1);
  2673. GPtrArray *ptrs = NULL;
  2674. gint what = 0;
  2675. if (task) {
  2676. if (lua_gettop (L) == 2) {
  2677. /* Get what value */
  2678. what = lua_task_str_to_get_type (L, task, 2);
  2679. }
  2680. switch (what & RSPAMD_ADDRESS_MASK) {
  2681. case RSPAMD_ADDRESS_SMTP:
  2682. /* Here we check merely envelope rcpt */
  2683. ptrs = task->rcpt_envelope;
  2684. break;
  2685. case RSPAMD_ADDRESS_MIME:
  2686. /* Here we check merely mime rcpt */
  2687. ptrs = task->rcpt_mime;
  2688. break;
  2689. case RSPAMD_ADDRESS_ANY:
  2690. default:
  2691. if (task->rcpt_envelope) {
  2692. ptrs = task->rcpt_envelope;
  2693. }
  2694. else {
  2695. ptrs = task->rcpt_mime;
  2696. }
  2697. break;
  2698. }
  2699. if (ptrs) {
  2700. lua_push_emails_address_list (L, ptrs, what & ~RSPAMD_ADDRESS_MASK);
  2701. }
  2702. else {
  2703. lua_pushnil (L);
  2704. }
  2705. }
  2706. else {
  2707. return luaL_error (L, "invalid arguments");
  2708. }
  2709. return 1;
  2710. }
  2711. static gint
  2712. lua_task_set_recipients (lua_State *L)
  2713. {
  2714. LUA_TRACE_POINT;
  2715. struct rspamd_task *task = lua_check_task (L, 1);
  2716. GPtrArray *ptrs = NULL;
  2717. struct rspamd_email_address *addr = NULL;
  2718. gint what = 0, pos = 3;
  2719. const gchar *how = "rewrite";
  2720. if (task && lua_gettop (L) >= 3) {
  2721. /* Get what value */
  2722. what = lua_task_str_to_get_type (L, task, 2);
  2723. if (lua_isstring (L, 4)) {
  2724. how = lua_tostring (L, 4);
  2725. }
  2726. switch (what) {
  2727. case RSPAMD_ADDRESS_SMTP:
  2728. /* Here we check merely envelope rcpt */
  2729. ptrs = task->rcpt_envelope;
  2730. break;
  2731. case RSPAMD_ADDRESS_MIME:
  2732. /* Here we check merely mime rcpt */
  2733. ptrs = task->rcpt_mime;
  2734. break;
  2735. case RSPAMD_ADDRESS_ANY:
  2736. default:
  2737. if (task->rcpt_envelope) {
  2738. ptrs = task->rcpt_envelope;
  2739. }
  2740. else {
  2741. ptrs = task->rcpt_mime;
  2742. }
  2743. break;
  2744. }
  2745. if (ptrs) {
  2746. guint i, flags_add = RSPAMD_EMAIL_ADDR_ORIGINAL;
  2747. struct rspamd_email_address *tmp;
  2748. if (strcmp (how, "alias") == 0) {
  2749. flags_add |= RSPAMD_EMAIL_ADDR_ALIASED;
  2750. }
  2751. PTR_ARRAY_FOREACH (ptrs, i, tmp) {
  2752. tmp->flags |= flags_add;
  2753. }
  2754. lua_pushvalue (L, pos);
  2755. for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
  2756. if (lua_import_email_address (L, task, lua_gettop (L), &addr)) {
  2757. g_ptr_array_add (ptrs, addr);
  2758. }
  2759. }
  2760. lua_pop (L, 1);
  2761. lua_pushboolean (L, true);
  2762. }
  2763. else {
  2764. lua_pushboolean (L, false);
  2765. }
  2766. }
  2767. else {
  2768. return luaL_error (L, "invalid arguments");
  2769. }
  2770. return 1;
  2771. }
  2772. #define CHECK_EMAIL_ADDR(addr) do { \
  2773. if (addr == NULL) { \
  2774. ret = 0; \
  2775. } \
  2776. else { \
  2777. ret = addr->flags & RSPAMD_EMAIL_ADDR_VALID; \
  2778. } \
  2779. } while (0)
  2780. #define CHECK_EMAIL_ADDR_LIST(addr) do { \
  2781. if (addr == NULL) { \
  2782. ret = 0; \
  2783. } \
  2784. else { \
  2785. ret = addr->len > 0; \
  2786. } \
  2787. } while (0)
  2788. static gint
  2789. lua_task_has_from (lua_State *L)
  2790. {
  2791. LUA_TRACE_POINT;
  2792. struct rspamd_task *task = lua_check_task (L, 1);
  2793. gint what = 0;
  2794. gboolean ret = FALSE;
  2795. if (task) {
  2796. if (lua_gettop (L) == 2) {
  2797. /* Get what value */
  2798. what = lua_task_str_to_get_type (L, task, 2);
  2799. }
  2800. switch (what & RSPAMD_ADDRESS_MASK) {
  2801. case RSPAMD_ADDRESS_SMTP:
  2802. /* Here we check merely envelope rcpt */
  2803. CHECK_EMAIL_ADDR (task->from_envelope);
  2804. break;
  2805. case RSPAMD_ADDRESS_MIME:
  2806. /* Here we check merely mime rcpt */
  2807. CHECK_EMAIL_ADDR_LIST (task->from_mime);
  2808. break;
  2809. case RSPAMD_ADDRESS_ANY:
  2810. default:
  2811. CHECK_EMAIL_ADDR (task->from_envelope);
  2812. if (!ret) {
  2813. CHECK_EMAIL_ADDR_LIST (task->from_mime);
  2814. }
  2815. break;
  2816. }
  2817. }
  2818. else {
  2819. return luaL_error (L, "invalid arguments");
  2820. }
  2821. lua_pushboolean (L, ret);
  2822. return 1;
  2823. }
  2824. static gint
  2825. lua_task_has_recipients (lua_State *L)
  2826. {
  2827. LUA_TRACE_POINT;
  2828. struct rspamd_task *task = lua_check_task (L, 1);
  2829. gint what = 0;
  2830. gboolean ret = FALSE;
  2831. if (task) {
  2832. if (lua_gettop (L) == 2) {
  2833. /* Get what value */
  2834. what = lua_task_str_to_get_type (L, task, 2);
  2835. }
  2836. switch (what & RSPAMD_ADDRESS_MASK) {
  2837. case RSPAMD_ADDRESS_SMTP:
  2838. /* Here we check merely envelope rcpt */
  2839. CHECK_EMAIL_ADDR_LIST (task->rcpt_envelope);
  2840. break;
  2841. case RSPAMD_ADDRESS_MIME:
  2842. /* Here we check merely mime rcpt */
  2843. CHECK_EMAIL_ADDR_LIST (task->rcpt_mime);
  2844. break;
  2845. case RSPAMD_ADDRESS_ANY:
  2846. default:
  2847. CHECK_EMAIL_ADDR_LIST (task->rcpt_envelope);
  2848. if (!ret) {
  2849. CHECK_EMAIL_ADDR_LIST (task->rcpt_mime);
  2850. }
  2851. break;
  2852. }
  2853. }
  2854. else {
  2855. return luaL_error (L, "invalid arguments");
  2856. }
  2857. lua_pushboolean (L, ret);
  2858. return 1;
  2859. }
  2860. static gint
  2861. lua_task_get_from (lua_State *L)
  2862. {
  2863. LUA_TRACE_POINT;
  2864. struct rspamd_task *task = lua_check_task (L, 1);
  2865. GPtrArray *addrs = NULL;
  2866. struct rspamd_email_address *addr = NULL;
  2867. gint what = 0;
  2868. if (task) {
  2869. if (lua_gettop (L) == 2) {
  2870. /* Get what value */
  2871. what = lua_task_str_to_get_type (L, task, 2);
  2872. }
  2873. switch (what & RSPAMD_ADDRESS_MASK) {
  2874. case RSPAMD_ADDRESS_SMTP:
  2875. /* Here we check merely envelope rcpt */
  2876. addr = task->from_envelope;
  2877. break;
  2878. case RSPAMD_ADDRESS_MIME:
  2879. /* Here we check merely mime rcpt */
  2880. addrs = task->from_mime;
  2881. break;
  2882. case RSPAMD_ADDRESS_ANY:
  2883. default:
  2884. if (task->from_envelope) {
  2885. addr = task->from_envelope;
  2886. }
  2887. else {
  2888. addrs = task->from_mime;
  2889. }
  2890. break;
  2891. }
  2892. if (addrs) {
  2893. lua_push_emails_address_list (L, addrs, what & ~RSPAMD_ADDRESS_MASK);
  2894. }
  2895. else if (addr) {
  2896. /* Create table to preserve compatibility */
  2897. if (addr->addr) {
  2898. lua_createtable (L, 1, 0);
  2899. lua_push_email_address (L, addr);
  2900. lua_rawseti (L, -2, 1);
  2901. }
  2902. else {
  2903. lua_pushnil (L);
  2904. }
  2905. }
  2906. else {
  2907. lua_pushnil (L);
  2908. }
  2909. }
  2910. else {
  2911. return luaL_error (L, "invalid arguments");
  2912. }
  2913. return 1;
  2914. }
  2915. static gint
  2916. lua_task_set_from (lua_State *L)
  2917. {
  2918. LUA_TRACE_POINT;
  2919. struct rspamd_task *task = lua_check_task (L, 1);
  2920. const gchar *how = "rewrite";
  2921. GPtrArray *addrs = NULL;
  2922. struct rspamd_email_address **paddr = NULL, *addr;
  2923. gint what = 0;
  2924. if (task && lua_gettop (L) >= 3) {
  2925. what = lua_task_str_to_get_type (L, task, 2);
  2926. if (lua_isstring (L, 4)) {
  2927. how = lua_tostring (L, 4);
  2928. }
  2929. switch (what & RSPAMD_ADDRESS_MASK) {
  2930. case RSPAMD_ADDRESS_SMTP:
  2931. /* Here we check merely envelope rcpt */
  2932. paddr = &task->from_envelope;
  2933. break;
  2934. case RSPAMD_ADDRESS_MIME:
  2935. /* Here we check merely mime rcpt */
  2936. addrs = task->from_mime;
  2937. break;
  2938. case RSPAMD_ADDRESS_ANY:
  2939. default:
  2940. if (task->from_envelope) {
  2941. paddr = &task->from_envelope;
  2942. }
  2943. else {
  2944. addrs = task->from_mime;
  2945. }
  2946. break;
  2947. }
  2948. if (addrs) {
  2949. if (lua_import_email_address (L, task, 3, &addr)) {
  2950. guint i, flags_add = RSPAMD_EMAIL_ADDR_ORIGINAL;
  2951. struct rspamd_email_address *tmp;
  2952. if (strcmp (how, "alias") == 0) {
  2953. flags_add |= RSPAMD_EMAIL_ADDR_ALIASED;
  2954. }
  2955. PTR_ARRAY_FOREACH (addrs, i, tmp) {
  2956. tmp->flags |= flags_add;
  2957. }
  2958. g_ptr_array_add (addrs, addr);
  2959. lua_pushboolean (L, true);
  2960. }
  2961. else {
  2962. lua_pushboolean (L, false);
  2963. }
  2964. }
  2965. else if (paddr) {
  2966. if (lua_import_email_address (L, task, 3, &addr)) {
  2967. if (paddr) {
  2968. rspamd_email_address_free (*paddr);
  2969. }
  2970. *paddr = addr;
  2971. lua_pushboolean (L, true);
  2972. }
  2973. else {
  2974. lua_pushboolean (L, false);
  2975. }
  2976. }
  2977. else {
  2978. lua_pushboolean (L, false);
  2979. }
  2980. }
  2981. else {
  2982. return luaL_error (L, "invalid arguments");
  2983. }
  2984. return 1;
  2985. }
  2986. static gint
  2987. lua_task_get_principal_recipient (lua_State *L)
  2988. {
  2989. LUA_TRACE_POINT;
  2990. struct rspamd_task *task = lua_check_task (L, 1);
  2991. const gchar *r;
  2992. if (task) {
  2993. r = rspamd_task_get_principal_recipient (task);
  2994. if (r != NULL) {
  2995. lua_pushstring (L, r);
  2996. }
  2997. else {
  2998. lua_pushnil (L);
  2999. }
  3000. }
  3001. else {
  3002. return luaL_error (L, "invalid arguments");
  3003. }
  3004. return 1;
  3005. }
  3006. static gint
  3007. lua_task_get_reply_sender (lua_State *L)
  3008. {
  3009. LUA_TRACE_POINT;
  3010. struct rspamd_task *task = lua_check_task (L, 1);
  3011. struct rspamd_mime_header *rh;
  3012. if (task) {
  3013. GPtrArray *ar;
  3014. ar = rspamd_message_get_header_array (task, "Reply-To", false);
  3015. if (ar && ar->len == 1) {
  3016. rh = (struct rspamd_mime_header *)g_ptr_array_index (ar, 0);
  3017. lua_pushstring (L, rh->decoded);
  3018. }
  3019. else if (task->from_mime && task->from_mime->len == 1) {
  3020. struct rspamd_email_address *addr;
  3021. addr = (struct rspamd_email_address *)g_ptr_array_index (task->from_mime, 0);
  3022. lua_pushlstring (L, addr->addr, addr->addr_len);
  3023. }
  3024. else if (task->from_envelope) {
  3025. lua_pushlstring (L, task->from_envelope->addr,
  3026. task->from_envelope->addr_len);
  3027. }
  3028. else {
  3029. lua_pushnil (L);
  3030. }
  3031. }
  3032. else {
  3033. return luaL_error (L, "invalid arguments");
  3034. }
  3035. return 1;
  3036. }
  3037. static gint
  3038. lua_task_get_user (lua_State *L)
  3039. {
  3040. LUA_TRACE_POINT;
  3041. struct rspamd_task *task = lua_check_task (L, 1);
  3042. if (task) {
  3043. if (task->user != NULL) {
  3044. lua_pushstring (L, task->user);
  3045. }
  3046. else {
  3047. lua_pushnil (L);
  3048. }
  3049. }
  3050. else {
  3051. return luaL_error (L, "invalid arguments");
  3052. }
  3053. return 1;
  3054. }
  3055. static gint
  3056. lua_task_set_user (lua_State *L)
  3057. {
  3058. LUA_TRACE_POINT;
  3059. struct rspamd_task *task = lua_check_task (L, 1);
  3060. const gchar *new_user;
  3061. if (task) {
  3062. if (lua_type (L, 2) == LUA_TSTRING) {
  3063. new_user = lua_tostring (L, 2);
  3064. if (task->user) {
  3065. /* Push old user */
  3066. lua_pushstring (L, task->user);
  3067. }
  3068. else {
  3069. lua_pushnil (L);
  3070. }
  3071. task->user = rspamd_mempool_strdup (task->task_pool, new_user);
  3072. }
  3073. else {
  3074. /* Reset user */
  3075. if (task->user) {
  3076. /* Push old user */
  3077. lua_pushstring (L, task->user);
  3078. }
  3079. else {
  3080. lua_pushnil (L);
  3081. }
  3082. task->user = NULL;
  3083. }
  3084. }
  3085. else {
  3086. return luaL_error (L, "invalid arguments");
  3087. }
  3088. return 1;
  3089. }
  3090. static gint
  3091. lua_task_get_from_ip (lua_State *L)
  3092. {
  3093. LUA_TRACE_POINT;
  3094. struct rspamd_task *task = lua_check_task (L, 1);
  3095. if (task) {
  3096. rspamd_lua_ip_push (L, task->from_addr);
  3097. }
  3098. else {
  3099. return luaL_error (L, "invalid arguments");
  3100. }
  3101. return 1;
  3102. }
  3103. static gint
  3104. lua_task_set_from_ip (lua_State *L)
  3105. {
  3106. LUA_TRACE_POINT;
  3107. struct rspamd_task *task = lua_check_task (L, 1);
  3108. const gchar *ip_str = luaL_checkstring (L, 2);
  3109. rspamd_inet_addr_t *addr = NULL;
  3110. if (!task || !ip_str) {
  3111. lua_pushstring (L, "invalid parameters");
  3112. return lua_error (L);
  3113. }
  3114. else {
  3115. if (!rspamd_parse_inet_address (&addr,
  3116. ip_str,
  3117. 0)) {
  3118. msg_warn_task ("cannot get IP from received header: '%s'",
  3119. ip_str);
  3120. }
  3121. else {
  3122. if (task->from_addr) {
  3123. rspamd_inet_address_free (task->from_addr);
  3124. }
  3125. task->from_addr = addr;
  3126. }
  3127. }
  3128. return 0;
  3129. }
  3130. static gint
  3131. lua_task_get_from_ip_num (lua_State *L)
  3132. {
  3133. LUA_TRACE_POINT;
  3134. msg_err ("this function is deprecated and should no longer be used");
  3135. lua_pushnil (L);
  3136. return 1;
  3137. }
  3138. static gint
  3139. lua_task_get_client_ip (lua_State *L)
  3140. {
  3141. LUA_TRACE_POINT;
  3142. struct rspamd_task *task = lua_check_task (L, 1);
  3143. if (task) {
  3144. rspamd_lua_ip_push (L, task->client_addr);
  3145. }
  3146. else {
  3147. return luaL_error (L, "invalid arguments");
  3148. }
  3149. return 1;
  3150. }
  3151. static gint
  3152. lua_task_get_helo (lua_State *L)
  3153. {
  3154. LUA_TRACE_POINT;
  3155. struct rspamd_task *task = lua_check_task (L, 1);
  3156. if (task) {
  3157. if (task->helo != NULL) {
  3158. lua_pushstring (L, task->helo);
  3159. return 1;
  3160. }
  3161. else {
  3162. lua_pushnil (L);
  3163. }
  3164. }
  3165. else {
  3166. return luaL_error (L, "invalid arguments");
  3167. }
  3168. return 1;
  3169. }
  3170. static gint
  3171. lua_task_get_subject (lua_State *L)
  3172. {
  3173. LUA_TRACE_POINT;
  3174. struct rspamd_task *task = lua_check_task (L, 1);
  3175. if (task) {
  3176. if (task->subject != NULL) {
  3177. lua_pushstring (L, task->subject);
  3178. return 1;
  3179. }
  3180. else {
  3181. lua_pushnil (L);
  3182. }
  3183. }
  3184. else {
  3185. return luaL_error (L, "invalid arguments");
  3186. }
  3187. return 1;
  3188. }
  3189. static gint
  3190. lua_task_set_helo (lua_State *L)
  3191. {
  3192. LUA_TRACE_POINT;
  3193. struct rspamd_task *task = lua_check_task (L, 1);
  3194. const gchar *new_helo;
  3195. if (task) {
  3196. new_helo = luaL_checkstring (L, 2);
  3197. if (new_helo) {
  3198. task->helo = rspamd_mempool_strdup (task->task_pool, new_helo);
  3199. }
  3200. }
  3201. else {
  3202. return luaL_error (L, "invalid arguments");
  3203. }
  3204. return 0;
  3205. }
  3206. static gint
  3207. lua_task_get_hostname (lua_State *L)
  3208. {
  3209. LUA_TRACE_POINT;
  3210. struct rspamd_task *task = lua_check_task (L, 1);
  3211. if (task) {
  3212. if (task->hostname != NULL) {
  3213. /* Check whether it looks like an IP address */
  3214. if (*task->hostname == '[') {
  3215. /*
  3216. * From the milter documentation:
  3217. * If the reverse lookup fails or if none of the IP
  3218. * addresses of the resolved host name matches the
  3219. * original IP address, hostname will contain the
  3220. * message sender's IP address enclosed in square
  3221. * brackets (e.g. `[a.b.c.d]')
  3222. */
  3223. lua_pushnil (L);
  3224. }
  3225. else {
  3226. lua_pushstring (L, task->hostname);
  3227. }
  3228. }
  3229. else {
  3230. lua_pushnil (L);
  3231. }
  3232. }
  3233. else {
  3234. return luaL_error (L, "invalid arguments");
  3235. }
  3236. return 1;
  3237. }
  3238. static gint
  3239. lua_task_set_hostname (lua_State *L)
  3240. {
  3241. LUA_TRACE_POINT;
  3242. struct rspamd_task *task = lua_check_task (L, 1);
  3243. const gchar *new_hostname;
  3244. if (task) {
  3245. new_hostname = luaL_checkstring (L, 2);
  3246. if (new_hostname) {
  3247. task->hostname = rspamd_mempool_strdup (task->task_pool,
  3248. new_hostname);
  3249. }
  3250. }
  3251. else {
  3252. return luaL_error (L, "invalid arguments");
  3253. }
  3254. return 0;
  3255. }
  3256. static gint
  3257. lua_task_get_images (lua_State *L)
  3258. {
  3259. LUA_TRACE_POINT;
  3260. struct rspamd_task *task = lua_check_task (L, 1);
  3261. guint nelt = 0, i;
  3262. struct rspamd_mime_part *part;
  3263. struct rspamd_image **pimg;
  3264. if (task) {
  3265. if (!lua_task_get_cached (L, task, "images", task->parts->len)) {
  3266. lua_createtable (L, task->parts->len, 0);
  3267. for (i = 0; i < task->parts->len; i ++) {
  3268. part = g_ptr_array_index (task->parts, i);
  3269. if (part->flags & RSPAMD_MIME_PART_IMAGE) {
  3270. pimg = lua_newuserdata (L, sizeof (struct rspamd_image *));
  3271. rspamd_lua_setclass (L, "rspamd{image}", -1);
  3272. *pimg = part->specific.img;
  3273. lua_rawseti (L, -2, ++nelt);
  3274. }
  3275. }
  3276. lua_task_set_cached (L, task, "images", -1, task->parts->len);
  3277. }
  3278. }
  3279. else {
  3280. return luaL_error (L, "invalid arguments");
  3281. }
  3282. return 1;
  3283. }
  3284. static gint
  3285. lua_task_get_archives (lua_State *L)
  3286. {
  3287. LUA_TRACE_POINT;
  3288. struct rspamd_task *task = lua_check_task (L, 1);
  3289. guint nelt = 0, i;
  3290. struct rspamd_mime_part *part;
  3291. struct rspamd_archive **parch;
  3292. if (task) {
  3293. if (!lua_task_get_cached (L, task, "archives", task->parts->len)) {
  3294. lua_createtable (L, task->parts->len, 0);
  3295. for (i = 0; i < task->parts->len; i ++) {
  3296. part = g_ptr_array_index (task->parts, i);
  3297. if (part->flags & RSPAMD_MIME_PART_ARCHIVE) {
  3298. parch = lua_newuserdata (L, sizeof (struct rspamd_archive *));
  3299. rspamd_lua_setclass (L, "rspamd{archive}", -1);
  3300. *parch = part->specific.arch;
  3301. lua_rawseti (L, -2, ++nelt);
  3302. }
  3303. }
  3304. lua_task_set_cached (L, task, "archives", -1, task->parts->len);
  3305. }
  3306. }
  3307. else {
  3308. return luaL_error (L, "invalid arguments");
  3309. }
  3310. return 1;
  3311. }
  3312. static gint
  3313. lua_task_get_dkim_results (lua_State *L)
  3314. {
  3315. LUA_TRACE_POINT;
  3316. struct rspamd_task *task = lua_check_task (L, 1);
  3317. guint nelt = 0, i;
  3318. struct rspamd_dkim_check_result **pres, **cur;
  3319. if (task) {
  3320. if (!lua_task_get_cached (L, task, "dkim_results", 0)) {
  3321. pres = rspamd_mempool_get_variable (task->task_pool,
  3322. RSPAMD_MEMPOOL_DKIM_CHECK_RESULTS);
  3323. if (pres == NULL) {
  3324. lua_newtable (L);
  3325. }
  3326. else {
  3327. for (cur = pres; *cur != NULL; cur ++) {
  3328. nelt ++;
  3329. }
  3330. lua_createtable (L, nelt, 0);
  3331. for (i = 0; i < nelt; i ++) {
  3332. struct rspamd_dkim_check_result *res = pres[i];
  3333. const gchar *result_str = "unknown";
  3334. lua_createtable (L, 0, 4);
  3335. switch (res->rcode) {
  3336. case DKIM_CONTINUE:
  3337. result_str = "allow";
  3338. break;
  3339. case DKIM_REJECT:
  3340. result_str = "reject";
  3341. break;
  3342. case DKIM_TRYAGAIN:
  3343. result_str = "tempfail";
  3344. break;
  3345. case DKIM_NOTFOUND:
  3346. result_str = "not found";
  3347. break;
  3348. case DKIM_RECORD_ERROR:
  3349. result_str = "bad record";
  3350. break;
  3351. case DKIM_PERM_ERROR:
  3352. result_str = "permanent error";
  3353. break;
  3354. default:
  3355. break;
  3356. }
  3357. rspamd_lua_table_set (L, "result", result_str);
  3358. if (res->domain) {
  3359. rspamd_lua_table_set (L, "domain", res->domain);
  3360. }
  3361. if (res->selector) {
  3362. rspamd_lua_table_set (L, "selector", res->selector);
  3363. }
  3364. if (res->short_b) {
  3365. rspamd_lua_table_set (L, "bhash", res->short_b);
  3366. }
  3367. if (res->fail_reason) {
  3368. rspamd_lua_table_set (L, "fail_reason", res->fail_reason);
  3369. }
  3370. lua_rawseti (L, -2, i + 1);
  3371. }
  3372. }
  3373. lua_task_set_cached (L, task, "dkim_results", -1, 0);
  3374. }
  3375. }
  3376. else {
  3377. return luaL_error (L, "invalid arguments");
  3378. }
  3379. return 1;
  3380. }
  3381. static inline gboolean
  3382. lua_push_symbol_result (lua_State *L,
  3383. struct rspamd_task *task,
  3384. const gchar *symbol,
  3385. struct rspamd_symbol_result *symbol_result,
  3386. gboolean add_metric,
  3387. gboolean add_name)
  3388. {
  3389. struct rspamd_metric_result *metric_res;
  3390. struct rspamd_symbol_result *s = NULL;
  3391. struct rspamd_symbol_option *opt;
  3392. struct rspamd_symbols_group *sym_group;
  3393. guint i;
  3394. gint j = 1, e = 4;
  3395. if (!symbol_result) {
  3396. metric_res = task->result;
  3397. if (metric_res) {
  3398. s = rspamd_task_find_symbol_result (task, symbol);
  3399. }
  3400. }
  3401. else {
  3402. s = symbol_result;
  3403. }
  3404. if (s) {
  3405. if (add_metric) {
  3406. e++;
  3407. }
  3408. if (add_name) {
  3409. e++;
  3410. }
  3411. lua_createtable (L, 0, e);
  3412. if (add_name) {
  3413. lua_pushstring (L, "name");
  3414. lua_pushstring (L, symbol);
  3415. lua_settable (L, -3);
  3416. }
  3417. lua_pushstring (L, "score");
  3418. lua_pushnumber (L, s->score);
  3419. lua_settable (L, -3);
  3420. if (s->sym && s->sym->gr) {
  3421. lua_pushstring (L, "group");
  3422. lua_pushstring (L, s->sym->gr->name);
  3423. lua_settable (L, -3);
  3424. lua_pushstring (L, "groups");
  3425. lua_createtable (L, s->sym->groups->len, 0);
  3426. PTR_ARRAY_FOREACH (s->sym->groups, i, sym_group) {
  3427. lua_pushstring (L, sym_group->name);
  3428. lua_rawseti (L, -2, i + 1);
  3429. }
  3430. lua_settable (L, -3);
  3431. }
  3432. else {
  3433. lua_pushstring (L, "group");
  3434. lua_pushstring (L, "ungrouped");
  3435. lua_settable (L, -3);
  3436. }
  3437. if (s->options) {
  3438. lua_pushstring (L, "options");
  3439. lua_createtable (L, kh_size (s->options), 0);
  3440. DL_FOREACH (s->opts_head, opt) {
  3441. lua_pushstring (L, (const char*)opt->option);
  3442. lua_rawseti (L, -2, j++);
  3443. }
  3444. lua_settable (L, -3);
  3445. }
  3446. return TRUE;
  3447. }
  3448. return FALSE;
  3449. }
  3450. static gint
  3451. lua_task_get_symbol (lua_State *L)
  3452. {
  3453. LUA_TRACE_POINT;
  3454. struct rspamd_task *task = lua_check_task (L, 1);
  3455. const gchar *symbol;
  3456. gboolean found = FALSE;
  3457. gint i = 1;
  3458. symbol = luaL_checkstring (L, 2);
  3459. if (task && symbol) {
  3460. lua_createtable (L, 1, 0);
  3461. if ((found = lua_push_symbol_result (L, task, symbol,
  3462. NULL, TRUE, FALSE))) {
  3463. lua_rawseti (L, -2, i++);
  3464. }
  3465. else {
  3466. /* Pop table */
  3467. lua_pop (L, 1);
  3468. }
  3469. }
  3470. else {
  3471. return luaL_error (L, "invalid arguments");
  3472. }
  3473. if (!found) {
  3474. lua_pushnil (L);
  3475. }
  3476. return 1;
  3477. }
  3478. static gint
  3479. lua_task_has_symbol (lua_State *L)
  3480. {
  3481. LUA_TRACE_POINT;
  3482. struct rspamd_task *task = lua_check_task (L, 1);
  3483. const gchar *symbol;
  3484. gboolean found = FALSE;
  3485. symbol = luaL_checkstring (L, 2);
  3486. if (task && symbol) {
  3487. found = (rspamd_task_find_symbol_result (task, symbol) != NULL);
  3488. lua_pushboolean (L, found);
  3489. }
  3490. else {
  3491. return luaL_error (L, "invalid arguments");
  3492. }
  3493. return 1;
  3494. }
  3495. static gint
  3496. lua_task_enable_symbol (lua_State *L)
  3497. {
  3498. LUA_TRACE_POINT;
  3499. struct rspamd_task *task = lua_check_task (L, 1);
  3500. const gchar *symbol;
  3501. gboolean found = FALSE;
  3502. symbol = luaL_checkstring (L, 2);
  3503. if (task && symbol) {
  3504. found = rspamd_symcache_enable_symbol (task, task->cfg->cache, symbol);
  3505. lua_pushboolean (L, found);
  3506. }
  3507. else {
  3508. return luaL_error (L, "invalid arguments");
  3509. }
  3510. return 1;
  3511. }
  3512. static gint
  3513. lua_task_disable_symbol (lua_State *L)
  3514. {
  3515. LUA_TRACE_POINT;
  3516. struct rspamd_task *task = lua_check_task (L, 1);
  3517. const gchar *symbol;
  3518. gboolean found = FALSE;
  3519. symbol = luaL_checkstring (L, 2);
  3520. if (task && symbol) {
  3521. found = rspamd_symcache_disable_symbol (task, task->cfg->cache, symbol);
  3522. lua_pushboolean (L, found);
  3523. }
  3524. else {
  3525. return luaL_error (L, "invalid arguments");
  3526. }
  3527. return 1;
  3528. }
  3529. static gint
  3530. lua_task_get_symbols (lua_State *L)
  3531. {
  3532. LUA_TRACE_POINT;
  3533. struct rspamd_task *task = lua_check_task (L, 1);
  3534. struct rspamd_metric_result *mres;
  3535. gint i = 1;
  3536. struct rspamd_symbol_result *s;
  3537. if (task) {
  3538. mres = task->result;
  3539. if (mres) {
  3540. lua_createtable (L, kh_size (mres->symbols), 0);
  3541. lua_createtable (L, kh_size (mres->symbols), 0);
  3542. kh_foreach_value_ptr (mres->symbols, s, {
  3543. if (!(s->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) {
  3544. lua_pushstring (L, s->name);
  3545. lua_rawseti (L, -3, i);
  3546. lua_pushnumber (L, s->score);
  3547. lua_rawseti (L, -2, i);
  3548. i++;
  3549. }
  3550. });
  3551. }
  3552. else {
  3553. lua_createtable (L, 0, 0);
  3554. lua_createtable (L, 0, 0);
  3555. }
  3556. }
  3557. else {
  3558. return luaL_error (L, "invalid arguments");
  3559. }
  3560. return 2;
  3561. }
  3562. static gint
  3563. lua_task_get_symbols_all (lua_State *L)
  3564. {
  3565. LUA_TRACE_POINT;
  3566. struct rspamd_task *task = lua_check_task (L, 1);
  3567. struct rspamd_metric_result *mres;
  3568. struct rspamd_symbol_result *s;
  3569. gboolean found = FALSE;
  3570. gint i = 1;
  3571. if (task) {
  3572. mres = task->result;
  3573. if (mres) {
  3574. found = TRUE;
  3575. lua_createtable (L, kh_size (mres->symbols), 0);
  3576. kh_foreach_value_ptr (mres->symbols, s, {
  3577. lua_push_symbol_result (L, task, s->name, s, FALSE, TRUE);
  3578. lua_rawseti (L, -2, i++);
  3579. });
  3580. }
  3581. }
  3582. else {
  3583. return luaL_error (L, "invalid arguments");
  3584. }
  3585. if (!found) {
  3586. lua_pushnil (L);
  3587. }
  3588. return 1;
  3589. }
  3590. static gint
  3591. lua_task_get_symbols_numeric (lua_State *L)
  3592. {
  3593. LUA_TRACE_POINT;
  3594. struct rspamd_task *task = lua_check_task (L, 1);
  3595. struct rspamd_metric_result *mres;
  3596. gint i = 1, id;
  3597. struct rspamd_symbol_result *s;
  3598. if (task) {
  3599. mres = task->result;
  3600. if (mres) {
  3601. lua_createtable (L, kh_size (mres->symbols), 0);
  3602. lua_createtable (L, kh_size (mres->symbols), 0);
  3603. lua_createtable (L, kh_size (mres->symbols), 0);
  3604. kh_foreach_value_ptr (mres->symbols, s, {
  3605. if (!(s->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) {
  3606. id = rspamd_symcache_find_symbol (task->cfg->cache,
  3607. s->name);
  3608. lua_pushinteger (L, id);
  3609. lua_rawseti (L, -3, i);
  3610. lua_pushnumber (L, s->score);
  3611. lua_rawseti (L, -2, i);
  3612. i++;
  3613. }
  3614. });
  3615. }
  3616. else {
  3617. lua_createtable (L, 0, 0);
  3618. lua_createtable (L, 0, 0);
  3619. }
  3620. }
  3621. else {
  3622. return luaL_error (L, "invalid arguments");
  3623. }
  3624. return 2;
  3625. }
  3626. struct tokens_foreach_cbdata {
  3627. struct rspamd_task *task;
  3628. lua_State *L;
  3629. gint idx;
  3630. gboolean normalize;
  3631. };
  3632. static void
  3633. tokens_foreach_cb (gint id, const gchar *sym, gint flags, gpointer ud)
  3634. {
  3635. struct tokens_foreach_cbdata *cbd = ud;
  3636. struct rspamd_symbol_result *s;
  3637. if (flags & SYMBOL_TYPE_NOSTAT) {
  3638. return;
  3639. }
  3640. if ((s = rspamd_task_find_symbol_result (cbd->task, sym)) != NULL) {
  3641. if (cbd->normalize) {
  3642. lua_pushnumber (cbd->L, tanh (s->score));
  3643. }
  3644. else {
  3645. lua_pushnumber (cbd->L, s->score);
  3646. }
  3647. }
  3648. else {
  3649. lua_pushnumber (cbd->L, 0.0);
  3650. }
  3651. lua_rawseti (cbd->L, -2, cbd->idx++);
  3652. }
  3653. static gint
  3654. lua_task_get_symbols_tokens (lua_State *L)
  3655. {
  3656. LUA_TRACE_POINT;
  3657. struct rspamd_task *task = lua_check_task (L, 1);
  3658. struct tokens_foreach_cbdata cbd;
  3659. cbd.task = task;
  3660. cbd.L = L;
  3661. cbd.idx = 1;
  3662. cbd.normalize = TRUE;
  3663. if (lua_type (L, 2) == LUA_TBOOLEAN) {
  3664. cbd.normalize = lua_toboolean (L, 2);
  3665. }
  3666. else {
  3667. cbd.normalize = TRUE;
  3668. }
  3669. lua_createtable (L,
  3670. rspamd_symcache_stats_symbols_count (task->cfg->cache), 0);
  3671. rspamd_symcache_foreach (task->cfg->cache, tokens_foreach_cb, &cbd);
  3672. return 1;
  3673. }
  3674. enum lua_date_type {
  3675. DATE_CONNECT = 0,
  3676. DATE_MESSAGE,
  3677. DATE_CONNECT_STRING,
  3678. DATE_MESSAGE_STRING
  3679. };
  3680. static enum lua_date_type
  3681. lua_task_detect_date_type (struct rspamd_task *task,
  3682. lua_State *L, gint idx, gboolean *gmt)
  3683. {
  3684. enum lua_date_type type = DATE_CONNECT;
  3685. if (lua_type (L, idx) == LUA_TNUMBER) {
  3686. gint num = lua_tonumber (L, idx);
  3687. if (num >= DATE_CONNECT && num <= DATE_MESSAGE_STRING) {
  3688. return num;
  3689. }
  3690. }
  3691. else if (lua_type (L, idx) == LUA_TTABLE) {
  3692. const gchar *str;
  3693. lua_pushvalue (L, idx);
  3694. lua_pushstring (L, "format");
  3695. lua_gettable (L, -2);
  3696. str = lua_tostring (L, -1);
  3697. if (str) {
  3698. if (g_ascii_strcasecmp (str, "message") == 0) {
  3699. type = DATE_MESSAGE;
  3700. } else if (g_ascii_strcasecmp (str, "connect_str") == 0) {
  3701. type = DATE_CONNECT_STRING;
  3702. } else if (g_ascii_strcasecmp (str, "message_str") == 0) {
  3703. type = DATE_MESSAGE_STRING;
  3704. }
  3705. }
  3706. else {
  3707. msg_warn_task ("date format has not been specified");
  3708. }
  3709. lua_pop (L, 1);
  3710. lua_pushstring (L, "gmt");
  3711. lua_gettable (L, -2);
  3712. if (lua_type (L, -1) == LUA_TBOOLEAN) {
  3713. *gmt = lua_toboolean (L, -1);
  3714. }
  3715. /* Value and table */
  3716. lua_pop (L, 2);
  3717. }
  3718. return type;
  3719. }
  3720. static gint
  3721. lua_task_get_date (lua_State *L)
  3722. {
  3723. LUA_TRACE_POINT;
  3724. struct rspamd_task *task = lua_check_task (L, 1);
  3725. GPtrArray *hdrs;
  3726. gdouble tim;
  3727. enum lua_date_type type = DATE_CONNECT;
  3728. gboolean gmt = TRUE;
  3729. if (task != NULL) {
  3730. if (lua_gettop (L) > 1) {
  3731. type = lua_task_detect_date_type (task, L, 2, &gmt);
  3732. }
  3733. /* Get GMT date and store it to time_t */
  3734. if (type == DATE_CONNECT || type == DATE_CONNECT_STRING) {
  3735. tim = (tv_to_msec (&task->tv)) / 1000.;
  3736. if (!gmt) {
  3737. struct tm t;
  3738. time_t tt;
  3739. tt = tim;
  3740. rspamd_localtime (tt, &t);
  3741. #if !defined(__sun)
  3742. t.tm_gmtoff = 0;
  3743. #endif
  3744. t.tm_isdst = 0;
  3745. tim = mktime (&t);
  3746. }
  3747. }
  3748. else {
  3749. hdrs = rspamd_message_get_header_array (task, "Date",
  3750. FALSE);
  3751. if (hdrs && hdrs->len > 0) {
  3752. time_t tt;
  3753. struct tm t;
  3754. struct rspamd_mime_header *h;
  3755. h = g_ptr_array_index (hdrs, 0);
  3756. tt = rspamd_parse_smtp_date (h->decoded, strlen (h->decoded));
  3757. if (!gmt) {
  3758. rspamd_localtime (tt, &t);
  3759. #if !defined(__sun)
  3760. t.tm_gmtoff = 0;
  3761. #endif
  3762. t.tm_isdst = 0;
  3763. tim = mktime (&t);
  3764. }
  3765. else {
  3766. tim = tt;
  3767. }
  3768. }
  3769. else {
  3770. tim = 0.0;
  3771. }
  3772. }
  3773. if (type == DATE_CONNECT || type == DATE_MESSAGE) {
  3774. lua_pushnumber (L, tim);
  3775. }
  3776. else {
  3777. GTimeVal tv;
  3778. gchar *out;
  3779. double_to_tv (tim, &tv);
  3780. out = g_time_val_to_iso8601 (&tv);
  3781. lua_pushstring (L, out);
  3782. g_free (out);
  3783. }
  3784. }
  3785. else {
  3786. return luaL_error (L, "invalid arguments");
  3787. }
  3788. return 1;
  3789. }
  3790. static gint
  3791. lua_task_get_message_id (lua_State *L)
  3792. {
  3793. LUA_TRACE_POINT;
  3794. struct rspamd_task *task = lua_check_task (L, 1);
  3795. if (task != NULL) {
  3796. if (task->message_id != NULL) {
  3797. lua_pushstring (L, task->message_id);
  3798. }
  3799. else {
  3800. lua_pushnil (L);
  3801. }
  3802. }
  3803. else {
  3804. return luaL_error (L, "invalid arguments");
  3805. }
  3806. return 1;
  3807. }
  3808. static gint
  3809. lua_task_get_timeval (lua_State *L)
  3810. {
  3811. LUA_TRACE_POINT;
  3812. struct rspamd_task *task = lua_check_task (L, 1);
  3813. if (task != NULL) {
  3814. lua_createtable (L, 0, 2);
  3815. lua_pushstring (L, "tv_sec");
  3816. lua_pushinteger (L, (lua_Integer)task->tv.tv_sec);
  3817. lua_settable (L, -3);
  3818. lua_pushstring (L, "tv_usec");
  3819. lua_pushinteger (L, (lua_Integer)task->tv.tv_usec);
  3820. lua_settable (L, -3);
  3821. }
  3822. else {
  3823. return luaL_error (L, "invalid arguments");
  3824. }
  3825. return 1;
  3826. }
  3827. static gint
  3828. lua_task_get_scan_time (lua_State *L)
  3829. {
  3830. LUA_TRACE_POINT;
  3831. struct rspamd_task *task = lua_check_task (L, 1);
  3832. gboolean set = TRUE;
  3833. if (task != NULL) {
  3834. if (lua_isboolean (L, 2)) {
  3835. set = lua_toboolean (L, 2);
  3836. }
  3837. rspamd_task_set_finish_time (task);
  3838. lua_pushnumber (L, task->time_real_finish - task->time_real);
  3839. lua_pushnumber (L, task->time_virtual_finish - task->time_virtual);
  3840. if (!set) {
  3841. /* Reset to nan to allow further calcs in rspamd_task_set_finish_time */
  3842. task->time_real_finish = NAN;
  3843. }
  3844. }
  3845. else {
  3846. return luaL_error (L, "invalid arguments");
  3847. }
  3848. return 2;
  3849. }
  3850. static gint
  3851. lua_task_get_size (lua_State *L)
  3852. {
  3853. LUA_TRACE_POINT;
  3854. struct rspamd_task *task = lua_check_task (L, 1);
  3855. if (task != NULL) {
  3856. lua_pushinteger (L, task->msg.len);
  3857. }
  3858. else {
  3859. return luaL_error (L, "invalid arguments");
  3860. }
  3861. return 1;
  3862. }
  3863. /**
  3864. * - `no_log`: do not log task summary
  3865. * - `no_stat`: do not include task into scanned stats
  3866. * - `pass_all`: check all filters for task
  3867. * - `extended_urls`: output extended info about urls
  3868. * - `skip`: skip task processing
  3869. */
  3870. #define LUA_TASK_FLAG_WRITE(flag, set) do { \
  3871. task->flags = (set) ? (task->flags | (flag)) : (task->flags & ~(flag)); \
  3872. } while(0)
  3873. #define LUA_TASK_SET_FLAG(flag, strname, macro, set) do { \
  3874. if (!found && strcmp ((flag), strname) == 0) { \
  3875. LUA_TASK_FLAG_WRITE((macro), set); \
  3876. found = TRUE; \
  3877. } \
  3878. } while(0)
  3879. #define LUA_TASK_FLAG_READ(flag) do { \
  3880. lua_pushboolean(L, !!(task->flags & (flag))); \
  3881. } while(0)
  3882. #define LUA_TASK_GET_FLAG(flag, strname, macro) do { \
  3883. if (!found && strcmp ((flag), strname) == 0) { \
  3884. LUA_TASK_FLAG_READ((macro)); \
  3885. found = TRUE; \
  3886. } \
  3887. } while(0)
  3888. static gint
  3889. lua_task_set_flag (lua_State *L)
  3890. {
  3891. LUA_TRACE_POINT;
  3892. struct rspamd_task *task = lua_check_task (L, 1);
  3893. const gchar *flag = luaL_checkstring (L, 2);
  3894. gboolean set = TRUE, found = FALSE;
  3895. if (lua_gettop (L) >= 3) {
  3896. set = lua_toboolean (L, 3);
  3897. }
  3898. if (task != NULL && flag != NULL) {
  3899. LUA_TASK_SET_FLAG (flag, "pass_all", RSPAMD_TASK_FLAG_PASS_ALL, set);
  3900. LUA_TASK_SET_FLAG (flag, "no_log", RSPAMD_TASK_FLAG_NO_LOG, set);
  3901. LUA_TASK_SET_FLAG (flag, "no_stat", RSPAMD_TASK_FLAG_NO_STAT, set);
  3902. LUA_TASK_SET_FLAG (flag, "skip", RSPAMD_TASK_FLAG_SKIP, set);
  3903. LUA_TASK_SET_FLAG (flag, "extended_urls", RSPAMD_TASK_FLAG_EXT_URLS, set);
  3904. LUA_TASK_SET_FLAG (flag, "learn_spam", RSPAMD_TASK_FLAG_LEARN_SPAM, set);
  3905. LUA_TASK_SET_FLAG (flag, "learn_ham", RSPAMD_TASK_FLAG_LEARN_HAM, set);
  3906. LUA_TASK_SET_FLAG (flag, "broken_headers",
  3907. RSPAMD_TASK_FLAG_BROKEN_HEADERS, set);
  3908. LUA_TASK_SET_FLAG (flag, "greylisted", RSPAMD_TASK_FLAG_GREYLISTED, set);
  3909. LUA_TASK_SET_FLAG (flag, "skip_process", RSPAMD_TASK_FLAG_SKIP_PROCESS, set);
  3910. if (!found) {
  3911. msg_warn_task ("unknown flag requested: %s", flag);
  3912. }
  3913. }
  3914. else {
  3915. return luaL_error (L, "invalid arguments");
  3916. }
  3917. return 0;
  3918. }
  3919. static gint
  3920. lua_task_has_flag (lua_State *L)
  3921. {
  3922. LUA_TRACE_POINT;
  3923. struct rspamd_task *task = lua_check_task (L, 1);
  3924. const gchar *flag = luaL_checkstring (L, 2);
  3925. gboolean found = FALSE;
  3926. if (task != NULL && flag != NULL) {
  3927. LUA_TASK_GET_FLAG (flag, "pass_all", RSPAMD_TASK_FLAG_PASS_ALL);
  3928. LUA_TASK_GET_FLAG (flag, "no_log", RSPAMD_TASK_FLAG_NO_LOG);
  3929. LUA_TASK_GET_FLAG (flag, "no_stat", RSPAMD_TASK_FLAG_NO_STAT);
  3930. LUA_TASK_GET_FLAG (flag, "skip", RSPAMD_TASK_FLAG_SKIP);
  3931. LUA_TASK_GET_FLAG (flag, "extended_urls", RSPAMD_TASK_FLAG_EXT_URLS);
  3932. LUA_TASK_GET_FLAG (flag, "learn_spam", RSPAMD_TASK_FLAG_LEARN_SPAM);
  3933. LUA_TASK_GET_FLAG (flag, "learn_ham", RSPAMD_TASK_FLAG_LEARN_HAM);
  3934. LUA_TASK_GET_FLAG (flag, "greylisted", RSPAMD_TASK_FLAG_GREYLISTED);
  3935. LUA_TASK_GET_FLAG (flag, "broken_headers",
  3936. RSPAMD_TASK_FLAG_BROKEN_HEADERS);
  3937. LUA_TASK_GET_FLAG (flag, "skip_process",
  3938. RSPAMD_TASK_FLAG_SKIP_PROCESS);
  3939. LUA_TASK_GET_FLAG (flag, "milter",
  3940. RSPAMD_TASK_FLAG_MILTER);
  3941. LUA_TASK_GET_FLAG (flag, "bad_unicode",
  3942. RSPAMD_TASK_FLAG_BAD_UNICODE);
  3943. LUA_TASK_GET_FLAG (flag, "mime",
  3944. RSPAMD_TASK_FLAG_MIME);
  3945. if (!found) {
  3946. msg_warn_task ("unknown flag requested: %s", flag);
  3947. lua_pushboolean (L, 0);
  3948. }
  3949. }
  3950. else {
  3951. return luaL_error (L, "invalid arguments");
  3952. }
  3953. return 1;
  3954. }
  3955. static gint
  3956. lua_task_get_flags (lua_State *L)
  3957. {
  3958. LUA_TRACE_POINT;
  3959. struct rspamd_task *task = lua_check_task (L, 1);
  3960. gint idx = 1;
  3961. guint flags, bit, i;
  3962. if (task) {
  3963. lua_createtable (L, 8, 0);
  3964. flags = task->flags;
  3965. for (i = 0; i < sizeof (task->flags) * NBBY; i ++) {
  3966. bit = (1U << i);
  3967. if (flags & bit) {
  3968. switch (bit) {
  3969. case RSPAMD_TASK_FLAG_PASS_ALL:
  3970. lua_pushstring (L, "pass_all");
  3971. lua_rawseti (L, -2, idx ++);
  3972. break;
  3973. case RSPAMD_TASK_FLAG_NO_LOG:
  3974. lua_pushstring (L, "no_log");
  3975. lua_rawseti (L, -2, idx++);
  3976. break;
  3977. case RSPAMD_TASK_FLAG_NO_STAT:
  3978. lua_pushstring (L, "no_stat");
  3979. lua_rawseti (L, -2, idx++);
  3980. break;
  3981. case RSPAMD_TASK_FLAG_SKIP:
  3982. lua_pushstring (L, "skip");
  3983. lua_rawseti (L, -2, idx++);
  3984. break;
  3985. case RSPAMD_TASK_FLAG_EXT_URLS:
  3986. lua_pushstring (L, "extended_urls");
  3987. lua_rawseti (L, -2, idx++);
  3988. break;
  3989. case RSPAMD_TASK_FLAG_BROKEN_HEADERS:
  3990. lua_pushstring (L, "broken_headers");
  3991. lua_rawseti (L, -2, idx++);
  3992. break;
  3993. case RSPAMD_TASK_FLAG_LEARN_SPAM:
  3994. lua_pushstring (L, "learn_spam");
  3995. lua_rawseti (L, -2, idx++);
  3996. break;
  3997. case RSPAMD_TASK_FLAG_LEARN_HAM:
  3998. lua_pushstring (L, "learn_ham");
  3999. lua_rawseti (L, -2, idx++);
  4000. break;
  4001. case RSPAMD_TASK_FLAG_GREYLISTED:
  4002. lua_pushstring (L, "greylisted");
  4003. lua_rawseti (L, -2, idx++);
  4004. break;
  4005. case RSPAMD_TASK_FLAG_SKIP_PROCESS:
  4006. lua_pushstring (L, "skip_process");
  4007. lua_rawseti (L, -2, idx++);
  4008. break;
  4009. case RSPAMD_TASK_FLAG_MILTER:
  4010. lua_pushstring (L, "milter");
  4011. lua_rawseti (L, -2, idx++);
  4012. break;
  4013. default:
  4014. break;
  4015. }
  4016. }
  4017. }
  4018. }
  4019. else {
  4020. return luaL_error (L, "invalid arguments");
  4021. }
  4022. return 1;
  4023. }
  4024. static gint
  4025. lua_task_get_digest (lua_State *L)
  4026. {
  4027. LUA_TRACE_POINT;
  4028. struct rspamd_task *task = lua_check_task (L, 1);
  4029. gchar hexbuf[33];
  4030. gint r;
  4031. if (task) {
  4032. r = rspamd_encode_hex_buf (task->digest, sizeof (task->digest),
  4033. hexbuf, sizeof (hexbuf) - 1);
  4034. if (r > 0) {
  4035. hexbuf[r] = '\0';
  4036. lua_pushstring (L, hexbuf);
  4037. }
  4038. else {
  4039. lua_pushnil (L);
  4040. }
  4041. }
  4042. else {
  4043. return luaL_error (L, "invalid arguments");
  4044. }
  4045. return 1;
  4046. }
  4047. static gint
  4048. lua_task_learn (lua_State *L)
  4049. {
  4050. LUA_TRACE_POINT;
  4051. struct rspamd_task *task = lua_check_task (L, 1);
  4052. gboolean is_spam = FALSE;
  4053. const gchar *clname = NULL;
  4054. GError *err = NULL;
  4055. int ret = 1;
  4056. if (task == NULL) {
  4057. return luaL_error (L, "invalid arguments");
  4058. }
  4059. is_spam = lua_toboolean(L, 2);
  4060. if (lua_gettop (L) > 2) {
  4061. clname = luaL_checkstring (L, 3);
  4062. }
  4063. if (!rspamd_learn_task_spam (task, is_spam, clname, &err)) {
  4064. lua_pushboolean (L, FALSE);
  4065. if (err != NULL) {
  4066. lua_pushstring (L, err->message);
  4067. ret = 2;
  4068. }
  4069. }
  4070. else {
  4071. lua_pushboolean (L, TRUE);
  4072. }
  4073. return ret;
  4074. }
  4075. static gint
  4076. lua_task_set_settings (lua_State *L)
  4077. {
  4078. LUA_TRACE_POINT;
  4079. struct rspamd_task *task = lua_check_task (L, 1);
  4080. ucl_object_t *settings;
  4081. const ucl_object_t *act, *elt, *metric_elt, *vars, *cur;
  4082. ucl_object_iter_t it = NULL;
  4083. struct rspamd_metric_result *mres;
  4084. guint i;
  4085. settings = ucl_object_lua_import (L, 2);
  4086. if (settings != NULL && task != NULL) {
  4087. metric_elt = ucl_object_lookup (settings, DEFAULT_METRIC);
  4088. if (metric_elt) {
  4089. task->settings = ucl_object_ref (metric_elt);
  4090. ucl_object_unref (settings);
  4091. }
  4092. else {
  4093. task->settings = settings;
  4094. }
  4095. act = ucl_object_lookup (task->settings, "actions");
  4096. if (act) {
  4097. /* Adjust desired actions */
  4098. mres = task->result;
  4099. for (i = 0; i < mres->nactions; i++) {
  4100. struct rspamd_action_result *act_res = &mres->actions_limits[i];
  4101. elt = ucl_object_lookup (act, act_res->action->name);
  4102. if (elt) {
  4103. if (ucl_object_type (elt) == UCL_FLOAT ||
  4104. ucl_object_type (elt) == UCL_INT) {
  4105. gdouble nscore = ucl_object_todouble (elt);
  4106. msg_debug_task ("adjusted action %s: %.2f -> %.2f",
  4107. ucl_object_key (elt),
  4108. act_res->cur_limit,
  4109. nscore);
  4110. act_res->cur_limit = nscore;
  4111. }
  4112. else if (ucl_object_type (elt) == UCL_NULL) {
  4113. act_res->cur_limit = NAN;
  4114. msg_info_task ("disabled action %s due to settings",
  4115. ucl_object_key (elt));
  4116. }
  4117. }
  4118. }
  4119. }
  4120. vars = ucl_object_lookup (task->settings, "variables");
  4121. if (vars && ucl_object_type (vars) == UCL_OBJECT) {
  4122. /* Set memory pool variables */
  4123. it = NULL;
  4124. while ((cur = ucl_object_iterate (vars, &it, true)) != NULL) {
  4125. if (ucl_object_type (cur) == UCL_STRING) {
  4126. rspamd_mempool_set_variable (task->task_pool,
  4127. ucl_object_key (cur), rspamd_mempool_strdup (
  4128. task->task_pool,
  4129. ucl_object_tostring (cur)
  4130. ), NULL);
  4131. }
  4132. }
  4133. }
  4134. rspamd_symcache_process_settings (task, task->cfg->cache);
  4135. }
  4136. else {
  4137. return luaL_error (L, "invalid arguments");
  4138. }
  4139. return 0;
  4140. }
  4141. static gint
  4142. lua_task_set_milter_reply (lua_State *L)
  4143. {
  4144. LUA_TRACE_POINT;
  4145. struct rspamd_task *task = lua_check_task (L, 1);
  4146. ucl_object_t *reply, *prev;
  4147. reply = ucl_object_lua_import (L, 2);
  4148. if (reply != NULL && task != NULL) {
  4149. prev = rspamd_mempool_get_variable (task->task_pool,
  4150. RSPAMD_MEMPOOL_MILTER_REPLY);
  4151. if (prev) {
  4152. ucl_object_merge (prev, reply, false);
  4153. ucl_object_unref (reply);
  4154. }
  4155. else {
  4156. rspamd_mempool_set_variable (task->task_pool,
  4157. RSPAMD_MEMPOOL_MILTER_REPLY,
  4158. reply, (rspamd_mempool_destruct_t) ucl_object_unref);
  4159. }
  4160. }
  4161. else {
  4162. return luaL_error (L, "invalid arguments");
  4163. }
  4164. return 0;
  4165. }
  4166. static gint
  4167. lua_task_get_settings (lua_State *L)
  4168. {
  4169. LUA_TRACE_POINT;
  4170. struct rspamd_task *task = lua_check_task (L, 1);
  4171. if (task != NULL) {
  4172. if (task->settings) {
  4173. return ucl_object_push_lua (L, task->settings, true);
  4174. }
  4175. else {
  4176. lua_pushnil (L);
  4177. }
  4178. }
  4179. else {
  4180. return luaL_error (L, "invalid arguments");
  4181. }
  4182. return 1;
  4183. }
  4184. static gint
  4185. lua_task_lookup_settings (lua_State *L)
  4186. {
  4187. LUA_TRACE_POINT;
  4188. struct rspamd_task *task = lua_check_task (L, 1);
  4189. const gchar *key = NULL;
  4190. const ucl_object_t *elt;
  4191. if (task != NULL) {
  4192. if (lua_isstring (L, 2)) {
  4193. key = lua_tostring (L, 2);
  4194. }
  4195. if (task->settings) {
  4196. if (key == NULL) {
  4197. return ucl_object_push_lua (L, task->settings, true);
  4198. }
  4199. else {
  4200. elt = ucl_object_lookup (task->settings, key);
  4201. if (elt) {
  4202. return ucl_object_push_lua (L, elt, true);
  4203. }
  4204. else {
  4205. lua_pushnil (L);
  4206. }
  4207. }
  4208. }
  4209. else {
  4210. lua_pushnil (L);
  4211. }
  4212. }
  4213. else {
  4214. return luaL_error (L, "invalid arguments");
  4215. }
  4216. return 1;
  4217. }
  4218. static gint
  4219. lua_task_get_settings_id (lua_State *L)
  4220. {
  4221. LUA_TRACE_POINT;
  4222. struct rspamd_task *task = lua_check_task (L, 1);
  4223. guint32 *hp;
  4224. if (task != NULL) {
  4225. hp = rspamd_mempool_get_variable (task->task_pool, "settings_hash");
  4226. if (hp) {
  4227. lua_pushnumber (L, *hp);
  4228. }
  4229. else {
  4230. lua_pushnil (L);
  4231. }
  4232. }
  4233. else {
  4234. return luaL_error (L, "invalid arguments");
  4235. }
  4236. return 1;
  4237. }
  4238. static gint
  4239. lua_task_cache_get (lua_State *L)
  4240. {
  4241. LUA_TRACE_POINT;
  4242. struct rspamd_task *task = lua_check_task (L, 1);
  4243. const gchar *key = luaL_checkstring (L, 2);
  4244. guint id = 0;
  4245. if (task && key) {
  4246. if (lua_type (L, 3) == LUA_TNUMBER) {
  4247. id = lua_tonumber (L, 3);
  4248. }
  4249. if (!lua_task_get_cached (L, task, key, id)) {
  4250. lua_pushnil (L);
  4251. }
  4252. }
  4253. else {
  4254. luaL_error (L, "invalid arguments");
  4255. }
  4256. return 1;
  4257. }
  4258. static gint
  4259. lua_task_cache_set (lua_State *L)
  4260. {
  4261. LUA_TRACE_POINT;
  4262. struct rspamd_task *task = lua_check_task (L, 1);
  4263. const gchar *key = luaL_checkstring (L, 2);
  4264. guint id = 0;
  4265. if (task && key && lua_gettop (L) >= 3) {
  4266. if (lua_type (L, 4) == LUA_TNUMBER) {
  4267. id = lua_tonumber (L, 4);
  4268. }
  4269. lua_task_set_cached (L, task, key, 3, id);
  4270. }
  4271. else {
  4272. luaL_error (L, "invalid arguments");
  4273. }
  4274. return 0;
  4275. }
  4276. struct lua_file_cbdata {
  4277. gchar *fname;
  4278. gint fd;
  4279. gboolean keep;
  4280. };
  4281. static void
  4282. lua_tmp_file_dtor (gpointer p)
  4283. {
  4284. struct lua_file_cbdata *cbdata = p;
  4285. if (!cbdata->keep) {
  4286. unlink (cbdata->fname);
  4287. }
  4288. close (cbdata->fd);
  4289. }
  4290. static gint
  4291. lua_task_store_in_file (lua_State *L)
  4292. {
  4293. LUA_TRACE_POINT;
  4294. struct rspamd_task *task = lua_check_task (L, 1);
  4295. gboolean force_new = FALSE, keep = FALSE;
  4296. gchar fpath[PATH_MAX];
  4297. const gchar *tmpmask = NULL, *fname = NULL;
  4298. guint64 mode = 00600;
  4299. gint fd;
  4300. struct lua_file_cbdata *cbdata;
  4301. GError *err = NULL;
  4302. if (task) {
  4303. if (lua_istable (L, 2)) {
  4304. if (!rspamd_lua_parse_table_arguments (L, 2, &err,
  4305. "filename=S;tmpmask=S;mode=I;force_new=B;keep=B",
  4306. &fname, &tmpmask, &mode, &force_new, &keep)) {
  4307. msg_err_task ("cannot get parameters list: %e", err);
  4308. if (err) {
  4309. g_error_free (err);
  4310. }
  4311. return luaL_error (L, "invalid arguments");
  4312. }
  4313. }
  4314. else if (lua_isnumber (L, 2)) {
  4315. mode = lua_tonumber (L, 2);
  4316. }
  4317. if (!force_new && (task->flags & RSPAMD_TASK_FLAG_FILE) &&
  4318. task->msg.fpath) {
  4319. lua_pushstring (L, task->msg.fpath);
  4320. }
  4321. else {
  4322. if (fname == NULL) {
  4323. if (tmpmask == NULL) {
  4324. rspamd_snprintf (fpath, sizeof (fpath), "%s%c%s",
  4325. task->cfg->temp_dir,
  4326. G_DIR_SEPARATOR, "rmsg-XXXXXXXXXX");
  4327. }
  4328. else {
  4329. rspamd_snprintf (fpath, sizeof (fpath), "%s", tmpmask);
  4330. }
  4331. fd = mkstemp (fpath);
  4332. fname = fpath;
  4333. if (fd != -1) {
  4334. fchmod (fd, mode);
  4335. }
  4336. }
  4337. else {
  4338. fd = rspamd_file_xopen (fname, O_WRONLY|O_CREAT|O_EXCL,
  4339. (guint)mode, FALSE);
  4340. }
  4341. if (fd == -1) {
  4342. msg_err_task ("cannot save file: %s", strerror (errno));
  4343. lua_pushnil (L);
  4344. }
  4345. else {
  4346. if (write (fd, task->msg.begin, task->msg.len) == -1) {
  4347. msg_err_task ("cannot write file %s: %s", fpath,
  4348. strerror (errno));
  4349. unlink (fname);
  4350. close (fd);
  4351. lua_pushnil (L);
  4352. return 1;
  4353. }
  4354. cbdata = rspamd_mempool_alloc (task->task_pool, sizeof (*cbdata));
  4355. cbdata->fd = fd;
  4356. cbdata->fname = rspamd_mempool_strdup (task->task_pool, fname);
  4357. cbdata->keep = keep;
  4358. lua_pushstring (L, cbdata->fname);
  4359. rspamd_mempool_add_destructor (task->task_pool,
  4360. lua_tmp_file_dtor, cbdata);
  4361. }
  4362. }
  4363. }
  4364. else {
  4365. luaL_error (L, "invalid arguments");
  4366. }
  4367. return 1;
  4368. }
  4369. static gint
  4370. lua_task_process_regexp (lua_State *L)
  4371. {
  4372. LUA_TRACE_POINT;
  4373. struct rspamd_task *task = lua_check_task (L, 1);
  4374. struct rspamd_lua_regexp *re = NULL;
  4375. gboolean strong = FALSE;
  4376. const gchar *type_str = NULL, *header_str = NULL;
  4377. gsize header_len = 0;
  4378. GError *err = NULL;
  4379. gint ret = 0;
  4380. enum rspamd_re_type type = RSPAMD_RE_BODY;
  4381. /*
  4382. * - `re`* : regular expression object
  4383. * - `type`*: type of regular expression:
  4384. * + `mime`: mime regexp
  4385. * + `rawmime`: raw mime regexp
  4386. * + `header`: header regexp
  4387. * + `rawheader`: raw header expression
  4388. * + `body`: raw body regexp
  4389. * + `url`: url regexp
  4390. * - `header`: for header and rawheader regexp means the name of header
  4391. * - `strong`: case sensitive match for headers
  4392. */
  4393. if (task != NULL) {
  4394. if (!rspamd_lua_parse_table_arguments (L, 2, &err,
  4395. "*re=U{regexp};*type=S;header=V;strong=B",
  4396. &re, &type_str, &header_len, &header_str,
  4397. &strong)) {
  4398. msg_err_task ("cannot get parameters list: %e", err);
  4399. if (err) {
  4400. g_error_free (err);
  4401. }
  4402. return luaL_error (L, "invalid arguments");
  4403. }
  4404. else {
  4405. type = rspamd_re_cache_type_from_string (type_str);
  4406. if ((type == RSPAMD_RE_HEADER || type == RSPAMD_RE_RAWHEADER)
  4407. && header_str == NULL) {
  4408. msg_err_task (
  4409. "header argument is mandatory for header/rawheader regexps");
  4410. }
  4411. else {
  4412. ret = rspamd_re_cache_process (task, re->re, type,
  4413. (gpointer) header_str, header_len, strong);
  4414. }
  4415. }
  4416. }
  4417. else {
  4418. return luaL_error (L, "invalid arguments");
  4419. }
  4420. lua_pushinteger (L, ret);
  4421. return 1;
  4422. }
  4423. static gint
  4424. lua_task_get_metric_result (lua_State *L)
  4425. {
  4426. LUA_TRACE_POINT;
  4427. struct rspamd_task *task = lua_check_task (L, 1);
  4428. struct rspamd_metric_result *metric_res;
  4429. struct rspamd_action *action;
  4430. if (task) {
  4431. metric_res = task->result;
  4432. /* Fields added:
  4433. * - `score`: current score
  4434. * - `action`: current action as a string
  4435. * - `nnegative`: number of negative rules matched
  4436. * - `npositive`: number of positive rules matched
  4437. * - `positive_score`: total score for positive rules
  4438. * - `negative_score`: total score for negative rules
  4439. * - `passthrough`: set to true if message has a passthrough result
  4440. */
  4441. lua_createtable (L, 0, 7);
  4442. lua_pushstring (L, "score");
  4443. lua_pushnumber (L, metric_res->score);
  4444. lua_settable (L, -3);
  4445. action = rspamd_check_action_metric (task);
  4446. if (action) {
  4447. lua_pushstring (L, "action");
  4448. lua_pushstring (L, action->name);
  4449. lua_settable (L, -3);
  4450. }
  4451. lua_pushstring (L, "nnegative");
  4452. lua_pushnumber (L, metric_res->nnegative);
  4453. lua_settable (L, -3);
  4454. lua_pushstring (L, "npositive");
  4455. lua_pushnumber (L, metric_res->npositive);
  4456. lua_settable (L, -3);
  4457. lua_pushstring (L, "positive_score");
  4458. lua_pushnumber (L, metric_res->positive_score);
  4459. lua_settable (L, -3);
  4460. lua_pushstring (L, "negative_score");
  4461. lua_pushnumber (L, metric_res->negative_score);
  4462. lua_settable (L, -3);
  4463. lua_pushstring (L, "passthrough");
  4464. lua_pushboolean (L, !!(metric_res->passthrough_result != NULL));
  4465. lua_settable (L, -3);
  4466. }
  4467. else {
  4468. return luaL_error (L, "invalid arguments");
  4469. }
  4470. return 1;
  4471. }
  4472. static gint
  4473. lua_task_get_metric_score (lua_State *L)
  4474. {
  4475. LUA_TRACE_POINT;
  4476. struct rspamd_task *task = lua_check_task (L, 1);
  4477. gdouble rs;
  4478. struct rspamd_metric_result *metric_res;
  4479. if (task) {
  4480. if ((metric_res = task->result) != NULL) {
  4481. lua_createtable (L, 2, 0);
  4482. lua_pushnumber (L, isnan (metric_res->score) ? 0.0 : metric_res->score);
  4483. rs = rspamd_task_get_required_score (task, metric_res);
  4484. lua_rawseti (L, -2, 1);
  4485. lua_pushnumber (L, rs);
  4486. lua_rawseti (L, -2, 2);
  4487. }
  4488. else {
  4489. lua_pushnil (L);
  4490. }
  4491. }
  4492. else {
  4493. return luaL_error (L, "invalid arguments");
  4494. }
  4495. return 1;
  4496. }
  4497. static gint
  4498. lua_task_get_metric_action (lua_State *L)
  4499. {
  4500. LUA_TRACE_POINT;
  4501. struct rspamd_task *task = lua_check_task (L, 1);
  4502. struct rspamd_metric_result *metric_res;
  4503. struct rspamd_action *action;
  4504. if (task) {
  4505. metric_res = task->result;
  4506. action = rspamd_check_action_metric (task);
  4507. lua_pushstring (L, action->name);
  4508. }
  4509. else {
  4510. return luaL_error (L, "invalid arguments");
  4511. }
  4512. return 1;
  4513. }
  4514. static gint
  4515. lua_task_set_metric_score (lua_State *L)
  4516. {
  4517. LUA_TRACE_POINT;
  4518. struct rspamd_task *task = lua_check_task (L, 1);
  4519. struct rspamd_metric_result *metric_res;
  4520. gdouble nscore;
  4521. if (lua_isnumber (L, 2)) {
  4522. nscore = luaL_checknumber (L, 2);
  4523. }
  4524. else {
  4525. nscore = luaL_checknumber (L, 3);
  4526. }
  4527. if (task) {
  4528. if ((metric_res = task->result) != NULL) {
  4529. msg_debug_task ("set metric score from %.2f to %.2f",
  4530. metric_res->score, nscore);
  4531. metric_res->score = nscore;
  4532. lua_pushboolean (L, true);
  4533. }
  4534. else {
  4535. lua_pushboolean (L, false);
  4536. }
  4537. }
  4538. else {
  4539. return luaL_error (L, "invalid arguments");
  4540. }
  4541. return 1;
  4542. }
  4543. static gint
  4544. lua_task_disable_action (lua_State *L)
  4545. {
  4546. LUA_TRACE_POINT;
  4547. struct rspamd_task *task = lua_check_task (L, 1);
  4548. const gchar *action_name;
  4549. struct rspamd_action_result *action_res;
  4550. action_name = luaL_checkstring (L, 2);
  4551. if (task && action_name) {
  4552. for (guint i = 0; i < task->result->nactions; i ++) {
  4553. action_res = &task->result->actions_limits[i];
  4554. if (strcmp (action_name, action_res->action->name) == 0) {
  4555. if (isnan (action_res->cur_limit)) {
  4556. lua_pushboolean (L, false);
  4557. }
  4558. else {
  4559. action_res->cur_limit = NAN;
  4560. lua_pushboolean (L, true);
  4561. }
  4562. break;
  4563. }
  4564. }
  4565. }
  4566. else {
  4567. return luaL_error (L, "invalid arguments");
  4568. }
  4569. return 1;
  4570. }
  4571. static gint
  4572. lua_task_get_newlines_type (lua_State *L)
  4573. {
  4574. LUA_TRACE_POINT;
  4575. struct rspamd_task *task = lua_check_task (L, 1);
  4576. if (task) {
  4577. switch (task->nlines_type) {
  4578. case RSPAMD_TASK_NEWLINES_CR:
  4579. lua_pushstring (L, "cr");
  4580. break;
  4581. case RSPAMD_TASK_NEWLINES_LF:
  4582. lua_pushstring (L, "lf");
  4583. break;
  4584. case RSPAMD_TASK_NEWLINES_CRLF:
  4585. default:
  4586. lua_pushstring (L, "crlf");
  4587. break;
  4588. }
  4589. }
  4590. else {
  4591. return luaL_error (L, "invalid arguments");
  4592. }
  4593. return 1;
  4594. }
  4595. static void
  4596. lua_push_stat_token (lua_State *L, rspamd_token_t *tok)
  4597. {
  4598. gchar numbuf[64];
  4599. /* Table values
  4600. * - `data`: 64 bit number encoded as a string
  4601. * - `t1`: the first token (if any)
  4602. * - `t2`: the second token (if any)
  4603. * - `win`: window index
  4604. * - `flag`: table of strings:
  4605. * - `text`: text token
  4606. * - `meta`: meta token
  4607. * - `lua`: lua meta token
  4608. * - `exception`: exception
  4609. * - `subject`: subject token
  4610. * - `unigram`: unigram token
  4611. */
  4612. lua_createtable (L, 0, 5);
  4613. rspamd_snprintf (numbuf, sizeof (numbuf), "%uL", tok->data);
  4614. lua_pushstring (L, "data");
  4615. lua_pushstring (L, numbuf);
  4616. lua_settable (L, -3);
  4617. if (tok->t1) {
  4618. lua_pushstring (L, "t1");
  4619. lua_pushlstring (L, tok->t1->stemmed.begin, tok->t1->stemmed.len);
  4620. lua_settable (L, -3);
  4621. }
  4622. if (tok->t2) {
  4623. lua_pushstring (L, "t2");
  4624. lua_pushlstring (L, tok->t2->stemmed.begin, tok->t2->stemmed.len);
  4625. lua_settable (L, -3);
  4626. }
  4627. lua_pushstring (L, "win");
  4628. lua_pushinteger (L, tok->window_idx);
  4629. lua_settable (L, -3);
  4630. lua_pushstring (L, "flags");
  4631. lua_createtable (L, 0, 5);
  4632. /* Flags */
  4633. {
  4634. if (tok->flags & RSPAMD_STAT_TOKEN_FLAG_TEXT) {
  4635. lua_pushstring (L, "text");
  4636. lua_pushboolean (L, true);
  4637. lua_settable (L, -3);
  4638. }
  4639. if (tok->flags & RSPAMD_STAT_TOKEN_FLAG_META) {
  4640. lua_pushstring (L, "meta");
  4641. lua_pushboolean (L, true);
  4642. lua_settable (L, -3);
  4643. }
  4644. if (tok->flags & RSPAMD_STAT_TOKEN_FLAG_LUA_META) {
  4645. lua_pushstring (L, "lua");
  4646. lua_pushboolean (L, true);
  4647. lua_settable (L, -3);
  4648. }
  4649. if (tok->flags & RSPAMD_STAT_TOKEN_FLAG_EXCEPTION) {
  4650. lua_pushstring (L, "exception");
  4651. lua_pushboolean (L, true);
  4652. lua_settable (L, -3);
  4653. }
  4654. if (tok->flags & RSPAMD_STAT_TOKEN_FLAG_HEADER) {
  4655. lua_pushstring (L, "header");
  4656. lua_pushboolean (L, true);
  4657. lua_settable (L, -3);
  4658. }
  4659. }
  4660. lua_settable (L, -3);
  4661. }
  4662. static gint
  4663. lua_task_get_stat_tokens (lua_State *L)
  4664. {
  4665. LUA_TRACE_POINT;
  4666. struct rspamd_task *task = lua_check_task (L, 1);
  4667. guint i;
  4668. rspamd_token_t *tok;
  4669. if (task) {
  4670. if (!task->tokens) {
  4671. rspamd_stat_process_tokenize (NULL, task);
  4672. }
  4673. if (!task->tokens) {
  4674. lua_pushnil (L);
  4675. }
  4676. else {
  4677. lua_createtable (L, task->tokens->len, 0);
  4678. PTR_ARRAY_FOREACH (task->tokens, i, tok) {
  4679. lua_push_stat_token (L, tok);
  4680. lua_rawseti (L, -2, i + 1);
  4681. }
  4682. }
  4683. }
  4684. else {
  4685. return luaL_error (L, "invalid arguments");
  4686. }
  4687. return 1;
  4688. }
  4689. static gint
  4690. lua_task_set_metric_subject (lua_State *L)
  4691. {
  4692. LUA_TRACE_POINT;
  4693. struct rspamd_task *task = lua_check_task (L, 1);
  4694. const gchar *subject;
  4695. subject = luaL_checkstring (L, 2);
  4696. if (task && subject) {
  4697. rspamd_mempool_set_variable (task->task_pool, "metric_subject",
  4698. rspamd_mempool_strdup(task->task_pool, subject), NULL);
  4699. lua_pushboolean (L, true);
  4700. }
  4701. else {
  4702. return luaL_error (L, "invalid arguments");
  4703. }
  4704. return 1;
  4705. }
  4706. static gint
  4707. lua_task_get_protocol_reply (lua_State *L)
  4708. {
  4709. LUA_TRACE_POINT;
  4710. struct rspamd_task *task = lua_check_task (L, 1);
  4711. guint flags = 0;
  4712. ucl_object_t *obj;
  4713. if (!task) {
  4714. return luaL_error (L, "invalid arguments");
  4715. }
  4716. if (!(task->processed_stages & (RSPAMD_TASK_STAGE_POST_FILTERS >> 1))) {
  4717. return luaL_error (L, "must not be called before post-filters");
  4718. }
  4719. if (lua_istable (L, 2)) {
  4720. for (lua_pushnil (L); lua_next (L, 2); lua_pop (L, 1)) {
  4721. if (lua_isstring (L, -1)) {
  4722. const gchar *str = lua_tostring (L, -1);
  4723. if (strcmp (str, "default") == 0) {
  4724. flags |= RSPAMD_PROTOCOL_DEFAULT;
  4725. }
  4726. else if (strcmp (str, "basic") == 0) {
  4727. flags |= RSPAMD_PROTOCOL_BASIC;
  4728. }
  4729. else if (strcmp (str, "metrics") == 0) {
  4730. flags |= RSPAMD_PROTOCOL_METRICS;
  4731. }
  4732. else if (strcmp (str, "messages") == 0) {
  4733. flags |= RSPAMD_PROTOCOL_MESSAGES;
  4734. }
  4735. else if (strcmp (str, "rmilter") == 0) {
  4736. flags |= RSPAMD_PROTOCOL_RMILTER;
  4737. }
  4738. else if (strcmp (str, "dkim") == 0) {
  4739. flags |= RSPAMD_PROTOCOL_DKIM;
  4740. }
  4741. else if (strcmp (str, "extra") == 0) {
  4742. flags |= RSPAMD_PROTOCOL_EXTRA;
  4743. }
  4744. else {
  4745. msg_err_task ("invalid protocol flag: %s", str);
  4746. }
  4747. }
  4748. }
  4749. }
  4750. else {
  4751. flags = RSPAMD_PROTOCOL_DEFAULT;
  4752. }
  4753. obj = rspamd_protocol_write_ucl (task, flags);
  4754. if (obj) {
  4755. ucl_object_push_lua (L, obj, true);
  4756. }
  4757. else {
  4758. lua_pushnil (L);
  4759. }
  4760. return 1;
  4761. }
  4762. static gint
  4763. lua_task_headers_foreach (lua_State *L)
  4764. {
  4765. LUA_TRACE_POINT;
  4766. struct rspamd_task *task = lua_check_task (L, 1);
  4767. enum rspamd_lua_task_header_type how = RSPAMD_TASK_HEADER_PUSH_SIMPLE;
  4768. struct rspamd_lua_regexp *re = NULL;
  4769. GList *cur;
  4770. struct rspamd_mime_header *hdr;
  4771. gint old_top;
  4772. if (task && lua_isfunction (L, 2)) {
  4773. if (lua_istable (L, 3)) {
  4774. lua_pushstring (L, "full");
  4775. lua_gettable (L, 3);
  4776. if (lua_isboolean (L, -1) && lua_toboolean (L, -1)) {
  4777. how = RSPAMD_TASK_HEADER_PUSH_FULL;
  4778. }
  4779. lua_pop (L, 1);
  4780. lua_pushstring (L, "raw");
  4781. lua_gettable (L, 3);
  4782. if (lua_isboolean (L, -1) && lua_toboolean (L, -1)) {
  4783. how = RSPAMD_TASK_HEADER_PUSH_RAW;
  4784. }
  4785. lua_pop (L, 1);
  4786. lua_pushstring (L, "regexp");
  4787. lua_gettable (L, 3);
  4788. if (lua_isuserdata (L, -1)) {
  4789. re = *(struct rspamd_lua_regexp **)
  4790. rspamd_lua_check_udata (L, -1, "rspamd{regexp}");
  4791. }
  4792. lua_pop (L, 1);
  4793. }
  4794. if (task->headers_order) {
  4795. cur = task->headers_order->head;
  4796. while (cur) {
  4797. hdr = cur->data;
  4798. if (re && re->re) {
  4799. if (!rspamd_regexp_match (re->re, hdr->name,
  4800. strlen (hdr->name),FALSE)) {
  4801. cur = g_list_next (cur);
  4802. continue;
  4803. }
  4804. }
  4805. old_top = lua_gettop (L);
  4806. lua_pushvalue (L, 2);
  4807. lua_pushstring (L, hdr->name);
  4808. rspamd_lua_push_header (L, hdr, how);
  4809. if (lua_pcall (L, 2, LUA_MULTRET, 0) != 0) {
  4810. msg_err ("call to header_foreach failed: %s",
  4811. lua_tostring (L, -1));
  4812. lua_settop (L, old_top);
  4813. break;
  4814. }
  4815. else {
  4816. if (lua_gettop (L) > old_top) {
  4817. if (lua_isboolean (L, old_top + 1)) {
  4818. if (lua_toboolean (L, old_top + 1)) {
  4819. lua_settop (L, old_top);
  4820. break;
  4821. }
  4822. }
  4823. }
  4824. }
  4825. lua_settop (L, old_top);
  4826. cur = g_list_next (cur);
  4827. }
  4828. }
  4829. }
  4830. return 0;
  4831. }
  4832. static gint
  4833. lua_task_get_meta_words (lua_State *L)
  4834. {
  4835. LUA_TRACE_POINT;
  4836. struct rspamd_task *task = lua_check_task (L, 1);
  4837. enum rspamd_lua_words_type how = RSPAMD_LUA_WORDS_STEM;
  4838. if (task == NULL) {
  4839. return luaL_error (L, "invalid arguments");
  4840. }
  4841. if (task->meta_words == NULL) {
  4842. lua_createtable (L, 0, 0);
  4843. }
  4844. else {
  4845. if (lua_type (L, 2) == LUA_TSTRING) {
  4846. const gchar *how_str = lua_tostring (L, 2);
  4847. if (strcmp (how_str, "stem") == 0) {
  4848. how = RSPAMD_LUA_WORDS_STEM;
  4849. }
  4850. else if (strcmp (how_str, "norm") == 0) {
  4851. how = RSPAMD_LUA_WORDS_NORM;
  4852. }
  4853. else if (strcmp (how_str, "raw") == 0) {
  4854. how = RSPAMD_LUA_WORDS_RAW;
  4855. }
  4856. else if (strcmp (how_str, "full") == 0) {
  4857. how = RSPAMD_LUA_WORDS_FULL;
  4858. }
  4859. else {
  4860. return luaL_error (L, "unknown words type: %s", how_str);
  4861. }
  4862. }
  4863. return rspamd_lua_push_words (L, task->meta_words, how);
  4864. }
  4865. return 1;
  4866. }
  4867. static guint
  4868. lua_lookup_words_array (lua_State *L,
  4869. gint cbpos,
  4870. struct rspamd_task *task,
  4871. struct rspamd_lua_map *map,
  4872. GArray *words)
  4873. {
  4874. rspamd_stat_token_t *tok;
  4875. guint i, nmatched = 0;
  4876. gint err_idx;
  4877. gboolean matched;
  4878. const gchar *key;
  4879. gsize keylen;
  4880. for (i = 0; i < words->len; i ++) {
  4881. tok = &g_array_index (words, rspamd_stat_token_t, i);
  4882. matched = FALSE;
  4883. if (tok->normalized.len == 0) {
  4884. continue;
  4885. }
  4886. key = tok->normalized.begin;
  4887. keylen = tok->normalized.len;
  4888. switch (map->type) {
  4889. case RSPAMD_LUA_MAP_SET:
  4890. case RSPAMD_LUA_MAP_HASH:
  4891. /* We know that tok->normalized is zero terminated in fact */
  4892. if (rspamd_match_hash_map (map->data.hash, key)) {
  4893. matched = TRUE;
  4894. }
  4895. break;
  4896. case RSPAMD_LUA_MAP_REGEXP:
  4897. case RSPAMD_LUA_MAP_REGEXP_MULTIPLE:
  4898. if (rspamd_match_regexp_map_single (map->data.re_map, key,
  4899. keylen)) {
  4900. matched = TRUE;
  4901. }
  4902. break;
  4903. default:
  4904. g_assert_not_reached ();
  4905. break;
  4906. }
  4907. if (matched) {
  4908. nmatched ++;
  4909. lua_pushcfunction (L, &rspamd_lua_traceback);
  4910. err_idx = lua_gettop (L);
  4911. lua_pushvalue (L, cbpos); /* Function */
  4912. rspamd_lua_push_full_word (L, tok);
  4913. if (lua_pcall (L, 1, 0, err_idx) != 0) {
  4914. GString *tb = lua_touserdata (L, -1);
  4915. msg_err_task ("cannot call callback function for lookup words: %s",
  4916. tb->str);
  4917. g_string_free (tb, TRUE);
  4918. }
  4919. lua_settop (L, err_idx - 1);
  4920. }
  4921. }
  4922. return nmatched;
  4923. }
  4924. static gint
  4925. lua_task_lookup_words (lua_State *L)
  4926. {
  4927. LUA_TRACE_POINT;
  4928. struct rspamd_task *task = lua_check_task (L, 1);
  4929. struct rspamd_lua_map *map = lua_check_map (L, 2);
  4930. struct rspamd_mime_text_part *tp;
  4931. guint i, matches = 0;
  4932. if (task == NULL || map == NULL || lua_type (L, 3) != LUA_TFUNCTION) {
  4933. return luaL_error (L, "invalid arguments");
  4934. }
  4935. if (map->type != RSPAMD_LUA_MAP_SET &&
  4936. map->type != RSPAMD_LUA_MAP_REGEXP &&
  4937. map->type != RSPAMD_LUA_MAP_HASH &&
  4938. map->type != RSPAMD_LUA_MAP_REGEXP_MULTIPLE) {
  4939. return luaL_error (L, "invalid map type");
  4940. }
  4941. PTR_ARRAY_FOREACH (task->text_parts, i, tp) {
  4942. if (tp->utf_words) {
  4943. matches += lua_lookup_words_array (L, 3, task, map, tp->utf_words);
  4944. }
  4945. }
  4946. if (task->meta_words) {
  4947. matches += lua_lookup_words_array (L, 3, task, map, task->meta_words);
  4948. }
  4949. lua_pushinteger (L, matches);
  4950. return 1;
  4951. }
  4952. static gint
  4953. lua_task_topointer (lua_State *L)
  4954. {
  4955. LUA_TRACE_POINT;
  4956. struct rspamd_task *task = lua_check_task (L, 1);
  4957. if (task) {
  4958. lua_pushlightuserdata (L, task);
  4959. }
  4960. else {
  4961. return luaL_error (L, "invalid arguments");
  4962. }
  4963. return 1;
  4964. }
  4965. /* Image functions */
  4966. static gint
  4967. lua_image_get_width (lua_State *L)
  4968. {
  4969. LUA_TRACE_POINT;
  4970. struct rspamd_image *img = lua_check_image (L);
  4971. if (img != NULL) {
  4972. lua_pushinteger (L, img->width);
  4973. }
  4974. else {
  4975. return luaL_error (L, "invalid arguments");
  4976. }
  4977. return 1;
  4978. }
  4979. static gint
  4980. lua_image_get_height (lua_State *L)
  4981. {
  4982. LUA_TRACE_POINT;
  4983. struct rspamd_image *img = lua_check_image (L);
  4984. if (img != NULL) {
  4985. lua_pushinteger (L, img->height);
  4986. }
  4987. else {
  4988. return luaL_error (L, "invalid arguments");
  4989. }
  4990. return 1;
  4991. }
  4992. static gint
  4993. lua_image_get_type (lua_State *L)
  4994. {
  4995. LUA_TRACE_POINT;
  4996. struct rspamd_image *img = lua_check_image (L);
  4997. if (img != NULL) {
  4998. lua_pushstring (L, rspamd_image_type_str (img->type));
  4999. }
  5000. else {
  5001. return luaL_error (L, "invalid arguments");
  5002. }
  5003. return 1;
  5004. }
  5005. static gint
  5006. lua_image_get_size (lua_State *L)
  5007. {
  5008. LUA_TRACE_POINT;
  5009. struct rspamd_image *img = lua_check_image (L);
  5010. if (img != NULL) {
  5011. lua_pushinteger (L, img->data->len);
  5012. }
  5013. else {
  5014. return luaL_error (L, "invalid arguments");
  5015. }
  5016. return 1;
  5017. }
  5018. static gint
  5019. lua_image_get_filename (lua_State *L)
  5020. {
  5021. LUA_TRACE_POINT;
  5022. struct rspamd_image *img = lua_check_image (L);
  5023. if (img != NULL) {
  5024. if (img->filename != NULL) {
  5025. lua_pushlstring (L, img->filename->begin, img->filename->len);
  5026. }
  5027. else {
  5028. lua_pushnil (L);
  5029. }
  5030. }
  5031. else {
  5032. return luaL_error (L, "invalid arguments");
  5033. }
  5034. return 1;
  5035. }
  5036. /* Arvhive methods */
  5037. static gint
  5038. lua_archive_get_type (lua_State *L)
  5039. {
  5040. LUA_TRACE_POINT;
  5041. struct rspamd_archive *arch = lua_check_archive (L);
  5042. if (arch != NULL) {
  5043. lua_pushstring (L, rspamd_archive_type_str (arch->type));
  5044. }
  5045. else {
  5046. return luaL_error (L, "invalid arguments");
  5047. }
  5048. return 1;
  5049. }
  5050. static gint
  5051. lua_archive_get_files (lua_State *L)
  5052. {
  5053. LUA_TRACE_POINT;
  5054. struct rspamd_archive *arch = lua_check_archive (L);
  5055. guint i;
  5056. struct rspamd_archive_file *f;
  5057. if (arch != NULL) {
  5058. lua_createtable (L, arch->files->len, 0);
  5059. for (i = 0; i < arch->files->len; i ++) {
  5060. f = g_ptr_array_index (arch->files, i);
  5061. lua_pushlstring (L, f->fname->str, f->fname->len);
  5062. lua_rawseti (L, -2, i + 1);
  5063. }
  5064. }
  5065. else {
  5066. return luaL_error (L, "invalid arguments");
  5067. }
  5068. return 1;
  5069. }
  5070. static gint
  5071. lua_archive_get_files_full (lua_State *L)
  5072. {
  5073. LUA_TRACE_POINT;
  5074. struct rspamd_archive *arch = lua_check_archive (L);
  5075. guint i;
  5076. struct rspamd_archive_file *f;
  5077. if (arch != NULL) {
  5078. lua_createtable (L, arch->files->len, 0);
  5079. for (i = 0; i < arch->files->len; i ++) {
  5080. f = g_ptr_array_index (arch->files, i);
  5081. lua_createtable (L, 0, 4);
  5082. lua_pushstring (L, "name");
  5083. lua_pushlstring (L, f->fname->str, f->fname->len);
  5084. lua_settable (L, -3);
  5085. lua_pushstring (L, "compressed_size");
  5086. lua_pushinteger (L, f->compressed_size);
  5087. lua_settable (L, -3);
  5088. lua_pushstring (L, "uncompressed_size");
  5089. lua_pushinteger (L, f->uncompressed_size);
  5090. lua_settable (L, -3);
  5091. lua_pushstring (L, "encrypted");
  5092. lua_pushboolean (L, (f->flags & RSPAMD_ARCHIVE_FILE_ENCRYPTED) ? true : false);
  5093. lua_settable (L, -3);
  5094. lua_rawseti (L, -2, i + 1);
  5095. }
  5096. }
  5097. else {
  5098. return luaL_error (L, "invalid arguments");
  5099. }
  5100. return 1;
  5101. }
  5102. static gint
  5103. lua_archive_is_encrypted (lua_State *L)
  5104. {
  5105. LUA_TRACE_POINT;
  5106. struct rspamd_archive *arch = lua_check_archive (L);
  5107. if (arch != NULL) {
  5108. lua_pushboolean (L, (arch->flags & RSPAMD_ARCHIVE_ENCRYPTED) ? true : false);
  5109. }
  5110. else {
  5111. return luaL_error (L, "invalid arguments");
  5112. }
  5113. return 1;
  5114. }
  5115. static gint
  5116. lua_archive_get_size (lua_State *L)
  5117. {
  5118. LUA_TRACE_POINT;
  5119. struct rspamd_archive *arch = lua_check_archive (L);
  5120. if (arch != NULL) {
  5121. lua_pushinteger (L, arch->size);
  5122. }
  5123. else {
  5124. return luaL_error (L, "invalid arguments");
  5125. }
  5126. return 1;
  5127. }
  5128. static gint
  5129. lua_archive_get_filename (lua_State *L)
  5130. {
  5131. LUA_TRACE_POINT;
  5132. struct rspamd_archive *arch = lua_check_archive (L);
  5133. if (arch != NULL) {
  5134. lua_pushlstring (L, arch->archive_name->begin, arch->archive_name->len);
  5135. }
  5136. else {
  5137. return luaL_error (L, "invalid arguments");
  5138. }
  5139. return 1;
  5140. }
  5141. /* Init part */
  5142. static gint
  5143. lua_load_task (lua_State * L)
  5144. {
  5145. lua_newtable (L);
  5146. luaL_register (L, NULL, tasklib_f);
  5147. return 1;
  5148. }
  5149. static void
  5150. luaopen_archive (lua_State * L)
  5151. {
  5152. rspamd_lua_new_class (L, "rspamd{archive}", archivelib_m);
  5153. lua_pop (L, 1);
  5154. }
  5155. void
  5156. luaopen_task (lua_State * L)
  5157. {
  5158. rspamd_lua_new_class (L, "rspamd{task}", tasklib_m);
  5159. lua_pop (L, 1);
  5160. rspamd_lua_add_preload (L, "rspamd_task", lua_load_task);
  5161. luaopen_archive (L);
  5162. }
  5163. void
  5164. luaopen_image (lua_State * L)
  5165. {
  5166. rspamd_lua_new_class (L, "rspamd{image}", imagelib_m);
  5167. lua_pop (L, 1);
  5168. }
  5169. void
  5170. rspamd_lua_task_push (lua_State *L, struct rspamd_task *task)
  5171. {
  5172. struct rspamd_task **ptask;
  5173. ptask = lua_newuserdata (L, sizeof (gpointer));
  5174. rspamd_lua_setclass (L, "rspamd{task}", -1);
  5175. *ptask = task;
  5176. }