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