]> git.cworth.org Git - sup/blob - lib/sup/mbox/loader.rb
have mbox, maildir and imap sources (de)serialize labels nicely
[sup] / lib / sup / mbox / loader.rb
1 require 'rmail'
2 require 'uri'
3 require 'set'
4
5 module Redwood
6 module MBox
7
8 class Loader < Source
9   include SerializeLabelsNicely
10   yaml_properties :uri, :cur_offset, :usual, :archived, :id, :labels
11
12   ## uri_or_fp is horrific. need to refactor.
13   def initialize uri_or_fp, start_offset=0, usual=true, archived=false, id=nil, labels=nil
14     @mutex = Mutex.new
15     @labels = Set.new((labels || []) - LabelManager::RESERVED_LABELS)
16
17     case uri_or_fp
18     when String
19       uri = URI(Source.expand_filesystem_uri(uri_or_fp))
20       raise ArgumentError, "not an mbox uri" unless uri.scheme == "mbox"
21       raise ArgumentError, "mbox URI ('#{uri}') cannot have a host: #{uri.host}" if uri.host
22       raise ArgumentError, "mbox URI must have a path component" unless uri.path
23       @f = File.open uri.path
24       @path = uri.path
25     else
26       @f = uri_or_fp
27       @path = uri_or_fp.path
28     end
29
30     super uri_or_fp, start_offset, usual, archived, id
31   end
32
33   def file_path; @path end
34   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
35
36   def self.suggest_labels_for path
37     ## heuristic: use the filename as a label, unless the file
38     ## has a path that probably represents an inbox.
39     if File.dirname(path) =~ /\b(var|usr|spool)\b/
40       []
41     else
42       [File.basename(path).downcase.intern]
43     end
44   end
45
46   def check
47     if (cur_offset ||= start_offset) > end_offset
48       raise OutOfSyncSourceError, "mbox file is smaller than last recorded message offset. Messages have probably been deleted by another client."
49     end
50   end
51
52   def start_offset; 0; end
53   def end_offset; File.size @f; end
54
55   def load_header offset
56     header = nil
57     @mutex.synchronize do
58       @f.seek offset
59       l = @f.gets
60       unless MBox::is_break_line? l
61         raise OutOfSyncSourceError, "mismatch in mbox file offset #{offset.inspect}: #{l.inspect}." 
62       end
63       header = parse_raw_email_header @f
64     end
65     header
66   end
67
68   def load_message offset
69     @mutex.synchronize do
70       @f.seek offset
71       begin
72         ## don't use RMail::Mailbox::MBoxReader because it doesn't properly ignore
73         ## "From" at the start of a message body line.
74         string = ""
75         l = @f.gets
76         string << l until @f.eof? || MBox::is_break_line?(l = @f.gets)
77         RMail::Parser.read string
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? || MBox::is_break_line?(l = @f.gets)
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   def store_message date, from_email, &block
114     need_blank = File.exists?(@filename) && !File.zero?(@filename)
115     File.open(@filename, "a") do |f|
116       f.puts if need_blank
117       f.puts "From #{from_email} #{date.utc}"
118       yield f
119     end
120   end
121
122   ## apparently it's a million times faster to call this directly if
123   ## we're just moving messages around on disk, than reading things
124   ## into memory with raw_message.
125   ##
126   ## i hoped never to have to move shit around on disk but
127   ## sup-sync-back has to do it.
128   def each_raw_message_line offset
129     @mutex.synchronize do
130       @f.seek offset
131       yield @f.gets
132       until @f.eof? || MBox::is_break_line?(l = @f.gets)
133         yield l
134       end
135     end
136   end
137
138   def next
139     returned_offset = nil
140     next_offset = cur_offset
141
142     begin
143       @mutex.synchronize do
144         @f.seek cur_offset
145
146         ## cur_offset could be at one of two places here:
147
148         ## 1. before a \n and a mbox separator, if it was previously at
149         ##    EOF and a new message was added; or,
150         ## 2. at the beginning of an mbox separator (in all other
151         ##    cases).
152
153         l = @f.gets or return nil
154         if l =~ /^\s*$/ # case 1
155           returned_offset = @f.tell
156           @f.gets # now we're at a BREAK_RE, so skip past it
157         else # case 2
158           returned_offset = cur_offset
159           ## we've already skipped past the BREAK_RE, so just go
160         end
161
162         while(line = @f.gets)
163           break if MBox::is_break_line? line
164           next_offset = @f.tell
165         end
166       end
167     rescue SystemCallError, IOError => e
168       raise FatalSourceError, "Error reading #{@f.path}: #{e.message}"
169     end
170
171     self.cur_offset = next_offset
172     [returned_offset, (@labels + [:unread])]
173   end
174 end
175
176 end
177 end