@@ -1,153 +1,160 | |||
|
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 |
# Status |
|
|
34 | # Status overridable by default | |
|
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 |
status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status))) |
|
|
81 | status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status))) | |
|
82 | 82 | |
|
83 | 83 | # check permission |
|
84 | 84 | raise UnauthorizedAction unless user.allowed_to?(:add_issues, project) |
|
85 |
issue = Issue.new(:author => user, :project => project, :tracker => tracker, :category => category, :priority => priority |
|
|
85 | issue = Issue.new(:author => user, :project => project, :tracker => tracker, :category => category, :priority => priority) | |
|
86 | # check workflow | |
|
87 | if status && issue.new_statuses_allowed_to(user).include?(status) | |
|
88 | issue.status = status | |
|
89 | end | |
|
86 | 90 | issue.subject = email.subject.chomp.toutf8 |
|
87 | 91 | issue.description = email.plain_text_body.chomp |
|
88 | 92 | issue.save! |
|
89 | 93 | add_attachments(issue) |
|
90 | 94 | logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info |
|
91 | 95 | Mailer.deliver_issue_add(issue) if Setting.notified_events.include?('issue_added') |
|
92 | 96 | issue |
|
93 | 97 | end |
|
94 | 98 | |
|
95 | 99 | def target_project |
|
96 | 100 | # TODO: other ways to specify project: |
|
97 | 101 | # * parse the email To field |
|
98 | 102 | # * specific project (eg. Setting.mail_handler_target_project) |
|
99 | 103 | target = Project.find_by_identifier(get_keyword(:project)) |
|
100 | 104 | raise MissingInformation.new('Unable to determine target project') if target.nil? |
|
101 | 105 | target |
|
102 | 106 | end |
|
103 | 107 | |
|
104 | 108 | # Adds a note to an existing issue |
|
105 | 109 | def receive_issue_update(issue_id) |
|
106 | 110 | status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status))) |
|
107 | 111 | |
|
108 | 112 | issue = Issue.find_by_id(issue_id) |
|
109 | 113 | return unless issue |
|
110 | 114 | # check permission |
|
111 | 115 | raise UnauthorizedAction unless user.allowed_to?(:add_issue_notes, issue.project) || user.allowed_to?(:edit_issues, issue.project) |
|
112 | 116 | raise UnauthorizedAction unless status.nil? || user.allowed_to?(:edit_issues, issue.project) |
|
113 | 117 | |
|
114 | 118 | # add the note |
|
115 | 119 | journal = issue.init_journal(user, email.plain_text_body.chomp) |
|
116 | 120 | add_attachments(issue) |
|
117 | issue.status = status unless status.nil? | |
|
121 | # check workflow | |
|
122 | if status && issue.new_statuses_allowed_to(user).include?(status) | |
|
123 | issue.status = status | |
|
124 | end | |
|
118 | 125 | issue.save! |
|
119 | 126 | logger.info "MailHandler: issue ##{issue.id} updated by #{user}" if logger && logger.info |
|
120 | 127 | Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated') |
|
121 | 128 | journal |
|
122 | 129 | end |
|
123 | 130 | |
|
124 | 131 | def add_attachments(obj) |
|
125 | 132 | if email.has_attachments? |
|
126 | 133 | email.attachments.each do |attachment| |
|
127 | 134 | Attachment.create(:container => obj, |
|
128 | 135 | :file => attachment, |
|
129 | 136 | :author => user, |
|
130 | 137 | :content_type => attachment.content_type) |
|
131 | 138 | end |
|
132 | 139 | end |
|
133 | 140 | end |
|
134 | 141 | |
|
135 | 142 | def get_keyword(attr) |
|
136 | 143 | if @@handler_options[:allow_override].include?(attr.to_s) && email.plain_text_body =~ /^#{attr}:[ \t]*(.+)$/i |
|
137 | 144 | $1.strip |
|
138 | 145 | elsif !@@handler_options[:issue][attr].blank? |
|
139 | 146 | @@handler_options[:issue][attr] |
|
140 | 147 | end |
|
141 | 148 | end |
|
142 | 149 | end |
|
143 | 150 | |
|
144 | 151 | class TMail::Mail |
|
145 | 152 | # Returns body of the first plain text part found if any |
|
146 | 153 | def plain_text_body |
|
147 | 154 | return @plain_text_body unless @plain_text_body.nil? |
|
148 | 155 | p = self.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten |
|
149 | 156 | plain = p.detect {|c| c.content_type == 'text/plain'} |
|
150 | 157 | @plain_text_body = plain.nil? ? self.body : plain.body |
|
151 | 158 | end |
|
152 | 159 | end |
|
153 | 160 |
General Comments 0
You need to be logged in to leave comments.
Login now