##// END OF EJS Templates
Merged r2520 from trunk....
Jean-Philippe Lang -
r2457:69b99aa518b4
parent child
Show More
@@ -1,199 +1,200
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(:first, :conditions => ["LOWER(mail) = ?", email.from.first.to_s.strip.downcase])
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 ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]+#(\d+)\]}
57 57
58 58 def dispatch
59 59 if m = email.subject.match(ISSUE_REPLY_SUBJECT_RE)
60 60 receive_issue_update(m[1].to_i)
61 61 else
62 62 receive_issue
63 63 end
64 64 rescue ActiveRecord::RecordInvalid => e
65 65 # TODO: send a email to the user
66 66 logger.error e.message if logger
67 67 false
68 68 rescue MissingInformation => e
69 69 logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger
70 70 false
71 71 rescue UnauthorizedAction => e
72 72 logger.error "MailHandler: unauthorized attempt from #{user}" if logger
73 73 false
74 74 end
75 75
76 76 # Creates a new issue
77 77 def receive_issue
78 78 project = target_project
79 79 tracker = (get_keyword(:tracker) && project.trackers.find_by_name(get_keyword(:tracker))) || project.trackers.find(:first)
80 80 category = (get_keyword(:category) && project.issue_categories.find_by_name(get_keyword(:category)))
81 81 priority = (get_keyword(:priority) && Enumeration.find_by_opt_and_name('IPRI', get_keyword(:priority)))
82 82 status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status)))
83 83
84 84 # check permission
85 85 raise UnauthorizedAction unless user.allowed_to?(:add_issues, project)
86 86 issue = Issue.new(:author => user, :project => project, :tracker => tracker, :category => category, :priority => priority)
87 87 # check workflow
88 88 if status && issue.new_statuses_allowed_to(user).include?(status)
89 89 issue.status = status
90 90 end
91 91 issue.subject = email.subject.chomp.toutf8
92 92 issue.description = plain_text_body
93 93 # custom fields
94 94 issue.custom_field_values = issue.available_custom_fields.inject({}) do |h, c|
95 95 if value = get_keyword(c.name, :override => true)
96 96 h[c.id] = value
97 97 end
98 98 h
99 99 end
100 100 issue.save!
101 101 add_attachments(issue)
102 102 logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info
103 103 # add To and Cc as watchers
104 104 add_watchers(issue)
105 105 # send notification after adding watchers so that they can reply to Redmine
106 106 Mailer.deliver_issue_add(issue) if Setting.notified_events.include?('issue_added')
107 107 issue
108 108 end
109 109
110 110 def target_project
111 111 # TODO: other ways to specify project:
112 112 # * parse the email To field
113 113 # * specific project (eg. Setting.mail_handler_target_project)
114 114 target = Project.find_by_identifier(get_keyword(:project))
115 115 raise MissingInformation.new('Unable to determine target project') if target.nil?
116 116 target
117 117 end
118 118
119 119 # Adds a note to an existing issue
120 120 def receive_issue_update(issue_id)
121 121 status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status)))
122 122
123 123 issue = Issue.find_by_id(issue_id)
124 124 return unless issue
125 125 # check permission
126 126 raise UnauthorizedAction unless user.allowed_to?(:add_issue_notes, issue.project) || user.allowed_to?(:edit_issues, issue.project)
127 127 raise UnauthorizedAction unless status.nil? || user.allowed_to?(:edit_issues, issue.project)
128 128
129 129 # add the note
130 130 journal = issue.init_journal(user, plain_text_body)
131 131 add_attachments(issue)
132 132 # check workflow
133 133 if status && issue.new_statuses_allowed_to(user).include?(status)
134 134 issue.status = status
135 135 end
136 136 issue.save!
137 137 logger.info "MailHandler: issue ##{issue.id} updated by #{user}" if logger && logger.info
138 138 Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated')
139 139 journal
140 140 end
141 141
142 142 def add_attachments(obj)
143 143 if email.has_attachments?
144 144 email.attachments.each do |attachment|
145 145 Attachment.create(:container => obj,
146 146 :file => attachment,
147 147 :author => user,
148 148 :content_type => attachment.content_type)
149 149 end
150 150 end
151 151 end
152 152
153 153 # Adds To and Cc as watchers of the given object if the sender has the
154 154 # appropriate permission
155 155 def add_watchers(obj)
156 156 if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project)
157 157 addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase}
158 158 unless addresses.empty?
159 159 watchers = User.active.find(:all, :conditions => ['LOWER(mail) IN (?)', addresses])
160 160 watchers.each {|w| obj.add_watcher(w)}
161 161 end
162 162 end
163 163 end
164 164
165 165 def get_keyword(attr, options={})
166 166 @keywords ||= {}
167 167 if @keywords.has_key?(attr)
168 168 @keywords[attr]
169 169 else
170 170 @keywords[attr] = begin
171 171 if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && plain_text_body.gsub!(/^#{attr}:[ \t]*(.+)\s*$/i, '')
172 172 $1.strip
173 173 elsif !@@handler_options[:issue][attr].blank?
174 174 @@handler_options[:issue][attr]
175 175 end
176 176 end
177 177 end
178 178 end
179 179
180 180 # Returns the text/plain part of the email
181 181 # If not found (eg. HTML-only email), returns the body with tags removed
182 182 def plain_text_body
183 183 return @plain_text_body unless @plain_text_body.nil?
184 184 parts = @email.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten
185 185 if parts.empty?
186 186 parts << @email
187 187 end
188 188 plain_text_part = parts.detect {|p| p.content_type == 'text/plain'}
189 189 if plain_text_part.nil?
190 190 # no text/plain part found, assuming html-only email
191 191 # strip html tags and remove doctype directive
192 192 @plain_text_body = strip_tags(@email.body.to_s)
193 193 @plain_text_body.gsub! %r{^<!DOCTYPE .*$}, ''
194 194 else
195 195 @plain_text_body = plain_text_part.body.to_s
196 196 end
197 197 @plain_text_body.strip!
198 @plain_text_body
198 199 end
199 200 end
General Comments 0
You need to be logged in to leave comments. Login now