Class: Juno::Folder

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

Instance Method Summary collapse

Constructor Details

#initialize(name, path) ⇒ Folder

Returns a new instance of Folder.



10
11
12
13
# File 'lib/juno/folder.rb', line 10

def initialize(name, path)
  @name = name
  @path = Pathname.new(path)
end

Instance Method Details

#contains_duplicate_messages?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/juno/folder.rb', line 55

def contains_duplicate_messages?
  @contains_duplicate_messages ||= Set.new(messages).count != messages.count
end

#messagesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/juno/folder.rb', line 15

def messages
  return @messages if defined? @messages
  ole = Ole::Storage.new(@path.to_s)
  @messages = ole.root.children.map do |message_dirent|

    # This condition is met once per folder, when
    # the dirent's name_utf16 is "directory"
    next if message_dirent.children.nil?
    
    body = message_dirent.children[0].read
    
    headers = message_dirent.children[1].read

    # Some emails have a weird numeric "header" separated
    # from the other headers by two newlines.  To the best
    # of my knowledge, this is invalid so let's just drop it.
    headers.sub!(/\n\n\d+\Z/, '')

    headers = headers.lines
    headers.each(&:chomp!)
    headers.reject!(&:empty?)

    # This condition is for emails from Juno, including advertisements
    # and announcements. These emails are a bit unusual in that they
    # lack a date header, so instead of dealing with whatever problems
    # that might cause, they're of so little interest to anybody that
    # we might as well just throw them away.
    next if headers.any?{|h| h =~ /^X-UNTD-MSG:\s+internal/}

    Message.new(headers, body)
  end

  @messages.compact!
  @messages
end

#nameObject



51
52
53
# File 'lib/juno/folder.rb', line 51

def name
  @name
end

#write_mbox(path, opts = {}) ⇒ Object



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

def write_mbox(path, opts={})
  if path.exist? && !opts[:overwrite]
    raise "#{path} already exists"
  end

  path.open('w') do |f|
    messages.each do |message|
      f.puts message.to_mbox
    end
  end
end