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
|
=head1 NAME
Mail::Rspamd::Config - Utilities for rspamd configuration
=head1 SYNOPSIS
=head1 DESCRIPTION
Mail::Rspamd::Config is a module that provides a perl implementation for
configuring rspamd.
=cut
package Mail::Rspamd::Config;
use Carp;
use XML::Parser;
use vars qw($VERSION);
$VERSION = "1.02";
use constant PARSER_STATE_START => 0;
use constant PARSER_STATE_MAIN => 1;
use constant PARSER_STATE_WORKER => 2;
use constant PARSER_STATE_MODULE => 3;
use constant PARSER_STATE_CLASSIFIER => 4;
use constant PARSER_STATE_STATFILE => 5;
use constant PARSER_STATE_LOGGING => 6;
use constant PARSER_STATE_FACTORS => 7;
use constant PARSER_STATE_METRIC => 8;
use constant PARSER_STATE_VIEW => 9;
use constant PARSER_STATE_END => -1;
=head1 PUBLIC METHODS
=head2 new
public class (Mail::Rspamd::Config) new (\% $args)
Description:
This method creates a new Mail::Rspamd::Config object.
=cut
sub new {
my ($class, $args) = @_;
$class = ref($class) || $class;
my $self = {
workers => [],
modules => {},
classifiers => {},
factors => {},
options => {},
variables => {},
logging => {},
parser_state => {
state => PARSER_STATE_START,
valid => 1,
},
};
if (defined ($args->{'file'})) {
$self->{'file'} = $args->{'file'}
}
bless($self, $class);
$self;
}
=head2 load
public load (String $file)
Description:
Loads rspamd config file and parses it.
=cut
sub load {
my ($self, $file) = @_;
if (defined ($file)) {
$self->{'file'} = $file;
}
if (!defined ($self->{'file'}) || ! -f $self->{'file'}) {
carp 'cannot open file specified';
return undef;
}
my $parser = new XML::Parser(Handlers => {Start => sub { $self->_handle_start_element(@_) },
End => sub { $self->_handle_end_element(@_) },
Char => sub { $self->_handle_text(@_) } });
$parser->parsefile($self->{file});
}
=head2 _handle_start_element
private _handle_start_element($parser, $element, [attr, value...])
Description:
Handle start xml tag of rspamd
=cut
sub _handle_start_element {
my ($self, $parser, $element, @attrs) = @_;
if ($self->{parser_state}->{valid}) {
# Start element
$self->{parser_state}->{element} = lc $element;
if ($self->{parser_state}->{state} == PARSER_STATE_START) {
if (lc $element eq 'rspamd') {
$self->{parser_state}->{state} = PARSER_STATE_MAIN;
}
else {
$self->{parser_state}->{valid} = 0;
$self->{error} = 'Start element missing, it must be <rspamd>, but is <' . $element . '>';
}
}
# Main section
elsif ($self->{parser_state}->{state} == PARSER_STATE_MAIN) {
my $lce = lc $element;
if ($lce eq 'logging') {
$self->{parser_state}->{state} = PARSER_STATE_LOGGING;
}
elsif ($lce eq 'worker') {
$self->{parser_state}->{state} = PARSER_STATE_WORKER;
$self->{parser_state}->{worker} = { options => {} };
}
elsif ($lce eq 'metric') {
$self->parser_state->state = PARSER_STATE_METRIC;
$self->_get_attr('name', 'name', 1, @attrs);
$self->{parser_state}->{metric} = {};
}
elsif ($lce eq 'module') {
$self->{parser_state}->{state} = PARSER_STATE_MODULE;
$self->_get_attr('name', 'name', 1, @attrs);
$self->{parser_state}->{module} = {};
}
elsif ($lce eq 'classifier') {
$self->{parser_state}->{state} = PARSER_STATE_CLASSIFIER;
$self->_get_attr('type', 'type', 1, @attrs);
$self->{parser_state}->{classifier} = { statfiles => []};
}
elsif ($lce eq 'factors') {
$self->{parser_state}->{state} = PARSER_STATE_FACTORS;
}
elsif ($lce eq 'variable') {
$self->_get_attr('name', 'name', 1, @attrs);
}
else {
# Other element
$self->{parser_state}->{element} = $lce;
}
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_MODULE) {
my $lce = lc $element;
if ($lce eq 'option') {
$self->_get_attr('name', 'option', 1, @attrs);
}
else {
$self->{parser_state}->{valid} = 0;
$self->{error} = 'Invalid tag <' . $lce . '> in module section';
}
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_FACTORS) {
my $lce = lc $element;
if ($lce eq 'factor') {
$self->_get_attr('name', 'name', 1, @attrs);
}
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_CLASSIFIER) {
my $lce = lc $element;
if ($lce eq 'statfile') {
$self->{parser_state}->{state} = PARSER_STATE_STATFILE;
}
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_WORKER) {
my $lce = lc $element;
if ($lce eq 'param') {
$self->_get_attr('name', 'name', 1, @attrs);
}
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_END) {
# Tags after end element
$self->{parser_state}->{valid} = 0;
$self->{error} = 'Invalid tag <' . $element . '> after end tag';
}
else {
# On other states just set element
}
}
}
=head2 _handle_end_element
private _handle_end_element($parser, $element)
Description:
Handle end xml tag of rspamd
=cut
sub _handle_end_element {
my ($self, $parser, $element) = @_;
if ($self->{parser_state}->{valid}) {
my $lce = lc $element;
if ($self->{parser_state}->{state} == PARSER_STATE_MAIN) {
if ($lce eq 'rspamd') {
$self->{parser_state}->{state} = PARSER_STATE_END;
}
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_WORKER) {
if ($lce eq 'worker') {
push(@{$self->{workers}}, $self->{parser_state}->{worker});
$self->{parser_state}->{state} = PARSER_STATE_MAIN;
$self->{parser_state}->{worker} = undef;
}
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_CLASSIFIER) {
if ($lce eq 'classifier') {
$self->{classifiers}->{ $self->{parser_state}->{type} } = $self->{parser_state}->{classifier};
$self->{parser_state}->{state} = PARSER_STATE_MAIN;
$self->{parser_state}->{classifier} = undef;
}
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_METRIC) {
if ($lce eq 'metric') {
$self->{metrics}->{ $self->{parser_state}->{name} } = $self->{parser_state}->{metric};
$self->{parser_state}->{state} = PARSER_STATE_MAIN;
$self->{parser_state}->{metric} = undef;
}
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_STATFILE) {
if ($lce eq 'statfile') {
push(@{$self->{parser_state}->{classifier}->{statfiles}}, $self->{parser_state}->{statfile});
$self->{parser_state}->{state} = PARSER_STATE_CLASSIFIER;
$self->{parser_state}->{statfile} = undef;
}
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_MODULE) {
if ($lce eq 'module') {
$self->{modules}->{ $self->{parser_state}->{name} } = $self->{parser_state}->{module};
$self->{parser_state}->{state} = PARSER_STATE_MAIN;
$self->{parser_state}->{module} = undef;
}
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_LOGGING) {
if ($lce eq 'logging') {
$self->{parser_state}->{state} = PARSER_STATE_MAIN;
}
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_FACTORS) {
if ($lce eq 'factors') {
$self->{parser_state}->{state} = PARSER_STATE_MAIN;
}
}
}
}
=head2 _handle_text
private _handle_text($parser, $string)
Description:
Handle data of xml tag
=cut
sub _handle_text {
my ($self, $parser, $string) = @_;
my $data;
if (defined ($string) && $string =~ /^\s*(\S*(?:\s+\S+)*)\s*$/) {
$data = $1;
}
else {
return undef;
}
if (!$data) {
return undef;
}
if ($self->{parser_state}->{valid}) {
if ($self->{parser_state}->{state} == PARSER_STATE_MAIN) {
if ($self->{parser_state}->{element} eq 'variable') {
$self->{variables}->{ $self->{parser_state}->{name} } = $data;
}
else {
$self->{options}->{ $self->{parser_state}->{element} } = $data;
}
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_LOGGING) {
$self->{logging}->{ $self->{parser_state}->{element} } = $data;
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_WORKER) {
if ($self->{parser_state}->{element} eq 'param') {
$self->{parser_state}->{worker}->{options}->{$self->{parser_state}->{name}} = $data;
}
else {
$self->{parser_state}->{worker}->{ $self->{parser_state}->{element} } = $data;
}
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_CLASSIFIER) {
$self->{parser_state}->{classifier}->{ $self->{parser_state}->{element} } = $data;
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_STATFILE) {
$self->{parser_state}->{statfile}->{ $self->{parser_state}->{element} } = $data;
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_MODULE) {
$self->{parser_state}->{module}->{ $self->{parser_state}->{option} } = $data;
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_FACTORS) {
$self->{factors}->{ $self->{parser_state}->{name} } = $data;
}
elsif ($self->{parser_state}->{state} == PARSER_STATE_METRIC) {
$self->{parser_state}->{metric}->{ $self->{parser_state}->{element} } = $data;
}
}
}
=head2 _get_attr
private _get_attr($name, $hash_name, $required, @attrs)
Description:
Extract specified attr and put it to parser_state
=cut
sub _get_attr {
my ($self, $name, $hash_name, $required, @attrs) = @_;
my $found = 0;
my $param = 1;
foreach (@attrs) {
if ($found) {
$self->{parser_state}->{$hash_name} = $_;
last;
}
else {
if ($param) {
if (lc $_ eq $name) {
$found = 1;
}
$param = 0;
}
else {
$param = 1;
}
}
}
if (!$found && $required) {
$self->{error} = "Attribute '$name' is required for tag '$self->{parser_state}->{element}'";
$self->{parser_state}->{'valid'} = 0;
}
}
|