Class: Swallow::MailDirFolder

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

Instance Method Summary collapse

Constructor Details

#initialize(folder_path) ⇒ MailDirFolder

Returns a new instance of MailDirFolder.



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

def initialize(folder_path)
    @folder_path = folder_path
    @sub_folders = ["cur", "new", "tmp"]
end

Instance Method Details

#add(emails) ⇒ Object



78
79
80
81
82
83
# File 'lib/maildir.rb', line 78

def add(emails)
    # We'll take these emails from some other location and add them to our "cur" folder.
    emails.each do |email|
        email.move_to(File.join(@folder_path,"cur"))
    end
end

#countObject



30
31
32
33
34
35
36
# File 'lib/maildir.rb', line 30

def count
    v = 0
    @sub_folders.each { |dir|
        v += length(dir)
    }
    return v
end

#delete_all_emailsObject



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

def delete_all_emails
    count = emails.length
    emails.each {|email| email.delete }
    puts "Deleted #{count} emails"
end

#emailsObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/maildir.rb', line 59

def emails
    list = Array.new
    @sub_folders.each do |sub_path|
        path = File.join(@folder_path, sub_path)
        Dir.open(path).each do |f|
            filename = File.join(path,f)
            next if File.directory?(filename)
            list << Email.new(filename)
        end
    end
    return list
end

#emails_newer_than(limit) ⇒ Object



85
86
87
# File 'lib/maildir.rb', line 85

def emails_newer_than(limit)
    emails.find_all { |e| e.newer_than?(limit) }
end

#emails_older_than(limit) ⇒ Object



89
90
91
# File 'lib/maildir.rb', line 89

def emails_older_than(limit)
    emails.find_all { |e| e.older_than?(limit) }
end

#enumObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/maildir.rb', line 13

def enum
    Dir.chdir(@folder_path)
    #puts "Subfolders:"
    a = Array.new
    Dir.glob("\.*").sort.each { |dir|
        next unless File.directory?(dir)
        next if dir == "." or dir == ".."
        #puts "#{dir}"
        a << dir
    }
    a
end

#length(sub_path) ⇒ Object



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

def length(sub_path)
    length = 0
    Dir.open(File.join(@folder_path,sub_path)).each do |filename|
        next if filename == "." or filename == ".."
        length += 1
    end
    length
end

#pathObject



26
27
28
# File 'lib/maildir.rb', line 26

def path
    @folder_path
end

#read_countObject



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

def read_count
    emails.find_all{ |e| e.read == true }.length
end

#statusObject



100
101
102
# File 'lib/maildir.rb', line 100

def status 
    "#{@folder_path} has #{unread_count} unread messages and #{read_count} read messages (#{unknown_count})."        
end

#sweep!(limit) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/maildir.rb', line 93

def sweep!(limit)
  if limit.kind_of? Integer then
    @sub_folder.each do |sub_path|
      Dir.open(File.join(@folder_path,sub_path)).sweep!(limit)
    end
  end
end

#to_sObject



103
104
105
# File 'lib/maildir.rb', line 103

def to_s
    "#{@folder_path}"
end

#unknown_countObject



46
47
48
# File 'lib/maildir.rb', line 46

def unknown_count
    length("tmp")
end

#unread_countObject



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

def unread_count
    emails.find_all{ |e| e.read == false }.length
end