summaryrefslogtreecommitdiffstats
path: root/server.pl
diff options
context:
space:
mode:
Diffstat (limited to 'server.pl')
-rwxr-xr-xserver.pl59
1 files changed, 59 insertions, 0 deletions
diff --git a/server.pl b/server.pl
new file mode 100755
index 0000000..e56b0b4
--- /dev/null
+++ b/server.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/perl -w
+
+use strict;
+use AnyEvent;
+use AnyEvent::Handle;
+use AnyEvent::Socket;
+use Storable;
+
+my $stats = {
+ ham => 0,
+ spam => 0,
+ block => 0,
+};
+
+my $log_socket = '/tmp/log.sock';
+my $stats_socket = '/tmp/stats.sock';
+
+# regex to match the spamd log lines
+my $regex = '^[A-Z][a-z][a-z] ?\d{1,2} \d{2}:\d{2}:\d{2} vm-mail spamd\[\d+\]: spamd: result: ([Y.]) (-?\d+) ';
+
+tcp_server 'unix/', $log_socket, sub {
+ my $h; $h = new AnyEvent::Handle
+ fh => @_,
+ on_eof => sub { $h->destroy; },
+ on_error => sub { $h->destroy; },
+ on_read => sub {
+ $h->push_read(line => sub {
+ my ($fh, $line) = @_;
+
+ if ($line =~ m/$regex/) {
+ if ($1 eq '.') {
+ $stats->{ham}++;
+ }
+ elsif ($1 eq 'Y') {
+ if ($2 > 10) {
+ $stats->{block}++;
+ }
+ else {
+ $stats->{spam}++;
+ }
+ }
+ }
+ })
+ };
+};
+
+tcp_server 'unix/', $stats_socket, sub {
+ my $h; $h = new AnyEvent::Handle
+ fh => @_,
+ on_eof => sub { $h->destroy; },
+ on_error => sub { $h->destroy; };
+
+ $h->push_write(Storable::freeze($stats) . "\n");
+ $stats = { ham => 0, spam => 0, block => 0 };
+ $h->destroy;
+ undef $h;
+};
+
+AnyEvent->condvar->recv;