forked from codefriar/RubySpamAssassin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor project structure. Added parser for report output for rules
- Loading branch information
Showing
7 changed files
with
132 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.0.2 | ||
1.0.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,5 @@ | ||
module RubySpamAssassin | ||
class SpamResult | ||
|
||
attr_accessor :response_version, | ||
:response_code, | ||
:response_message, | ||
:spam, | ||
:score, | ||
:threshold, | ||
:tags, | ||
:report, | ||
:content_length | ||
|
||
#returns true if the message was spam, otherwise false | ||
def spam? | ||
(@spam == "True" || @spam == "Yes") ? true : false | ||
end | ||
end | ||
|
||
class SpamClient | ||
|
||
require 'socket' | ||
require 'timeout' | ||
|
||
def initialize(host="localhost", port=783, timeout=5) | ||
@port = port | ||
@host = host | ||
@timeout =timeout | ||
@socket = TCPSocket.open(@host, @port) | ||
end | ||
|
||
def reconnect | ||
@socket = @socket || TCPSocket.open(@host, @port) | ||
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 report(message) | ||
protocol_response = send_message("REPORT", message) | ||
result = process_headers protocol_response[0...2] | ||
result.report = protocol_response[3..-1].join | ||
end | ||
|
||
def report_ifspam(message) | ||
result = report(message).spam? | ||
end | ||
|
||
def skip | ||
protocol_response = send_message("SKIP", message) | ||
end | ||
|
||
def ping | ||
protocol_response = send_message("PING", message) | ||
result = process_headers protocol_response[0] | ||
end | ||
|
||
alias :process :report | ||
|
||
private | ||
def send_message(command, message) | ||
length = message.length | ||
@socket.write(command + " SPAMC/1.2\r\n") | ||
@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 | ||
response = @socket.readlines | ||
@socket.close #might as well close it now | ||
|
||
response | ||
end | ||
|
||
def process_headers(headers) | ||
result = 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 | ||
autoload(:SpamClient, "RubySpamAssassin/spam_client") | ||
autoload(:SpamResult, "RubySpamAssassin/spam_result") | ||
autoload(:ReportParser, "RubySpamAssassin/report_parser") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class RubySpamAssassin::ReportParser | ||
LINE_REGEXP = /-$/ | ||
RULE_REGEXP = /[0-9]*[.][0-9]\s\w*\s/ | ||
|
||
def self.parse(report_text) | ||
last_part = report_text.split(LINE_REGEXP)[1].sub(/^[\n\r]./,'').chomp.chomp | ||
pts_rules = last_part.gsub(RULE_REGEXP).collect { |sub| sub.chomp(' ') } | ||
rule_texts = last_part.split(RULE_REGEXP).collect { |text| text.delete("\n").squeeze.chomp(' ').sub(/^\s/, '') } | ||
|
||
rules = [] | ||
pts_rules.each_with_index do |pts_rule, i| | ||
rules << { | ||
:pts => pts_rule.split(' ')[0], | ||
:rule => pts_rule.split(' ')[1], | ||
:text => rule_texts[i + 1] | ||
} | ||
end | ||
|
||
rules | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
class RubySpamAssassin::SpamClient | ||
require 'socket' | ||
require 'timeout' | ||
|
||
def initialize(host="localhost", port=783, timeout=5) | ||
@port = port | ||
@host = host | ||
@timeout =timeout | ||
@socket = TCPSocket.open(@host, @port) | ||
end | ||
|
||
def reconnect | ||
@socket = @socket || TCPSocket.open(@host, @port) | ||
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 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", message) | ||
end | ||
|
||
def ping | ||
protocol_response = send_message("PING", message) | ||
result = process_headers protocol_response[0] | ||
end | ||
|
||
alias :process :report | ||
|
||
private | ||
def send_message(command, message) | ||
length = message.length | ||
@socket.write(command + " SPAMC/1.2\r\n") | ||
@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 | ||
response = @socket.readlines | ||
@socket.close #might as well close it now | ||
|
||
response | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class RubySpamAssassin::SpamResult | ||
attr_accessor :response_version, | ||
:response_code, | ||
:response_message, | ||
:spam, | ||
:score, | ||
:threshold, | ||
:tags, | ||
:report, | ||
:content_length, | ||
:rules | ||
|
||
#returns true if the message was spam, otherwise false | ||
def spam? | ||
(@spam == "True" || @spam == "Yes") ? true : false | ||
end | ||
end |