aboutsummaryrefslogtreecommitdiffstats
path: root/script/handle-mail-replies
blob: 68cab90351bc1cad574b11966479c1d7542fa409 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

# Handle email responses sent to us.
#
# This script is invoked as a pipe command, i.e. with the raw email message on stdin.
# - If a message is identified as a permanent bounce, the user is marked as having a
#   bounced address, and will not be sent any more messages.
# - If a message is identified as an out-of-office autoreply, it is discarded.
# - Any other messages are forwarded to config.get("FORWARD_NONBOUNCE_RESPONSES_TO")


# We want to avoid loading rails unless we need it, so we start by just loading the
# config file ourselves.
$alaveteli_dir = File.join(File.dirname(__FILE__), '..')
$:.push(File.join($alaveteli_dir, "commonlib", "rblib"))
load "config.rb"
MySociety::Config.set_file(File.join($alaveteli_dir, 'config', 'general'), true)
MySociety::Config.load_default

require 'rubygems'
if File.exist? File.join($alaveteli_dir,'vendor','rails','Rakefile')
    $:.push(File.join($alaveteli_dir, "vendor", "rails", "actionmailer", "lib", "action_mailer", "vendor", "tmail-1.2.7"))
    require 'tmail'
else
    require 'action_mailer'
end

def main(in_test_mode)
    Dir.chdir($alaveteli_dir) do
        raw_message = $stdin.read
        begin
            message = TMail::Mail.parse(raw_message)
        rescue
            # Error parsing message. Just pass it on, to be on the safe side.
            forward_on(raw_message) unless in_test_mode
            return 0
        end
        
        pfas = permanently_failed_addresses(message)
        if !pfas.empty?
            if in_test_mode
                puts pfas
            else
                pfas.each do |pfa|
                    record_bounce(pfa, raw_message)
                end
            end
            return 1
        end
        
        if is_oof? message
            # Discard out-of-office messages
            return 2
        end
        
        # Otherwise forward the message on
        forward_on(raw_message) unless in_test_mode
        return 0
    end
end

def permanently_failed_addresses(message)
    if message.header_string("Return-Path") == "<>"
        # Some sort of auto-response
    
        # Check for Exim’s X-Failed-Recipients header
        failed_recipients = message.header_string("X-Failed-Recipients")
        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./
                return failed_recipients.split(/,\s*/)
            end
        end
        
        # Next, look for multipart/report
        if message.content_type == "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/)
                    # The first section is a generic header; subsequent sections
                    # represent a particular recipient. Since we 
                    sections[1..-1].each do |section|
                        if section !~ /^Status: (\d)/ || $1 != '5'
                            # Either we couldn’t find the Status field, or it was a transient failure
                            break
                        end
                        if section =~ /^Final-Recipient: rfc822;(.+)/
                            permanently_failed_recipients.push($1)
                        end
                    end
                end
            end
            if !permanently_failed_recipients.empty?
                return permanently_failed_recipients
            end
        end
    end
    
    return []
end

def is_oof?(message)
    # Check for out-of-office
    
    if message.header_string("X-POST-MessageClass") == "9; Autoresponder"
        return true
    end
    
    subject = message.header_string("Subject").downcase
    if message.header_string("Return-Path") == "<>"
        if subject.start_with? "out of office: "
            return true
        end
        if subject.start_with? "automatic reply: "
            return true
        end
    end
    
    if message.header_string("Auto-Submitted") == "auto-generated"
        if subject =~ /out of( the)? office/
            return true
        end
    end
    
    if subject.start_with? "out of office autoreply:"
        return true
    end
    if subject == "out of office"
        return true
    end
    if subject == "out of office reply"
        return true
    end
    if subject.end_with? "is out of the office"
        return true
    end
    return false
end

def forward_on(raw_message)
    forward_non_bounces_to = MySociety::Config.get("FORWARD_NONBOUNCE_RESPONSES_TO", "user-support@localhost")
    IO.popen("/usr/sbin/sendmail -i #{forward_non_bounces_to}", "w") do |f|
        f.write(raw_message);
        f.close;
    end
end

def load_rails
    require File.join('config', 'boot')
    require RAILS_ROOT + '/config/environment'
end

def record_bounce(email_address, bounce_message)
    load_rails
    User.record_bounce_for_email(email_address, bounce_message)
end

in_test_mode = (ARGV[0] == "--test")
status = main(in_test_mode)
exit(status) if in_test_mode
ass="w"> [] do resource :report, :only => [:new, :create] end resources :info_request_batch, :only => :show #### User controller # Use /profile for things to do with the currently signed in user. # Use /user/XXXX for things that anyone can see about that user. # Note that /profile isn't indexed by search (see robots.txt) match '/profile/sign_in' => 'user#signin', :as => :signin match '/profile/sign_up' => 'user#signup', :as => :signup, :via => :post match '/profile/sign_up' => 'user#signin', :via => :get match '/profile/sign_out' => 'user#signout', :as => :signout match '/c/:email_token' => 'user#confirm', :as => :confirm match '/user/:url_name' => 'user#show', :as => :show_user match '/user/:url_name/profile' => 'user#show', :as => :show_user_profile, :view => 'profile' match '/user/:url_name/requests' => 'user#show', :as => :show_user_requests, :view => 'requests' match '/user/:url_name/wall' => 'user#wall', :as => :show_user_wall match '/user/contact/:id' => 'user#contact', :as => :contact_user match '/profile/change_password' => 'user#signchangepassword', :as => :signchangepassword match '/profile/change_email' => 'user#signchangeemail', :as => :signchangeemail match '/profile/set_photo' => 'user#set_profile_photo', :as => :set_profile_photo match '/profile/clear_photo' => 'user#clear_profile_photo', :as => :clear_profile_photo match '/user/:url_name/photo.png' => 'user#get_profile_photo', :as => :get_profile_photo match '/profile/draft_photo/:id.png' => 'user#get_draft_profile_photo', :as => :get_draft_profile_photo match '/profile/set_about_me' => 'user#set_profile_about_me', :as => :set_profile_about_me match '/profile/set_receive_alerts' => 'user#set_receive_email_alerts', :as => :set_receive_email_alerts match '/profile/river' => 'user#river', :as => :river #### #### PublicBody controller match '/body/search_ahead' => 'public_body#search_typeahead', :as => :search_ahead_bodies match '/body' => 'public_body#list', :as => :list_public_bodies match '/body/list/all' => 'public_body#list', :as => :list_public_bodies_default match '/body/list/:tag' => 'public_body#list', :as => :list_public_bodies match '/local/:tag' => 'public_body#list_redirect', :as => :list_public_bodies_redirect match '/body/all-authorities.csv' => 'public_body#list_all_csv', :as => :all_public_bodies_csv match '/body/:url_name' => 'public_body#show', :as => :show_public_body, :view => 'all' match '/body/:url_name/all' => 'public_body#show', :as => :show_public_body_all, :view => 'all' match '/body/:url_name/successful' => 'public_body#show', :as => :show_public_body_successful, :view => 'successful' match '/body/:url_name/unsuccessful' => 'public_body#show', :as => :show_public_body_unsuccessful, :view => 'unsuccessful' match '/body/:url_name/awaiting' => 'public_body#show', :as => :show_public_body_awaiting, :view => 'awaiting' match '/body/:url_name/view_email' => 'public_body#view_email', :as => :view_public_body_email match '/body/:url_name/:tag' => 'public_body#show', :as => :show_public_body_tag match '/body/:url_name/:tag/:view' => 'public_body#show', :as => :show_public_body_tag_view match '/body_statistics' => 'public_body#statistics', :as => :public_bodies_statistics #### resource :change_request, :only => [:new, :create], :controller => 'public_body_change_requests' #### Comment controller match '/annotate/request/:url_title' => 'comment#new', :as => :new_comment, :type => 'request' #### #### Services controller match '/country_message' => 'services#other_country_message', :as => :other_country_message match '/hidden_user_explanation' => 'services#hidden_user_explanation', :as => :hidden_user_explanation #### #### Track controller # /track/ is for setting up an email alert for the item # /feed/ is a direct RSS feed of the item match '/:feed/request/:url_title' => 'track#track_request', :as => :track_request, :feed => /(track|feed)/ match '/:feed/list/:view' => 'track#track_list', :as => :track_list, :view => nil, :feed => /(track|feed)/ match '/:feed/body/:url_name' => 'track#track_public_body', :as => :track_public_body, :feed => /(track|feed)/ match '/:feed/user/:url_name' => 'track#track_user', :as => :track_user, :feed => /(track|feed)/ # TODO: :format doesn't work. See hacky code in the controller that makes up for this. match '/:feed/search/:query_array' => 'track#track_search_query', :as => :track_search, :feed => /(track|feed)/, :constraints => { :query_array => /.*/ } match '/track/update/:track_id' => 'track#update', :as => :update match '/track/delete_all_type' => 'track#delete_all_type', :as => :delete_all_type match '/track/feed/:track_id' => 'track#atom_feed', :as => :atom_feed #### #### Help controller match '/help/unhappy/:url_title' => 'help#unhappy', :as => :help_unhappy match '/help/about' => 'help#about', :as => :help_about match '/help/alaveteli' => 'help#alaveteli', :as => :help_alaveteli match '/help/contact' => 'help#contact', :as => :help_contact match '/help/officers' => 'help#officers', :as => :help_officers match '/help/requesting' => 'help#requesting', :as => :help_requesting match '/help/privacy' => 'help#privacy', :as => :help_privacy match '/help/api' => 'help#api', :as => :help_api match '/help/credits' => 'help#credits', :as => :help_credits match '/help/:action' => 'help#action', :as => :help_general match '/help' => 'help#index' #### #### Holiday controller match '/due_date/:holiday' => 'holiday#due_date', :as => :due_date #### #### RequestGame controller match '/categorise/play' => 'request_game#play', :as => :categorise_play match '/categorise/request/:url_title' => 'request_game#show', :as => :categorise_request match '/categorise/stop' => 'request_game#stop', :as => :categorise_stop #### #### AdminPublicBody controller match '/admin/missing_scheme' => 'admin_public_body#missing_scheme', :as => :admin_body_missing match '/admin/body' => 'admin_public_body#index', :as => :admin_body_index match '/admin/body/list' => 'admin_public_body#list', :as => :admin_body_list match '/admin/body/show/:id' => 'admin_public_body#show', :as => :admin_body_show match '/admin/body/new' => 'admin_public_body#new', :as => :admin_body_new match '/admin/body/edit/:id' => 'admin_public_body#edit', :as => :admin_body_edit match '/admin/body/update/:id' => 'admin_public_body#update', :as => :admin_body_update match '/admin/body/create' => 'admin_public_body#create', :as => :admin_body_create match '/admin/body/destroy/:id' => 'admin_public_body#destroy', :as => :admin_body_destroy match '/admin/body/import_csv' => 'admin_public_body#import_csv', :as => :admin_body_import_csv match '/admin/body/mass_tag_add' => 'admin_public_body#mass_tag_add', :as => :admin_body_mass_tag_add #### #### AdminPublicBodyCategory controller scope '/admin', :as => 'admin' do resources :categories, :controller => 'admin_public_body_categories' end #### #### AdminPublicBodyHeading controller scope '/admin', :as => 'admin' do resources :headings, :controller => 'admin_public_body_headings', :except => [:index] do post 'reorder', :on => :collection post 'reorder_categories', :on => :member end end #### #### AdminPublicBodyChangeRequest controller match '/admin/change_request/edit/:id' => 'admin_public_body_change_requests#edit', :as => :admin_change_request_edit match '/admin/change_request/update/:id' => 'admin_public_body_change_requests#update', :as => :admin_change_request_update #### #### AdminGeneral controller match '/admin' => 'admin_general#index', :as => :admin_general_index match '/admin/timeline' => 'admin_general#timeline', :as => :admin_timeline match '/admin/debug' => 'admin_general#debug', :as => :admin_debug match '/admin/stats' => 'admin_general#stats', :as => :admin_stats #### #### AdminRequest controller match '/admin/request' => 'admin_request#index', :as => :admin_request_index match '/admin/request/list' => 'admin_request#list', :as => :admin_request_list match '/admin/request/show/:id' => 'admin_request#show', :as => :admin_request_show match '/admin/request/resend' => 'admin_request#resend', :as => :admin_request_resend match '/admin/request/edit/:id' => 'admin_request#edit', :as => :admin_request_edit match '/admin/request/update/:id' => 'admin_request#update', :as => :admin_request_update match '/admin/request/destroy/:id' => 'admin_request#fully_destroy', :as => :admin_request_destroy match '/admin/request/edit_comment/:id' => 'admin_request#edit_comment', :as => :admin_request_edit_comment match '/admin/request/update_comment/:id' => 'admin_request#update_comment', :as => :admin_request_update_comment match '/admin/request/move_request' => 'admin_request#move_request', :as => :admin_request_move_request match '/admin/request/generate_upload_url/:id' => 'admin_request#generate_upload_url', :as => :admin_request_generate_upload_url match '/admin/request/show_raw_email/:id' => 'admin_request#show_raw_email', :as => :admin_request_show_raw_email match '/admin/request/download_raw_email/:id' => 'admin_request#download_raw_email', :as => :admin_request_download_raw_email match '/admin/request/mark_event_as_clarification' => 'admin_request#mark_event_as_clarification', :as => :admin_request_clarification match '/admin/request/hide/:id' => 'admin_request#hide_request', :as => :admin_request_hide #### #### AdminIncomingMessage controller match '/admin/incoming/destroy' => 'admin_incoming_message#destroy', :as => :admin_incoming_destroy match '/admin/incoming/redeliver' => 'admin_incoming_message#redeliver', :as => :admin_incoming_redeliver match '/admin/incoming/edit/:id' => 'admin_incoming_message#edit', :as => :admin_incoming_edit match '/admin/incoming/update/:id' => 'admin_incoming_message#update', :as => :admin_incoming_update #### #### AdminOutgoingMessage controller match '/admin/outgoing/edit/:id' => 'admin_outgoing_message#edit', :as => :admin_outgoing_edit match '/admin/outgoing/destroy/:id' => 'admin_outgoing_message#destroy', :as => :admin_outgoing_destroy match '/admin/outgoing/update/:id' => 'admin_outgoing_message#update', :as => :admin_outgoing_update #### #### AdminUser controller match '/admin/user' => 'admin_user#index', :as => :admin_user_index match '/admin/user/list' => 'admin_user#list', :as => :admin_user_list match '/admin/user/banned' => 'admin_user#list_banned', :as => :admin_user_list_banned match '/admin/user/show/:id' => 'admin_user#show', :as => :admin_user_show match '/admin/user/edit/:id' => 'admin_user#edit', :as => :admin_user_edit match '/admin/user/show_bounce_message/:id' => 'admin_user#show_bounce_message', :as => :admin_user_show_bounce match '/admin/user/update/:id' => 'admin_user#update', :as => :admin_user_update match '/admin/user/clear_bounce/:id' => 'admin_user#clear_bounce', :as => :admin_user_clear_bounce match '/admin/user/destroy_track' => 'admin_user#destroy_track', :as => :admin_user_destroy_track match '/admin/user/login_as/:id' => 'admin_user#login_as', :as => :admin_user_login_as match '/admin/user/clear_profile_photo/:id' => 'admin_user#clear_profile_photo', :as => :admin_clear_profile_photo match '/admin/user/modify_comment_visibility/:id' => 'admin_user#modify_comment_visibility', :as => 'admin_user_modify_comment_visibility' #### #### AdminTrack controller match '/admin/track/list' => 'admin_track#list', :as => :admin_track_list #### #### AdminCensorRule controller match '/admin/censor/new' => 'admin_censor_rule#new', :as => :admin_rule_new match '/admin/censor/create' => 'admin_censor_rule#create', :as => :admin_rule_create match '/admin/censor/edit/:id' => 'admin_censor_rule#edit', :as => :admin_rule_edit match '/admin/censor/update/:id' => 'admin_censor_rule#update', :as => :admin_rule_update match '/admin/censor/destroy/:censor_rule_id' => 'admin_censor_rule#destroy', :as => :admin_rule_destroy scope '/admin', :as => 'admin' do resources :info_requests, :only => [] do resources :censor_rules, :controller => 'admin_censor_rule', :only => [:new, :create], :name_prefix => 'info_request_' end end scope '/admin', :as => 'admin' do resources :users, :only => [] do resources :censor_rules, :controller => 'admin_censor_rule', :only => [:new, :create], :name_prefix => 'user_' end end #### #### AdminSpamAddresses controller scope '/admin', :as => 'admin' do resources :spam_addresses, :controller => 'admin_spam_addresses', :only => [:index, :create, :destroy] end #### #### Api controller match '/api/v2/request.json' => 'api#create_request', :as => :api_create_request, :via => :post match '/api/v2/request/:id.json' => 'api#show_request', :as => :api_show_request, :via => :get match '/api/v2/request/:id.json' => 'api#add_correspondence', :as => :api_add_correspondence, :via => :post match '/api/v2/request/:id/update.json' => 'api#update_state', :as => :api_update_state, :via => :post match '/api/v2/body/:id/request_events.:feed_type' => 'api#body_request_events', :as => :api_body_request_events, :feed_type => '^(json|atom)$' #### filter :conditionallyprependlocale end