lib/sup.rb in sup-0.11 vs lib/sup.rb in sup-0.12

- old
+ new

@@ -3,10 +3,11 @@ require 'zlib' require 'thread' require 'fileutils' require 'gettext' require 'curses' +require 'rmail' begin require 'fastthread' rescue LoadError end @@ -35,11 +36,11 @@ end end end module Redwood - VERSION = "0.11" + VERSION = "0.12" BASE_DIR = ENV["SUP_BASE"] || File.join(ENV["HOME"], ".sup") CONFIG_FN = File.join(BASE_DIR, "config.yaml") COLOR_FN = File.join(BASE_DIR, "colors.yaml") SOURCE_FN = File.join(BASE_DIR, "sources.yaml") @@ -49,10 +50,11 @@ SENT_FN = File.join(BASE_DIR, "sent.mbox") LOCK_FN = File.join(BASE_DIR, "lock") SUICIDE_FN = File.join(BASE_DIR, "please-kill-yourself") HOOK_DIR = File.join(BASE_DIR, "hooks") SEARCH_FN = File.join(BASE_DIR, "searches.txt") + LOG_FN = File.join(BASE_DIR, "log") YAML_DOMAIN = "masanjin.net" YAML_DATE = "2006-10-01" ## record exceptions thrown in threads nicely @@ -117,30 +119,45 @@ o.after_unmarshal! if o.respond_to?(:after_unmarshal!) end o end + def managers + %w(HookManager SentManager ContactManager LabelManager AccountManager + DraftManager UpdateManager PollManager CryptoManager UndoManager + SourceManager SearchManager IdleManager).map { |x| Redwood.const_get x.to_sym } + end + def start + managers.each { |x| fail "#{x} already instantiated" if x.instantiated? } + + FileUtils.mkdir_p Redwood::BASE_DIR + $config = load_config Redwood::CONFIG_FN + @log_io = File.open(Redwood::LOG_FN, 'a') + Redwood::Logger.add_sink @log_io + Redwood::HookManager.init Redwood::HOOK_DIR Redwood::SentManager.init $config[:sent_source] || 'sup://sent' Redwood::ContactManager.init Redwood::CONTACT_FN Redwood::LabelManager.init Redwood::LABEL_FN Redwood::AccountManager.init $config[:accounts] Redwood::DraftManager.init Redwood::DRAFT_DIR - Redwood::UpdateManager.init - Redwood::PollManager.init - Redwood::CryptoManager.init - Redwood::UndoManager.init - Redwood::SourceManager.init Redwood::SearchManager.init Redwood::SEARCH_FN - Redwood::IdleManager.init + + managers.each { |x| x.init unless x.instantiated? } end def finish Redwood::LabelManager.save if Redwood::LabelManager.instantiated? Redwood::ContactManager.save if Redwood::ContactManager.instantiated? - Redwood::BufferManager.deinstantiate! if Redwood::BufferManager.instantiated? Redwood::SearchManager.save if Redwood::SearchManager.instantiated? + Redwood::Logger.remove_sink @log_io + + managers.each { |x| x.deinstantiate! if x.instantiated? } + + @log_io.close + @log_io = nil + $config = nil end ## not really a good place for this, so I'll just dump it here. ## ## a source error is either a FatalSourceError or an OutOfSyncSourceError. @@ -218,85 +235,88 @@ EOS abort end end - module_function :save_yaml_obj, :load_yaml_obj, :start, :finish, - :report_broken_sources, :check_library_version_against -end + ## set up default configuration file + def load_config filename + if File.exists? filename + config = Redwood::load_yaml_obj filename + abort "#{filename} is not a valid configuration file (it's a #{config.class}, not a hash)" unless config.is_a?(Hash) + config + else + require 'etc' + require 'socket' + name = Etc.getpwnam(ENV["USER"]).gecos.split(/,/).first rescue nil + name ||= ENV["USER"] + email = ENV["USER"] + "@" + + begin + Socket.gethostbyname(Socket.gethostname).first + rescue SocketError + Socket.gethostname + end -## set up default configuration file -if File.exists? Redwood::CONFIG_FN - $config = Redwood::load_yaml_obj Redwood::CONFIG_FN - abort "#{Redwood::CONFIG_FN} is not a valid configuration file (it's a #{$config.class}, not a hash)" unless $config.is_a?(Hash) -else - require 'etc' - require 'socket' - name = Etc.getpwnam(ENV["USER"]).gecos.split(/,/).first rescue nil - name ||= ENV["USER"] - email = ENV["USER"] + "@" + - begin - Socket.gethostbyname(Socket.gethostname).first - rescue SocketError - Socket.gethostname - end - - $config = { - :accounts => { - :default => { - :name => name, - :email => email, - :alternates => [], - :sendmail => "/usr/sbin/sendmail -oem -ti", - :signature => File.join(ENV["HOME"], ".signature") + config = { + :accounts => { + :default => { + :name => name, + :email => email, + :alternates => [], + :sendmail => "/usr/sbin/sendmail -oem -ti", + :signature => File.join(ENV["HOME"], ".signature"), + :gpgkey => "" + } + }, + :editor => ENV["EDITOR"] || "/usr/bin/vim -f -c 'setlocal spell spelllang=en_us' -c 'set filetype=mail'", + :thread_by_subject => false, + :edit_signature => false, + :ask_for_from => false, + :ask_for_to => true, + :ask_for_cc => true, + :ask_for_bcc => false, + :ask_for_subject => true, + :confirm_no_attachments => true, + :confirm_top_posting => true, + :jump_to_open_message => true, + :discard_snippets_from_encrypted_messages => false, + :default_attachment_save_dir => "", + :sent_source => "sup://sent", + :poll_interval => 300, + :wrap_width => 0, + :slip_rows => 0 } - }, - :editor => ENV["EDITOR"] || "/usr/bin/vim -f -c 'setlocal spell spelllang=en_us' -c 'set filetype=mail'", - :thread_by_subject => false, - :edit_signature => false, - :ask_for_to => true, - :ask_for_cc => true, - :ask_for_bcc => false, - :ask_for_subject => true, - :confirm_no_attachments => true, - :confirm_top_posting => true, - :jump_to_open_message => true, - :discard_snippets_from_encrypted_messages => false, - :default_attachment_save_dir => "", - :sent_source => "sup://sent", - :poll_interval => 300, - :wrap_width => 0 - } - begin - FileUtils.mkdir_p Redwood::BASE_DIR - Redwood::save_yaml_obj $config, Redwood::CONFIG_FN - rescue StandardError => e - $stderr.puts "warning: #{e.message}" + begin + Redwood::save_yaml_obj config, filename + rescue StandardError => e + $stderr.puts "warning: #{e.message}" + end + config + end end + + module_function :save_yaml_obj, :load_yaml_obj, :start, :finish, + :report_broken_sources, :check_library_version_against, + :load_config, :managers end require "sup/util" require "sup/hook" -## we have to initialize this guy first, because other classes must -## reference it in order to register hooks, and they do that at parse -## time. -Redwood::HookManager.init Redwood::HOOK_DIR - ## everything we need to get logging working require "sup/logger" Redwood::Logger.init.add_sink $stderr include Redwood::LogsStuff ## determine encoding and character set - $encoding = Locale.current.charset - if $encoding - debug "using character set encoding #{$encoding.inspect}" - else - warn "can't find character set by using locale, defaulting to utf-8" - $encoding = "UTF-8" - end +$encoding = Locale.current.charset +$encoding = "UTF-8" if $encoding == "utf8" +if $encoding + debug "using character set encoding #{$encoding.inspect}" +else + warn "can't find character set by using locale, defaulting to utf-8" + $encoding = "UTF-8" +end require "sup/buffer" require "sup/keymap" require "sup/mode" require "sup/modes/scroll-mode" @@ -306,11 +326,10 @@ require "sup/message-chunks" require "sup/message" require "sup/source" require "sup/mbox" require "sup/maildir" -require "sup/imap" require "sup/person" require "sup/account" require "sup/thread" require "sup/interactive-lock" require "sup/index" @@ -346,10 +365,9 @@ require "sup/modes/console-mode" require "sup/sent" require "sup/search" require "sup/modes/search-list-mode" require "sup/idle" -require "sup/connection" $:.each do |base| d = File.join base, "sup/share/modes/" Redwood::Mode.load_all_modes d if File.directory? d end