14 ## this is for debugging purposes because i keep calling #id on the
15 ## wrong object and i want it to throw an exception
17 raise "wrong id called on #{self.inspect}"
22 def yaml_properties *props
23 props = props.map { |p| p.to_s }
24 vars = props.map { |p| "@#{p}" }
26 path = klass.name.gsub(/::/, "/")
28 klass.instance_eval do
29 define_method(:to_yaml_properties) { vars }
30 define_method(:to_yaml_type) { "!#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}/#{path}" }
33 YAML.add_domain_type("#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}", path) do |type, val|
34 klass.new(*props.map { |p| val[p] })
42 BASE_DIR = ENV["SUP_BASE"] || File.join(ENV["HOME"], ".sup")
43 CONFIG_FN = File.join(BASE_DIR, "config.yaml")
44 COLOR_FN = File.join(BASE_DIR, "colors.yaml")
45 SOURCE_FN = File.join(BASE_DIR, "sources.yaml")
46 LABEL_FN = File.join(BASE_DIR, "labels.txt")
47 CONTACT_FN = File.join(BASE_DIR, "contacts.txt")
48 DRAFT_DIR = File.join(BASE_DIR, "drafts")
49 SENT_FN = File.join(BASE_DIR, "sent.mbox")
50 LOCK_FN = File.join(BASE_DIR, "lock")
51 SUICIDE_FN = File.join(BASE_DIR, "please-kill-yourself")
52 HOOK_DIR = File.join(BASE_DIR, "hooks")
54 YAML_DOMAIN = "masanjin.net"
55 YAML_DATE = "2006-10-01"
57 DEFAULT_INDEX = 'ferret'
59 ## record exceptions thrown in threads nicely
61 @exception_mutex = Mutex.new
63 attr_reader :exceptions
64 def record_exception e, name
65 @exception_mutex.synchronize do
67 @exceptions << [e, name]
71 def reporting_thread name
79 record_exception e, name
85 module_function :reporting_thread, :record_exception, :exceptions
87 ## one-stop shop for yamliciousness
88 def save_yaml_obj o, fn, safe=false
90 o.map { |x| (x.respond_to?(:before_marshal) && x.before_marshal) || x }
92 o.respond_to?(:before_marshal) && o.before_marshal
96 safe_fn = "#{File.dirname fn}/safe_#{File.basename fn}"
97 mode = File.stat(fn).mode if File.exists? fn
98 File.open(safe_fn, "w", mode) { |f| f.puts o.to_yaml }
99 FileUtils.mv safe_fn, fn
101 File.open(fn, "w") { |f| f.puts o.to_yaml }
105 def load_yaml_obj fn, compress=false
106 o = if File.exists? fn
108 Zlib::GzipReader.open(fn) { |f| YAML::load f }
114 o.each { |x| x.after_unmarshal! if x.respond_to?(:after_unmarshal!) }
116 o.after_unmarshal! if o.respond_to?(:after_unmarshal!)
122 Redwood::SentManager.new $config[:sent_source] || 'sup://sent'
123 Redwood::ContactManager.new Redwood::CONTACT_FN
124 Redwood::LabelManager.new Redwood::LABEL_FN
125 Redwood::AccountManager.new $config[:accounts]
126 Redwood::DraftManager.new Redwood::DRAFT_DIR
127 Redwood::UpdateManager.new
128 Redwood::PollManager.new
129 Redwood::SuicideManager.new Redwood::SUICIDE_FN
130 Redwood::CryptoManager.new
131 Redwood::UndoManager.new
132 Redwood::SourceManager.new
136 Redwood::LabelManager.save if Redwood::LabelManager.instantiated?
137 Redwood::ContactManager.save if Redwood::ContactManager.instantiated?
138 Redwood::BufferManager.deinstantiate! if Redwood::BufferManager.instantiated?
141 ## not really a good place for this, so I'll just dump it here.
143 ## a source error is either a FatalSourceError or an OutOfSyncSourceError.
144 ## the superclass SourceError is just a generic.
145 def report_broken_sources opts={}
146 return unless BufferManager.instantiated?
148 broken_sources = SourceManager.sources.select { |s| s.error.is_a? FatalSourceError }
149 unless broken_sources.empty?
150 BufferManager.spawn_unless_exists("Broken source notification for #{broken_sources.join(',')}", opts) do
152 Source error notification
153 -------------------------
155 Hi there. It looks like one or more message sources is reporting
156 errors. Until this is corrected, messages from these sources cannot
157 be viewed, and new messages will not be detected.
159 #{broken_sources.map { |s| "Source: " + s.to_s + "\n Error: " + s.error.message.wrap(70).join("\n ")}.join("\n\n")}
165 desynced_sources = SourceManager.sources.select { |s| s.error.is_a? OutOfSyncSourceError }
166 unless desynced_sources.empty?
167 BufferManager.spawn_unless_exists("Out-of-sync source notification for #{broken_sources.join(',')}", opts) do
169 Out-of-sync source notification
170 -------------------------------
172 Hi there. It looks like one or more sources has fallen out of sync
173 with my index. This can happen when you modify these sources with
174 other email clients. (Sorry, I don't play well with others.)
176 Until this is corrected, messages from these sources cannot be viewed,
177 and new messages will not be detected. Luckily, this is easy to correct!
179 #{desynced_sources.map do |s|
180 "Source: " + s.to_s +
181 "\n Error: " + s.error.message.wrap(70).join("\n ") +
182 "\n Fix: sup-sync --changed #{s.to_s}"
190 module_function :save_yaml_obj, :load_yaml_obj, :start, :finish,
191 :report_broken_sources
194 ## set up default configuration file
195 if File.exists? Redwood::CONFIG_FN
196 $config = Redwood::load_yaml_obj Redwood::CONFIG_FN
200 name = Etc.getpwnam(ENV["USER"]).gecos.split(/,/).first rescue nil
202 email = ENV["USER"] + "@" +
204 Socket.gethostbyname(Socket.gethostname).first
215 :sendmail => "/usr/sbin/sendmail -oem -ti",
216 :signature => File.join(ENV["HOME"], ".signature")
219 :editor => ENV["EDITOR"] || "/usr/bin/vim -f -c 'setlocal spell spelllang=en_us' -c 'set filetype=mail'",
220 :thread_by_subject => false,
221 :edit_signature => false,
223 :ask_for_bcc => false,
224 :ask_for_subject => true,
225 :confirm_no_attachments => true,
226 :confirm_top_posting => true,
227 :discard_snippets_from_encrypted_messages => false,
228 :default_attachment_save_dir => "",
229 :sent_source => "sup://sent"
232 FileUtils.mkdir_p Redwood::BASE_DIR
233 Redwood::save_yaml_obj $config, Redwood::CONFIG_FN
234 rescue StandardError => e
235 $stderr.puts "warning: #{e.message}"
242 ## we have to initialize this guy first, because other classes must
243 ## reference it in order to register hooks, and they do that at parse
245 Redwood::HookManager.new Redwood::HOOK_DIR
247 ## everything we need to get logging working
251 require "sup/modes/scroll-mode"
252 require "sup/modes/text-mode"
253 require "sup/modes/log-mode"
256 def log s; Logger.log s; end
260 ## determine encoding and character set
261 $encoding = Locale.current.charset
263 Redwood::log "using character set encoding #{$encoding.inspect}"
265 Redwood::log "warning: can't find character set by using locale, defaulting to utf-8"
269 ## now everything else (which can feel free to call Redwood::log at load time)
271 require "sup/suicide"
272 require "sup/message-chunks"
273 require "sup/message"
276 require "sup/maildir"
279 require "sup/account"
282 require "sup/textfield"
283 require "sup/colormap"
285 require "sup/contact"
291 require "sup/horizontal-selector"
292 require "sup/modes/line-cursor-mode"
293 require "sup/modes/help-mode"
294 require "sup/modes/edit-message-mode"
295 require "sup/modes/compose-mode"
296 require "sup/modes/resume-mode"
297 require "sup/modes/forward-mode"
298 require "sup/modes/reply-mode"
299 require "sup/modes/label-list-mode"
300 require "sup/modes/contact-list-mode"
301 require "sup/modes/thread-view-mode"
302 require "sup/modes/thread-index-mode"
303 require "sup/modes/label-search-results-mode"
304 require "sup/modes/search-results-mode"
305 require "sup/modes/person-search-results-mode"
306 require "sup/modes/inbox-mode"
307 require "sup/modes/buffer-list-mode"
308 require "sup/modes/poll-mode"
309 require "sup/modes/file-browser-mode"
310 require "sup/modes/completion-mode"
314 d = File.join base, "sup/share/modes/"
315 Redwood::Mode.load_all_modes d if File.directory? d