class Luggage::Message

Attributes

connection[R]
date[R]
flags[RW]
mailbox[R]
message_id[R]
template[R]

Public Class Methods

new(connection, mailbox, args = {}, &block) click to toggle source

Creates a Message instance

`connection` should be an authenticated Imap connection `mailbox` should be either a Mailbox or a string describing a remote mailbox `args` when this message is appended to the remote server, this is the date which will be used `args` use this file as the initial raw email content. `args` use this as for the Message-ID header. This header is used to identify messages across requests

# File lib/luggage/message.rb, line 47
def initialize(connection, mailbox, args = {}, &block)
  @connection = connection
  @mailbox = mailbox.kind_of?(Mailbox) ? mailbox : Mailbox.new(connection, mailbox)
  @flags = args.has_key?(:flags) ? Array(args[:flags]) : []
  @date = args[:date] || Time.now
  @template = args[:template]
  @message_id = args[:message_id] || "<#{UUIDTools::UUID.random_create}@test.oib.com>"

  instance_eval &block if block_given?
end
new_local(connection, mailbox, args = {}, &block) click to toggle source

Creates a local Message instance

`connection` should be an authenticated Imap connection `mailbox` should be either a Mailbox or a string describing a remote mailbox `args` will be passed to Message::new. Keys used by Message::new will be set, any

other keys will be delegated to Mail::Message.

Example Usage: Message.new_local(c, 'INBOX', :template => 'base.eml', :date => 4.days.ago) Message.new_local(c, m, :subject => “VIAGRA ROCKS!”, :body => “<insert phishing here>”, :cc => “yourmom@gmail.com”)

# File lib/luggage/message.rb, line 20
def self.new_local(connection, mailbox, args = {}, &block)
  message = new(connection, mailbox, args)
  message.instance_eval do
    @mail = @template ? Mail.read(@template) : Mail.new
    args.each do |key, value|
      mail[key] = value if mail.respond_to?(key)
    end

    if mail[:message_id] && !args.has_key?(:message_id)
      @message_id = mail.message_id
    else
      mail[:message_id] = message_id
    end
  end

  message.instance_eval &block if block_given?
  message
end

Public Instance Methods

copy_to!(mailbox_name) click to toggle source

Uses IMAP's COPY command to copy the message into the named mailbox

# File lib/luggage/message.rb, line 85
def copy_to!(mailbox_name)
  mailbox.select!
  connection.uid_copy([uid], Luggage::Mailbox.convert_mailbox_name(mailbox_name))
end
delete!() click to toggle source

Add the 'Deleted' flag to this message on the remote server

# File lib/luggage/message.rb, line 92
def delete!
  mailbox.select!
  connection.uid_store([uid], "+FLAGS", [:Deleted])
  @mail = nil
end
exists?() click to toggle source

Returns true if a message with the same message_id exists in the remote mailbox

# File lib/luggage/message.rb, line 100
def exists?
  mailbox.select!
  connection.uid_search("HEADER.PEEK Message-ID #{message_id}")
end
host() click to toggle source
# File lib/luggage/message.rb, line 119
def host
  connection.instance_variable_get(:@host)
end
inspect() click to toggle source
# File lib/luggage/message.rb, line 115
def inspect
  "#<Luggage::Message server: \"#{host}\", mailbox: \"#{mailbox.name}\", message_id: \"#{message_id}\">"
end
method_missing(meth, *args, &block) click to toggle source

Proxy all other methods to this instance's Mail::Message

Calls superclass method
# File lib/luggage/message.rb, line 107
def method_missing(meth, *args, &block)
   if mail.respond_to?(meth)
     mail.send(meth, *args, &block)
   else
     super
   end
end
reload() click to toggle source

Fetch this message from the server and update all its attributes

# File lib/luggage/message.rb, line 68
def reload
  fields = fetch_fields
  @mail = Mail.new(fields["BODY[]"])
  @flags = fields["FLAGS"]
  @date = Time.parse(fields["INTERNALDATE"])
  self
end
save!() click to toggle source

Append this message to the remote mailbox

# File lib/luggage/message.rb, line 78
def save!
  mailbox.select!
  connection.append(mailbox.name, raw_message, flags, date)
end
to_s() click to toggle source

Formatted to save to file

Mail::Message.new( message.to_s ).raw_source = message.to_s

# File lib/luggage/message.rb, line 62
def to_s
  mail.encoded
end

Private Instance Methods

fetch_fields() click to toggle source
# File lib/luggage/message.rb, line 150
def fetch_fields
  response = connection.uid_fetch([uid], ["FLAGS", "INTERNALDATE", "BODY.PEEK[]"])
  raise MessageNotFoundError if response.empty?
  raise DuplicateMessageError if response.length > 1
  response.first[:attr]
end
fetch_uid() click to toggle source
# File lib/luggage/message.rb, line 143
def fetch_uid
  response = connection.uid_search("HEADER Message-ID #{message_id}")
  raise MessageNotFoundError if response.empty?
  raise DuplicateMessageError if response.length > 1
  response.first
end
mail() click to toggle source
# File lib/luggage/message.rb, line 131
def mail
  reload if @mail.nil?
  @mail
end
raw_message() click to toggle source

Formatted to upload to IMAP server

# File lib/luggage/message.rb, line 127
def raw_message
  mail.to_s
end
uid() click to toggle source
# File lib/luggage/message.rb, line 136
def uid
  return @uid if instance_variable_defined?(:@uid)

  mailbox.select!
  @uid = fetch_uid
end