]> git.cworth.org Git - sup/blob - lib/sup/mbox/loader.rb
minor bugfix
[sup] / lib / sup / mbox / loader.rb
1 require 'rmail'
2
3 module Redwood
4 module MBox
5
6 class Loader < Source
7   attr_reader :labels
8
9   def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil
10     super
11
12     @mutex = Mutex.new
13     @labels = [:unread]
14     @labels << :inbox unless archived?
15
16     case uri_or_fp
17     when String
18       raise ArgumentError, "not an mbox uri" unless uri_or_fp =~ %r!mbox://!
19
20       fn = uri_or_fp.sub(%r!^mbox://!, "")
21       ## heuristic: use the filename as a label, unless the file
22       ## has a path that probably represents an inbox.
23       @labels << File.basename(fn).intern unless File.dirname(fn) =~ /\b(var|usr|spool)\b/
24       @f = File.open fn
25     else
26       @f = uri_or_fp
27     end
28   end
29
30   attr_writer :f
31   protected :f=
32
33   def start_offset; 0; end
34   def end_offset; File.size @f; end
35   def total; end_offset; end
36
37   def load_header offset
38     header = nil
39     @mutex.synchronize do
40       @f.seek offset
41       l = @f.gets
42       unless l =~ BREAK_RE
43         self.broken_msg = "offset mismatch in mbox file offset #{offset.inspect}: #{l.inspect}. Run 'sup-import --rebuild #{to_s}' to correct this." 
44         raise SourceError, self.broken_msg
45       end
46       header = MBox::read_header @f
47     end
48     header
49   end
50
51   def load_message offset
52     raise SourceError, self.broken_msg if broken?
53     @mutex.synchronize do
54       @f.seek offset
55       begin
56         RMail::Mailbox::MBoxReader.new(@f).each_message do |input|
57           return RMail::Parser.read(input)
58         end
59       rescue RMail::Parser::Error => e
60         raise SourceError, "error parsing message with rmail: #{e.message}"
61       end
62     end
63   end
64
65   def raw_header offset
66     raise SourceError, self.broken_msg if broken?
67     ret = ""
68     @mutex.synchronize do
69       @f.seek offset
70       until @f.eof? || (l = @f.gets) =~ /^$/
71         ret += l
72       end
73     end
74     ret
75   end
76
77   def raw_full_message offset
78     raise SourceError, self.broken_msg if broken?
79     ret = ""
80     @mutex.synchronize do
81       @f.seek offset
82       @f.gets # skip mbox header
83       until @f.eof? || (l = @f.gets) =~ BREAK_RE
84         ret += l
85       end
86     end
87     ret
88   end
89
90   def next
91     raise SourceError, self.broken_msg if broken?
92     returned_offset = nil
93     next_offset = cur_offset
94
95     @mutex.synchronize do
96       @f.seek cur_offset
97
98       ## cur_offset could be at one of two places here:
99
100       ## 1. before a \n and a mbox separator, if it was previously at
101       ##    EOF and a new message was added; or,
102       ## 2. at the beginning of an mbox separator (in all other
103       ##    cases).
104
105       l = @f.gets or raise "next while at EOF"
106       if l =~ /^\s*$/ # case 1
107         returned_offset = @f.tell
108         @f.gets # now we're at a BREAK_RE, so skip past it
109       else # case 2
110         returned_offset = cur_offset
111         ## we've already skipped past the BREAK_RE, to just go
112       end
113
114       while(line = @f.gets)
115         break if line =~ BREAK_RE
116         next_offset = @f.tell
117       end
118     end
119
120     self.cur_offset = next_offset
121     [returned_offset, labels]
122   end
123 end
124
125 Redwood::register_yaml(Loader, %w(uri cur_offset usual archived id))
126
127 end
128 end