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