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";
=cut
sub process_path {
my $self = shift;
+ my $cb = shift;
my %res;
+
foreach (@_) {
- $res{$_} = $self->process_item($_);
+ $res{$_} = $self->process_item($_, $cb);
}
return \%res;
sub _process_file {
my $self = shift;
my $file = shift;
+ my $cb = shift;
+ my $res;
open(FILE, "< $file") or return;
}
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);
}
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 {
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 ++;
}
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',
}
}
+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;
}
}
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);
}