about summary refs log tree commit homepage
path: root/lib/PublicInbox/Spamcheck/Spamc.pm
blob: a76e920f0210221a6345528c2c8fd8998033789f (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
# Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
package PublicInbox::Spamcheck::Spamc;
use strict;
use warnings;
use PublicInbox::Spawn qw(popen_rd spawn);
use IO::Handle;
use Fcntl qw(:DEFAULT SEEK_SET);

sub new {
	my ($class) = @_;
	bless {
		checkcmd => [qw(spamc -E --headers)],
		hamcmd => [qw(spamc -L ham)],
		spamcmd => [qw(spamc -L spam)],
	}, $class;
}

sub spamcheck {
	my ($self, $msg, $out) = @_;

	my $tmp;
	my $fd = _msg_to_fd($self, $msg, \$tmp);
	my $rdr = { 0 => $fd };
	my ($fh, $pid) = popen_rd($self->{checkcmd}, undef, $rdr);
	defined $pid or die "failed to popen_rd spamc: $!\n";
	my $r;
	unless (ref $out) {
		my $buf = '';
		$out = \$buf;
	}
again:
	do {
		$r = sysread($fh, $$out, 65536, length($$out));
	} while (defined($r) && $r != 0);
	unless (defined $r) {
		goto again if $!{EINTR};
		die "read failed: $!";
	}
	close $fh or die "close failed: $!";
	waitpid($pid, 0);
	($? || $$out eq '') ? 0 : 1;
}

sub hamlearn {
	my ($self, $msg, $rdr) = @_;
	_learn($self, $msg, $rdr, 'hamcmd');
}

sub spamlearn {
	my ($self, $msg, $rdr) = @_;
	_learn($self, $msg, $rdr, 'spamcmd');
}

sub _learn {
	my ($self, $msg, $rdr, $field) = @_;
	$rdr ||= {};
	$rdr->{1} ||= $self->_devnull;
	$rdr->{2} ||= $self->_devnull;
	my $tmp;
	$rdr->{0} = _msg_to_fd($self, $msg, \$tmp);
	my $pid = spawn($self->{$field}, undef, $rdr);
	waitpid($pid, 0);
	!$?;
}

sub _devnull {
	my ($self) = @_;
	my $fd = $self->{-devnullfd};
	return $fd if defined $fd;
	open my $fh, '+>', '/dev/null' or
				die "failed to open /dev/null: $!";
	$self->{-devnull} = $fh;
	$self->{-devnullfd} = fileno($fh);
}

sub _msg_to_fd {
	my ($self, $msg, $tmpref) = @_;
	my $fd;
	if (my $ref = ref($msg)) {
		my $fileno = eval { fileno($msg) };
		return $fileno if defined $fileno;

		open(my $tmpfh, '+>', undef) or die "failed to open: $!";
		$tmpfh->autoflush(1);
		$msg = \($msg->as_string) if $ref ne 'SCALAR';
		print $tmpfh $$msg or die "failed to print: $!";
		sysseek($tmpfh, 0, SEEK_SET) or
			die "sysseek(fh) failed: $!";
		$$tmpref = $tmpfh;

		return fileno($tmpfh);
	}
	$msg;
}

1;