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