X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=lib%2Fsup%2Fmbox%2Floader.rb;h=a11bf9541e6efed049754cf85442c6cdbd714c8b;hb=4efc8adaf8c63ff054c71149de6c12fe8194ff03;hp=ebb2aed675e171397b99e877023e24611cf119c7;hpb=7ab166131aa760740dac97333f04a79966cb5fc2;p=sup diff --git a/lib/sup/mbox/loader.rb b/lib/sup/mbox/loader.rb index ebb2aed..a11bf95 100644 --- a/lib/sup/mbox/loader.rb +++ b/lib/sup/mbox/loader.rb @@ -1,17 +1,20 @@ require 'rmail' require 'uri' +require 'set' module Redwood module MBox class Loader < Source + include SerializeLabelsNicely yaml_properties :uri, :cur_offset, :usual, :archived, :id, :labels - attr_accessor :labels + + attr_reader :labels ## uri_or_fp is horrific. need to refactor. - def initialize uri_or_fp, start_offset=0, usual=true, archived=false, id=nil, labels=[] + def initialize uri_or_fp, start_offset=0, usual=true, archived=false, id=nil, labels=nil @mutex = Mutex.new - @labels = ((labels || []) - LabelManager::RESERVED_LABELS).uniq.freeze + @labels = Set.new((labels || []) - LabelManager::RESERVED_LABELS) case uri_or_fp when String @@ -47,7 +50,7 @@ class Loader < Source raise OutOfSyncSourceError, "mbox file is smaller than last recorded message offset. Messages have probably been deleted by another client." end end - + def start_offset; 0; end def end_offset; File.size @f; end @@ -85,7 +88,7 @@ class Loader < Source @mutex.synchronize do @f.seek cur_offset string = "" - until @f.eof? || (l = @f.gets) =~ BREAK_RE + until @f.eof? || MBox::is_break_line?(l = @f.gets) string << l end self.cur_offset += string.length @@ -109,6 +112,15 @@ class Loader < Source ret end + def store_message date, from_email, &block + need_blank = File.exists?(@filename) && !File.zero?(@filename) + File.open(@filename, "a") do |f| + f.puts if need_blank + f.puts "From #{from_email} #{date.utc}" + yield f + end + 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_message. @@ -159,7 +171,7 @@ class Loader < Source end self.cur_offset = next_offset - [returned_offset, (self.labels + [:unread]).uniq] + [returned_offset, (labels + [:unread])] end end