Project:
Mailvisa
Code Location:
git://repo.or.cz/mailvisa.gitmaster
/
Outline
- > M usage()
mailvisa.rb
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
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
### Defaults confdir = ENV['HOME'] + '/settings/mailvisa' sockpath = 'mailvisad.sock' blocksize = 16384 threshold = 0.5 passthrough = true # output message header = true # output X-Spam headers errorcodes = false # suppress error codes if true endl = "\015\012" input = $stdin output = $stdout Spam_status = 160 require 'socket' ### Functions def usage 'USAGE: ' + $0 + ' [options]' end help = <<EOT Valid options are: -c <path> Look for files in <path> (default: $HOME/settings/mailvisa) -q Do not output message or X-Spam headers -e Do not use exit status to indicate spam -b <num> Read <num> bytes at a time (default: 16384) -t <num> Threshold for flagging messages as spam (default: 0.5) -m <command> Pipe output to <command> -s <path> Use <path> to connect to mailvisad (default: mailvisad.sock) EOT ### Main program ## Parse command line i = 0 while i < ARGV.length case ARGV[i] when '-c' i = i + 1 confdir = ARGV[i] when '-q', '--quiet' passthrough = false header = false when '-e' errorcodes = true when '-s', '--socket' i = i + 1 sockpath = ARGV[i] when '-b', '--blocksize' i = i + 1 blocksize = ARGV[i].to_i when '-m', '--mda' i = i + 1 output = IO.popen ARGV[i], 'w' when '-t' i = i + 1 threshold = ARGV[i].to_f when '-h' puts usage print "\n" + help exit when /^-/ $stderr.puts 'Unknown option: ' + ARGV[i] $stderr.puts usage $stderr.puts 'Use "' + $0 + ' -h" to list valid options' exit 0x80 else input = open ARGV[i] end i = i + 1 end sockpath = confdir + '/' + sockpath if sockpath.index('/') == nil status = 0 msg = input.read blocksize begin ## Send first block of message to filter conn = UNIXSocket.open(sockpath) conn.print msg conn.flush ## Get message score conn.close_write score = conn.read.to_f conn.close status = score > threshold ? Spam_status : 0 rescue $stderr.puts $! status = 1 # Indicate error end ## X-Spam header if header && (status == 0 || status == Spam_status) output.print 'X-Spam: ' + (status == 0 ? 'false' : 'true') + endl end ## Passthrough (verbose output) if passthrough while true output.print msg msg = input.read blocksize break if msg == nil end end # Return value exit errorcodes ? 0 : status
