#-- # Copyright (C) 2007 Dimitrij Denissenko # Please read LICENSE document for more information. #++ require 'uri' class Notifications < ActionMailer::Base helper NotificationsHelper include ActionController::UrlWriter def self.default_url_options options = {} if uri = site_uri options[:protocol] = uri.scheme options[:host] = uri.host options[:port] = uri.port unless uri.port == uri.default_port end options end def self.site_uri uri = URI.parse(RetroCM[:general][:basic][:site_url].dup) rescue nil uri.is_a?(URI::HTTP) ? uri : nil end # Overwrites url_for to support relative-root installation def url_for(options) url = super(options) if site_uri = self.class.site_uri uri = URI.parse(url) uri.path = site_uri.path + uri.path url = uri.to_s end url end def account_validation(user, options = {}) @subject = options[:subject] || "[#{site_name}] " + _("Account validation") @body = {:user => user, :site_name => site_name} @recipients = options[:recipients] || user.email @from = options[:from] || from_address @sent_on = options[:sent_on] || Time.now @headers = {} end def account_activation_note(user, options = {}) @subject = options[:subject] || "[#{site_name}] " + _("Account activation") @body = {:user => user, :site_name => site_name} @recipients = options[:recipients] || user.email @from = options[:from] || from_address @sent_on = options[:sent_on] || Time.now @headers = {} end def password_reset_note(user, password, options = {}) @subject = options[:subject] || "[#{site_name}] " + _("Password reset") @body = {:user => user, :password => password, :site_name => site_name} @recipients = options[:recipients] || user.email @from = options[:from] || from_address @sent_on = options[:sent_on] || Time.now @headers = {} end def ticket_creation_note(ticket, options = {}) @subject = options[:subject] || "[#{site_name}] #{ticket.previewable.title}" @body = {:ticket => ticket} @recipients = options[:recipients] @from = options[:from] || from_address @sent_on = options[:sent_on] || Time.now @headers = {} end def ticket_update_note(ticket_change, options = {}) @subject = options[:subject] || "[#{site_name}] #{ticket_change.previewable.title}" @body = {:ticket_change => ticket_change} @recipients = options[:recipients] @from = options[:from] || from_address @sent_on = options[:sent_on] || Time.now @headers = {} end def receive(email) begin notification_logger.info("\nReceived notification '#{email.subject}' from '#{email.from}'.") ticket_id = Kernel.Integer(email.subject.scan(%r{\#(\d+)})[0][0]) rescue nil raise "Unable to determine Ticket ID from mail subject." unless ticket_id && ticket_id > 0 content = email.body.split(%r{[-_]{5,}}m).first.strip raise "Email content is empty." if content.blank? ticket = Ticket.find_by_id(ticket_id) raise "Unable to find ticket '##{ticket_id}' in the DB." unless ticket ticket.monitor_attribute_changes! user = User.find_by_email(email.from) raise "Unable to find user '#{email.from}' in the DB." unless user raise "User '#{user.name}' does not have the permission to modify tickets." unless user.has_permission?(ticket.project, :create_and_work_on_tickets) change = ticket.ticket_changes.build(:content => content) change.user = user if email.has_attachments? change.attachment = Attachment.parse(email.attachments.first) end TicketChange.without_spam_check do if ticket.save notification_logger.info "Ticket '##{ticket.id}' was sucessfully updated." end end rescue notification_logger.error(" [ERROR] #{$!}") end end def self.notification_logger @logger ||= Logger.new(File.join(RAILS_ROOT, 'log', 'notifications.log')) end def notification_logger self.class.notification_logger end def self.reload_settings settings = RetroCM[:email][:smtp].settings.inject({}) do |res, setting| key = setting.name.to_sym res[key] = setting.value res end settings[:authentication] = settings[:authentication].to_sym if settings[:authentication] == :none || settings[:user_name].blank? || settings[:password].blank? settings.delete(:user_name) settings.delete(:password) settings.delete(:authentication) end ActionMailer::Base.smtp_settings = settings end private def from_address RetroCM[:email][:general][:from] end def site_name RetroCM[:general][:basic][:site_name] end end