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