]> git.cworth.org Git - sup/blob - lib/sup.rb
better source error reporting, and better message loading
[sup] / lib / sup.rb
1 require 'rubygems'
2 require 'yaml'
3 require 'zlib'
4 require 'thread'
5 require 'fileutils'
6 require 'curses'
7
8 class Object
9   ## this is for debugging purposes because i keep calling #id on the
10   ## wrong object and i want it to throw an exception
11   def id
12     raise "wrong id called on #{self.inspect}"
13   end
14 end
15
16 class Module
17   def yaml_properties *props
18     props = props.map { |p| p.to_s }
19     vars = props.map { |p| "@#{p}" }
20     klass = self
21     path = klass.name.gsub(/::/, "/")
22     
23     klass.instance_eval do
24       define_method(:to_yaml_properties) { vars }
25       define_method(:to_yaml_type) { "!#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}/#{path}" }
26     end
27
28     YAML.add_domain_type("#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}", path) do |type, val|
29       klass.new(*props.map { |p| val[p] })
30     end
31   end
32 end
33
34 module Redwood
35   VERSION = "0.2"
36
37   BASE_DIR   = ENV["SUP_BASE"] || File.join(ENV["HOME"], ".sup")
38   CONFIG_FN  = File.join(BASE_DIR, "config.yaml")
39   SOURCE_FN  = File.join(BASE_DIR, "sources.yaml")
40   LABEL_FN   = File.join(BASE_DIR, "labels.txt")
41   PERSON_FN  = File.join(BASE_DIR, "people.txt")
42   CONTACT_FN = File.join(BASE_DIR, "contacts.txt")
43   DRAFT_DIR  = File.join(BASE_DIR, "drafts")
44   SENT_FN    = File.join(BASE_DIR, "sent.mbox")
45   LOCK_FN    = File.join(BASE_DIR, "lock")
46   SUICIDE_FN = File.join(BASE_DIR, "please-kill-yourself")
47   HOOK_DIR   = File.join(BASE_DIR, "hooks")
48
49   YAML_DOMAIN = "masanjin.net"
50   YAML_DATE = "2006-10-01"
51
52 ## determine encoding and character set
53 ## probably a better way to do this
54   $ctype = ENV["LC_CTYPE"] || ENV["LANG"] || "en-US.utf-8"
55   $encoding =
56     if $ctype =~ /\.(.*)?/
57       $1
58     else
59       "utf-8"
60     end
61
62 ## record exceptions thrown in threads nicely
63   $exception = nil
64   def reporting_thread
65     if $opts[:no_threads]
66       yield
67     else
68       ::Thread.new do
69         begin
70           yield
71         rescue Exception => e
72           $exception ||= e
73           raise
74         end
75       end
76     end
77   end
78   module_function :reporting_thread
79
80 ## one-stop shop for yamliciousness
81   def save_yaml_obj object, fn, safe=false
82     if safe
83       safe_fn = "#{File.dirname fn}/safe_#{File.basename fn}"
84       mode = File.stat(fn) if File.exists? fn
85       File.open(safe_fn, "w", mode) { |f| f.puts object.to_yaml }
86       FileUtils.mv safe_fn, fn
87     else
88       File.open(fn, "w") { |f| f.puts object.to_yaml }
89     end
90   end
91
92   def load_yaml_obj fn, compress=false
93     if File.exists? fn
94       if compress
95         Zlib::GzipReader.open(fn) { |f| YAML::load f }
96       else
97         YAML::load_file fn
98       end
99     end
100   end
101
102   def start
103     Redwood::PersonManager.new Redwood::PERSON_FN
104     Redwood::SentManager.new Redwood::SENT_FN
105     Redwood::ContactManager.new Redwood::CONTACT_FN
106     Redwood::LabelManager.new Redwood::LABEL_FN
107     Redwood::AccountManager.new $config[:accounts]
108     Redwood::DraftManager.new Redwood::DRAFT_DIR
109     Redwood::UpdateManager.new
110     Redwood::PollManager.new
111     Redwood::SuicideManager.new Redwood::SUICIDE_FN
112     Redwood::CryptoManager.new
113   end
114
115   def finish
116     Redwood::LabelManager.save if Redwood::LabelManager.instantiated?
117     Redwood::ContactManager.save if Redwood::ContactManager.instantiated?
118     Redwood::PersonManager.save if Redwood::PersonManager.instantiated?
119     Redwood::BufferManager.deinstantiate! if Redwood::BufferManager.instantiated?
120   end
121
122   ## not really a good place for this, so I'll just dump it here.
123   def report_broken_sources opts={}
124     return unless BufferManager.instantiated?
125
126     broken_sources = Index.usual_sources.select { |s| s.error.is_a? FatalSourceError }
127     File.open("goat", "w") { |f| f.puts Kernel.caller }
128     unless broken_sources.empty?
129       BufferManager.spawn_unless_exists("Broken source notification for #{broken_sources.join(',')}", opts) do
130         TextMode.new(<<EOM)
131 Source error notification
132 -------------------------
133
134 Hi there. It looks like one or more message sources is reporting
135 errors. Until this is corrected, messages from these sources cannot
136 be viewed, and new messages will not be detected.
137
138 #{broken_sources.map { |s| "Source: " + s.to_s + "\n Error: " + s.error.message.wrap(70).join("\n        ")}.join("\n\n")}
139 EOM
140 #' stupid ruby-mode
141       end
142     end
143
144     desynced_sources = Index.usual_sources.select { |s| s.error.is_a? OutOfSyncSourceError }
145     unless desynced_sources.empty?
146       BufferManager.spawn_unless_exists("Out-of-sync source notification for #{broken_sources.join(',')}", opts) do
147         TextMode.new(<<EOM)
148 Out-of-sync source notification
149 -------------------------------
150
151 Hi there. It looks like one or more sources has fallen out of sync
152 with my index. This can happen when you modify these sources with
153 other email clients. (Sorry, I don't play well with others.)
154
155 Until this is corrected, messages from these sources cannot be viewed,
156 and new messages will not be detected. Luckily, this is easy to correct!
157
158 #{desynced_sources.map do |s|
159   "Source: " + s.to_s + 
160    "\n Error: " + s.error.message.wrap(70).join("\n        ") + 
161    "\n   Fix: sup-sync --changed #{s.to_s}"
162   end}
163 EOM
164 #' stupid ruby-mode
165       end
166     end
167   end
168
169   module_function :save_yaml_obj, :load_yaml_obj, :start, :finish,
170                   :report_broken_sources
171 end
172
173 ## set up default configuration file
174 if File.exists? Redwood::CONFIG_FN
175   $config = Redwood::load_yaml_obj Redwood::CONFIG_FN
176 else
177   require 'etc'
178   require 'socket'
179   name = Etc.getpwnam(ENV["USER"]).gecos.split(/,/).first
180   email = ENV["USER"] + "@" + 
181     begin
182       Socket.gethostbyname(Socket.gethostname).first
183     rescue SocketError
184       Socket.gethostname
185     end
186
187   $config = {
188     :accounts => {
189       :default => {
190         :name => name,
191         :email => email,
192         :alternates => [],
193         :sendmail => "/usr/sbin/sendmail -oem -ti",
194         :signature => File.join(ENV["HOME"], ".signature")
195       }
196     },
197     :editor => ENV["EDITOR"] || "/usr/bin/vim -f -c 'setlocal spell spelllang=en_us' -c 'set filetype=mail'",
198     :thread_by_subject => false,
199     :edit_signature => false,
200     :ask_for_cc => true,
201     :ask_for_bcc => false,
202     :ask_for_subject => true,
203     :confirm_no_attachments => true,
204     :confirm_top_posting => true,
205   }
206   begin
207     FileUtils.mkdir_p Redwood::BASE_DIR
208     Redwood::save_yaml_obj $config, Redwood::CONFIG_FN
209   rescue StandardError => e
210     $stderr.puts "warning: #{e.message}"
211   end
212 end
213
214 require "sup/util"
215 require "sup/hook"
216
217 ## we have to initialize this guy first, because other classes must
218 ## reference it in order to register hooks, and they do that at parse
219 ## time.
220 Redwood::HookManager.new Redwood::HOOK_DIR
221
222 ## everything we need to get logging working
223 require "sup/buffer"
224 require "sup/keymap"
225 require "sup/mode"
226 require "sup/modes/scroll-mode"
227 require "sup/modes/text-mode"
228 require "sup/modes/log-mode"
229 require "sup/logger"
230 module Redwood
231   def log s; Logger.log s; end
232   module_function :log
233 end
234
235 ## now everything else (which can feel free to call Redwood::log at load time)
236 require "sup/update"
237 require "sup/suicide"
238 require "sup/message-chunks"
239 require "sup/message"
240 require "sup/source"
241 require "sup/mbox"
242 require "sup/maildir"
243 require "sup/imap"
244 require "sup/person"
245 require "sup/account"
246 require "sup/thread"
247 require "sup/index"
248 require "sup/textfield"
249 require "sup/colormap"
250 require "sup/label"
251 require "sup/contact"
252 require "sup/tagger"
253 require "sup/draft"
254 require "sup/poll"
255 require "sup/crypto"
256 require "sup/modes/line-cursor-mode"
257 require "sup/modes/help-mode"
258 require "sup/modes/edit-message-mode"
259 require "sup/modes/compose-mode"
260 require "sup/modes/resume-mode"
261 require "sup/modes/forward-mode"
262 require "sup/modes/reply-mode"
263 require "sup/modes/label-list-mode"
264 require "sup/modes/contact-list-mode"
265 require "sup/modes/thread-view-mode"
266 require "sup/modes/thread-index-mode"
267 require "sup/modes/label-search-results-mode"
268 require "sup/modes/search-results-mode"
269 require "sup/modes/person-search-results-mode"
270 require "sup/modes/inbox-mode"
271 require "sup/modes/buffer-list-mode"
272 require "sup/modes/poll-mode"
273 require "sup/modes/file-browser-mode"
274 require "sup/modes/completion-mode"
275 require "sup/sent"
276
277 $:.each do |base|
278   d = File.join base, "sup/share/modes/"
279   Redwood::Mode.load_all_modes d if File.directory? d
280 end