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