]> git.cworth.org Git - sup/blob - lib/sup/mbox/loader.rb
massive fixes for imap and mbox+ssh
[sup] / lib / sup / mbox / loader.rb
1 require 'rmail'
2
3 module Redwood
4 module MBox
5
6 class Loader < Source
7   attr_reader_cloned :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   def start_offset; 0; end
31   def end_offset; File.size @f; end
32   def total; end_offset; end
33
34   def load_header offset
35     header = nil
36     @mutex.synchronize do
37       @f.seek offset
38       l = @f.gets
39       unless l =~ BREAK_RE
40         self.broken_msg = "offset mismatch in mbox file offset #{offset.inspect}: #{l.inspect}. Run 'sup-import --rebuild #{to_s}' to correct this." 
41         raise SourceError, self.broken_msg
42       end
43       header = MBox::read_header @f
44     end
45     header
46   end
47
48   def load_message offset
49     raise SourceError, self.broken_msg if broken?
50     @mutex.synchronize do
51       @f.seek offset
52       begin
53         RMail::Mailbox::MBoxReader.new(@f).each_message do |input|
54           return RMail::Parser.read(input)
55         end
56       rescue RMail::Parser::Error => e
57         raise SourceError, "error parsing message with rmail: #{e.message}"
58       end
59     end
60   end
61
62   def raw_header offset
63     raise SourceError, self.broken_msg if broken?
64     ret = ""
65     @mutex.synchronize do
66       @f.seek offset
67       until @f.eof? || (l = @f.gets) =~ /^$/
68         ret += l
69       end
70     end
71     ret
72   end
73
74   def raw_full_message offset
75     raise SourceError, self.broken_msg if broken?
76     ret = ""
77     @mutex.synchronize do
78       @f.seek offset
79       @f.gets # skip mbox header
80       until @f.eof? || (l = @f.gets) =~ BREAK_RE
81         ret += l
82       end
83     end
84     ret
85   end
86
87   def next
88     raise SourceError, self.broken_msg if broken?
89     returned_offset = nil
90     next_offset = cur_offset
91
92     @mutex.synchronize do
93       @f.seek cur_offset
94
95       ## cur_offset could be at one of two places here:
96
97       ## 1. before a \n and a mbox separator, if it was previously at
98       ##    EOF and a new message was added; or,
99       ## 2. at the beginning of an mbox separator (in all other
100       ##    cases).
101
102       l = @f.gets or raise "next while at EOF"
103       if l =~ /^\s*$/ # case 1
104         returned_offset = @f.tell
105         @f.gets # now we're at a BREAK_RE, so skip past it
106       else # case 2
107         returned_offset = cur_offset
108         ## we've already skipped past the BREAK_RE, to just go
109       end
110
111       while(line = @f.gets)
112         break if line =~ BREAK_RE
113         next_offset = @f.tell
114       end
115     end
116
117     self.cur_offset = next_offset
118     [returned_offset, labels]
119   end
120 end
121
122 Redwood::register_yaml(Loader, %w(uri cur_offset usual archived id))
123
124 end
125 end