Class: Maildir

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/maildir.rb,
lib/maildir/version.rb,
lib/maildir/serializer/base.rb,
lib/maildir/serializer/json.rb,
lib/maildir/serializer/mail.rb,
lib/maildir/serializer/yaml.rb,
lib/maildir/serializer/marshal.rb

Defined Under Namespace

Modules: Serializer Classes: Message, UniqueName

Constant Summary collapse

SUBDIRS =
[:tmp, :new, :cur].freeze
DEFAULT_SERIALIZER =

Default serializer.

Maildir::Serializer::Base.new.freeze
VERSION =
'2.2.1'
@@serializer =
DEFAULT_SERIALIZER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, create = true) ⇒ Maildir

Create a new maildir at path. If create is true, will ensure that the required subdirectories exist.



35
36
37
38
39
40
# File 'lib/maildir.rb', line 35

def initialize(path, create = true)
  @path = File.expand_path(path)
  @path = File.join(@path, '/') # Ensure path has a trailing slash
  @path_regexp = /^#{Regexp.quote(@path)}/ # For parsing directory listings
  create_directories if create
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#serializerObject

Returns own serializer or falls back to default.



43
44
45
# File 'lib/maildir.rb', line 43

def serializer
  @serializer
end

Class Method Details

.serializerObject

Gets the default serializer.



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

def self.serializer
  @@serializer
end

.serializer=(serializer) ⇒ Object

Sets the default serializer.



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

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

Instance Method Details

#<=>(maildir) ⇒ Object

Compare maildirs by their paths. If maildir is a different class, return nil. Otherwise, return 1, 0, or -1.



50
51
52
53
54
55
# File 'lib/maildir.rb', line 50

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

  self.path <=> maildir.path
end

#add(data) ⇒ Object

Writes data object out as a new message. Returns a Maildir::Message. See Maildir::Message.create for more.



118
119
120
# File 'lib/maildir.rb', line 118

def add(data)
  Maildir::Message.create(self, data)
end

#create_directoriesObject

Ensure subdirectories exist. This can safely be called multiple times, but must hit the disk. Avoid calling this if you’re certain the directories exist.



72
73
74
75
76
77
# File 'lib/maildir.rb', line 72

def create_directories
  SUBDIRS.each do |subdir|
    subdir_path = File.join(path, subdir.to_s)
    FileUtils.mkdir_p(subdir_path)
  end
end

#delete(key) ⇒ Object

Deletes the message for key by calling destroy() on the message.



128
129
130
# File 'lib/maildir.rb', line 128

def delete(key)
  get(key).destroy
end

#get(key) ⇒ Object

Returns a message object for key



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

def get(key)
  Maildir::Message.new(self, key)
end

#get_stale_tmp(time = Time.now - 129600) ⇒ Object

Finds messages in the tmp folder that have not been modified since time. time defaults to 36 hours ago.



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

def get_stale_tmp(time = Time.now - 129600)
  list(:tmp).select do |message|
    (mtime = message.mtime) && mtime < time
  end
end

#inspectObject

Friendly inspect method



58
59
60
# File 'lib/maildir.rb', line 58

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

#list(dir, options = {}) ⇒ Object

Returns an arry of messages from :new or :cur directory, sorted by key. If options is specified and directory is :cur, returns messages with flags specified

E.g. maildir.list(:cur, :flags => ‘F’) # => lists all messages with flag ‘F’ maildir.list(:cur, :flags => ‘FS’) # => lists all messages with flag ‘F’ and ‘S’; Flags must be specified in acending ASCII order (‘FS’ and not ‘SF’) maildir.list(:cur, :flags => ”) # => lists all messages without any flags This option does not work for :new directory

If options is specified, returns only so many keys.

E.g.

maildir.list(:new) # => all new messages
maildir.list(:cur, :limit => 10) # => 10 oldest messages in cur


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/maildir.rb', line 93

def list(dir, options = {})
  unless SUBDIRS.include? dir.to_sym
    raise ArgumentError, "dir must be :new, :cur, or :tmp"
  end

  # Set flags to filter messages
  # Silently ignored if dir is :new
  flags = (dir.to_sym == :cur) ? options[:flags] : nil
  keys = get_dir_listing(dir, :flags => flags)

  # Sort the keys (chronological order)
  # TODO: make sorting configurable
  keys.sort!

  # Apply the limit after sorting
  if limit = options[:limit]
    keys = keys[0,limit]
  end

  # Map keys to message objects
  keys.map{|key| get(key)}
end