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
VERSION =
'2.2.0'
@@serializer =

Default serializer.

Maildir::Serializer::Base.new

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.



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

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.



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

def serializer
  @serializer
end

Class Method Details

.serializerObject

Gets the default serializer.



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

def self.serializer
  @@serializer
end

.serializer=(serializer) ⇒ Object

Sets the default serializer.



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

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.



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

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.



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

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.



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

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.



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

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

#get(key) ⇒ Object

Returns a message object for key



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

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.



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

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

#inspectObject

Friendly inspect method



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

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


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

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