8 $opts = Trollop::options do
9 version "sup v#{Redwood::VERSION}"
11 Sup is a curses-based email client.
18 opt :no_threads, "Turn of threading. Helps with debugging. (Necessarily disables background polling for new messages.)"
21 Thread.abort_on_exception = true # make debugging possible
25 global_keymap = Keymap.new do |k|
26 k.add :quit, "Quit Redwood", 'q'
27 k.add :help, "Show help", 'H', '?'
28 k.add :roll_buffers, "Switch to next buffer", 'b'
29 # k.add :roll_buffers_backwards, "Switch to previous buffer", 'B'
30 k.add :kill_buffer, "Kill the current buffer", 'x'
31 k.add :list_buffers, "List all buffers", 'B'
32 k.add :list_contacts, "List contacts", 'C'
33 k.add :redraw, "Redraw screen", :ctrl_l
34 k.add :search, "Search messages", '/'
35 k.add :list_labels, "List labels", 'L'
36 k.add :poll, "Poll for new messages", 'P'
37 k.add :compose, "Compose new message", 'm'
38 k.add :recall_draft, "Edit most recent draft message", 'R'
45 Ncurses.stdscr.keypad 1
55 module_function :start_cursing, :stop_cursing
60 if(s = Index.source_for DraftManager.source_name)
61 DraftManager.source = s
63 Index.add_source DraftManager.new_source
66 if(s = Index.source_for SentManager.source_name)
67 SentManager.source = s
69 Index.add_source SentManager.new_source
76 log "initializing colormap"
78 c.add :status_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLUE, Ncurses::A_BOLD
79 c.add :index_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
80 c.add :index_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK,
82 c.add :index_starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
84 c.add :labellist_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
85 c.add :labellist_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK,
87 c.add :twiddle_color, Ncurses::COLOR_BLUE, Ncurses::COLOR_BLACK
88 c.add :label_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
89 c.add :message_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_GREEN
90 c.add :alternate_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_BLUE
91 c.add :missing_message_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_RED
92 c.add :mime_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
93 c.add :quote_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
94 c.add :sig_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
95 c.add :quote_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
96 c.add :sig_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
97 c.add :to_me_color, Ncurses::COLOR_GREEN, Ncurses::COLOR_BLACK
98 c.add :starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
100 c.add :starred_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_GREEN,
102 c.add :alternate_starred_patina_color, Ncurses::COLOR_YELLOW,
103 Ncurses::COLOR_BLUE, Ncurses::A_BOLD
104 c.add :snippet_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
105 c.add :option_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
106 c.add :tagged_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
108 c.add :draft_notification_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
112 log "initializing buffer manager"
113 bm = BufferManager.new
115 log "initializing mail index buffer"
116 imode = InboxMode.new
117 ibuf = bm.spawn "Inbox", imode
119 log "ready for interaction!"
125 Index.usual_sources.each { |s| s.check }
127 # do nothing! we'll report it at the next step
129 Redwood::report_broken_sources
131 Index.usual_sources.each do |s|
135 rescue SourceError => e
136 Redwood::log "fatal error loading from #{s}: #{e.message}"
138 end if s.respond_to? :connect
141 imode.load_threads :num => ibuf.content_height, :when_done => lambda { reporting_thread { sleep 1; PollManager.poll } }
143 PollManager.start_thread unless $opts[:no_threads]
147 c = Ncurses.nonblocking_getch
150 if c == Ncurses::KEY_RESIZE
153 unless bm.handle_input(c)
154 x = global_keymap.action_for c
157 break if bm.kill_all_buffers_safely
159 curmode = bm.focus_buf.mode
160 bm.spawn_unless_exists("<help for #{curmode.name}>") { HelpMode.new curmode, global_keymap }
163 when :roll_buffers_backwards
164 bm.roll_buffers_backwards
166 bm.kill_buffer_safely bm.focus_buf
168 bm.spawn_unless_exists("Buffer List") { BufferListMode.new }
170 b = bm.spawn_unless_exists("Contact List") { ContactListMode.new }
171 b.mode.load_in_background
173 text = bm.ask :search, "query: "
174 next unless text && text !~ /^\s*$/
177 qobj = Index.parse_user_query_string text
178 short_text = text.length < 20 ? text : text[0 ... 20] + "..."
179 log "built query from #{text.inspect}: #{qobj}"
180 mode = SearchResultsMode.new qobj
181 bm.spawn "search: \"#{short_text}\"", mode
182 mode.load_threads :num => mode.buffer.content_height
183 rescue Ferret::QueryParser::QueryParseException => e
184 bm.flash "Couldn't parse query."
187 b = bm.spawn_unless_exists("Label list") { LabelListMode.new }
188 b.mode.load_in_background
190 mode = ComposeMode.new
191 bm.spawn "New Message", mode
194 # bm.raise_to_front PollManager.buffer
195 reporting_thread { PollManager.poll }
197 case Index.num_results_for :label => :draft
199 bm.flash "No draft messages."
202 Index.each_id_by_date(:label => :draft) { |mid, builder| m = builder.call }
203 r = ResumeMode.new(m)
204 BufferManager.spawn "Edit message", r
207 b = BufferManager.spawn_unless_exists("All drafts") do
208 mode = LabelSearchResultsMode.new [:draft]
210 b.mode.load_threads :num => b.content_height
214 bm.completely_redraw_screen
216 bm.flash "Unknown key press '#{c.to_character}' for #{bm.focus_buf.mode.name}."
221 rescue Exception => e
227 Redwood::log "oh crap, an exception"
229 Redwood::log "good night, sweet prince!"
233 Index.save unless $exception # TODO: think about this
237 ----------------------------------------------------------------
238 I'm very sorry, but it seems that an error occurred in Sup.
239 Please accept my sincere apologies. If you don't mind, please
240 send the backtrace below and a brief report of the circumstances
241 to wmorgan-sup at masanjin dot nets so that I might address this
246 ----------------------------------------------------------------
248 The problem was: #{$exception.message} (error type #{$exception.class.name})