]> git.cworth.org Git - sup/commitdiff
SentManager: rework handling to allow for user specified source
authorBen Walton <bwalton@artsci.utoronto.ca>
Thu, 7 May 2009 02:44:24 +0000 (22:44 -0400)
committerBen Walton <bwalton@artsci.utoronto.ca>
Thu, 28 May 2009 15:27:21 +0000 (11:27 -0400)
* The handling of SentManager now allows for a parameter in config.yaml
  called sent_source.  This parameter should be the URI of the source
  that the users wishes to store outbound mail in.

* The default is sup://sent

* A FatalSourceError is raised in the event that sent_source is
  set to a source type that doesn't support the store_message() method.

* SentManager and SentLoader have been somewhat decoupled.

Signed-off-by: Ben Walton <bwalton@artsci.utoronto.ca>
bin/sup
lib/sup.rb
lib/sup/sent.rb

diff --git a/bin/sup b/bin/sup
index 0af3d11ecfea671bc299342f6ca410d6cb83bf12..9abe1b54c395630857445d4268baacfcd1c5e930 100755 (executable)
--- a/bin/sup
+++ b/bin/sup
@@ -168,11 +168,10 @@ begin
     Index.add_source DraftManager.new_source
   end
 
-  if(s = Index.source_for SentManager.source_name)
+  if(s = Index.source_for SentManager.source_uri)
     SentManager.source = s
   else
-    Redwood::log "no sent mail source, auto-adding..."
-    Index.add_source SentManager.new_source
+    Index.add_source SentManager.default_source
   end
 
   HookManager.run "startup"
index 96510b2e2d7419629a0a3fd3e9d96eafca29c36e..4f59eaaf13caf6f1c5187af73bc7f4b26201a24b 100644 (file)
@@ -101,7 +101,7 @@ module Redwood
   end
 
   def start
-    Redwood::SentManager.new Redwood::SENT_FN
+    Redwood::SentManager.new $config[:sent_source] || 'sup://sent'
     Redwood::ContactManager.new Redwood::CONTACT_FN
     Redwood::LabelManager.new Redwood::LABEL_FN
     Redwood::AccountManager.new $config[:accounts]
@@ -207,6 +207,7 @@ else
     :confirm_top_posting => true,
     :discard_snippets_from_encrypted_messages => false,
     :default_attachment_save_dir => "",
+    :sent_source => "sup://sent"
   }
   begin
     FileUtils.mkdir_p Redwood::BASE_DIR
index ee843c7dc54837acbcc39daab851509037692bb0..bad20837a825d0109fbf3f363022a3b8f44fd323 100644 (file)
@@ -3,24 +3,33 @@ module Redwood
 class SentManager
   include Singleton
 
-  attr_accessor :source
-  def initialize fn
-    @fn = fn
+  attr_reader :source
+  attr_reader :source_uri
+
+  def initialize source_uri
     @source = nil
+    @source_uri = source_uri
     self.class.i_am_the_instance self
+    Redwood::log "SentManager intialized with source uri: #@source_uri"
   end
 
-  def self.source_name; "sup://sent"; end
-  def self.source_id; 9998; end
-  def new_source; @source = Recoverable.new SentLoader.new; end
+  def source_id; @source.id; end
 
-  def write_sent_message date, from_email
-    need_blank = File.exists?(@fn) && !File.zero?(@fn)
-    File.open(@fn, "a") do |f|
-      f.puts if need_blank
-      f.puts "From #{from_email} #{date}"
-      yield f
-    end
+  def source= s
+    raise FatalSourceError.new("Configured sent_source [#{s.uri}] can't store mail.  Correct your configuration.") unless s.respond_to? :store_message
+    @souce_uri = s.uri
+    @source = s
+  end
+
+  def default_source
+    @source = Recoverable.new SentLoader.new
+    Redwood::log "SentManager initializing default source: #@source."
+    @source_uri = @source.uri
+    @source
+  end
+
+  def write_sent_message date, from_email, &block
+    @source.store_message date, from_email, &block
 
     PollManager.add_messages_from(@source) do |m, o, e|
       m.remove_label :unread
@@ -40,9 +49,10 @@ class SentLoader < MBox::Loader
 
   def file_path; @filename end
 
-  def uri; SentManager.source_name; end
-  def to_s; SentManager.source_name; end
-  def id; SentManager.source_id; end
+  def to_s; 'sup://sent'; end
+  def uri; 'sup://sent' end
+
+  def id; 9998; end
   def labels; [:sent, :inbox]; end
 end