9 ## the following magic enables wide characters when used with a ruby
10 ## ncurses.so that's been compiled against libncursesw. (note the w.) why
11 ## this works, i have no idea. much like pretty much every aspect of
12 ## dealing with curses. cargo cult programming at its best.
17 dlload Config::CONFIG['arch'] =~ /darwin/ ? "libc.dylib" : "libc.so.6"
18 extern "void setlocale(int, const char *)"
20 LibC.setlocale(6, "") # LC_ALL == 6
23 ## this is for debugging purposes because i keep calling #id on the
24 ## wrong object and i want it to throw an exception
26 raise "wrong id called on #{self.inspect}"
31 def yaml_properties *props
32 props = props.map { |p| p.to_s }
33 vars = props.map { |p| "@#{p}" }
35 path = klass.name.gsub(/::/, "/")
37 klass.instance_eval do
38 define_method(:to_yaml_properties) { vars }
39 define_method(:to_yaml_type) { "!#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}/#{path}" }
42 YAML.add_domain_type("#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}", path) do |type, val|
43 klass.new(*props.map { |p| val[p] })
51 BASE_DIR = ENV["SUP_BASE"] || File.join(ENV["HOME"], ".sup")
52 CONFIG_FN = File.join(BASE_DIR, "config.yaml")
53 COLOR_FN = File.join(BASE_DIR, "colors.yaml")
54 SOURCE_FN = File.join(BASE_DIR, "sources.yaml")
55 LABEL_FN = File.join(BASE_DIR, "labels.txt")
56 PERSON_FN = File.join(BASE_DIR, "people.txt")
57 CONTACT_FN = File.join(BASE_DIR, "contacts.txt")
58 DRAFT_DIR = File.join(BASE_DIR, "drafts")
59 SENT_FN = File.join(BASE_DIR, "sent.mbox")
60 LOCK_FN = File.join(BASE_DIR, "lock")
61 SUICIDE_FN = File.join(BASE_DIR, "please-kill-yourself")
62 HOOK_DIR = File.join(BASE_DIR, "hooks")
64 YAML_DOMAIN = "masanjin.net"
65 YAML_DATE = "2006-10-01"
67 ## record exceptions thrown in threads nicely
69 @exception_mutex = Mutex.new
71 attr_reader :exceptions
72 def record_exception e, name
73 @exception_mutex.synchronize do
75 @exceptions << [e, name]
79 def reporting_thread name
87 record_exception e, name
93 module_function :reporting_thread, :record_exception, :exceptions
95 ## one-stop shop for yamliciousness
96 def save_yaml_obj object, fn, safe=false
98 safe_fn = "#{File.dirname fn}/safe_#{File.basename fn}"
99 mode = File.stat(fn).mode if File.exists? fn
100 File.open(safe_fn, "w", mode) { |f| f.puts object.to_yaml }
101 FileUtils.mv safe_fn, fn
103 File.open(fn, "w") { |f| f.puts object.to_yaml }
107 def load_yaml_obj fn, compress=false
110 Zlib::GzipReader.open(fn) { |f| YAML::load f }
118 Redwood::PersonManager.new Redwood::PERSON_FN
119 Redwood::SentManager.new Redwood::SENT_FN
120 Redwood::ContactManager.new Redwood::CONTACT_FN
121 Redwood::LabelManager.new Redwood::LABEL_FN
122 Redwood::AccountManager.new $config[:accounts]
123 Redwood::DraftManager.new Redwood::DRAFT_DIR
124 Redwood::UpdateManager.new
125 Redwood::PollManager.new
126 Redwood::SuicideManager.new Redwood::SUICIDE_FN
127 Redwood::CryptoManager.new
131 Redwood::LabelManager.save if Redwood::LabelManager.instantiated?
132 Redwood::ContactManager.save if Redwood::ContactManager.instantiated?
133 Redwood::PersonManager.save if Redwood::PersonManager.instantiated?
134 Redwood::BufferManager.deinstantiate! if Redwood::BufferManager.instantiated?
137 ## not really a good place for this, so I'll just dump it here.
139 ## a source error is either a FatalSourceError or an OutOfSyncSourceError.
140 ## the superclass SourceError is just a generic.
141 def report_broken_sources opts={}
142 return unless BufferManager.instantiated?
144 broken_sources = Index.sources.select { |s| s.error.is_a? FatalSourceError }
145 unless broken_sources.empty?
146 BufferManager.spawn_unless_exists("Broken source notification for #{broken_sources.join(',')}", opts) do
148 Source error notification
149 -------------------------
151 Hi there. It looks like one or more message sources is reporting
152 errors. Until this is corrected, messages from these sources cannot
153 be viewed, and new messages will not be detected.
155 #{broken_sources.map { |s| "Source: " + s.to_s + "\n Error: " + s.error.message.wrap(70).join("\n ")}.join("\n\n")}
161 desynced_sources = Index.sources.select { |s| s.error.is_a? OutOfSyncSourceError }
162 unless desynced_sources.empty?
163 BufferManager.spawn_unless_exists("Out-of-sync source notification for #{broken_sources.join(',')}", opts) do
165 Out-of-sync source notification
166 -------------------------------
168 Hi there. It looks like one or more sources has fallen out of sync
169 with my index. This can happen when you modify these sources with
170 other email clients. (Sorry, I don't play well with others.)
172 Until this is corrected, messages from these sources cannot be viewed,
173 and new messages will not be detected. Luckily, this is easy to correct!
175 #{desynced_sources.map do |s|
176 "Source: " + s.to_s +
177 "\n Error: " + s.error.message.wrap(70).join("\n ") +
178 "\n Fix: sup-sync --changed #{s.to_s}"
186 module_function :save_yaml_obj, :load_yaml_obj, :start, :finish,
187 :report_broken_sources
190 ## set up default configuration file
191 if File.exists? Redwood::CONFIG_FN
192 $config = Redwood::load_yaml_obj Redwood::CONFIG_FN
196 name = Etc.getpwnam(ENV["USER"]).gecos.split(/,/).first rescue nil
198 email = ENV["USER"] + "@" +
200 Socket.gethostbyname(Socket.gethostname).first
211 :sendmail => "/usr/sbin/sendmail -oem -ti",
212 :signature => File.join(ENV["HOME"], ".signature")
215 :editor => ENV["EDITOR"] || "/usr/bin/vim -f -c 'setlocal spell spelllang=en_us' -c 'set filetype=mail'",
216 :thread_by_subject => false,
217 :edit_signature => false,
219 :ask_for_bcc => false,
220 :ask_for_subject => true,
221 :confirm_no_attachments => true,
222 :confirm_top_posting => true,
223 :discard_snippets_from_encrypted_messages => false,
226 FileUtils.mkdir_p Redwood::BASE_DIR
227 Redwood::save_yaml_obj $config, Redwood::CONFIG_FN
228 rescue StandardError => e
229 $stderr.puts "warning: #{e.message}"
236 ## we have to initialize this guy first, because other classes must
237 ## reference it in order to register hooks, and they do that at parse
239 Redwood::HookManager.new Redwood::HOOK_DIR
241 ## everything we need to get logging working
245 require "sup/modes/scroll-mode"
246 require "sup/modes/text-mode"
247 require "sup/modes/log-mode"
250 def log s; Logger.log s; end
254 ## determine encoding and character set
255 $encoding = Locale.current.charset
257 Redwood::log "using character set encoding #{$encoding.inspect}"
259 Redwood::log "warning: can't find character set by using locale, defaulting to utf-8"
263 ## now everything else (which can feel free to call Redwood::log at load time)
265 require "sup/suicide"
266 require "sup/message-chunks"
267 require "sup/message"
270 require "sup/maildir"
273 require "sup/account"
276 require "sup/textfield"
277 require "sup/colormap"
279 require "sup/contact"
284 require "sup/horizontal-selector"
285 require "sup/modes/line-cursor-mode"
286 require "sup/modes/help-mode"
287 require "sup/modes/edit-message-mode"
288 require "sup/modes/compose-mode"
289 require "sup/modes/resume-mode"
290 require "sup/modes/forward-mode"
291 require "sup/modes/reply-mode"
292 require "sup/modes/label-list-mode"
293 require "sup/modes/contact-list-mode"
294 require "sup/modes/thread-view-mode"
295 require "sup/modes/thread-index-mode"
296 require "sup/modes/label-search-results-mode"
297 require "sup/modes/search-results-mode"
298 require "sup/modes/person-search-results-mode"
299 require "sup/modes/inbox-mode"
300 require "sup/modes/buffer-list-mode"
301 require "sup/modes/poll-mode"
302 require "sup/modes/file-browser-mode"
303 require "sup/modes/completion-mode"
307 d = File.join base, "sup/share/modes/"
308 Redwood::Mode.load_all_modes d if File.directory? d