From: Vsevolod Stakhov Date: Thu, 4 Aug 2016 09:42:33 +0000 (+0100) Subject: [Fix] Parse CGP envelope data X-Git-Tag: 1.3.2~56 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=af7255bac4318ec96b0b36a57ea87ea893ba7b02;p=rspamd.git [Fix] Parse CGP envelope data --- diff --git a/utils/cgp_rspamd.pl b/utils/cgp_rspamd.pl index f9e0bb4d7..482bbd3bb 100644 --- a/utils/cgp_rspamd.pl +++ b/utils/cgp_rspamd.pl @@ -20,7 +20,6 @@ my $reject_message = "Spam message rejected"; GetOptions( "host=s" => \$rspamd_host, "header=s" => \$header, - "local" => \$local, "reject-message=s" => \$reject_message, "help|?" => \$help, "man" => \$man @@ -100,13 +99,51 @@ sub rspamd_scan { if ($local) { # Use file scan + # XXX: not implemented now due to CGP queue format http_get("http://$rspamd_host/symbols?file=$file", $http_callback); } else { aio_load($file, sub { my ($data) = @_ or return print "* Cannot open $file: $!\n$tag FAILURE\n"; - http_post("http://$rspamd_host/symbols", $data, $http_callback); + # Parse CGP format + $data =~ s/^((?:[^\n]*\n)*?)\n(.*)$/$2/ms; + my @envelope = split /\n/, $1; + chomp(@envelope); + my $from; + my @rcpts; + my $ip; + + foreach my $elt (@envelope) { + if ($elt =~ /^P\s[^<]*(<[^>]*>).*$/) { + $from = $1; + } + elsif ($elt =~ /^R\s[^<]*(<[^>]*>).*$/) { + push @rcpts, $1; + } + elsif ($elt =~ /^S .*\[(.+)\]/) { + $ip = $1; + } + } + + my $headers = {}; + if ($file =~ /\/([^\/.]+)\.msg$/) { + $headers->{'Queue-ID'} = $1; + } + if ($from) { + $headers->{From} = $from; + } + if (scalar(@rcpts) > 0) { + # XXX: Anyevent cannot parse headers with multiple values + foreach (@rcpts) { + $headers->{Rcpt} = $_; + } + } + if ($ip) { + $headers->{IP} = $ip; + } + + http_post("http://$rspamd_host/symbols", $data, headers => $headers, $http_callback); }); } } @@ -154,7 +191,6 @@ cgp_rspamd [options] Options: --host=hostport Rspamd host to connect (localhost:11333 by default) - --local Rspamd runs locally and can access CGP files (false by default) --header Add specific header for a spam message ("X-Spam: yes" by default) --reject-message Rejection message for spam mail ("Spam message rejected" by default) --help brief help message @@ -168,10 +204,6 @@ cgp_rspamd [options] Specifies Rspamd host to use for scanning -=item B<--local> - -Should be used if Rspamd runs on the same machine and can access CGP files - =item B<--header> Specifies the header that should be added when Rspamd action is B