Class: Vmail::ReplyTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/vmail/reply_template.rb

Instance Method Summary collapse

Constructor Details

#initialize(mail, username, name, replyall, always_cc = nil) ⇒ ReplyTemplate

Returns a new instance of ReplyTemplate.



8
9
10
11
# File 'lib/vmail/reply_template.rb', line 8

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

Instance Method Details

#address_to_string(x) ⇒ Object

deprecated



54
55
56
# File 'lib/vmail/reply_template.rb', line 54

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

#ccObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vmail/reply_template.rb', line 37

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

#primary_recipientObject



31
32
33
34
35
# File 'lib/vmail/reply_template.rb', line 31

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

#reply_headers(try_again = true) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vmail/reply_template.rb', line 13

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.plaintext_part
  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

#senderObject



49
50
51
# File 'lib/vmail/reply_template.rb', line 49

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