Revision 1297:0a574315af3e .svn/pristine/65
| .svn/pristine/65/6521265256bb36fe92b152231305d87ec785bbb9.svn-base | ||
|---|---|---|
| 1 |
# Redmine - project management software |
|
| 2 |
# Copyright (C) 2006-2012 Jean-Philippe Lang |
|
| 3 |
# |
|
| 4 |
# This program is free software; you can redistribute it and/or |
|
| 5 |
# modify it under the terms of the GNU General Public License |
|
| 6 |
# as published by the Free Software Foundation; either version 2 |
|
| 7 |
# of the License, or (at your option) any later version. |
|
| 8 |
# |
|
| 9 |
# This program is distributed in the hope that it will be useful, |
|
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 12 |
# GNU General Public License for more details. |
|
| 13 |
# |
|
| 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 |
|
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
| 17 |
|
|
| 18 |
module Redmine |
|
| 19 |
module I18n |
|
| 20 |
def self.included(base) |
|
| 21 |
base.extend Redmine::I18n |
|
| 22 |
end |
|
| 23 |
|
|
| 24 |
def l(*args) |
|
| 25 |
case args.size |
|
| 26 |
when 1 |
|
| 27 |
::I18n.t(*args) |
|
| 28 |
when 2 |
|
| 29 |
if args.last.is_a?(Hash) |
|
| 30 |
::I18n.t(*args) |
|
| 31 |
elsif args.last.is_a?(String) |
|
| 32 |
::I18n.t(args.first, :value => args.last) |
|
| 33 |
else |
|
| 34 |
::I18n.t(args.first, :count => args.last) |
|
| 35 |
end |
|
| 36 |
else |
|
| 37 |
raise "Translation string with multiple values: #{args.first}"
|
|
| 38 |
end |
|
| 39 |
end |
|
| 40 |
|
|
| 41 |
def l_or_humanize(s, options={})
|
|
| 42 |
k = "#{options[:prefix]}#{s}".to_sym
|
|
| 43 |
::I18n.t(k, :default => s.to_s.humanize) |
|
| 44 |
end |
|
| 45 |
|
|
| 46 |
def l_hours(hours) |
|
| 47 |
hours = hours.to_f |
|
| 48 |
l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => ("%.2f" % hours.to_f))
|
|
| 49 |
end |
|
| 50 |
|
|
| 51 |
def ll(lang, str, value=nil) |
|
| 52 |
::I18n.t(str.to_s, :value => value, :locale => lang.to_s.gsub(%r{(.+)\-(.+)$}) { "#{$1}-#{$2.upcase}" })
|
|
| 53 |
end |
|
| 54 |
|
|
| 55 |
def format_date(date) |
|
| 56 |
return nil unless date |
|
| 57 |
options = {}
|
|
| 58 |
options[:format] = Setting.date_format unless Setting.date_format.blank? |
|
| 59 |
options[:locale] = User.current.language unless User.current.language.blank? |
|
| 60 |
::I18n.l(date.to_date, options) |
|
| 61 |
end |
|
| 62 |
|
|
| 63 |
def format_time(time, include_date = true) |
|
| 64 |
return nil unless time |
|
| 65 |
options = {}
|
|
| 66 |
options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format) |
|
| 67 |
options[:locale] = User.current.language unless User.current.language.blank? |
|
| 68 |
time = time.to_time if time.is_a?(String) |
|
| 69 |
zone = User.current.time_zone |
|
| 70 |
local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time) |
|
| 71 |
(include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, options)
|
|
| 72 |
end |
|
| 73 |
|
|
| 74 |
def day_name(day) |
|
| 75 |
::I18n.t('date.day_names')[day % 7]
|
|
| 76 |
end |
|
| 77 |
|
|
| 78 |
def day_letter(day) |
|
| 79 |
::I18n.t('date.abbr_day_names')[day % 7].first
|
|
| 80 |
end |
|
| 81 |
|
|
| 82 |
def month_name(month) |
|
| 83 |
::I18n.t('date.month_names')[month]
|
|
| 84 |
end |
|
| 85 |
|
|
| 86 |
def valid_languages |
|
| 87 |
::I18n.available_locales |
|
| 88 |
end |
|
| 89 |
|
|
| 90 |
# Returns an array of languages names and code sorted by names, example: |
|
| 91 |
# [["Deutsch", "de"], ["English", "en"] ...] |
|
| 92 |
# |
|
| 93 |
# The result is cached to prevent from loading all translations files. |
|
| 94 |
def languages_options |
|
| 95 |
ActionController::Base.cache_store.fetch "i18n/languages_options" do |
|
| 96 |
valid_languages.map {|lang| [ll(lang.to_s, :general_lang_name), lang.to_s]}.sort {|x,y| x.first <=> y.first }
|
|
| 97 |
end |
|
| 98 |
end |
|
| 99 |
|
|
| 100 |
def find_language(lang) |
|
| 101 |
@@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k }
|
|
| 102 |
@@languages_lookup[lang.to_s.downcase] |
|
| 103 |
end |
|
| 104 |
|
|
| 105 |
def set_language_if_valid(lang) |
|
| 106 |
if l = find_language(lang) |
|
| 107 |
::I18n.locale = l |
|
| 108 |
end |
|
| 109 |
end |
|
| 110 |
|
|
| 111 |
def current_language |
|
| 112 |
::I18n.locale |
|
| 113 |
end |
|
| 114 |
|
|
| 115 |
# Custom backend based on I18n::Backend::Simple with the following changes: |
|
| 116 |
# * lazy loading of translation files |
|
| 117 |
# * available_locales are determined by looking at translation file names |
|
| 118 |
class Backend |
|
| 119 |
(class << self; self; end).class_eval { public :include }
|
|
| 120 |
|
|
| 121 |
module Implementation |
|
| 122 |
include ::I18n::Backend::Base |
|
| 123 |
|
|
| 124 |
# Stores translations for the given locale in memory. |
|
| 125 |
# This uses a deep merge for the translations hash, so existing |
|
| 126 |
# translations will be overwritten by new ones only at the deepest |
|
| 127 |
# level of the hash. |
|
| 128 |
def store_translations(locale, data, options = {})
|
|
| 129 |
locale = locale.to_sym |
|
| 130 |
translations[locale] ||= {}
|
|
| 131 |
data = data.deep_symbolize_keys |
|
| 132 |
translations[locale].deep_merge!(data) |
|
| 133 |
end |
|
| 134 |
|
|
| 135 |
# Get available locales from the translations filenames |
|
| 136 |
def available_locales |
|
| 137 |
@available_locales ||= ::I18n.load_path.map {|path| File.basename(path, '.*')}.uniq.sort.map(&:to_sym)
|
|
| 138 |
end |
|
| 139 |
|
|
| 140 |
# Clean up translations |
|
| 141 |
def reload! |
|
| 142 |
@translations = nil |
|
| 143 |
@available_locales = nil |
|
| 144 |
super |
|
| 145 |
end |
|
| 146 |
|
|
| 147 |
protected |
|
| 148 |
|
|
| 149 |
def init_translations(locale) |
|
| 150 |
locale = locale.to_s |
|
| 151 |
paths = ::I18n.load_path.select {|path| File.basename(path, '.*') == locale}
|
|
| 152 |
load_translations(paths) |
|
| 153 |
translations[locale] ||= {}
|
|
| 154 |
end |
|
| 155 |
|
|
| 156 |
def translations |
|
| 157 |
@translations ||= {}
|
|
| 158 |
end |
|
| 159 |
|
|
| 160 |
# Looks up a translation from the translations hash. Returns nil if |
|
| 161 |
# eiher key is nil, or locale, scope or key do not exist as a key in the |
|
| 162 |
# nested translations hash. Splits keys or scopes containing dots |
|
| 163 |
# into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as |
|
| 164 |
# <tt>%w(currency format)</tt>. |
|
| 165 |
def lookup(locale, key, scope = [], options = {})
|
|
| 166 |
init_translations(locale) unless translations.key?(locale) |
|
| 167 |
keys = ::I18n.normalize_keys(locale, key, scope, options[:separator]) |
|
| 168 |
|
|
| 169 |
keys.inject(translations) do |result, _key| |
|
| 170 |
_key = _key.to_sym |
|
| 171 |
return nil unless result.is_a?(Hash) && result.has_key?(_key) |
|
| 172 |
result = result[_key] |
|
| 173 |
result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol) |
|
| 174 |
result |
|
| 175 |
end |
|
| 176 |
end |
|
| 177 |
end |
|
| 178 |
|
|
| 179 |
include Implementation |
|
| 180 |
# Adds fallback to default locale for untranslated strings |
|
| 181 |
include ::I18n::Backend::Fallbacks |
|
| 182 |
end |
|
| 183 |
end |
|
| 184 |
end |
|
| .svn/pristine/65/654a3e4d61187374fd83543621c54137d6380420.svn-base | ||
|---|---|---|
| 1 |
require File.dirname(__FILE__) + '/string/conversions' |
|
| 2 |
require File.dirname(__FILE__) + '/string/inflections' |
|
| 3 |
|
|
| 4 |
class String #:nodoc: |
|
| 5 |
include Redmine::CoreExtensions::String::Conversions |
|
| 6 |
include Redmine::CoreExtensions::String::Inflections |
|
| 7 |
|
|
| 8 |
def is_binary_data? |
|
| 9 |
( self.count( "^ -~", "^\r\n" ).fdiv(self.size) > 0.3 || self.index( "\x00" ) ) unless empty? |
|
| 10 |
end |
|
| 11 |
end |
|
| .svn/pristine/65/6585cf898d7ab88d915dc6705a06f8b1775d8422.svn-base | ||
|---|---|---|
| 1 |
# Redmine - project management software |
|
| 2 |
# Copyright (C) 2006-2012 Jean-Philippe Lang |
|
| 3 |
# |
|
| 4 |
# This program is free software; you can redistribute it and/or |
|
| 5 |
# modify it under the terms of the GNU General Public License |
|
| 6 |
# as published by the Free Software Foundation; either version 2 |
|
| 7 |
# of the License, or (at your option) any later version. |
|
| 8 |
# |
|
| 9 |
# This program is distributed in the hope that it will be useful, |
|
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 12 |
# GNU General Public License for more details. |
|
| 13 |
# |
|
| 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 |
|
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
| 17 |
|
|
| 18 |
class MailHandler < ActionMailer::Base |
|
| 19 |
include ActionView::Helpers::SanitizeHelper |
|
| 20 |
include Redmine::I18n |
|
| 21 |
|
|
| 22 |
class UnauthorizedAction < StandardError; end |
|
| 23 |
class MissingInformation < StandardError; end |
|
| 24 |
|
|
| 25 |
attr_reader :email, :user |
|
| 26 |
|
|
| 27 |
def self.receive(email, options={})
|
|
| 28 |
@@handler_options = options.dup |
|
| 29 |
|
|
| 30 |
@@handler_options[:issue] ||= {}
|
|
| 31 |
|
|
| 32 |
if @@handler_options[:allow_override].is_a?(String) |
|
| 33 |
@@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip)
|
|
| 34 |
end |
|
| 35 |
@@handler_options[:allow_override] ||= [] |
|
| 36 |
# Project needs to be overridable if not specified |
|
| 37 |
@@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project) |
|
| 38 |
# Status overridable by default |
|
| 39 |
@@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status) |
|
| 40 |
|
|
| 41 |
@@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1' ? true : false) |
|
| 42 |
|
|
| 43 |
email.force_encoding('ASCII-8BIT') if email.respond_to?(:force_encoding)
|
|
| 44 |
super(email) |
|
| 45 |
end |
|
| 46 |
|
|
| 47 |
def logger |
|
| 48 |
Rails.logger |
|
| 49 |
end |
|
| 50 |
|
|
| 51 |
cattr_accessor :ignored_emails_headers |
|
| 52 |
@@ignored_emails_headers = {
|
|
| 53 |
'X-Auto-Response-Suppress' => 'oof', |
|
| 54 |
'Auto-Submitted' => /^auto-/ |
|
| 55 |
} |
|
| 56 |
|
|
| 57 |
# Processes incoming emails |
|
| 58 |
# Returns the created object (eg. an issue, a message) or false |
|
| 59 |
def receive(email) |
|
| 60 |
@email = email |
|
| 61 |
sender_email = email.from.to_a.first.to_s.strip |
|
| 62 |
# Ignore emails received from the application emission address to avoid hell cycles |
|
| 63 |
if sender_email.downcase == Setting.mail_from.to_s.strip.downcase |
|
| 64 |
if logger && logger.info |
|
| 65 |
logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]"
|
|
| 66 |
end |
|
| 67 |
return false |
|
| 68 |
end |
|
| 69 |
# Ignore auto generated emails |
|
| 70 |
self.class.ignored_emails_headers.each do |key, ignored_value| |
|
| 71 |
value = email.header[key] |
|
| 72 |
if value |
|
| 73 |
value = value.to_s.downcase |
|
| 74 |
if (ignored_value.is_a?(Regexp) && value.match(ignored_value)) || value == ignored_value |
|
| 75 |
if logger && logger.info |
|
| 76 |
logger.info "MailHandler: ignoring email with #{key}:#{value} header"
|
|
| 77 |
end |
|
| 78 |
return false |
|
| 79 |
end |
|
| 80 |
end |
|
| 81 |
end |
|
| 82 |
@user = User.find_by_mail(sender_email) if sender_email.present? |
|
| 83 |
if @user && !@user.active? |
|
| 84 |
if logger && logger.info |
|
| 85 |
logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]"
|
|
| 86 |
end |
|
| 87 |
return false |
|
| 88 |
end |
|
| 89 |
if @user.nil? |
|
| 90 |
# Email was submitted by an unknown user |
|
| 91 |
case @@handler_options[:unknown_user] |
|
| 92 |
when 'accept' |
|
| 93 |
@user = User.anonymous |
|
| 94 |
when 'create' |
|
| 95 |
@user = create_user_from_email |
|
| 96 |
if @user |
|
| 97 |
if logger && logger.info |
|
| 98 |
logger.info "MailHandler: [#{@user.login}] account created"
|
|
| 99 |
end |
|
| 100 |
Mailer.account_information(@user, @user.password).deliver |
|
| 101 |
else |
|
| 102 |
if logger && logger.error |
|
| 103 |
logger.error "MailHandler: could not create account for [#{sender_email}]"
|
|
| 104 |
end |
|
| 105 |
return false |
|
| 106 |
end |
|
| 107 |
else |
|
| 108 |
# Default behaviour, emails from unknown users are ignored |
|
| 109 |
if logger && logger.info |
|
| 110 |
logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]"
|
|
| 111 |
end |
|
| 112 |
return false |
|
| 113 |
end |
|
| 114 |
end |
|
| 115 |
User.current = @user |
|
| 116 |
dispatch |
|
| 117 |
end |
|
| 118 |
|
|
| 119 |
private |
|
| 120 |
|
|
| 121 |
MESSAGE_ID_RE = %r{^<?redmine\.([a-z0-9_]+)\-(\d+)\.\d+@}
|
|
| 122 |
ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]*#(\d+)\]}
|
|
| 123 |
MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]*msg(\d+)\]}
|
|
| 124 |
|
|
| 125 |
def dispatch |
|
| 126 |
headers = [email.in_reply_to, email.references].flatten.compact |
|
| 127 |
subject = email.subject.to_s |
|
| 128 |
if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE}
|
|
| 129 |
klass, object_id = $1, $2.to_i |
|
| 130 |
method_name = "receive_#{klass}_reply"
|
|
| 131 |
if self.class.private_instance_methods.collect(&:to_s).include?(method_name) |
|
| 132 |
send method_name, object_id |
|
| 133 |
else |
|
| 134 |
# ignoring it |
|
| 135 |
end |
|
| 136 |
elsif m = subject.match(ISSUE_REPLY_SUBJECT_RE) |
|
| 137 |
receive_issue_reply(m[1].to_i) |
|
| 138 |
elsif m = subject.match(MESSAGE_REPLY_SUBJECT_RE) |
|
| 139 |
receive_message_reply(m[1].to_i) |
|
| 140 |
else |
|
| 141 |
dispatch_to_default |
|
| 142 |
end |
|
| 143 |
rescue ActiveRecord::RecordInvalid => e |
|
| 144 |
# TODO: send a email to the user |
|
| 145 |
logger.error e.message if logger |
|
| 146 |
false |
|
| 147 |
rescue MissingInformation => e |
|
| 148 |
logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger
|
|
| 149 |
false |
|
| 150 |
rescue UnauthorizedAction => e |
|
| 151 |
logger.error "MailHandler: unauthorized attempt from #{user}" if logger
|
|
| 152 |
false |
|
| 153 |
end |
|
| 154 |
|
|
| 155 |
def dispatch_to_default |
|
| 156 |
receive_issue |
|
| 157 |
end |
|
| 158 |
|
|
| 159 |
# Creates a new issue |
|
| 160 |
def receive_issue |
|
| 161 |
project = target_project |
|
| 162 |
# check permission |
|
| 163 |
unless @@handler_options[:no_permission_check] |
|
| 164 |
raise UnauthorizedAction unless user.allowed_to?(:add_issues, project) |
|
| 165 |
end |
|
| 166 |
|
|
| 167 |
issue = Issue.new(:author => user, :project => project) |
|
| 168 |
issue.safe_attributes = issue_attributes_from_keywords(issue) |
|
| 169 |
issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)}
|
|
| 170 |
issue.subject = cleaned_up_subject |
|
| 171 |
if issue.subject.blank? |
|
| 172 |
issue.subject = '(no subject)' |
|
| 173 |
end |
|
| 174 |
issue.description = cleaned_up_text_body |
|
| 175 |
|
|
| 176 |
# add To and Cc as watchers before saving so the watchers can reply to Redmine |
|
| 177 |
add_watchers(issue) |
|
| 178 |
issue.save! |
|
| 179 |
add_attachments(issue) |
|
| 180 |
logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info
|
|
| 181 |
issue |
|
| 182 |
end |
|
| 183 |
|
|
| 184 |
# Adds a note to an existing issue |
|
| 185 |
def receive_issue_reply(issue_id, from_journal=nil) |
|
| 186 |
issue = Issue.find_by_id(issue_id) |
|
| 187 |
return unless issue |
|
| 188 |
# check permission |
|
| 189 |
unless @@handler_options[:no_permission_check] |
|
| 190 |
unless user.allowed_to?(:add_issue_notes, issue.project) || |
|
| 191 |
user.allowed_to?(:edit_issues, issue.project) |
|
| 192 |
raise UnauthorizedAction |
|
| 193 |
end |
|
| 194 |
end |
|
| 195 |
|
|
| 196 |
# ignore CLI-supplied defaults for new issues |
|
| 197 |
@@handler_options[:issue].clear |
|
| 198 |
|
|
| 199 |
journal = issue.init_journal(user) |
|
| 200 |
if from_journal && from_journal.private_notes? |
|
| 201 |
# If the received email was a reply to a private note, make the added note private |
|
| 202 |
issue.private_notes = true |
|
| 203 |
end |
|
| 204 |
issue.safe_attributes = issue_attributes_from_keywords(issue) |
|
| 205 |
issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)}
|
|
| 206 |
journal.notes = cleaned_up_text_body |
|
| 207 |
add_attachments(issue) |
|
| 208 |
issue.save! |
|
| 209 |
if logger && logger.info |
|
| 210 |
logger.info "MailHandler: issue ##{issue.id} updated by #{user}"
|
|
| 211 |
end |
|
| 212 |
journal |
|
| 213 |
end |
|
| 214 |
|
|
| 215 |
# Reply will be added to the issue |
|
| 216 |
def receive_journal_reply(journal_id) |
|
| 217 |
journal = Journal.find_by_id(journal_id) |
|
| 218 |
if journal && journal.journalized_type == 'Issue' |
|
| 219 |
receive_issue_reply(journal.journalized_id, journal) |
|
| 220 |
end |
|
| 221 |
end |
|
| 222 |
|
|
| 223 |
# Receives a reply to a forum message |
|
| 224 |
def receive_message_reply(message_id) |
|
| 225 |
message = Message.find_by_id(message_id) |
|
| 226 |
if message |
|
| 227 |
message = message.root |
|
| 228 |
|
|
| 229 |
unless @@handler_options[:no_permission_check] |
|
| 230 |
raise UnauthorizedAction unless user.allowed_to?(:add_messages, message.project) |
|
| 231 |
end |
|
| 232 |
|
|
| 233 |
if !message.locked? |
|
| 234 |
reply = Message.new(:subject => cleaned_up_subject.gsub(%r{^.*msg\d+\]}, '').strip,
|
|
| 235 |
:content => cleaned_up_text_body) |
|
| 236 |
reply.author = user |
|
| 237 |
reply.board = message.board |
|
| 238 |
message.children << reply |
|
| 239 |
add_attachments(reply) |
|
| 240 |
reply |
|
| 241 |
else |
|
| 242 |
if logger && logger.info |
|
| 243 |
logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic"
|
|
| 244 |
end |
|
| 245 |
end |
|
| 246 |
end |
|
| 247 |
end |
|
| 248 |
|
|
| 249 |
def add_attachments(obj) |
|
| 250 |
if email.attachments && email.attachments.any? |
|
| 251 |
email.attachments.each do |attachment| |
|
| 252 |
filename = attachment.filename |
|
| 253 |
unless filename.respond_to?(:encoding) |
|
| 254 |
# try to reencode to utf8 manually with ruby1.8 |
|
| 255 |
h = attachment.header['Content-Disposition'] |
|
| 256 |
unless h.nil? |
|
| 257 |
begin |
|
| 258 |
if m = h.value.match(/filename\*[0-9\*]*=([^=']+)'/) |
|
| 259 |
filename = Redmine::CodesetUtil.to_utf8(filename, m[1]) |
|
| 260 |
elsif m = h.value.match(/filename=.*=\?([^\?]+)\?[BbQq]\?/) |
|
| 261 |
# http://tools.ietf.org/html/rfc2047#section-4 |
|
| 262 |
filename = Redmine::CodesetUtil.to_utf8(filename, m[1]) |
|
| 263 |
end |
|
| 264 |
rescue |
|
| 265 |
# nop |
|
| 266 |
end |
|
| 267 |
end |
|
| 268 |
end |
|
| 269 |
obj.attachments << Attachment.create(:container => obj, |
|
| 270 |
:file => attachment.decoded, |
|
| 271 |
:filename => filename, |
|
| 272 |
:author => user, |
|
| 273 |
:content_type => attachment.mime_type) |
|
| 274 |
end |
|
| 275 |
end |
|
| 276 |
end |
|
| 277 |
|
|
| 278 |
# Adds To and Cc as watchers of the given object if the sender has the |
|
| 279 |
# appropriate permission |
|
| 280 |
def add_watchers(obj) |
|
| 281 |
if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project)
|
|
| 282 |
addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase}
|
|
| 283 |
unless addresses.empty? |
|
| 284 |
watchers = User.active.find(:all, :conditions => ['LOWER(mail) IN (?)', addresses]) |
|
| 285 |
watchers.each {|w| obj.add_watcher(w)}
|
|
| 286 |
end |
|
| 287 |
end |
|
| 288 |
end |
|
| 289 |
|
|
| 290 |
def get_keyword(attr, options={})
|
|
| 291 |
@keywords ||= {}
|
|
| 292 |
if @keywords.has_key?(attr) |
|
| 293 |
@keywords[attr] |
|
| 294 |
else |
|
| 295 |
@keywords[attr] = begin |
|
| 296 |
if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && |
|
| 297 |
(v = extract_keyword!(plain_text_body, attr, options[:format])) |
|
| 298 |
v |
|
| 299 |
elsif !@@handler_options[:issue][attr].blank? |
|
| 300 |
@@handler_options[:issue][attr] |
|
| 301 |
end |
|
| 302 |
end |
|
| 303 |
end |
|
| 304 |
end |
|
| 305 |
|
|
| 306 |
# Destructively extracts the value for +attr+ in +text+ |
|
| 307 |
# Returns nil if no matching keyword found |
|
| 308 |
def extract_keyword!(text, attr, format=nil) |
|
| 309 |
keys = [attr.to_s.humanize] |
|
| 310 |
if attr.is_a?(Symbol) |
|
| 311 |
if user && user.language.present? |
|
| 312 |
keys << l("field_#{attr}", :default => '', :locale => user.language)
|
|
| 313 |
end |
|
| 314 |
if Setting.default_language.present? |
|
| 315 |
keys << l("field_#{attr}", :default => '', :locale => Setting.default_language)
|
|
| 316 |
end |
|
| 317 |
end |
|
| 318 |
keys.reject! {|k| k.blank?}
|
|
| 319 |
keys.collect! {|k| Regexp.escape(k)}
|
|
| 320 |
format ||= '.+' |
|
| 321 |
keyword = nil |
|
| 322 |
regexp = /^(#{keys.join('|')})[ \t]*:[ \t]*(#{format})\s*$/i
|
|
| 323 |
if m = text.match(regexp) |
|
| 324 |
keyword = m[2].strip |
|
| 325 |
text.gsub!(regexp, '') |
|
| 326 |
end |
|
| 327 |
keyword |
|
| 328 |
end |
|
| 329 |
|
|
| 330 |
def target_project |
|
| 331 |
# TODO: other ways to specify project: |
|
| 332 |
# * parse the email To field |
|
| 333 |
# * specific project (eg. Setting.mail_handler_target_project) |
|
| 334 |
target = Project.find_by_identifier(get_keyword(:project)) |
|
| 335 |
raise MissingInformation.new('Unable to determine target project') if target.nil?
|
|
| 336 |
target |
|
| 337 |
end |
|
| 338 |
|
|
| 339 |
# Returns a Hash of issue attributes extracted from keywords in the email body |
|
| 340 |
def issue_attributes_from_keywords(issue) |
|
| 341 |
assigned_to = (k = get_keyword(:assigned_to, :override => true)) && find_assignee_from_keyword(k, issue) |
|
| 342 |
|
|
| 343 |
attrs = {
|
|
| 344 |
'tracker_id' => (k = get_keyword(:tracker)) && issue.project.trackers.named(k).first.try(:id), |
|
| 345 |
'status_id' => (k = get_keyword(:status)) && IssueStatus.named(k).first.try(:id), |
|
| 346 |
'priority_id' => (k = get_keyword(:priority)) && IssuePriority.named(k).first.try(:id), |
|
| 347 |
'category_id' => (k = get_keyword(:category)) && issue.project.issue_categories.named(k).first.try(:id), |
|
| 348 |
'assigned_to_id' => assigned_to.try(:id), |
|
| 349 |
'fixed_version_id' => (k = get_keyword(:fixed_version, :override => true)) && |
|
| 350 |
issue.project.shared_versions.named(k).first.try(:id), |
|
| 351 |
'start_date' => get_keyword(:start_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'),
|
|
| 352 |
'due_date' => get_keyword(:due_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'),
|
|
| 353 |
'estimated_hours' => get_keyword(:estimated_hours, :override => true), |
|
| 354 |
'done_ratio' => get_keyword(:done_ratio, :override => true, :format => '(\d|10)?0') |
|
| 355 |
}.delete_if {|k, v| v.blank? }
|
|
| 356 |
|
|
| 357 |
if issue.new_record? && attrs['tracker_id'].nil? |
|
| 358 |
attrs['tracker_id'] = issue.project.trackers.find(:first).try(:id) |
|
| 359 |
end |
|
| 360 |
|
|
| 361 |
attrs |
|
| 362 |
end |
|
| 363 |
|
|
| 364 |
# Returns a Hash of issue custom field values extracted from keywords in the email body |
|
| 365 |
def custom_field_values_from_keywords(customized) |
|
| 366 |
customized.custom_field_values.inject({}) do |h, v|
|
|
| 367 |
if keyword = get_keyword(v.custom_field.name, :override => true) |
|
| 368 |
h[v.custom_field.id.to_s] = v.custom_field.value_from_keyword(keyword, customized) |
|
| 369 |
end |
|
| 370 |
h |
|
| 371 |
end |
|
| 372 |
end |
|
| 373 |
|
|
| 374 |
# Returns the text/plain part of the email |
|
| 375 |
# If not found (eg. HTML-only email), returns the body with tags removed |
|
| 376 |
def plain_text_body |
|
| 377 |
return @plain_text_body unless @plain_text_body.nil? |
|
| 378 |
|
|
| 379 |
part = email.text_part || email.html_part || email |
|
| 380 |
@plain_text_body = Redmine::CodesetUtil.to_utf8(part.body.decoded, part.charset) |
|
| 381 |
|
|
| 382 |
# strip html tags and remove doctype directive |
|
| 383 |
@plain_text_body = strip_tags(@plain_text_body.strip) |
|
| 384 |
@plain_text_body.sub! %r{^<!DOCTYPE .*$}, ''
|
|
| 385 |
@plain_text_body |
|
| 386 |
end |
|
| 387 |
|
|
| 388 |
def cleaned_up_text_body |
|
| 389 |
cleanup_body(plain_text_body) |
|
| 390 |
end |
|
| 391 |
|
|
| 392 |
def cleaned_up_subject |
|
| 393 |
subject = email.subject.to_s |
|
| 394 |
unless subject.respond_to?(:encoding) |
|
| 395 |
# try to reencode to utf8 manually with ruby1.8 |
|
| 396 |
begin |
|
| 397 |
if h = email.header[:subject] |
|
| 398 |
# http://tools.ietf.org/html/rfc2047#section-4 |
|
| 399 |
if m = h.value.match(/=\?([^\?]+)\?[BbQq]\?/) |
|
| 400 |
subject = Redmine::CodesetUtil.to_utf8(subject, m[1]) |
|
| 401 |
end |
|
| 402 |
end |
|
| 403 |
rescue |
|
| 404 |
# nop |
|
| 405 |
end |
|
| 406 |
end |
|
| 407 |
subject.strip[0,255] |
|
| 408 |
end |
|
| 409 |
|
|
| 410 |
def self.full_sanitizer |
|
| 411 |
@full_sanitizer ||= HTML::FullSanitizer.new |
|
| 412 |
end |
|
| 413 |
|
|
| 414 |
def self.assign_string_attribute_with_limit(object, attribute, value, limit=nil) |
|
| 415 |
limit ||= object.class.columns_hash[attribute.to_s].limit || 255 |
|
| 416 |
value = value.to_s.slice(0, limit) |
|
| 417 |
object.send("#{attribute}=", value)
|
|
| 418 |
end |
|
| 419 |
|
|
| 420 |
# Returns a User from an email address and a full name |
|
| 421 |
def self.new_user_from_attributes(email_address, fullname=nil) |
|
| 422 |
user = User.new |
|
| 423 |
|
|
| 424 |
# Truncating the email address would result in an invalid format |
|
| 425 |
user.mail = email_address |
|
| 426 |
assign_string_attribute_with_limit(user, 'login', email_address, User::LOGIN_LENGTH_LIMIT) |
|
| 427 |
|
|
| 428 |
names = fullname.blank? ? email_address.gsub(/@.*$/, '').split('.') : fullname.split
|
|
| 429 |
assign_string_attribute_with_limit(user, 'firstname', names.shift) |
|
| 430 |
assign_string_attribute_with_limit(user, 'lastname', names.join(' '))
|
|
| 431 |
user.lastname = '-' if user.lastname.blank? |
|
| 432 |
|
|
| 433 |
password_length = [Setting.password_min_length.to_i, 10].max |
|
| 434 |
user.password = Redmine::Utils.random_hex(password_length / 2 + 1) |
|
| 435 |
user.language = Setting.default_language |
|
| 436 |
|
|
| 437 |
unless user.valid? |
|
| 438 |
user.login = "user#{Redmine::Utils.random_hex(6)}" unless user.errors[:login].blank?
|
|
| 439 |
user.firstname = "-" unless user.errors[:firstname].blank? |
|
| 440 |
user.lastname = "-" unless user.errors[:lastname].blank? |
|
| 441 |
end |
|
| 442 |
|
|
| 443 |
user |
|
| 444 |
end |
|
| 445 |
|
|
| 446 |
# Creates a User for the +email+ sender |
|
| 447 |
# Returns the user or nil if it could not be created |
|
| 448 |
def create_user_from_email |
|
| 449 |
from = email.header['from'].to_s |
|
| 450 |
addr, name = from, nil |
|
| 451 |
if m = from.match(/^"?(.+?)"?\s+<(.+@.+)>$/) |
|
| 452 |
addr, name = m[2], m[1] |
|
| 453 |
end |
|
| 454 |
if addr.present? |
|
| 455 |
user = self.class.new_user_from_attributes(addr, name) |
|
| 456 |
if user.save |
|
| 457 |
user |
|
| 458 |
else |
|
| 459 |
logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger
|
|
| 460 |
nil |
|
| 461 |
end |
|
| 462 |
else |
|
| 463 |
logger.error "MailHandler: failed to create User: no FROM address found" if logger |
|
| 464 |
nil |
|
| 465 |
end |
|
| 466 |
end |
|
| 467 |
|
|
| 468 |
# Removes the email body of text after the truncation configurations. |
|
| 469 |
def cleanup_body(body) |
|
| 470 |
delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).map {|s| Regexp.escape(s)}
|
|
| 471 |
unless delimiters.empty? |
|
| 472 |
regex = Regexp.new("^[> ]*(#{ delimiters.join('|') })\s*[\r\n].*", Regexp::MULTILINE)
|
|
| 473 |
body = body.gsub(regex, '') |
|
| 474 |
end |
|
| 475 |
body.strip |
|
| 476 |
end |
|
| 477 |
|
|
| 478 |
def find_assignee_from_keyword(keyword, issue) |
|
| 479 |
keyword = keyword.to_s.downcase |
|
| 480 |
assignable = issue.assignable_users |
|
| 481 |
assignee = nil |
|
| 482 |
assignee ||= assignable.detect {|a|
|
|
| 483 |
a.mail.to_s.downcase == keyword || |
|
| 484 |
a.login.to_s.downcase == keyword |
|
| 485 |
} |
|
| 486 |
if assignee.nil? && keyword.match(/ /) |
|
| 487 |
firstname, lastname = *(keyword.split) # "First Last Throwaway" |
|
| 488 |
assignee ||= assignable.detect {|a|
|
|
| 489 |
a.is_a?(User) && a.firstname.to_s.downcase == firstname && |
|
| 490 |
a.lastname.to_s.downcase == lastname |
|
| 491 |
} |
|
| 492 |
end |
|
| 493 |
if assignee.nil? |
|
| 494 |
assignee ||= assignable.detect {|a| a.name.downcase == keyword}
|
|
| 495 |
end |
|
| 496 |
assignee |
|
| 497 |
end |
|
| 498 |
end |
|
| .svn/pristine/65/65b74e35a9a66d426c65b1f9b8de763ac90e4894.svn-base | ||
|---|---|---|
| 1 |
api.array :groups do |
|
| 2 |
@groups.each do |group| |
|
| 3 |
api.group do |
|
| 4 |
api.id group.id |
|
| 5 |
api.name group.lastname |
|
| 6 |
|
|
| 7 |
render_api_custom_values group.visible_custom_field_values, api |
|
| 8 |
end |
|
| 9 |
end |
|
| 10 |
end |
|
| .svn/pristine/65/65d6be6883079fe415bfb4c73e1cd79a79360e50.svn-base | ||
|---|---|---|
| 1 |
# Redmine - project management software |
|
| 2 |
# Copyright (C) 2006-2012 Jean-Philippe Lang |
|
| 3 |
# |
|
| 4 |
# This program is free software; you can redistribute it and/or |
|
| 5 |
# modify it under the terms of the GNU General Public License |
|
| 6 |
# as published by the Free Software Foundation; either version 2 |
|
| 7 |
# of the License, or (at your option) any later version. |
|
| 8 |
# |
|
| 9 |
# This program is distributed in the hope that it will be useful, |
|
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 12 |
# GNU General Public License for more details. |
|
| 13 |
# |
|
| 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 |
|
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
| 17 |
|
|
| 18 |
require File.expand_path('../../../../../../test_helper', __FILE__)
|
|
| 19 |
|
|
| 20 |
class Redmine::Views::Builders::XmlTest < ActiveSupport::TestCase |
|
| 21 |
|
|
| 22 |
def test_hash |
|
| 23 |
assert_xml_output('<person><name>Ryan</name><age>32</age></person>') do |b|
|
|
| 24 |
b.person do |
|
| 25 |
b.name 'Ryan' |
|
| 26 |
b.age 32 |
|
| 27 |
end |
|
| 28 |
end |
|
| 29 |
end |
|
| 30 |
|
|
| 31 |
def test_array |
|
| 32 |
assert_xml_output('<books type="array"><book title="Book 1"/><book title="Book 2"/></books>') do |b|
|
|
| 33 |
b.array :books do |b| |
|
| 34 |
b.book :title => 'Book 1' |
|
| 35 |
b.book :title => 'Book 2' |
|
| 36 |
end |
|
| 37 |
end |
|
| 38 |
end |
|
| 39 |
|
|
| 40 |
def test_array_with_content_tags |
|
| 41 |
assert_xml_output('<books type="array"><book author="B. Smith">Book 1</book><book author="G. Cooper">Book 2</book></books>') do |b|
|
|
| 42 |
b.array :books do |b| |
|
| 43 |
b.book 'Book 1', :author => 'B. Smith' |
|
| 44 |
b.book 'Book 2', :author => 'G. Cooper' |
|
| 45 |
end |
|
| 46 |
end |
|
| 47 |
end |
|
| 48 |
|
|
| 49 |
def test_nested_arrays |
|
| 50 |
assert_xml_output('<books type="array"><book><authors type="array"><author>B. Smith</author><author>G. Cooper</author></authors></book></books>') do |b|
|
|
| 51 |
b.array :books do |books| |
|
| 52 |
books.book do |book| |
|
| 53 |
book.array :authors do |authors| |
|
| 54 |
authors.author 'B. Smith' |
|
| 55 |
authors.author 'G. Cooper' |
|
| 56 |
end |
|
| 57 |
end |
|
| 58 |
end |
|
| 59 |
end |
|
| 60 |
end |
|
| 61 |
|
|
| 62 |
def assert_xml_output(expected, &block) |
|
| 63 |
builder = Redmine::Views::Builders::Xml.new(ActionDispatch::TestRequest.new, ActionDispatch::TestResponse.new) |
|
| 64 |
block.call(builder) |
|
| 65 |
assert_equal('<?xml version="1.0" encoding="UTF-8"?>' + expected, builder.output)
|
|
| 66 |
end |
|
| 67 |
end |
|
| .svn/pristine/65/65e02704959a4eede1859ab38db94f171c07d983.svn-base | ||
|---|---|---|
| 1 |
<div class="contextual"> |
|
| 2 |
<%= link_to(l(:label_attachment_new), new_project_file_path(@project), :class => 'icon icon-add') if User.current.allowed_to?(:manage_files, @project) %> |
|
| 3 |
</div> |
|
| 4 |
|
|
| 5 |
<h2><%=l(:label_attachment_plural)%></h2> |
|
| 6 |
|
|
| 7 |
<% delete_allowed = User.current.allowed_to?(:manage_files, @project) %> |
|
| 8 |
|
|
| 9 |
<table class="list files"> |
|
| 10 |
<thead><tr> |
|
| 11 |
<%= sort_header_tag('filename', :caption => l(:field_filename)) %>
|
|
| 12 |
<%= sort_header_tag('created_on', :caption => l(:label_date), :default_order => 'desc') %>
|
|
| 13 |
<%= sort_header_tag('size', :caption => l(:field_filesize), :default_order => 'desc') %>
|
|
| 14 |
<%= sort_header_tag('downloads', :caption => l(:label_downloads_abbr), :default_order => 'desc') %>
|
|
| 15 |
<th>MD5</th> |
|
| 16 |
<th></th> |
|
| 17 |
</tr></thead> |
|
| 18 |
<tbody> |
|
| 19 |
<% @containers.each do |container| %> |
|
| 20 |
<% next if container.attachments.empty? -%> |
|
| 21 |
<% if container.is_a?(Version) -%> |
|
| 22 |
<tr> |
|
| 23 |
<th colspan="6" align="left"> |
|
| 24 |
<%= link_to(h(container), {:controller => 'versions', :action => 'show', :id => container}, :class => "icon icon-package") %>
|
|
| 25 |
</th> |
|
| 26 |
</tr> |
|
| 27 |
<% end -%> |
|
| 28 |
<% container.attachments.each do |file| %> |
|
| 29 |
<tr class="file <%= cycle("odd", "even") %>">
|
|
| 30 |
<td class="filename"><%= link_to_attachment file, :download => true, :title => file.description %></td> |
|
| 31 |
<td class="created_on"><%= format_time(file.created_on) %></td> |
|
| 32 |
<td class="filesize"><%= number_to_human_size(file.filesize) %></td> |
|
| 33 |
<td class="downloads"><%= file.downloads %></td> |
|
| 34 |
<td class="digest"><%= file.digest %></td> |
|
| 35 |
<td align="center"> |
|
| 36 |
<%= link_to(image_tag('delete.png'), attachment_path(file),
|
|
| 37 |
:data => {:confirm => l(:text_are_you_sure)}, :method => :delete) if delete_allowed %>
|
|
| 38 |
</td> |
|
| 39 |
</tr> |
|
| 40 |
<% end |
|
| 41 |
reset_cycle %> |
|
| 42 |
<% end %> |
|
| 43 |
</tbody> |
|
| 44 |
</table> |
|
| 45 |
|
|
| 46 |
<% html_title(l(:label_attachment_plural)) -%> |
|
Also available in: Unified diff