File: sa-wrapper

package info (click to toggle)
dtc 0.35.5-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 18,824 kB
  • sloc: php: 50,739; sh: 8,596; makefile: 572; perl: 148; xml: 25
file content (98 lines) | stat: -rwxr-xr-x 1,945 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl -w
# Time-stamp: <05 April 2004, 13:37 home>
#
# sa-wrapper.pl
#
# SpamAssassin sa-learn wrapper
# (c) Alexandre Jousset, 2004
# This script is GPL'd
#
# Thanks to: Chung-Kie Tung for the removal of the dir
#            Adam Gent for bug report
#
# v1.2

use strict;
use MIME::Tools;
use MIME::Parser;
use File::Temp;

my $DEBUG = 0;
my $debug_file;
my $debug_file2;
my $UNPACK_DIR = '/var/lib/amavis/tmp';
my $SA_LEARN = '/usr/bin/sa-learn';
# my @DOMAINS = qw/example.com example.org/;
# read from file instead
my $file='/var/lib/dtc/etc/local_domains';
open (FH, "< $file") or die "Can't open $file for read: $!";
my @DOMAINS = <FH>;
close FH or die "Cannot close $file: $!"; 
@DOMAINS = trim(@DOMAINS);

# trim the whitespace off the array or string
sub trim 
{
	my @out = @_;
	for (@out)
	{
		s/^\s+//;
		s/\s+$//;
	}
	return wantarray ? @out : $out[0];
}

my ($spamham, $sender) = @ARGV;

sub recurs
{
	my $ent = shift;

	if ($ent->head->mime_type eq 'message/rfc822') {
		if ($DEBUG) {
			$debug_file = mktemp("/tmp/sa-wrapper.log.XXXXXX");
			open(OUT, "|$SA_LEARN -D --$spamham --single >>$debug_file 2>&1") or die "Cannot pipe $SA_LEARN: $!";
		} else {
			open(OUT, "|$SA_LEARN --$spamham --single") or die "Cannot pipe $SA_LEARN: $!";
		}

		$ent->bodyhandle->print(\*OUT);

		close(OUT);
		return;
	}

	my @parts = $ent->parts;

	if (@parts) {
		map { recurs($_) } @parts;
	}
}

my ($domain) = $sender =~ /\@(.*)$/;
unless (grep { $_ eq $domain } @DOMAINS) {
	die "$sender, I don't recognize your domain ($domain)!";
}

if ($DEBUG) {
	MIME::Tools->debugging(1);
	$debug_file2 = mktemp("/tmp/sa-wrapper.stderr.XXXXXX");
	open(STDERR, ">$debug_file2");
}
my $parser = new MIME::Parser;
$parser->extract_nested_messages(0);
$parser->output_under($UNPACK_DIR);

my $entity;
eval {
	$entity = $parser->parse(\*STDIN);
};

if ($@) {
	die $@;
} else {
	recurs($entity);
}

$parser->filer->purge;
rmdir $parser->output_dir;