1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
package SpamCat;
use Carp;
use Data::Dumper;
use DBI;
use Email::Simple;
use IO::File;
use strict;
use warnings;
our $VERSION = '0';
my $log = sub {
print STDERR "DEBUG: " . join(', ', @_) . "\n";
};
sub new {
my($class, %conf) = @_;
my $dbh = DBI->connect("dbi:SQLite:dbname=$conf{dbpath}", '', '');
$conf{dbh} = $dbh;
bless \%conf, $class;
}
sub deliver {
my ($self) = @_;
local $/;
my $email = Email::Simple->new(<>);
my $email_to = $email->header('To');
my @to_addrs = $self->parse_to(split /,\s*/, $email_to);
my $count;
foreach my $to_addr (@to_addrs) {
foreach my $domain (@{$self->{domains}}) {
if ($to_addr =~ /(.*)\@$domain/) {
my $sender = $1;
my $c = $self->decrement_count($sender);
if (!defined $count || $c > $count) {
$count = $c;
}
}
}
}
if (defined $count) {
return if $count == 0;
my $count_str = '[' . $count . '/' . $self->{default_count} . ']';
my $new_subject = $email->header('Subject');
if ($new_subject) {
$new_subject .= ' - ' . $count_str;
} else {
$new_subject = $count_str;
}
$email->header_set('Subject' => $new_subject);
}
my $deliverfh = IO::File->new("| " . $self->{deliver}) ||
die "Couldn't open pipe to " . $self->{deliver} . ": $!\n";
print $deliverfh $email->as_string;
$deliverfh->close;
}
sub parse_to {
my ($self, @tovals) = @_;
map {
if ($_ =~ /<(.*)>/) {
$1;
} else {
$_;
}
} @tovals;
}
sub get_count {
my ($self, $sender) = @_;
$self->in_transaction(sub { $self->get_count_t($sender) });
}
sub set_count {
my ($self, $sender, $count) = @_;
$self->in_transaction(sub { $self->set_count_t($sender, $count); });
}
sub decrement_count {
my ($self, $sender) = @_;
$self->in_transaction(sub { $self->decrement_count_t($sender); });
}
#
# The _t functions are meant to be run inside transacitons.
#
sub get_count_t {
my ($self, $sender) = @_;
my $q = 'SELECT count FROM emails WHERE sender=?';
my $sth = $self->{dbh}->prepare($q);
$sth->execute($sender);
my @row = $sth->fetchrow_array;
$sth->finish;
$row[0];
}
sub set_count_t {
my ($self, $sender, $count) = @_;
my $q;
if (!defined $self->get_count_t($sender)) {
# Insert when there's no count.
$q = 'INSERT INTO emails (count, sender) VALUES (?, ?)';
} else {
# Otherwise update the record with the new count.
$q = 'UPDATE emails SET count = ? modified = CURRENT_TIMESTAMP WHERE sender = ?'
}
my $sth = $self->{dbh}->prepare($q);
$sth->execute($count, $sender);
$sth->finish;
$count;
}
sub decrement_count_t {
my ($self, $sender) = @_;
my $q;
my $count = $self->get_count_t($sender);
if (!defined $count) {
$count = $self->{default_count};
$q = 'INSERT INTO emails (count, sender) VALUES (?, ?)';
} else {
$count = $count <= 0 ? '0' : $count - 1;
$q = "UPDATE emails SET count = ?, modified = CURRENT_TIMESTAMP WHERE sender = ?";
}
my $sth = $self->{dbh}->prepare($q);
$sth->execute($count, $sender);
$sth->finish;
$count;
}
sub in_transaction {
my ($self, $sub) = @_;
$self->{dbh}->begin_work;
my $rc = eval { &$sub($self); };
if ($@) {
$self->{dbh}->rollback;
warn "ERROR: Transaction failed: $@\n";
exit 1;
} else {
$self->{dbh}->commit;
}
$rc;
}
1;
|