From 10a3c7a278b569bc978614f3b6e78d29f10a87e0 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 2 Jul 2010 18:28:10 +0400 Subject: [PATCH] * Take callback argument in Mail::Rspamd::Client for processing files and directories * Print results if rspamc is called for a directory --- perl/lib/Mail/Rspamd/Client.pm | 27 ++++++++++++++++++++------- rspamc.pl.in | 31 ++++++++++++++++++------------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/perl/lib/Mail/Rspamd/Client.pm b/perl/lib/Mail/Rspamd/Client.pm index ad2ab7a86..ca90b7421 100644 --- a/perl/lib/Mail/Rspamd/Client.pm +++ b/perl/lib/Mail/Rspamd/Client.pm @@ -150,16 +150,17 @@ The return value is a hash reference containing results of each command for each sub process_item { my $self = shift; my $item = shift; + my $cb = shift; if (defined ($item)) { if ($item =~ qr|^imap(s?):user:([^:]+):password:([^:]*):host:([^:]+):mbox:(.+)$|) { - return $self->_process_imap ($1, $2, $3, $4, $5); + return $self->_process_imap ($1, $2, $3, $4, $5, $cb); } elsif (-f $item) { - return $self->_process_file ($item); + return $self->_process_file ($item, $cb); } elsif (-d $item) { - return $self->_process_directory ($item); + return $self->_process_directory ($item, $cb); } else { warn "urecognized argument: $item"; @@ -180,9 +181,11 @@ The return value is a hash reference containing results of each command for each =cut sub process_path { my $self = shift; + my $cb = shift; my %res; + foreach (@_) { - $res{$_} = $self->process_item($_); + $res{$_} = $self->process_item($_, $cb); } return \%res; @@ -1167,6 +1170,8 @@ sub _do_control_command { sub _process_file { my $self = shift; my $file = shift; + my $cb = shift; + my $res; open(FILE, "< $file") or return; @@ -1176,19 +1181,23 @@ sub _process_file { } close FILE; - $self->do_all_cmd ($input); + $res = $self->do_all_cmd ($input); + if (defined ($cb) && $res) { + $cb->($file, $res); + } } sub _process_directory { my $self = shift; my $dir = shift; + my $cb = shift; opendir (DIR, $dir) or return; while (defined (my $file = readdir (DIR))) { $file = "$dir/$file"; if (-f $file) { - $self->_process_file ($file); + $self->_process_file ($file, $cb); } } closedir (DIR); @@ -1269,9 +1278,10 @@ sub _parse_imap_sequences { } sub _process_imap { - my ($self, $ssl, $user, $password, $host, $mbox) = @_; + my ($self, $ssl, $user, $password, $host, $mbox, $cb) = @_; my $seq = 1; my $sock; + my $res; if (!$password) { eval { @@ -1329,6 +1339,9 @@ sub _process_imap { syswrite $sock, "$seq FETCH $message body[]$EOL"; if (defined (my $input = $self->_parse_imap_body ($sock, $seq))) { $self->do_all_cmd ($input); + if (defined ($cb) && $res) { + $cb->($seq, $res); + } } $seq ++; } diff --git a/rspamc.pl.in b/rspamc.pl.in index 077832438..a71e061f1 100755 --- a/rspamc.pl.in +++ b/rspamc.pl.in @@ -10,6 +10,7 @@ use Socket qw(:DEFAULT :crlf); use Term::Cap; use Mail::Rspamd::Client; +use Data::Dumper; my %cfg = ( 'conf_file' => '@CMAKE_INSTALL_PREFIX@/etc/rspamd.conf', @@ -186,6 +187,22 @@ sub print_rspamc_result { } } +sub print_item_result { + my ($file, $res) = @_; + $terminal->Tputs( 'md', 1, *STDOUT ); + print "Results for item $file:\n\n"; + $terminal->Tputs( 'me', 1, *STDOUT ); + + while (my ($host, $result) = each (%{ $res })) { + if ($cfg{control}) { + print_control_result ($host, $result); + } + else { + print_rspamc_result ($host, $result); + } + } + +} ############################# Main part ########################################### my %args; @@ -330,17 +347,5 @@ if (!defined ($path[0]) || ! $cfg{'require_input'}) { } } else { - my $res = $rspamd->process_path (@path); - - while (my ($item, $result) = each (%{ $res })) { - print "Results for item $item:\n"; - while (my ($host, $r) = each (%{ $result })) { - if ($cfg{control}) { - print_control_result ($host, $r); - } - else { - print_rspamc_result ($host, $r); - } - } - } + $rspamd->process_path (\&print_item_result, @path); } -- 2.39.5