@@ -22,28 +22,33 class MailHandler < ActionMailer::Base | |||||
22 | class UnauthorizedAction < StandardError; end |
|
22 | class UnauthorizedAction < StandardError; end | |
23 | class MissingInformation < StandardError; end |
|
23 | class MissingInformation < StandardError; end | |
24 |
|
24 | |||
25 | attr_reader :email, :user |
|
25 | attr_reader :email, :user, :handler_options | |
26 |
|
26 | |||
27 |
def self.receive( |
|
27 | def self.receive(raw_mail, options={}) | |
28 |
|
|
28 | options = options.deep_dup | |
29 |
|
29 | |||
30 |
|
|
30 | options[:issue] ||= {} | |
31 |
|
31 | |||
32 |
if |
|
32 | if options[:allow_override].is_a?(String) | |
33 |
|
|
33 | options[:allow_override] = options[:allow_override].split(',').collect(&:strip) | |
34 | end |
|
34 | end | |
35 |
|
|
35 | options[:allow_override] ||= [] | |
36 | # Project needs to be overridable if not specified |
|
36 | # Project needs to be overridable if not specified | |
37 |
|
|
37 | options[:allow_override] << 'project' unless options[:issue].has_key?(:project) | |
38 | # Status overridable by default |
|
38 | # Status overridable by default | |
39 |
|
|
39 | options[:allow_override] << 'status' unless options[:issue].has_key?(:status) | |
40 |
|
40 | |||
41 |
|
|
41 | options[:no_account_notice] = (options[:no_account_notice].to_s == '1') | |
42 |
|
|
42 | options[:no_notification] = (options[:no_notification].to_s == '1') | |
43 |
|
|
43 | options[:no_permission_check] = (options[:no_permission_check].to_s == '1') | |
44 |
|
44 | |||
45 |
|
|
45 | raw_mail.force_encoding('ASCII-8BIT') | |
46 | super(email) |
|
46 | ||
|
47 | ActiveSupport::Notifications.instrument("receive.action_mailer") do |payload| | |||
|
48 | mail = Mail.new(raw_mail) | |||
|
49 | set_payload_for_mail(payload, mail) | |||
|
50 | new.receive(mail, options) | |||
|
51 | end | |||
47 | end |
|
52 | end | |
48 |
|
53 | |||
49 | # Receives an email and rescues any exception |
|
54 | # Receives an email and rescues any exception | |
@@ -79,8 +84,9 class MailHandler < ActionMailer::Base | |||||
79 |
|
84 | |||
80 | # Processes incoming emails |
|
85 | # Processes incoming emails | |
81 | # Returns the created object (eg. an issue, a message) or false |
|
86 | # Returns the created object (eg. an issue, a message) or false | |
82 | def receive(email) |
|
87 | def receive(email, options={}) | |
83 | @email = email |
|
88 | @email = email | |
|
89 | @handler_options = options | |||
84 | sender_email = email.from.to_a.first.to_s.strip |
|
90 | sender_email = email.from.to_a.first.to_s.strip | |
85 | # Ignore emails received from the application emission address to avoid hell cycles |
|
91 | # Ignore emails received from the application emission address to avoid hell cycles | |
86 | if sender_email.downcase == Setting.mail_from.to_s.strip.downcase |
|
92 | if sender_email.downcase == Setting.mail_from.to_s.strip.downcase | |
@@ -111,7 +117,7 class MailHandler < ActionMailer::Base | |||||
111 | end |
|
117 | end | |
112 | if @user.nil? |
|
118 | if @user.nil? | |
113 | # Email was submitted by an unknown user |
|
119 | # Email was submitted by an unknown user | |
114 |
case |
|
120 | case handler_options[:unknown_user] | |
115 | when 'accept' |
|
121 | when 'accept' | |
116 | @user = User.anonymous |
|
122 | @user = User.anonymous | |
117 | when 'create' |
|
123 | when 'create' | |
@@ -120,8 +126,8 class MailHandler < ActionMailer::Base | |||||
120 | if logger |
|
126 | if logger | |
121 | logger.info "MailHandler: [#{@user.login}] account created" |
|
127 | logger.info "MailHandler: [#{@user.login}] account created" | |
122 | end |
|
128 | end | |
123 |
add_user_to_group( |
|
129 | add_user_to_group(handler_options[:default_group]) | |
124 |
unless |
|
130 | unless handler_options[:no_account_notice] | |
125 | Mailer.account_information(@user, @user.password).deliver |
|
131 | Mailer.account_information(@user, @user.password).deliver | |
126 | end |
|
132 | end | |
127 | else |
|
133 | else | |
@@ -186,7 +192,7 class MailHandler < ActionMailer::Base | |||||
186 | def receive_issue |
|
192 | def receive_issue | |
187 | project = target_project |
|
193 | project = target_project | |
188 | # check permission |
|
194 | # check permission | |
189 |
unless |
|
195 | unless handler_options[:no_permission_check] | |
190 | raise UnauthorizedAction unless user.allowed_to?(:add_issues, project) |
|
196 | raise UnauthorizedAction unless user.allowed_to?(:add_issues, project) | |
191 | end |
|
197 | end | |
192 |
|
198 | |||
@@ -213,7 +219,7 class MailHandler < ActionMailer::Base | |||||
213 | issue = Issue.find_by_id(issue_id) |
|
219 | issue = Issue.find_by_id(issue_id) | |
214 | return unless issue |
|
220 | return unless issue | |
215 | # check permission |
|
221 | # check permission | |
216 |
unless |
|
222 | unless handler_options[:no_permission_check] | |
217 | unless user.allowed_to?(:add_issue_notes, issue.project) || |
|
223 | unless user.allowed_to?(:add_issue_notes, issue.project) || | |
218 | user.allowed_to?(:edit_issues, issue.project) |
|
224 | user.allowed_to?(:edit_issues, issue.project) | |
219 | raise UnauthorizedAction |
|
225 | raise UnauthorizedAction | |
@@ -221,7 +227,7 class MailHandler < ActionMailer::Base | |||||
221 | end |
|
227 | end | |
222 |
|
228 | |||
223 | # ignore CLI-supplied defaults for new issues |
|
229 | # ignore CLI-supplied defaults for new issues | |
224 |
|
|
230 | handler_options[:issue].clear | |
225 |
|
231 | |||
226 | journal = issue.init_journal(user) |
|
232 | journal = issue.init_journal(user) | |
227 | if from_journal && from_journal.private_notes? |
|
233 | if from_journal && from_journal.private_notes? | |
@@ -253,7 +259,7 class MailHandler < ActionMailer::Base | |||||
253 | if message |
|
259 | if message | |
254 | message = message.root |
|
260 | message = message.root | |
255 |
|
261 | |||
256 |
unless |
|
262 | unless handler_options[:no_permission_check] | |
257 | raise UnauthorizedAction unless user.allowed_to?(:add_messages, message.project) |
|
263 | raise UnauthorizedAction unless user.allowed_to?(:add_messages, message.project) | |
258 | end |
|
264 | end | |
259 |
|
265 | |||
@@ -318,11 +324,11 class MailHandler < ActionMailer::Base | |||||
318 | @keywords[attr] |
|
324 | @keywords[attr] | |
319 | else |
|
325 | else | |
320 | @keywords[attr] = begin |
|
326 | @keywords[attr] = begin | |
321 |
if (options[:override] || |
|
327 | if (options[:override] || handler_options[:allow_override].include?(attr.to_s)) && | |
322 | (v = extract_keyword!(cleaned_up_text_body, attr, options[:format])) |
|
328 | (v = extract_keyword!(cleaned_up_text_body, attr, options[:format])) | |
323 | v |
|
329 | v | |
324 |
elsif ! |
|
330 | elsif !handler_options[:issue][attr].blank? | |
325 |
|
|
331 | handler_options[:issue][attr] | |
326 | end |
|
332 | end | |
327 | end |
|
333 | end | |
328 | end |
|
334 | end | |
@@ -359,7 +365,7 class MailHandler < ActionMailer::Base | |||||
359 | target = Project.find_by_identifier(get_keyword(:project)) |
|
365 | target = Project.find_by_identifier(get_keyword(:project)) | |
360 | if target.nil? |
|
366 | if target.nil? | |
361 | # Invalid project keyword, use the project specified as the default one |
|
367 | # Invalid project keyword, use the project specified as the default one | |
362 |
default_project = |
|
368 | default_project = handler_options[:issue][:project] | |
363 | if default_project.present? |
|
369 | if default_project.present? | |
364 | target = Project.find_by_identifier(default_project) |
|
370 | target = Project.find_by_identifier(default_project) | |
365 | end |
|
371 | end | |
@@ -485,7 +491,7 class MailHandler < ActionMailer::Base | |||||
485 | end |
|
491 | end | |
486 | if addr.present? |
|
492 | if addr.present? | |
487 | user = self.class.new_user_from_attributes(addr, name) |
|
493 | user = self.class.new_user_from_attributes(addr, name) | |
488 |
if |
|
494 | if handler_options[:no_notification] | |
489 | user.mail_notification = 'none' |
|
495 | user.mail_notification = 'none' | |
490 | end |
|
496 | end | |
491 | if user.save |
|
497 | if user.save |
General Comments 0
You need to be logged in to leave comments.
Login now