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