Mercurial > hg > soundsoftware-site
comparison app/models/mail_handler.rb @ 1338:25603efa57b5
Merge from live branch
author | Chris Cannam |
---|---|
date | Thu, 20 Jun 2013 13:14:14 +0100 |
parents | 0a574315af3e |
children | 4f746d8966dd fb9a13467253 |
comparison
equal
deleted
inserted
replaced
1209:1b1138f6f55e | 1338:25603efa57b5 |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2011 Jean-Philippe Lang | 2 # Copyright (C) 2006-2012 Jean-Philippe Lang |
3 # | 3 # |
4 # This program is free software; you can redistribute it and/or | 4 # This program is free software; you can redistribute it and/or |
5 # modify it under the terms of the GNU General Public License | 5 # modify it under the terms of the GNU General Public License |
6 # as published by the Free Software Foundation; either version 2 | 6 # as published by the Free Software Foundation; either version 2 |
7 # of the License, or (at your option) any later version. | 7 # of the License, or (at your option) any later version. |
27 def self.receive(email, options={}) | 27 def self.receive(email, options={}) |
28 @@handler_options = options.dup | 28 @@handler_options = options.dup |
29 | 29 |
30 @@handler_options[:issue] ||= {} | 30 @@handler_options[:issue] ||= {} |
31 | 31 |
32 @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip) if @@handler_options[:allow_override].is_a?(String) | 32 if @@handler_options[:allow_override].is_a?(String) |
33 @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip) | |
34 end | |
33 @@handler_options[:allow_override] ||= [] | 35 @@handler_options[:allow_override] ||= [] |
34 # Project needs to be overridable if not specified | 36 # Project needs to be overridable if not specified |
35 @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project) | 37 @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project) |
36 # Status overridable by default | 38 # Status overridable by default |
37 @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status) | 39 @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status) |
38 | 40 |
39 @@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1' ? true : false) | 41 @@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1' ? true : false) |
40 super email | 42 |
41 end | 43 email.force_encoding('ASCII-8BIT') if email.respond_to?(:force_encoding) |
44 super(email) | |
45 end | |
46 | |
47 def logger | |
48 Rails.logger | |
49 end | |
50 | |
51 cattr_accessor :ignored_emails_headers | |
52 @@ignored_emails_headers = { | |
53 'X-Auto-Response-Suppress' => 'oof', | |
54 'Auto-Submitted' => /^auto-/ | |
55 } | |
42 | 56 |
43 # Processes incoming emails | 57 # Processes incoming emails |
44 # Returns the created object (eg. an issue, a message) or false | 58 # Returns the created object (eg. an issue, a message) or false |
45 def receive(email) | 59 def receive(email) |
46 @email = email | 60 @email = email |
47 sender_email = email.from.to_a.first.to_s.strip | 61 sender_email = email.from.to_a.first.to_s.strip |
48 # Ignore emails received from the application emission address to avoid hell cycles | 62 # Ignore emails received from the application emission address to avoid hell cycles |
49 if sender_email.downcase == Setting.mail_from.to_s.strip.downcase | 63 if sender_email.downcase == Setting.mail_from.to_s.strip.downcase |
50 logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]" if logger && logger.info | 64 if logger && logger.info |
65 logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]" | |
66 end | |
51 return false | 67 return false |
68 end | |
69 # Ignore auto generated emails | |
70 self.class.ignored_emails_headers.each do |key, ignored_value| | |
71 value = email.header[key] | |
72 if value | |
73 value = value.to_s.downcase | |
74 if (ignored_value.is_a?(Regexp) && value.match(ignored_value)) || value == ignored_value | |
75 if logger && logger.info | |
76 logger.info "MailHandler: ignoring email with #{key}:#{value} header" | |
77 end | |
78 return false | |
79 end | |
80 end | |
52 end | 81 end |
53 @user = User.find_by_mail(sender_email) if sender_email.present? | 82 @user = User.find_by_mail(sender_email) if sender_email.present? |
54 if @user && !@user.active? | 83 if @user && !@user.active? |
55 logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]" if logger && logger.info | 84 if logger && logger.info |
85 logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]" | |
86 end | |
56 return false | 87 return false |
57 end | 88 end |
58 if @user.nil? | 89 if @user.nil? |
59 # Email was submitted by an unknown user | 90 # Email was submitted by an unknown user |
60 case @@handler_options[:unknown_user] | 91 case @@handler_options[:unknown_user] |
61 when 'accept' | 92 when 'accept' |
62 @user = User.anonymous | 93 @user = User.anonymous |
63 when 'create' | 94 when 'create' |
64 @user = create_user_from_email(email) | 95 @user = create_user_from_email |
65 if @user | 96 if @user |
66 logger.info "MailHandler: [#{@user.login}] account created" if logger && logger.info | 97 if logger && logger.info |
67 Mailer.deliver_account_information(@user, @user.password) | 98 logger.info "MailHandler: [#{@user.login}] account created" |
99 end | |
100 Mailer.account_information(@user, @user.password).deliver | |
68 else | 101 else |
69 logger.error "MailHandler: could not create account for [#{sender_email}]" if logger && logger.error | 102 if logger && logger.error |
103 logger.error "MailHandler: could not create account for [#{sender_email}]" | |
104 end | |
70 return false | 105 return false |
71 end | 106 end |
72 else | 107 else |
73 # Default behaviour, emails from unknown users are ignored | 108 # Default behaviour, emails from unknown users are ignored |
74 logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]" if logger && logger.info | 109 if logger && logger.info |
110 logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]" | |
111 end | |
75 return false | 112 return false |
76 end | 113 end |
77 end | 114 end |
78 User.current = @user | 115 User.current = @user |
79 dispatch | 116 dispatch |
80 end | 117 end |
81 | 118 |
82 private | 119 private |
83 | 120 |
84 MESSAGE_ID_RE = %r{^<redmine\.([a-z0-9_]+)\-(\d+)\.\d+@} | 121 MESSAGE_ID_RE = %r{^<?redmine\.([a-z0-9_]+)\-(\d+)\.\d+@} |
85 ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]*#(\d+)\]} | 122 ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]*#(\d+)\]} |
86 MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]*msg(\d+)\]} | 123 MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]*msg(\d+)\]} |
87 | 124 |
88 def dispatch | 125 def dispatch |
89 headers = [email.in_reply_to, email.references].flatten.compact | 126 headers = [email.in_reply_to, email.references].flatten.compact |
127 subject = email.subject.to_s | |
90 if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE} | 128 if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE} |
91 klass, object_id = $1, $2.to_i | 129 klass, object_id = $1, $2.to_i |
92 method_name = "receive_#{klass}_reply" | 130 method_name = "receive_#{klass}_reply" |
93 if self.class.private_instance_methods.collect(&:to_s).include?(method_name) | 131 if self.class.private_instance_methods.collect(&:to_s).include?(method_name) |
94 send method_name, object_id | 132 send method_name, object_id |
95 else | 133 else |
96 # ignoring it | 134 # ignoring it |
97 end | 135 end |
98 elsif m = email.subject.match(ISSUE_REPLY_SUBJECT_RE) | 136 elsif m = subject.match(ISSUE_REPLY_SUBJECT_RE) |
99 receive_issue_reply(m[1].to_i) | 137 receive_issue_reply(m[1].to_i) |
100 elsif m = email.subject.match(MESSAGE_REPLY_SUBJECT_RE) | 138 elsif m = subject.match(MESSAGE_REPLY_SUBJECT_RE) |
101 receive_message_reply(m[1].to_i) | 139 receive_message_reply(m[1].to_i) |
102 else | 140 else |
103 dispatch_to_default | 141 dispatch_to_default |
104 end | 142 end |
105 rescue ActiveRecord::RecordInvalid => e | 143 rescue ActiveRecord::RecordInvalid => e |
127 end | 165 end |
128 | 166 |
129 issue = Issue.new(:author => user, :project => project) | 167 issue = Issue.new(:author => user, :project => project) |
130 issue.safe_attributes = issue_attributes_from_keywords(issue) | 168 issue.safe_attributes = issue_attributes_from_keywords(issue) |
131 issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} | 169 issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} |
132 issue.subject = email.subject.to_s.chomp[0,255] | 170 issue.subject = cleaned_up_subject |
133 if issue.subject.blank? | 171 if issue.subject.blank? |
134 issue.subject = '(no subject)' | 172 issue.subject = '(no subject)' |
135 end | 173 end |
136 issue.description = cleaned_up_text_body | 174 issue.description = cleaned_up_text_body |
137 | 175 |
142 logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info | 180 logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info |
143 issue | 181 issue |
144 end | 182 end |
145 | 183 |
146 # Adds a note to an existing issue | 184 # Adds a note to an existing issue |
147 def receive_issue_reply(issue_id) | 185 def receive_issue_reply(issue_id, from_journal=nil) |
148 issue = Issue.find_by_id(issue_id) | 186 issue = Issue.find_by_id(issue_id) |
149 return unless issue | 187 return unless issue |
150 # check permission | 188 # check permission |
151 unless @@handler_options[:no_permission_check] | 189 unless @@handler_options[:no_permission_check] |
152 raise UnauthorizedAction unless user.allowed_to?(:add_issue_notes, issue.project) || user.allowed_to?(:edit_issues, issue.project) | 190 unless user.allowed_to?(:add_issue_notes, issue.project) || |
191 user.allowed_to?(:edit_issues, issue.project) | |
192 raise UnauthorizedAction | |
193 end | |
153 end | 194 end |
154 | 195 |
155 # ignore CLI-supplied defaults for new issues | 196 # ignore CLI-supplied defaults for new issues |
156 @@handler_options[:issue].clear | 197 @@handler_options[:issue].clear |
157 | 198 |
158 journal = issue.init_journal(user) | 199 journal = issue.init_journal(user) |
200 if from_journal && from_journal.private_notes? | |
201 # If the received email was a reply to a private note, make the added note private | |
202 issue.private_notes = true | |
203 end | |
159 issue.safe_attributes = issue_attributes_from_keywords(issue) | 204 issue.safe_attributes = issue_attributes_from_keywords(issue) |
160 issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} | 205 issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} |
161 journal.notes = cleaned_up_text_body | 206 journal.notes = cleaned_up_text_body |
162 add_attachments(issue) | 207 add_attachments(issue) |
163 issue.save! | 208 issue.save! |
164 logger.info "MailHandler: issue ##{issue.id} updated by #{user}" if logger && logger.info | 209 if logger && logger.info |
210 logger.info "MailHandler: issue ##{issue.id} updated by #{user}" | |
211 end | |
165 journal | 212 journal |
166 end | 213 end |
167 | 214 |
168 # Reply will be added to the issue | 215 # Reply will be added to the issue |
169 def receive_journal_reply(journal_id) | 216 def receive_journal_reply(journal_id) |
170 journal = Journal.find_by_id(journal_id) | 217 journal = Journal.find_by_id(journal_id) |
171 if journal && journal.journalized_type == 'Issue' | 218 if journal && journal.journalized_type == 'Issue' |
172 receive_issue_reply(journal.journalized_id) | 219 receive_issue_reply(journal.journalized_id, journal) |
173 end | 220 end |
174 end | 221 end |
175 | 222 |
176 # Receives a reply to a forum message | 223 # Receives a reply to a forum message |
177 def receive_message_reply(message_id) | 224 def receive_message_reply(message_id) |
182 unless @@handler_options[:no_permission_check] | 229 unless @@handler_options[:no_permission_check] |
183 raise UnauthorizedAction unless user.allowed_to?(:add_messages, message.project) | 230 raise UnauthorizedAction unless user.allowed_to?(:add_messages, message.project) |
184 end | 231 end |
185 | 232 |
186 if !message.locked? | 233 if !message.locked? |
187 reply = Message.new(:subject => email.subject.gsub(%r{^.*msg\d+\]}, '').strip, | 234 reply = Message.new(:subject => cleaned_up_subject.gsub(%r{^.*msg\d+\]}, '').strip, |
188 :content => cleaned_up_text_body) | 235 :content => cleaned_up_text_body) |
189 reply.author = user | 236 reply.author = user |
190 reply.board = message.board | 237 reply.board = message.board |
191 message.children << reply | 238 message.children << reply |
192 add_attachments(reply) | 239 add_attachments(reply) |
193 reply | 240 reply |
194 else | 241 else |
195 logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic" if logger && logger.info | 242 if logger && logger.info |
243 logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic" | |
244 end | |
196 end | 245 end |
197 end | 246 end |
198 end | 247 end |
199 | 248 |
200 def add_attachments(obj) | 249 def add_attachments(obj) |
201 if email.attachments && email.attachments.any? | 250 if email.attachments && email.attachments.any? |
202 email.attachments.each do |attachment| | 251 email.attachments.each do |attachment| |
203 obj.attachments << Attachment.create(:container => obj, | 252 obj.attachments << Attachment.create(:container => obj, |
204 :file => attachment, | 253 :file => attachment.decoded, |
254 :filename => attachment.filename, | |
205 :author => user, | 255 :author => user, |
206 :content_type => attachment.content_type) | 256 :content_type => attachment.mime_type) |
207 end | 257 end |
208 end | 258 end |
209 end | 259 end |
210 | 260 |
211 # Adds To and Cc as watchers of the given object if the sender has the | 261 # Adds To and Cc as watchers of the given object if the sender has the |
224 @keywords ||= {} | 274 @keywords ||= {} |
225 if @keywords.has_key?(attr) | 275 if @keywords.has_key?(attr) |
226 @keywords[attr] | 276 @keywords[attr] |
227 else | 277 else |
228 @keywords[attr] = begin | 278 @keywords[attr] = begin |
229 if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && (v = extract_keyword!(plain_text_body, attr, options[:format])) | 279 if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && |
280 (v = extract_keyword!(plain_text_body, attr, options[:format])) | |
230 v | 281 v |
231 elsif !@@handler_options[:issue][attr].blank? | 282 elsif !@@handler_options[:issue][attr].blank? |
232 @@handler_options[:issue][attr] | 283 @@handler_options[:issue][attr] |
233 end | 284 end |
234 end | 285 end |
238 # Destructively extracts the value for +attr+ in +text+ | 289 # Destructively extracts the value for +attr+ in +text+ |
239 # Returns nil if no matching keyword found | 290 # Returns nil if no matching keyword found |
240 def extract_keyword!(text, attr, format=nil) | 291 def extract_keyword!(text, attr, format=nil) |
241 keys = [attr.to_s.humanize] | 292 keys = [attr.to_s.humanize] |
242 if attr.is_a?(Symbol) | 293 if attr.is_a?(Symbol) |
243 keys << l("field_#{attr}", :default => '', :locale => user.language) if user && user.language.present? | 294 if user && user.language.present? |
244 keys << l("field_#{attr}", :default => '', :locale => Setting.default_language) if Setting.default_language.present? | 295 keys << l("field_#{attr}", :default => '', :locale => user.language) |
296 end | |
297 if Setting.default_language.present? | |
298 keys << l("field_#{attr}", :default => '', :locale => Setting.default_language) | |
299 end | |
245 end | 300 end |
246 keys.reject! {|k| k.blank?} | 301 keys.reject! {|k| k.blank?} |
247 keys.collect! {|k| Regexp.escape(k)} | 302 keys.collect! {|k| Regexp.escape(k)} |
248 format ||= '.+' | 303 format ||= '.+' |
249 text.gsub!(/^(#{keys.join('|')})[ \t]*:[ \t]*(#{format})\s*$/i, '') | 304 keyword = nil |
250 $2 && $2.strip | 305 regexp = /^(#{keys.join('|')})[ \t]*:[ \t]*(#{format})\s*$/i |
306 if m = text.match(regexp) | |
307 keyword = m[2].strip | |
308 text.gsub!(regexp, '') | |
309 end | |
310 keyword | |
251 end | 311 end |
252 | 312 |
253 def target_project | 313 def target_project |
254 # TODO: other ways to specify project: | 314 # TODO: other ways to specify project: |
255 # * parse the email To field | 315 # * parse the email To field |
267 'tracker_id' => (k = get_keyword(:tracker)) && issue.project.trackers.named(k).first.try(:id), | 327 'tracker_id' => (k = get_keyword(:tracker)) && issue.project.trackers.named(k).first.try(:id), |
268 'status_id' => (k = get_keyword(:status)) && IssueStatus.named(k).first.try(:id), | 328 'status_id' => (k = get_keyword(:status)) && IssueStatus.named(k).first.try(:id), |
269 'priority_id' => (k = get_keyword(:priority)) && IssuePriority.named(k).first.try(:id), | 329 'priority_id' => (k = get_keyword(:priority)) && IssuePriority.named(k).first.try(:id), |
270 'category_id' => (k = get_keyword(:category)) && issue.project.issue_categories.named(k).first.try(:id), | 330 'category_id' => (k = get_keyword(:category)) && issue.project.issue_categories.named(k).first.try(:id), |
271 'assigned_to_id' => assigned_to.try(:id), | 331 'assigned_to_id' => assigned_to.try(:id), |
272 'fixed_version_id' => (k = get_keyword(:fixed_version, :override => true)) && issue.project.shared_versions.named(k).first.try(:id), | 332 'fixed_version_id' => (k = get_keyword(:fixed_version, :override => true)) && |
333 issue.project.shared_versions.named(k).first.try(:id), | |
273 'start_date' => get_keyword(:start_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'), | 334 'start_date' => get_keyword(:start_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'), |
274 'due_date' => get_keyword(:due_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'), | 335 'due_date' => get_keyword(:due_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'), |
275 'estimated_hours' => get_keyword(:estimated_hours, :override => true), | 336 'estimated_hours' => get_keyword(:estimated_hours, :override => true), |
276 'done_ratio' => get_keyword(:done_ratio, :override => true, :format => '(\d|10)?0') | 337 'done_ratio' => get_keyword(:done_ratio, :override => true, :format => '(\d|10)?0') |
277 }.delete_if {|k, v| v.blank? } | 338 }.delete_if {|k, v| v.blank? } |
284 end | 345 end |
285 | 346 |
286 # Returns a Hash of issue custom field values extracted from keywords in the email body | 347 # Returns a Hash of issue custom field values extracted from keywords in the email body |
287 def custom_field_values_from_keywords(customized) | 348 def custom_field_values_from_keywords(customized) |
288 customized.custom_field_values.inject({}) do |h, v| | 349 customized.custom_field_values.inject({}) do |h, v| |
289 if value = get_keyword(v.custom_field.name, :override => true) | 350 if keyword = get_keyword(v.custom_field.name, :override => true) |
290 h[v.custom_field.id.to_s] = value | 351 h[v.custom_field.id.to_s] = v.custom_field.value_from_keyword(keyword, customized) |
291 end | 352 end |
292 h | 353 h |
293 end | 354 end |
294 end | 355 end |
295 | 356 |
296 # Returns the text/plain part of the email | 357 # Returns the text/plain part of the email |
297 # If not found (eg. HTML-only email), returns the body with tags removed | 358 # If not found (eg. HTML-only email), returns the body with tags removed |
298 def plain_text_body | 359 def plain_text_body |
299 return @plain_text_body unless @plain_text_body.nil? | 360 return @plain_text_body unless @plain_text_body.nil? |
300 parts = @email.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten | 361 |
301 if parts.empty? | 362 part = email.text_part || email.html_part || email |
302 parts << @email | 363 @plain_text_body = Redmine::CodesetUtil.to_utf8(part.body.decoded, part.charset) |
303 end | 364 |
304 plain_text_part = parts.detect {|p| p.content_type == 'text/plain'} | 365 # strip html tags and remove doctype directive |
305 if plain_text_part.nil? | 366 @plain_text_body = strip_tags(@plain_text_body.strip) |
306 # no text/plain part found, assuming html-only email | 367 @plain_text_body.sub! %r{^<!DOCTYPE .*$}, '' |
307 # strip html tags and remove doctype directive | |
308 @plain_text_body = strip_tags(@email.body.to_s) | |
309 @plain_text_body.gsub! %r{^<!DOCTYPE .*$}, '' | |
310 else | |
311 @plain_text_body = plain_text_part.body.to_s | |
312 end | |
313 @plain_text_body.strip! | |
314 @plain_text_body | 368 @plain_text_body |
315 end | 369 end |
316 | 370 |
317 def cleaned_up_text_body | 371 def cleaned_up_text_body |
318 cleanup_body(plain_text_body) | 372 cleanup_body(plain_text_body) |
319 end | 373 end |
320 | 374 |
375 def cleaned_up_subject | |
376 subject = email.subject.to_s | |
377 subject.strip[0,255] | |
378 end | |
379 | |
321 def self.full_sanitizer | 380 def self.full_sanitizer |
322 @full_sanitizer ||= HTML::FullSanitizer.new | 381 @full_sanitizer ||= HTML::FullSanitizer.new |
323 end | 382 end |
324 | 383 |
325 def self.assign_string_attribute_with_limit(object, attribute, value) | 384 def self.assign_string_attribute_with_limit(object, attribute, value, limit=nil) |
326 limit = object.class.columns_hash[attribute.to_s].limit || 255 | 385 limit ||= object.class.columns_hash[attribute.to_s].limit || 255 |
327 value = value.to_s.slice(0, limit) | 386 value = value.to_s.slice(0, limit) |
328 object.send("#{attribute}=", value) | 387 object.send("#{attribute}=", value) |
329 end | 388 end |
330 | 389 |
331 # Returns a User from an email address and a full name | 390 # Returns a User from an email address and a full name |
332 def self.new_user_from_attributes(email_address, fullname=nil) | 391 def self.new_user_from_attributes(email_address, fullname=nil) |
333 user = User.new | 392 user = User.new |
334 | 393 |
335 # Truncating the email address would result in an invalid format | 394 # Truncating the email address would result in an invalid format |
336 user.mail = email_address | 395 user.mail = email_address |
337 assign_string_attribute_with_limit(user, 'login', email_address) | 396 assign_string_attribute_with_limit(user, 'login', email_address, User::LOGIN_LENGTH_LIMIT) |
338 | 397 |
339 names = fullname.blank? ? email_address.gsub(/@.*$/, '').split('.') : fullname.split | 398 names = fullname.blank? ? email_address.gsub(/@.*$/, '').split('.') : fullname.split |
340 assign_string_attribute_with_limit(user, 'firstname', names.shift) | 399 assign_string_attribute_with_limit(user, 'firstname', names.shift) |
341 assign_string_attribute_with_limit(user, 'lastname', names.join(' ')) | 400 assign_string_attribute_with_limit(user, 'lastname', names.join(' ')) |
342 user.lastname = '-' if user.lastname.blank? | 401 user.lastname = '-' if user.lastname.blank? |
343 | 402 |
344 password_length = [Setting.password_min_length.to_i, 10].max | 403 password_length = [Setting.password_min_length.to_i, 10].max |
345 user.password = ActiveSupport::SecureRandom.hex(password_length / 2 + 1) | 404 user.password = Redmine::Utils.random_hex(password_length / 2 + 1) |
346 user.language = Setting.default_language | 405 user.language = Setting.default_language |
347 | 406 |
348 unless user.valid? | 407 unless user.valid? |
349 user.login = "user#{ActiveSupport::SecureRandom.hex(6)}" if user.errors.on(:login) | 408 user.login = "user#{Redmine::Utils.random_hex(6)}" unless user.errors[:login].blank? |
350 user.firstname = "-" if user.errors.on(:firstname) | 409 user.firstname = "-" unless user.errors[:firstname].blank? |
351 user.lastname = "-" if user.errors.on(:lastname) | 410 user.lastname = "-" unless user.errors[:lastname].blank? |
352 end | 411 end |
353 | 412 |
354 user | 413 user |
355 end | 414 end |
356 | 415 |
357 # Creates a User for the +email+ sender | 416 # Creates a User for the +email+ sender |
358 # Returns the user or nil if it could not be created | 417 # Returns the user or nil if it could not be created |
359 def create_user_from_email(email) | 418 def create_user_from_email |
360 addr = email.from_addrs.to_a.first | 419 from = email.header['from'].to_s |
361 if addr && !addr.spec.blank? | 420 addr, name = from, nil |
362 user = self.class.new_user_from_attributes(addr.spec, addr.name) | 421 if m = from.match(/^"?(.+?)"?\s+<(.+@.+)>$/) |
422 addr, name = m[2], m[1] | |
423 end | |
424 if addr.present? | |
425 user = self.class.new_user_from_attributes(addr, name) | |
363 if user.save | 426 if user.save |
364 user | 427 user |
365 else | 428 else |
366 logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger | 429 logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger |
367 nil | 430 nil |
369 else | 432 else |
370 logger.error "MailHandler: failed to create User: no FROM address found" if logger | 433 logger.error "MailHandler: failed to create User: no FROM address found" if logger |
371 nil | 434 nil |
372 end | 435 end |
373 end | 436 end |
374 | |
375 private | |
376 | 437 |
377 # Removes the email body of text after the truncation configurations. | 438 # Removes the email body of text after the truncation configurations. |
378 def cleanup_body(body) | 439 def cleanup_body(body) |
379 delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).map {|s| Regexp.escape(s)} | 440 delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).map {|s| Regexp.escape(s)} |
380 unless delimiters.empty? | 441 unless delimiters.empty? |
386 | 447 |
387 def find_assignee_from_keyword(keyword, issue) | 448 def find_assignee_from_keyword(keyword, issue) |
388 keyword = keyword.to_s.downcase | 449 keyword = keyword.to_s.downcase |
389 assignable = issue.assignable_users | 450 assignable = issue.assignable_users |
390 assignee = nil | 451 assignee = nil |
391 assignee ||= assignable.detect {|a| a.mail.to_s.downcase == keyword || a.login.to_s.downcase == keyword} | 452 assignee ||= assignable.detect {|a| |
453 a.mail.to_s.downcase == keyword || | |
454 a.login.to_s.downcase == keyword | |
455 } | |
392 if assignee.nil? && keyword.match(/ /) | 456 if assignee.nil? && keyword.match(/ /) |
393 firstname, lastname = *(keyword.split) # "First Last Throwaway" | 457 firstname, lastname = *(keyword.split) # "First Last Throwaway" |
394 assignee ||= assignable.detect {|a| a.is_a?(User) && a.firstname.to_s.downcase == firstname && a.lastname.to_s.downcase == lastname} | 458 assignee ||= assignable.detect {|a| |
459 a.is_a?(User) && a.firstname.to_s.downcase == firstname && | |
460 a.lastname.to_s.downcase == lastname | |
461 } | |
395 end | 462 end |
396 if assignee.nil? | 463 if assignee.nil? |
397 assignee ||= assignable.detect {|a| a.is_a?(Group) && a.name.downcase == keyword} | 464 assignee ||= assignable.detect {|a| a.name.downcase == keyword} |
398 end | 465 end |
399 assignee | 466 assignee |
400 end | 467 end |
401 end | 468 end |