@@ -1,95 +1,95 | |||||
1 | source 'https://rubygems.org' |
|
1 | source 'https://rubygems.org' | |
2 |
|
2 | |||
3 |
gem |
|
3 | gem "rails", "3.2.13" | |
4 | gem "jquery-rails", "~> 2.0.2" |
|
4 | gem "jquery-rails", "~> 2.0.2" | |
5 | gem "i18n", "~> 0.6.0" |
|
5 | gem "i18n", "~> 0.6.0" | |
6 | gem "coderay", "~> 1.0.6" |
|
6 | gem "coderay", "~> 1.0.6" | |
7 | gem "fastercsv", "~> 1.5.0", :platforms => [:mri_18, :mingw_18, :jruby] |
|
7 | gem "fastercsv", "~> 1.5.0", :platforms => [:mri_18, :mingw_18, :jruby] | |
8 | gem "builder", "3.0.0" |
|
8 | gem "builder", "3.0.0" | |
9 |
|
9 | |||
10 | # Optional gem for LDAP authentication |
|
10 | # Optional gem for LDAP authentication | |
11 | group :ldap do |
|
11 | group :ldap do | |
12 | gem "net-ldap", "~> 0.3.1" |
|
12 | gem "net-ldap", "~> 0.3.1" | |
13 | end |
|
13 | end | |
14 |
|
14 | |||
15 | # Optional gem for OpenID authentication |
|
15 | # Optional gem for OpenID authentication | |
16 | group :openid do |
|
16 | group :openid do | |
17 | gem "ruby-openid", "~> 2.1.4", :require => "openid" |
|
17 | gem "ruby-openid", "~> 2.1.4", :require => "openid" | |
18 | gem "rack-openid" |
|
18 | gem "rack-openid" | |
19 | end |
|
19 | end | |
20 |
|
20 | |||
21 | # Optional gem for exporting the gantt to a PNG file, not supported with jruby |
|
21 | # Optional gem for exporting the gantt to a PNG file, not supported with jruby | |
22 | platforms :mri, :mingw do |
|
22 | platforms :mri, :mingw do | |
23 | group :rmagick do |
|
23 | group :rmagick do | |
24 | # RMagick 2 supports ruby 1.9 |
|
24 | # RMagick 2 supports ruby 1.9 | |
25 | # RMagick 1 would be fine for ruby 1.8 but Bundler does not support |
|
25 | # RMagick 1 would be fine for ruby 1.8 but Bundler does not support | |
26 | # different requirements for the same gem on different platforms |
|
26 | # different requirements for the same gem on different platforms | |
27 | gem "rmagick", ">= 2.0.0" |
|
27 | gem "rmagick", ">= 2.0.0" | |
28 | end |
|
28 | end | |
29 | end |
|
29 | end | |
30 |
|
30 | |||
31 | platforms :jruby do |
|
31 | platforms :jruby do | |
32 | # jruby-openssl is bundled with JRuby 1.7.0 |
|
32 | # jruby-openssl is bundled with JRuby 1.7.0 | |
33 | gem "jruby-openssl" if Object.const_defined?(:JRUBY_VERSION) && JRUBY_VERSION < '1.7.0' |
|
33 | gem "jruby-openssl" if Object.const_defined?(:JRUBY_VERSION) && JRUBY_VERSION < '1.7.0' | |
34 | gem "activerecord-jdbc-adapter", "1.2.5" |
|
34 | gem "activerecord-jdbc-adapter", "1.2.5" | |
35 | end |
|
35 | end | |
36 |
|
36 | |||
37 | # Include database gems for the adapters found in the database |
|
37 | # Include database gems for the adapters found in the database | |
38 | # configuration file |
|
38 | # configuration file | |
39 | require 'erb' |
|
39 | require 'erb' | |
40 | require 'yaml' |
|
40 | require 'yaml' | |
41 | database_file = File.join(File.dirname(__FILE__), "config/database.yml") |
|
41 | database_file = File.join(File.dirname(__FILE__), "config/database.yml") | |
42 | if File.exist?(database_file) |
|
42 | if File.exist?(database_file) | |
43 | database_config = YAML::load(ERB.new(IO.read(database_file)).result) |
|
43 | database_config = YAML::load(ERB.new(IO.read(database_file)).result) | |
44 | adapters = database_config.values.map {|c| c['adapter']}.compact.uniq |
|
44 | adapters = database_config.values.map {|c| c['adapter']}.compact.uniq | |
45 | if adapters.any? |
|
45 | if adapters.any? | |
46 | adapters.each do |adapter| |
|
46 | adapters.each do |adapter| | |
47 | case adapter |
|
47 | case adapter | |
48 | when 'mysql2' |
|
48 | when 'mysql2' | |
49 | gem "mysql2", "~> 0.3.11", :platforms => [:mri, :mingw] |
|
49 | gem "mysql2", "~> 0.3.11", :platforms => [:mri, :mingw] | |
50 | gem "activerecord-jdbcmysql-adapter", :platforms => :jruby |
|
50 | gem "activerecord-jdbcmysql-adapter", :platforms => :jruby | |
51 | when 'mysql' |
|
51 | when 'mysql' | |
52 | gem "mysql", "~> 2.8.1", :platforms => [:mri, :mingw] |
|
52 | gem "mysql", "~> 2.8.1", :platforms => [:mri, :mingw] | |
53 | gem "activerecord-jdbcmysql-adapter", :platforms => :jruby |
|
53 | gem "activerecord-jdbcmysql-adapter", :platforms => :jruby | |
54 | when /postgresql/ |
|
54 | when /postgresql/ | |
55 | gem "pg", ">= 0.11.0", :platforms => [:mri, :mingw] |
|
55 | gem "pg", ">= 0.11.0", :platforms => [:mri, :mingw] | |
56 | gem "activerecord-jdbcpostgresql-adapter", :platforms => :jruby |
|
56 | gem "activerecord-jdbcpostgresql-adapter", :platforms => :jruby | |
57 | when /sqlite3/ |
|
57 | when /sqlite3/ | |
58 | gem "sqlite3", :platforms => [:mri, :mingw] |
|
58 | gem "sqlite3", :platforms => [:mri, :mingw] | |
59 | gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby |
|
59 | gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby | |
60 | when /sqlserver/ |
|
60 | when /sqlserver/ | |
61 | gem "tiny_tds", "~> 0.5.1", :platforms => [:mri, :mingw] |
|
61 | gem "tiny_tds", "~> 0.5.1", :platforms => [:mri, :mingw] | |
62 | gem "activerecord-sqlserver-adapter", :platforms => [:mri, :mingw] |
|
62 | gem "activerecord-sqlserver-adapter", :platforms => [:mri, :mingw] | |
63 | else |
|
63 | else | |
64 | warn("Unknown database adapter `#{adapter}` found in config/database.yml, use Gemfile.local to load your own database gems") |
|
64 | warn("Unknown database adapter `#{adapter}` found in config/database.yml, use Gemfile.local to load your own database gems") | |
65 | end |
|
65 | end | |
66 | end |
|
66 | end | |
67 | else |
|
67 | else | |
68 | warn("No adapter found in config/database.yml, please configure it first") |
|
68 | warn("No adapter found in config/database.yml, please configure it first") | |
69 | end |
|
69 | end | |
70 | else |
|
70 | else | |
71 | warn("Please configure your config/database.yml first") |
|
71 | warn("Please configure your config/database.yml first") | |
72 | end |
|
72 | end | |
73 |
|
73 | |||
74 | group :development do |
|
74 | group :development do | |
75 | gem "rdoc", ">= 2.4.2" |
|
75 | gem "rdoc", ">= 2.4.2" | |
76 | gem "yard" |
|
76 | gem "yard" | |
77 | end |
|
77 | end | |
78 |
|
78 | |||
79 | group :test do |
|
79 | group :test do | |
80 | gem "shoulda", "~> 3.3.2" |
|
80 | gem "shoulda", "~> 3.3.2" | |
81 | gem "mocha" |
|
81 | gem "mocha", "~> 0.13.3" | |
82 | gem 'capybara', '~> 2.0.0' |
|
82 | gem 'capybara', '~> 2.0.0' | |
83 | end |
|
83 | end | |
84 |
|
84 | |||
85 | local_gemfile = File.join(File.dirname(__FILE__), "Gemfile.local") |
|
85 | local_gemfile = File.join(File.dirname(__FILE__), "Gemfile.local") | |
86 | if File.exists?(local_gemfile) |
|
86 | if File.exists?(local_gemfile) | |
87 | puts "Loading Gemfile.local ..." if $DEBUG # `ruby -d` or `bundle -v` |
|
87 | puts "Loading Gemfile.local ..." if $DEBUG # `ruby -d` or `bundle -v` | |
88 | instance_eval File.read(local_gemfile) |
|
88 | instance_eval File.read(local_gemfile) | |
89 | end |
|
89 | end | |
90 |
|
90 | |||
91 | # Load plugins' Gemfiles |
|
91 | # Load plugins' Gemfiles | |
92 | Dir.glob File.expand_path("../plugins/*/Gemfile", __FILE__) do |file| |
|
92 | Dir.glob File.expand_path("../plugins/*/Gemfile", __FILE__) do |file| | |
93 | puts "Loading #{file} ..." if $DEBUG # `ruby -d` or `bundle -v` |
|
93 | puts "Loading #{file} ..." if $DEBUG # `ruby -d` or `bundle -v` | |
94 | instance_eval File.read(file) |
|
94 | instance_eval File.read(file) | |
95 | end |
|
95 | end |
@@ -1,520 +1,490 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class MailHandler < ActionMailer::Base |
|
18 | class MailHandler < ActionMailer::Base | |
19 | include ActionView::Helpers::SanitizeHelper |
|
19 | include ActionView::Helpers::SanitizeHelper | |
20 | include Redmine::I18n |
|
20 | include Redmine::I18n | |
21 |
|
21 | |||
22 | class UnauthorizedAction < StandardError; end |
|
22 | class UnauthorizedAction < StandardError; end | |
23 | class MissingInformation < StandardError; end |
|
23 | class MissingInformation < StandardError; end | |
24 |
|
24 | |||
25 | attr_reader :email, :user |
|
25 | attr_reader :email, :user | |
26 |
|
26 | |||
27 | def self.receive(email, options={}) |
|
27 | def self.receive(email, options={}) | |
28 | @@handler_options = options.dup |
|
28 | @@handler_options = options.dup | |
29 |
|
29 | |||
30 | @@handler_options[:issue] ||= {} |
|
30 | @@handler_options[:issue] ||= {} | |
31 |
|
31 | |||
32 | if @@handler_options[:allow_override].is_a?(String) |
|
32 | if @@handler_options[:allow_override].is_a?(String) | |
33 | @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip) |
|
33 | @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip) | |
34 | end |
|
34 | end | |
35 | @@handler_options[:allow_override] ||= [] |
|
35 | @@handler_options[:allow_override] ||= [] | |
36 | # Project needs to be overridable if not specified |
|
36 | # Project needs to be overridable if not specified | |
37 | @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project) |
|
37 | @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project) | |
38 | # Status overridable by default |
|
38 | # Status overridable by default | |
39 | @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status) |
|
39 | @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status) | |
40 |
|
40 | |||
41 | @@handler_options[:no_account_notice] = (@@handler_options[:no_account_notice].to_s == '1') |
|
41 | @@handler_options[:no_account_notice] = (@@handler_options[:no_account_notice].to_s == '1') | |
42 | @@handler_options[:no_notification] = (@@handler_options[:no_notification].to_s == '1') |
|
42 | @@handler_options[:no_notification] = (@@handler_options[:no_notification].to_s == '1') | |
43 | @@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1') |
|
43 | @@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1') | |
44 |
|
44 | |||
45 | email.force_encoding('ASCII-8BIT') if email.respond_to?(:force_encoding) |
|
45 | email.force_encoding('ASCII-8BIT') if email.respond_to?(:force_encoding) | |
46 | super(email) |
|
46 | super(email) | |
47 | end |
|
47 | end | |
48 |
|
48 | |||
49 | def logger |
|
49 | def logger | |
50 | Rails.logger |
|
50 | Rails.logger | |
51 | end |
|
51 | end | |
52 |
|
52 | |||
53 | cattr_accessor :ignored_emails_headers |
|
53 | cattr_accessor :ignored_emails_headers | |
54 | @@ignored_emails_headers = { |
|
54 | @@ignored_emails_headers = { | |
55 | 'X-Auto-Response-Suppress' => 'oof', |
|
55 | 'X-Auto-Response-Suppress' => 'oof', | |
56 | 'Auto-Submitted' => /^auto-/ |
|
56 | 'Auto-Submitted' => /^auto-/ | |
57 | } |
|
57 | } | |
58 |
|
58 | |||
59 | # Processes incoming emails |
|
59 | # Processes incoming emails | |
60 | # Returns the created object (eg. an issue, a message) or false |
|
60 | # Returns the created object (eg. an issue, a message) or false | |
61 | def receive(email) |
|
61 | def receive(email) | |
62 | @email = email |
|
62 | @email = email | |
63 | sender_email = email.from.to_a.first.to_s.strip |
|
63 | sender_email = email.from.to_a.first.to_s.strip | |
64 | # Ignore emails received from the application emission address to avoid hell cycles |
|
64 | # Ignore emails received from the application emission address to avoid hell cycles | |
65 | if sender_email.downcase == Setting.mail_from.to_s.strip.downcase |
|
65 | if sender_email.downcase == Setting.mail_from.to_s.strip.downcase | |
66 | if logger && logger.info |
|
66 | if logger && logger.info | |
67 | logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]" |
|
67 | logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]" | |
68 | end |
|
68 | end | |
69 | return false |
|
69 | return false | |
70 | end |
|
70 | end | |
71 | # Ignore auto generated emails |
|
71 | # Ignore auto generated emails | |
72 | self.class.ignored_emails_headers.each do |key, ignored_value| |
|
72 | self.class.ignored_emails_headers.each do |key, ignored_value| | |
73 | value = email.header[key] |
|
73 | value = email.header[key] | |
74 | if value |
|
74 | if value | |
75 | value = value.to_s.downcase |
|
75 | value = value.to_s.downcase | |
76 | if (ignored_value.is_a?(Regexp) && value.match(ignored_value)) || value == ignored_value |
|
76 | if (ignored_value.is_a?(Regexp) && value.match(ignored_value)) || value == ignored_value | |
77 | if logger && logger.info |
|
77 | if logger && logger.info | |
78 | logger.info "MailHandler: ignoring email with #{key}:#{value} header" |
|
78 | logger.info "MailHandler: ignoring email with #{key}:#{value} header" | |
79 | end |
|
79 | end | |
80 | return false |
|
80 | return false | |
81 | end |
|
81 | end | |
82 | end |
|
82 | end | |
83 | end |
|
83 | end | |
84 | @user = User.find_by_mail(sender_email) if sender_email.present? |
|
84 | @user = User.find_by_mail(sender_email) if sender_email.present? | |
85 | if @user && !@user.active? |
|
85 | if @user && !@user.active? | |
86 | if logger && logger.info |
|
86 | if logger && logger.info | |
87 | logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]" |
|
87 | logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]" | |
88 | end |
|
88 | end | |
89 | return false |
|
89 | return false | |
90 | end |
|
90 | end | |
91 | if @user.nil? |
|
91 | if @user.nil? | |
92 | # Email was submitted by an unknown user |
|
92 | # Email was submitted by an unknown user | |
93 | case @@handler_options[:unknown_user] |
|
93 | case @@handler_options[:unknown_user] | |
94 | when 'accept' |
|
94 | when 'accept' | |
95 | @user = User.anonymous |
|
95 | @user = User.anonymous | |
96 | when 'create' |
|
96 | when 'create' | |
97 | @user = create_user_from_email |
|
97 | @user = create_user_from_email | |
98 | if @user |
|
98 | if @user | |
99 | if logger && logger.info |
|
99 | if logger && logger.info | |
100 | logger.info "MailHandler: [#{@user.login}] account created" |
|
100 | logger.info "MailHandler: [#{@user.login}] account created" | |
101 | end |
|
101 | end | |
102 | add_user_to_group(@@handler_options[:default_group]) |
|
102 | add_user_to_group(@@handler_options[:default_group]) | |
103 | unless @@handler_options[:no_account_notice] |
|
103 | unless @@handler_options[:no_account_notice] | |
104 | Mailer.account_information(@user, @user.password).deliver |
|
104 | Mailer.account_information(@user, @user.password).deliver | |
105 | end |
|
105 | end | |
106 | else |
|
106 | else | |
107 | if logger && logger.error |
|
107 | if logger && logger.error | |
108 | logger.error "MailHandler: could not create account for [#{sender_email}]" |
|
108 | logger.error "MailHandler: could not create account for [#{sender_email}]" | |
109 | end |
|
109 | end | |
110 | return false |
|
110 | return false | |
111 | end |
|
111 | end | |
112 | else |
|
112 | else | |
113 | # Default behaviour, emails from unknown users are ignored |
|
113 | # Default behaviour, emails from unknown users are ignored | |
114 | if logger && logger.info |
|
114 | if logger && logger.info | |
115 | logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]" |
|
115 | logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]" | |
116 | end |
|
116 | end | |
117 | return false |
|
117 | return false | |
118 | end |
|
118 | end | |
119 | end |
|
119 | end | |
120 | User.current = @user |
|
120 | User.current = @user | |
121 | dispatch |
|
121 | dispatch | |
122 | end |
|
122 | end | |
123 |
|
123 | |||
124 | private |
|
124 | private | |
125 |
|
125 | |||
126 | MESSAGE_ID_RE = %r{^<?redmine\.([a-z0-9_]+)\-(\d+)\.\d+@} |
|
126 | MESSAGE_ID_RE = %r{^<?redmine\.([a-z0-9_]+)\-(\d+)\.\d+@} | |
127 | ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]*#(\d+)\]} |
|
127 | ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]*#(\d+)\]} | |
128 | MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]*msg(\d+)\]} |
|
128 | MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]*msg(\d+)\]} | |
129 |
|
129 | |||
130 | def dispatch |
|
130 | def dispatch | |
131 | headers = [email.in_reply_to, email.references].flatten.compact |
|
131 | headers = [email.in_reply_to, email.references].flatten.compact | |
132 | subject = email.subject.to_s |
|
132 | subject = email.subject.to_s | |
133 | if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE} |
|
133 | if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE} | |
134 | klass, object_id = $1, $2.to_i |
|
134 | klass, object_id = $1, $2.to_i | |
135 | method_name = "receive_#{klass}_reply" |
|
135 | method_name = "receive_#{klass}_reply" | |
136 | if self.class.private_instance_methods.collect(&:to_s).include?(method_name) |
|
136 | if self.class.private_instance_methods.collect(&:to_s).include?(method_name) | |
137 | send method_name, object_id |
|
137 | send method_name, object_id | |
138 | else |
|
138 | else | |
139 | # ignoring it |
|
139 | # ignoring it | |
140 | end |
|
140 | end | |
141 | elsif m = subject.match(ISSUE_REPLY_SUBJECT_RE) |
|
141 | elsif m = subject.match(ISSUE_REPLY_SUBJECT_RE) | |
142 | receive_issue_reply(m[1].to_i) |
|
142 | receive_issue_reply(m[1].to_i) | |
143 | elsif m = subject.match(MESSAGE_REPLY_SUBJECT_RE) |
|
143 | elsif m = subject.match(MESSAGE_REPLY_SUBJECT_RE) | |
144 | receive_message_reply(m[1].to_i) |
|
144 | receive_message_reply(m[1].to_i) | |
145 | else |
|
145 | else | |
146 | dispatch_to_default |
|
146 | dispatch_to_default | |
147 | end |
|
147 | end | |
148 | rescue ActiveRecord::RecordInvalid => e |
|
148 | rescue ActiveRecord::RecordInvalid => e | |
149 | # TODO: send a email to the user |
|
149 | # TODO: send a email to the user | |
150 | logger.error e.message if logger |
|
150 | logger.error e.message if logger | |
151 | false |
|
151 | false | |
152 | rescue MissingInformation => e |
|
152 | rescue MissingInformation => e | |
153 | logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger |
|
153 | logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger | |
154 | false |
|
154 | false | |
155 | rescue UnauthorizedAction => e |
|
155 | rescue UnauthorizedAction => e | |
156 | logger.error "MailHandler: unauthorized attempt from #{user}" if logger |
|
156 | logger.error "MailHandler: unauthorized attempt from #{user}" if logger | |
157 | false |
|
157 | false | |
158 | end |
|
158 | end | |
159 |
|
159 | |||
160 | def dispatch_to_default |
|
160 | def dispatch_to_default | |
161 | receive_issue |
|
161 | receive_issue | |
162 | end |
|
162 | end | |
163 |
|
163 | |||
164 | # Creates a new issue |
|
164 | # Creates a new issue | |
165 | def receive_issue |
|
165 | def receive_issue | |
166 | project = target_project |
|
166 | project = target_project | |
167 | # check permission |
|
167 | # check permission | |
168 | unless @@handler_options[:no_permission_check] |
|
168 | unless @@handler_options[:no_permission_check] | |
169 | raise UnauthorizedAction unless user.allowed_to?(:add_issues, project) |
|
169 | raise UnauthorizedAction unless user.allowed_to?(:add_issues, project) | |
170 | end |
|
170 | end | |
171 |
|
171 | |||
172 | issue = Issue.new(:author => user, :project => project) |
|
172 | issue = Issue.new(:author => user, :project => project) | |
173 | issue.safe_attributes = issue_attributes_from_keywords(issue) |
|
173 | issue.safe_attributes = issue_attributes_from_keywords(issue) | |
174 | issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} |
|
174 | issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} | |
175 | issue.subject = cleaned_up_subject |
|
175 | issue.subject = cleaned_up_subject | |
176 | if issue.subject.blank? |
|
176 | if issue.subject.blank? | |
177 | issue.subject = '(no subject)' |
|
177 | issue.subject = '(no subject)' | |
178 | end |
|
178 | end | |
179 | issue.description = cleaned_up_text_body |
|
179 | issue.description = cleaned_up_text_body | |
180 |
|
180 | |||
181 | # add To and Cc as watchers before saving so the watchers can reply to Redmine |
|
181 | # add To and Cc as watchers before saving so the watchers can reply to Redmine | |
182 | add_watchers(issue) |
|
182 | add_watchers(issue) | |
183 | issue.save! |
|
183 | issue.save! | |
184 | add_attachments(issue) |
|
184 | add_attachments(issue) | |
185 | logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info |
|
185 | logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info | |
186 | issue |
|
186 | issue | |
187 | end |
|
187 | end | |
188 |
|
188 | |||
189 | # Adds a note to an existing issue |
|
189 | # Adds a note to an existing issue | |
190 | def receive_issue_reply(issue_id, from_journal=nil) |
|
190 | def receive_issue_reply(issue_id, from_journal=nil) | |
191 | issue = Issue.find_by_id(issue_id) |
|
191 | issue = Issue.find_by_id(issue_id) | |
192 | return unless issue |
|
192 | return unless issue | |
193 | # check permission |
|
193 | # check permission | |
194 | unless @@handler_options[:no_permission_check] |
|
194 | unless @@handler_options[:no_permission_check] | |
195 | unless user.allowed_to?(:add_issue_notes, issue.project) || |
|
195 | unless user.allowed_to?(:add_issue_notes, issue.project) || | |
196 | user.allowed_to?(:edit_issues, issue.project) |
|
196 | user.allowed_to?(:edit_issues, issue.project) | |
197 | raise UnauthorizedAction |
|
197 | raise UnauthorizedAction | |
198 | end |
|
198 | end | |
199 | end |
|
199 | end | |
200 |
|
200 | |||
201 | # ignore CLI-supplied defaults for new issues |
|
201 | # ignore CLI-supplied defaults for new issues | |
202 | @@handler_options[:issue].clear |
|
202 | @@handler_options[:issue].clear | |
203 |
|
203 | |||
204 | journal = issue.init_journal(user) |
|
204 | journal = issue.init_journal(user) | |
205 | if from_journal && from_journal.private_notes? |
|
205 | if from_journal && from_journal.private_notes? | |
206 | # If the received email was a reply to a private note, make the added note private |
|
206 | # If the received email was a reply to a private note, make the added note private | |
207 | issue.private_notes = true |
|
207 | issue.private_notes = true | |
208 | end |
|
208 | end | |
209 | issue.safe_attributes = issue_attributes_from_keywords(issue) |
|
209 | issue.safe_attributes = issue_attributes_from_keywords(issue) | |
210 | issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} |
|
210 | issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} | |
211 | journal.notes = cleaned_up_text_body |
|
211 | journal.notes = cleaned_up_text_body | |
212 | add_attachments(issue) |
|
212 | add_attachments(issue) | |
213 | issue.save! |
|
213 | issue.save! | |
214 | if logger && logger.info |
|
214 | if logger && logger.info | |
215 | logger.info "MailHandler: issue ##{issue.id} updated by #{user}" |
|
215 | logger.info "MailHandler: issue ##{issue.id} updated by #{user}" | |
216 | end |
|
216 | end | |
217 | journal |
|
217 | journal | |
218 | end |
|
218 | end | |
219 |
|
219 | |||
220 | # Reply will be added to the issue |
|
220 | # Reply will be added to the issue | |
221 | def receive_journal_reply(journal_id) |
|
221 | def receive_journal_reply(journal_id) | |
222 | journal = Journal.find_by_id(journal_id) |
|
222 | journal = Journal.find_by_id(journal_id) | |
223 | if journal && journal.journalized_type == 'Issue' |
|
223 | if journal && journal.journalized_type == 'Issue' | |
224 | receive_issue_reply(journal.journalized_id, journal) |
|
224 | receive_issue_reply(journal.journalized_id, journal) | |
225 | end |
|
225 | end | |
226 | end |
|
226 | end | |
227 |
|
227 | |||
228 | # Receives a reply to a forum message |
|
228 | # Receives a reply to a forum message | |
229 | def receive_message_reply(message_id) |
|
229 | def receive_message_reply(message_id) | |
230 | message = Message.find_by_id(message_id) |
|
230 | message = Message.find_by_id(message_id) | |
231 | if message |
|
231 | if message | |
232 | message = message.root |
|
232 | message = message.root | |
233 |
|
233 | |||
234 | unless @@handler_options[:no_permission_check] |
|
234 | unless @@handler_options[:no_permission_check] | |
235 | raise UnauthorizedAction unless user.allowed_to?(:add_messages, message.project) |
|
235 | raise UnauthorizedAction unless user.allowed_to?(:add_messages, message.project) | |
236 | end |
|
236 | end | |
237 |
|
237 | |||
238 | if !message.locked? |
|
238 | if !message.locked? | |
239 | reply = Message.new(:subject => cleaned_up_subject.gsub(%r{^.*msg\d+\]}, '').strip, |
|
239 | reply = Message.new(:subject => cleaned_up_subject.gsub(%r{^.*msg\d+\]}, '').strip, | |
240 | :content => cleaned_up_text_body) |
|
240 | :content => cleaned_up_text_body) | |
241 | reply.author = user |
|
241 | reply.author = user | |
242 | reply.board = message.board |
|
242 | reply.board = message.board | |
243 | message.children << reply |
|
243 | message.children << reply | |
244 | add_attachments(reply) |
|
244 | add_attachments(reply) | |
245 | reply |
|
245 | reply | |
246 | else |
|
246 | else | |
247 | if logger && logger.info |
|
247 | if logger && logger.info | |
248 | logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic" |
|
248 | logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic" | |
249 | end |
|
249 | end | |
250 | end |
|
250 | end | |
251 | end |
|
251 | end | |
252 | end |
|
252 | end | |
253 |
|
253 | |||
254 | def add_attachments(obj) |
|
254 | def add_attachments(obj) | |
255 | if email.attachments && email.attachments.any? |
|
255 | if email.attachments && email.attachments.any? | |
256 | email.attachments.each do |attachment| |
|
256 | email.attachments.each do |attachment| | |
257 | filename = attachment.filename |
|
|||
258 | unless filename.respond_to?(:encoding) |
|
|||
259 | # try to reencode to utf8 manually with ruby1.8 |
|
|||
260 | h = attachment.header['Content-Disposition'] |
|
|||
261 | unless h.nil? |
|
|||
262 | begin |
|
|||
263 | if m = h.value.match(/filename\*[0-9\*]*=([^=']+)'/) |
|
|||
264 | filename = Redmine::CodesetUtil.to_utf8(filename, m[1]) |
|
|||
265 | elsif m = h.value.match(/filename=.*=\?([^\?]+)\?[BbQq]\?/) |
|
|||
266 | # http://tools.ietf.org/html/rfc2047#section-4 |
|
|||
267 | filename = Redmine::CodesetUtil.to_utf8(filename, m[1]) |
|
|||
268 | end |
|
|||
269 | rescue |
|
|||
270 | # nop |
|
|||
271 | end |
|
|||
272 | end |
|
|||
273 | end |
|
|||
274 | obj.attachments << Attachment.create(:container => obj, |
|
257 | obj.attachments << Attachment.create(:container => obj, | |
275 | :file => attachment.decoded, |
|
258 | :file => attachment.decoded, | |
276 | :filename => filename, |
|
259 | :filename => attachment.filename, | |
277 | :author => user, |
|
260 | :author => user, | |
278 | :content_type => attachment.mime_type) |
|
261 | :content_type => attachment.mime_type) | |
279 | end |
|
262 | end | |
280 | end |
|
263 | end | |
281 | end |
|
264 | end | |
282 |
|
265 | |||
283 | # Adds To and Cc as watchers of the given object if the sender has the |
|
266 | # Adds To and Cc as watchers of the given object if the sender has the | |
284 | # appropriate permission |
|
267 | # appropriate permission | |
285 | def add_watchers(obj) |
|
268 | def add_watchers(obj) | |
286 | if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project) |
|
269 | if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project) | |
287 | addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase} |
|
270 | addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase} | |
288 | unless addresses.empty? |
|
271 | unless addresses.empty? | |
289 | watchers = User.active.where('LOWER(mail) IN (?)', addresses).all |
|
272 | watchers = User.active.where('LOWER(mail) IN (?)', addresses).all | |
290 | watchers.each {|w| obj.add_watcher(w)} |
|
273 | watchers.each {|w| obj.add_watcher(w)} | |
291 | end |
|
274 | end | |
292 | end |
|
275 | end | |
293 | end |
|
276 | end | |
294 |
|
277 | |||
295 | def get_keyword(attr, options={}) |
|
278 | def get_keyword(attr, options={}) | |
296 | @keywords ||= {} |
|
279 | @keywords ||= {} | |
297 | if @keywords.has_key?(attr) |
|
280 | if @keywords.has_key?(attr) | |
298 | @keywords[attr] |
|
281 | @keywords[attr] | |
299 | else |
|
282 | else | |
300 | @keywords[attr] = begin |
|
283 | @keywords[attr] = begin | |
301 | if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && |
|
284 | if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && | |
302 | (v = extract_keyword!(plain_text_body, attr, options[:format])) |
|
285 | (v = extract_keyword!(plain_text_body, attr, options[:format])) | |
303 | v |
|
286 | v | |
304 | elsif !@@handler_options[:issue][attr].blank? |
|
287 | elsif !@@handler_options[:issue][attr].blank? | |
305 | @@handler_options[:issue][attr] |
|
288 | @@handler_options[:issue][attr] | |
306 | end |
|
289 | end | |
307 | end |
|
290 | end | |
308 | end |
|
291 | end | |
309 | end |
|
292 | end | |
310 |
|
293 | |||
311 | # Destructively extracts the value for +attr+ in +text+ |
|
294 | # Destructively extracts the value for +attr+ in +text+ | |
312 | # Returns nil if no matching keyword found |
|
295 | # Returns nil if no matching keyword found | |
313 | def extract_keyword!(text, attr, format=nil) |
|
296 | def extract_keyword!(text, attr, format=nil) | |
314 | keys = [attr.to_s.humanize] |
|
297 | keys = [attr.to_s.humanize] | |
315 | if attr.is_a?(Symbol) |
|
298 | if attr.is_a?(Symbol) | |
316 | if user && user.language.present? |
|
299 | if user && user.language.present? | |
317 | keys << l("field_#{attr}", :default => '', :locale => user.language) |
|
300 | keys << l("field_#{attr}", :default => '', :locale => user.language) | |
318 | end |
|
301 | end | |
319 | if Setting.default_language.present? |
|
302 | if Setting.default_language.present? | |
320 | keys << l("field_#{attr}", :default => '', :locale => Setting.default_language) |
|
303 | keys << l("field_#{attr}", :default => '', :locale => Setting.default_language) | |
321 | end |
|
304 | end | |
322 | end |
|
305 | end | |
323 | keys.reject! {|k| k.blank?} |
|
306 | keys.reject! {|k| k.blank?} | |
324 | keys.collect! {|k| Regexp.escape(k)} |
|
307 | keys.collect! {|k| Regexp.escape(k)} | |
325 | format ||= '.+' |
|
308 | format ||= '.+' | |
326 | keyword = nil |
|
309 | keyword = nil | |
327 | regexp = /^(#{keys.join('|')})[ \t]*:[ \t]*(#{format})\s*$/i |
|
310 | regexp = /^(#{keys.join('|')})[ \t]*:[ \t]*(#{format})\s*$/i | |
328 | if m = text.match(regexp) |
|
311 | if m = text.match(regexp) | |
329 | keyword = m[2].strip |
|
312 | keyword = m[2].strip | |
330 | text.gsub!(regexp, '') |
|
313 | text.gsub!(regexp, '') | |
331 | end |
|
314 | end | |
332 | keyword |
|
315 | keyword | |
333 | end |
|
316 | end | |
334 |
|
317 | |||
335 | def target_project |
|
318 | def target_project | |
336 | # TODO: other ways to specify project: |
|
319 | # TODO: other ways to specify project: | |
337 | # * parse the email To field |
|
320 | # * parse the email To field | |
338 | # * specific project (eg. Setting.mail_handler_target_project) |
|
321 | # * specific project (eg. Setting.mail_handler_target_project) | |
339 | target = Project.find_by_identifier(get_keyword(:project)) |
|
322 | target = Project.find_by_identifier(get_keyword(:project)) | |
340 | raise MissingInformation.new('Unable to determine target project') if target.nil? |
|
323 | raise MissingInformation.new('Unable to determine target project') if target.nil? | |
341 | target |
|
324 | target | |
342 | end |
|
325 | end | |
343 |
|
326 | |||
344 | # Returns a Hash of issue attributes extracted from keywords in the email body |
|
327 | # Returns a Hash of issue attributes extracted from keywords in the email body | |
345 | def issue_attributes_from_keywords(issue) |
|
328 | def issue_attributes_from_keywords(issue) | |
346 | assigned_to = (k = get_keyword(:assigned_to, :override => true)) && find_assignee_from_keyword(k, issue) |
|
329 | assigned_to = (k = get_keyword(:assigned_to, :override => true)) && find_assignee_from_keyword(k, issue) | |
347 |
|
330 | |||
348 | attrs = { |
|
331 | attrs = { | |
349 | 'tracker_id' => (k = get_keyword(:tracker)) && issue.project.trackers.named(k).first.try(:id), |
|
332 | 'tracker_id' => (k = get_keyword(:tracker)) && issue.project.trackers.named(k).first.try(:id), | |
350 | 'status_id' => (k = get_keyword(:status)) && IssueStatus.named(k).first.try(:id), |
|
333 | 'status_id' => (k = get_keyword(:status)) && IssueStatus.named(k).first.try(:id), | |
351 | 'priority_id' => (k = get_keyword(:priority)) && IssuePriority.named(k).first.try(:id), |
|
334 | 'priority_id' => (k = get_keyword(:priority)) && IssuePriority.named(k).first.try(:id), | |
352 | 'category_id' => (k = get_keyword(:category)) && issue.project.issue_categories.named(k).first.try(:id), |
|
335 | 'category_id' => (k = get_keyword(:category)) && issue.project.issue_categories.named(k).first.try(:id), | |
353 | 'assigned_to_id' => assigned_to.try(:id), |
|
336 | 'assigned_to_id' => assigned_to.try(:id), | |
354 | 'fixed_version_id' => (k = get_keyword(:fixed_version, :override => true)) && |
|
337 | 'fixed_version_id' => (k = get_keyword(:fixed_version, :override => true)) && | |
355 | issue.project.shared_versions.named(k).first.try(:id), |
|
338 | issue.project.shared_versions.named(k).first.try(:id), | |
356 | 'start_date' => get_keyword(:start_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'), |
|
339 | 'start_date' => get_keyword(:start_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'), | |
357 | 'due_date' => get_keyword(:due_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'), |
|
340 | 'due_date' => get_keyword(:due_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'), | |
358 | 'estimated_hours' => get_keyword(:estimated_hours, :override => true), |
|
341 | 'estimated_hours' => get_keyword(:estimated_hours, :override => true), | |
359 | 'done_ratio' => get_keyword(:done_ratio, :override => true, :format => '(\d|10)?0') |
|
342 | 'done_ratio' => get_keyword(:done_ratio, :override => true, :format => '(\d|10)?0') | |
360 | }.delete_if {|k, v| v.blank? } |
|
343 | }.delete_if {|k, v| v.blank? } | |
361 |
|
344 | |||
362 | if issue.new_record? && attrs['tracker_id'].nil? |
|
345 | if issue.new_record? && attrs['tracker_id'].nil? | |
363 | attrs['tracker_id'] = issue.project.trackers.first.try(:id) |
|
346 | attrs['tracker_id'] = issue.project.trackers.first.try(:id) | |
364 | end |
|
347 | end | |
365 |
|
348 | |||
366 | attrs |
|
349 | attrs | |
367 | end |
|
350 | end | |
368 |
|
351 | |||
369 | # Returns a Hash of issue custom field values extracted from keywords in the email body |
|
352 | # Returns a Hash of issue custom field values extracted from keywords in the email body | |
370 | def custom_field_values_from_keywords(customized) |
|
353 | def custom_field_values_from_keywords(customized) | |
371 | customized.custom_field_values.inject({}) do |h, v| |
|
354 | customized.custom_field_values.inject({}) do |h, v| | |
372 | if keyword = get_keyword(v.custom_field.name, :override => true) |
|
355 | if keyword = get_keyword(v.custom_field.name, :override => true) | |
373 | h[v.custom_field.id.to_s] = v.custom_field.value_from_keyword(keyword, customized) |
|
356 | h[v.custom_field.id.to_s] = v.custom_field.value_from_keyword(keyword, customized) | |
374 | end |
|
357 | end | |
375 | h |
|
358 | h | |
376 | end |
|
359 | end | |
377 | end |
|
360 | end | |
378 |
|
361 | |||
379 | # Returns the text/plain part of the email |
|
362 | # Returns the text/plain part of the email | |
380 | # If not found (eg. HTML-only email), returns the body with tags removed |
|
363 | # If not found (eg. HTML-only email), returns the body with tags removed | |
381 | def plain_text_body |
|
364 | def plain_text_body | |
382 | return @plain_text_body unless @plain_text_body.nil? |
|
365 | return @plain_text_body unless @plain_text_body.nil? | |
383 |
|
366 | |||
384 | part = email.text_part || email.html_part || email |
|
367 | part = email.text_part || email.html_part || email | |
385 | @plain_text_body = Redmine::CodesetUtil.to_utf8(part.body.decoded, part.charset) |
|
368 | @plain_text_body = Redmine::CodesetUtil.to_utf8(part.body.decoded, part.charset) | |
386 |
|
369 | |||
387 | # strip html tags and remove doctype directive |
|
370 | # strip html tags and remove doctype directive | |
388 | @plain_text_body = strip_tags(@plain_text_body.strip) |
|
371 | @plain_text_body = strip_tags(@plain_text_body.strip) | |
389 | @plain_text_body.sub! %r{^<!DOCTYPE .*$}, '' |
|
372 | @plain_text_body.sub! %r{^<!DOCTYPE .*$}, '' | |
390 | @plain_text_body |
|
373 | @plain_text_body | |
391 | end |
|
374 | end | |
392 |
|
375 | |||
393 | def cleaned_up_text_body |
|
376 | def cleaned_up_text_body | |
394 | cleanup_body(plain_text_body) |
|
377 | cleanup_body(plain_text_body) | |
395 | end |
|
378 | end | |
396 |
|
379 | |||
397 | def cleaned_up_subject |
|
380 | def cleaned_up_subject | |
398 | subject = email.subject.to_s |
|
381 | subject = email.subject.to_s | |
399 | unless subject.respond_to?(:encoding) |
|
|||
400 | # try to reencode to utf8 manually with ruby1.8 |
|
|||
401 | begin |
|
|||
402 | if h = email.header[:subject] |
|
|||
403 | # http://tools.ietf.org/html/rfc2047#section-4 |
|
|||
404 | if m = h.value.match(/=\?([^\?]+)\?[BbQq]\?/) |
|
|||
405 | subject = Redmine::CodesetUtil.to_utf8(subject, m[1]) |
|
|||
406 | end |
|
|||
407 | end |
|
|||
408 | rescue |
|
|||
409 | # nop |
|
|||
410 | end |
|
|||
411 | end |
|
|||
412 | subject.strip[0,255] |
|
382 | subject.strip[0,255] | |
413 | end |
|
383 | end | |
414 |
|
384 | |||
415 | def self.full_sanitizer |
|
385 | def self.full_sanitizer | |
416 | @full_sanitizer ||= HTML::FullSanitizer.new |
|
386 | @full_sanitizer ||= HTML::FullSanitizer.new | |
417 | end |
|
387 | end | |
418 |
|
388 | |||
419 | def self.assign_string_attribute_with_limit(object, attribute, value, limit=nil) |
|
389 | def self.assign_string_attribute_with_limit(object, attribute, value, limit=nil) | |
420 | limit ||= object.class.columns_hash[attribute.to_s].limit || 255 |
|
390 | limit ||= object.class.columns_hash[attribute.to_s].limit || 255 | |
421 | value = value.to_s.slice(0, limit) |
|
391 | value = value.to_s.slice(0, limit) | |
422 | object.send("#{attribute}=", value) |
|
392 | object.send("#{attribute}=", value) | |
423 | end |
|
393 | end | |
424 |
|
394 | |||
425 | # Returns a User from an email address and a full name |
|
395 | # Returns a User from an email address and a full name | |
426 | def self.new_user_from_attributes(email_address, fullname=nil) |
|
396 | def self.new_user_from_attributes(email_address, fullname=nil) | |
427 | user = User.new |
|
397 | user = User.new | |
428 |
|
398 | |||
429 | # Truncating the email address would result in an invalid format |
|
399 | # Truncating the email address would result in an invalid format | |
430 | user.mail = email_address |
|
400 | user.mail = email_address | |
431 | assign_string_attribute_with_limit(user, 'login', email_address, User::LOGIN_LENGTH_LIMIT) |
|
401 | assign_string_attribute_with_limit(user, 'login', email_address, User::LOGIN_LENGTH_LIMIT) | |
432 |
|
402 | |||
433 | names = fullname.blank? ? email_address.gsub(/@.*$/, '').split('.') : fullname.split |
|
403 | names = fullname.blank? ? email_address.gsub(/@.*$/, '').split('.') : fullname.split | |
434 | assign_string_attribute_with_limit(user, 'firstname', names.shift, 30) |
|
404 | assign_string_attribute_with_limit(user, 'firstname', names.shift, 30) | |
435 | assign_string_attribute_with_limit(user, 'lastname', names.join(' '), 30) |
|
405 | assign_string_attribute_with_limit(user, 'lastname', names.join(' '), 30) | |
436 | user.lastname = '-' if user.lastname.blank? |
|
406 | user.lastname = '-' if user.lastname.blank? | |
437 |
|
407 | |||
438 | password_length = [Setting.password_min_length.to_i, 10].max |
|
408 | password_length = [Setting.password_min_length.to_i, 10].max | |
439 | user.password = Redmine::Utils.random_hex(password_length / 2 + 1) |
|
409 | user.password = Redmine::Utils.random_hex(password_length / 2 + 1) | |
440 | user.language = Setting.default_language |
|
410 | user.language = Setting.default_language | |
441 | user.mail_notification = 'only_my_events' |
|
411 | user.mail_notification = 'only_my_events' | |
442 |
|
412 | |||
443 | unless user.valid? |
|
413 | unless user.valid? | |
444 | user.login = "user#{Redmine::Utils.random_hex(6)}" unless user.errors[:login].blank? |
|
414 | user.login = "user#{Redmine::Utils.random_hex(6)}" unless user.errors[:login].blank? | |
445 | user.firstname = "-" unless user.errors[:firstname].blank? |
|
415 | user.firstname = "-" unless user.errors[:firstname].blank? | |
446 | (puts user.errors[:lastname];user.lastname = "-") unless user.errors[:lastname].blank? |
|
416 | (puts user.errors[:lastname];user.lastname = "-") unless user.errors[:lastname].blank? | |
447 | end |
|
417 | end | |
448 |
|
418 | |||
449 | user |
|
419 | user | |
450 | end |
|
420 | end | |
451 |
|
421 | |||
452 | # Creates a User for the +email+ sender |
|
422 | # Creates a User for the +email+ sender | |
453 | # Returns the user or nil if it could not be created |
|
423 | # Returns the user or nil if it could not be created | |
454 | def create_user_from_email |
|
424 | def create_user_from_email | |
455 | from = email.header['from'].to_s |
|
425 | from = email.header['from'].to_s | |
456 | addr, name = from, nil |
|
426 | addr, name = from, nil | |
457 | if m = from.match(/^"?(.+?)"?\s+<(.+@.+)>$/) |
|
427 | if m = from.match(/^"?(.+?)"?\s+<(.+@.+)>$/) | |
458 | addr, name = m[2], m[1] |
|
428 | addr, name = m[2], m[1] | |
459 | end |
|
429 | end | |
460 | if addr.present? |
|
430 | if addr.present? | |
461 | user = self.class.new_user_from_attributes(addr, name) |
|
431 | user = self.class.new_user_from_attributes(addr, name) | |
462 | if @@handler_options[:no_notification] |
|
432 | if @@handler_options[:no_notification] | |
463 | user.mail_notification = 'none' |
|
433 | user.mail_notification = 'none' | |
464 | end |
|
434 | end | |
465 | if user.save |
|
435 | if user.save | |
466 | user |
|
436 | user | |
467 | else |
|
437 | else | |
468 | logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger |
|
438 | logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger | |
469 | nil |
|
439 | nil | |
470 | end |
|
440 | end | |
471 | else |
|
441 | else | |
472 | logger.error "MailHandler: failed to create User: no FROM address found" if logger |
|
442 | logger.error "MailHandler: failed to create User: no FROM address found" if logger | |
473 | nil |
|
443 | nil | |
474 | end |
|
444 | end | |
475 | end |
|
445 | end | |
476 |
|
446 | |||
477 | # Adds the newly created user to default group |
|
447 | # Adds the newly created user to default group | |
478 | def add_user_to_group(default_group) |
|
448 | def add_user_to_group(default_group) | |
479 | if default_group.present? |
|
449 | if default_group.present? | |
480 | default_group.split(',').each do |group_name| |
|
450 | default_group.split(',').each do |group_name| | |
481 | if group = Group.named(group_name).first |
|
451 | if group = Group.named(group_name).first | |
482 | group.users << @user |
|
452 | group.users << @user | |
483 | elsif logger |
|
453 | elsif logger | |
484 | logger.warn "MailHandler: could not add user to [#{group_name}], group not found" |
|
454 | logger.warn "MailHandler: could not add user to [#{group_name}], group not found" | |
485 | end |
|
455 | end | |
486 | end |
|
456 | end | |
487 | end |
|
457 | end | |
488 | end |
|
458 | end | |
489 |
|
459 | |||
490 | # Removes the email body of text after the truncation configurations. |
|
460 | # Removes the email body of text after the truncation configurations. | |
491 | def cleanup_body(body) |
|
461 | def cleanup_body(body) | |
492 | delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).map {|s| Regexp.escape(s)} |
|
462 | delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).map {|s| Regexp.escape(s)} | |
493 | unless delimiters.empty? |
|
463 | unless delimiters.empty? | |
494 | regex = Regexp.new("^[> ]*(#{ delimiters.join('|') })\s*[\r\n].*", Regexp::MULTILINE) |
|
464 | regex = Regexp.new("^[> ]*(#{ delimiters.join('|') })\s*[\r\n].*", Regexp::MULTILINE) | |
495 | body = body.gsub(regex, '') |
|
465 | body = body.gsub(regex, '') | |
496 | end |
|
466 | end | |
497 | body.strip |
|
467 | body.strip | |
498 | end |
|
468 | end | |
499 |
|
469 | |||
500 | def find_assignee_from_keyword(keyword, issue) |
|
470 | def find_assignee_from_keyword(keyword, issue) | |
501 | keyword = keyword.to_s.downcase |
|
471 | keyword = keyword.to_s.downcase | |
502 | assignable = issue.assignable_users |
|
472 | assignable = issue.assignable_users | |
503 | assignee = nil |
|
473 | assignee = nil | |
504 | assignee ||= assignable.detect {|a| |
|
474 | assignee ||= assignable.detect {|a| | |
505 | a.mail.to_s.downcase == keyword || |
|
475 | a.mail.to_s.downcase == keyword || | |
506 | a.login.to_s.downcase == keyword |
|
476 | a.login.to_s.downcase == keyword | |
507 | } |
|
477 | } | |
508 | if assignee.nil? && keyword.match(/ /) |
|
478 | if assignee.nil? && keyword.match(/ /) | |
509 | firstname, lastname = *(keyword.split) # "First Last Throwaway" |
|
479 | firstname, lastname = *(keyword.split) # "First Last Throwaway" | |
510 | assignee ||= assignable.detect {|a| |
|
480 | assignee ||= assignable.detect {|a| | |
511 | a.is_a?(User) && a.firstname.to_s.downcase == firstname && |
|
481 | a.is_a?(User) && a.firstname.to_s.downcase == firstname && | |
512 | a.lastname.to_s.downcase == lastname |
|
482 | a.lastname.to_s.downcase == lastname | |
513 | } |
|
483 | } | |
514 | end |
|
484 | end | |
515 | if assignee.nil? |
|
485 | if assignee.nil? | |
516 | assignee ||= assignable.detect {|a| a.name.downcase == keyword} |
|
486 | assignee ||= assignable.detect {|a| a.name.downcase == keyword} | |
517 | end |
|
487 | end | |
518 | assignee |
|
488 | assignee | |
519 | end |
|
489 | end | |
520 | end |
|
490 | end |
General Comments 0
You need to be logged in to leave comments.
Login now