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