diff options
Diffstat (limited to 'script')
-rwxr-xr-x | script/handle-mail-replies.rb | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/script/handle-mail-replies.rb b/script/handle-mail-replies.rb index c05dca453..73fca33c3 100755 --- a/script/handle-mail-replies.rb +++ b/script/handle-mail-replies.rb @@ -20,9 +20,11 @@ $:.push(File.join($alaveteli_dir, "lib", "mail_handler")) require "configuration" MySociety::Config.set_file(File.join($alaveteli_dir, 'config', 'general'), true) MySociety::Config.load_default - -require 'action_mailer' require 'mail_handler' +if RUBY_VERSION.to_f >= 1.9 + # the default encoding for IO is utf-8, and we use utf-8 internally + Encoding.default_external = Encoding.default_internal = Encoding::UTF_8 +end def main(in_test_mode) Dir.chdir($alaveteli_dir) do @@ -47,16 +49,17 @@ def main(in_test_mode) return 1 end + content_type = MailHandler.get_content_type(message) # If we are still here, there are no permanent failures, # so if the message is a multipart/report then it must be # reporting a temporary failure. In this case we discard it - if message.content_type == "multipart/report" + if content_type == "multipart/report" return 1 end # Another style of temporary failure message - subject = message.header_string("Subject") - if message.content_type == "multipart/mixed" && subject == "Delivery Status Notification (Delay)" + subject = MailHandler.get_header_string("Subject", message) + if content_type == "multipart/mixed" && subject == "Delivery Status Notification (Delay)" return 1 end @@ -72,26 +75,26 @@ def main(in_test_mode) end def permanently_failed_addresses(message) - if message.header_string("Return-Path") == "<>" + if MailHandler.empty_return_path?(message) # Some sort of auto-response # Check for Exim’s X-Failed-Recipients header - failed_recipients = message.header_string("X-Failed-Recipients") + failed_recipients = MailHandler.get_header_string("X-Failed-Recipients", message) if !failed_recipients.nil? # The X-Failed-Recipients header contains the email address that failed # Check for the words "This is a permanent error." in the body, to indicate # a permanent failure - if message.body =~ /This is a permanent error./ + if MailHandler.get_part_body(message) =~ /This is a permanent error./ return failed_recipients.split(/,\s*/) end end # Next, look for multipart/report - if message.content_type == "multipart/report" + if MailHandler.get_content_type(message) == "multipart/report" permanently_failed_recipients = [] message.parts.each do |part| - if part.content_type == "message/delivery-status" - sections = part.body.split(/\r?\n\r?\n/) + if MailHandler.get_content_type(part) == "message/delivery-status" + sections = MailHandler.get_part_body(part).split(/\r?\n\r?\n/) # The first section is a generic header; subsequent sections # represent a particular recipient. Since we sections[1..-1].each do |section| @@ -111,11 +114,11 @@ def permanently_failed_addresses(message) end end - subject = message.header_string("Subject") + subject = MailHandler.get_header_string("Subject", message) # Then look for the style we’ve seen in WebShield bounces # (These do not have a return path of <> in the cases I have seen.) if subject == "Returned Mail: Error During Delivery" - if message.body =~ /^\s*---- Failed Recipients ----\s*((?:<[^>]+>\n)+)/ + if MailHandler.get_part_body(message) =~ /^\s*---- Failed Recipients ----\s*((?:<[^>]+>\n)+)/ return $1.scan(/<([^>]+)>/).flatten end end @@ -126,12 +129,12 @@ end def is_oof?(message) # Check for out-of-office - if message.header_string("X-POST-MessageClass") == "9; Autoresponder" + if MailHandler.get_header_string("X-POST-MessageClass", message) == "9; Autoresponder" return true end - subject = message.header_string("Subject").downcase - if message.header_string("Return-Path") == "<>" + subject = MailHandler.get_header_string("Subject", message).downcase + if MailHandler.empty_return_path?(message) if subject.start_with? "out of office: " return true end @@ -140,7 +143,7 @@ def is_oof?(message) end end - if message.header_string("Auto-Submitted") == "auto-generated" + if MailHandler.get_header_string("Auto-Submitted", message) == "auto-generated" if subject =~ /out of( the)? office/ return true end @@ -169,8 +172,8 @@ def forward_on(raw_message) end def load_rails - require File.join('config', 'boot') - require File.join('config', 'environment') + require File.join($alaveteli_dir, 'config', 'boot') + require File.join($alaveteli_dir, 'config', 'environment') end def record_bounce(email_address, bounce_message) |