]> git.cworth.org Git - sup/blob - lib/sup.rb
Added UndoManager class
[sup] / lib / sup.rb
1 require 'rubygems'
2 require 'yaml'
3 require 'zlib'
4 require 'thread'
5 require 'fileutils'
6 require 'gettext'
7 require 'curses'
8
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.
13
14 require 'dl/import'
15 module LibC
16   extend DL::Importable
17   dlload Config::CONFIG['arch'] =~ /darwin/ ? "libc.dylib" : "libc.so.6"
18   extern "void setlocale(int, const char *)"
19 end
20 LibC.setlocale(6, "")  # LC_ALL == 6
21
22 class Object
23   ## this is for debugging purposes because i keep calling #id on the
24   ## wrong object and i want it to throw an exception
25   def id
26     raise "wrong id called on #{self.inspect}"
27   end
28 end
29
30 class Module
31   def yaml_properties *props
32     props = props.map { |p| p.to_s }
33     vars = props.map { |p| "@#{p}" }
34     klass = self
35     path = klass.name.gsub(/::/, "/")
36     
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}" }
40     end
41
42     YAML.add_domain_type("#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}", path) do |type, val|
43       klass.new(*props.map { |p| val[p] })
44     end
45   end
46 end
47
48 module Redwood
49   VERSION = "git"
50
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")
63
64   YAML_DOMAIN = "masanjin.net"
65   YAML_DATE = "2006-10-01"
66
67   ## record exceptions thrown in threads nicely
68   @exceptions = []
69   @exception_mutex = Mutex.new
70
71   attr_reader :exceptions
72   def record_exception e, name
73     @exception_mutex.synchronize do
74       @exceptions ||= []
75       @exceptions << [e, name]
76     end
77   end
78
79   def reporting_thread name
80     if $opts[:no_threads]
81       yield
82     else
83       ::Thread.new do
84         begin
85           yield
86         rescue Exception => e
87           record_exception e, name
88         end
89       end
90     end
91   end
92
93   module_function :reporting_thread, :record_exception, :exceptions
94
95 ## one-stop shop for yamliciousness
96   def save_yaml_obj object, fn, safe=false
97     if safe
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
102     else
103       File.open(fn, "w") { |f| f.puts object.to_yaml }
104     end
105   end
106
107   def load_yaml_obj fn, compress=false
108     if File.exists? fn
109       if compress
110         Zlib::GzipReader.open(fn) { |f| YAML::load f }
111       else
112         YAML::load_file fn
113       end
114     end
115   end
116
117   def start
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
128     Redwood::UndoManager.new
129   end
130
131   def finish
132     Redwood::LabelManager.save if Redwood::LabelManager.instantiated?
133     Redwood::ContactManager.save if Redwood::ContactManager.instantiated?
134     Redwood::PersonManager.save if Redwood::PersonManager.instantiated?
135     Redwood::BufferManager.deinstantiate! if Redwood::BufferManager.instantiated?
136   end
137
138   ## not really a good place for this, so I'll just dump it here.
139   ##
140   ## a source error is either a FatalSourceError or an OutOfSyncSourceError.
141   ## the superclass SourceError is just a generic.
142   def report_broken_sources opts={}
143     return unless BufferManager.instantiated?
144
145     broken_sources = Index.sources.select { |s| s.error.is_a? FatalSourceError }
146     unless broken_sources.empty?
147       BufferManager.spawn_unless_exists("Broken source notification for #{broken_sources.join(',')}", opts) do
148         TextMode.new(<<EOM)
149 Source error notification
150 -------------------------
151
152 Hi there. It looks like one or more message sources is reporting
153 errors. Until this is corrected, messages from these sources cannot
154 be viewed, and new messages will not be detected.
155
156 #{broken_sources.map { |s| "Source: " + s.to_s + "\n Error: " + s.error.message.wrap(70).join("\n        ")}.join("\n\n")}
157 EOM
158 #' stupid ruby-mode
159       end
160     end
161
162     desynced_sources = Index.sources.select { |s| s.error.is_a? OutOfSyncSourceError }
163     unless desynced_sources.empty?
164       BufferManager.spawn_unless_exists("Out-of-sync source notification for #{broken_sources.join(',')}", opts) do
165         TextMode.new(<<EOM)
166 Out-of-sync source notification
167 -------------------------------
168
169 Hi there. It looks like one or more sources has fallen out of sync
170 with my index. This can happen when you modify these sources with
171 other email clients. (Sorry, I don't play well with others.)
172
173 Until this is corrected, messages from these sources cannot be viewed,
174 and new messages will not be detected. Luckily, this is easy to correct!
175
176 #{desynced_sources.map do |s|
177   "Source: " + s.to_s + 
178    "\n Error: " + s.error.message.wrap(70).join("\n        ") + 
179    "\n   Fix: sup-sync --changed #{s.to_s}"
180   end}
181 EOM
182 #' stupid ruby-mode
183       end
184     end
185   end
186
187   module_function :save_yaml_obj, :load_yaml_obj, :start, :finish,
188                   :report_broken_sources
189 end
190
191 ## set up default configuration file
192 if File.exists? Redwood::CONFIG_FN
193   $config = Redwood::load_yaml_obj Redwood::CONFIG_FN
194 else
195   require 'etc'
196   require 'socket'
197   name = Etc.getpwnam(ENV["USER"]).gecos.split(/,/).first rescue nil
198   name ||= ENV["USER"]
199   email = ENV["USER"] + "@" + 
200     begin
201       Socket.gethostbyname(Socket.gethostname).first
202     rescue SocketError
203       Socket.gethostname
204     end
205
206   $config = {
207     :accounts => {
208       :default => {
209         :name => name,
210         :email => email,
211         :alternates => [],
212         :sendmail => "/usr/sbin/sendmail -oem -ti",
213         :signature => File.join(ENV["HOME"], ".signature")
214       }
215     },
216     :editor => ENV["EDITOR"] || "/usr/bin/vim -f -c 'setlocal spell spelllang=en_us' -c 'set filetype=mail'",
217     :thread_by_subject => false,
218     :edit_signature => false,
219     :ask_for_cc => true,
220     :ask_for_bcc => false,
221     :ask_for_subject => true,
222     :confirm_no_attachments => true,
223     :confirm_top_posting => true,
224     :discard_snippets_from_encrypted_messages => false,
225   }
226   begin
227     FileUtils.mkdir_p Redwood::BASE_DIR
228     Redwood::save_yaml_obj $config, Redwood::CONFIG_FN
229   rescue StandardError => e
230     $stderr.puts "warning: #{e.message}"
231   end
232 end
233
234 require "sup/util"
235 require "sup/hook"
236
237 ## we have to initialize this guy first, because other classes must
238 ## reference it in order to register hooks, and they do that at parse
239 ## time.
240 Redwood::HookManager.new Redwood::HOOK_DIR
241
242 ## everything we need to get logging working
243 require "sup/buffer"
244 require "sup/keymap"
245 require "sup/mode"
246 require "sup/modes/scroll-mode"
247 require "sup/modes/text-mode"
248 require "sup/modes/log-mode"
249 require "sup/logger"
250 module Redwood
251   def log s; Logger.log s; end
252   module_function :log
253 end
254
255 ## determine encoding and character set
256   $encoding = Locale.current.charset
257   if $encoding
258     Redwood::log "using character set encoding #{$encoding.inspect}"
259   else
260     Redwood::log "warning: can't find character set by using locale, defaulting to utf-8"
261     $encoding = "utf-8"
262   end
263
264 ## now everything else (which can feel free to call Redwood::log at load time)
265 require "sup/update"
266 require "sup/suicide"
267 require "sup/message-chunks"
268 require "sup/message"
269 require "sup/source"
270 require "sup/mbox"
271 require "sup/maildir"
272 require "sup/imap"
273 require "sup/person"
274 require "sup/account"
275 require "sup/thread"
276 require "sup/index"
277 require "sup/textfield"
278 require "sup/colormap"
279 require "sup/label"
280 require "sup/contact"
281 require "sup/tagger"
282 require "sup/draft"
283 require "sup/poll"
284 require "sup/crypto"
285 require "sup/undo"
286 require "sup/horizontal-selector"
287 require "sup/modes/line-cursor-mode"
288 require "sup/modes/help-mode"
289 require "sup/modes/edit-message-mode"
290 require "sup/modes/compose-mode"
291 require "sup/modes/resume-mode"
292 require "sup/modes/forward-mode"
293 require "sup/modes/reply-mode"
294 require "sup/modes/label-list-mode"
295 require "sup/modes/contact-list-mode"
296 require "sup/modes/thread-view-mode"
297 require "sup/modes/thread-index-mode"
298 require "sup/modes/label-search-results-mode"
299 require "sup/modes/search-results-mode"
300 require "sup/modes/person-search-results-mode"
301 require "sup/modes/inbox-mode"
302 require "sup/modes/buffer-list-mode"
303 require "sup/modes/poll-mode"
304 require "sup/modes/file-browser-mode"
305 require "sup/modes/completion-mode"
306 require "sup/sent"
307
308 $:.each do |base|
309   d = File.join base, "sup/share/modes/"
310   Redwood::Mode.load_all_modes d if File.directory? d
311 end