##// END OF EJS Templates
Fixed that MailHandler#plain_text_body was returning nil if there was nothing to strip (#2814)....
Jean-Philippe Lang -
r2456:0f68334f0bed
parent child
Show More
@@ -1,244 +1,245
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 super email
38 38 end
39 39
40 40 # Processes incoming emails
41 41 def receive(email)
42 42 @email = email
43 43 @user = User.active.find_by_mail(email.from.first.to_s.strip)
44 44 unless @user
45 45 # Unknown user => the email is ignored
46 46 # TODO: ability to create the user's account
47 47 logger.info "MailHandler: email submitted by unknown user [#{email.from.first}]" if logger && logger.info
48 48 return false
49 49 end
50 50 User.current = @user
51 51 dispatch
52 52 end
53 53
54 54 private
55 55
56 56 MESSAGE_ID_RE = %r{^<redmine\.([a-z0-9_]+)\-(\d+)\.\d+@}
57 57 ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]+#(\d+)\]}
58 58 MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]+msg(\d+)\]}
59 59
60 60 def dispatch
61 61 headers = [email.in_reply_to, email.references].flatten.compact
62 62 if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE}
63 63 klass, object_id = $1, $2.to_i
64 64 method_name = "receive_#{klass}_reply"
65 65 if self.class.private_instance_methods.include?(method_name)
66 66 send method_name, object_id
67 67 else
68 68 # ignoring it
69 69 end
70 70 elsif m = email.subject.match(ISSUE_REPLY_SUBJECT_RE)
71 71 receive_issue_reply(m[1].to_i)
72 72 elsif m = email.subject.match(MESSAGE_REPLY_SUBJECT_RE)
73 73 receive_message_reply(m[1].to_i)
74 74 else
75 75 receive_issue
76 76 end
77 77 rescue ActiveRecord::RecordInvalid => e
78 78 # TODO: send a email to the user
79 79 logger.error e.message if logger
80 80 false
81 81 rescue MissingInformation => e
82 82 logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger
83 83 false
84 84 rescue UnauthorizedAction => e
85 85 logger.error "MailHandler: unauthorized attempt from #{user}" if logger
86 86 false
87 87 end
88 88
89 89 # Creates a new issue
90 90 def receive_issue
91 91 project = target_project
92 92 tracker = (get_keyword(:tracker) && project.trackers.find_by_name(get_keyword(:tracker))) || project.trackers.find(:first)
93 93 category = (get_keyword(:category) && project.issue_categories.find_by_name(get_keyword(:category)))
94 94 priority = (get_keyword(:priority) && Enumeration.find_by_opt_and_name('IPRI', get_keyword(:priority)))
95 95 status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status)))
96 96
97 97 # check permission
98 98 raise UnauthorizedAction unless user.allowed_to?(:add_issues, project)
99 99 issue = Issue.new(:author => user, :project => project, :tracker => tracker, :category => category, :priority => priority)
100 100 # check workflow
101 101 if status && issue.new_statuses_allowed_to(user).include?(status)
102 102 issue.status = status
103 103 end
104 104 issue.subject = email.subject.chomp.toutf8
105 105 issue.description = plain_text_body
106 106 # custom fields
107 107 issue.custom_field_values = issue.available_custom_fields.inject({}) do |h, c|
108 108 if value = get_keyword(c.name, :override => true)
109 109 h[c.id] = value
110 110 end
111 111 h
112 112 end
113 113 issue.save!
114 114 add_attachments(issue)
115 115 logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info
116 116 # add To and Cc as watchers
117 117 add_watchers(issue)
118 118 # send notification after adding watchers so that they can reply to Redmine
119 119 Mailer.deliver_issue_add(issue) if Setting.notified_events.include?('issue_added')
120 120 issue
121 121 end
122 122
123 123 def target_project
124 124 # TODO: other ways to specify project:
125 125 # * parse the email To field
126 126 # * specific project (eg. Setting.mail_handler_target_project)
127 127 target = Project.find_by_identifier(get_keyword(:project))
128 128 raise MissingInformation.new('Unable to determine target project') if target.nil?
129 129 target
130 130 end
131 131
132 132 # Adds a note to an existing issue
133 133 def receive_issue_reply(issue_id)
134 134 status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status)))
135 135
136 136 issue = Issue.find_by_id(issue_id)
137 137 return unless issue
138 138 # check permission
139 139 raise UnauthorizedAction unless user.allowed_to?(:add_issue_notes, issue.project) || user.allowed_to?(:edit_issues, issue.project)
140 140 raise UnauthorizedAction unless status.nil? || user.allowed_to?(:edit_issues, issue.project)
141 141
142 142 # add the note
143 143 journal = issue.init_journal(user, plain_text_body)
144 144 add_attachments(issue)
145 145 # check workflow
146 146 if status && issue.new_statuses_allowed_to(user).include?(status)
147 147 issue.status = status
148 148 end
149 149 issue.save!
150 150 logger.info "MailHandler: issue ##{issue.id} updated by #{user}" if logger && logger.info
151 151 Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated')
152 152 journal
153 153 end
154 154
155 155 # Reply will be added to the issue
156 156 def receive_journal_reply(journal_id)
157 157 journal = Journal.find_by_id(journal_id)
158 158 if journal && journal.journalized_type == 'Issue'
159 159 receive_issue_reply(journal.journalized_id)
160 160 end
161 161 end
162 162
163 163 # Receives a reply to a forum message
164 164 def receive_message_reply(message_id)
165 165 message = Message.find_by_id(message_id)
166 166 if message
167 167 message = message.root
168 168 if user.allowed_to?(:add_messages, message.project) && !message.locked?
169 169 reply = Message.new(:subject => email.subject.gsub(%r{^.*msg\d+\]}, '').strip,
170 170 :content => plain_text_body)
171 171 reply.author = user
172 172 reply.board = message.board
173 173 message.children << reply
174 174 add_attachments(reply)
175 175 reply
176 176 else
177 177 raise UnauthorizedAction
178 178 end
179 179 end
180 180 end
181 181
182 182 def add_attachments(obj)
183 183 if email.has_attachments?
184 184 email.attachments.each do |attachment|
185 185 Attachment.create(:container => obj,
186 186 :file => attachment,
187 187 :author => user,
188 188 :content_type => attachment.content_type)
189 189 end
190 190 end
191 191 end
192 192
193 193 # Adds To and Cc as watchers of the given object if the sender has the
194 194 # appropriate permission
195 195 def add_watchers(obj)
196 196 if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project)
197 197 addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase}
198 198 unless addresses.empty?
199 199 watchers = User.active.find(:all, :conditions => ['LOWER(mail) IN (?)', addresses])
200 200 watchers.each {|w| obj.add_watcher(w)}
201 201 end
202 202 end
203 203 end
204 204
205 205 def get_keyword(attr, options={})
206 206 @keywords ||= {}
207 207 if @keywords.has_key?(attr)
208 208 @keywords[attr]
209 209 else
210 210 @keywords[attr] = begin
211 211 if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && plain_text_body.gsub!(/^#{attr}:[ \t]*(.+)\s*$/i, '')
212 212 $1.strip
213 213 elsif !@@handler_options[:issue][attr].blank?
214 214 @@handler_options[:issue][attr]
215 215 end
216 216 end
217 217 end
218 218 end
219 219
220 220 # Returns the text/plain part of the email
221 221 # If not found (eg. HTML-only email), returns the body with tags removed
222 222 def plain_text_body
223 223 return @plain_text_body unless @plain_text_body.nil?
224 224 parts = @email.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten
225 225 if parts.empty?
226 226 parts << @email
227 227 end
228 228 plain_text_part = parts.detect {|p| p.content_type == 'text/plain'}
229 229 if plain_text_part.nil?
230 230 # no text/plain part found, assuming html-only email
231 231 # strip html tags and remove doctype directive
232 232 @plain_text_body = strip_tags(@email.body.to_s)
233 233 @plain_text_body.gsub! %r{^<!DOCTYPE .*$}, ''
234 234 else
235 235 @plain_text_body = plain_text_part.body.to_s
236 236 end
237 237 @plain_text_body.strip!
238 @plain_text_body
238 239 end
239 240
240 241
241 242 def self.full_sanitizer
242 243 @full_sanitizer ||= HTML::FullSanitizer.new
243 244 end
244 245 end
General Comments 0
You need to be logged in to leave comments. Login now