]> git.cworth.org Git - sup/blob - lib/sup.rb
Merge branch 'dont-canonicalize-email-addresses' into next
[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   CONTACT_FN = File.join(BASE_DIR, "contacts.txt")
57   DRAFT_DIR  = File.join(BASE_DIR, "drafts")
58   SENT_FN    = File.join(BASE_DIR, "sent.mbox")
59   LOCK_FN    = File.join(BASE_DIR, "lock")
60   SUICIDE_FN = File.join(BASE_DIR, "please-kill-yourself")
61   HOOK_DIR   = File.join(BASE_DIR, "hooks")
62
63   YAML_DOMAIN = "masanjin.net"
64   YAML_DATE = "2006-10-01"
65
66   ## record exceptions thrown in threads nicely
67   @exceptions = []
68   @exception_mutex = Mutex.new
69
70   attr_reader :exceptions
71   def record_exception e, name
72     @exception_mutex.synchronize do
73       @exceptions ||= []
74       @exceptions << [e, name]
75     end
76   end
77
78   def reporting_thread name
79     if $opts[:no_threads]
80       yield
81     else
82       ::Thread.new do
83         begin
84           yield
85         rescue Exception => e
86           record_exception e, name
87         end
88       end
89     end
90   end
91
92   module_function :reporting_thread, :record_exception, :exceptions
93
94 ## one-stop shop for yamliciousness
95   def save_yaml_obj object, fn, safe=false
96     if safe
97       safe_fn = "#{File.dirname fn}/safe_#{File.basename fn}"
98       mode = File.stat(fn).mode if File.exists? fn
99       File.open(safe_fn, "w", mode) { |f| f.puts object.to_yaml }
100       FileUtils.mv safe_fn, fn
101     else
102       File.open(fn, "w") { |f| f.puts object.to_yaml }
103     end
104   end
105
106   def load_yaml_obj fn, compress=false
107     if File.exists? fn
108       if compress
109         Zlib::GzipReader.open(fn) { |f| YAML::load f }
110       else
111         YAML::load_file fn
112       end
113     end
114   end
115
116   def start
117     Redwood::SentManager.new Redwood::SENT_FN
118     Redwood::ContactManager.new Redwood::CONTACT_FN
119     Redwood::LabelManager.new Redwood::LABEL_FN
120     Redwood::AccountManager.new $config[:accounts]
121     Redwood::DraftManager.new Redwood::DRAFT_DIR
122     Redwood::UpdateManager.new
123     Redwood::PollManager.new
124     Redwood::SuicideManager.new Redwood::SUICIDE_FN
125     Redwood::CryptoManager.new
126     Redwood::UndoManager.new
127   end
128
129   def finish
130     Redwood::LabelManager.save if Redwood::LabelManager.instantiated?
131     Redwood::ContactManager.save if Redwood::ContactManager.instantiated?
132     Redwood::BufferManager.deinstantiate! if Redwood::BufferManager.instantiated?
133   end
134
135   ## not really a good place for this, so I'll just dump it here.
136   ##
137   ## a source error is either a FatalSourceError or an OutOfSyncSourceError.
138   ## the superclass SourceError is just a generic.
139   def report_broken_sources opts={}
140     return unless BufferManager.instantiated?
141
142     broken_sources = Index.sources.select { |s| s.error.is_a? FatalSourceError }
143     unless broken_sources.empty?
144       BufferManager.spawn_unless_exists("Broken source notification for #{broken_sources.join(',')}", opts) do
145         TextMode.new(<<EOM)
146 Source error notification
147 -------------------------
148
149 Hi there. It looks like one or more message sources is reporting
150 errors. Until this is corrected, messages from these sources cannot
151 be viewed, and new messages will not be detected.
152
153 #{broken_sources.map { |s| "Source: " + s.to_s + "\n Error: " + s.error.message.wrap(70).join("\n        ")}.join("\n\n")}
154 EOM
155 #' stupid ruby-mode
156       end
157     end
158
159     desynced_sources = Index.sources.select { |s| s.error.is_a? OutOfSyncSourceError }
160     unless desynced_sources.empty?
161       BufferManager.spawn_unless_exists("Out-of-sync source notification for #{broken_sources.join(',')}", opts) do
162         TextMode.new(<<EOM)
163 Out-of-sync source notification
164 -------------------------------
165
166 Hi there. It looks like one or more sources has fallen out of sync
167 with my index. This can happen when you modify these sources with
168 other email clients. (Sorry, I don't play well with others.)
169
170 Until this is corrected, messages from these sources cannot be viewed,
171 and new messages will not be detected. Luckily, this is easy to correct!
172
173 #{desynced_sources.map do |s|
174   "Source: " + s.to_s + 
175    "\n Error: " + s.error.message.wrap(70).join("\n        ") + 
176    "\n   Fix: sup-sync --changed #{s.to_s}"
177   end}
178 EOM
179 #' stupid ruby-mode
180       end
181     end
182   end
183
184   module_function :save_yaml_obj, :load_yaml_obj, :start, :finish,
185                   :report_broken_sources
186 end
187
188 ## set up default configuration file
189 if File.exists? Redwood::CONFIG_FN
190   $config = Redwood::load_yaml_obj Redwood::CONFIG_FN
191 else
192   require 'etc'
193   require 'socket'
194   name = Etc.getpwnam(ENV["USER"]).gecos.split(/,/).first rescue nil
195   name ||= ENV["USER"]
196   email = ENV["USER"] + "@" + 
197     begin
198       Socket.gethostbyname(Socket.gethostname).first
199     rescue SocketError
200       Socket.gethostname
201     end
202
203   $config = {
204     :accounts => {
205       :default => {
206         :name => name,
207         :email => email,
208         :alternates => [],
209         :sendmail => "/usr/sbin/sendmail -oem -ti",
210         :signature => File.join(ENV["HOME"], ".signature")
211       }
212     },
213     :editor => ENV["EDITOR"] || "/usr/bin/vim -f -c 'setlocal spell spelllang=en_us' -c 'set filetype=mail'",
214     :thread_by_subject => false,
215     :edit_signature => false,
216     :ask_for_cc => true,
217     :ask_for_bcc => false,
218     :ask_for_subject => true,
219     :confirm_no_attachments => true,
220     :confirm_top_posting => true,
221     :discard_snippets_from_encrypted_messages => false,
222     :default_attachment_save_dir => "",
223   }
224   begin
225     FileUtils.mkdir_p Redwood::BASE_DIR
226     Redwood::save_yaml_obj $config, Redwood::CONFIG_FN
227   rescue StandardError => e
228     $stderr.puts "warning: #{e.message}"
229   end
230 end
231
232 require "sup/util"
233 require "sup/hook"
234
235 ## we have to initialize this guy first, because other classes must
236 ## reference it in order to register hooks, and they do that at parse
237 ## time.
238 Redwood::HookManager.new Redwood::HOOK_DIR
239
240 ## everything we need to get logging working
241 require "sup/buffer"
242 require "sup/keymap"
243 require "sup/mode"
244 require "sup/modes/scroll-mode"
245 require "sup/modes/text-mode"
246 require "sup/modes/log-mode"
247 require "sup/logger"
248 module Redwood
249   def log s; Logger.log s; end
250   module_function :log
251 end
252
253 ## determine encoding and character set
254   $encoding = Locale.current.charset
255   if $encoding
256     Redwood::log "using character set encoding #{$encoding.inspect}"
257   else
258     Redwood::log "warning: can't find character set by using locale, defaulting to utf-8"
259     $encoding = "utf-8"
260   end
261
262 ## now everything else (which can feel free to call Redwood::log at load time)
263 require "sup/update"
264 require "sup/suicide"
265 require "sup/message-chunks"
266 require "sup/message"
267 require "sup/source"
268 require "sup/mbox"
269 require "sup/maildir"
270 require "sup/imap"
271 require "sup/person"
272 require "sup/account"
273 require "sup/thread"
274 require "sup/index"
275 require "sup/textfield"
276 require "sup/colormap"
277 require "sup/label"
278 require "sup/contact"
279 require "sup/tagger"
280 require "sup/draft"
281 require "sup/poll"
282 require "sup/crypto"
283 require "sup/undo"
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"
304 require "sup/sent"
305
306 $:.each do |base|
307   d = File.join base, "sup/share/modes/"
308   Redwood::Mode.load_all_modes d if File.directory? d
309 end