1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Default spam filter class for wrapping spamc(1)
5 package PublicInbox::Spamcheck::Spamc;
8 use PublicInbox::Spawn qw(popen_rd spawn);
10 use Fcntl qw(SEEK_SET);
15 checkcmd => [qw(spamc -E --headers)],
16 hamcmd => [qw(spamc -L ham)],
17 spamcmd => [qw(spamc -L spam)],
22 my ($self, $msg, $out) = @_;
24 my $rdr = { 0 => _msg_to_fh($self, $msg) };
25 my ($fh, $pid) = popen_rd($self->{checkcmd}, undef, $rdr);
33 $r = sysread($fh, $$out, 65536, length($$out));
34 } while (defined($r) && $r != 0);
36 goto again if $!{EINTR};
37 die "read failed: $!";
39 close $fh or die "close failed: $!";
41 ($? || $$out eq '') ? 0 : 1;
45 my ($self, $msg, $rdr) = @_;
46 _learn($self, $msg, $rdr, 'hamcmd');
50 my ($self, $msg, $rdr) = @_;
51 _learn($self, $msg, $rdr, 'spamcmd');
55 my ($self, $msg, $rdr, $field) = @_;
57 $rdr->{0} = _msg_to_fh($self, $msg);
58 $rdr->{1} ||= $self->_devnull;
59 $rdr->{2} ||= $self->_devnull;
60 my $pid = spawn($self->{$field}, undef, $rdr);
67 $self->{-devnull} //= do {
68 open my $fh, '+>', '/dev/null' or
69 die "failed to open /dev/null: $!";
75 my ($self, $msg) = @_;
76 if (my $ref = ref($msg)) {
77 my $fd = eval { fileno($msg) };
78 return $msg if defined($fd) && $fd >= 0;
80 open(my $tmpfh, '+>', undef) or die "failed to open: $!";
82 $msg = \($msg->as_string) if $ref ne 'SCALAR';
83 print $tmpfh $$msg or die "failed to print: $!";
84 sysseek($tmpfh, 0, SEEK_SET) or
85 die "sysseek(fh) failed: $!";