]> git.cworth.org Git - sup/blob - lib/sup/mbox/loader.rb
831c71705ed11f8dab9a781c323ff2e5b2fd04d3
[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=0, 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 MBox::is_break_line? l
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         ## don't use RMail::Mailbox::MBoxReader because it doesn't properly ignore
72         ## "From" at the start of a message body line.
73         string = ""
74         l = @f.gets
75         string << l until @f.eof? || MBox::is_break_line?(l = @f.gets)
76         RMail::Parser.read string
77       rescue RMail::Parser::Error => e
78         raise FatalSourceError, "error parsing mbox file: #{e.message}"
79       end
80     end
81   end
82
83   ## scan forward until we're at the valid start of a message
84   def correct_offset!
85     @mutex.synchronize do
86       @f.seek cur_offset
87       string = ""
88       until @f.eof? || MBox::is_break_line?(l = @f.gets)
89         string << l
90       end
91       self.cur_offset += string.length
92     end
93   end
94
95   def raw_header offset
96     ret = ""
97     @mutex.synchronize do
98       @f.seek offset
99       until @f.eof? || (l = @f.gets) =~ /^\r*$/
100         ret << l
101       end
102     end
103     ret
104   end
105
106   def raw_message offset
107     ret = ""
108     each_raw_message_line(offset) { |l| ret << l }
109     ret
110   end
111
112   def store_message date, from_email, &block
113     need_blank = File.exists?(@filename) && !File.zero?(@filename)
114     File.open(@filename, "a") do |f|
115       f.puts if need_blank
116       f.puts "From #{from_email} #{date}"
117       yield f
118     end
119   end
120
121   ## apparently it's a million times faster to call this directly if
122   ## we're just moving messages around on disk, than reading things
123   ## into memory with raw_message.
124   ##
125   ## i hoped never to have to move shit around on disk but
126   ## sup-sync-back has to do it.
127   def each_raw_message_line offset
128     @mutex.synchronize do
129       @f.seek offset
130       yield @f.gets
131       until @f.eof? || MBox::is_break_line?(l = @f.gets)
132         yield l
133       end
134     end
135   end
136
137   def next
138     returned_offset = nil
139     next_offset = cur_offset
140
141     begin
142       @mutex.synchronize do
143         @f.seek cur_offset
144
145         ## cur_offset could be at one of two places here:
146
147         ## 1. before a \n and a mbox separator, if it was previously at
148         ##    EOF and a new message was added; or,
149         ## 2. at the beginning of an mbox separator (in all other
150         ##    cases).
151
152         l = @f.gets or return nil
153         if l =~ /^\s*$/ # case 1
154           returned_offset = @f.tell
155           @f.gets # now we're at a BREAK_RE, so skip past it
156         else # case 2
157           returned_offset = cur_offset
158           ## we've already skipped past the BREAK_RE, so just go
159         end
160
161         while(line = @f.gets)
162           break if MBox::is_break_line? line
163           next_offset = @f.tell
164         end
165       end
166     rescue SystemCallError, IOError => e
167       raise FatalSourceError, "Error reading #{@f.path}: #{e.message}"
168     end
169
170     self.cur_offset = next_offset
171     [returned_offset, (self.labels + [:unread]).uniq]
172   end
173 end
174
175 end
176 end