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