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