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

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