1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
|
/*-
* Copyright 2016 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lua_common.h"
#include "message.h"
#include "libserver/html/html.h"
#include "libserver/html/html.hxx"
#include "libserver/html/html_tag.hxx"
#include "libserver/html/html_block.hxx"
#include "images.h"
#include "contrib/ankerl/unordered_dense.h"
#include <frozen/string.h>
#include <frozen/unordered_map.h>
/***
* @module rspamd_html
* This module provides different methods to access HTML tags. To get HTML context
* from an HTML part you could use method `part:get_html()`
* @example
rspamd_config.R_EMPTY_IMAGE = function(task)
local tp = task:get_text_parts() -- get text parts in a message
for _,p in ipairs(tp) do -- iterate over text parts array using `ipairs`
if p:is_html() then -- if the current part is html part
local hc = p:get_html() -- we get HTML context
local len = p:get_length() -- and part's length
if len < 50 then -- if we have a part that has less than 50 bytes of text
local images = hc:get_images() -- then we check for HTML images
if images then -- if there are images
for _,i in ipairs(images) do -- then iterate over images in the part
if i['height'] + i['width'] >= 400 then -- if we have a large image
return true -- add symbol
end
end
end
end
end
end
end
*/
/***
* @method html:has_tag(name)
* Checks if a specified tag `name` is presented in a part
* @param {string} name name of tag to check
* @return {boolean} `true` if the tag exists in HTML tree
*/
LUA_FUNCTION_DEF(html, has_tag);
/***
* @method html:check_property(name)
* Checks if the HTML has a specific property. Here is the list of available properties:
*
* - `no_html` - no html tag presented
* - `bad_element` - part has some broken elements
* - `xml` - part is xhtml
* - `unknown_element` - part has some unknown elements
* - `duplicate_element` - part has some duplicate elements that should be unique (namely, `title` tag)
* - `unbalanced` - part has unbalanced tags
* @param {string} name name of property
* @return {boolean} true if the part has the specified property
*/
LUA_FUNCTION_DEF(html, has_property);
/***
* @method html:get_images()
* Returns a table of images found in html. Each image is, in turn, a table with the following fields:
*
* - `src` - link to the source
* - `height` - height in pixels
* - `width` - width in pixels
* - `embedded` - `true` if an image is embedded in a message
* @return {table} table of images in html part
*/
LUA_FUNCTION_DEF(html, get_images);
/***
* @method html:foreach_tag(tagname, callback)
* Processes HTML tree calling the specified callback for each tag of the specified
* type.
*
* Callback is called with the following attributes:
*
* - `tag`: html tag structure
* - `content_length`: length of content within a tag
*
* Callback function should return `true` to **stop** processing and `false` to continue
* @return nothing
*/
LUA_FUNCTION_DEF(html, foreach_tag);
/***
* @method html:get_invisible()
* Returns invisible content of the HTML data
* @return
*/
LUA_FUNCTION_DEF(html, get_invisible);
static const struct luaL_reg htmllib_m[] = {
LUA_INTERFACE_DEF(html, has_tag),
LUA_INTERFACE_DEF(html, has_property),
LUA_INTERFACE_DEF(html, get_images),
LUA_INTERFACE_DEF(html, foreach_tag),
LUA_INTERFACE_DEF(html, get_invisible),
{"__tostring", rspamd_lua_class_tostring},
{NULL, NULL}};
/***
* @method html_tag:get_type()
* Returns string representation of HTML type for a tag
* @return {string} type of tag
*/
LUA_FUNCTION_DEF(html_tag, get_type);
/***
* @method html_tag:get_extra()
* Returns extra data associated with the tag
* @return {url|image|nil} extra data associated with the tag
*/
LUA_FUNCTION_DEF(html_tag, get_extra);
/***
* @method html_tag:get_parent()
* Returns parent node for a specified tag
* @return {html_tag} parent object for a specified tag
*/
LUA_FUNCTION_DEF(html_tag, get_parent);
/***
* @method html_tag:get_flags()
* Returns flags a specified tag:
*
* - `closed`: tag is properly closed
* - `closing`: tag is a closing tag
* - `broken`: tag is somehow broken
* - `unbalanced`: tag is unbalanced
* - `xml`: tag is xml tag
* @return {table} table of flags
*/
LUA_FUNCTION_DEF(html_tag, get_flags);
/***
* @method html_tag:get_content()
* Returns content of tag (approximate for some cases)
* @return {rspamd_text} rspamd text with tag's content
*/
LUA_FUNCTION_DEF(html_tag, get_content);
/***
* @method html_tag:get_content_length()
* Returns length of a tag's content
* @return {number} size of content enclosed within a tag
*/
LUA_FUNCTION_DEF(html_tag, get_content_length);
/***
* @method html_tag:get_style()
* Returns style calculated for the element
* @return {table} table associated with the style
*/
LUA_FUNCTION_DEF(html_tag, get_style);
/***
* @method html_tag:get_attribute(name)
* Returns value of attribute for the element
* Refer to `html_components_map` in `src/libserver/html/html.cxx` for recognised names
* @return {string|nil} value of the attribute
*/
LUA_FUNCTION_DEF(html_tag, get_attribute);
/***
* @method html_tag:get_all_attributes()
* Returns table of all attributes for the element
* @return {table} table with attribute names as keys and values as strings
*/
LUA_FUNCTION_DEF(html_tag, get_all_attributes);
/***
* @method html_tag:get_unknown_attributes()
* Returns table of unknown/unrecognized attributes for the element
* @return {table} table with unknown attribute names as keys and values as strings
*/
LUA_FUNCTION_DEF(html_tag, get_unknown_attributes);
/***
* @method html_tag:get_children()
* Returns array of child tags for the element
* @return {table} array of child html_tag objects
*/
LUA_FUNCTION_DEF(html_tag, get_children);
/***
* @method html_tag:has_attribute(name)
* Checks if element has the specified attribute
* @param {string} name attribute name to check
* @return {boolean} true if attribute exists
*/
LUA_FUNCTION_DEF(html_tag, has_attribute);
/***
* @method html_tag:get_numeric_attribute(name)
* Returns numeric value of attribute (if supported and parseable)
* Works for attributes like width, height, font-size, etc.
* @param {string} name attribute name
* @return {number|nil} numeric value or nil if not numeric/parseable
*/
LUA_FUNCTION_DEF(html_tag, get_numeric_attribute);
static const struct luaL_reg taglib_m[] = {
LUA_INTERFACE_DEF(html_tag, get_type),
LUA_INTERFACE_DEF(html_tag, get_extra),
LUA_INTERFACE_DEF(html_tag, get_parent),
LUA_INTERFACE_DEF(html_tag, get_flags),
LUA_INTERFACE_DEF(html_tag, get_content),
LUA_INTERFACE_DEF(html_tag, get_content_length),
LUA_INTERFACE_DEF(html_tag, get_style),
LUA_INTERFACE_DEF(html_tag, get_attribute),
LUA_INTERFACE_DEF(html_tag, get_all_attributes),
LUA_INTERFACE_DEF(html_tag, get_unknown_attributes),
LUA_INTERFACE_DEF(html_tag, get_children),
LUA_INTERFACE_DEF(html_tag, has_attribute),
LUA_INTERFACE_DEF(html_tag, get_numeric_attribute),
{"__tostring", rspamd_lua_class_tostring},
{NULL, NULL}};
static struct rspamd::html::html_content *
lua_check_html(lua_State *L, int pos)
{
void *ud = rspamd_lua_check_udata(L, pos, rspamd_html_classname);
luaL_argcheck(L, ud != NULL, pos, "'html' expected");
return ud ? *((struct rspamd::html::html_content **) ud) : NULL;
}
struct lua_html_tag {
rspamd::html::html_content *html;
const rspamd::html::html_tag *tag;
};
static struct lua_html_tag *
lua_check_html_tag(lua_State *L, int pos)
{
void *ud = rspamd_lua_check_udata(L, pos, rspamd_html_tag_classname);
luaL_argcheck(L, ud != NULL, pos, "'html_tag' expected");
return ud ? ((struct lua_html_tag *) ud) : NULL;
}
static int
lua_html_has_tag(lua_State *L)
{
LUA_TRACE_POINT;
auto *hc = lua_check_html(L, 1);
const char *tagname = luaL_checkstring(L, 2);
gboolean ret = FALSE;
if (hc && tagname) {
if (rspamd_html_tag_seen(hc, tagname)) {
ret = TRUE;
}
}
lua_pushboolean(L, ret);
return 1;
}
constexpr const auto prop_map = frozen::make_unordered_map<frozen::string, int>({
{"no_html", RSPAMD_HTML_FLAG_BAD_START},
{"bad_start", RSPAMD_HTML_FLAG_BAD_START},
{"bad_element", RSPAMD_HTML_FLAG_BAD_ELEMENTS},
{"bad_elements", RSPAMD_HTML_FLAG_BAD_ELEMENTS},
{"xml", RSPAMD_HTML_FLAG_XML},
{"unknown_element", RSPAMD_HTML_FLAG_UNKNOWN_ELEMENTS},
{"unknown_elements", RSPAMD_HTML_FLAG_UNKNOWN_ELEMENTS},
{"duplicate_element", RSPAMD_HTML_FLAG_DUPLICATE_ELEMENTS},
{"duplicate_elements", RSPAMD_HTML_FLAG_DUPLICATE_ELEMENTS},
{"unbalanced", RSPAMD_HTML_FLAG_UNBALANCED},
{"data_urls", RSPAMD_HTML_FLAG_HAS_DATA_URLS},
});
static int
lua_html_has_property(lua_State *L)
{
LUA_TRACE_POINT;
auto *hc = lua_check_html(L, 1);
const char *propname = luaL_checkstring(L, 2);
gboolean ret = FALSE;
if (hc && propname) {
auto found_prop = prop_map.find(frozen::string(propname));
if (found_prop != prop_map.end()) {
ret = hc->flags & found_prop->second;
}
}
lua_pushboolean(L, ret);
return 1;
}
static void
lua_html_push_image(lua_State *L, const struct html_image *img)
{
LUA_TRACE_POINT;
struct lua_html_tag *ltag;
struct rspamd_url **purl;
lua_createtable(L, 0, 7);
if (img->src) {
lua_pushstring(L, "src");
if (img->flags & RSPAMD_HTML_FLAG_IMAGE_DATA) {
struct rspamd_lua_text *t;
t = static_cast<rspamd_lua_text *>(lua_newuserdata(L, sizeof(*t)));
t->start = img->src;
t->len = strlen(img->src);
t->flags = 0;
rspamd_lua_setclass(L, rspamd_text_classname, -1);
}
else {
lua_pushstring(L, img->src);
}
lua_settable(L, -3);
}
if (img->url) {
lua_pushstring(L, "url");
purl = static_cast<rspamd_url **>(lua_newuserdata(L, sizeof(gpointer)));
*purl = img->url;
rspamd_lua_setclass(L, rspamd_url_classname, -1);
lua_settable(L, -3);
}
if (img->tag) {
lua_pushstring(L, "tag");
ltag = static_cast<lua_html_tag *>(lua_newuserdata(L, sizeof(struct lua_html_tag)));
ltag->tag = static_cast<rspamd::html::html_tag *>(img->tag);
ltag->html = NULL;
rspamd_lua_setclass(L, rspamd_html_tag_classname, -1);
lua_settable(L, -3);
}
lua_pushstring(L, "height");
lua_pushinteger(L, img->height);
lua_settable(L, -3);
lua_pushstring(L, "width");
lua_pushinteger(L, img->width);
lua_settable(L, -3);
lua_pushstring(L, "embedded");
lua_pushboolean(L, img->flags & RSPAMD_HTML_FLAG_IMAGE_EMBEDDED);
lua_settable(L, -3);
lua_pushstring(L, "data");
lua_pushboolean(L, img->flags & RSPAMD_HTML_FLAG_IMAGE_DATA);
lua_settable(L, -3);
}
static int
lua_html_get_images(lua_State *L)
{
LUA_TRACE_POINT;
auto *hc = lua_check_html(L, 1);
unsigned int i = 1;
if (hc != NULL) {
lua_createtable(L, hc->images.size(), 0);
for (const auto *img: hc->images) {
lua_html_push_image(L, img);
lua_rawseti(L, -2, i++);
}
}
else {
lua_newtable(L);
}
return 1;
}
static void
lua_html_push_block(lua_State *L, const struct rspamd::html::html_block *bl)
{
LUA_TRACE_POINT;
lua_createtable(L, 0, 6);
if (bl->fg_color_mask) {
lua_pushstring(L, "color");
lua_createtable(L, 4, 0);
lua_pushinteger(L, bl->fg_color.r);
lua_rawseti(L, -2, 1);
lua_pushinteger(L, bl->fg_color.g);
lua_rawseti(L, -2, 2);
lua_pushinteger(L, bl->fg_color.b);
lua_rawseti(L, -2, 3);
lua_pushinteger(L, bl->fg_color.alpha);
lua_rawseti(L, -2, 4);
lua_settable(L, -3);
}
if (bl->bg_color_mask) {
lua_pushstring(L, "bgcolor");
lua_createtable(L, 4, 0);
lua_pushinteger(L, bl->bg_color.r);
lua_rawseti(L, -2, 1);
lua_pushinteger(L, bl->bg_color.g);
lua_rawseti(L, -2, 2);
lua_pushinteger(L, bl->bg_color.b);
lua_rawseti(L, -2, 3);
lua_pushinteger(L, bl->bg_color.alpha);
lua_rawseti(L, -2, 4);
lua_settable(L, -3);
}
if (bl->font_mask) {
lua_pushstring(L, "font_size");
lua_pushinteger(L, bl->font_size);
lua_settable(L, -3);
}
lua_pushstring(L, "visible");
lua_pushboolean(L, bl->is_visible());
lua_settable(L, -3);
lua_pushstring(L, "transparent");
lua_pushboolean(L, bl->is_transparent());
lua_settable(L, -3);
}
static int
lua_html_foreach_tag(lua_State *L)
{
LUA_TRACE_POINT;
auto *hc = lua_check_html(L, 1);
const char *tagname;
int id;
auto any = false;
ankerl::unordered_dense::set<int> tags;
if (lua_type(L, 2) == LUA_TSTRING) {
tagname = luaL_checkstring(L, 2);
if (strcmp(tagname, "any") == 0) {
any = true;
}
else {
id = rspamd_html_tag_by_name(tagname);
if (id == -1) {
return luaL_error(L, "invalid tagname: %s", tagname);
}
tags.insert(id);
}
}
else if (lua_type(L, 2) == LUA_TTABLE) {
lua_pushvalue(L, 2);
for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
tagname = luaL_checkstring(L, -1);
if (strcmp(tagname, "any") == 0) {
any = TRUE;
}
else {
id = rspamd_html_tag_by_name(tagname);
if (id == -1) {
return luaL_error(L, "invalid tagname: %s", tagname);
}
tags.insert(id);
}
}
lua_pop(L, 1);
}
if (hc && (any || !tags.empty()) && lua_isfunction(L, 3)) {
hc->traverse_all_tags([&](const rspamd::html::html_tag *tag) -> bool {
if (tag && (any || tags.contains(tag->id))) {
lua_pushcfunction(L, &rspamd_lua_traceback);
auto err_idx = lua_gettop(L);
lua_pushvalue(L, 3);
auto *ltag = static_cast<lua_html_tag *>(lua_newuserdata(L, sizeof(lua_html_tag)));
ltag->tag = tag;
ltag->html = hc;
auto ct = ltag->tag->get_content(hc);
rspamd_lua_setclass(L, rspamd_html_tag_classname, -1);
lua_pushinteger(L, ct.size());
/* Leaf flag */
if (tag->children.empty()) {
lua_pushboolean(L, true);
}
else {
lua_pushboolean(L, false);
}
if (lua_pcall(L, 3, 1, err_idx) != 0) {
msg_err("error in foreach_tag callback: %s", lua_tostring(L, -1));
lua_settop(L, err_idx - 1);
return false;
}
if (lua_toboolean(L, -1)) {
lua_settop(L, err_idx - 1);
return false;
}
lua_settop(L, err_idx - 1);
}
return true;
});
}
else {
return luaL_error(L, "invalid arguments");
}
return 0;
}
static int
lua_html_get_invisible(lua_State *L)
{
LUA_TRACE_POINT;
auto *hc = lua_check_html(L, 1);
if (hc != NULL) {
lua_new_text(L, hc->invisible.c_str(), hc->invisible.size(), false);
}
else {
lua_newtable(L);
}
return 1;
}
static int
lua_html_tag_get_type(lua_State *L)
{
LUA_TRACE_POINT;
struct lua_html_tag *ltag = lua_check_html_tag(L, 1);
const char *tagname;
if (ltag != NULL) {
tagname = rspamd_html_tag_by_id(ltag->tag->id);
if (tagname) {
lua_pushstring(L, tagname);
}
else {
lua_pushnil(L);
}
}
else {
return luaL_error(L, "invalid arguments");
}
return 1;
}
static int
lua_html_tag_get_parent(lua_State *L)
{
LUA_TRACE_POINT;
struct lua_html_tag *ltag = lua_check_html_tag(L, 1), *ptag;
if (ltag != NULL) {
auto *parent = ltag->tag->parent;
if (parent) {
ptag = static_cast<lua_html_tag *>(lua_newuserdata(L, sizeof(*ptag)));
ptag->tag = static_cast<rspamd::html::html_tag *>(parent);
ptag->html = ltag->html;
rspamd_lua_setclass(L, rspamd_html_tag_classname, -1);
}
else {
lua_pushnil(L);
}
}
else {
return luaL_error(L, "invalid arguments");
}
return 1;
}
static int
lua_html_tag_get_flags(lua_State *L)
{
LUA_TRACE_POINT;
struct lua_html_tag *ltag = lua_check_html_tag(L, 1);
int i = 1;
if (ltag && ltag->tag) {
/* Push flags */
lua_createtable(L, 4, 0);
if (ltag->tag->flags & FL_HREF) {
lua_pushstring(L, "href");
lua_rawseti(L, -2, i++);
}
if (ltag->tag->flags & FL_CLOSED) {
lua_pushstring(L, "closed");
lua_rawseti(L, -2, i++);
}
if (ltag->tag->flags & FL_BROKEN) {
lua_pushstring(L, "broken");
lua_rawseti(L, -2, i++);
}
if (ltag->tag->flags & FL_XML) {
lua_pushstring(L, "xml");
lua_rawseti(L, -2, i++);
}
if (ltag->tag->flags & RSPAMD_HTML_FLAG_UNBALANCED) {
lua_pushstring(L, "unbalanced");
lua_rawseti(L, -2, i++);
}
}
else {
return luaL_error(L, "invalid arguments");
}
return 1;
}
static int
lua_html_tag_get_content(lua_State *L)
{
LUA_TRACE_POINT;
struct lua_html_tag *ltag = lua_check_html_tag(L, 1);
struct rspamd_lua_text *t;
if (ltag) {
if (ltag->html) {
auto ct = ltag->tag->get_content(ltag->html);
if (ct.size() > 0) {
t = static_cast<rspamd_lua_text *>(lua_newuserdata(L, sizeof(*t)));
rspamd_lua_setclass(L, rspamd_text_classname, -1);
t->start = ct.data();
t->len = ct.size();
t->flags = 0;
}
else {
lua_pushnil(L);
}
}
else {
lua_pushnil(L);
}
}
else {
return luaL_error(L, "invalid arguments");
}
return 1;
}
static int
lua_html_tag_get_content_length(lua_State *L)
{
LUA_TRACE_POINT;
struct lua_html_tag *ltag = lua_check_html_tag(L, 1);
if (ltag) {
if (ltag->html) {
auto ct = ltag->tag->get_content(ltag->html);
lua_pushinteger(L, ct.size());
}
else {
lua_pushinteger(L, ltag->tag->get_content_length());
}
}
else {
return luaL_error(L, "invalid arguments");
}
return 1;
}
static int
lua_html_tag_get_extra(lua_State *L)
{
LUA_TRACE_POINT;
struct lua_html_tag *ltag = lua_check_html_tag(L, 1);
struct html_image *img;
if (ltag) {
if (!std::holds_alternative<std::monostate>(ltag->tag->extra)) {
if (std::holds_alternative<struct html_image *>(ltag->tag->extra)) {
img = std::get<struct html_image *>(ltag->tag->extra);
lua_html_push_image(L, img);
}
else if (std::holds_alternative<struct rspamd_url *>(ltag->tag->extra)) {
/* For A that's URL */
auto *lua_url = static_cast<rspamd_lua_url *>(lua_newuserdata(L, sizeof(rspamd_lua_url)));
lua_url->url = std::get<struct rspamd_url *>(ltag->tag->extra);
rspamd_lua_setclass(L, rspamd_url_classname, -1);
}
else {
/* Unknown extra ? */
lua_pushnil(L);
}
}
else {
lua_pushnil(L);
}
}
else {
return luaL_error(L, "invalid arguments");
}
return 1;
}
static int
lua_html_tag_get_style(lua_State *L)
{
LUA_TRACE_POINT;
struct lua_html_tag *ltag = lua_check_html_tag(L, 1);
if (ltag) {
if (ltag->tag->block) {
lua_html_push_block(L, ltag->tag->block);
}
}
else {
return luaL_error(L, "invalid arguments");
}
return 1;
}
static int
lua_html_tag_get_all_attributes(lua_State *L)
{
LUA_TRACE_POINT;
struct lua_html_tag *ltag = lua_check_html_tag(L, 1);
if (ltag) {
auto all_attrs = ltag->tag->get_all_attributes();
lua_createtable(L, 0, all_attrs.size());
for (const auto &[name, value]: all_attrs) {
lua_pushlstring(L, name.data(), name.size());
lua_pushlstring(L, value.data(), value.size());
lua_settable(L, -3);
}
}
else {
return luaL_error(L, "invalid arguments");
}
return 1;
}
static int
lua_html_tag_get_attribute(lua_State *L)
{
LUA_TRACE_POINT;
struct lua_html_tag *ltag = lua_check_html_tag(L, 1);
gsize slen;
const char *attr_name = luaL_checklstring(L, 2, &slen);
if (ltag && attr_name) {
auto maybe_attr = ltag->tag->find_component_by_name({attr_name, slen});
if (maybe_attr) {
lua_pushlstring(L, maybe_attr->data(), maybe_attr->size());
}
else {
lua_pushnil(L);
}
}
else {
return luaL_error(L, "invalid arguments");
}
return 1;
}
static int
lua_html_tag_get_unknown_attributes(lua_State *L)
{
LUA_TRACE_POINT;
struct lua_html_tag *ltag = lua_check_html_tag(L, 1);
if (ltag) {
auto unknown_attrs = ltag->tag->get_unknown_components();
lua_createtable(L, 0, unknown_attrs.size());
for (const auto &[name, value]: unknown_attrs) {
lua_pushlstring(L, name.data(), name.size());
lua_pushlstring(L, value.data(), value.size());
lua_settable(L, -3);
}
}
else {
return luaL_error(L, "invalid arguments");
}
return 1;
}
static int
lua_html_tag_get_children(lua_State *L)
{
LUA_TRACE_POINT;
struct lua_html_tag *ltag = lua_check_html_tag(L, 1);
if (ltag) {
lua_createtable(L, ltag->tag->children.size(), 0);
for (int i = 0; i < ltag->tag->children.size(); i++) {
auto *child_tag = static_cast<lua_html_tag *>(lua_newuserdata(L, sizeof(lua_html_tag)));
child_tag->tag = ltag->tag->children[i];
child_tag->html = ltag->html;
rspamd_lua_setclass(L, rspamd_html_tag_classname, -1);
lua_rawseti(L, -2, i + 1);
}
}
else {
return luaL_error(L, "invalid arguments");
}
return 1;
}
static int
lua_html_tag_has_attribute(lua_State *L)
{
LUA_TRACE_POINT;
struct lua_html_tag *ltag = lua_check_html_tag(L, 1);
gsize slen;
const char *attr_name = luaL_checklstring(L, 2, &slen);
if (ltag && attr_name) {
auto maybe_attr = ltag->tag->find_component_by_name({attr_name, slen});
lua_pushboolean(L, maybe_attr.has_value());
}
else {
return luaL_error(L, "invalid arguments");
}
return 1;
}
static int
lua_html_tag_get_numeric_attribute(lua_State *L)
{
LUA_TRACE_POINT;
struct lua_html_tag *ltag = lua_check_html_tag(L, 1);
gsize slen;
const char *attr_name = luaL_checklstring(L, 2, &slen);
if (ltag && attr_name) {
std::string_view name_view{attr_name, slen};
// Check for numeric components
if (name_view == "width") {
if (auto comp = ltag->tag->find_component<rspamd::html::html_component_width>()) {
if (auto numeric_val = comp.value()->get_numeric_value()) {
lua_pushinteger(L, numeric_val.value());
return 1;
}
}
}
else if (name_view == "height") {
if (auto comp = ltag->tag->find_component<rspamd::html::html_component_height>()) {
if (auto numeric_val = comp.value()->get_numeric_value()) {
lua_pushinteger(L, numeric_val.value());
return 1;
}
}
}
else if (name_view == "size") {
if (auto comp = ltag->tag->find_component<rspamd::html::html_component_size>()) {
if (auto numeric_val = comp.value()->get_numeric_value()) {
lua_pushinteger(L, numeric_val.value());
return 1;
}
}
}
else if (name_view == "font-size") {
if (auto comp = ltag->tag->find_component<rspamd::html::html_component_font_size>()) {
if (auto numeric_val = comp.value()->get_numeric_value()) {
lua_pushinteger(L, numeric_val.value());
return 1;
}
}
}
else if (name_view == "line-height") {
if (auto comp = ltag->tag->find_component<rspamd::html::html_component_line_height>()) {
if (auto numeric_val = comp.value()->get_numeric_value()) {
lua_pushinteger(L, numeric_val.value());
return 1;
}
}
}
else if (name_view == "border-width") {
if (auto comp = ltag->tag->find_component<rspamd::html::html_component_border_width>()) {
if (auto numeric_val = comp.value()->get_numeric_value()) {
lua_pushinteger(L, numeric_val.value());
return 1;
}
}
}
else if (name_view == "opacity") {
if (auto comp = ltag->tag->find_component<rspamd::html::html_component_opacity>()) {
if (auto numeric_val = comp.value()->get_numeric_value()) {
lua_pushnumber(L, numeric_val.value());
return 1;
}
}
}
else if (name_view == "min-width") {
if (auto comp = ltag->tag->find_component<rspamd::html::html_component_min_width>()) {
if (auto numeric_val = comp.value()->get_numeric_value()) {
lua_pushinteger(L, numeric_val.value());
return 1;
}
}
}
else if (name_view == "max-width") {
if (auto comp = ltag->tag->find_component<rspamd::html::html_component_max_width>()) {
if (auto numeric_val = comp.value()->get_numeric_value()) {
lua_pushinteger(L, numeric_val.value());
return 1;
}
}
}
else if (name_view == "min-height") {
if (auto comp = ltag->tag->find_component<rspamd::html::html_component_min_height>()) {
if (auto numeric_val = comp.value()->get_numeric_value()) {
lua_pushinteger(L, numeric_val.value());
return 1;
}
}
}
else if (name_view == "max-height") {
if (auto comp = ltag->tag->find_component<rspamd::html::html_component_max_height>()) {
if (auto numeric_val = comp.value()->get_numeric_value()) {
lua_pushinteger(L, numeric_val.value());
return 1;
}
}
}
else if (name_view == "cellpadding") {
if (auto comp = ltag->tag->find_component<rspamd::html::html_component_cellpadding>()) {
if (auto numeric_val = comp.value()->get_numeric_value()) {
lua_pushinteger(L, numeric_val.value());
return 1;
}
}
}
else if (name_view == "cellspacing") {
if (auto comp = ltag->tag->find_component<rspamd::html::html_component_cellspacing>()) {
if (auto numeric_val = comp.value()->get_numeric_value()) {
lua_pushinteger(L, numeric_val.value());
return 1;
}
}
}
else if (name_view == "tabindex") {
if (auto comp = ltag->tag->find_component<rspamd::html::html_component_tabindex>()) {
if (auto numeric_val = comp.value()->get_numeric_value()) {
lua_pushinteger(L, numeric_val.value());
return 1;
}
}
}
lua_pushnil(L);
}
else {
return luaL_error(L, "invalid arguments");
}
return 1;
}
void luaopen_html(lua_State *L)
{
rspamd_lua_new_class(L, rspamd_html_classname, htmllib_m);
lua_pop(L, 1);
rspamd_lua_new_class(L, rspamd_html_tag_classname, taglib_m);
lua_pop(L, 1);
}
|