lib/sup/mbox/loader.rb in sup-0.1 vs lib/sup/mbox/loader.rb in sup-0.2
- old
+ new
@@ -4,28 +4,34 @@
module Redwood
module MBox
class Loader < Source
yaml_properties :uri, :cur_offset, :usual, :archived, :id, :labels
- def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil, labels=[]
- super uri_or_fp, start_offset, usual, archived, id
+ ## uri_or_fp is horrific. need to refactor.
+ def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil, labels=[]
@mutex = Mutex.new
- @labels = (labels || []).freeze
+ @labels = ((labels || []) - LabelManager::RESERVED_LABELS).uniq.freeze
case uri_or_fp
when String
- uri = URI(uri_or_fp)
+ uri = URI(Source.expand_filesystem_uri(uri_or_fp))
raise ArgumentError, "not an mbox uri" unless uri.scheme == "mbox"
- raise ArgumentError, "mbox uri ('#{uri}') cannot have a host: #{uri.host}" if uri.host
+ raise ArgumentError, "mbox URI ('#{uri}') cannot have a host: #{uri.host}" if uri.host
+ raise ArgumentError, "mbox URI must have a path component" unless uri.path
@f = File.open uri.path
+ @path = uri.path
else
@f = uri_or_fp
+ @path = uri_or_fp.path
end
+
+ super uri_or_fp, start_offset, usual, archived, id
end
- def file_path; URI(uri).path end
+ def file_path; @path end
+ 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
def self.suggest_labels_for path
## heuristic: use the filename as a label, unless the file
## has a path that probably represents an inbox.
if File.dirname(path) =~ /\b(var|usr|spool)\b/
@@ -60,11 +66,15 @@
def load_message offset
@mutex.synchronize do
@f.seek offset
begin
RMail::Mailbox::MBoxReader.new(@f).each_message do |input|
- return RMail::Parser.read(input)
+ m = RMail::Parser.read(input)
+ if m.body && m.body.is_a?(String)
+ m.body.gsub!(/^>From /, "From ")
+ end
+ return m
end
rescue RMail::Parser::Error => e
raise FatalSourceError, "error parsing mbox file: #{e.message}"
end
end
@@ -79,23 +89,23 @@
end
end
ret
end
- def raw_full_message offset
+ def raw_message offset
ret = ""
- each_raw_full_message_line(offset) { |l| ret += l }
+ each_raw_message_line(offset) { |l| ret += l }
ret
end
## apparently it's a million times faster to call this directly if
## we're just moving messages around on disk, than reading things
- ## into memory with raw_full_message.
+ ## into memory with raw_message.
##
## i hoped never to have to move shit around on disk but
## sup-sync-back has to do it.
- def each_raw_full_message_line offset
+ def each_raw_message_line offset
@mutex.synchronize do
@f.seek offset
yield @f.gets
until @f.eof? || (l = @f.gets) =~ BREAK_RE
yield l
@@ -135,10 +145,10 @@
rescue SystemCallError, IOError => e
raise FatalSourceError, "Error reading #{@f.path}: #{e.message}"
end
self.cur_offset = next_offset
- [returned_offset, @labels]
+ [returned_offset, (@labels + [:unread]).uniq]
end
end
end
end