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