@@ -1,317 +1,316 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 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. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class MailHandler < ActionMailer::Base |
|
18 | class MailHandler < ActionMailer::Base | |
19 | include ActionView::Helpers::SanitizeHelper |
|
19 | include ActionView::Helpers::SanitizeHelper | |
20 |
|
20 | |||
21 | class UnauthorizedAction < StandardError; end |
|
21 | class UnauthorizedAction < StandardError; end | |
22 | class MissingInformation < StandardError; end |
|
22 | class MissingInformation < StandardError; end | |
23 |
|
23 | |||
24 | attr_reader :email, :user |
|
24 | attr_reader :email, :user | |
25 |
|
25 | |||
26 | def self.receive(email, options={}) |
|
26 | def self.receive(email, options={}) | |
27 | @@handler_options = options.dup |
|
27 | @@handler_options = options.dup | |
28 |
|
28 | |||
29 | @@handler_options[:issue] ||= {} |
|
29 | @@handler_options[:issue] ||= {} | |
30 |
|
30 | |||
31 | @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip) if @@handler_options[:allow_override].is_a?(String) |
|
31 | @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip) if @@handler_options[:allow_override].is_a?(String) | |
32 | @@handler_options[:allow_override] ||= [] |
|
32 | @@handler_options[:allow_override] ||= [] | |
33 | # Project needs to be overridable if not specified |
|
33 | # Project needs to be overridable if not specified | |
34 | @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project) |
|
34 | @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project) | |
35 | # Status overridable by default |
|
35 | # Status overridable by default | |
36 | @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status) |
|
36 | @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status) | |
37 |
|
37 | |||
38 | @@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1' ? true : false) |
|
38 | @@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1' ? true : false) | |
39 | super email |
|
39 | super email | |
40 | end |
|
40 | end | |
41 |
|
41 | |||
42 | # Processes incoming emails |
|
42 | # Processes incoming emails | |
43 | # Returns the created object (eg. an issue, a message) or false |
|
43 | # Returns the created object (eg. an issue, a message) or false | |
44 | def receive(email) |
|
44 | def receive(email) | |
45 | @email = email |
|
45 | @email = email | |
46 | sender_email = email.from.to_a.first.to_s.strip |
|
46 | sender_email = email.from.to_a.first.to_s.strip | |
47 | # Ignore emails received from the application emission address to avoid hell cycles |
|
47 | # Ignore emails received from the application emission address to avoid hell cycles | |
48 | if sender_email.downcase == Setting.mail_from.to_s.strip.downcase |
|
48 | if sender_email.downcase == Setting.mail_from.to_s.strip.downcase | |
49 | logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]" if logger && logger.info |
|
49 | logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]" if logger && logger.info | |
50 | return false |
|
50 | return false | |
51 | end |
|
51 | end | |
52 | @user = User.find_by_mail(sender_email) |
|
52 | @user = User.find_by_mail(sender_email) | |
53 | if @user && !@user.active? |
|
53 | if @user && !@user.active? | |
54 | logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]" if logger && logger.info |
|
54 | logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]" if logger && logger.info | |
55 | return false |
|
55 | return false | |
56 | end |
|
56 | end | |
57 | if @user.nil? |
|
57 | if @user.nil? | |
58 | # Email was submitted by an unknown user |
|
58 | # Email was submitted by an unknown user | |
59 | case @@handler_options[:unknown_user] |
|
59 | case @@handler_options[:unknown_user] | |
60 | when 'accept' |
|
60 | when 'accept' | |
61 | @user = User.anonymous |
|
61 | @user = User.anonymous | |
62 | when 'create' |
|
62 | when 'create' | |
63 | @user = MailHandler.create_user_from_email(email) |
|
63 | @user = MailHandler.create_user_from_email(email) | |
64 | if @user |
|
64 | if @user | |
65 | logger.info "MailHandler: [#{@user.login}] account created" if logger && logger.info |
|
65 | logger.info "MailHandler: [#{@user.login}] account created" if logger && logger.info | |
66 | Mailer.deliver_account_information(@user, @user.password) |
|
66 | Mailer.deliver_account_information(@user, @user.password) | |
67 | else |
|
67 | else | |
68 | logger.error "MailHandler: could not create account for [#{sender_email}]" if logger && logger.error |
|
68 | logger.error "MailHandler: could not create account for [#{sender_email}]" if logger && logger.error | |
69 | return false |
|
69 | return false | |
70 | end |
|
70 | end | |
71 | else |
|
71 | else | |
72 | # Default behaviour, emails from unknown users are ignored |
|
72 | # Default behaviour, emails from unknown users are ignored | |
73 | logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]" if logger && logger.info |
|
73 | logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]" if logger && logger.info | |
74 | return false |
|
74 | return false | |
75 | end |
|
75 | end | |
76 | end |
|
76 | end | |
77 | User.current = @user |
|
77 | User.current = @user | |
78 | dispatch |
|
78 | dispatch | |
79 | end |
|
79 | end | |
80 |
|
80 | |||
81 | private |
|
81 | private | |
82 |
|
82 | |||
83 | MESSAGE_ID_RE = %r{^<redmine\.([a-z0-9_]+)\-(\d+)\.\d+@} |
|
83 | MESSAGE_ID_RE = %r{^<redmine\.([a-z0-9_]+)\-(\d+)\.\d+@} | |
84 | ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]*#(\d+)\]} |
|
84 | ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]*#(\d+)\]} | |
85 | MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]*msg(\d+)\]} |
|
85 | MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]*msg(\d+)\]} | |
86 |
|
86 | |||
87 | def dispatch |
|
87 | def dispatch | |
88 | headers = [email.in_reply_to, email.references].flatten.compact |
|
88 | headers = [email.in_reply_to, email.references].flatten.compact | |
89 | if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE} |
|
89 | if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE} | |
90 | klass, object_id = $1, $2.to_i |
|
90 | klass, object_id = $1, $2.to_i | |
91 | method_name = "receive_#{klass}_reply" |
|
91 | method_name = "receive_#{klass}_reply" | |
92 | if self.class.private_instance_methods.collect(&:to_s).include?(method_name) |
|
92 | if self.class.private_instance_methods.collect(&:to_s).include?(method_name) | |
93 | send method_name, object_id |
|
93 | send method_name, object_id | |
94 | else |
|
94 | else | |
95 | # ignoring it |
|
95 | # ignoring it | |
96 | end |
|
96 | end | |
97 | elsif m = email.subject.match(ISSUE_REPLY_SUBJECT_RE) |
|
97 | elsif m = email.subject.match(ISSUE_REPLY_SUBJECT_RE) | |
98 | receive_issue_reply(m[1].to_i) |
|
98 | receive_issue_reply(m[1].to_i) | |
99 | elsif m = email.subject.match(MESSAGE_REPLY_SUBJECT_RE) |
|
99 | elsif m = email.subject.match(MESSAGE_REPLY_SUBJECT_RE) | |
100 | receive_message_reply(m[1].to_i) |
|
100 | receive_message_reply(m[1].to_i) | |
101 | else |
|
101 | else | |
102 | receive_issue |
|
102 | receive_issue | |
103 | end |
|
103 | end | |
104 | rescue ActiveRecord::RecordInvalid => e |
|
104 | rescue ActiveRecord::RecordInvalid => e | |
105 | # TODO: send a email to the user |
|
105 | # TODO: send a email to the user | |
106 | logger.error e.message if logger |
|
106 | logger.error e.message if logger | |
107 | false |
|
107 | false | |
108 | rescue MissingInformation => e |
|
108 | rescue MissingInformation => e | |
109 | logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger |
|
109 | logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger | |
110 | false |
|
110 | false | |
111 | rescue UnauthorizedAction => e |
|
111 | rescue UnauthorizedAction => e | |
112 | logger.error "MailHandler: unauthorized attempt from #{user}" if logger |
|
112 | logger.error "MailHandler: unauthorized attempt from #{user}" if logger | |
113 | false |
|
113 | false | |
114 | end |
|
114 | end | |
115 |
|
115 | |||
116 | # Creates a new issue |
|
116 | # Creates a new issue | |
117 | def receive_issue |
|
117 | def receive_issue | |
118 | project = target_project |
|
118 | project = target_project | |
119 | tracker = (get_keyword(:tracker) && project.trackers.find_by_name(get_keyword(:tracker))) || project.trackers.find(:first) |
|
119 | tracker = (get_keyword(:tracker) && project.trackers.find_by_name(get_keyword(:tracker))) || project.trackers.find(:first) | |
120 | category = (get_keyword(:category) && project.issue_categories.find_by_name(get_keyword(:category))) |
|
120 | category = (get_keyword(:category) && project.issue_categories.find_by_name(get_keyword(:category))) | |
121 | priority = (get_keyword(:priority) && IssuePriority.find_by_name(get_keyword(:priority))) |
|
121 | priority = (get_keyword(:priority) && IssuePriority.find_by_name(get_keyword(:priority))) | |
122 | status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status))) |
|
122 | status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status))) | |
123 |
|
123 | |||
124 | # check permission |
|
124 | # check permission | |
125 | unless @@handler_options[:no_permission_check] |
|
125 | unless @@handler_options[:no_permission_check] | |
126 | raise UnauthorizedAction unless user.allowed_to?(:add_issues, project) |
|
126 | raise UnauthorizedAction unless user.allowed_to?(:add_issues, project) | |
127 | end |
|
127 | end | |
128 |
|
128 | |||
129 | issue = Issue.new(:author => user, :project => project, :tracker => tracker, :category => category, :priority => priority) |
|
129 | issue = Issue.new(:author => user, :project => project, :tracker => tracker, :category => category, :priority => priority) | |
130 | # check workflow |
|
130 | # check workflow | |
131 | if status && issue.new_statuses_allowed_to(user).include?(status) |
|
131 | if status && issue.new_statuses_allowed_to(user).include?(status) | |
132 | issue.status = status |
|
132 | issue.status = status | |
133 | end |
|
133 | end | |
134 | issue.subject = email.subject.chomp |
|
134 | issue.subject = email.subject.chomp | |
135 | issue.subject = issue.subject.toutf8 if issue.subject.respond_to?(:toutf8) |
|
|||
136 | if issue.subject.blank? |
|
135 | if issue.subject.blank? | |
137 | issue.subject = '(no subject)' |
|
136 | issue.subject = '(no subject)' | |
138 | end |
|
137 | end | |
139 | # custom fields |
|
138 | # custom fields | |
140 | issue.custom_field_values = issue.available_custom_fields.inject({}) do |h, c| |
|
139 | issue.custom_field_values = issue.available_custom_fields.inject({}) do |h, c| | |
141 | if value = get_keyword(c.name, :override => true) |
|
140 | if value = get_keyword(c.name, :override => true) | |
142 | h[c.id] = value |
|
141 | h[c.id] = value | |
143 | end |
|
142 | end | |
144 | h |
|
143 | h | |
145 | end |
|
144 | end | |
146 | issue.description = cleaned_up_text_body |
|
145 | issue.description = cleaned_up_text_body | |
147 | # add To and Cc as watchers before saving so the watchers can reply to Redmine |
|
146 | # add To and Cc as watchers before saving so the watchers can reply to Redmine | |
148 | add_watchers(issue) |
|
147 | add_watchers(issue) | |
149 | issue.save! |
|
148 | issue.save! | |
150 | add_attachments(issue) |
|
149 | add_attachments(issue) | |
151 | logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info |
|
150 | logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info | |
152 | issue |
|
151 | issue | |
153 | end |
|
152 | end | |
154 |
|
153 | |||
155 | def target_project |
|
154 | def target_project | |
156 | # TODO: other ways to specify project: |
|
155 | # TODO: other ways to specify project: | |
157 | # * parse the email To field |
|
156 | # * parse the email To field | |
158 | # * specific project (eg. Setting.mail_handler_target_project) |
|
157 | # * specific project (eg. Setting.mail_handler_target_project) | |
159 | target = Project.find_by_identifier(get_keyword(:project)) |
|
158 | target = Project.find_by_identifier(get_keyword(:project)) | |
160 | raise MissingInformation.new('Unable to determine target project') if target.nil? |
|
159 | raise MissingInformation.new('Unable to determine target project') if target.nil? | |
161 | target |
|
160 | target | |
162 | end |
|
161 | end | |
163 |
|
162 | |||
164 | # Adds a note to an existing issue |
|
163 | # Adds a note to an existing issue | |
165 | def receive_issue_reply(issue_id) |
|
164 | def receive_issue_reply(issue_id) | |
166 | status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status))) |
|
165 | status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status))) | |
167 |
|
166 | |||
168 | issue = Issue.find_by_id(issue_id) |
|
167 | issue = Issue.find_by_id(issue_id) | |
169 | return unless issue |
|
168 | return unless issue | |
170 | # check permission |
|
169 | # check permission | |
171 | unless @@handler_options[:no_permission_check] |
|
170 | unless @@handler_options[:no_permission_check] | |
172 | raise UnauthorizedAction unless user.allowed_to?(:add_issue_notes, issue.project) || user.allowed_to?(:edit_issues, issue.project) |
|
171 | raise UnauthorizedAction unless user.allowed_to?(:add_issue_notes, issue.project) || user.allowed_to?(:edit_issues, issue.project) | |
173 | raise UnauthorizedAction unless status.nil? || user.allowed_to?(:edit_issues, issue.project) |
|
172 | raise UnauthorizedAction unless status.nil? || user.allowed_to?(:edit_issues, issue.project) | |
174 | end |
|
173 | end | |
175 |
|
174 | |||
176 | # add the note |
|
175 | # add the note | |
177 | journal = issue.init_journal(user, cleaned_up_text_body) |
|
176 | journal = issue.init_journal(user, cleaned_up_text_body) | |
178 | add_attachments(issue) |
|
177 | add_attachments(issue) | |
179 | # check workflow |
|
178 | # check workflow | |
180 | if status && issue.new_statuses_allowed_to(user).include?(status) |
|
179 | if status && issue.new_statuses_allowed_to(user).include?(status) | |
181 | issue.status = status |
|
180 | issue.status = status | |
182 | end |
|
181 | end | |
183 | issue.save! |
|
182 | issue.save! | |
184 | logger.info "MailHandler: issue ##{issue.id} updated by #{user}" if logger && logger.info |
|
183 | logger.info "MailHandler: issue ##{issue.id} updated by #{user}" if logger && logger.info | |
185 | journal |
|
184 | journal | |
186 | end |
|
185 | end | |
187 |
|
186 | |||
188 | # Reply will be added to the issue |
|
187 | # Reply will be added to the issue | |
189 | def receive_journal_reply(journal_id) |
|
188 | def receive_journal_reply(journal_id) | |
190 | journal = Journal.find_by_id(journal_id) |
|
189 | journal = Journal.find_by_id(journal_id) | |
191 | if journal && journal.journalized_type == 'Issue' |
|
190 | if journal && journal.journalized_type == 'Issue' | |
192 | receive_issue_reply(journal.journalized_id) |
|
191 | receive_issue_reply(journal.journalized_id) | |
193 | end |
|
192 | end | |
194 | end |
|
193 | end | |
195 |
|
194 | |||
196 | # Receives a reply to a forum message |
|
195 | # Receives a reply to a forum message | |
197 | def receive_message_reply(message_id) |
|
196 | def receive_message_reply(message_id) | |
198 | message = Message.find_by_id(message_id) |
|
197 | message = Message.find_by_id(message_id) | |
199 | if message |
|
198 | if message | |
200 | message = message.root |
|
199 | message = message.root | |
201 |
|
200 | |||
202 | unless @@handler_options[:no_permission_check] |
|
201 | unless @@handler_options[:no_permission_check] | |
203 | raise UnauthorizedAction unless user.allowed_to?(:add_messages, message.project) |
|
202 | raise UnauthorizedAction unless user.allowed_to?(:add_messages, message.project) | |
204 | end |
|
203 | end | |
205 |
|
204 | |||
206 | if !message.locked? |
|
205 | if !message.locked? | |
207 | reply = Message.new(:subject => email.subject.gsub(%r{^.*msg\d+\]}, '').strip, |
|
206 | reply = Message.new(:subject => email.subject.gsub(%r{^.*msg\d+\]}, '').strip, | |
208 | :content => cleaned_up_text_body) |
|
207 | :content => cleaned_up_text_body) | |
209 | reply.author = user |
|
208 | reply.author = user | |
210 | reply.board = message.board |
|
209 | reply.board = message.board | |
211 | message.children << reply |
|
210 | message.children << reply | |
212 | add_attachments(reply) |
|
211 | add_attachments(reply) | |
213 | reply |
|
212 | reply | |
214 | else |
|
213 | else | |
215 | logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic" if logger && logger.info |
|
214 | logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic" if logger && logger.info | |
216 | end |
|
215 | end | |
217 | end |
|
216 | end | |
218 | end |
|
217 | end | |
219 |
|
218 | |||
220 | def add_attachments(obj) |
|
219 | def add_attachments(obj) | |
221 | if email.has_attachments? |
|
220 | if email.has_attachments? | |
222 | email.attachments.each do |attachment| |
|
221 | email.attachments.each do |attachment| | |
223 | Attachment.create(:container => obj, |
|
222 | Attachment.create(:container => obj, | |
224 | :file => attachment, |
|
223 | :file => attachment, | |
225 | :author => user, |
|
224 | :author => user, | |
226 | :content_type => attachment.content_type) |
|
225 | :content_type => attachment.content_type) | |
227 | end |
|
226 | end | |
228 | end |
|
227 | end | |
229 | end |
|
228 | end | |
230 |
|
229 | |||
231 | # Adds To and Cc as watchers of the given object if the sender has the |
|
230 | # Adds To and Cc as watchers of the given object if the sender has the | |
232 | # appropriate permission |
|
231 | # appropriate permission | |
233 | def add_watchers(obj) |
|
232 | def add_watchers(obj) | |
234 | if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project) |
|
233 | if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project) | |
235 | addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase} |
|
234 | addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase} | |
236 | unless addresses.empty? |
|
235 | unless addresses.empty? | |
237 | watchers = User.active.find(:all, :conditions => ['LOWER(mail) IN (?)', addresses]) |
|
236 | watchers = User.active.find(:all, :conditions => ['LOWER(mail) IN (?)', addresses]) | |
238 | watchers.each {|w| obj.add_watcher(w)} |
|
237 | watchers.each {|w| obj.add_watcher(w)} | |
239 | end |
|
238 | end | |
240 | end |
|
239 | end | |
241 | end |
|
240 | end | |
242 |
|
241 | |||
243 | def get_keyword(attr, options={}) |
|
242 | def get_keyword(attr, options={}) | |
244 | @keywords ||= {} |
|
243 | @keywords ||= {} | |
245 | if @keywords.has_key?(attr) |
|
244 | if @keywords.has_key?(attr) | |
246 | @keywords[attr] |
|
245 | @keywords[attr] | |
247 | else |
|
246 | else | |
248 | @keywords[attr] = begin |
|
247 | @keywords[attr] = begin | |
249 | if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && plain_text_body.gsub!(/^#{attr}[ \t]*:[ \t]*(.+)\s*$/i, '') |
|
248 | if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && plain_text_body.gsub!(/^#{attr}[ \t]*:[ \t]*(.+)\s*$/i, '') | |
250 | $1.strip |
|
249 | $1.strip | |
251 | elsif !@@handler_options[:issue][attr].blank? |
|
250 | elsif !@@handler_options[:issue][attr].blank? | |
252 | @@handler_options[:issue][attr] |
|
251 | @@handler_options[:issue][attr] | |
253 | end |
|
252 | end | |
254 | end |
|
253 | end | |
255 | end |
|
254 | end | |
256 | end |
|
255 | end | |
257 |
|
256 | |||
258 | # Returns the text/plain part of the email |
|
257 | # Returns the text/plain part of the email | |
259 | # If not found (eg. HTML-only email), returns the body with tags removed |
|
258 | # If not found (eg. HTML-only email), returns the body with tags removed | |
260 | def plain_text_body |
|
259 | def plain_text_body | |
261 | return @plain_text_body unless @plain_text_body.nil? |
|
260 | return @plain_text_body unless @plain_text_body.nil? | |
262 | parts = @email.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten |
|
261 | parts = @email.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten | |
263 | if parts.empty? |
|
262 | if parts.empty? | |
264 | parts << @email |
|
263 | parts << @email | |
265 | end |
|
264 | end | |
266 | plain_text_part = parts.detect {|p| p.content_type == 'text/plain'} |
|
265 | plain_text_part = parts.detect {|p| p.content_type == 'text/plain'} | |
267 | if plain_text_part.nil? |
|
266 | if plain_text_part.nil? | |
268 | # no text/plain part found, assuming html-only email |
|
267 | # no text/plain part found, assuming html-only email | |
269 | # strip html tags and remove doctype directive |
|
268 | # strip html tags and remove doctype directive | |
270 | @plain_text_body = strip_tags(@email.body.to_s) |
|
269 | @plain_text_body = strip_tags(@email.body.to_s) | |
271 | @plain_text_body.gsub! %r{^<!DOCTYPE .*$}, '' |
|
270 | @plain_text_body.gsub! %r{^<!DOCTYPE .*$}, '' | |
272 | else |
|
271 | else | |
273 | @plain_text_body = plain_text_part.body.to_s |
|
272 | @plain_text_body = plain_text_part.body.to_s | |
274 | end |
|
273 | end | |
275 | @plain_text_body.strip! |
|
274 | @plain_text_body.strip! | |
276 | @plain_text_body |
|
275 | @plain_text_body | |
277 | end |
|
276 | end | |
278 |
|
277 | |||
279 | def cleaned_up_text_body |
|
278 | def cleaned_up_text_body | |
280 | cleanup_body(plain_text_body) |
|
279 | cleanup_body(plain_text_body) | |
281 | end |
|
280 | end | |
282 |
|
281 | |||
283 | def self.full_sanitizer |
|
282 | def self.full_sanitizer | |
284 | @full_sanitizer ||= HTML::FullSanitizer.new |
|
283 | @full_sanitizer ||= HTML::FullSanitizer.new | |
285 | end |
|
284 | end | |
286 |
|
285 | |||
287 | # Creates a user account for the +email+ sender |
|
286 | # Creates a user account for the +email+ sender | |
288 | def self.create_user_from_email(email) |
|
287 | def self.create_user_from_email(email) | |
289 | addr = email.from_addrs.to_a.first |
|
288 | addr = email.from_addrs.to_a.first | |
290 | if addr && !addr.spec.blank? |
|
289 | if addr && !addr.spec.blank? | |
291 | user = User.new |
|
290 | user = User.new | |
292 | user.mail = addr.spec |
|
291 | user.mail = addr.spec | |
293 |
|
292 | |||
294 | names = addr.name.blank? ? addr.spec.gsub(/@.*$/, '').split('.') : addr.name.split |
|
293 | names = addr.name.blank? ? addr.spec.gsub(/@.*$/, '').split('.') : addr.name.split | |
295 | user.firstname = names.shift |
|
294 | user.firstname = names.shift | |
296 | user.lastname = names.join(' ') |
|
295 | user.lastname = names.join(' ') | |
297 | user.lastname = '-' if user.lastname.blank? |
|
296 | user.lastname = '-' if user.lastname.blank? | |
298 |
|
297 | |||
299 | user.login = user.mail |
|
298 | user.login = user.mail | |
300 | user.password = ActiveSupport::SecureRandom.hex(5) |
|
299 | user.password = ActiveSupport::SecureRandom.hex(5) | |
301 | user.language = Setting.default_language |
|
300 | user.language = Setting.default_language | |
302 | user.save ? user : nil |
|
301 | user.save ? user : nil | |
303 | end |
|
302 | end | |
304 | end |
|
303 | end | |
305 |
|
304 | |||
306 | private |
|
305 | private | |
307 |
|
306 | |||
308 | # Removes the email body of text after the truncation configurations. |
|
307 | # Removes the email body of text after the truncation configurations. | |
309 | def cleanup_body(body) |
|
308 | def cleanup_body(body) | |
310 | delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).map {|s| Regexp.escape(s)} |
|
309 | delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).map {|s| Regexp.escape(s)} | |
311 | unless delimiters.empty? |
|
310 | unless delimiters.empty? | |
312 | regex = Regexp.new("^(#{ delimiters.join('|') })\s*[\r\n].*", Regexp::MULTILINE) |
|
311 | regex = Regexp.new("^(#{ delimiters.join('|') })\s*[\r\n].*", Regexp::MULTILINE) | |
313 | body = body.gsub(regex, '') |
|
312 | body = body.gsub(regex, '') | |
314 | end |
|
313 | end | |
315 | body.strip |
|
314 | body.strip | |
316 | end |
|
315 | end | |
317 | end |
|
316 | end |
General Comments 0
You need to be logged in to leave comments.
Login now