-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathspam_client.rb
More file actions
87 lines (75 loc) · 2.47 KB
/
spam_client.rb
File metadata and controls
87 lines (75 loc) · 2.47 KB
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
class RubySpamAssassin::SpamClient
require_relative '../single_connection_pool'
require 'timeout'
def initialize(host="localhost", port=783, timeout=5)
@connection_pool = SingleConnectionPool.new(host: host, port: port, timeout: timeout)
end
def send_symbol(message)
protocol_response = send_message("SYMBOLS", message)
result = process_headers protocol_response[0...2]
result.tags = protocol_response[3...-1].join(" ").split(',')
end
def check(message)
protocol_response = send_message("CHECK", message)
result = process_headers protocol_response[0...2]
end
def tell(message, headers={})
h = []
h << "Message-class: #{headers[:message_class]}" if headers[:message_class]
h << "Set: #{headers[:set]}" if headers[:set]
h << "Remove: #{headers[:remove]}" if headers[:remove]
h << "User: #{headers[:user]}" if headers[:user]
protocol_response = send_message("TELL", message, h)
result = process_headers protocol_response[0...2]
end
def report(message)
protocol_response = send_message("REPORT", message)
result = process_headers protocol_response[0...2]
result.report = protocol_response[3..-1].join
result.rules = RubySpamAssassin::ReportParser.parse(result.report)
result
end
def report_ifspam(message)
result = report(message).spam?
end
def skip
protocol_response = send_message("SKIP")
end
def ping
protocol_response = send_message("PING")
result = process_headers protocol_response[0]
end
alias :process :report
private
def send_message(command, message="", headers=[])
length = message.bytesize
@connection_pool.with do |socket|
socket.write(command + " SPAMC/1.2\r\n")
headers.each do |h|
socket.write(h + "\r\n")
end
socket.write("Content-length: " + length.to_s + "\r\n\r\n")
socket.write(message)
socket.shutdown(1) #have to shutdown sending side to get response
socket.readlines
end
end
def process_headers(headers)
result = RubySpamAssassin::SpamResult.new
headers.each do |line|
case line.chomp
when /(.+)\/(.+) (.+) (.+)/ then
result.response_version = $2
result.response_code = $3
result.response_message = $4
when /^Spam: (.+) ; (.+) . (.+)$/ then
result.score = $2
result.spam = $1
result.threshold = $3
when /Content-length: (.+)/ then
result.content_length = $1
end
end
result
end
end