]> git.cworth.org Git - sup/blob - lib/sup/mbox/loader.rb
labels now fully determined by sources.yaml, and lots of improvements to sup-config
[sup] / lib / sup / mbox / loader.rb
1 require 'rmail'
2 require 'uri'
3
4 module Redwood
5 module MBox
6
7 class Loader < Source
8   yaml_properties :uri, :cur_offset, :usual, :archived, :id, :labels
9   def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil, labels=[]
10     super uri_or_fp, start_offset, usual, archived, id
11
12     @mutex = Mutex.new
13     @labels = (labels || []).freeze
14
15     case uri_or_fp
16     when String
17       uri = URI(uri_or_fp)
18       raise ArgumentError, "not an mbox uri" unless uri.scheme == "mbox"
19       raise ArgumentError, "mbox uri ('#{uri}') cannot have a host: #{uri.host}" if uri.host
20       @f = File.open uri.path
21     else
22       @f = uri_or_fp
23     end
24   end
25
26   def self.suggest_labels_for path
27     ## heuristic: use the filename as a label, unless the file
28     ## has a path that probably represents an inbox.
29     if File.dirname(path) =~ /\b(var|usr|spool)\b/
30       []
31     else
32       [File.basename(path).intern]
33     end
34   end
35
36   def check
37     if (cur_offset ||= start_offset) > end_offset
38       raise OutOfSyncSourceError, "mbox file is smaller than last recorded message offset. Messages have probably been deleted by another client."
39     end
40   end
41     
42   def start_offset; 0; end
43   def end_offset; File.size @f; end
44
45   def load_header offset
46     header = nil
47     @mutex.synchronize do
48       @f.seek offset
49       l = @f.gets
50       unless l =~ BREAK_RE
51         raise OutOfSyncSourceError, "mismatch in mbox file offset #{offset.inspect}: #{l.inspect}." 
52       end
53       header = MBox::read_header @f
54     end
55     header
56   end
57
58   def load_message offset
59     @mutex.synchronize do
60       @f.seek offset
61       begin
62         RMail::Mailbox::MBoxReader.new(@f).each_message do |input|
63           return RMail::Parser.read(input)
64         end
65       rescue RMail::Parser::Error => e
66         raise FatalSourceError, "error parsing mbox file: #{e.message}"
67       end
68     end
69   end
70
71   def raw_header offset
72     ret = ""
73     @mutex.synchronize do
74       @f.seek offset
75       until @f.eof? || (l = @f.gets) =~ /^$/
76         ret += l
77       end
78     end
79     ret
80   end
81
82   def raw_full_message offset
83     ret = ""
84     each_raw_full_message_line(offset) { |l| ret += l }
85     ret
86   end
87
88   ## apparently it's a million times faster to call this directly if
89   ## we're just moving messages around on disk, than reading things
90   ## into memory with raw_full_message.
91   ##
92   ## i hoped never to have to move shit around on disk but
93   ## sup-sync-back has to do it.
94   def each_raw_full_message_line offset
95     @mutex.synchronize do
96       @f.seek offset
97       yield @f.gets
98       until @f.eof? || (l = @f.gets) =~ BREAK_RE
99         yield l
100       end
101     end
102   end
103
104   def next
105     returned_offset = nil
106     next_offset = cur_offset
107
108     begin
109       @mutex.synchronize do
110         @f.seek cur_offset
111
112         ## cur_offset could be at one of two places here:
113
114         ## 1. before a \n and a mbox separator, if it was previously at
115         ##    EOF and a new message was added; or,
116         ## 2. at the beginning of an mbox separator (in all other
117         ##    cases).
118
119         l = @f.gets or raise "next while at EOF"
120         if l =~ /^\s*$/ # case 1
121           returned_offset = @f.tell
122           @f.gets # now we're at a BREAK_RE, so skip past it
123         else # case 2
124           returned_offset = cur_offset
125           ## we've already skipped past the BREAK_RE, so just go
126         end
127
128         while(line = @f.gets)
129           break if line =~ BREAK_RE
130           next_offset = @f.tell
131         end
132       end
133     rescue SystemCallError, IOError => e
134       raise FatalSourceError, "Error reading #{@f.path}: #{e.message}"
135     end
136
137     self.cur_offset = next_offset
138     [returned_offset, @labels]
139   end
140 end
141
142 end
143 end