aboutsummaryrefslogtreecommitdiffstats
path: root/lib/SpamCat.pm
blob: 282ac7c6523bfc09db796a214d97c45c50d17fd1 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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 filter {
  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;
	}
      }
    }
  }

  #
  # Negative counts indicate unlimited delivery.
  #
  if (defined $count && $count >= 0) {
      my $count_str = '[' . ($self->{default_count} - $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);
      $email->header_set('X-SpamCat-Remaining' => $count);
  }

  print $email->as_string;
}

sub parse_to {
  my ($self, @tovals) = @_;

  map {
    if ($_ =~ /<(.*)>/) {
      $1;
    } else {
      $_;
    }
  } @tovals;
}

sub get_table {
    my ($self) = @_;

    $self->in_transaction(sub { $self->get_table_t() });
}

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_table_t {
    my ($self) = @_;

    my @rows;
    my $q = 'SELECT * FROM emails';
    my $sth = $self->{dbh}->prepare($q);
    $sth->execute();
    while (my $row = $sth->fetchrow_hashref) {
	push @rows, $row
    }
    if ($sth->err) {
	$sth->finish;
	carp $sth->errstr;
	return;
    }
    $sth->finish;

    \@rows;
}

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} - 1;
    $q = 'INSERT INTO emails (count, sender) VALUES (?, ?)';
  } elsif ($count <= 0) {
    return $count;
  } else {
    $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 in_transaction {
  my ($self, $sub) = @_;

  $self->{dbh}->begin_work;
  my $rc = eval { &$sub($self); };
  if ($@) {
    $self->{dbh}->rollback;
    carp "Transaction failed: $@\n";
  } else {
    $self->{dbh}->commit;
  }

  $rc;
}

1;