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
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
|
/*
* Copyright (c) 2009-2012, Vsevolod Stakhov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "lua_common.h"
#include "map.h"
#include "message.h"
#include "radix.h"
#include "expression.h"
#include "utlist.h"
/***
* This module is used to configure rspamd and is normally available as global
* variable named `rspamd_config`. Unlike other modules, it is not necessary to
* require it before usage.
* @module rspamd_config
* @example
-- Register some callback symbol
local function foo(task)
-- do something
end
rspamd_config:register_symbol('SYMBOL', 1.0, foo)
-- Get configuration
local tab = rspamd_config:get_all_opt('module') -- get table for module's options
local opts = rspamd_config:get_key('options') -- get content of the specified key in rspamd configuration
*/
/* Config file methods */
/***
* @method rspamd_config:get_module_opt(mname, optname)
* Returns value of specified option `optname` for a module `mname`,
* @param {string} mname name of module
* @param {string} optname option to get
* @return {string or table} value of the option or `nil` if option is not found
*/
LUA_FUNCTION_DEF (config, get_module_opt);
/***
* @method rspamd_config:get_all_opt(mname)
* Returns value of all options for a module `mname`, flattening values into a single table consisting
* of all sections with such a name.
* @param {string} mname name of module
* @return {table} table of all options for `mname` or `nil` if a module's configuration is not found
*/
LUA_FUNCTION_DEF (config, get_all_opt);
/***
* @method rspamd_config:get_mempool()
* Returns static configuration memory pool.
* @return {mempool} [memory pool](mempool.md) object
*/
LUA_FUNCTION_DEF (config, get_mempool);
/***
* @method rspamd_config:add_radix_map(mapline[, description])
* Creates new dynamic map of IP/mask addresses.
* @param {string} mapline URL for a map
* @param {string} description optional map description
* @return {radix} radix tree object
* @example
local ip_map = rspamd_config:add_radix_map ('file:///path/to/file', 'my radix map')
...
local function foo(task)
local ip = task:get_from_ip()
if ip_map:get_key(ip) then
return true
end
return false
end
*/
LUA_FUNCTION_DEF (config, add_radix_map);
/***
* @method rspamd_config:radix_from_config(mname, optname)
* Creates new static map of IP/mask addresses from config.
* @param {string} mname name of module
* @param {string} optname option to get
* @return {radix} radix tree object
* @example
local ip_map = rspamd_config:radix_from_config ('mymodule', 'ips')
...
local function foo(task)
local ip = task:get_from_ip()
if ip_map:get_key(ip) then
return true
end
return false
end
*/
LUA_FUNCTION_DEF (config, radix_from_config);
/***
* @method rspamd_config:add_hash_map(mapline[, description])
* Creates new dynamic map string objects.
* @param {string} mapline URL for a map
* @param {string} description optional map description
* @return {hash} hash set object
* @example
local hash_map = rspamd_config:add_hash_map ('file:///path/to/file', 'my hash map')
...
local function foo(task)
local from = task:get_from()
if hash_map:get_key(from['user']) then
return true
end
return false
end
*/
LUA_FUNCTION_DEF (config, add_hash_map);
/***
* @method rspamd_config:add_kv_map(mapline[, description])
* Creates new dynamic map of key/values associations.
* @param {string} mapline URL for a map
* @param {string} description optional map description
* @return {hash} hash table object
* @example
local kv_map = rspamd_config:add_kv_map ('file:///path/to/file', 'my kv map')
...
local function foo(task)
local from = task:get_from()
if from then
local value = kv_map:get_key(from['user'])
if value then
return true,value
end
end
return false
end
*/
LUA_FUNCTION_DEF (config, add_kv_map);
/***
* @method rspamd_config:add_map(mapline[, description], callback)
* Creates new dynamic map with free-form callback
* @param {string} mapline URL for a map
* @param {string} description optional map description
* @param {function} callback function to be called on map load and/or update
* @return {bool} `true` if map has been added
* @example
local str = ''
local function process_map(in)
str = in
end
rspamd_config:add_map('http://example.com/map', "settings map", process_map)
*/
LUA_FUNCTION_DEF (config, add_map);
/***
* @method rspamd_config:get_classifier(name)
* Returns classifier config.
* @param {string} name name of classifier (e.g. `bayes`)
* @return {classifier} classifier object or `nil`
*/
LUA_FUNCTION_DEF (config, get_classifier);
/***
* @method rspamd_config:register_symbol(name, weight, callback)
* Register callback function to be called for a specified symbol with initial weight.
* @param {string} name symbol's name
* @param {number} weight initial weight of symbol (can be less than zero to specify non-spam symbols)
* @param {function} callback callback function to be called for a specified symbol
*/
LUA_FUNCTION_DEF (config, register_symbol);
/***
* @method rspamd_config:register_symbols(callback, [weight], callback_name, [, symbol, ...])
* Register callback function to be called for a set of symbols with initial weight.
* @param {function} callback callback function to be called for a specified symbol
* @param {number} weight initial weight of symbol (can be less than zero to specify non-spam symbols)
* @param {string} callback_name symbolic name of callback
* @param {list of strings} symbol list of symbols registered by this function
*/
LUA_FUNCTION_DEF (config, register_symbols);
/***
* @method rspamd_config:register_virtual_symbol(name, weight,)
* Register virtual symbol that is not associated with any callback.
* @param {string} virtual name symbol's name
* @param {number} weight initial weight of symbol (can be less than zero to specify non-spam symbols)
*/
LUA_FUNCTION_DEF (config, register_virtual_symbol);
/***
* @method rspamd_config:register_callback_symbol(name, weight, callback)
* Register callback function to be called for a specified symbol with initial weight. Symbol itself is
* not registered in the metric and is not intended to be visible by a user.
* @param {string} name symbol's name (just for unique id purposes)
* @param {number} weight initial weight of symbol (can be less than zero to specify non-spam symbols)
* @param {function} callback callback function to be called for a specified symbol
*/
LUA_FUNCTION_DEF (config, register_callback_symbol);
LUA_FUNCTION_DEF (config, register_callback_symbol_priority);
/**
* @method rspamd_config:set_metric_symbol(name, weight, [description], [metric])
* Set the value of a specified symbol in a metric
* @param {string} name name of symbol
* @param {number} weight the weight multiplier
* @param {string} description symbolic description
* @param {string} metric metric name (default metric is used if this value is absent)
*/
LUA_FUNCTION_DEF (config, set_metric_symbol);
/**
* @method rspamd_config:add_composite(name, expression)
* @param {string} name name of composite symbol
* @param {string} expression symbolic expression of the composite rule
* @return {bool} true if a composite has been added sucessfully
*/
LUA_FUNCTION_DEF (config, add_composite);
/***
* @method rspamd_config:register_pre_filter(callback)
* Register function to be called prior to symbols processing.
* @param {function} callback callback function
* @example
local function check_function(task)
-- It is possible to manipulate the task object here: set settings, set pre-action and so on
...
end
rspamd_config:register_pre_filter(check_function)
*/
LUA_FUNCTION_DEF (config, register_pre_filter);
/***
* @method rspamd_config:register_pre_filter(callback)
* Register function to be called after symbols are processed.
* @param {function} callback callback function
*/
LUA_FUNCTION_DEF (config, register_post_filter);
/* XXX: obsoleted */
LUA_FUNCTION_DEF (config, register_module_option);
/* XXX: not needed now */
LUA_FUNCTION_DEF (config, get_api_version);
/***
* @method rspamd_config:get_key(name)
* Returns configuration section with the specified `name`.
* @param {string} name name of config section
* @return {variant} specific value of section
* @example
local set_section = rspamd_config:get_key("settings")
if type(set_section) == "string" then
-- Just a map of ucl
if rspamd_config:add_map(set_section, "settings map", process_settings_map) then
rspamd_config:register_pre_filter(check_settings)
end
elseif type(set_section) == "table" then
if process_settings_table(set_section) then
rspamd_config:register_pre_filter(check_settings)
end
end
*/
LUA_FUNCTION_DEF (config, get_key);
/***
* @method rspamd_config:__newindex(name, callback)
* This metamethod is called if new indicies are added to the `rspamd_config` object.
* Technically, it is the equialent of @see rspamd_config:register_symbol where `weight` is 1.0.
* @param {string} name index name
* @param {function} callback callback to be called
* @example
rspamd_config.R_EMPTY_IMAGE = function (task)
parts = task:get_text_parts()
if parts then
for _,part in ipairs(parts) do
if part:is_empty() then
images = task:get_images()
if images then
-- Symbol `R_EMPTY_IMAGE` is inserted
return true
end
return false
end
end
end
return false
end
*/
LUA_FUNCTION_DEF (config, newindex);
static const struct luaL_reg configlib_m[] = {
LUA_INTERFACE_DEF (config, get_module_opt),
LUA_INTERFACE_DEF (config, get_mempool),
LUA_INTERFACE_DEF (config, get_all_opt),
LUA_INTERFACE_DEF (config, add_radix_map),
LUA_INTERFACE_DEF (config, radix_from_config),
LUA_INTERFACE_DEF (config, add_hash_map),
LUA_INTERFACE_DEF (config, add_kv_map),
LUA_INTERFACE_DEF (config, add_map),
LUA_INTERFACE_DEF (config, get_classifier),
LUA_INTERFACE_DEF (config, register_symbol),
LUA_INTERFACE_DEF (config, register_symbols),
LUA_INTERFACE_DEF (config, register_virtual_symbol),
LUA_INTERFACE_DEF (config, register_callback_symbol),
LUA_INTERFACE_DEF (config, register_callback_symbol_priority),
LUA_INTERFACE_DEF (config, set_metric_symbol),
LUA_INTERFACE_DEF (config, add_composite),
LUA_INTERFACE_DEF (config, register_module_option),
LUA_INTERFACE_DEF (config, register_pre_filter),
LUA_INTERFACE_DEF (config, register_post_filter),
LUA_INTERFACE_DEF (config, get_api_version),
LUA_INTERFACE_DEF (config, get_key),
{"__tostring", rspamd_lua_class_tostring},
{"__newindex", lua_config_newindex},
{NULL, NULL}
};
/* Radix tree */
LUA_FUNCTION_DEF (radix, get_key);
static const struct luaL_reg radixlib_m[] = {
LUA_INTERFACE_DEF (radix, get_key),
{"__tostring", rspamd_lua_class_tostring},
{NULL, NULL}
};
/* Hash table */
LUA_FUNCTION_DEF (hash_table, get_key);
static const struct luaL_reg hashlib_m[] = {
LUA_INTERFACE_DEF (hash_table, get_key),
{"__tostring", rspamd_lua_class_tostring},
{NULL, NULL}
};
struct rspamd_config *
lua_check_config (lua_State * L, gint pos)
{
void *ud = luaL_checkudata (L, pos, "rspamd{config}");
luaL_argcheck (L, ud != NULL, pos, "'config' expected");
return ud ? *((struct rspamd_config **)ud) : NULL;
}
static radix_compressed_t *
lua_check_radix (lua_State * L)
{
void *ud = luaL_checkudata (L, 1, "rspamd{radix}");
luaL_argcheck (L, ud != NULL, 1, "'radix' expected");
return ud ? **((radix_compressed_t ***)ud) : NULL;
}
static GHashTable *
lua_check_hash_table (lua_State * L)
{
void *ud = luaL_checkudata (L, 1, "rspamd{hash_table}");
luaL_argcheck (L, ud != NULL, 1, "'hash_table' expected");
return ud ? **((GHashTable ***)ud) : NULL;
}
/*** Config functions ***/
static gint
lua_config_get_api_version (lua_State *L)
{
lua_pushinteger (L, RSPAMD_LUA_API_VERSION);
return 1;
}
static gint
lua_config_get_module_opt (lua_State * L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
const gchar *mname, *optname;
const ucl_object_t *obj;
if (cfg) {
mname = luaL_checkstring (L, 2);
optname = luaL_checkstring (L, 3);
if (mname && optname) {
obj = rspamd_config_get_module_opt (cfg, mname, optname);
if (obj) {
return ucl_object_push_lua (L, obj, TRUE);
}
}
}
lua_pushnil (L);
return 1;
}
static int
lua_config_get_mempool (lua_State * L)
{
rspamd_mempool_t **ppool;
struct rspamd_config *cfg = lua_check_config (L, 1);
if (cfg != NULL) {
ppool = lua_newuserdata (L, sizeof (rspamd_mempool_t *));
rspamd_lua_setclass (L, "rspamd{mempool}", -1);
*ppool = cfg->cfg_pool;
}
return 1;
}
static gint
lua_config_get_all_opt (lua_State * L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
const gchar *mname;
const ucl_object_t *obj, *cur, *cur_elt;
ucl_object_iter_t it = NULL;
if (cfg) {
mname = luaL_checkstring (L, 2);
if (mname) {
obj = ucl_obj_get_key (cfg->rcl_obj, mname);
/* Flatten object */
if (obj != NULL && ucl_object_type (obj) == UCL_OBJECT) {
lua_newtable (L);
it = ucl_object_iterate_new (obj);
LL_FOREACH (obj, cur) {
it = ucl_object_iterate_reset (it, cur);
while ((cur_elt = ucl_object_iterate_safe (it, true))) {
lua_pushstring (L, ucl_object_key (cur_elt));
ucl_object_push_lua (L, cur_elt, true);
lua_settable (L, -3);
}
}
ucl_object_iterate_free (it);
return 1;
}
}
}
lua_pushnil (L);
return 1;
}
static gint
lua_config_get_classifier (lua_State * L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
struct rspamd_classifier_config *clc = NULL, **pclc = NULL;
const gchar *name;
GList *cur;
if (cfg) {
name = luaL_checkstring (L, 2);
cur = g_list_first (cfg->classifiers);
while (cur) {
clc = cur->data;
if (g_ascii_strcasecmp (clc->name, name) == 0) {
pclc = &clc;
break;
}
cur = g_list_next (cur);
}
if (pclc) {
pclc = lua_newuserdata (L,
sizeof (struct rspamd_classifier_config *));
rspamd_lua_setclass (L, "rspamd{classifier}", -1);
*pclc = clc;
return 1;
}
}
lua_pushnil (L);
return 1;
}
struct lua_callback_data {
union {
gchar *name;
gint ref;
} callback;
gboolean cb_is_ref;
lua_State *L;
gchar *symbol;
};
/*
* Unref symbol if it is local reference
*/
static void
lua_destroy_cfg_symbol (gpointer ud)
{
struct lua_callback_data *cd = ud;
/* Unref callback */
if (cd->cb_is_ref) {
luaL_unref (cd->L, LUA_REGISTRYINDEX, cd->callback.ref);
}
}
static gint
lua_config_register_module_option (lua_State *L)
{
return 0;
}
void
rspamd_lua_call_post_filters (struct rspamd_task *task)
{
struct lua_callback_data *cd;
struct rspamd_task **ptask;
GList *cur;
cur = task->cfg->post_filters;
while (cur) {
cd = cur->data;
if (cd->cb_is_ref) {
lua_rawgeti (cd->L, LUA_REGISTRYINDEX, cd->callback.ref);
}
else {
lua_getglobal (cd->L, cd->callback.name);
}
ptask = lua_newuserdata (cd->L, sizeof (struct rspamd_task *));
rspamd_lua_setclass (cd->L, "rspamd{task}", -1);
*ptask = task;
if (lua_pcall (cd->L, 1, 0, 0) != 0) {
msg_info ("call to %s failed: %s",
cd->cb_is_ref ? "local function" :
cd->callback.name,
lua_tostring (cd->L, -1));
}
cur = g_list_next (cur);
}
}
static gint
lua_config_register_post_filter (lua_State *L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
struct lua_callback_data *cd;
if (cfg) {
cd =
rspamd_mempool_alloc (cfg->cfg_pool,
sizeof (struct lua_callback_data));
if (lua_type (L, 2) == LUA_TSTRING) {
cd->callback.name = rspamd_mempool_strdup (cfg->cfg_pool,
luaL_checkstring (L, 2));
cd->cb_is_ref = FALSE;
}
else {
lua_pushvalue (L, 2);
/* Get a reference */
cd->callback.ref = luaL_ref (L, LUA_REGISTRYINDEX);
cd->cb_is_ref = TRUE;
}
cd->L = L;
cfg->post_filters = g_list_prepend (cfg->post_filters, cd);
rspamd_mempool_add_destructor (cfg->cfg_pool,
(rspamd_mempool_destruct_t)lua_destroy_cfg_symbol,
cd);
}
return 1;
}
void
rspamd_lua_call_pre_filters (struct rspamd_task *task)
{
struct lua_callback_data *cd;
struct rspamd_task **ptask;
GList *cur;
cur = task->cfg->pre_filters;
while (cur) {
cd = cur->data;
if (cd->cb_is_ref) {
lua_rawgeti (cd->L, LUA_REGISTRYINDEX, cd->callback.ref);
}
else {
lua_getglobal (cd->L, cd->callback.name);
}
ptask = lua_newuserdata (cd->L, sizeof (struct rspamd_task *));
rspamd_lua_setclass (cd->L, "rspamd{task}", -1);
*ptask = task;
if (lua_pcall (cd->L, 1, 0, 0) != 0) {
msg_info ("call to %s failed: %s",
cd->cb_is_ref ? "local function" :
cd->callback.name,
lua_tostring (cd->L, -1));
}
cur = g_list_next (cur);
}
}
static gint
lua_config_register_pre_filter (lua_State *L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
struct lua_callback_data *cd;
if (cfg) {
cd =
rspamd_mempool_alloc (cfg->cfg_pool,
sizeof (struct lua_callback_data));
if (lua_type (L, 2) == LUA_TSTRING) {
cd->callback.name = rspamd_mempool_strdup (cfg->cfg_pool,
luaL_checkstring (L, 2));
cd->cb_is_ref = FALSE;
}
else {
lua_pushvalue (L, 2);
/* Get a reference */
cd->callback.ref = luaL_ref (L, LUA_REGISTRYINDEX);
cd->cb_is_ref = TRUE;
}
cd->L = L;
cfg->pre_filters = g_list_prepend (cfg->pre_filters, cd);
rspamd_mempool_add_destructor (cfg->cfg_pool,
(rspamd_mempool_destruct_t)lua_destroy_cfg_symbol,
cd);
}
return 1;
}
static gint
lua_config_add_radix_map (lua_State *L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
const gchar *map_line, *description;
radix_compressed_t **r, ***ud;
if (cfg) {
map_line = luaL_checkstring (L, 2);
description = lua_tostring (L, 3);
r = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (radix_compressed_t *));
*r = radix_create_compressed ();
if (!rspamd_map_add (cfg, map_line, description, rspamd_radix_read,
rspamd_radix_fin, (void **)r)) {
msg_warn ("invalid radix map %s", map_line);
radix_destroy_compressed (*r);
lua_pushnil (L);
return 1;
}
ud = lua_newuserdata (L, sizeof (radix_compressed_t *));
*ud = r;
rspamd_lua_setclass (L, "rspamd{radix}", -1);
return 1;
}
lua_pushnil (L);
return 1;
}
static gint
lua_config_radix_from_config (lua_State *L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
const gchar *mname, *optname;
const ucl_object_t *obj;
radix_compressed_t **r, ***ud;
if (!cfg) {
lua_pushnil (L);
return 1;
}
mname = luaL_checkstring (L, 2);
optname = luaL_checkstring (L, 3);
if (mname && optname) {
obj = rspamd_config_get_module_opt (cfg, mname, optname);
if (obj) {
r = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (radix_compressed_t *));
*r = radix_create_compressed ();
radix_add_generic_iplist (ucl_obj_tostring (obj), r);
ud = lua_newuserdata (L, sizeof (radix_compressed_t *));
*ud = r;
rspamd_lua_setclass (L, "rspamd{radix}", -1);
return 1;
} else {
msg_warn ("Couldnt find config option [%s][%s]", mname, optname);
lua_pushnil (L);
return 1;
}
} else {
msg_warn ("Couldnt find config option");
lua_pushnil (L);
return 1;
}
}
static gint
lua_config_add_hash_map (lua_State *L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
const gchar *map_line, *description;
GHashTable **r, ***ud;
if (cfg) {
map_line = luaL_checkstring (L, 2);
description = lua_tostring (L, 3);
r = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (GHashTable *));
*r = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
if (!rspamd_map_add (cfg, map_line, description, rspamd_hosts_read, rspamd_hosts_fin,
(void **)r)) {
msg_warn ("invalid hash map %s", map_line);
g_hash_table_destroy (*r);
lua_pushnil (L);
return 1;
}
rspamd_mempool_add_destructor (cfg->cfg_pool,
(rspamd_mempool_destruct_t)g_hash_table_destroy,
*r);
ud = lua_newuserdata (L, sizeof (GHashTable *));
*ud = r;
rspamd_lua_setclass (L, "rspamd{hash_table}", -1);
return 1;
}
lua_pushnil (L);
return 1;
}
static gint
lua_config_add_kv_map (lua_State *L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
const gchar *map_line, *description;
GHashTable **r, ***ud;
if (cfg) {
map_line = luaL_checkstring (L, 2);
description = lua_tostring (L, 3);
r = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (GHashTable *));
*r = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
if (!rspamd_map_add (cfg, map_line, description, rspamd_kv_list_read, rspamd_kv_list_fin,
(void **)r)) {
msg_warn ("invalid hash map %s", map_line);
g_hash_table_destroy (*r);
lua_pushnil (L);
return 1;
}
rspamd_mempool_add_destructor (cfg->cfg_pool,
(rspamd_mempool_destruct_t)g_hash_table_destroy,
*r);
ud = lua_newuserdata (L, sizeof (GHashTable *));
*ud = r;
rspamd_lua_setclass (L, "rspamd{hash_table}", -1);
return 1;
}
lua_pushnil (L);
return 1;
}
static gint
lua_config_get_key (lua_State *L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
const gchar *name;
size_t namelen;
const ucl_object_t *val;
name = luaL_checklstring(L, 2, &namelen);
if (name && cfg) {
val = ucl_object_find_keyl(cfg->rcl_obj, name, namelen);
if (val != NULL) {
ucl_object_push_lua (L, val, val->type != UCL_ARRAY);
}
else {
lua_pushnil (L);
}
}
else {
lua_pushnil (L);
}
return 1;
}
static void
lua_metric_symbol_callback (struct rspamd_task *task, gpointer ud)
{
struct lua_callback_data *cd = ud;
struct rspamd_task **ptask;
gint level = lua_gettop (cd->L), nresults;
if (cd->cb_is_ref) {
lua_rawgeti (cd->L, LUA_REGISTRYINDEX, cd->callback.ref);
}
else {
lua_getglobal (cd->L, cd->callback.name);
}
ptask = lua_newuserdata (cd->L, sizeof (struct rspamd_task *));
rspamd_lua_setclass (cd->L, "rspamd{task}", -1);
*ptask = task;
if (lua_pcall (cd->L, 1, LUA_MULTRET, 0) != 0) {
msg_info ("call to (%s)%s failed: %s", cd->symbol,
cd->cb_is_ref ? "local function" : cd->callback.name,
lua_tostring (cd->L, -1));
}
nresults = lua_gettop (cd->L) - level;
if (nresults >= 1) {
/* Function returned boolean, so maybe we need to insert result? */
gboolean res;
GList *opts = NULL;
gint i;
gdouble flag = 1.0;
if (lua_type (cd->L, level + 1) == LUA_TBOOLEAN) {
res = lua_toboolean (cd->L, level + 1);
if (res) {
gint first_opt = 2;
if (lua_type (cd->L, level + 2) == LUA_TNUMBER) {
flag = lua_tonumber (cd->L, level + 2);
/* Shift opt index */
first_opt = 3;
}
for (i = lua_gettop (cd->L); i >= level + first_opt; i --) {
if (lua_type (cd->L, i) == LUA_TSTRING) {
const char *opt = lua_tostring (cd->L, i);
opts = g_list_prepend (opts,
rspamd_mempool_strdup (task->task_pool, opt));
}
}
rspamd_task_insert_result (task, cd->symbol, flag, opts);
}
}
lua_pop (cd->L, nresults);
}
}
static void
rspamd_register_symbol_fromlua (lua_State *L,
struct rspamd_config *cfg,
const gchar *name,
gint ref,
gdouble weight,
gint priority,
enum rspamd_symbol_type type)
{
struct lua_callback_data *cd;
if (name) {
cd = rspamd_mempool_alloc0 (cfg->cfg_pool,
sizeof (struct lua_callback_data));
cd->cb_is_ref = TRUE;
cd->callback.ref = ref;
cd->L = L;
cd->symbol = rspamd_mempool_strdup (cfg->cfg_pool, name);
rspamd_symbols_cache_add_symbol (cfg->cache,
name,
weight,
priority,
lua_metric_symbol_callback,
cd,
type);
rspamd_mempool_add_destructor (cfg->cfg_pool,
(rspamd_mempool_destruct_t)lua_destroy_cfg_symbol,
cd);
}
}
static gint
lua_config_register_symbol (lua_State * L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
gchar *name;
double weight;
if (cfg) {
name = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2));
weight = luaL_checknumber (L, 3);
if (lua_type (L, 4) == LUA_TSTRING) {
lua_getglobal (L, luaL_checkstring (L, 4));
}
else {
lua_pushvalue (L, 4);
}
if (name) {
rspamd_register_symbol_fromlua (L,
cfg,
name,
luaL_ref (L, LUA_REGISTRYINDEX),
weight,
0,
SYMBOL_TYPE_NORMAL);
}
}
return 0;
}
static gint
lua_config_register_symbols (lua_State *L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
gint i, top, idx;
gchar *sym;
gdouble weight = 1.0;
if (lua_gettop (L) < 3) {
msg_err ("not enough arguments to register a function");
return 0;
}
if (cfg) {
if (lua_type (L, 2) == LUA_TSTRING) {
lua_getglobal (L, luaL_checkstring (L, 2));
}
else {
lua_pushvalue (L, 2);
}
idx = luaL_ref (L, LUA_REGISTRYINDEX);
if (lua_type (L, 3) == LUA_TNUMBER) {
weight = lua_tonumber (L, 3);
top = 4;
}
else {
top = 3;
}
sym = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, top ++));
rspamd_register_symbol_fromlua (L,
cfg,
sym,
idx,
weight,
0,
SYMBOL_TYPE_CALLBACK);
for (i = top; i <= lua_gettop (L); i++) {
if (lua_type (L, i) == LUA_TTABLE) {
lua_pushvalue (L, i);
lua_pushnil (L);
while (lua_next (L, -2)) {
lua_pushvalue (L, -2);
sym = rspamd_mempool_strdup (cfg->cfg_pool,
luaL_checkstring (L, -2));
rspamd_symbols_cache_add_symbol_virtual (cfg->cache, sym, weight);
lua_pop (L, 2);
}
lua_pop (L, 1);
}
else if (lua_type (L, i) == LUA_TSTRING) {
sym = rspamd_mempool_strdup (cfg->cfg_pool,
luaL_checkstring (L, i));
rspamd_symbols_cache_add_symbol_virtual (cfg->cache, sym, weight);
}
}
}
return 0;
}
static gint
lua_config_register_virtual_symbol (lua_State * L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
gchar *name;
double weight;
if (cfg) {
name = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2));
weight = luaL_checknumber (L, 3);
if (name) {
rspamd_symbols_cache_add_symbol_virtual (cfg->cache, name, weight);
}
}
return 0;
}
static gint
lua_config_register_callback_symbol (lua_State * L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
gchar *name;
double weight;
if (cfg) {
name = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2));
weight = luaL_checknumber (L, 3);
if (lua_type (L, 4) == LUA_TSTRING) {
lua_getglobal (L, luaL_checkstring (L, 4));
}
else {
lua_pushvalue (L, 4);
}
if (name) {
rspamd_register_symbol_fromlua (L,
cfg,
name,
luaL_ref (L, LUA_REGISTRYINDEX),
weight,
0,
SYMBOL_TYPE_CALLBACK);
}
}
return 0;
}
static gint
lua_config_register_callback_symbol_priority (lua_State * L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
gchar *name;
double weight;
gint priority;
if (cfg) {
name = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2));
weight = luaL_checknumber (L, 3);
priority = luaL_checknumber (L, 4);
if (lua_type (L, 5) == LUA_TSTRING) {
lua_getglobal (L, luaL_checkstring (L, 5));
}
else {
lua_pushvalue (L, 5);
}
if (name) {
rspamd_register_symbol_fromlua (L,
cfg,
name,
luaL_ref (L, LUA_REGISTRYINDEX),
weight,
priority,
SYMBOL_TYPE_CALLBACK);
}
}
return 0;
}
static gint
lua_config_set_metric_symbol (lua_State * L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
GList *metric_list;
gchar *name;
const gchar *metric_name = DEFAULT_METRIC, *description = NULL;
double weight;
struct rspamd_symbol_def *s;
struct metric *metric;
if (cfg) {
name = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2));
weight = luaL_checknumber (L, 3);
if (lua_gettop (L) > 3 && lua_type (L, 4) == LUA_TSTRING) {
description = luaL_checkstring (L, 4);
}
if (lua_gettop (L) > 4 && lua_type (L, 5) == LUA_TSTRING) {
metric_name = luaL_checkstring (L, 5);
}
metric = g_hash_table_lookup (cfg->metrics, metric_name);
if (metric == NULL) {
msg_err ("metric named %s is not defined", metric_name);
}
else if (name != NULL) {
s = g_hash_table_lookup (metric->symbols, name);
if (s == NULL) {
msg_debug ("set new symbol %s in metric %s with weight %.2f",
name, metric_name, weight);
s = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*s));
s->name = rspamd_mempool_strdup (cfg->cfg_pool, name);
s->weight_ptr = rspamd_mempool_alloc (cfg->cfg_pool,
sizeof (gdouble));
if (description != NULL) {
s->description = rspamd_mempool_strdup (cfg->cfg_pool,
description);
}
g_hash_table_insert (metric->symbols, s->name, s);
if ((metric_list =
g_hash_table_lookup (cfg->metrics_symbols, s->name)) == NULL) {
metric_list = g_list_prepend (NULL, metric);
rspamd_mempool_add_destructor (cfg->cfg_pool,
(rspamd_mempool_destruct_t)g_list_free,
metric_list);
g_hash_table_insert (cfg->metrics_symbols, s->name, metric_list);
}
else {
/* Slow but keep start element of list in safe */
if (!g_list_find (metric_list, metric)) {
metric_list = g_list_append (metric_list, metric);
}
}
}
*s->weight_ptr = weight;
}
}
return 0;
}
static gint
lua_config_add_composite (lua_State * L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
struct rspamd_expression *expr;
gchar *name;
const gchar *expr_str;
struct rspamd_composite *composite;
gboolean ret = FALSE, new = TRUE;
GError *err = NULL;
if (cfg) {
name = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2));
expr_str = luaL_checkstring (L, 3);
if (name && expr_str) {
if (!rspamd_parse_expression (expr_str, 0, &composite_expr_subr,
NULL, cfg->cfg_pool, &err, &expr)) {
msg_err ("cannot parse composite expression %s: %e", expr_str,
err);
g_error_free (err);
}
else {
if (g_hash_table_lookup (cfg->composite_symbols, name) != NULL) {
msg_warn ("composite %s is redefined", name);
new = FALSE;
}
composite = rspamd_mempool_alloc (cfg->cfg_pool,
sizeof (struct rspamd_composite));
composite->expr = expr;
composite->id = g_hash_table_size (cfg->composite_symbols);
g_hash_table_insert (cfg->composite_symbols,
(gpointer)name,
composite);
if (new) {
rspamd_symbols_cache_add_symbol_virtual (cfg->cache, name, 1);
}
ret = TRUE;
}
}
}
lua_pushboolean (L, ret);
return 1;
}
static gint
lua_config_newindex (lua_State *L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
const gchar *name;
name = luaL_checkstring (L, 2);
if (cfg != NULL && name != NULL && lua_gettop (L) > 2) {
if (lua_type (L, 3) == LUA_TFUNCTION) {
/* Normal symbol from just a function */
lua_pushvalue (L, 3);
rspamd_register_symbol_fromlua (L,
cfg,
name,
luaL_ref (L, LUA_REGISTRYINDEX),
1.0,
0,
SYMBOL_TYPE_NORMAL);
}
else if (lua_type (L, 3) == LUA_TTABLE) {
gint type = SYMBOL_TYPE_NORMAL, priority = 0, idx;
gdouble weight = 1.0;
const char *type_str;
/*
* Table can have the following attributes:
* "callback" - should be a callback function
* "weight" - optional weight
* "priority" - optional priority
* "type" - optional type (normal, virtual, callback)
*/
lua_pushstring (L, "callback");
lua_gettable (L, -2);
if (lua_type (L, -1) != LUA_TFUNCTION) {
lua_pop (L, 1);
msg_info ("cannot find callback definition for %s", name);
return 0;
}
idx = luaL_ref (L, LUA_REGISTRYINDEX);
/* Optional fields */
lua_pushstring (L, "weight");
lua_gettable (L, -2);
if (lua_type (L, -1) == LUA_TNUMBER) {
weight = lua_tonumber (L, -1);
}
lua_pop (L, 1);
lua_pushstring (L, "priority");
lua_gettable (L, -2);
if (lua_type (L, -1) == LUA_TNUMBER) {
priority = lua_tonumber (L, -1);
}
lua_pop (L, 1);
lua_pushstring (L, "type");
lua_gettable (L, -2);
if (lua_type (L, -1) == LUA_TSTRING) {
type_str = lua_tostring (L, -1);
if (strcmp (type_str, "normal") == 0) {
type = SYMBOL_TYPE_NORMAL;
}
else if (strcmp (type_str, "virtual") == 0) {
type = SYMBOL_TYPE_VIRTUAL;
}
else if (strcmp (type_str, "callback") == 0) {
type = SYMBOL_TYPE_CALLBACK;
}
else {
msg_info ("unknown type: %s", type_str);
}
}
lua_pop (L, 1);
rspamd_register_symbol_fromlua (L,
cfg,
name,
idx,
weight,
priority,
type);
}
}
return 0;
}
struct lua_map_callback_data {
lua_State *L;
gint ref;
GString *data;
};
static gchar *
lua_map_read (rspamd_mempool_t *pool, gchar *chunk, gint len,
struct map_cb_data *data)
{
struct lua_map_callback_data *cbdata, *old;
if (data->cur_data == NULL) {
cbdata = g_slice_alloc0 (sizeof (*cbdata));
old = (struct lua_map_callback_data *)data->prev_data;
cbdata->L = old->L;
cbdata->ref = old->ref;
data->cur_data = cbdata;
}
else {
cbdata = (struct lua_map_callback_data *)data->cur_data;
}
if (cbdata->data == NULL) {
cbdata->data = g_string_new_len (chunk, len);
}
else {
g_string_append_len (cbdata->data, chunk, len);
}
return NULL;
}
void
lua_map_fin (rspamd_mempool_t * pool, struct map_cb_data *data)
{
struct lua_map_callback_data *cbdata, *old;
if (data->prev_data) {
/* Cleanup old data */
old = (struct lua_map_callback_data *)data->prev_data;
if (old->data) {
g_string_free (old->data, TRUE);
}
g_slice_free1 (sizeof (*old), old);
data->prev_data = NULL;
}
if (data->cur_data) {
cbdata = (struct lua_map_callback_data *)data->cur_data;
}
else {
msg_err ("no data read for map");
return;
}
if (cbdata->data != NULL && cbdata->data->len != 0) {
lua_rawgeti (cbdata->L, LUA_REGISTRYINDEX, cbdata->ref);
lua_pushlstring (cbdata->L, cbdata->data->str, cbdata->data->len);
if (lua_pcall (cbdata->L, 1, 0, 0) != 0) {
msg_info ("call to %s failed: %s", "local function",
lua_tostring (cbdata->L, -1));
}
}
}
static gint
lua_config_add_map (lua_State *L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
const gchar *map_line, *description;
struct lua_map_callback_data *cbdata, **pcbdata;
int cbidx;
if (cfg) {
map_line = luaL_checkstring (L, 2);
if (lua_gettop (L) == 4) {
description = lua_tostring (L, 3);
cbidx = 4;
}
else {
description = NULL;
cbidx = 3;
}
if (lua_type (L, cbidx) == LUA_TFUNCTION) {
cbdata = g_slice_alloc (sizeof (*cbdata));
cbdata->L = L;
cbdata->data = NULL;
lua_pushvalue (L, cbidx);
/* Get a reference */
cbdata->ref = luaL_ref (L, LUA_REGISTRYINDEX);
pcbdata = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (cbdata));
*pcbdata = cbdata;
if (!rspamd_map_add (cfg, map_line, description, lua_map_read, lua_map_fin,
(void **)pcbdata)) {
msg_warn ("invalid hash map %s", map_line);
lua_pushboolean (L, false);
}
else {
lua_pushboolean (L, true);
}
}
else {
msg_warn ("invalid callback argument for map %s", map_line);
lua_pushboolean (L, false);
}
}
else {
lua_pushboolean (L, false);
}
return 1;
}
/* Radix and hash table functions */
static gint
lua_radix_get_key (lua_State * L)
{
radix_compressed_t *radix = lua_check_radix (L);
struct rspamd_lua_ip *addr = NULL;
gpointer ud;
guint32 key_num = 0;
gboolean ret = FALSE;
if (radix) {
if (lua_type (L, 2) == LUA_TNUMBER) {
key_num = htonl (luaL_checkint (L, 2));
}
else if (lua_type (L, 2) == LUA_TUSERDATA) {
ud = luaL_checkudata (L, 2, "rspamd{ip}");
if (ud != NULL) {
addr = *((struct rspamd_lua_ip **)ud);
if (addr->addr == NULL) {
msg_err ("rspamd{ip} is not valid");
addr = NULL;
}
}
else {
msg_err ("invalid userdata type provided, rspamd{ip} expected");
}
}
if (addr != NULL) {
if (radix_find_compressed_addr (radix, addr->addr)
!= RADIX_NO_VALUE) {
ret = TRUE;
}
}
else if (key_num != 0) {
if (radix_find_compressed (radix, (guint8 *)&key_num, sizeof (key_num))
!= RADIX_NO_VALUE) {
ret = TRUE;
}
}
}
lua_pushboolean (L, ret);
return 1;
}
static gint
lua_hash_table_get_key (lua_State * L)
{
GHashTable *tbl = lua_check_hash_table (L);
const gchar *key, *value;
if (tbl) {
key = luaL_checkstring (L, 2);
if ((value = g_hash_table_lookup (tbl, key)) != NULL) {
lua_pushstring (L, value);
return 1;
}
}
lua_pushnil (L);
return 1;
}
/* Trie functions */
/* Init functions */
void
luaopen_config (lua_State * L)
{
rspamd_lua_new_class (L, "rspamd{config}", configlib_m);
lua_pop (L, 1); /* remove metatable from stack */
}
void
luaopen_radix (lua_State * L)
{
rspamd_lua_new_class (L, "rspamd{radix}", radixlib_m);
lua_pop (L, 1); /* remove metatable from stack */
}
void
luaopen_hash_table (lua_State * L)
{
rspamd_lua_new_class (L, "rspamd{hash_table}", hashlib_m);
luaL_register (L, "rspamd_hash_table", null_reg);
lua_pop (L, 1); /* remove metatable from stack */
}
|