Class: Maildir::Message

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/maildir/message.rb

Constant Summary collapse

COLON =

COLON seperates the unique name from the info

':'
INFO =

The default info, to which flags are appended

"2,"
FLAG_NAMES =
{
  :passed => 'P',
  :replied => 'R',
  :seen => 'S',
  :trashed => 'T',
  :draft => 'D',
  :flagged => 'F'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(maildir, key = nil) ⇒ Message

Create a new, unwritten message or instantiate an existing message. If key is nil, create a new message:

Message.new(maildir) # => a new, unwritten message

If key is not nil, instantiate a message object for the message at key.

Message.new(maildir, key) # => an existing message


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/maildir/message.rb', line 43

def initialize(maildir, key=nil)
  @maildir = maildir
  if key.nil?
    @dir         = :tmp
    @info        = nil
    @unique_name = Maildir::UniqueName.create
  else
    parse_key(key)
  end

  unless Maildir::SUBDIRS.include? dir
    raise ArgumentError, "State must be in #{Maildir::SUBDIRS.inspect}"
  end
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



34
35
36
# File 'lib/maildir/message.rb', line 34

def dir
  @dir
end

#infoObject

Returns the value of attribute info.



34
35
36
# File 'lib/maildir/message.rb', line 34

def info
  @info
end

#unique_nameObject (readonly)

Returns the value of attribute unique_name.



34
35
36
# File 'lib/maildir/message.rb', line 34

def unique_name
  @unique_name
end

Class Method Details

.create(maildir, data) ⇒ Object

Create a new message in maildir with data. The message is first written to the tmp dir, then moved to new. This is a shortcut for:

message = Maildir::Message.new(maildir)
message.write(data)


16
17
18
19
20
# File 'lib/maildir/message.rb', line 16

def self.create(maildir, data)
  message = self.new(maildir)
  message.write(data)
  message
end

.serializerObject

DEPRECATED: Get the serializer.

See Also:



24
25
26
# File 'lib/maildir/message.rb', line 24

def self.serializer
  Maildir.serializer
end

.serializer=(serializer) ⇒ Object

DEPRECATED: Set the serializer.



30
31
32
# File 'lib/maildir/message.rb', line 30

def self.serializer=(serializer)
  Maildir.serializer = serializer
end

Instance Method Details

#<=>(message) ⇒ Object

Compares messages by their paths. If message is a different class, return nil. Otherwise, return 1, 0, or -1.



61
62
63
64
65
66
# File 'lib/maildir/message.rb', line 61

def <=>(message)
  # Return nil if comparing different classes
  return nil unless self.class === message

  self.path <=> message.path
end

#add_flag(flag) ⇒ Object

Adds a flag to a message. Returns the message’s key if successful, false otherwise.



136
137
138
# File 'lib/maildir/message.rb', line 136

def add_flag(flag)
  self.flags = (flags << flag.upcase)
end

#atimeObject

Returns the message’s atime, or false if the file doesn’t exist.



177
178
179
# File 'lib/maildir/message.rb', line 177

def atime
  guard { File.atime(path) }
end

#dataObject

Returns the message’s data from disk. If the path doesn’t exist, freeze’s the object and raises Errno:ENOENT



166
167
168
# File 'lib/maildir/message.rb', line 166

def data
  guard(true) { serializer.load(path) }
end

#destroyObject

Deletes the message path and freezes the message object. Returns 1 if the file was destroyed, false if the file was missings.



188
189
190
191
# File 'lib/maildir/message.rb', line 188

def destroy
  freeze
  guard { File.delete(path) }
end

#filenameObject

Returns the filename of the message



150
151
152
# File 'lib/maildir/message.rb', line 150

def filename
  [unique_name, info].compact.join(COLON)
end

#flagsObject

Returns an array of single letter flags applied to the message



124
125
126
# File 'lib/maildir/message.rb', line 124

def flags
  @info.to_s.sub(INFO,'').split(//)
end

#flags=(*flags) ⇒ Object

Sets the flags on a message. Returns the message’s key if successful, false otherwise.



130
131
132
# File 'lib/maildir/message.rb', line 130

def flags=(*flags)
  self.info = INFO + sort_flags(flags.flatten.join(''))
end

#inspectObject

Friendly inspect method



69
70
71
# File 'lib/maildir/message.rb', line 69

def inspect
  "#<#{self.class} key=#{key} maildir=#{@maildir.inspect}>"
end

#keyObject

Returns the key to identify the message



155
156
157
# File 'lib/maildir/message.rb', line 155

def key
  File.join(dir.to_s, filename)
end

#mtimeObject

Returns the message’s mtime, or false if the file doesn’t exist.



182
183
184
# File 'lib/maildir/message.rb', line 182

def mtime
  guard { File.mtime(path) }
end

#pathObject

Returns the full path to the message



160
161
162
# File 'lib/maildir/message.rb', line 160

def path
  File.join(@maildir.path, key)
end

#processObject

Move a message from new to cur, add info. Returns the message’s key



94
95
96
# File 'lib/maildir/message.rb', line 94

def process
  rename(:cur, INFO)
end

#remove_flag(flags) ⇒ Object

Removes flags from a message. Returns the message’s key if successful, false otherwise.

flags

String or Array



144
145
146
147
# File 'lib/maildir/message.rb', line 144

def remove_flag(flags)
  return self.flags if flags.blank?
  self.flags = self.flags.reject { |f| f =~ /[#{Array(flags).join}]/i }
end

#serializerObject

Helper to get serializer.



74
75
76
# File 'lib/maildir/message.rb', line 74

def serializer
  @maildir.serializer
end

#utime(atime, mtime) ⇒ Object

Updates the modification and access time. Returns 1 if successful, false otherwise.



172
173
174
# File 'lib/maildir/message.rb', line 172

def utime(atime, mtime)
  guard { File.utime(atime, mtime, path) }
end

#write(data) ⇒ Object

Writes data to disk. Can only be called on messages instantiated without a key (which haven’t been written to disk). After successfully writing to disk, rename the message to the new dir

Returns the message’s key



83
84
85
86
87
88
89
90
# File 'lib/maildir/message.rb', line 83

def write(data)
  raise "Can only write to messages in tmp" unless :tmp == @dir

  # Write out contents to tmp
  serializer.dump(data, path)

  rename(:new)
end