]> git.cworth.org Git - sup/blob - bin/sup
multiple concurrent process detection and resolution
[sup] / bin / sup
1 #!/usr/bin/env ruby
2
3 require 'rubygems'
4 require 'ncurses'
5 require 'fileutils'
6 require 'trollop'
7 require "sup"
8
9 $opts = Trollop::options do
10   version "sup v#{Redwood::VERSION}"
11   banner <<EOS
12 Sup is a curses-based email client.
13
14 Usage:
15   sup [options]
16
17 Options are:
18 EOS
19   opt :no_threads, "Turn of threading. Helps with debugging. (Necessarily disables background polling for new messages.)"
20 end
21
22 Thread.abort_on_exception = true # make debugging possible
23
24 module Redwood
25
26 global_keymap = Keymap.new do |k|
27   k.add :quit, "Quit Redwood", 'q'
28   k.add :help, "Show help", 'H', '?'
29   k.add :roll_buffers, "Switch to next buffer", 'b'
30 #  k.add :roll_buffers_backwards, "Switch to previous buffer", 'B'
31   k.add :kill_buffer, "Kill the current buffer", 'x'
32   k.add :list_buffers, "List all buffers", 'B'
33   k.add :list_contacts, "List contacts", 'C'
34   k.add :redraw, "Redraw screen", :ctrl_l
35   k.add :search, "Search messages", '/'
36   k.add :list_labels, "List labels", 'L'
37   k.add :poll, "Poll for new messages", 'P'
38   k.add :compose, "Compose new message", 'm'
39   k.add :recall_draft, "Edit most recent draft message", 'R'
40 end
41
42 def start_cursing
43   Ncurses.initscr
44   Ncurses.noecho
45   Ncurses.cbreak
46   Ncurses.stdscr.keypad 1
47   Ncurses.curs_set 0
48   Ncurses.start_color
49 end
50
51 def stop_cursing
52   Ncurses.curs_set 1
53   Ncurses.echo
54   Ncurses.endwin
55 end
56 module_function :start_cursing, :stop_cursing
57
58 begin
59   Redwood::lock
60 rescue LockError => e
61   require 'highline'
62   h = HighLine.new
63   h.say <<EOS
64 Error: sup is already running! User #{e.user} on host #{e.host} was running sup
65 with pid #{e.pid} as of #{e.time}.
66 EOS
67
68   case h.ask("Should I try and kill that process? ")
69   when /^\s*y\s*$/i
70     h.say "Ok, suggesting sepuku..."
71     FileUtils.touch Redwood::SUICIDE_FN
72     sleep SuicideManager::DELAY * 2
73     FileUtils.rm_f Redwood::SUICIDE_FN
74     h.say "Let's try that again."
75     retry
76   else
77     h.say "Ok, see you later."
78     exit
79   end
80 end
81
82 Redwood::start
83 Index.new.load
84
85 if(s = Index.source_for DraftManager.source_name)
86   DraftManager.source = s
87 else
88   Index.add_source DraftManager.new_source
89 end
90
91 if(s = Index.source_for SentManager.source_name)
92   SentManager.source = s
93 else
94   Index.add_source SentManager.new_source
95 end
96
97 begin
98   log "starting curses"
99   start_cursing
100
101   log "initializing colormap"
102   Colormap.new do |c|
103     c.add :status_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLUE, Ncurses::A_BOLD
104     c.add :index_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
105     c.add :index_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
106            Ncurses::A_BOLD
107     c.add :index_starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK, 
108            Ncurses::A_BOLD
109     c.add :labellist_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
110     c.add :labellist_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
111            Ncurses::A_BOLD
112     c.add :twiddle_color, Ncurses::COLOR_BLUE, Ncurses::COLOR_BLACK
113     c.add :label_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
114     c.add :message_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_GREEN
115     c.add :alternate_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_BLUE
116     c.add :missing_message_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_RED
117     c.add :mime_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
118     c.add :quote_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
119     c.add :sig_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
120     c.add :quote_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
121     c.add :sig_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
122     c.add :to_me_color, Ncurses::COLOR_GREEN, Ncurses::COLOR_BLACK
123     c.add :starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
124           Ncurses::A_BOLD
125     c.add :starred_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_GREEN,
126           Ncurses::A_BOLD
127     c.add :alternate_starred_patina_color, Ncurses::COLOR_YELLOW,
128           Ncurses::COLOR_BLUE, Ncurses::A_BOLD
129     c.add :snippet_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
130     c.add :option_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
131     c.add :tagged_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
132           Ncurses::A_BOLD
133     c.add :draft_notification_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
134           Ncurses::A_BOLD
135   end
136
137   log "initializing buffer manager"
138   bm = BufferManager.new
139
140   log "initializing mail index buffer"
141   imode = InboxMode.new
142   ibuf = bm.spawn "Inbox", imode
143
144   log "ready for interaction!"
145   Logger.make_buf
146
147   bm.draw_screen
148
149   begin
150     Index.usual_sources.each { |s| s.check }
151   rescue SourceError
152     # do nothing! we'll report it at the next step
153   end
154   Redwood::report_broken_sources
155   
156   Index.usual_sources.each do |s|
157     reporting_thread do
158       begin
159         s.connect
160       rescue SourceError => e
161         Redwood::log "fatal error loading from #{s}: #{e.message}"
162       end
163     end if s.respond_to? :connect
164   end
165
166   imode.load_threads :num => ibuf.content_height, :when_done => lambda { reporting_thread { sleep 1; PollManager.poll } }
167
168   unless $opts[:no_threads]
169     PollManager.start_thread
170     SuicideManager.start_thread
171   end
172
173   until $exception
174     bm.draw_screen
175     c = Ncurses.nonblocking_getch
176     bm.erase_flash
177
178     if c == Ncurses::KEY_RESIZE
179       bm.handle_resize
180     elsif c
181       unless bm.handle_input(c)
182         x = global_keymap.action_for c
183         case x
184         when :quit
185           break if bm.kill_all_buffers_safely
186         when :help
187           curmode = bm.focus_buf.mode
188           bm.spawn_unless_exists("<help for #{curmode.name}>") { HelpMode.new curmode, global_keymap }
189         when :roll_buffers
190           bm.roll_buffers
191         when :roll_buffers_backwards
192           bm.roll_buffers_backwards
193         when :kill_buffer
194           bm.kill_buffer_safely bm.focus_buf
195         when :list_buffers
196           bm.spawn_unless_exists("Buffer List") { BufferListMode.new }
197         when :list_contacts
198           b = bm.spawn_unless_exists("Contact List") { ContactListMode.new }
199           b.mode.load_in_background
200         when :search
201           text = bm.ask :search, "query: "
202           next unless text && text !~ /^\s*$/
203
204           begin
205             qobj = Index.parse_user_query_string text
206             short_text = text.length < 20 ? text : text[0 ... 20] + "..."
207             log "built query from #{text.inspect}: #{qobj}"
208             mode = SearchResultsMode.new qobj
209             bm.spawn "search: \"#{short_text}\"", mode
210             mode.load_threads :num => mode.buffer.content_height
211           rescue Ferret::QueryParser::QueryParseException => e
212             bm.flash "Couldn't parse query."
213           end
214         when :list_labels
215           b = bm.spawn_unless_exists("Label list") { LabelListMode.new }
216           b.mode.load_in_background
217         when :compose
218           mode = ComposeMode.new
219           bm.spawn "New Message", mode
220           mode.edit
221         when :poll
222 #          bm.raise_to_front PollManager.buffer
223           reporting_thread { PollManager.poll }
224         when :recall_draft
225           case Index.num_results_for :label => :draft
226           when 0
227             bm.flash "No draft messages."
228           when 1
229             m = nil
230             Index.each_id_by_date(:label => :draft) { |mid, builder| m = builder.call }
231             r = ResumeMode.new(m)
232             BufferManager.spawn "Edit message", r
233             r.edit
234           else
235             b = BufferManager.spawn_unless_exists("All drafts") do
236               mode = LabelSearchResultsMode.new [:draft]
237             end
238             b.mode.load_threads :num => b.content_height
239           end
240         when :nothing
241         when :redraw
242           bm.completely_redraw_screen
243         else
244           bm.flash "Unknown key press '#{c.to_character}' for #{bm.focus_buf.mode.name}."
245         end
246       end
247     end
248   end
249 rescue Exception => e
250   $exception ||= e
251 ensure
252   Redwood::finish
253   stop_cursing
254
255   case $exception
256   when SuicideException
257     Redwood::log "I've been asked to commit sepuku. I obey!"
258     exit
259   when nil
260     Redwood::log "good night, sweet prince!"
261     Index.save
262   else
263     Redwood::log "oh crap, an exception"
264   end
265
266   Redwood::unlock
267 end
268
269 if $exception 
270   $stderr.puts <<EOS
271 ----------------------------------------------------------------
272 I'm very sorry, but it seems that an error occurred in Sup. 
273 Please accept my sincere apologies. If you don't mind, please
274 send the backtrace below and a brief report of the circumstances
275 to wmorgan-sup at masanjin dot nets so that I might address this
276 problem. Thank you!
277
278 Sincerely,
279 William
280 ----------------------------------------------------------------
281
282 The problem was: #{$exception.message} (error type #{$exception.class.name})
283 A backtrace follows:
284 EOS
285   raise $exception
286 end
287
288 end