From e09d4f6ceaa808cef9ce965804db98f032b20a83 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 22 Apr 2010 18:12:14 +0400 Subject: [PATCH] * Implement config object dumping to xml --- perl/lib/Mail/Rspamd/Config.pm | 184 +++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) diff --git a/perl/lib/Mail/Rspamd/Config.pm b/perl/lib/Mail/Rspamd/Config.pm index 13acc66a0..c468a3170 100644 --- a/perl/lib/Mail/Rspamd/Config.pm +++ b/perl/lib/Mail/Rspamd/Config.pm @@ -29,6 +29,7 @@ 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_MODULES => 10; use constant PARSER_STATE_END => -1; @@ -56,6 +57,9 @@ sub new { options => {}, variables => {}, logging => {}, + lua => [], + composites => {}, + paths => [], parser_state => { state => PARSER_STATE_START, valid => 1, @@ -100,6 +104,30 @@ sub load { $parser->parsefile($self->{file}); } +=head2 save + +public save (String $file) + +Description: +Dumps rspamd config to xml file. + +=cut + +sub save { + my ($self, $file) = @_; + + if (defined ($file)) { + $self->{'file'} = $file; + } + + if (!defined ($self->{'file'})) { + carp 'cannot open file specified'; + return undef; + } + + $self->_dump(); +} + =head2 _handle_start_element private _handle_start_element($parser, $element, [attr, value...]) @@ -156,6 +184,15 @@ sub _handle_start_element { elsif ($lce eq 'variable') { $self->_get_attr('name', 'name', 1, @attrs); } + elsif ($lce eq 'lua') { + $self->_get_attr('src', 'src', 1, @attrs); + } + elsif ($lce eq 'composite') { + $self->_get_attr('name', 'name', 1, @attrs); + } + elsif ($lce eq 'modules') { + $self->{parser_state}->{state} = PARSER_STATE_MODULES; + } else { # Other element $self->{parser_state}->{element} = $lce; @@ -264,6 +301,11 @@ sub _handle_end_element { $self->{parser_state}->{state} = PARSER_STATE_MAIN; } } + elsif ($self->{parser_state}->{state} == PARSER_STATE_FACTORS) { + if ($lce eq 'modules') { + $self->{parser_state}->{state} = PARSER_STATE_MAIN; + } + } } } @@ -295,6 +337,12 @@ sub _handle_text { if ($self->{parser_state}->{element} eq 'variable') { $self->{variables}->{ $self->{parser_state}->{name} } = $data; } + elsif ($self->{parser_state}->{element} eq 'composite') { + $self->{composites}->{ $self->{parser_state}->{name} } = $data; + } + elsif ($self->{parser_state}->{element} eq 'lua') { + push(@{$self->{lua}}, $self->{parser_state}->{src}); + } else { $self->{options}->{ $self->{parser_state}->{element} } = $data; } @@ -322,6 +370,9 @@ sub _handle_text { elsif ($self->{parser_state}->{state} == PARSER_STATE_FACTORS) { $self->{factors}->{ $self->{parser_state}->{name} } = $data; } + elsif ($self->{parser_state}->{state} == PARSER_STATE_MODULES) { + push(@{$self->{paths}}, $data); + } elsif ($self->{parser_state}->{state} == PARSER_STATE_METRIC) { $self->{parser_state}->{metric}->{ $self->{parser_state}->{element} } = $data; } @@ -364,3 +415,136 @@ sub _get_attr { $self->{parser_state}->{'valid'} = 0; } } + +=head2 _dump + +private _dump() + +Description: +Dumps rspamd config to xml file + +=cut +sub _dump { + my ($self) = @_; + + open(XML, "> $self->{file}") or carp "cannot open file '$self->file'"; + + print XML "\n\n"; + + print XML "\n"; + while(my ($k, $v) = each (%{$self->{options}})) { + my $ek = $self->_xml_escape($k); + print XML "<$ek>" . $self->_xml_escape($v) . "\n"; + } + foreach my $lua(@{$self->{lua}}) { + print XML "_xml_escape($lua) ."\">lua\n"; + } + print XML "\n\n"; + + print XML "\n"; + while(my ($k, $v) = each (%{$self->{variables}})) { + my $ek = $self->_xml_escape($k); + print XML "" . $self->_xml_escape($v) . "\n"; + } + print XML "\n\n"; + + print XML "\n"; + while(my ($k, $v) = each (%{$self->{composites}})) { + my $ek = $self->_xml_escape($k); + print XML "" . $self->_xml_escape($v) . "\n"; + } + print XML "\n\n"; + + print XML "\n"; + foreach my $worker (@{$self->{workers}}) { + print XML "\n"; + while (my ($k, $v) = each (%{$worker})) { + my $ek = $self->_xml_escape($k); + if ($k eq 'options') { + while (my ($kk, $vv) = each (%{$v})) { + print XML " _xml_escape($kk) ."\">" . $self->_xml_escape($vv) . "\n"; + } + } + else { + print XML " <$ek>" . $self->_xml_escape($v) . "\n"; + } + } + print XML "\n"; + } + print XML "\n\n"; + + print XML "\n\n"; + while (my ($k, $v) = each (%{$self->{factors}})) { + print XML " _xml_escape($k) ."\">" . $self->_xml_escape($v) . "\n"; + } + print XML "\n\n\n"; + + print XML "\n\n"; + while (my ($k, $v) = each (%{$self->{logging}})) { + my $ek = $self->_xml_escape($k); + print XML " <$ek>" . $self->_xml_escape($v) . "\n"; + } + print XML "\n\n\n"; + + print XML "\n"; + while (my ($type, $classifier) = each(%{$self->{classifiers}})) { + print XML "_xml_escape($type) ."\">\n"; + while (my ($k, $v) = each (%{$classifier})) { + my $ek = $self->_xml_escape($k); + if ($k eq 'statfiles') { + foreach my $statfile (@{$v}) { + print XML " \n"; + while (my ($kk, $vv) = each (%{$statfile})) { + my $ekk = $self->_xml_escape($kk); + print XML " <$ekk>" . $self->_xml_escape($vv) . "\n"; + } + print XML " \n"; + } + } + else { + print XML " <$ek>" . $self->_xml_escape($v) . "\n"; + } + } + print XML "\n"; + } + print XML "\n\n"; + + print XML "\n"; + while (my ($name, $module) = each(%{$self->{modules}})) { + print XML "_xml_escape($name) ."\">\n"; + while (my ($k, $v) = each (%{$module})) { + my $ek = $self->_xml_escape($k); + print XML " \n"; + } + print XML "\n"; + } + print XML "\n\n"; + + print XML "\n\n"; + foreach my $module(@{$self->{paths}}) { + print XML " " . $self->_xml_escape($module) . "\n"; + } + print XML "\n\n\n"; + + print XML "\n"; +} + +=head2 _xml_escape + +private _xml_escape() + +Description: +Escapes characters in xml string + +=cut +sub _xml_escape { + my $data = $_[1]; + if ($data =~ /[\&\<\>\"]/) { + $data =~ s/\&/\&\;/g; + $data =~ s/\/\>\;/g; + $data =~ s/\"/\"\;/g; + } + return $data; +} + -- 2.39.5