Sha256: 247c0d1cff8b1e5b982bd76c49bb112c4ddb9d613581f2f2f7685f3e59c9e389

Contents?: true

Size: 1.86 KB

Versions: 3

Compression:

Stored size: 1.86 KB

Contents

# encoding: UTF-8
require 'mail'
require 'time'

module Vmail
  class ReplyTemplate

    def initialize(mail, username, name, replyall, always_cc=nil)
      @username, @name, @replyall, @always_cc = username, name, replyall, always_cc
      @mail = Mail.new(mail)
    end

    def reply_headers(try_again = true)
      formatter = Vmail::MessageFormatter.new(@mail)
      @orig_headers = formatter.extract_headers
      subject = @orig_headers['subject']
      if subject !~ /Re: /
        subject = "Re: #{subject}"
      end
      date = @orig_headers['date'].is_a?(String) ? Time.parse(@orig_headers['date']) : @orig_headers['date']
      quote_header = date ? "On #{date.strftime('%a, %b %d, %Y at %I:%M %p')}, #{sender} wrote:\n\n" : "#{sender} wrote:\n\n"
      body = quote_header + formatter.process_body
      begin
        body = body.gsub(/^(?=>)/, ">").gsub(/^(?!>)/, "> ")
      rescue
        body = Iconv.conv(body, "US-ASCII//TRANSLIT//IGNORE", "UTF-8").gsub(/^(?=>)/, ">").gsub(/^(?!>)/, "> ")
      end
      {'from' => "#@name <#@username>", 'to' => primary_recipient, 'cc' => cc, 'subject' => subject, :body => body}
    end

    def primary_recipient
      from = @orig_headers['from']
      reply_to = @mail.header['Reply-To']
      @primary_recipient = (reply_to || from).to_s 
    end

    def cc
      return nil unless (@replyall || @always_cc)
      cc = @mail.header['to'] ? @mail.header['to'].value.split(/,\s*/) : []
      if @mail.header['cc']
        cc += @mail.header['cc'].value.split(/,\s*/) 
      end
      cc = cc.flatten.compact.
        select {|x| x !~ /#{@username}/} 
      cc << @always_cc
      cc.select {|x| x !~ /#{@primary_recipient}/}.join(', ')
    end

    def sender
      @mail.header['from'].value
    end

    # deprecated
    def address_to_string(x)
      x.name ? "#{x.name} <#{x.mailbox}@#{x.host}>" : "#{x.mailbox}@#{x.host}"
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vmail-1.6.8 lib/vmail/reply_template.rb
vmail-1.6.7 lib/vmail/reply_template.rb
vmail-1.6.6 lib/vmail/reply_template.rb