| 24 |
24 |
|
| 25 |
25 |
attr_reader :email, :user, :handler_options
|
| 26 |
26 |
|
| 27 |
|
def self.receive(raw_mail, options={})
|
|
27 |
def self.receive(raw_mail, options={}, error=[])
|
| 28 |
28 |
options = options.deep_dup
|
| 29 |
29 |
|
| 30 |
30 |
options[:issue] ||= {}
|
| ... | ... | |
| 46 |
46 |
ActiveSupport::Notifications.instrument("receive.action_mailer") do |payload|
|
| 47 |
47 |
mail = Mail.new(raw_mail)
|
| 48 |
48 |
set_payload_for_mail(payload, mail)
|
| 49 |
|
new.receive(mail, options)
|
|
49 |
new.receive(mail, options, error)
|
| 50 |
50 |
end
|
| 51 |
51 |
end
|
| 52 |
52 |
|
| ... | ... | |
| 86 |
86 |
|
| 87 |
87 |
# Processes incoming emails
|
| 88 |
88 |
# Returns the created object (eg. an issue, a message) or false
|
| 89 |
|
def receive(email, options={})
|
|
89 |
def receive(email, options={}, error=[])
|
| 90 |
90 |
@email = email
|
| 91 |
91 |
@handler_options = options
|
| 92 |
92 |
sender_email = email.from.to_a.first.to_s.strip
|
| 93 |
93 |
# Ignore emails received from the application emission address to avoid hell cycles
|
| 94 |
94 |
emission_address = Setting.mail_from.to_s.gsub(/(?:.*<|>.*|\(.*\))/, '').strip
|
| 95 |
95 |
if sender_email.casecmp(emission_address) == 0
|
|
96 |
error << "Ignoring email from Redmine emission address [#{sender_email}]."
|
| 96 |
97 |
if logger
|
| 97 |
98 |
logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]"
|
| 98 |
99 |
end
|
| ... | ... | |
| 104 |
105 |
if value
|
| 105 |
106 |
value = value.to_s.downcase
|
| 106 |
107 |
if (ignored_value.is_a?(Regexp) && value.match(ignored_value)) || value == ignored_value
|
|
108 |
error << "Ignoring email with #{key}:#{value} header."
|
| 107 |
109 |
if logger
|
| 108 |
110 |
logger.info "MailHandler: ignoring email with #{key}:#{value} header"
|
| 109 |
111 |
end
|
| ... | ... | |
| 113 |
115 |
end
|
| 114 |
116 |
@user = User.find_by_mail(sender_email) if sender_email.present?
|
| 115 |
117 |
if @user && !@user.active?
|
|
118 |
error << "Ignoring email from non-active user [#{@user.login}]."
|
| 116 |
119 |
if logger
|
| 117 |
120 |
logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]"
|
| 118 |
121 |
end
|
| ... | ... | |
| 134 |
137 |
::Mailer.deliver_account_information(@user, @user.password)
|
| 135 |
138 |
end
|
| 136 |
139 |
else
|
|
140 |
error << "Could not create account for [#{sender_email}]."
|
| 137 |
141 |
if logger
|
| 138 |
142 |
logger.error "MailHandler: could not create account for [#{sender_email}]"
|
| 139 |
143 |
end
|
| ... | ... | |
| 141 |
145 |
end
|
| 142 |
146 |
else
|
| 143 |
147 |
# Default behaviour, emails from unknown users are ignored
|
|
148 |
error << "Ignoring email from unknown user [#{sender_email}]."
|
| 144 |
149 |
if logger
|
| 145 |
150 |
logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]"
|
| 146 |
151 |
end
|
| ... | ... | |
| 148 |
153 |
end
|
| 149 |
154 |
end
|
| 150 |
155 |
User.current = @user
|
| 151 |
|
dispatch
|
|
156 |
dispatch(error)
|
| 152 |
157 |
end
|
| 153 |
158 |
|
| 154 |
159 |
private
|
| ... | ... | |
| 157 |
162 |
ISSUE_REPLY_SUBJECT_RE = %r{\[(?:[^\]]*\s+)?#(\d+)\]}
|
| 158 |
163 |
MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]*msg(\d+)\]}
|
| 159 |
164 |
|
| 160 |
|
def dispatch
|
|
165 |
def dispatch(error)
|
| 161 |
166 |
headers = [email.in_reply_to, email.references].flatten.compact
|
| 162 |
167 |
subject = email.subject.to_s
|
| 163 |
168 |
if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE}
|
| ... | ... | |
| 177 |
182 |
end
|
| 178 |
183 |
rescue ActiveRecord::RecordInvalid => e
|
| 179 |
184 |
# TODO: send a email to the user
|
|
185 |
error << "#{e.message}."
|
| 180 |
186 |
logger.error "MailHandler: #{e.message}" if logger
|
| 181 |
187 |
false
|
| 182 |
188 |
rescue MissingInformation => e
|
|
189 |
error << "Missing information from #{user}: #{e.message}."
|
| 183 |
190 |
logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger
|
| 184 |
191 |
false
|
| 185 |
192 |
rescue UnauthorizedAction => e
|
|
193 |
error << "Unauthorized attempt from #{user}."
|
| 186 |
194 |
logger.error "MailHandler: unauthorized attempt from #{user}" if logger
|
| 187 |
195 |
false
|
| 188 |
196 |
end
|