##// END OF EJS Templates
Fixed: broken subject when submitting issue via email written in japanese (Patch #2059 by Go MAEDA)....
Jean-Philippe Lang -
r1988:63c72fe87f90
parent child
Show More
@@ -1,153 +1,153
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
20 20 class UnauthorizedAction < StandardError; end
21 21 class MissingInformation < StandardError; end
22 22
23 23 attr_reader :email, :user
24 24
25 25 def self.receive(email, options={})
26 26 @@handler_options = options.dup
27 27
28 28 @@handler_options[:issue] ||= {}
29 29
30 30 @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip) if @@handler_options[:allow_override].is_a?(String)
31 31 @@handler_options[:allow_override] ||= []
32 32 # Project needs to be overridable if not specified
33 33 @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project)
34 34 # Status needs to be overridable if not specified
35 35 @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status)
36 36 super email
37 37 end
38 38
39 39 # Processes incoming emails
40 40 def receive(email)
41 41 @email = email
42 42 @user = User.find_active(:first, :conditions => ["LOWER(mail) = ?", email.from.first.to_s.strip.downcase])
43 43 unless @user
44 44 # Unknown user => the email is ignored
45 45 # TODO: ability to create the user's account
46 46 logger.info "MailHandler: email submitted by unknown user [#{email.from.first}]" if logger && logger.info
47 47 return false
48 48 end
49 49 User.current = @user
50 50 dispatch
51 51 end
52 52
53 53 private
54 54
55 55 ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]+#(\d+)\]}
56 56
57 57 def dispatch
58 58 if m = email.subject.match(ISSUE_REPLY_SUBJECT_RE)
59 59 receive_issue_update(m[1].to_i)
60 60 else
61 61 receive_issue
62 62 end
63 63 rescue ActiveRecord::RecordInvalid => e
64 64 # TODO: send a email to the user
65 65 logger.error e.message if logger
66 66 false
67 67 rescue MissingInformation => e
68 68 logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger
69 69 false
70 70 rescue UnauthorizedAction => e
71 71 logger.error "MailHandler: unauthorized attempt from #{user}" if logger
72 72 false
73 73 end
74 74
75 75 # Creates a new issue
76 76 def receive_issue
77 77 project = target_project
78 78 tracker = (get_keyword(:tracker) && project.trackers.find_by_name(get_keyword(:tracker))) || project.trackers.find(:first)
79 79 category = (get_keyword(:category) && project.issue_categories.find_by_name(get_keyword(:category)))
80 80 priority = (get_keyword(:priority) && Enumeration.find_by_opt_and_name('IPRI', get_keyword(:priority)))
81 81 status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status))) || IssueStatus.default
82 82
83 83 # check permission
84 84 raise UnauthorizedAction unless user.allowed_to?(:add_issues, project)
85 85 issue = Issue.new(:author => user, :project => project, :tracker => tracker, :category => category, :priority => priority, :status => status)
86 issue.subject = email.subject.chomp
86 issue.subject = email.subject.chomp.toutf8
87 87 issue.description = email.plain_text_body.chomp
88 88 issue.save!
89 89 add_attachments(issue)
90 90 logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info
91 91 Mailer.deliver_issue_add(issue) if Setting.notified_events.include?('issue_added')
92 92 issue
93 93 end
94 94
95 95 def target_project
96 96 # TODO: other ways to specify project:
97 97 # * parse the email To field
98 98 # * specific project (eg. Setting.mail_handler_target_project)
99 99 target = Project.find_by_identifier(get_keyword(:project))
100 100 raise MissingInformation.new('Unable to determine target project') if target.nil?
101 101 target
102 102 end
103 103
104 104 # Adds a note to an existing issue
105 105 def receive_issue_update(issue_id)
106 106 status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status)))
107 107
108 108 issue = Issue.find_by_id(issue_id)
109 109 return unless issue
110 110 # check permission
111 111 raise UnauthorizedAction unless user.allowed_to?(:add_issue_notes, issue.project) || user.allowed_to?(:edit_issues, issue.project)
112 112 raise UnauthorizedAction unless status.nil? || user.allowed_to?(:edit_issues, issue.project)
113 113
114 114 # add the note
115 115 journal = issue.init_journal(user, email.plain_text_body.chomp)
116 116 add_attachments(issue)
117 117 issue.status = status unless status.nil?
118 118 issue.save!
119 119 logger.info "MailHandler: issue ##{issue.id} updated by #{user}" if logger && logger.info
120 120 Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated')
121 121 journal
122 122 end
123 123
124 124 def add_attachments(obj)
125 125 if email.has_attachments?
126 126 email.attachments.each do |attachment|
127 127 Attachment.create(:container => obj,
128 128 :file => attachment,
129 129 :author => user,
130 130 :content_type => attachment.content_type)
131 131 end
132 132 end
133 133 end
134 134
135 135 def get_keyword(attr)
136 136 if @@handler_options[:allow_override].include?(attr.to_s) && email.plain_text_body =~ /^#{attr}:[ \t]*(.+)$/i
137 137 $1.strip
138 138 elsif !@@handler_options[:issue][attr].blank?
139 139 @@handler_options[:issue][attr]
140 140 end
141 141 end
142 142 end
143 143
144 144 class TMail::Mail
145 145 # Returns body of the first plain text part found if any
146 146 def plain_text_body
147 147 return @plain_text_body unless @plain_text_body.nil?
148 148 p = self.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten
149 149 plain = p.detect {|c| c.content_type == 'text/plain'}
150 150 @plain_text_body = plain.nil? ? self.body : plain.body
151 151 end
152 152 end
153 153
General Comments 0
You need to be logged in to leave comments. Login now