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