Sha256: 75981488de967fd199aa67a23cce6fc467b7f084eb488faebd07701a55d71721

Contents?: true

Size: 1.57 KB

Versions: 53

Compression:

Stored size: 1.57 KB

Contents

require 'vmail/message_formatter'
require 'mail'
require 'time'

module Vmail
  class ReplyTemplate

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

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

    def primary_recipient
      from = @mail.header['from']
      reply_to = @mail.header['reply-to']
      [ reply_to, from ].flatten.compact.map(&:to_s)[0]
    end

    def cc
      return nil unless @replyall
      cc = @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.to_s[/<([^>]+)>/, 1] !~ /#{@username}/ && x.to_s[/^[^<]+/, 1] !~ /#{@name}/
          }.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

53 entries across 53 versions & 1 rubygems

Version Path
vmail-1.0.4 lib/vmail/reply_template.rb
vmail-1.0.3 lib/vmail/reply_template.rb
vmail-1.0.2 lib/vmail/reply_template.rb
vmail-1.0.1 lib/vmail/reply_template.rb
vmail-1.0.0 lib/vmail/reply_template.rb
vmail-0.9.9 lib/vmail/reply_template.rb
vmail-0.9.8 lib/vmail/reply_template.rb
vmail-0.9.7 lib/vmail/reply_template.rb
vmail-0.9.6 lib/vmail/reply_template.rb
vmail-0.9.5 lib/vmail/reply_template.rb
vmail-0.9.4 lib/vmail/reply_template.rb
vmail-0.9.3 lib/vmail/reply_template.rb
vmail-0.9.2 lib/vmail/reply_template.rb
vmail-0.9.1 lib/vmail/reply_template.rb
vmail-0.9.0 lib/vmail/reply_template.rb
vmail-0.8.9 lib/vmail/reply_template.rb
vmail-0.8.8 lib/vmail/reply_template.rb
vmail-0.8.7 lib/vmail/reply_template.rb
vmail-0.8.6 lib/vmail/reply_template.rb
vmail-0.8.5 lib/vmail/reply_template.rb