10 ## this is for debugging purposes because i keep calling #id on the
11 ## wrong object and i want it to throw an exception
13 raise "wrong id called on #{self.inspect}"
18 def yaml_properties *props
19 props = props.map { |p| p.to_s }
20 vars = props.map { |p| "@#{p}" }
22 path = klass.name.gsub(/::/, "/")
24 klass.instance_eval do
25 define_method(:to_yaml_properties) { vars }
26 define_method(:to_yaml_type) { "!#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}/#{path}" }
29 YAML.add_domain_type("#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}", path) do |type, val|
30 klass.new(*props.map { |p| val[p] })
38 BASE_DIR = ENV["SUP_BASE"] || File.join(ENV["HOME"], ".sup")
39 CONFIG_FN = File.join(BASE_DIR, "config.yaml")
40 SOURCE_FN = File.join(BASE_DIR, "sources.yaml")
41 LABEL_FN = File.join(BASE_DIR, "labels.txt")
42 PERSON_FN = File.join(BASE_DIR, "people.txt")
43 CONTACT_FN = File.join(BASE_DIR, "contacts.txt")
44 DRAFT_DIR = File.join(BASE_DIR, "drafts")
45 SENT_FN = File.join(BASE_DIR, "sent.mbox")
46 LOCK_FN = File.join(BASE_DIR, "lock")
47 SUICIDE_FN = File.join(BASE_DIR, "please-kill-yourself")
48 HOOK_DIR = File.join(BASE_DIR, "hooks")
50 YAML_DOMAIN = "masanjin.net"
51 YAML_DATE = "2006-10-01"
53 ## record exceptions thrown in threads nicely
54 def reporting_thread name
63 $exceptions << [e, name]
69 module_function :reporting_thread
71 ## one-stop shop for yamliciousness
72 def save_yaml_obj object, fn, safe=false
74 safe_fn = "#{File.dirname fn}/safe_#{File.basename fn}"
75 mode = File.stat(fn).mode if File.exists? fn
76 File.open(safe_fn, "w", mode) { |f| f.puts object.to_yaml }
77 FileUtils.mv safe_fn, fn
79 File.open(fn, "w") { |f| f.puts object.to_yaml }
83 def load_yaml_obj fn, compress=false
86 Zlib::GzipReader.open(fn) { |f| YAML::load f }
94 Redwood::PersonManager.new Redwood::PERSON_FN
95 Redwood::SentManager.new Redwood::SENT_FN
96 Redwood::ContactManager.new Redwood::CONTACT_FN
97 Redwood::LabelManager.new Redwood::LABEL_FN
98 Redwood::AccountManager.new $config[:accounts]
99 Redwood::DraftManager.new Redwood::DRAFT_DIR
100 Redwood::UpdateManager.new
101 Redwood::PollManager.new
102 Redwood::SuicideManager.new Redwood::SUICIDE_FN
103 Redwood::CryptoManager.new
107 Redwood::LabelManager.save if Redwood::LabelManager.instantiated?
108 Redwood::ContactManager.save if Redwood::ContactManager.instantiated?
109 Redwood::PersonManager.save if Redwood::PersonManager.instantiated?
110 Redwood::BufferManager.deinstantiate! if Redwood::BufferManager.instantiated?
113 ## not really a good place for this, so I'll just dump it here.
115 ## a source error is either a FatalSourceError or an OutOfSyncSourceError.
116 ## the superclass SourceError is just a generic.
117 def report_broken_sources opts={}
118 return unless BufferManager.instantiated?
120 broken_sources = Index.sources.select { |s| s.error.is_a? FatalSourceError }
121 unless broken_sources.empty?
122 BufferManager.spawn_unless_exists("Broken source notification for #{broken_sources.join(',')}", opts) do
124 Source error notification
125 -------------------------
127 Hi there. It looks like one or more message sources is reporting
128 errors. Until this is corrected, messages from these sources cannot
129 be viewed, and new messages will not be detected.
131 #{broken_sources.map { |s| "Source: " + s.to_s + "\n Error: " + s.error.message.wrap(70).join("\n ")}.join("\n\n")}
137 desynced_sources = Index.sources.select { |s| s.error.is_a? OutOfSyncSourceError }
138 unless desynced_sources.empty?
139 BufferManager.spawn_unless_exists("Out-of-sync source notification for #{broken_sources.join(',')}", opts) do
141 Out-of-sync source notification
142 -------------------------------
144 Hi there. It looks like one or more sources has fallen out of sync
145 with my index. This can happen when you modify these sources with
146 other email clients. (Sorry, I don't play well with others.)
148 Until this is corrected, messages from these sources cannot be viewed,
149 and new messages will not be detected. Luckily, this is easy to correct!
151 #{desynced_sources.map do |s|
152 "Source: " + s.to_s +
153 "\n Error: " + s.error.message.wrap(70).join("\n ") +
154 "\n Fix: sup-sync --changed #{s.to_s}"
162 module_function :save_yaml_obj, :load_yaml_obj, :start, :finish,
163 :report_broken_sources
166 ## set up default configuration file
167 if File.exists? Redwood::CONFIG_FN
168 $config = Redwood::load_yaml_obj Redwood::CONFIG_FN
172 name = Etc.getpwnam(ENV["USER"]).gecos.split(/,/).first rescue nil
174 email = ENV["USER"] + "@" +
176 Socket.gethostbyname(Socket.gethostname).first
187 :sendmail => "/usr/sbin/sendmail -oem -ti",
188 :signature => File.join(ENV["HOME"], ".signature")
191 :editor => ENV["EDITOR"] || "/usr/bin/vim -f -c 'setlocal spell spelllang=en_us' -c 'set filetype=mail'",
192 :thread_by_subject => false,
193 :edit_signature => false,
195 :ask_for_bcc => false,
196 :ask_for_subject => true,
197 :confirm_no_attachments => true,
198 :confirm_top_posting => true,
199 :discard_snippets_from_encrypted_messages => false,
202 FileUtils.mkdir_p Redwood::BASE_DIR
203 Redwood::save_yaml_obj $config, Redwood::CONFIG_FN
204 rescue StandardError => e
205 $stderr.puts "warning: #{e.message}"
212 ## we have to initialize this guy first, because other classes must
213 ## reference it in order to register hooks, and they do that at parse
215 Redwood::HookManager.new Redwood::HOOK_DIR
217 ## everything we need to get logging working
221 require "sup/modes/scroll-mode"
222 require "sup/modes/text-mode"
223 require "sup/modes/log-mode"
226 def log s; Logger.log s; end
230 ## determine encoding and character set
231 $encoding = Locale.current.charset
233 Redwood::log "using character set encoding #{$encoding.inspect}"
235 Redwood::log "warning: can't find character set by using locale, defaulting to utf-8"
239 ## now everything else (which can feel free to call Redwood::log at load time)
241 require "sup/suicide"
242 require "sup/message-chunks"
243 require "sup/message"
246 require "sup/maildir"
249 require "sup/account"
252 require "sup/textfield"
253 require "sup/colormap"
255 require "sup/contact"
260 require "sup/horizontal-selector"
261 require "sup/modes/line-cursor-mode"
262 require "sup/modes/help-mode"
263 require "sup/modes/edit-message-mode"
264 require "sup/modes/compose-mode"
265 require "sup/modes/resume-mode"
266 require "sup/modes/forward-mode"
267 require "sup/modes/reply-mode"
268 require "sup/modes/label-list-mode"
269 require "sup/modes/contact-list-mode"
270 require "sup/modes/thread-view-mode"
271 require "sup/modes/thread-index-mode"
272 require "sup/modes/label-search-results-mode"
273 require "sup/modes/search-results-mode"
274 require "sup/modes/person-search-results-mode"
275 require "sup/modes/inbox-mode"
276 require "sup/modes/buffer-list-mode"
277 require "sup/modes/poll-mode"
278 require "sup/modes/file-browser-mode"
279 require "sup/modes/completion-mode"
283 d = File.join base, "sup/share/modes/"
284 Redwood::Mode.load_all_modes d if File.directory? d