1 | /* Copyright (c) 2004 James Kretchmar. All rights reserved. |
---|
2 | * |
---|
3 | * Redistribution and use in source and binary forms, with or without |
---|
4 | * modification, are permitted provided that the following conditions are |
---|
5 | * met: |
---|
6 | * |
---|
7 | * * Redistributions of source code must retain the above copyright |
---|
8 | * notice, this list of conditions and the following disclaimer. |
---|
9 | * |
---|
10 | * * Redistributions in binary form must reproduce the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer in |
---|
12 | * the documentation and/or other materials provided with the |
---|
13 | * distribution. |
---|
14 | * |
---|
15 | * * Redistributions in any form must be accompanied by information on |
---|
16 | * how to obtain complete source code for the Owl software and any |
---|
17 | * accompanying software that uses the Owl software. The source code |
---|
18 | * must either be included in the distribution or be available for no |
---|
19 | * more than the cost of distribution plus a nominal fee, and must be |
---|
20 | * freely redistributable under reasonable conditions. For an |
---|
21 | * executable file, complete source code means the source code for |
---|
22 | * all modules it contains. It does not include source code for |
---|
23 | * modules or files that typically accompany the major components of |
---|
24 | * the operating system on which the executable file runs. |
---|
25 | * |
---|
26 | * |
---|
27 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
---|
28 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
29 | * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR |
---|
30 | * NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
---|
31 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
32 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
33 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
---|
34 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
---|
35 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
---|
36 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
---|
37 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
38 | */ |
---|
39 | |
---|
40 | #include <stdio.h> |
---|
41 | #include <unistd.h> |
---|
42 | #include <stdlib.h> |
---|
43 | #include <string.h> |
---|
44 | #include <signal.h> |
---|
45 | #include <time.h> |
---|
46 | #include <sys/param.h> |
---|
47 | #include <sys/types.h> |
---|
48 | #include <sys/time.h> |
---|
49 | #include <termios.h> |
---|
50 | #include <sys/stat.h> |
---|
51 | #include "owl.h" |
---|
52 | |
---|
53 | #if OWL_STDERR_REDIR |
---|
54 | #ifdef HAVE_SYS_IOCTL_H |
---|
55 | #include <sys/ioctl.h> |
---|
56 | #endif |
---|
57 | #ifdef HAVE_SYS_FILIO_H |
---|
58 | #include <sys/filio.h> |
---|
59 | #endif |
---|
60 | int stderr_replace(void); |
---|
61 | #endif |
---|
62 | |
---|
63 | #define STDIN 0 |
---|
64 | |
---|
65 | static const char fileIdent[] = "$Id$"; |
---|
66 | |
---|
67 | owl_global g; |
---|
68 | |
---|
69 | int main(int argc, char **argv, char **env) |
---|
70 | { |
---|
71 | WINDOW *recwin, *sepwin, *typwin, *msgwin; |
---|
72 | owl_editwin *tw; |
---|
73 | owl_popwin *pw; |
---|
74 | int ret, initialsubs, debug, argcsave, followlast; |
---|
75 | int newmsgs, nexttimediff; |
---|
76 | struct sigaction sigact; |
---|
77 | char *configfile, *tty, *perlout, *perlerr, **argvsave, buff[LINE], startupmsg[LINE]; |
---|
78 | char *confdir; |
---|
79 | owl_filter *f; |
---|
80 | owl_style *s; |
---|
81 | time_t nexttime, now; |
---|
82 | struct tm *today; |
---|
83 | char *dir; |
---|
84 | struct termios tio; |
---|
85 | owl_message *m; |
---|
86 | #if OWL_STDERR_REDIR |
---|
87 | int newstderr; |
---|
88 | #endif |
---|
89 | |
---|
90 | argcsave=argc; |
---|
91 | argvsave=argv; |
---|
92 | configfile=NULL; |
---|
93 | confdir = NULL; |
---|
94 | tty=NULL; |
---|
95 | debug=0; |
---|
96 | initialsubs=1; |
---|
97 | if (argc>0) { |
---|
98 | argv++; |
---|
99 | argc--; |
---|
100 | } |
---|
101 | while (argc>0) { |
---|
102 | if (!strcmp(argv[0], "-n")) { |
---|
103 | initialsubs=0; |
---|
104 | argv++; |
---|
105 | argc--; |
---|
106 | } else if (!strcmp(argv[0], "-c")) { |
---|
107 | if (argc<2) { |
---|
108 | fprintf(stderr, "Too few arguments to -c\n"); |
---|
109 | usage(); |
---|
110 | exit(1); |
---|
111 | } |
---|
112 | configfile=argv[1]; |
---|
113 | argv+=2; |
---|
114 | argc-=2; |
---|
115 | } else if (!strcmp(argv[0], "-t")) { |
---|
116 | if (argc<2) { |
---|
117 | fprintf(stderr, "Too few arguments to -t\n"); |
---|
118 | usage(); |
---|
119 | exit(1); |
---|
120 | } |
---|
121 | tty=argv[1]; |
---|
122 | argv+=2; |
---|
123 | argc-=2; |
---|
124 | } else if (!strcmp(argv[0], "-s")){ |
---|
125 | if (argc<2) { |
---|
126 | fprintf(stderr, "Too few arguments to -s\n"); |
---|
127 | usage(); |
---|
128 | exit(1); |
---|
129 | } |
---|
130 | confdir = argv[1]; |
---|
131 | argv+=2; |
---|
132 | argc-=2; |
---|
133 | } else if (!strcmp(argv[0], "-d")) { |
---|
134 | debug=1; |
---|
135 | argv++; |
---|
136 | argc--; |
---|
137 | } else if (!strcmp(argv[0], "-D")) { |
---|
138 | debug=1; |
---|
139 | unlink(OWL_DEBUG_FILE); |
---|
140 | argv++; |
---|
141 | argc--; |
---|
142 | } else if (!strcmp(argv[0], "-v")) { |
---|
143 | printf("This is barnowl version %s\n", OWL_VERSION_STRING); |
---|
144 | exit(0); |
---|
145 | } else { |
---|
146 | fprintf(stderr, "Uknown argument\n"); |
---|
147 | usage(); |
---|
148 | exit(1); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | owl_function_debugmsg("startup: Finished parsing arguments"); |
---|
153 | |
---|
154 | /* signal handler */ |
---|
155 | /*sigact.sa_handler=sig_handler;*/ |
---|
156 | sigact.sa_sigaction=sig_handler; |
---|
157 | sigemptyset(&sigact.sa_mask); |
---|
158 | sigact.sa_flags=SA_SIGINFO; |
---|
159 | sigaction(SIGWINCH, &sigact, NULL); |
---|
160 | sigaction(SIGALRM, &sigact, NULL); |
---|
161 | sigaction(SIGPIPE, &sigact, NULL); |
---|
162 | sigaction(SIGTERM, &sigact, NULL); |
---|
163 | sigaction(SIGHUP, &sigact, NULL); |
---|
164 | |
---|
165 | /* save initial terminal settings */ |
---|
166 | tcgetattr(0, owl_global_get_startup_tio(&g)); |
---|
167 | |
---|
168 | /* turn ISTRIP off */ |
---|
169 | tcgetattr(0, &tio); |
---|
170 | tio.c_iflag &= ~ISTRIP; |
---|
171 | tcsetattr(0, TCSAFLUSH, &tio); |
---|
172 | |
---|
173 | /* screen init */ |
---|
174 | if (!getenv("TERMINFO")) { |
---|
175 | owl_function_debugmsg("startup: Not setting TERMINFO"); |
---|
176 | } else { |
---|
177 | owl_function_debugmsg("startup: leaving TERMINFO as %s from envrionment", getenv("TERMINFO")); |
---|
178 | } |
---|
179 | initscr(); |
---|
180 | start_color(); |
---|
181 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
182 | use_default_colors(); |
---|
183 | #endif |
---|
184 | raw(); |
---|
185 | noecho(); |
---|
186 | |
---|
187 | /* define simple color pairs */ |
---|
188 | if (has_colors() && COLOR_PAIRS>=8) { |
---|
189 | int bg = COLOR_BLACK; |
---|
190 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
191 | bg = -1; |
---|
192 | #endif |
---|
193 | init_pair(OWL_COLOR_BLACK, COLOR_BLACK, bg); |
---|
194 | init_pair(OWL_COLOR_RED, COLOR_RED, bg); |
---|
195 | init_pair(OWL_COLOR_GREEN, COLOR_GREEN, bg); |
---|
196 | init_pair(OWL_COLOR_YELLOW, COLOR_YELLOW, bg); |
---|
197 | init_pair(OWL_COLOR_BLUE, COLOR_BLUE, bg); |
---|
198 | init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, bg); |
---|
199 | init_pair(OWL_COLOR_CYAN, COLOR_CYAN, bg); |
---|
200 | init_pair(OWL_COLOR_WHITE, COLOR_WHITE, bg); |
---|
201 | } |
---|
202 | |
---|
203 | /* owl global init */ |
---|
204 | owl_global_init(&g); |
---|
205 | if (debug) owl_global_set_debug_on(&g); |
---|
206 | if (confdir) owl_global_set_confdir(&g, confdir); |
---|
207 | owl_function_debugmsg("startup: first available debugging message"); |
---|
208 | owl_global_set_startupargs(&g, argcsave, argvsave); |
---|
209 | owl_global_set_haveaim(&g); |
---|
210 | |
---|
211 | /* prepare stdin dispatch */ |
---|
212 | { |
---|
213 | owl_dispatch *d = owl_malloc(sizeof(owl_dispatch)); |
---|
214 | d->fd = STDIN; |
---|
215 | d->cfunc = &owl_process_input; |
---|
216 | d->pfunc = NULL; |
---|
217 | owl_select_add_dispatch(d); |
---|
218 | } |
---|
219 | |
---|
220 | #ifdef HAVE_LIBZEPHYR |
---|
221 | /* zephyr init */ |
---|
222 | ret=owl_zephyr_initialize(); |
---|
223 | if (!ret) { |
---|
224 | owl_dispatch *d = owl_malloc(sizeof(owl_dispatch)); |
---|
225 | d->fd = ZGetFD(); |
---|
226 | d->cfunc = &owl_zephyr_process_events; |
---|
227 | d->pfunc = NULL; |
---|
228 | owl_select_add_dispatch(d); |
---|
229 | owl_global_set_havezephyr(&g); |
---|
230 | } |
---|
231 | |
---|
232 | #endif |
---|
233 | |
---|
234 | #if OWL_STDERR_REDIR |
---|
235 | /* Do this only after we've started curses up... */ |
---|
236 | owl_function_debugmsg("startup: doing stderr redirection"); |
---|
237 | newstderr = stderr_replace(); |
---|
238 | owl_muxevents_add(owl_global_get_muxevents(&g), newstderr, OWL_MUX_READ, |
---|
239 | stderr_redirect_handler, NULL); |
---|
240 | #endif |
---|
241 | |
---|
242 | /* create the owl directory, in case it does not exist */ |
---|
243 | owl_function_debugmsg("startup: creating owl directory, if not present"); |
---|
244 | dir=owl_global_get_confdir(&g); |
---|
245 | mkdir(dir, S_IRWXU); |
---|
246 | |
---|
247 | /* set the tty, either from the command line, or by figuring it out */ |
---|
248 | owl_function_debugmsg("startup: setting tty name"); |
---|
249 | if (tty) { |
---|
250 | owl_global_set_tty(&g, tty); |
---|
251 | } else { |
---|
252 | owl_global_set_tty(&g, owl_util_get_default_tty()); |
---|
253 | } |
---|
254 | |
---|
255 | /* Initialize perl */ |
---|
256 | owl_function_debugmsg("startup: processing config file"); |
---|
257 | owl_context_set_readconfig(owl_global_get_context(&g)); |
---|
258 | perlerr=owl_perlconfig_initperl(configfile); |
---|
259 | if (perlerr) { |
---|
260 | endwin(); |
---|
261 | owl_function_error("Internal perl error: %s\n", perlerr); |
---|
262 | fprintf(stderr, "Internal perl error: %s\n", perlerr); |
---|
263 | fflush(stderr); |
---|
264 | printf("Internal perl error: %s\n", perlerr); |
---|
265 | fflush(stdout); |
---|
266 | exit(1); |
---|
267 | } |
---|
268 | |
---|
269 | /* setup the built-in styles */ |
---|
270 | owl_function_debugmsg("startup: creating built-in styles"); |
---|
271 | |
---|
272 | s=owl_malloc(sizeof(owl_style)); |
---|
273 | owl_style_create_internal(s, "basic", &owl_stylefunc_basic, "Basic message formatting."); |
---|
274 | owl_global_add_style(&g, s); |
---|
275 | |
---|
276 | /* setup the default filters */ |
---|
277 | /* the personal filter will need to change again when AIM chat's are |
---|
278 | * included. Also, there should be an %aimme% */ |
---|
279 | owl_function_debugmsg("startup: creating default filters"); |
---|
280 | f=owl_malloc(sizeof(owl_filter)); |
---|
281 | owl_filter_init_fromstring(f, "personal", "isprivate ^true$ and ( not type ^zephyr$" |
---|
282 | " or ( class ^message and" |
---|
283 | " ( instance ^personal$ or instance ^urgent$ ) ) )"); |
---|
284 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
285 | |
---|
286 | f=owl_malloc(sizeof(owl_filter)); |
---|
287 | owl_filter_init_fromstring(f, "wordwrap", "not ( type ^admin$ or type ^zephyr$ ) "); |
---|
288 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
289 | |
---|
290 | f=owl_malloc(sizeof(owl_filter)); |
---|
291 | owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )"); |
---|
292 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
293 | |
---|
294 | f=owl_malloc(sizeof(owl_filter)); |
---|
295 | owl_filter_init_fromstring(f, "ping", "opcode ^ping$"); |
---|
296 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
297 | |
---|
298 | f=owl_malloc(sizeof(owl_filter)); |
---|
299 | owl_filter_init_fromstring(f, "auto", "opcode ^auto$"); |
---|
300 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
301 | |
---|
302 | f=owl_malloc(sizeof(owl_filter)); |
---|
303 | owl_filter_init_fromstring(f, "login", "not login ^none$"); |
---|
304 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
305 | |
---|
306 | f=owl_malloc(sizeof(owl_filter)); |
---|
307 | owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$"); |
---|
308 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
309 | |
---|
310 | f=owl_malloc(sizeof(owl_filter)); |
---|
311 | owl_filter_init_fromstring(f, "out", "direction ^out$"); |
---|
312 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
313 | |
---|
314 | f=owl_malloc(sizeof(owl_filter)); |
---|
315 | owl_filter_init_fromstring(f, "aim", "type ^aim$"); |
---|
316 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
317 | |
---|
318 | f=owl_malloc(sizeof(owl_filter)); |
---|
319 | owl_filter_init_fromstring(f, "zephyr", "type ^zephyr$"); |
---|
320 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
321 | |
---|
322 | f=owl_malloc(sizeof(owl_filter)); |
---|
323 | owl_filter_init_fromstring(f, "none", "false"); |
---|
324 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
325 | |
---|
326 | f=owl_malloc(sizeof(owl_filter)); |
---|
327 | owl_filter_init_fromstring(f, "all", "true"); |
---|
328 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
329 | |
---|
330 | /* set the current view */ |
---|
331 | owl_function_debugmsg("startup: setting the current view"); |
---|
332 | owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default")); |
---|
333 | |
---|
334 | /* AIM init */ |
---|
335 | owl_function_debugmsg("startup: doing AIM initialization"); |
---|
336 | owl_aim_init(); |
---|
337 | |
---|
338 | /* execute the startup function in the configfile */ |
---|
339 | owl_function_debugmsg("startup: executing perl startup, if applicable"); |
---|
340 | perlout = owl_perlconfig_execute("BarnOwl::Hooks::_startup();"); |
---|
341 | if (perlout) owl_free(perlout); |
---|
342 | |
---|
343 | /* hold on to the window names for convenience */ |
---|
344 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
345 | recwin=owl_global_get_curs_recwin(&g); |
---|
346 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
347 | typwin=owl_global_get_curs_typwin(&g); |
---|
348 | tw=owl_global_get_typwin(&g); |
---|
349 | |
---|
350 | /* welcome message */ |
---|
351 | owl_function_debugmsg("startup: creating splash message"); |
---|
352 | strcpy(startupmsg, "-----------------------------------------------------------------------\n"); |
---|
353 | sprintf(buff, "Welcome to barnowl version %s. Press 'h' for on-line help. \n", OWL_VERSION_STRING); |
---|
354 | strcat(startupmsg, buff); |
---|
355 | strcat(startupmsg, " \n"); |
---|
356 | strcat(startupmsg, "This is a development build of barnowl. If you are using this \n"); |
---|
357 | strcat(startupmsg, "build regularly, please add yourself to barnowl-users@mit.edu \n"); |
---|
358 | strcat(startupmsg, " ^ ^ \n"); |
---|
359 | strcat(startupmsg, " OvO \n"); |
---|
360 | strcat(startupmsg, "Please report any bugs to dirty-owl-hackers@mit.edu ( ) \n"); |
---|
361 | strcat(startupmsg, "-----------------------------------------------------------------m-m---\n"); |
---|
362 | owl_function_adminmsg("", startupmsg); |
---|
363 | sepbar(NULL); |
---|
364 | |
---|
365 | /* process the startup file */ |
---|
366 | owl_function_debugmsg("startup: processing startup file"); |
---|
367 | owl_function_source(NULL); |
---|
368 | |
---|
369 | wrefresh(sepwin); |
---|
370 | |
---|
371 | /* load zephyr subs */ |
---|
372 | if (initialsubs) { |
---|
373 | int ret2; |
---|
374 | owl_function_debugmsg("startup: loading initial zephyr subs"); |
---|
375 | |
---|
376 | /* load default subscriptions */ |
---|
377 | ret=owl_zephyr_loaddefaultsubs(); |
---|
378 | |
---|
379 | /* load subscriptions from subs file */ |
---|
380 | ret2=owl_zephyr_loadsubs(NULL, 0); |
---|
381 | |
---|
382 | if (ret || ret2) { |
---|
383 | owl_function_adminmsg("", "Error loading zephyr subscriptions"); |
---|
384 | } else if (ret2!=-1) { |
---|
385 | owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES); |
---|
386 | } |
---|
387 | |
---|
388 | /* load login subscriptions */ |
---|
389 | if (owl_global_is_loginsubs(&g)) { |
---|
390 | owl_function_debugmsg("startup: loading login subs"); |
---|
391 | owl_function_loadloginsubs(NULL); |
---|
392 | } |
---|
393 | } |
---|
394 | |
---|
395 | /* First buddy check to sync the list without notifications */ |
---|
396 | owl_function_debugmsg("startup: doing initial zephyr buddy check"); |
---|
397 | /* owl_function_zephyr_buddy_check(0); */ |
---|
398 | |
---|
399 | /* set the startup and default style, based on userclue and presence of a |
---|
400 | * formatting function */ |
---|
401 | owl_function_debugmsg("startup: setting startup and default style"); |
---|
402 | if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) { |
---|
403 | /* the style was set by the user: leave it alone */ |
---|
404 | } else if (owl_global_is_config_format(&g)) { |
---|
405 | owl_global_set_default_style(&g, "perl"); |
---|
406 | } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) { |
---|
407 | owl_global_set_default_style(&g, "default"); |
---|
408 | } else { |
---|
409 | owl_global_set_default_style(&g, "basic"); |
---|
410 | } |
---|
411 | |
---|
412 | /* zlog in if we need to */ |
---|
413 | if (owl_global_is_startuplogin(&g)) { |
---|
414 | owl_function_debugmsg("startup: doing zlog in"); |
---|
415 | owl_zephyr_zlog_in(); |
---|
416 | } |
---|
417 | |
---|
418 | owl_function_debugmsg("startup: set style for the view: %s", owl_global_get_default_style(&g)); |
---|
419 | s = owl_global_get_style_by_name(&g, owl_global_get_default_style(&g)); |
---|
420 | if(s) |
---|
421 | owl_view_set_style(owl_global_get_current_view(&g), s); |
---|
422 | else |
---|
423 | owl_function_error("No such style: %s", owl_global_get_default_style(&g)); |
---|
424 | |
---|
425 | owl_function_debugmsg("startup: setting context interactive"); |
---|
426 | owl_context_set_interactive(owl_global_get_context(&g)); |
---|
427 | |
---|
428 | nexttimediff=10; |
---|
429 | nexttime=time(NULL); |
---|
430 | |
---|
431 | owl_function_debugmsg("startup: entering main loop"); |
---|
432 | /* main loop */ |
---|
433 | while (1) { |
---|
434 | |
---|
435 | /* if a resize has been scheduled, deal with it */ |
---|
436 | owl_global_resize(&g, 0, 0); |
---|
437 | |
---|
438 | /* these are here in case a resize changes the windows */ |
---|
439 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
440 | recwin=owl_global_get_curs_recwin(&g); |
---|
441 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
442 | typwin=owl_global_get_curs_typwin(&g); |
---|
443 | |
---|
444 | followlast=owl_global_should_followlast(&g); |
---|
445 | |
---|
446 | owl_perlconfig_mainloop(); |
---|
447 | |
---|
448 | /* little hack */ |
---|
449 | now=time(NULL); |
---|
450 | today=localtime(&now); |
---|
451 | if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) { |
---|
452 | if (time(NULL)>nexttime) { |
---|
453 | if (nexttimediff==1) { |
---|
454 | nexttimediff=10; |
---|
455 | } else { |
---|
456 | nexttimediff=1; |
---|
457 | } |
---|
458 | nexttime+=nexttimediff; |
---|
459 | owl_hack_animate(); |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | /* Grab incoming messages. */ |
---|
464 | newmsgs=0; |
---|
465 | while(owl_global_messagequeue_pending(&g)) { |
---|
466 | |
---|
467 | m = owl_global_messagequeue_popmsg(&g); |
---|
468 | |
---|
469 | if(owl_process_message(m)) |
---|
470 | newmsgs = 1; |
---|
471 | } |
---|
472 | |
---|
473 | /* is it time to check zbuddies? */ |
---|
474 | if (owl_global_is_pseudologins(&g)) { |
---|
475 | if (owl_timer_is_expired(owl_global_get_zephyr_buddycheck_timer(&g))) { |
---|
476 | owl_function_debugmsg("Doing zephyr buddy check"); |
---|
477 | owl_function_zephyr_buddy_check(1); |
---|
478 | owl_timer_reset(owl_global_get_zephyr_buddycheck_timer(&g)); |
---|
479 | } |
---|
480 | } |
---|
481 | |
---|
482 | /* dispatch any muxevents */ |
---|
483 | owl_muxevents_dispatch(owl_global_get_muxevents(&g), 0); |
---|
484 | |
---|
485 | /* follow the last message if we're supposed to */ |
---|
486 | if (newmsgs && followlast) { |
---|
487 | owl_function_lastmsg_noredisplay(); |
---|
488 | } |
---|
489 | |
---|
490 | /* do the newmsgproc thing */ |
---|
491 | if (newmsgs) { |
---|
492 | owl_function_do_newmsgproc(); |
---|
493 | } |
---|
494 | |
---|
495 | /* redisplay if necessary */ |
---|
496 | /* this should be optimized to not run if the new messages won't be displayed */ |
---|
497 | if (newmsgs) { |
---|
498 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
499 | sepbar(NULL); |
---|
500 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
501 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
502 | /* TODO: this is a broken kludge */ |
---|
503 | if (owl_global_get_viewwin(&g)) { |
---|
504 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
505 | } |
---|
506 | } |
---|
507 | owl_global_set_needrefresh(&g); |
---|
508 | } |
---|
509 | |
---|
510 | /* if a popwin just came up, refresh it */ |
---|
511 | pw=owl_global_get_popwin(&g); |
---|
512 | if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) { |
---|
513 | owl_popwin_refresh(pw); |
---|
514 | owl_popwin_no_needs_first_refresh(pw); |
---|
515 | owl_global_set_needrefresh(&g); |
---|
516 | /* TODO: this is a broken kludge */ |
---|
517 | if (owl_global_get_viewwin(&g)) { |
---|
518 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
519 | } |
---|
520 | } |
---|
521 | |
---|
522 | /* update the terminal if we need to */ |
---|
523 | if (owl_global_is_needrefresh(&g)) { |
---|
524 | /* leave the cursor in the appropriate window */ |
---|
525 | if (owl_global_is_typwin_active(&g)) { |
---|
526 | owl_function_set_cursor(typwin); |
---|
527 | } else { |
---|
528 | owl_function_set_cursor(sepwin); |
---|
529 | } |
---|
530 | doupdate(); |
---|
531 | owl_global_set_noneedrefresh(&g); |
---|
532 | } |
---|
533 | |
---|
534 | /* Handle all keypresses. If no key has been pressed, sleep for a |
---|
535 | * little bit, but otherwise do not. This lets input be grabbed |
---|
536 | * as quickly as possbile */ |
---|
537 | |
---|
538 | /* select on FDs we know about. */ |
---|
539 | owl_select(); |
---|
540 | |
---|
541 | /* Log any error signals */ |
---|
542 | { |
---|
543 | siginfo_t si; |
---|
544 | int signum; |
---|
545 | if ((signum = owl_global_get_errsignal_and_clear(&g, &si)) > 0) { |
---|
546 | owl_function_error("Got unexpected signal: %d %s (code: %d band: %d errno: %d)", |
---|
547 | signum, signum==SIGPIPE?"SIGPIPE":"SIG????", |
---|
548 | si.si_code, si.si_band, si.si_errno); |
---|
549 | } |
---|
550 | } |
---|
551 | |
---|
552 | } |
---|
553 | } |
---|
554 | |
---|
555 | /* |
---|
556 | * Process a new message passed to us on the message queue from some |
---|
557 | * protocol. This includes adding it to the message list, updating the |
---|
558 | * view and scrolling if appropriate, logging it, and so on. |
---|
559 | * |
---|
560 | * Either a pointer is kept to the message internally, or it is freed |
---|
561 | * if unneeded. The caller no longer ``owns'' the message's memory. |
---|
562 | * |
---|
563 | * Returns 1 if the message was added to the message list, and 0 if it |
---|
564 | * was ignored due to user settings or otherwise. |
---|
565 | */ |
---|
566 | int owl_process_message(owl_message *m) { |
---|
567 | owl_filter *f; |
---|
568 | /* if this message it on the puntlist, nuke it and continue */ |
---|
569 | if (owl_global_message_is_puntable(&g, m)) { |
---|
570 | owl_message_free(m); |
---|
571 | return 0; |
---|
572 | } |
---|
573 | |
---|
574 | /* login or logout that should be ignored? */ |
---|
575 | if (owl_global_is_ignorelogins(&g) |
---|
576 | && owl_message_is_loginout(m)) { |
---|
577 | owl_message_free(m); |
---|
578 | return 0; |
---|
579 | } |
---|
580 | |
---|
581 | if (!owl_global_is_displayoutgoing(&g) |
---|
582 | && owl_message_is_direction_out(m)) { |
---|
583 | owl_message_free(m); |
---|
584 | return 0; |
---|
585 | } |
---|
586 | |
---|
587 | /* add it to the global list */ |
---|
588 | owl_messagelist_append_element(owl_global_get_msglist(&g), m); |
---|
589 | /* add it to any necessary views; right now there's only the current view */ |
---|
590 | owl_view_consider_message(owl_global_get_current_view(&g), m); |
---|
591 | |
---|
592 | if(owl_message_is_direction_in(m)) { |
---|
593 | /* let perl know about it*/ |
---|
594 | owl_perlconfig_getmsg(m, 0, NULL); |
---|
595 | |
---|
596 | /* do we need to autoreply? */ |
---|
597 | if (owl_global_is_zaway(&g) && !owl_message_get_attribute_value(m, "isauto")) { |
---|
598 | if (owl_message_is_type_zephyr(m)) { |
---|
599 | owl_zephyr_zaway(m); |
---|
600 | } else if (owl_message_is_type_aim(m)) { |
---|
601 | if (owl_message_is_private(m)) { |
---|
602 | owl_function_send_aimawymsg(owl_message_get_sender(m), owl_global_get_zaway_msg(&g)); |
---|
603 | } |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | /* ring the bell if it's a personal */ |
---|
608 | if (!strcmp(owl_global_get_personalbell(&g), "on")) { |
---|
609 | if (!owl_message_is_loginout(m) && |
---|
610 | !owl_message_is_mail(m) && |
---|
611 | owl_message_is_personal(m)) { |
---|
612 | owl_function_beep(); |
---|
613 | } |
---|
614 | } else if (!strcmp(owl_global_get_personalbell(&g), "off")) { |
---|
615 | /* do nothing */ |
---|
616 | } else { |
---|
617 | f=owl_global_get_filter(&g, owl_global_get_personalbell(&g)); |
---|
618 | if (f && owl_filter_message_match(f, m)) { |
---|
619 | owl_function_beep(); |
---|
620 | } |
---|
621 | } |
---|
622 | |
---|
623 | /* if it matches the alert filter, do the alert action */ |
---|
624 | f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g)); |
---|
625 | if (f && owl_filter_message_match(f, m)) { |
---|
626 | owl_function_command(owl_global_get_alert_action(&g)); |
---|
627 | } |
---|
628 | |
---|
629 | /* if it's a zephyr login or logout, update the zbuddylist */ |
---|
630 | if (owl_message_is_type_zephyr(m) && owl_message_is_loginout(m)) { |
---|
631 | if (owl_message_is_login(m)) { |
---|
632 | owl_zbuddylist_adduser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m)); |
---|
633 | } else if (owl_message_is_logout(m)) { |
---|
634 | owl_zbuddylist_deluser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m)); |
---|
635 | } else { |
---|
636 | owl_function_error("Internal error: received login notice that is neither login nor logout"); |
---|
637 | } |
---|
638 | } |
---|
639 | |
---|
640 | /* check for burning ears message */ |
---|
641 | /* this is an unsupported feature */ |
---|
642 | if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) { |
---|
643 | char *buff; |
---|
644 | buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m)); |
---|
645 | owl_function_adminmsg(buff, ""); |
---|
646 | owl_free(buff); |
---|
647 | owl_function_beep(); |
---|
648 | } |
---|
649 | } |
---|
650 | |
---|
651 | /* log the message if we need to */ |
---|
652 | owl_log_message(m); |
---|
653 | |
---|
654 | return 1; |
---|
655 | } |
---|
656 | |
---|
657 | void owl_process_aim() |
---|
658 | { |
---|
659 | if (owl_global_is_doaimevents(&g)) { |
---|
660 | owl_aim_process_events(); |
---|
661 | |
---|
662 | if (owl_global_is_aimloggedin(&g)) { |
---|
663 | if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) { |
---|
664 | /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */ |
---|
665 | owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g)); |
---|
666 | } |
---|
667 | } |
---|
668 | } |
---|
669 | } |
---|
670 | |
---|
671 | void owl_process_input() |
---|
672 | { |
---|
673 | int ret, j; |
---|
674 | owl_popwin *pw; |
---|
675 | owl_editwin *tw; |
---|
676 | |
---|
677 | j = wgetch(owl_global_get_curs_typwin(&g)); |
---|
678 | if (j == ERR) return; |
---|
679 | |
---|
680 | owl_global_set_lastinputtime(&g, time(NULL)); |
---|
681 | pw=owl_global_get_popwin(&g); |
---|
682 | tw=owl_global_get_typwin(&g); |
---|
683 | |
---|
684 | /* find and activate the current keymap. |
---|
685 | * TODO: this should really get fixed by activating |
---|
686 | * keymaps as we switch between windows... |
---|
687 | */ |
---|
688 | if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) { |
---|
689 | owl_context_set_popless(owl_global_get_context(&g), |
---|
690 | owl_global_get_viewwin(&g)); |
---|
691 | owl_function_activate_keymap("popless"); |
---|
692 | } else if (owl_global_is_typwin_active(&g) |
---|
693 | && owl_editwin_get_style(tw) == OWL_EDITWIN_STYLE_ONELINE) { |
---|
694 | /* |
---|
695 | owl_context_set_editline(owl_global_get_context(&g), tw); |
---|
696 | owl_function_activate_keymap("editline"); |
---|
697 | */ |
---|
698 | } else if (owl_global_is_typwin_active(&g) |
---|
699 | && owl_editwin_get_style(tw) == OWL_EDITWIN_STYLE_MULTILINE) { |
---|
700 | owl_context_set_editmulti(owl_global_get_context(&g), tw); |
---|
701 | owl_function_activate_keymap("editmulti"); |
---|
702 | } else { |
---|
703 | owl_context_set_recv(owl_global_get_context(&g)); |
---|
704 | owl_function_activate_keymap("recv"); |
---|
705 | } |
---|
706 | /* now actually handle the keypress */ |
---|
707 | ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j); |
---|
708 | if (ret != 0 && ret != 1) { |
---|
709 | owl_function_makemsg("Unable to handle keypress"); |
---|
710 | } |
---|
711 | } |
---|
712 | |
---|
713 | void sig_handler(int sig, siginfo_t *si, void *data) |
---|
714 | { |
---|
715 | if (sig==SIGWINCH) { |
---|
716 | /* we can't inturrupt a malloc here, so it just sets a flag |
---|
717 | * schedulding a resize for later |
---|
718 | */ |
---|
719 | owl_function_resize(); |
---|
720 | } else if (sig==SIGPIPE || sig==SIGCHLD) { |
---|
721 | /* Set a flag and some info that we got the sigpipe |
---|
722 | * so we can record that we got it and why... */ |
---|
723 | owl_global_set_errsignal(&g, sig, si); |
---|
724 | } else if (sig==SIGTERM || sig==SIGHUP) { |
---|
725 | owl_function_quit(); |
---|
726 | } |
---|
727 | } |
---|
728 | |
---|
729 | void usage() |
---|
730 | { |
---|
731 | fprintf(stderr, "Barnowl version %s\n", OWL_VERSION_STRING); |
---|
732 | fprintf(stderr, "Usage: barnowl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-s <confdir>] [-t <ttyname>]\n"); |
---|
733 | fprintf(stderr, " -n don't load zephyr subscriptions\n"); |
---|
734 | fprintf(stderr, " -d enable debugging\n"); |
---|
735 | fprintf(stderr, " -D enable debugging and delete previous debug file\n"); |
---|
736 | fprintf(stderr, " -v print the Barnowl version number and exit\n"); |
---|
737 | fprintf(stderr, " -h print this help message\n"); |
---|
738 | fprintf(stderr, " -c specify an alternate config file\n"); |
---|
739 | fprintf(stderr, " -s specify an alternate config dir (default ~/.owl)\n"); |
---|
740 | fprintf(stderr, " -t set the tty name\n"); |
---|
741 | } |
---|
742 | |
---|
743 | #if OWL_STDERR_REDIR |
---|
744 | |
---|
745 | /* Replaces stderr with a pipe so that we can read from it. |
---|
746 | * Returns the fd of the pipe from which stderr can be read. */ |
---|
747 | int stderr_replace(void) |
---|
748 | { |
---|
749 | int pipefds[2]; |
---|
750 | if (0 != pipe(pipefds)) { |
---|
751 | perror("pipe"); |
---|
752 | owl_function_debugmsg("stderr_replace: pipe FAILED\n"); |
---|
753 | return -1; |
---|
754 | } |
---|
755 | owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]); |
---|
756 | if (-1 == dup2(pipefds[1], 2 /*stderr*/)) { |
---|
757 | owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno)); |
---|
758 | perror("dup2"); |
---|
759 | return -1; |
---|
760 | } |
---|
761 | return pipefds[0]; |
---|
762 | } |
---|
763 | |
---|
764 | /* Sends stderr (read from rfd) messages to the error console */ |
---|
765 | void stderr_redirect_handler(int handle, int rfd, int eventmask, void *data) |
---|
766 | { |
---|
767 | int navail, bread; |
---|
768 | char *buf; |
---|
769 | /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/ |
---|
770 | if (rfd<0) return; |
---|
771 | if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) { |
---|
772 | return; |
---|
773 | } |
---|
774 | /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/ |
---|
775 | if (navail<=0) return; |
---|
776 | if (navail>256) { navail = 256; } |
---|
777 | buf = owl_malloc(navail+1); |
---|
778 | bread = read(rfd, buf, navail); |
---|
779 | if (buf[navail-1] != '\0') { |
---|
780 | buf[navail] = '\0'; |
---|
781 | } |
---|
782 | owl_function_error("Err: %s", buf); |
---|
783 | owl_free(buf); |
---|
784 | } |
---|
785 | |
---|
786 | #endif /* OWL_STDERR_REDIR */ |
---|