8 ## time for some monkeypatching!
12 'host' => "#{ Socket.gethostname }",
13 'pid' => "#{ Process.pid }",
14 'ppid' => "#{ Process.ppid }",
20 def dump_lock_id lock_id = @lock_id
21 "host: %s\npid: %s\nppid: %s\ntime: %s\nuser: %s\n" %
22 lock_id.values_at('host','pid','ppid','time','user')
26 load_lock_id IO.read(path)
31 ## this is for debugging purposes because i keep calling #id on the
32 ## wrong object and i want it to throw an exception
34 raise "wrong id called on #{self.inspect}"
38 class LockError < StandardError
44 def method_missing m; @h[m.to_s] end
48 def yaml_properties *props
49 props = props.map { |p| p.to_s }
50 vars = props.map { |p| "@#{p}" }
52 path = klass.name.gsub(/::/, "/")
54 klass.instance_eval do
55 define_method(:to_yaml_properties) { vars }
56 define_method(:to_yaml_type) { "!#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}/#{path}" }
59 YAML.add_domain_type("#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}", path) do |type, val|
60 klass.new(*props.map { |p| val[p] })
68 BASE_DIR = ENV["SUP_BASE"] || File.join(ENV["HOME"], ".sup")
69 CONFIG_FN = File.join(BASE_DIR, "config.yaml")
70 SOURCE_FN = File.join(BASE_DIR, "sources.yaml")
71 LABEL_FN = File.join(BASE_DIR, "labels.txt")
72 PERSON_FN = File.join(BASE_DIR, "people.txt")
73 CONTACT_FN = File.join(BASE_DIR, "contacts.txt")
74 DRAFT_DIR = File.join(BASE_DIR, "drafts")
75 SENT_FN = File.join(BASE_DIR, "sent.mbox")
76 LOCK_FN = File.join(BASE_DIR, "lock")
77 SUICIDE_FN = File.join(BASE_DIR, "please-kill-yourself")
79 YAML_DOMAIN = "masanjin.net"
80 YAML_DATE = "2006-10-01"
82 ## record exceptions thrown in threads nicely
92 File.open("sup-exception-log.txt", "w") do |f|
93 f.puts "--- #{e.class.name} at #{Time.now}"
94 f.puts e.message, e.backtrace
95 end unless e.is_a? SuicideException
102 module_function :reporting_thread
104 ## one-stop shop for yamliciousness
105 def save_yaml_obj object, fn, compress=false
107 Zlib::GzipWriter.open(fn) { |f| f.puts object.to_yaml }
109 File.open(fn, "w") { |f| f.puts object.to_yaml }
113 def load_yaml_obj fn, compress=false
116 Zlib::GzipReader.open(fn) { |f| YAML::load f }
124 Redwood::PersonManager.new Redwood::PERSON_FN
125 Redwood::SentManager.new Redwood::SENT_FN
126 Redwood::ContactManager.new Redwood::CONTACT_FN
127 Redwood::LabelManager.new Redwood::LABEL_FN
128 Redwood::AccountManager.new $config[:accounts]
129 Redwood::DraftManager.new Redwood::DRAFT_DIR
130 Redwood::UpdateManager.new
131 Redwood::PollManager.new
132 Redwood::SuicideManager.new Redwood::SUICIDE_FN
136 Redwood::LabelManager.save
137 Redwood::ContactManager.save
138 Redwood::PersonManager.save
139 Redwood::BufferManager.deinstantiate!
143 FileUtils.rm_f SUICIDE_FN
145 Redwood::log "locking #{LOCK_FN}..."
146 $lock = Lockfile.new LOCK_FN, :retries => 0
149 rescue Lockfile::MaxTriesLockError
150 raise LockError, $lock.lockinfo_on_disk
155 Redwood::log "unlocking #{LOCK_FN}..."
156 $lock.unlock if $lock
159 ## not really a good place for this, so I'll just dump it here.
160 def report_broken_sources opts={}
161 return unless BufferManager.instantiated?
163 broken_sources = Index.usual_sources.select { |s| s.error.is_a? FatalSourceError }
164 unless broken_sources.empty?
165 BufferManager.spawn "Broken source notification", TextMode.new(<<EOM), opts
166 Source error notification
167 -------------------------
169 Hi there. It looks like one or more message sources is reporting
170 errors. Until this is corrected, messages from these sources cannot
171 be viewed, and new messages will not be detected.
173 #{broken_sources.map { |s| "Source: " + s.to_s + "\n Error: " + s.error.message.wrap(70).join("\n ")}.join("\n\n")}
178 desynced_sources = Index.usual_sources.select { |s| s.error.is_a? OutOfSyncSourceError }
179 unless desynced_sources.empty?
180 BufferManager.spawn "Out-of-sync source notification", TextMode.new(<<EOM), opts
181 Out-of-sync source notification
182 -------------------------------
184 Hi there. It looks like one or more sources has fallen out of sync
185 with my index. This can happen when you modify these sources with
186 other email clients. (Sorry, I don't play well with others.)
188 Until this is corrected, messages from these sources cannot be viewed,
189 and new messages will not be detected. Luckily, this is easy to correct!
191 #{desynced_sources.map do |s|
192 "Source: " + s.to_s +
193 "\n Error: " + s.error.message.wrap(70).join("\n ") +
194 "\n Fix: sup-sync --changed #{s.to_s}"
201 module_function :save_yaml_obj, :load_yaml_obj, :start, :finish,
202 :lock, :unlock, :report_broken_sources
205 ## set up default configuration file
206 if File.exists? Redwood::CONFIG_FN
207 $config = Redwood::load_yaml_obj Redwood::CONFIG_FN
211 name = Etc.getpwnam(ENV["USER"]).gecos.split(/,/).first
212 email = ENV["USER"] + "@" +
214 Socket.gethostbyname(Socket.gethostname).first
225 :sendmail => "/usr/sbin/sendmail -oem -ti",
226 :signature => File.join(ENV["HOME"], ".signature")
229 :editor => ENV["EDITOR"] || "/usr/bin/vim -f -c 'setlocal spell spelllang=en_us' -c 'set filetype=mail'",
230 :thread_by_subject => false,
233 FileUtils.mkdir_p Redwood::BASE_DIR
234 Redwood::save_yaml_obj $config, Redwood::CONFIG_FN
235 rescue StandardError => e
236 $stderr.puts "warning: #{e.message}"
242 require "sup/suicide"
243 require "sup/message"
246 require "sup/maildir"
249 require "sup/account"
252 require "sup/textfield"
256 require "sup/colormap"
258 require "sup/contact"
262 require "sup/modes/scroll-mode"
263 require "sup/modes/text-mode"
264 require "sup/modes/line-cursor-mode"
265 require "sup/modes/help-mode"
266 require "sup/modes/edit-message-mode"
267 require "sup/modes/compose-mode"
268 require "sup/modes/resume-mode"
269 require "sup/modes/forward-mode"
270 require "sup/modes/reply-mode"
271 require "sup/modes/label-list-mode"
272 require "sup/modes/contact-list-mode"
273 require "sup/modes/thread-view-mode"
274 require "sup/modes/thread-index-mode"
275 require "sup/modes/label-search-results-mode"
276 require "sup/modes/search-results-mode"
277 require "sup/modes/person-search-results-mode"
278 require "sup/modes/inbox-mode"
279 require "sup/modes/buffer-list-mode"
280 require "sup/modes/log-mode"
281 require "sup/modes/poll-mode"
286 def log s; Logger.log s; end
291 d = File.join base, "sup/share/modes/"
292 Redwood::Mode.load_all_modes d if File.directory? d