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_mimepart.c 56KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  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 "libmime/message.h"
  19. #include "libmime/lang_detection.h"
  20. #include "libstat/stat_api.h"
  21. #include "libcryptobox/cryptobox.h"
  22. #include "libutil/shingles.h"
  23. #include "contrib/uthash/utlist.h"
  24. /* Textpart methods */
  25. /***
  26. * @module rspamd_textpart
  27. * This module provides different methods to manipulate text parts data. Text parts
  28. * could be obtained from the `rspamd_task` by using of method `task:get_text_parts()`
  29. @example
  30. rspamd_config.R_EMPTY_IMAGE = function (task)
  31. parts = task:get_text_parts()
  32. if parts then
  33. for _,part in ipairs(parts) do
  34. if part:is_empty() then
  35. texts = task:get_texts()
  36. if texts then
  37. return true
  38. end
  39. return false
  40. end
  41. end
  42. end
  43. return false
  44. end
  45. */
  46. /***
  47. * @method text_part:is_utf()
  48. * Return TRUE if part is a valid utf text
  49. * @return {boolean} true if part is valid `UTF8` part
  50. */
  51. LUA_FUNCTION_DEF (textpart, is_utf);
  52. /***
  53. * @method text_part:has_8bit_raw()
  54. * Return TRUE if a part has raw 8bit characters
  55. * @return {boolean} true if a part has raw 8bit characters
  56. */
  57. LUA_FUNCTION_DEF (textpart, has_8bit_raw);
  58. /***
  59. * @method text_part:has_8bit()
  60. * Return TRUE if a part has raw 8bit characters
  61. * @return {boolean} true if a part has encoded 8bit characters
  62. */
  63. LUA_FUNCTION_DEF (textpart, has_8bit);
  64. /***
  65. * @method text_part:get_content([type])
  66. * Get the text of the part (html tags stripped). Optional `type` defines type of content to get:
  67. * - `content` (default): utf8 content with HTML tags stripped and newlines preserved
  68. * - `content_oneline`: utf8 content with HTML tags and newlines stripped
  69. * - `raw`: raw content, not mime decoded nor utf8 converted
  70. * - `raw_parsed`: raw content, mime decoded, not utf8 converted
  71. * - `raw_utf`: raw content, mime decoded, utf8 converted (but with HTML tags and newlines)
  72. * @return {text} `UTF8` encoded content of the part (zero-copy if not converted to a lua string)
  73. */
  74. LUA_FUNCTION_DEF (textpart, get_content);
  75. /***
  76. * @method text_part:get_raw_content()
  77. * Get the original text of the part
  78. * @return {text} `UTF8` encoded content of the part (zero-copy if not converted to a lua string)
  79. */
  80. LUA_FUNCTION_DEF (textpart, get_raw_content);
  81. /***
  82. * @method text_part:get_content_oneline()
  83. *Get the text of the part (html tags and newlines stripped)
  84. * @return {text} `UTF8` encoded content of the part (zero-copy if not converted to a lua string)
  85. */
  86. LUA_FUNCTION_DEF (textpart, get_content_oneline);
  87. /***
  88. * @method text_part:get_length()
  89. * Get length of the text of the part
  90. * @return {integer} length of part in **bytes**
  91. */
  92. LUA_FUNCTION_DEF (textpart, get_length);
  93. /***
  94. * @method mime_part:get_raw_length()
  95. * Get length of the **raw** content of the part (e.g. HTML with tags unstripped)
  96. * @return {integer} length of part in **bytes**
  97. */
  98. LUA_FUNCTION_DEF (textpart, get_raw_length);
  99. /***
  100. * @method mime_part:get_urls_length()
  101. * Get length of the urls within the part
  102. * @return {integer} length of urls in **bytes**
  103. */
  104. LUA_FUNCTION_DEF (textpart, get_urls_length);
  105. /***
  106. * @method mime_part:get_lines_count()
  107. * Get lines number in the part
  108. * @return {integer} number of lines in the part
  109. */
  110. LUA_FUNCTION_DEF (textpart, get_lines_count);
  111. /***
  112. * @method mime_part:get_stats()
  113. * Returns a table with the following data:
  114. * - `lines`: number of lines
  115. * - `spaces`: number of spaces
  116. * - `double_spaces`: double spaces
  117. * - `empty_lines`: number of empty lines
  118. * - `non_ascii_characters`: number of non ascii characters
  119. * - `ascii_characters`: number of ascii characters
  120. * @return {table} table of stats
  121. */
  122. LUA_FUNCTION_DEF (textpart, get_stats);
  123. /***
  124. * @method mime_part:get_words_count()
  125. * Get words number in the part
  126. * @return {integer} number of words in the part
  127. */
  128. LUA_FUNCTION_DEF (textpart, get_words_count);
  129. /***
  130. * @method mime_part:get_words([how])
  131. * Get words in the part. Optional `how` argument defines type of words returned:
  132. * - `stem`: stemmed words (default)
  133. * - `norm`: normalised words (utf normalised + lowercased)
  134. * - `raw`: raw words in utf (if possible)
  135. * - `full`: list of tables, each table has the following fields:
  136. * - [1] - stemmed word
  137. * - [2] - normalised word
  138. * - [3] - raw word
  139. * - [4] - flags (table of strings)
  140. * @return {table/strings} words in the part
  141. */
  142. LUA_FUNCTION_DEF (textpart, get_words);
  143. /***
  144. * @method mime_part:filter_words(regexp, [how][, max]])
  145. * Filter words using some regexp:
  146. * - `stem`: stemmed words (default)
  147. * - `norm`: normalised words (utf normalised + lowercased)
  148. * - `raw`: raw words in utf (if possible)
  149. * - `full`: list of tables, each table has the following fields:
  150. * - [1] - stemmed word
  151. * - [2] - normalised word
  152. * - [3] - raw word
  153. * - [4] - flags (table of strings)
  154. * @param {rspamd_regexp} regexp regexp to match
  155. * @param {string} how what words to extract
  156. * @param {number} max maximum number of hits returned (all hits if <= 0 or nil)
  157. * @return {table/strings} words matching regexp
  158. */
  159. LUA_FUNCTION_DEF (textpart, filter_words);
  160. /***
  161. * @method text_part:is_empty()
  162. * Returns `true` if the specified part is empty
  163. * @return {bool} whether a part is empty
  164. */
  165. LUA_FUNCTION_DEF (textpart, is_empty);
  166. /***
  167. * @method text_part:is_html()
  168. * Returns `true` if the specified part has HTML content
  169. * @return {bool} whether a part is HTML part
  170. */
  171. LUA_FUNCTION_DEF (textpart, is_html);
  172. /***
  173. * @method text_part:get_html()
  174. * Returns html content of the specified part
  175. * @return {html} html content
  176. */
  177. LUA_FUNCTION_DEF (textpart, get_html);
  178. /***
  179. * @method text_part:get_language()
  180. * Returns the code of the most used unicode script in the text part. Does not work with raw parts
  181. * @return {string} short abbreviation (such as `ru`) for the script's language
  182. */
  183. LUA_FUNCTION_DEF (textpart, get_language);
  184. /***
  185. * @method text_part:get_charset()
  186. * Returns part real charset
  187. * @return {string} charset of the part
  188. */
  189. LUA_FUNCTION_DEF (textpart, get_charset);
  190. /***
  191. * @method text_part:get_languages()
  192. * Returns array of tables of all languages detected for a part:
  193. * - 'code': language code (short string)
  194. * - 'prob': logarithm of probability
  195. * @return {array|tables} all languages detected for the part
  196. */
  197. LUA_FUNCTION_DEF (textpart, get_languages);
  198. /***
  199. * @method text_part:get_fuzzy_hashes(mempool)
  200. * @param {rspamd_mempool} mempool - memory pool (usually task pool)
  201. * Returns direct hash of textpart as a string and array [1..32] of shingles each represented as a following table:
  202. * - [1] - 64 bit fuzzy hash represented as a string
  203. * - [2..4] - strings used to generate this hash
  204. * @return {string,array|tables} fuzzy hashes calculated
  205. */
  206. LUA_FUNCTION_DEF (textpart, get_fuzzy_hashes);
  207. /***
  208. * @method text_part:get_mimepart()
  209. * Returns the mime part object corresponding to this text part
  210. * @return {mimepart} mimepart object
  211. */
  212. LUA_FUNCTION_DEF (textpart, get_mimepart);
  213. static const struct luaL_reg textpartlib_m[] = {
  214. LUA_INTERFACE_DEF (textpart, is_utf),
  215. LUA_INTERFACE_DEF (textpart, has_8bit_raw),
  216. LUA_INTERFACE_DEF (textpart, has_8bit),
  217. LUA_INTERFACE_DEF (textpart, get_content),
  218. LUA_INTERFACE_DEF (textpart, get_raw_content),
  219. LUA_INTERFACE_DEF (textpart, get_content_oneline),
  220. LUA_INTERFACE_DEF (textpart, get_length),
  221. LUA_INTERFACE_DEF (textpart, get_raw_length),
  222. LUA_INTERFACE_DEF (textpart, get_urls_length),
  223. LUA_INTERFACE_DEF (textpart, get_lines_count),
  224. LUA_INTERFACE_DEF (textpart, get_words_count),
  225. LUA_INTERFACE_DEF (textpart, get_words),
  226. LUA_INTERFACE_DEF (textpart, filter_words),
  227. LUA_INTERFACE_DEF (textpart, is_empty),
  228. LUA_INTERFACE_DEF (textpart, is_html),
  229. LUA_INTERFACE_DEF (textpart, get_html),
  230. LUA_INTERFACE_DEF (textpart, get_language),
  231. LUA_INTERFACE_DEF (textpart, get_charset),
  232. LUA_INTERFACE_DEF (textpart, get_languages),
  233. LUA_INTERFACE_DEF (textpart, get_mimepart),
  234. LUA_INTERFACE_DEF (textpart, get_stats),
  235. LUA_INTERFACE_DEF (textpart, get_fuzzy_hashes),
  236. {"__tostring", rspamd_lua_class_tostring},
  237. {NULL, NULL}
  238. };
  239. /* Mimepart methods */
  240. /***
  241. * @module rspamd_mimepart
  242. * This module provides access to mime parts found in a message
  243. @example
  244. rspamd_config.MISSING_CONTENT_TYPE = function(task)
  245. local parts = task:get_parts()
  246. if parts and #parts > 1 then
  247. -- We have more than one part
  248. for _,p in ipairs(parts) do
  249. local ct = p:get_header('Content-Type')
  250. -- And some parts have no Content-Type header
  251. if not ct then
  252. return true
  253. end
  254. end
  255. end
  256. return false
  257. end
  258. */
  259. /***
  260. * @method mime_part:get_header(name[, case_sensitive])
  261. * Get decoded value of a header specified with optional case_sensitive flag.
  262. * By default headers are searched in caseless matter.
  263. * @param {string} name name of header to get
  264. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  265. * @return {string} decoded value of a header
  266. */
  267. LUA_FUNCTION_DEF (mimepart, get_header);
  268. /***
  269. * @method mime_part:get_header_raw(name[, case_sensitive])
  270. * Get raw value of a header specified with optional case_sensitive flag.
  271. * By default headers are searched in caseless matter.
  272. * @param {string} name name of header to get
  273. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  274. * @return {string} raw value of a header
  275. */
  276. LUA_FUNCTION_DEF (mimepart, get_header_raw);
  277. /***
  278. * @method mime_part:get_header_full(name[, case_sensitive])
  279. * Get raw value of a header specified with optional case_sensitive flag.
  280. * By default headers are searched in caseless matter. This method returns more
  281. * information about the header as a list of tables with the following structure:
  282. *
  283. * - `name` - name of a header
  284. * - `value` - raw value of a header
  285. * - `decoded` - decoded value of a header
  286. * - `tab_separated` - `true` if a header and a value are separated by `tab` character
  287. * - `empty_separator` - `true` if there are no separator between a header and a value
  288. * @param {string} name name of header to get
  289. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  290. * @return {list of tables} all values of a header as specified above
  291. @example
  292. function check_header_delimiter_tab(task, header_name)
  293. for _,rh in ipairs(task:get_header_full(header_name)) do
  294. if rh['tab_separated'] then return true end
  295. end
  296. return false
  297. end
  298. */
  299. LUA_FUNCTION_DEF (mimepart, get_header_full);
  300. /***
  301. * @method mimepart:get_header_count(name[, case_sensitive])
  302. * Lightweight version if you need just a header's count
  303. * * By default headers are searched in caseless matter.
  304. * @param {string} name name of header to get
  305. * @param {boolean} case_sensitive case sensitiveness flag to search for a header
  306. * @return {number} number of header's occurrences or 0 if not found
  307. */
  308. LUA_FUNCTION_DEF (mimepart, get_header_count);
  309. /***
  310. * @method mimepart:get_raw_headers()
  311. * Get all undecoded headers of a mime part as a string
  312. * @return {rspamd_text} all raw headers for a message as opaque text
  313. */
  314. LUA_FUNCTION_DEF (mimepart, get_raw_headers);
  315. /***
  316. * @method mimepart:get_headers()
  317. * Get all undecoded headers of a mime part as a string
  318. * @return {rspamd_text} all raw headers for a message as opaque text
  319. */
  320. LUA_FUNCTION_DEF (mimepart, get_headers);
  321. /***
  322. * @method mime_part:get_content()
  323. * Get the parsed content of part
  324. * @return {text} opaque text object (zero-copy if not casted to lua string)
  325. */
  326. LUA_FUNCTION_DEF (mimepart, get_content);
  327. /***
  328. * @method mime_part:get_raw_content()
  329. * Get the raw content of part
  330. * @return {text} opaque text object (zero-copy if not casted to lua string)
  331. */
  332. LUA_FUNCTION_DEF (mimepart, get_raw_content);
  333. /***
  334. * @method mime_part:get_length()
  335. * Get length of the content of the part
  336. * @return {integer} length of part in **bytes**
  337. */
  338. LUA_FUNCTION_DEF (mimepart, get_length);
  339. /***
  340. * @method mime_part:get_type()
  341. * Extract content-type string of the mime part
  342. * @return {string,string} content type in form 'type','subtype'
  343. */
  344. LUA_FUNCTION_DEF (mimepart, get_type);
  345. /***
  346. * @method mime_part:get_type_full()
  347. * Extract content-type string of the mime part with all attributes
  348. * @return {string,string,table} content type in form 'type','subtype', {attrs}
  349. */
  350. LUA_FUNCTION_DEF (mimepart, get_type_full);
  351. /***
  352. * @method mime_part:get_detected_type()
  353. * Extract content-type string of the mime part. Use lua_magic detection
  354. * @return {string,string} content type in form 'type','subtype'
  355. */
  356. LUA_FUNCTION_DEF (mimepart, get_detected_type);
  357. /***
  358. * @method mime_part:get_detected_type_full()
  359. * Extract content-type string of the mime part with all attributes. Use lua_magic detection
  360. * @return {string,string,table} content type in form 'type','subtype', {attrs}
  361. */
  362. LUA_FUNCTION_DEF (mimepart, get_detected_type_full);
  363. /***
  364. * @method mime_part:get_detected_ext()
  365. * Returns a msdos extension name according to lua_magic detection
  366. * @return {string} detected extension (see lua_magic.types)
  367. */
  368. LUA_FUNCTION_DEF (mimepart, get_detected_ext);
  369. /***
  370. * @method mime_part:get_cte()
  371. * Extract content-transfer-encoding for a part
  372. * @return {string} content transfer encoding (e.g. `base64` or `7bit`)
  373. */
  374. LUA_FUNCTION_DEF (mimepart, get_cte);
  375. /***
  376. * @method mime_part:get_filename()
  377. * Extract filename associated with mime part if it is an attachment
  378. * @return {string} filename or `nil` if no file is associated with this part
  379. */
  380. LUA_FUNCTION_DEF (mimepart, get_filename);
  381. /***
  382. * @method mime_part:is_image()
  383. * Returns true if mime part is an image
  384. * @return {bool} true if a part is an image
  385. */
  386. LUA_FUNCTION_DEF (mimepart, is_image);
  387. /***
  388. * @method mime_part:get_image()
  389. * Returns rspamd_image structure associated with this part. This structure has
  390. * the following methods:
  391. *
  392. * * `get_width` - return width of an image in pixels
  393. * * `get_height` - return height of an image in pixels
  394. * * `get_type` - return string representation of image's type (e.g. 'jpeg')
  395. * * `get_filename` - return string with image's file name
  396. * * `get_size` - return size in bytes
  397. * @return {rspamd_image} image structure or nil if a part is not an image
  398. */
  399. LUA_FUNCTION_DEF (mimepart, get_image);
  400. /***
  401. * @method mime_part:is_archive()
  402. * Returns true if mime part is an archive
  403. * @return {bool} true if a part is an archive
  404. */
  405. LUA_FUNCTION_DEF (mimepart, is_archive);
  406. /***
  407. * @method mime_part:is_attachment()
  408. * Returns true if mime part looks like an attachment
  409. * @return {bool} true if a part looks like an attachment
  410. */
  411. LUA_FUNCTION_DEF (mimepart, is_attachment);
  412. /***
  413. * @method mime_part:get_archive()
  414. * Returns rspamd_archive structure associated with this part. This structure has
  415. * the following methods:
  416. *
  417. * * `get_files` - return list of strings with filenames inside archive
  418. * * `get_files_full` - return list of tables with all information about files
  419. * * `is_encrypted` - return true if an archive is encrypted
  420. * * `get_type` - return string representation of image's type (e.g. 'zip')
  421. * * `get_filename` - return string with archive's file name
  422. * * `get_size` - return size in bytes
  423. * @return {rspamd_archive} archive structure or nil if a part is not an archive
  424. */
  425. LUA_FUNCTION_DEF (mimepart, get_archive);
  426. /***
  427. * @method mime_part:is_multipart()
  428. * Returns true if mime part is a multipart part
  429. * @return {bool} true if a part is is a multipart part
  430. */
  431. LUA_FUNCTION_DEF (mimepart, is_multipart);
  432. /***
  433. * @method mime_part:is_message()
  434. * Returns true if mime part is a message part (message/rfc822)
  435. * @return {bool} true if a part is is a message part
  436. */
  437. LUA_FUNCTION_DEF (mimepart, is_message);
  438. /***
  439. * @method mime_part:get_boundary()
  440. * Returns boundary for a part (extracted from parent multipart for normal parts and
  441. * from the part itself for multipart)
  442. * @return {string} boundary value or nil
  443. */
  444. LUA_FUNCTION_DEF (mimepart, get_boundary);
  445. /***
  446. * @method mime_part:get_enclosing_boundary()
  447. * Returns an enclosing boundary for a part even for multiparts. For normal parts
  448. * this method is identical to `get_boundary`
  449. * @return {string} boundary value or nil
  450. */
  451. LUA_FUNCTION_DEF (mimepart, get_enclosing_boundary);
  452. /***
  453. * @method mime_part:get_children()
  454. * Returns rspamd_mimepart table of part's childer. Returns nil if mime part is not multipart
  455. * or a message part.
  456. * @return {rspamd_mimepart} table of children
  457. */
  458. LUA_FUNCTION_DEF (mimepart, get_children);
  459. /***
  460. * @method mime_part:is_text()
  461. * Returns true if mime part is a text part
  462. * @return {bool} true if a part is a text part
  463. */
  464. LUA_FUNCTION_DEF (mimepart, is_text);
  465. /***
  466. * @method mime_part:get_text()
  467. * Returns rspamd_textpart structure associated with this part.
  468. * @return {rspamd_textpart} textpart structure or nil if a part is not an text
  469. */
  470. LUA_FUNCTION_DEF (mimepart, get_text);
  471. /***
  472. * @method mime_part:get_digest()
  473. * Returns the unique digest for this mime part
  474. * @return {string} 128 characters hex string with digest of the part
  475. */
  476. LUA_FUNCTION_DEF (mimepart, get_digest);
  477. /***
  478. * @method mime_part:get_id()
  479. * Returns the order of the part in parts list
  480. * @return {number} index of the part (starting from 1 as it is Lua API)
  481. */
  482. LUA_FUNCTION_DEF (mimepart, get_id);
  483. /***
  484. * @method mime_part:is_broken()
  485. * Returns true if mime part has incorrectly specified content type
  486. * @return {bool} true if a part has bad content type
  487. */
  488. LUA_FUNCTION_DEF (mimepart, is_broken);
  489. /***
  490. * @method mime_part:headers_foreach(callback, [params])
  491. * This method calls `callback` for each header that satisfies some condition.
  492. * By default, all headers are iterated unless `callback` returns `true`. Nil or
  493. * false means continue of iterations.
  494. * Params could be as following:
  495. *
  496. * - `full`: header value is full table of all attributes @see task:get_header_full for details
  497. * - `regexp`: return headers that satisfies the specified regexp
  498. * @param {function} callback function from header name and header value
  499. * @param {table} params optional parameters
  500. */
  501. LUA_FUNCTION_DEF (mimepart, headers_foreach);
  502. /***
  503. * @method mime_part:get_parent()
  504. * Returns parent part for this part
  505. * @return {rspamd_mimepart} parent part or nil
  506. */
  507. LUA_FUNCTION_DEF (mimepart, get_parent);
  508. /***
  509. * @method mime_part:get_specific()
  510. * Returns specific lua content for this part
  511. * @return {any} specific lua content
  512. */
  513. LUA_FUNCTION_DEF (mimepart, get_specific);
  514. /***
  515. * @method mime_part:set_specific(<any>)
  516. * Sets a specific content for this part
  517. * @return {any} previous specific lua content (or nil)
  518. */
  519. LUA_FUNCTION_DEF (mimepart, set_specific);
  520. /***
  521. * @method mime_part:is_specific(<any>)
  522. * Returns true if part has specific lua content
  523. * @return {boolean} flag
  524. */
  525. LUA_FUNCTION_DEF (mimepart, is_specific);
  526. /***
  527. * @method mime_part:get_urls([need_emails|list_protos][, need_images])
  528. * Get all URLs found in a mime part. Telephone urls and emails are not included unless explicitly asked in `list_protos`
  529. * @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`)
  530. * @param {boolean} need_images return urls from images (<img src=...>) as well
  531. * @return {table rspamd_url} list of all urls found
  532. */
  533. LUA_FUNCTION_DEF (mimepart, get_urls);
  534. static const struct luaL_reg mimepartlib_m[] = {
  535. LUA_INTERFACE_DEF (mimepart, get_content),
  536. LUA_INTERFACE_DEF (mimepart, get_raw_content),
  537. LUA_INTERFACE_DEF (mimepart, get_length),
  538. LUA_INTERFACE_DEF (mimepart, get_type),
  539. LUA_INTERFACE_DEF (mimepart, get_type_full),
  540. LUA_INTERFACE_DEF (mimepart, get_detected_type),
  541. LUA_INTERFACE_DEF (mimepart, get_detected_ext),
  542. LUA_INTERFACE_DEF (mimepart, get_detected_type_full),
  543. LUA_INTERFACE_DEF (mimepart, get_cte),
  544. LUA_INTERFACE_DEF (mimepart, get_filename),
  545. LUA_INTERFACE_DEF (mimepart, get_boundary),
  546. LUA_INTERFACE_DEF (mimepart, get_enclosing_boundary),
  547. LUA_INTERFACE_DEF (mimepart, get_header),
  548. LUA_INTERFACE_DEF (mimepart, get_header_raw),
  549. LUA_INTERFACE_DEF (mimepart, get_header_full),
  550. LUA_INTERFACE_DEF (mimepart, get_header_count),
  551. LUA_INTERFACE_DEF (mimepart, get_raw_headers),
  552. LUA_INTERFACE_DEF (mimepart, get_headers),
  553. LUA_INTERFACE_DEF (mimepart, is_image),
  554. LUA_INTERFACE_DEF (mimepart, get_image),
  555. LUA_INTERFACE_DEF (mimepart, is_archive),
  556. LUA_INTERFACE_DEF (mimepart, get_archive),
  557. LUA_INTERFACE_DEF (mimepart, is_multipart),
  558. LUA_INTERFACE_DEF (mimepart, is_message),
  559. LUA_INTERFACE_DEF (mimepart, get_children),
  560. LUA_INTERFACE_DEF (mimepart, get_parent),
  561. LUA_INTERFACE_DEF (mimepart, get_urls),
  562. LUA_INTERFACE_DEF (mimepart, is_text),
  563. LUA_INTERFACE_DEF (mimepart, is_broken),
  564. LUA_INTERFACE_DEF (mimepart, is_attachment),
  565. LUA_INTERFACE_DEF (mimepart, get_text),
  566. LUA_INTERFACE_DEF (mimepart, get_digest),
  567. LUA_INTERFACE_DEF (mimepart, get_id),
  568. LUA_INTERFACE_DEF (mimepart, headers_foreach),
  569. LUA_INTERFACE_DEF (mimepart, get_specific),
  570. LUA_INTERFACE_DEF (mimepart, set_specific),
  571. LUA_INTERFACE_DEF (mimepart, is_specific),
  572. {"__tostring", rspamd_lua_class_tostring},
  573. {NULL, NULL}
  574. };
  575. static struct rspamd_mime_text_part *
  576. lua_check_textpart (lua_State * L)
  577. {
  578. void *ud = rspamd_lua_check_udata (L, 1, "rspamd{textpart}");
  579. luaL_argcheck (L, ud != NULL, 1, "'textpart' expected");
  580. return ud ? *((struct rspamd_mime_text_part **)ud) : NULL;
  581. }
  582. static struct rspamd_mime_part *
  583. lua_check_mimepart (lua_State * L)
  584. {
  585. void *ud = rspamd_lua_check_udata (L, 1, "rspamd{mimepart}");
  586. luaL_argcheck (L, ud != NULL, 1, "'mimepart' expected");
  587. return ud ? *((struct rspamd_mime_part **)ud) : NULL;
  588. }
  589. static gint
  590. lua_textpart_is_utf (lua_State * L)
  591. {
  592. LUA_TRACE_POINT;
  593. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  594. if (part == NULL || IS_TEXT_PART_EMPTY (part)) {
  595. lua_pushboolean (L, FALSE);
  596. return 1;
  597. }
  598. lua_pushboolean (L, IS_TEXT_PART_UTF (part));
  599. return 1;
  600. }
  601. static gint
  602. lua_textpart_has_8bit_raw (lua_State * L)
  603. {
  604. LUA_TRACE_POINT;
  605. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  606. if (part) {
  607. if (part->flags & RSPAMD_MIME_TEXT_PART_FLAG_8BIT_RAW) {
  608. lua_pushboolean (L, TRUE);
  609. }
  610. else {
  611. lua_pushboolean (L, FALSE);
  612. }
  613. }
  614. else {
  615. return luaL_error (L, "invalid arguments");
  616. }
  617. return 1;
  618. }
  619. static gint
  620. lua_textpart_has_8bit (lua_State * L)
  621. {
  622. LUA_TRACE_POINT;
  623. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  624. if (part) {
  625. if (part->flags & RSPAMD_MIME_TEXT_PART_FLAG_8BIT_ENCODED) {
  626. lua_pushboolean (L, TRUE);
  627. }
  628. else {
  629. lua_pushboolean (L, FALSE);
  630. }
  631. }
  632. else {
  633. return luaL_error (L, "invalid arguments");
  634. }
  635. return 1;
  636. }
  637. static gint
  638. lua_textpart_get_content (lua_State * L)
  639. {
  640. LUA_TRACE_POINT;
  641. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  642. struct rspamd_lua_text *t;
  643. gsize len;
  644. const gchar *start, *type = NULL;
  645. if (part == NULL) {
  646. lua_pushnil (L);
  647. return 1;
  648. }
  649. if (lua_type (L, 2) == LUA_TSTRING) {
  650. type = lua_tostring (L, 2);
  651. }
  652. if (!type) {
  653. if (IS_TEXT_PART_EMPTY (part)) {
  654. lua_pushnil (L);
  655. return 1;
  656. }
  657. start = part->utf_content.begin;
  658. len = part->utf_content.len;
  659. }
  660. else if (strcmp (type, "content") == 0) {
  661. if (IS_TEXT_PART_EMPTY (part)) {
  662. lua_pushnil (L);
  663. return 1;
  664. }
  665. start = part->utf_content.begin;
  666. len = part->utf_content.len;
  667. }
  668. else if (strcmp (type, "content_oneline") == 0) {
  669. if (IS_TEXT_PART_EMPTY (part)) {
  670. lua_pushnil (L);
  671. return 1;
  672. }
  673. start = part->utf_stripped_content->data;
  674. len = part->utf_stripped_content->len;
  675. }
  676. else if (strcmp (type, "raw_parsed") == 0) {
  677. if (part->parsed.len == 0) {
  678. lua_pushnil (L);
  679. return 1;
  680. }
  681. start = part->parsed.begin;
  682. len = part->parsed.len;
  683. }
  684. else if (strcmp (type, "raw_utf") == 0) {
  685. if (part->utf_raw_content == NULL || part->utf_raw_content->len == 0) {
  686. lua_pushnil (L);
  687. return 1;
  688. }
  689. start = part->utf_raw_content->data;
  690. len = part->utf_raw_content->len;
  691. }
  692. else if (strcmp (type, "raw") == 0) {
  693. if (part->raw.len == 0) {
  694. lua_pushnil (L);
  695. return 1;
  696. }
  697. start = part->raw.begin;
  698. len = part->raw.len;
  699. }
  700. else {
  701. return luaL_error (L, "invalid content type: %s", type);
  702. }
  703. t = lua_newuserdata (L, sizeof (*t));
  704. rspamd_lua_setclass (L, "rspamd{text}", -1);
  705. t->start = start;
  706. t->len = len;
  707. t->flags = 0;
  708. return 1;
  709. }
  710. static gint
  711. lua_textpart_get_raw_content (lua_State * L)
  712. {
  713. LUA_TRACE_POINT;
  714. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  715. struct rspamd_lua_text *t;
  716. if (part == NULL || IS_TEXT_PART_EMPTY (part)) {
  717. lua_pushnil (L);
  718. return 1;
  719. }
  720. t = lua_newuserdata (L, sizeof (*t));
  721. rspamd_lua_setclass (L, "rspamd{text}", -1);
  722. t->start = part->raw.begin;
  723. t->len = part->raw.len;
  724. t->flags = 0;
  725. return 1;
  726. }
  727. static gint
  728. lua_textpart_get_content_oneline (lua_State * L)
  729. {
  730. LUA_TRACE_POINT;
  731. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  732. struct rspamd_lua_text *t;
  733. if (part == NULL || IS_TEXT_PART_EMPTY (part)) {
  734. lua_pushnil (L);
  735. return 1;
  736. }
  737. t = lua_newuserdata (L, sizeof (*t));
  738. rspamd_lua_setclass (L, "rspamd{text}", -1);
  739. t->start = part->utf_stripped_content->data;
  740. t->len = part->utf_stripped_content->len;
  741. t->flags = 0;
  742. return 1;
  743. }
  744. static gint
  745. lua_textpart_get_length (lua_State * L)
  746. {
  747. LUA_TRACE_POINT;
  748. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  749. if (part == NULL) {
  750. lua_pushnil (L);
  751. return 1;
  752. }
  753. if (IS_TEXT_PART_EMPTY (part) || part->utf_content.len == 0) {
  754. lua_pushinteger (L, 0);
  755. }
  756. else {
  757. lua_pushinteger (L, part->utf_content.len);
  758. }
  759. return 1;
  760. }
  761. static gint
  762. lua_textpart_get_raw_length (lua_State * L)
  763. {
  764. LUA_TRACE_POINT;
  765. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  766. if (part == NULL) {
  767. lua_pushnil (L);
  768. return 1;
  769. }
  770. lua_pushinteger (L, part->raw.len);
  771. return 1;
  772. }
  773. static gint
  774. lua_textpart_get_urls_length (lua_State * L)
  775. {
  776. LUA_TRACE_POINT;
  777. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  778. GList *cur;
  779. guint total = 0;
  780. struct rspamd_process_exception *ex;
  781. if (part == NULL) {
  782. lua_pushnil (L);
  783. return 1;
  784. }
  785. for (cur = part->exceptions; cur != NULL; cur = g_list_next (cur)) {
  786. ex = cur->data;
  787. if (ex->type == RSPAMD_EXCEPTION_URL) {
  788. total += ex->len;
  789. }
  790. }
  791. lua_pushinteger (L, total);
  792. return 1;
  793. }
  794. static gint
  795. lua_textpart_get_lines_count (lua_State * L)
  796. {
  797. LUA_TRACE_POINT;
  798. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  799. if (part == NULL) {
  800. lua_pushnil (L);
  801. return 1;
  802. }
  803. if (IS_TEXT_PART_EMPTY (part)) {
  804. lua_pushinteger (L, 0);
  805. }
  806. else {
  807. lua_pushinteger (L, part->nlines);
  808. }
  809. return 1;
  810. }
  811. static gint
  812. lua_textpart_get_words_count (lua_State *L)
  813. {
  814. LUA_TRACE_POINT;
  815. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  816. if (part == NULL) {
  817. lua_pushnil (L);
  818. return 1;
  819. }
  820. if (IS_TEXT_PART_EMPTY (part) || part->utf_words == NULL) {
  821. lua_pushinteger (L, 0);
  822. }
  823. else {
  824. lua_pushinteger (L, part->nwords);
  825. }
  826. return 1;
  827. }
  828. static inline enum rspamd_lua_words_type
  829. word_extract_type_from_string (const gchar *how_str)
  830. {
  831. enum rspamd_lua_words_type how = RSPAMD_LUA_WORDS_MAX;
  832. if (strcmp (how_str, "stem") == 0) {
  833. how = RSPAMD_LUA_WORDS_STEM;
  834. }
  835. else if (strcmp (how_str, "norm") == 0) {
  836. how = RSPAMD_LUA_WORDS_NORM;
  837. }
  838. else if (strcmp (how_str, "raw") == 0) {
  839. how = RSPAMD_LUA_WORDS_RAW;
  840. }
  841. else if (strcmp (how_str, "full") == 0) {
  842. how = RSPAMD_LUA_WORDS_FULL;
  843. }
  844. return how;
  845. }
  846. static gint
  847. lua_textpart_get_words (lua_State *L)
  848. {
  849. LUA_TRACE_POINT;
  850. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  851. enum rspamd_lua_words_type how = RSPAMD_LUA_WORDS_STEM;
  852. if (part == NULL) {
  853. return luaL_error (L, "invalid arguments");
  854. }
  855. if (IS_TEXT_PART_EMPTY (part) || part->utf_words == NULL) {
  856. lua_createtable (L, 0, 0);
  857. }
  858. else {
  859. if (lua_type (L, 2) == LUA_TSTRING) {
  860. const gchar *how_str = lua_tostring (L, 2);
  861. how = word_extract_type_from_string (how_str);
  862. if (how == RSPAMD_LUA_WORDS_MAX) {
  863. return luaL_error (L, "invalid extraction type: %s", how_str);
  864. }
  865. }
  866. return rspamd_lua_push_words (L, part->utf_words, how);
  867. }
  868. return 1;
  869. }
  870. static gint
  871. lua_textpart_filter_words (lua_State *L)
  872. {
  873. LUA_TRACE_POINT;
  874. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  875. struct rspamd_lua_regexp *re = lua_check_regexp (L, 2);
  876. gint lim = -1;
  877. enum rspamd_lua_words_type how = RSPAMD_LUA_WORDS_STEM;
  878. if (part == NULL || re == NULL) {
  879. return luaL_error (L, "invalid arguments");
  880. }
  881. if (IS_TEXT_PART_EMPTY (part) || part->utf_words == NULL) {
  882. lua_createtable (L, 0, 0);
  883. }
  884. else {
  885. if (lua_type (L, 3) == LUA_TSTRING) {
  886. const gchar *how_str = lua_tostring (L, 3);
  887. how = word_extract_type_from_string (how_str);
  888. if (how == RSPAMD_LUA_WORDS_MAX) {
  889. return luaL_error (L, "invalid extraction type: %s", how_str);
  890. }
  891. }
  892. if (lua_type (L, 4) == LUA_TNUMBER) {
  893. lim = lua_tointeger (L, 4);
  894. }
  895. guint cnt, i;
  896. lua_createtable (L, 8, 0);
  897. for (i = 0, cnt = 1; i < part->utf_words->len; i ++) {
  898. rspamd_stat_token_t *w = &g_array_index (part->utf_words,
  899. rspamd_stat_token_t, i);
  900. switch (how) {
  901. case RSPAMD_LUA_WORDS_STEM:
  902. if (w->stemmed.len > 0) {
  903. if (rspamd_regexp_match (re->re, w->stemmed.begin,
  904. w->stemmed.len, FALSE)) {
  905. lua_pushlstring (L, w->stemmed.begin, w->stemmed.len);
  906. lua_rawseti (L, -2, cnt++);
  907. }
  908. }
  909. break;
  910. case RSPAMD_LUA_WORDS_NORM:
  911. if (w->normalized.len > 0) {
  912. if (rspamd_regexp_match (re->re, w->normalized.begin,
  913. w->normalized.len, FALSE)) {
  914. lua_pushlstring (L, w->normalized.begin, w->normalized.len);
  915. lua_rawseti (L, -2, cnt++);
  916. }
  917. }
  918. break;
  919. case RSPAMD_LUA_WORDS_RAW:
  920. if (w->original.len > 0) {
  921. if (rspamd_regexp_match (re->re, w->original.begin,
  922. w->original.len, TRUE)) {
  923. lua_pushlstring (L, w->original.begin, w->original.len);
  924. lua_rawseti (L, -2, cnt++);
  925. }
  926. }
  927. break;
  928. case RSPAMD_LUA_WORDS_FULL:
  929. if (rspamd_regexp_match (re->re, w->normalized.begin,
  930. w->normalized.len, FALSE)) {
  931. rspamd_lua_push_full_word (L, w);
  932. /* Push to the resulting vector */
  933. lua_rawseti (L, -2, cnt++);
  934. }
  935. break;
  936. default:
  937. break;
  938. }
  939. if (lim > 0 && cnt >= lim) {
  940. break;
  941. }
  942. }
  943. }
  944. return 1;
  945. }
  946. static gint
  947. lua_textpart_is_empty (lua_State * L)
  948. {
  949. LUA_TRACE_POINT;
  950. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  951. if (part == NULL) {
  952. lua_pushnil (L);
  953. return 1;
  954. }
  955. lua_pushboolean (L, IS_TEXT_PART_EMPTY (part));
  956. return 1;
  957. }
  958. static gint
  959. lua_textpart_is_html (lua_State * L)
  960. {
  961. LUA_TRACE_POINT;
  962. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  963. if (part == NULL) {
  964. lua_pushnil (L);
  965. return 1;
  966. }
  967. lua_pushboolean (L, IS_TEXT_PART_HTML (part));
  968. return 1;
  969. }
  970. static gint
  971. lua_textpart_get_html (lua_State * L)
  972. {
  973. LUA_TRACE_POINT;
  974. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  975. struct html_content **phc;
  976. if (part == NULL || part->html == NULL) {
  977. lua_pushnil (L);
  978. }
  979. else {
  980. phc = lua_newuserdata (L, sizeof (*phc));
  981. rspamd_lua_setclass (L, "rspamd{html}", -1);
  982. *phc = part->html;
  983. }
  984. return 1;
  985. }
  986. static gint
  987. lua_textpart_get_language (lua_State * L)
  988. {
  989. LUA_TRACE_POINT;
  990. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  991. if (part != NULL) {
  992. if (part->language != NULL && part->language[0] != '\0') {
  993. lua_pushstring (L, part->language);
  994. return 1;
  995. }
  996. else {
  997. lua_pushnil (L);
  998. }
  999. }
  1000. else {
  1001. return luaL_error (L, "invalid arguments");
  1002. }
  1003. return 1;
  1004. }
  1005. static gint
  1006. lua_textpart_get_charset (lua_State * L)
  1007. {
  1008. LUA_TRACE_POINT;
  1009. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  1010. if (part != NULL) {
  1011. if (part->real_charset != NULL) {
  1012. lua_pushstring (L, part->real_charset);
  1013. return 1;
  1014. }
  1015. else {
  1016. lua_pushnil (L);
  1017. }
  1018. }
  1019. else {
  1020. return luaL_error (L, "invalid arguments");
  1021. }
  1022. return 1;
  1023. }
  1024. static gint
  1025. lua_textpart_get_languages (lua_State * L)
  1026. {
  1027. LUA_TRACE_POINT;
  1028. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  1029. guint i;
  1030. struct rspamd_lang_detector_res *cur;
  1031. if (part != NULL) {
  1032. if (part->languages != NULL) {
  1033. lua_createtable (L, part->languages->len, 0);
  1034. PTR_ARRAY_FOREACH (part->languages, i, cur) {
  1035. lua_createtable (L, 0, 2);
  1036. lua_pushstring (L, "code");
  1037. lua_pushstring (L, cur->lang);
  1038. lua_settable (L, -3);
  1039. lua_pushstring (L, "prob");
  1040. lua_pushnumber (L, cur->prob);
  1041. lua_settable (L, -3);
  1042. lua_rawseti (L, -2, i + 1);
  1043. }
  1044. }
  1045. else {
  1046. lua_newtable (L);
  1047. }
  1048. }
  1049. else {
  1050. luaL_error (L, "invalid arguments");
  1051. }
  1052. return 1;
  1053. }
  1054. struct lua_shingle_data {
  1055. guint64 hash;
  1056. rspamd_ftok_t t1;
  1057. rspamd_ftok_t t2;
  1058. rspamd_ftok_t t3;
  1059. };
  1060. struct lua_shingle_filter_cbdata {
  1061. struct rspamd_mime_text_part *part;
  1062. rspamd_mempool_t *pool;
  1063. };
  1064. #define STORE_TOKEN(i, t) do { \
  1065. if ((i) < part->utf_words->len) { \
  1066. word = &g_array_index (part->utf_words, rspamd_stat_token_t, (i)); \
  1067. sd->t.begin = word->stemmed.begin; \
  1068. sd->t.len = word->stemmed.len; \
  1069. } \
  1070. }while (0)
  1071. static guint64
  1072. lua_shingles_filter (guint64 *input, gsize count,
  1073. gint shno, const guchar *key, gpointer ud)
  1074. {
  1075. guint64 minimal = G_MAXUINT64;
  1076. gsize i, min_idx = 0;
  1077. struct lua_shingle_data *sd;
  1078. rspamd_stat_token_t *word;
  1079. struct lua_shingle_filter_cbdata *cbd = (struct lua_shingle_filter_cbdata *)ud;
  1080. struct rspamd_mime_text_part *part;
  1081. part = cbd->part;
  1082. for (i = 0; i < count; i ++) {
  1083. if (minimal > input[i]) {
  1084. minimal = input[i];
  1085. min_idx = i;
  1086. }
  1087. }
  1088. sd = rspamd_mempool_alloc0 (cbd->pool, sizeof (*sd));
  1089. sd->hash = minimal;
  1090. STORE_TOKEN (min_idx, t1);
  1091. STORE_TOKEN (min_idx + 1, t2);
  1092. STORE_TOKEN (min_idx + 2, t3);
  1093. return GPOINTER_TO_SIZE (sd);
  1094. }
  1095. #undef STORE_TOKEN
  1096. static gint
  1097. lua_textpart_get_fuzzy_hashes (lua_State * L)
  1098. {
  1099. LUA_TRACE_POINT;
  1100. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  1101. rspamd_mempool_t *pool = rspamd_lua_check_mempool (L, 2);
  1102. guchar key[rspamd_cryptobox_HASHBYTES], digest[rspamd_cryptobox_HASHBYTES],
  1103. hexdigest[rspamd_cryptobox_HASHBYTES * 2 + 1], numbuf[64];
  1104. struct rspamd_shingle *sgl;
  1105. guint i;
  1106. struct lua_shingle_data *sd;
  1107. rspamd_cryptobox_hash_state_t st;
  1108. rspamd_stat_token_t *word;
  1109. struct lua_shingle_filter_cbdata cbd;
  1110. if (part == NULL || pool == NULL) {
  1111. return luaL_error (L, "invalid arguments");
  1112. }
  1113. if (IS_TEXT_PART_EMPTY (part) || part->utf_words == NULL) {
  1114. lua_pushnil (L);
  1115. lua_pushnil (L);
  1116. }
  1117. else {
  1118. /* TODO: add keys and algorithms support */
  1119. rspamd_cryptobox_hash (key, "rspamd", strlen ("rspamd"), NULL, 0);
  1120. /* TODO: add short text support */
  1121. /* Calculate direct hash */
  1122. rspamd_cryptobox_hash_init (&st, key, rspamd_cryptobox_HASHKEYBYTES);
  1123. for (i = 0; i < part->utf_words->len; i ++) {
  1124. word = &g_array_index (part->utf_words, rspamd_stat_token_t, i);
  1125. rspamd_cryptobox_hash_update (&st,
  1126. word->stemmed.begin, word->stemmed.len);
  1127. }
  1128. rspamd_cryptobox_hash_final (&st, digest);
  1129. rspamd_encode_hex_buf (digest, sizeof (digest), hexdigest,
  1130. sizeof (hexdigest));
  1131. lua_pushlstring (L, hexdigest, sizeof (hexdigest) - 1);
  1132. cbd.pool = pool;
  1133. cbd.part = part;
  1134. sgl = rspamd_shingles_from_text (part->utf_words, key,
  1135. pool, lua_shingles_filter, &cbd, RSPAMD_SHINGLES_MUMHASH);
  1136. if (sgl == NULL) {
  1137. lua_pushnil (L);
  1138. }
  1139. else {
  1140. lua_createtable (L, G_N_ELEMENTS (sgl->hashes), 0);
  1141. for (i = 0; i < G_N_ELEMENTS (sgl->hashes); i ++) {
  1142. sd = GSIZE_TO_POINTER (sgl->hashes[i]);
  1143. lua_createtable (L, 4, 0);
  1144. rspamd_snprintf (numbuf, sizeof (numbuf), "%uL", sd->hash);
  1145. lua_pushstring (L, numbuf);
  1146. lua_rawseti (L, -2, 1);
  1147. /* Tokens */
  1148. lua_pushlstring (L, sd->t1.begin, sd->t1.len);
  1149. lua_rawseti (L, -2, 2);
  1150. lua_pushlstring (L, sd->t2.begin, sd->t2.len);
  1151. lua_rawseti (L, -2, 3);
  1152. lua_pushlstring (L, sd->t3.begin, sd->t3.len);
  1153. lua_rawseti (L, -2, 4);
  1154. lua_rawseti (L, -2, i + 1); /* Store table */
  1155. }
  1156. }
  1157. }
  1158. return 2;
  1159. }
  1160. static gint
  1161. lua_textpart_get_mimepart (lua_State * L)
  1162. {
  1163. LUA_TRACE_POINT;
  1164. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  1165. struct rspamd_mime_part **pmime;
  1166. if (part != NULL) {
  1167. if (part->mime_part != NULL) {
  1168. pmime = lua_newuserdata (L, sizeof (struct rspamd_mime_part *));
  1169. rspamd_lua_setclass (L, "rspamd{mimepart}", -1);
  1170. *pmime = part->mime_part;
  1171. return 1;
  1172. }
  1173. }
  1174. lua_pushnil (L);
  1175. return 1;
  1176. }
  1177. /***
  1178. * @method mime_part:get_stats()
  1179. * Returns a table with the following data:
  1180. * -
  1181. * - `lines`: number of lines
  1182. * - `spaces`: number of spaces
  1183. * - `double_spaces`: double spaces
  1184. * - `empty_lines`: number of empty lines
  1185. * - `non_ascii_characters`: number of non ascii characters
  1186. * - `ascii_characters`: number of ascii characters
  1187. * @return {table} table of stats
  1188. */
  1189. static gint
  1190. lua_textpart_get_stats (lua_State * L)
  1191. {
  1192. LUA_TRACE_POINT;
  1193. struct rspamd_mime_text_part *part = lua_check_textpart (L);
  1194. if (part != NULL) {
  1195. lua_createtable (L, 0, 9);
  1196. lua_pushstring (L, "lines");
  1197. lua_pushinteger (L, part->nlines);
  1198. lua_settable (L, -3);
  1199. lua_pushstring (L, "empty_lines");
  1200. lua_pushinteger (L, part->empty_lines);
  1201. lua_settable (L, -3);
  1202. lua_pushstring (L, "spaces");
  1203. lua_pushinteger (L, part->spaces);
  1204. lua_settable (L, -3);
  1205. lua_pushstring (L, "non_spaces");
  1206. lua_pushinteger (L, part->non_spaces);
  1207. lua_settable (L, -3);
  1208. lua_pushstring (L, "double_spaces");
  1209. lua_pushinteger (L, part->double_spaces);
  1210. lua_settable (L, -3);
  1211. lua_pushstring (L, "ascii_characters");
  1212. lua_pushinteger (L, part->ascii_chars);
  1213. lua_settable (L, -3);
  1214. lua_pushstring (L, "non_ascii_characters");
  1215. lua_pushinteger (L, part->non_ascii_chars);
  1216. lua_settable (L, -3);
  1217. lua_pushstring (L, "capital_letters");
  1218. lua_pushinteger (L, part->capital_letters);
  1219. lua_settable (L, -3);
  1220. lua_pushstring (L, "numeric_characters");
  1221. lua_pushinteger (L, part->numeric_characters);
  1222. lua_settable (L, -3);
  1223. }
  1224. else {
  1225. return luaL_error (L, "invalid arguments");
  1226. }
  1227. return 1;
  1228. }
  1229. /* Mimepart implementation */
  1230. static gint
  1231. lua_mimepart_get_content (lua_State * L)
  1232. {
  1233. LUA_TRACE_POINT;
  1234. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1235. struct rspamd_lua_text *t;
  1236. if (part == NULL) {
  1237. lua_pushnil (L);
  1238. return 1;
  1239. }
  1240. t = lua_newuserdata (L, sizeof (*t));
  1241. rspamd_lua_setclass (L, "rspamd{text}", -1);
  1242. t->start = part->parsed_data.begin;
  1243. t->len = part->parsed_data.len;
  1244. t->flags = 0;
  1245. return 1;
  1246. }
  1247. static gint
  1248. lua_mimepart_get_raw_content (lua_State * L)
  1249. {
  1250. LUA_TRACE_POINT;
  1251. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1252. struct rspamd_lua_text *t;
  1253. if (part == NULL) {
  1254. lua_pushnil (L);
  1255. return 1;
  1256. }
  1257. t = lua_newuserdata (L, sizeof (*t));
  1258. rspamd_lua_setclass (L, "rspamd{text}", -1);
  1259. t->start = part->raw_data.begin;
  1260. t->len = part->raw_data.len;
  1261. t->flags = 0;
  1262. return 1;
  1263. }
  1264. static gint
  1265. lua_mimepart_get_length (lua_State * L)
  1266. {
  1267. LUA_TRACE_POINT;
  1268. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1269. if (part == NULL) {
  1270. lua_pushnil (L);
  1271. return 1;
  1272. }
  1273. lua_pushinteger (L, part->parsed_data.len);
  1274. return 1;
  1275. }
  1276. static gint
  1277. lua_mimepart_get_type_common (lua_State * L, struct rspamd_content_type *ct,
  1278. gboolean full)
  1279. {
  1280. GHashTableIter it;
  1281. gpointer k, v;
  1282. struct rspamd_content_type_param *param;
  1283. if (ct == NULL) {
  1284. lua_pushnil (L);
  1285. lua_pushnil (L);
  1286. return 2;
  1287. }
  1288. lua_pushlstring (L, ct->type.begin, ct->type.len);
  1289. lua_pushlstring (L, ct->subtype.begin, ct->subtype.len);
  1290. if (!full) {
  1291. return 2;
  1292. }
  1293. lua_createtable (L, 0, 2 + (ct->attrs ?
  1294. g_hash_table_size (ct->attrs) : 0));
  1295. if (ct->charset.len > 0) {
  1296. lua_pushstring (L, "charset");
  1297. lua_pushlstring (L, ct->charset.begin, ct->charset.len);
  1298. lua_settable (L, -3);
  1299. }
  1300. if (ct->boundary.len > 0) {
  1301. lua_pushstring (L, "boundary");
  1302. lua_pushlstring (L, ct->boundary.begin, ct->boundary.len);
  1303. lua_settable (L, -3);
  1304. }
  1305. if (ct->attrs) {
  1306. g_hash_table_iter_init (&it, ct->attrs);
  1307. while (g_hash_table_iter_next (&it, &k, &v)) {
  1308. param = v;
  1309. if (param->name.len > 0 && param->value.len > 0) {
  1310. /* TODO: think about multiple values here */
  1311. lua_pushlstring (L, param->name.begin, param->name.len);
  1312. lua_pushlstring (L, param->value.begin, param->value.len);
  1313. lua_settable (L, -3);
  1314. }
  1315. }
  1316. }
  1317. return 3;
  1318. }
  1319. static gint
  1320. lua_mimepart_get_type (lua_State * L)
  1321. {
  1322. LUA_TRACE_POINT;
  1323. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1324. if (part == NULL) {
  1325. return luaL_error (L, "invalid arguments");
  1326. }
  1327. return lua_mimepart_get_type_common (L, part->ct, FALSE);
  1328. }
  1329. static gint
  1330. lua_mimepart_get_type_full (lua_State * L)
  1331. {
  1332. LUA_TRACE_POINT;
  1333. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1334. if (part == NULL) {
  1335. return luaL_error (L, "invalid arguments");
  1336. }
  1337. return lua_mimepart_get_type_common (L, part->ct, TRUE);
  1338. }
  1339. static gint
  1340. lua_mimepart_get_detected_type (lua_State * L)
  1341. {
  1342. LUA_TRACE_POINT;
  1343. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1344. if (part == NULL) {
  1345. return luaL_error (L, "invalid arguments");
  1346. }
  1347. return lua_mimepart_get_type_common (L, part->detected_ct, FALSE);
  1348. }
  1349. static gint
  1350. lua_mimepart_get_detected_type_full (lua_State * L)
  1351. {
  1352. LUA_TRACE_POINT;
  1353. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1354. if (part == NULL) {
  1355. return luaL_error (L, "invalid arguments");
  1356. }
  1357. return lua_mimepart_get_type_common (L, part->detected_ct, TRUE);
  1358. }
  1359. static gint
  1360. lua_mimepart_get_detected_ext (lua_State * L)
  1361. {
  1362. LUA_TRACE_POINT;
  1363. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1364. if (part == NULL) {
  1365. return luaL_error (L, "invalid arguments");
  1366. }
  1367. if (part->detected_ext) {
  1368. lua_pushstring (L, part->detected_ext);
  1369. }
  1370. else {
  1371. lua_pushnil (L);
  1372. }
  1373. return 1;
  1374. }
  1375. static gint
  1376. lua_mimepart_get_cte (lua_State * L)
  1377. {
  1378. LUA_TRACE_POINT;
  1379. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1380. if (part == NULL) {
  1381. lua_pushnil (L);
  1382. return 1;
  1383. }
  1384. lua_pushstring (L, rspamd_cte_to_string (part->cte));
  1385. return 1;
  1386. }
  1387. static gint
  1388. lua_mimepart_get_filename (lua_State * L)
  1389. {
  1390. LUA_TRACE_POINT;
  1391. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1392. if (part == NULL || part->cd == NULL || part->cd->filename.len == 0) {
  1393. lua_pushnil (L);
  1394. return 1;
  1395. }
  1396. lua_pushlstring (L, part->cd->filename.begin, part->cd->filename.len);
  1397. return 1;
  1398. }
  1399. static gint
  1400. lua_mimepart_get_boundary (lua_State * L)
  1401. {
  1402. LUA_TRACE_POINT;
  1403. struct rspamd_mime_part *part = lua_check_mimepart (L), *parent;
  1404. if (part == NULL) {
  1405. return luaL_error (L, "invalid arguments");
  1406. }
  1407. if (IS_PART_MULTIPART (part)) {
  1408. lua_pushlstring (L, part->specific.mp->boundary.begin,
  1409. part->specific.mp->boundary.len);
  1410. }
  1411. else {
  1412. parent = part->parent_part;
  1413. if (!parent || !IS_PART_MULTIPART (parent)) {
  1414. lua_pushnil (L);
  1415. }
  1416. else {
  1417. lua_pushlstring (L, parent->specific.mp->boundary.begin,
  1418. parent->specific.mp->boundary.len);
  1419. }
  1420. }
  1421. return 1;
  1422. }
  1423. static gint
  1424. lua_mimepart_get_enclosing_boundary (lua_State * L)
  1425. {
  1426. LUA_TRACE_POINT;
  1427. struct rspamd_mime_part *part = lua_check_mimepart (L), *parent;
  1428. if (part == NULL) {
  1429. return luaL_error (L, "invalid arguments");
  1430. }
  1431. parent = part->parent_part;
  1432. if (!parent || !IS_PART_MULTIPART (parent)) {
  1433. lua_pushnil (L);
  1434. }
  1435. else {
  1436. lua_pushlstring (L, parent->specific.mp->boundary.begin,
  1437. parent->specific.mp->boundary.len);
  1438. }
  1439. return 1;
  1440. }
  1441. static gint
  1442. lua_mimepart_get_header_common (lua_State *L, enum rspamd_lua_task_header_type how)
  1443. {
  1444. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1445. const gchar *name;
  1446. gboolean strong = FALSE;
  1447. name = luaL_checkstring (L, 2);
  1448. if (name && part) {
  1449. if (lua_isboolean (L, 3)) {
  1450. strong = lua_toboolean (L, 3);
  1451. }
  1452. return rspamd_lua_push_header_array (L,
  1453. name,
  1454. rspamd_message_get_header_from_hash(part->raw_headers, name, FALSE),
  1455. how,
  1456. strong);
  1457. }
  1458. lua_pushnil (L);
  1459. return 1;
  1460. }
  1461. static gint
  1462. lua_mimepart_get_header_full (lua_State * L)
  1463. {
  1464. LUA_TRACE_POINT;
  1465. return lua_mimepart_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_FULL);
  1466. }
  1467. static gint
  1468. lua_mimepart_get_header (lua_State * L)
  1469. {
  1470. LUA_TRACE_POINT;
  1471. return lua_mimepart_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_SIMPLE);
  1472. }
  1473. static gint
  1474. lua_mimepart_get_header_raw (lua_State * L)
  1475. {
  1476. LUA_TRACE_POINT;
  1477. return lua_mimepart_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_RAW);
  1478. }
  1479. static gint
  1480. lua_mimepart_get_header_count (lua_State * L)
  1481. {
  1482. LUA_TRACE_POINT;
  1483. return lua_mimepart_get_header_common (L, RSPAMD_TASK_HEADER_PUSH_COUNT);
  1484. }
  1485. static gint
  1486. lua_mimepart_get_raw_headers (lua_State *L)
  1487. {
  1488. LUA_TRACE_POINT;
  1489. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1490. struct rspamd_lua_text *t;
  1491. if (part) {
  1492. t = lua_newuserdata (L, sizeof (*t));
  1493. rspamd_lua_setclass (L, "rspamd{text}", -1);
  1494. t->start = part->raw_headers_str;
  1495. t->len = part->raw_headers_len;
  1496. t->flags = 0;
  1497. }
  1498. else {
  1499. return luaL_error (L, "invalid arguments");
  1500. }
  1501. return 1;
  1502. }
  1503. static gint
  1504. lua_mimepart_get_headers (lua_State *L)
  1505. {
  1506. LUA_TRACE_POINT;
  1507. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1508. bool need_modified = lua_isnoneornil(L, 2) ? false : lua_toboolean(L, 2);
  1509. if (part) {
  1510. struct rspamd_mime_header *cur;
  1511. int i = 1;
  1512. lua_createtable (L, rspamd_mime_headers_count(part->raw_headers), 0);
  1513. LL_FOREACH2(part->headers_order, cur, ord_next) {
  1514. if (need_modified && cur->modified_chain) {
  1515. struct rspamd_mime_header *cur_modified;
  1516. LL_FOREACH(cur->modified_chain, cur_modified) {
  1517. rspamd_lua_push_header(L, cur_modified, RSPAMD_TASK_HEADER_PUSH_FULL);
  1518. lua_rawseti(L, -2, i++);
  1519. }
  1520. }
  1521. else {
  1522. rspamd_lua_push_header(L, cur, RSPAMD_TASK_HEADER_PUSH_FULL);
  1523. lua_rawseti(L, -2, i++);
  1524. }
  1525. }
  1526. }
  1527. else {
  1528. return luaL_error (L, "invalid arguments");
  1529. }
  1530. return 1;
  1531. }
  1532. static gint
  1533. lua_mimepart_is_image (lua_State * L)
  1534. {
  1535. LUA_TRACE_POINT;
  1536. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1537. if (part == NULL) {
  1538. return luaL_error (L, "invalid arguments");
  1539. }
  1540. lua_pushboolean (L, part->part_type == RSPAMD_MIME_PART_IMAGE);
  1541. return 1;
  1542. }
  1543. static gint
  1544. lua_mimepart_is_archive (lua_State * L)
  1545. {
  1546. LUA_TRACE_POINT;
  1547. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1548. if (part == NULL) {
  1549. return luaL_error (L, "invalid arguments");
  1550. }
  1551. lua_pushboolean (L, part->part_type == RSPAMD_MIME_PART_ARCHIVE);
  1552. return 1;
  1553. }
  1554. static gint
  1555. lua_mimepart_is_multipart (lua_State * L)
  1556. {
  1557. LUA_TRACE_POINT;
  1558. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1559. if (part == NULL) {
  1560. return luaL_error (L, "invalid arguments");
  1561. }
  1562. lua_pushboolean (L, IS_PART_MULTIPART (part) ? true : false);
  1563. return 1;
  1564. }
  1565. static gint
  1566. lua_mimepart_is_message (lua_State * L)
  1567. {
  1568. LUA_TRACE_POINT;
  1569. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1570. if (part == NULL) {
  1571. return luaL_error (L, "invalid arguments");
  1572. }
  1573. lua_pushboolean (L, IS_PART_MESSAGE (part) ? true : false);
  1574. return 1;
  1575. }
  1576. static gint
  1577. lua_mimepart_is_attachment (lua_State * L)
  1578. {
  1579. LUA_TRACE_POINT;
  1580. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1581. if (part == NULL) {
  1582. return luaL_error (L, "invalid arguments");
  1583. }
  1584. if (part->cd && part->cd->type == RSPAMD_CT_ATTACHMENT) {
  1585. lua_pushboolean (L, true);
  1586. }
  1587. else {
  1588. /* if has_name and not (image and Content-ID_header_present) */
  1589. if (part->cd && part->cd->filename.len > 0) {
  1590. if (part->part_type != RSPAMD_MIME_PART_IMAGE &&
  1591. rspamd_message_get_header_from_hash(part->raw_headers,
  1592. "Content-Id", FALSE) == NULL) {
  1593. /* Filename is presented but no content id and not image */
  1594. lua_pushboolean (L, true);
  1595. }
  1596. else {
  1597. /* Image or an embedded object */
  1598. lua_pushboolean (L, false);
  1599. }
  1600. }
  1601. else {
  1602. /* No filename */
  1603. lua_pushboolean (L, false);
  1604. }
  1605. }
  1606. return 1;
  1607. }
  1608. static gint
  1609. lua_mimepart_is_text (lua_State * L)
  1610. {
  1611. LUA_TRACE_POINT;
  1612. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1613. if (part == NULL) {
  1614. return luaL_error (L, "invalid arguments");
  1615. }
  1616. lua_pushboolean (L, part->part_type == RSPAMD_MIME_PART_TEXT);
  1617. return 1;
  1618. }
  1619. static gint
  1620. lua_mimepart_is_broken (lua_State * L)
  1621. {
  1622. LUA_TRACE_POINT;
  1623. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1624. if (part == NULL) {
  1625. return luaL_error (L, "invalid arguments");
  1626. }
  1627. if (part->ct) {
  1628. lua_pushboolean (L, (part->ct->flags & RSPAMD_CONTENT_TYPE_BROKEN) ?
  1629. true : false);
  1630. }
  1631. else {
  1632. lua_pushboolean (L, false);
  1633. }
  1634. return 1;
  1635. }
  1636. static gint
  1637. lua_mimepart_get_image (lua_State * L)
  1638. {
  1639. LUA_TRACE_POINT;
  1640. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1641. struct rspamd_image **pimg;
  1642. if (part == NULL) {
  1643. return luaL_error (L, "invalid arguments");
  1644. }
  1645. if (part->part_type != RSPAMD_MIME_PART_IMAGE || part->specific.img == NULL) {
  1646. lua_pushnil (L);
  1647. }
  1648. else {
  1649. pimg = lua_newuserdata (L, sizeof (*pimg));
  1650. *pimg = part->specific.img;
  1651. rspamd_lua_setclass (L, "rspamd{image}", -1);
  1652. }
  1653. return 1;
  1654. }
  1655. static gint
  1656. lua_mimepart_get_archive (lua_State * L)
  1657. {
  1658. LUA_TRACE_POINT;
  1659. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1660. struct rspamd_archive **parch;
  1661. if (part == NULL) {
  1662. return luaL_error (L, "invalid arguments");
  1663. }
  1664. if (part->part_type != RSPAMD_MIME_PART_ARCHIVE || part->specific.arch == NULL) {
  1665. lua_pushnil (L);
  1666. }
  1667. else {
  1668. parch = lua_newuserdata (L, sizeof (*parch));
  1669. *parch = part->specific.arch;
  1670. rspamd_lua_setclass (L, "rspamd{archive}", -1);
  1671. }
  1672. return 1;
  1673. }
  1674. static gint
  1675. lua_mimepart_get_children (lua_State * L)
  1676. {
  1677. LUA_TRACE_POINT;
  1678. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1679. struct rspamd_mime_part **pcur, *cur;
  1680. guint i;
  1681. if (part == NULL) {
  1682. return luaL_error (L, "invalid arguments");
  1683. }
  1684. if (!IS_PART_MULTIPART (part) || part->specific.mp->children == NULL) {
  1685. lua_pushnil (L);
  1686. }
  1687. else {
  1688. lua_createtable (L, part->specific.mp->children->len, 0);
  1689. PTR_ARRAY_FOREACH (part->specific.mp->children, i, cur) {
  1690. pcur = lua_newuserdata (L, sizeof (*pcur));
  1691. *pcur = cur;
  1692. rspamd_lua_setclass (L, "rspamd{mimepart}", -1);
  1693. lua_rawseti (L, -2, i + 1);
  1694. }
  1695. }
  1696. return 1;
  1697. }
  1698. static gint
  1699. lua_mimepart_get_parent (lua_State * L)
  1700. {
  1701. LUA_TRACE_POINT;
  1702. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1703. struct rspamd_mime_part **pparent;
  1704. if (part == NULL) {
  1705. return luaL_error (L, "invalid arguments");
  1706. }
  1707. if (part->parent_part) {
  1708. pparent = lua_newuserdata (L, sizeof (*pparent));
  1709. *pparent = part->parent_part;
  1710. rspamd_lua_setclass (L, "rspamd{mimepart}", -1);
  1711. }
  1712. else {
  1713. lua_pushnil (L);
  1714. }
  1715. return 1;
  1716. }
  1717. static gint
  1718. lua_mimepart_get_text (lua_State * L)
  1719. {
  1720. LUA_TRACE_POINT;
  1721. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1722. struct rspamd_mime_text_part **ppart;
  1723. if (part == NULL) {
  1724. return luaL_error (L, "invalid arguments");
  1725. }
  1726. if (part->part_type != RSPAMD_MIME_PART_TEXT || part->specific.txt == NULL) {
  1727. lua_pushnil (L);
  1728. }
  1729. else {
  1730. ppart = lua_newuserdata (L, sizeof (*ppart));
  1731. *ppart = part->specific.txt;
  1732. rspamd_lua_setclass (L, "rspamd{textpart}", -1);
  1733. }
  1734. return 1;
  1735. }
  1736. static gint
  1737. lua_mimepart_get_digest (lua_State * L)
  1738. {
  1739. LUA_TRACE_POINT;
  1740. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1741. gchar digestbuf[rspamd_cryptobox_HASHBYTES * 2 + 1];
  1742. if (part == NULL) {
  1743. return luaL_error (L, "invalid arguments");
  1744. }
  1745. memset (digestbuf, 0, sizeof (digestbuf));
  1746. rspamd_encode_hex_buf (part->digest, sizeof (part->digest),
  1747. digestbuf, sizeof (digestbuf));
  1748. lua_pushstring (L, digestbuf);
  1749. return 1;
  1750. }
  1751. static gint
  1752. lua_mimepart_get_id (lua_State * L)
  1753. {
  1754. LUA_TRACE_POINT;
  1755. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1756. if (part == NULL) {
  1757. return luaL_error (L, "invalid arguments");
  1758. }
  1759. lua_pushinteger (L, part->part_number);
  1760. return 1;
  1761. }
  1762. static gint
  1763. lua_mimepart_headers_foreach (lua_State *L)
  1764. {
  1765. LUA_TRACE_POINT;
  1766. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1767. enum rspamd_lua_task_header_type how = RSPAMD_TASK_HEADER_PUSH_SIMPLE;
  1768. struct rspamd_lua_regexp *re = NULL;
  1769. struct rspamd_mime_header *hdr, *cur;
  1770. gint old_top;
  1771. if (part && lua_isfunction (L, 2)) {
  1772. if (lua_istable (L, 3)) {
  1773. lua_pushstring (L, "full");
  1774. lua_gettable (L, 3);
  1775. if (lua_isboolean (L, -1) && lua_toboolean (L, -1)) {
  1776. how = RSPAMD_TASK_HEADER_PUSH_FULL;
  1777. }
  1778. lua_pop (L, 1);
  1779. lua_pushstring (L, "raw");
  1780. lua_gettable (L, 3);
  1781. if (lua_isboolean (L, -1) && lua_toboolean (L, -1)) {
  1782. how = RSPAMD_TASK_HEADER_PUSH_RAW;
  1783. }
  1784. lua_pop (L, 1);
  1785. lua_pushstring (L, "regexp");
  1786. lua_gettable (L, 3);
  1787. if (lua_isuserdata (L, -1)) {
  1788. RSPAMD_LUA_CHECK_UDATA_PTR_OR_RETURN(L, -1, "rspamd{regexp}",
  1789. struct rspamd_lua_regexp, re);
  1790. }
  1791. lua_pop (L, 1);
  1792. }
  1793. if (part->headers_order) {
  1794. hdr = part->headers_order;
  1795. LL_FOREACH2 (hdr, cur, ord_next) {
  1796. if (re && re->re) {
  1797. if (!rspamd_regexp_match (re->re, cur->name,
  1798. strlen (cur->name),FALSE)) {
  1799. continue;
  1800. }
  1801. }
  1802. old_top = lua_gettop (L);
  1803. lua_pushvalue (L, 2);
  1804. lua_pushstring (L, cur->name);
  1805. rspamd_lua_push_header (L, cur, how);
  1806. if (lua_pcall (L, 2, LUA_MULTRET, 0) != 0) {
  1807. msg_err ("call to header_foreach failed: %s",
  1808. lua_tostring (L, -1));
  1809. lua_settop (L, old_top);
  1810. break;
  1811. }
  1812. else {
  1813. if (lua_gettop (L) > old_top) {
  1814. if (lua_isboolean (L, old_top + 1)) {
  1815. if (lua_toboolean (L, old_top + 1)) {
  1816. lua_settop (L, old_top);
  1817. break;
  1818. }
  1819. }
  1820. }
  1821. }
  1822. lua_settop (L, old_top);
  1823. }
  1824. }
  1825. }
  1826. return 0;
  1827. }
  1828. static gint
  1829. lua_mimepart_get_specific (lua_State * L)
  1830. {
  1831. LUA_TRACE_POINT;
  1832. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1833. if (part == NULL) {
  1834. return luaL_error (L, "invalid arguments");
  1835. }
  1836. if (part->part_type != RSPAMD_MIME_PART_CUSTOM_LUA) {
  1837. lua_pushnil (L);
  1838. }
  1839. else {
  1840. lua_rawgeti (L, LUA_REGISTRYINDEX, part->specific.lua_specific.cbref);
  1841. }
  1842. return 1;
  1843. }
  1844. static gint
  1845. lua_mimepart_get_urls (lua_State * L)
  1846. {
  1847. LUA_TRACE_POINT;
  1848. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1849. if (part == NULL) {
  1850. return luaL_error (L, "invalid arguments");
  1851. }
  1852. struct lua_tree_cb_data cb;
  1853. struct rspamd_url *u;
  1854. static const gint default_protocols_mask = PROTOCOL_HTTP|PROTOCOL_HTTPS|
  1855. PROTOCOL_FILE|PROTOCOL_FTP;
  1856. gsize sz, max_urls = 0, i;
  1857. if (part->urls == NULL) {
  1858. lua_newtable (L);
  1859. return 1;
  1860. }
  1861. if (!lua_url_cbdata_fill (L, 2, &cb, default_protocols_mask,
  1862. ~(0), max_urls)) {
  1863. return luaL_error (L, "invalid arguments");
  1864. }
  1865. sz = part->urls->len;
  1866. lua_createtable (L, sz, 0);
  1867. PTR_ARRAY_FOREACH (part->urls, i, u) {
  1868. lua_tree_url_callback (u, u, &cb);
  1869. }
  1870. lua_url_cbdata_dtor (&cb);
  1871. return 1;
  1872. }
  1873. static gint
  1874. lua_mimepart_is_specific (lua_State * L)
  1875. {
  1876. LUA_TRACE_POINT;
  1877. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1878. if (part == NULL) {
  1879. return luaL_error (L, "invalid arguments");
  1880. }
  1881. lua_pushboolean (L, part->part_type == RSPAMD_MIME_PART_CUSTOM_LUA);
  1882. return 1;
  1883. }
  1884. static gint
  1885. lua_mimepart_set_specific (lua_State * L)
  1886. {
  1887. LUA_TRACE_POINT;
  1888. struct rspamd_mime_part *part = lua_check_mimepart (L);
  1889. if (part == NULL || lua_isnil (L, 2)) {
  1890. return luaL_error (L, "invalid arguments");
  1891. }
  1892. if (part->part_type != RSPAMD_MIME_PART_UNDEFINED &&
  1893. part->part_type != RSPAMD_MIME_PART_CUSTOM_LUA) {
  1894. return luaL_error (L,
  1895. "internal error: trying to set specific lua content on part of type %d",
  1896. part->part_type);
  1897. }
  1898. if (part->part_type == RSPAMD_MIME_PART_CUSTOM_LUA) {
  1899. /* Push old specific data */
  1900. lua_rawgeti (L, LUA_REGISTRYINDEX, part->specific.lua_specific.cbref);
  1901. luaL_unref (L, LUA_REGISTRYINDEX, part->specific.lua_specific.cbref);
  1902. }
  1903. else {
  1904. part->part_type = RSPAMD_MIME_PART_CUSTOM_LUA;
  1905. lua_pushnil (L);
  1906. }
  1907. /* Now, we push argument on the position 2 and save its reference */
  1908. lua_pushvalue (L, 2);
  1909. part->specific.lua_specific.cbref = luaL_ref (L, LUA_REGISTRYINDEX);
  1910. /* Now stack has just a return value as luaL_ref removes value from stack */
  1911. gint ltype = lua_type (L, 2);
  1912. switch (ltype) {
  1913. case LUA_TTABLE:
  1914. part->specific.lua_specific.type = RSPAMD_LUA_PART_TABLE;
  1915. break;
  1916. case LUA_TSTRING:
  1917. part->specific.lua_specific.type = RSPAMD_LUA_PART_STRING;
  1918. break;
  1919. case LUA_TUSERDATA:
  1920. if (rspamd_lua_check_udata_maybe (L, 2, "rspamd{text}")) {
  1921. part->specific.lua_specific.type = RSPAMD_LUA_PART_TEXT;
  1922. }
  1923. else {
  1924. part->specific.lua_specific.type = RSPAMD_LUA_PART_UNKNOWN;
  1925. }
  1926. break;
  1927. case LUA_TFUNCTION:
  1928. part->specific.lua_specific.type = RSPAMD_LUA_PART_FUNCTION;
  1929. break;
  1930. default:
  1931. part->specific.lua_specific.type = RSPAMD_LUA_PART_UNKNOWN;
  1932. break;
  1933. }
  1934. return 1;
  1935. }
  1936. void
  1937. luaopen_textpart (lua_State * L)
  1938. {
  1939. rspamd_lua_new_class (L, "rspamd{textpart}", textpartlib_m);
  1940. lua_pop (L, 1);
  1941. }
  1942. void
  1943. luaopen_mimepart (lua_State * L)
  1944. {
  1945. rspamd_lua_new_class (L, "rspamd{mimepart}", mimepartlib_m);
  1946. lua_pop (L, 1);
  1947. }