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