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

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