9 $opts = Trollop::options do
10 version "sup v#{Redwood::VERSION}"
12 Sup is a curses-based email client.
19 opt :no_threads, "Turn of threading. Helps with debugging. (Necessarily disables background polling for new messages.)"
22 Thread.abort_on_exception = true # make debugging possible
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'
46 Ncurses.stdscr.keypad 1
56 module_function :start_cursing, :stop_cursing
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}.
68 case h.ask("Should I try and kill that process? ")
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."
77 h.say "Ok, see you later."
85 if(s = Index.source_for DraftManager.source_name)
86 DraftManager.source = s
88 Index.add_source DraftManager.new_source
91 if(s = Index.source_for SentManager.source_name)
92 SentManager.source = s
94 Index.add_source SentManager.new_source
101 log "initializing colormap"
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,
107 c.add :index_starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
109 c.add :labellist_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
110 c.add :labellist_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK,
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,
125 c.add :starred_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_GREEN,
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,
133 c.add :draft_notification_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
137 log "initializing buffer manager"
138 bm = BufferManager.new
140 log "initializing mail index buffer"
141 imode = InboxMode.new
142 ibuf = bm.spawn "Inbox", imode
144 log "ready for interaction!"
150 Index.usual_sources.each { |s| s.check }
152 # do nothing! we'll report it at the next step
154 Redwood::report_broken_sources
156 Index.usual_sources.each do |s|
160 rescue SourceError => e
161 Redwood::log "fatal error loading from #{s}: #{e.message}"
163 end if s.respond_to? :connect
166 imode.load_threads :num => ibuf.content_height, :when_done => lambda { reporting_thread { sleep 1; PollManager.poll } }
168 unless $opts[:no_threads]
169 PollManager.start_thread
170 SuicideManager.start_thread
175 c = Ncurses.nonblocking_getch
178 if c == Ncurses::KEY_RESIZE
181 unless bm.handle_input(c)
182 x = global_keymap.action_for c
185 break if bm.kill_all_buffers_safely
187 curmode = bm.focus_buf.mode
188 bm.spawn_unless_exists("<help for #{curmode.name}>") { HelpMode.new curmode, global_keymap }
191 when :roll_buffers_backwards
192 bm.roll_buffers_backwards
194 bm.kill_buffer_safely bm.focus_buf
196 bm.spawn_unless_exists("Buffer List") { BufferListMode.new }
198 b = bm.spawn_unless_exists("Contact List") { ContactListMode.new }
199 b.mode.load_in_background
201 text = bm.ask :search, "query: "
202 next unless text && text !~ /^\s*$/
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."
215 b = bm.spawn_unless_exists("Label list") { LabelListMode.new }
216 b.mode.load_in_background
218 mode = ComposeMode.new
219 bm.spawn "New Message", mode
222 # bm.raise_to_front PollManager.buffer
223 reporting_thread { PollManager.poll }
225 case Index.num_results_for :label => :draft
227 bm.flash "No draft messages."
230 Index.each_id_by_date(:label => :draft) { |mid, builder| m = builder.call }
231 r = ResumeMode.new(m)
232 BufferManager.spawn "Edit message", r
235 b = BufferManager.spawn_unless_exists("All drafts") do
236 mode = LabelSearchResultsMode.new [:draft]
238 b.mode.load_threads :num => b.content_height
242 bm.completely_redraw_screen
244 bm.flash "Unknown key press '#{c.to_character}' for #{bm.focus_buf.mode.name}."
249 rescue Exception => e
256 when SuicideException
257 Redwood::log "I've been asked to commit sepuku. I obey!"
260 Redwood::log "good night, sweet prince!"
263 Redwood::log "oh crap, an exception"
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
280 ----------------------------------------------------------------
282 The problem was: #{$exception.message} (error type #{$exception.class.name})