1 | /* Copyright (c) 2006-2009 The BarnOwl Developers. All rights reserved. |
---|
2 | * Copyright (c) 2004 James Kretchmar. All rights reserved. |
---|
3 | * |
---|
4 | * This program is free software. You can redistribute it and/or |
---|
5 | * modify under the terms of the Sleepycat License. See the COPYING |
---|
6 | * file included with the distribution for more information. |
---|
7 | */ |
---|
8 | |
---|
9 | #include <stdio.h> |
---|
10 | #include <unistd.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #include <string.h> |
---|
13 | #include <signal.h> |
---|
14 | #include <time.h> |
---|
15 | #include <sys/param.h> |
---|
16 | #include <sys/types.h> |
---|
17 | #include <sys/time.h> |
---|
18 | #include <termios.h> |
---|
19 | #include <sys/stat.h> |
---|
20 | #include <locale.h> |
---|
21 | #include "owl.h" |
---|
22 | |
---|
23 | |
---|
24 | #if OWL_STDERR_REDIR |
---|
25 | #ifdef HAVE_SYS_IOCTL_H |
---|
26 | #include <sys/ioctl.h> |
---|
27 | #endif |
---|
28 | #ifdef HAVE_SYS_FILIO_H |
---|
29 | #include <sys/filio.h> |
---|
30 | #endif |
---|
31 | int stderr_replace(void); |
---|
32 | #endif |
---|
33 | |
---|
34 | #define STDIN 0 |
---|
35 | |
---|
36 | owl_global g; |
---|
37 | |
---|
38 | int main(int argc, char **argv, char **env) |
---|
39 | { |
---|
40 | WINDOW *recwin, *sepwin, *typwin, *msgwin; |
---|
41 | owl_editwin *tw; |
---|
42 | owl_popwin *pw; |
---|
43 | int debug, argcsave, followlast; |
---|
44 | int newmsgs, nexttimediff; |
---|
45 | struct sigaction sigact; |
---|
46 | const char *configfile, *tty, *const *argvsave; |
---|
47 | char *perlout, *perlerr; |
---|
48 | const char *confdir; |
---|
49 | owl_filter *f; |
---|
50 | owl_style *s; |
---|
51 | time_t nexttime, now; |
---|
52 | struct tm *today; |
---|
53 | const char *dir; |
---|
54 | struct termios tio; |
---|
55 | owl_message *m; |
---|
56 | |
---|
57 | if (!GLIB_CHECK_VERSION (2, 12, 0)) |
---|
58 | g_error ("GLib version 2.12.0 or above is needed."); |
---|
59 | |
---|
60 | argcsave=argc; |
---|
61 | argvsave=strs(argv); |
---|
62 | configfile=NULL; |
---|
63 | confdir = NULL; |
---|
64 | tty=NULL; |
---|
65 | debug=0; |
---|
66 | g.load_initial_subs = 1; |
---|
67 | |
---|
68 | setlocale(LC_ALL, ""); |
---|
69 | |
---|
70 | if (argc>0) { |
---|
71 | argv++; |
---|
72 | argc--; |
---|
73 | } |
---|
74 | while (argc>0) { |
---|
75 | if (!strcmp(argv[0], "-n")) { |
---|
76 | g.load_initial_subs = 0; |
---|
77 | argv++; |
---|
78 | argc--; |
---|
79 | } else if (!strcmp(argv[0], "-c")) { |
---|
80 | if (argc<2) { |
---|
81 | fprintf(stderr, "Too few arguments to -c\n"); |
---|
82 | usage(); |
---|
83 | exit(1); |
---|
84 | } |
---|
85 | configfile=argv[1]; |
---|
86 | argv+=2; |
---|
87 | argc-=2; |
---|
88 | } else if (!strcmp(argv[0], "-t")) { |
---|
89 | if (argc<2) { |
---|
90 | fprintf(stderr, "Too few arguments to -t\n"); |
---|
91 | usage(); |
---|
92 | exit(1); |
---|
93 | } |
---|
94 | tty=argv[1]; |
---|
95 | argv+=2; |
---|
96 | argc-=2; |
---|
97 | } else if (!strcmp(argv[0], "-s")){ |
---|
98 | if (argc<2) { |
---|
99 | fprintf(stderr, "Too few arguments to -s\n"); |
---|
100 | usage(); |
---|
101 | exit(1); |
---|
102 | } |
---|
103 | confdir = argv[1]; |
---|
104 | argv+=2; |
---|
105 | argc-=2; |
---|
106 | } else if (!strcmp(argv[0], "-d")) { |
---|
107 | debug=1; |
---|
108 | argv++; |
---|
109 | argc--; |
---|
110 | } else if (!strcmp(argv[0], "-D")) { |
---|
111 | debug=1; |
---|
112 | unlink(OWL_DEBUG_FILE); |
---|
113 | argv++; |
---|
114 | argc--; |
---|
115 | } else if (!strcmp(argv[0], "-v")) { |
---|
116 | printf("This is barnowl version %s\n", OWL_VERSION_STRING); |
---|
117 | exit(0); |
---|
118 | } else { |
---|
119 | fprintf(stderr, "Unknown argument\n"); |
---|
120 | usage(); |
---|
121 | exit(1); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | owl_function_debugmsg("startup: Finished parsing arguments"); |
---|
126 | |
---|
127 | /* signal handler */ |
---|
128 | /*sigact.sa_handler=sig_handler;*/ |
---|
129 | sigact.sa_sigaction=sig_handler; |
---|
130 | sigemptyset(&sigact.sa_mask); |
---|
131 | sigact.sa_flags=SA_SIGINFO; |
---|
132 | sigaction(SIGWINCH, &sigact, NULL); |
---|
133 | sigaction(SIGALRM, &sigact, NULL); |
---|
134 | sigaction(SIGPIPE, &sigact, NULL); |
---|
135 | sigaction(SIGTERM, &sigact, NULL); |
---|
136 | sigaction(SIGHUP, &sigact, NULL); |
---|
137 | |
---|
138 | sigact.sa_sigaction=sigint_handler; |
---|
139 | sigaction(SIGINT, &sigact, NULL); |
---|
140 | |
---|
141 | /* save initial terminal settings */ |
---|
142 | tcgetattr(0, owl_global_get_startup_tio(&g)); |
---|
143 | |
---|
144 | /* turn ISTRIP off */ |
---|
145 | tcgetattr(0, &tio); |
---|
146 | tio.c_iflag &= ~(ISTRIP|IEXTEN); |
---|
147 | tio.c_cc[VQUIT] = 0; |
---|
148 | tcsetattr(0, TCSAFLUSH, &tio); |
---|
149 | |
---|
150 | /* screen init */ |
---|
151 | if (!getenv("TERMINFO")) { |
---|
152 | owl_function_debugmsg("startup: Not setting TERMINFO"); |
---|
153 | } else { |
---|
154 | owl_function_debugmsg("startup: leaving TERMINFO as %s from envrionment", getenv("TERMINFO")); |
---|
155 | } |
---|
156 | initscr(); |
---|
157 | start_color(); |
---|
158 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
159 | use_default_colors(); |
---|
160 | #endif |
---|
161 | cbreak(); |
---|
162 | noecho(); |
---|
163 | |
---|
164 | /* define simple color pairs */ |
---|
165 | if (has_colors() && COLOR_PAIRS>=8) { |
---|
166 | int bg = COLOR_BLACK; |
---|
167 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
168 | bg = -1; |
---|
169 | #endif |
---|
170 | init_pair(OWL_COLOR_BLACK, COLOR_BLACK, bg); |
---|
171 | init_pair(OWL_COLOR_RED, COLOR_RED, bg); |
---|
172 | init_pair(OWL_COLOR_GREEN, COLOR_GREEN, bg); |
---|
173 | init_pair(OWL_COLOR_YELLOW, COLOR_YELLOW, bg); |
---|
174 | init_pair(OWL_COLOR_BLUE, COLOR_BLUE, bg); |
---|
175 | init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, bg); |
---|
176 | init_pair(OWL_COLOR_CYAN, COLOR_CYAN, bg); |
---|
177 | init_pair(OWL_COLOR_WHITE, COLOR_WHITE, bg); |
---|
178 | } |
---|
179 | |
---|
180 | /* owl global init */ |
---|
181 | owl_global_init(&g); |
---|
182 | if (debug) owl_global_set_debug_on(&g); |
---|
183 | if (confdir) owl_global_set_confdir(&g, confdir); |
---|
184 | owl_function_debugmsg("startup: first available debugging message"); |
---|
185 | owl_global_set_startupargs(&g, argcsave, argvsave); |
---|
186 | owl_global_set_haveaim(&g); |
---|
187 | |
---|
188 | /* prepare stdin dispatch */ |
---|
189 | { |
---|
190 | owl_dispatch *d = owl_malloc(sizeof(owl_dispatch)); |
---|
191 | d->fd = STDIN; |
---|
192 | d->cfunc = &owl_process_input; |
---|
193 | d->destroy = NULL; |
---|
194 | owl_select_add_dispatch(d); |
---|
195 | } |
---|
196 | |
---|
197 | owl_zephyr_initialize(); |
---|
198 | |
---|
199 | #if OWL_STDERR_REDIR |
---|
200 | /* Do this only after we've started curses up... */ |
---|
201 | { |
---|
202 | owl_dispatch *d = owl_malloc(sizeof(owl_dispatch)); |
---|
203 | owl_function_debugmsg("startup: doing stderr redirection"); |
---|
204 | d->fd = stderr_replace(); |
---|
205 | d->cfunc = stderr_redirect_handler; |
---|
206 | d->destroy = NULL; |
---|
207 | owl_select_add_dispatch(d); |
---|
208 | } |
---|
209 | #endif |
---|
210 | |
---|
211 | /* create the owl directory, in case it does not exist */ |
---|
212 | owl_function_debugmsg("startup: creating owl directory, if not present"); |
---|
213 | dir=owl_global_get_confdir(&g); |
---|
214 | mkdir(dir, S_IRWXU); |
---|
215 | |
---|
216 | /* set the tty, either from the command line, or by figuring it out */ |
---|
217 | owl_function_debugmsg("startup: setting tty name"); |
---|
218 | if (tty) { |
---|
219 | owl_global_set_tty(&g, tty); |
---|
220 | } else { |
---|
221 | owl_global_set_tty(&g, owl_util_get_default_tty()); |
---|
222 | } |
---|
223 | |
---|
224 | /* Initialize perl */ |
---|
225 | owl_function_debugmsg("startup: processing config file"); |
---|
226 | owl_context_set_readconfig(owl_global_get_context(&g)); |
---|
227 | perlerr=owl_perlconfig_initperl(configfile, &argc, &argv, &env); |
---|
228 | if (perlerr) { |
---|
229 | endwin(); |
---|
230 | fprintf(stderr, "Internal perl error: %s\n", perlerr); |
---|
231 | fflush(stderr); |
---|
232 | printf("Internal perl error: %s\n", perlerr); |
---|
233 | fflush(stdout); |
---|
234 | exit(1); |
---|
235 | } |
---|
236 | |
---|
237 | owl_global_complete_setup(&g); |
---|
238 | |
---|
239 | /* setup the default filters */ |
---|
240 | /* the personal filter will need to change again when AIM chat's are |
---|
241 | * included. Also, there should be an %aimme% */ |
---|
242 | owl_function_debugmsg("startup: creating default filters"); |
---|
243 | f=owl_malloc(sizeof(owl_filter)); |
---|
244 | owl_filter_init_fromstring(f, "personal", "isprivate ^true$ and ( not type ^zephyr$" |
---|
245 | " or ( class ^message ) )"); |
---|
246 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
247 | |
---|
248 | f=owl_malloc(sizeof(owl_filter)); |
---|
249 | owl_filter_init_fromstring(f, "wordwrap", "not ( type ^admin$ or type ^zephyr$ ) "); |
---|
250 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
251 | |
---|
252 | f=owl_malloc(sizeof(owl_filter)); |
---|
253 | owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )"); |
---|
254 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
255 | |
---|
256 | f=owl_malloc(sizeof(owl_filter)); |
---|
257 | owl_filter_init_fromstring(f, "ping", "opcode ^ping$"); |
---|
258 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
259 | |
---|
260 | f=owl_malloc(sizeof(owl_filter)); |
---|
261 | owl_filter_init_fromstring(f, "auto", "opcode ^auto$"); |
---|
262 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
263 | |
---|
264 | f=owl_malloc(sizeof(owl_filter)); |
---|
265 | owl_filter_init_fromstring(f, "login", "not login ^none$"); |
---|
266 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
267 | |
---|
268 | f=owl_malloc(sizeof(owl_filter)); |
---|
269 | owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$"); |
---|
270 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
271 | |
---|
272 | f=owl_malloc(sizeof(owl_filter)); |
---|
273 | owl_filter_init_fromstring(f, "out", "direction ^out$"); |
---|
274 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
275 | |
---|
276 | f=owl_malloc(sizeof(owl_filter)); |
---|
277 | owl_filter_init_fromstring(f, "aim", "type ^aim$"); |
---|
278 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
279 | |
---|
280 | f=owl_malloc(sizeof(owl_filter)); |
---|
281 | owl_filter_init_fromstring(f, "zephyr", "type ^zephyr$"); |
---|
282 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
283 | |
---|
284 | f=owl_malloc(sizeof(owl_filter)); |
---|
285 | owl_filter_init_fromstring(f, "none", "false"); |
---|
286 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
287 | |
---|
288 | f=owl_malloc(sizeof(owl_filter)); |
---|
289 | owl_filter_init_fromstring(f, "all", "true"); |
---|
290 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
291 | |
---|
292 | /* set the current view */ |
---|
293 | owl_function_debugmsg("startup: setting the current view"); |
---|
294 | owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default")); |
---|
295 | |
---|
296 | /* AIM init */ |
---|
297 | owl_function_debugmsg("startup: doing AIM initialization"); |
---|
298 | owl_aim_init(); |
---|
299 | |
---|
300 | /* execute the startup function in the configfile */ |
---|
301 | owl_function_debugmsg("startup: executing perl startup, if applicable"); |
---|
302 | perlout = owl_perlconfig_execute("BarnOwl::Hooks::_startup();"); |
---|
303 | if (perlout) owl_free(perlout); |
---|
304 | |
---|
305 | /* hold on to the window names for convenience */ |
---|
306 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
307 | recwin=owl_global_get_curs_recwin(&g); |
---|
308 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
309 | typwin=owl_global_get_curs_typwin(&g); |
---|
310 | tw=owl_global_get_typwin(&g); |
---|
311 | |
---|
312 | /* welcome message */ |
---|
313 | owl_function_debugmsg("startup: creating splash message"); |
---|
314 | owl_function_adminmsg("", |
---|
315 | "-----------------------------------------------------------------------\n" |
---|
316 | "Welcome to barnowl version " OWL_VERSION_STRING ". Press 'h' for on-line help.\n" |
---|
317 | "To see a quick introduction, type ':show quickstart'. \n" |
---|
318 | " \n" |
---|
319 | "BarnOwl is free software. Type ':show license' for more \n" |
---|
320 | "information. ^ ^ \n" |
---|
321 | " OvO \n" |
---|
322 | "Please report any bugs or suggestions to bug-barnowl@mit.edu ( ) \n" |
---|
323 | "-----------------------------------------------------------------m-m---\n" |
---|
324 | ); |
---|
325 | sepbar(NULL); |
---|
326 | |
---|
327 | /* process the startup file */ |
---|
328 | owl_function_debugmsg("startup: processing startup file"); |
---|
329 | owl_function_source(NULL); |
---|
330 | |
---|
331 | wrefresh(sepwin); |
---|
332 | |
---|
333 | /* First buddy check to sync the list without notifications */ |
---|
334 | owl_function_debugmsg("startup: doing initial zephyr buddy check"); |
---|
335 | /* owl_function_zephyr_buddy_check(0); */ |
---|
336 | |
---|
337 | /* set the startup and default style, based on userclue and presence of a |
---|
338 | * formatting function */ |
---|
339 | owl_function_debugmsg("startup: setting startup and default style"); |
---|
340 | if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) { |
---|
341 | /* the style was set by the user: leave it alone */ |
---|
342 | } else { |
---|
343 | owl_global_set_default_style(&g, "default"); |
---|
344 | } |
---|
345 | |
---|
346 | owl_function_debugmsg("startup: set style for the view: %s", owl_global_get_default_style(&g)); |
---|
347 | s = owl_global_get_style_by_name(&g, owl_global_get_default_style(&g)); |
---|
348 | if(s) |
---|
349 | owl_view_set_style(owl_global_get_current_view(&g), s); |
---|
350 | else |
---|
351 | owl_function_error("No such style: %s", owl_global_get_default_style(&g)); |
---|
352 | |
---|
353 | owl_function_debugmsg("startup: setting context interactive"); |
---|
354 | owl_context_set_interactive(owl_global_get_context(&g)); |
---|
355 | |
---|
356 | nexttimediff=10; |
---|
357 | nexttime=time(NULL); |
---|
358 | |
---|
359 | |
---|
360 | owl_select_add_timer(180, 180, owl_zephyr_buddycheck_timer, NULL, NULL); |
---|
361 | |
---|
362 | /* If we ever deprecate the mainloop hook, remove this. */ |
---|
363 | owl_select_add_timer(0, 1, owl_perlconfig_mainloop, NULL, NULL); |
---|
364 | |
---|
365 | |
---|
366 | owl_function_debugmsg("startup: entering main loop"); |
---|
367 | /* main loop */ |
---|
368 | while (1) { |
---|
369 | |
---|
370 | /* if a resize has been scheduled, deal with it */ |
---|
371 | owl_global_resize(&g, 0, 0); |
---|
372 | |
---|
373 | /* these are here in case a resize changes the windows */ |
---|
374 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
375 | recwin=owl_global_get_curs_recwin(&g); |
---|
376 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
377 | typwin=owl_global_get_curs_typwin(&g); |
---|
378 | |
---|
379 | followlast=owl_global_should_followlast(&g); |
---|
380 | |
---|
381 | /* little hack */ |
---|
382 | now=time(NULL); |
---|
383 | today=localtime(&now); |
---|
384 | if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) { |
---|
385 | if (time(NULL)>nexttime) { |
---|
386 | if (nexttimediff==1) { |
---|
387 | nexttimediff=10; |
---|
388 | } else { |
---|
389 | nexttimediff=1; |
---|
390 | } |
---|
391 | nexttime+=nexttimediff; |
---|
392 | owl_hack_animate(); |
---|
393 | } |
---|
394 | } |
---|
395 | |
---|
396 | /* Grab incoming messages. */ |
---|
397 | newmsgs=0; |
---|
398 | while(owl_global_messagequeue_pending(&g)) { |
---|
399 | |
---|
400 | m = owl_global_messagequeue_popmsg(&g); |
---|
401 | |
---|
402 | if(owl_process_message(m)) |
---|
403 | newmsgs = 1; |
---|
404 | } |
---|
405 | |
---|
406 | /* follow the last message if we're supposed to */ |
---|
407 | if (newmsgs && followlast) { |
---|
408 | owl_function_lastmsg_noredisplay(); |
---|
409 | } |
---|
410 | |
---|
411 | /* do the newmsgproc thing */ |
---|
412 | if (newmsgs) { |
---|
413 | owl_function_do_newmsgproc(); |
---|
414 | } |
---|
415 | |
---|
416 | /* redisplay if necessary */ |
---|
417 | /* this should be optimized to not run if the new messages won't be displayed */ |
---|
418 | if (newmsgs) { |
---|
419 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
420 | sepbar(NULL); |
---|
421 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
422 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
423 | /* TODO: this is a broken kludge */ |
---|
424 | if (owl_global_get_viewwin(&g)) { |
---|
425 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
426 | } |
---|
427 | } |
---|
428 | owl_global_set_needrefresh(&g); |
---|
429 | } |
---|
430 | |
---|
431 | /* if a popwin just came up, refresh it */ |
---|
432 | pw=owl_global_get_popwin(&g); |
---|
433 | if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) { |
---|
434 | owl_popwin_refresh(pw); |
---|
435 | owl_popwin_no_needs_first_refresh(pw); |
---|
436 | owl_global_set_needrefresh(&g); |
---|
437 | /* TODO: this is a broken kludge */ |
---|
438 | if (owl_global_get_viewwin(&g)) { |
---|
439 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
440 | } |
---|
441 | } |
---|
442 | |
---|
443 | /* update the terminal if we need to */ |
---|
444 | if (owl_global_is_needrefresh(&g)) { |
---|
445 | /* leave the cursor in the appropriate window */ |
---|
446 | if (owl_global_is_typwin_active(&g)) { |
---|
447 | owl_function_set_cursor(typwin); |
---|
448 | } else { |
---|
449 | owl_function_set_cursor(sepwin); |
---|
450 | } |
---|
451 | doupdate(); |
---|
452 | owl_global_set_noneedrefresh(&g); |
---|
453 | } |
---|
454 | |
---|
455 | /* Some calls into libzephyr call Z_WaitForNotice(), which has its |
---|
456 | * own select loop and may leave zephyrs on the queue. Check for |
---|
457 | * them now, and process any we find. */ |
---|
458 | owl_zephyr_process_events(NULL); |
---|
459 | |
---|
460 | /* select on FDs we know about. */ |
---|
461 | owl_select(); |
---|
462 | |
---|
463 | /* Log any error signals */ |
---|
464 | { |
---|
465 | siginfo_t si; |
---|
466 | int signum; |
---|
467 | if ((signum = owl_global_get_errsignal_and_clear(&g, &si)) > 0) { |
---|
468 | owl_function_error("Got unexpected signal: %d %s (code: %d band: %ld errno: %d)", |
---|
469 | signum, signum==SIGPIPE?"SIGPIPE":"SIG????", |
---|
470 | si.si_code, si.si_band, si.si_errno); |
---|
471 | } |
---|
472 | } |
---|
473 | |
---|
474 | } |
---|
475 | } |
---|
476 | |
---|
477 | /* |
---|
478 | * Process a new message passed to us on the message queue from some |
---|
479 | * protocol. This includes adding it to the message list, updating the |
---|
480 | * view and scrolling if appropriate, logging it, and so on. |
---|
481 | * |
---|
482 | * Either a pointer is kept to the message internally, or it is freed |
---|
483 | * if unneeded. The caller no longer ``owns'' the message's memory. |
---|
484 | * |
---|
485 | * Returns 1 if the message was added to the message list, and 0 if it |
---|
486 | * was ignored due to user settings or otherwise. |
---|
487 | */ |
---|
488 | int owl_process_message(owl_message *m) { |
---|
489 | owl_filter *f; |
---|
490 | /* if this message it on the puntlist, nuke it and continue */ |
---|
491 | if (owl_global_message_is_puntable(&g, m)) { |
---|
492 | owl_message_free(m); |
---|
493 | return 0; |
---|
494 | } |
---|
495 | |
---|
496 | /* login or logout that should be ignored? */ |
---|
497 | if (owl_global_is_ignorelogins(&g) |
---|
498 | && owl_message_is_loginout(m)) { |
---|
499 | owl_message_free(m); |
---|
500 | return 0; |
---|
501 | } |
---|
502 | |
---|
503 | if (!owl_global_is_displayoutgoing(&g) |
---|
504 | && owl_message_is_direction_out(m)) { |
---|
505 | owl_message_free(m); |
---|
506 | return 0; |
---|
507 | } |
---|
508 | |
---|
509 | /* add it to the global list */ |
---|
510 | owl_messagelist_append_element(owl_global_get_msglist(&g), m); |
---|
511 | /* add it to any necessary views; right now there's only the current view */ |
---|
512 | owl_view_consider_message(owl_global_get_current_view(&g), m); |
---|
513 | |
---|
514 | if(owl_message_is_direction_in(m)) { |
---|
515 | /* let perl know about it*/ |
---|
516 | owl_perlconfig_getmsg(m, NULL); |
---|
517 | |
---|
518 | /* do we need to autoreply? */ |
---|
519 | if (owl_global_is_zaway(&g) && !owl_message_get_attribute_value(m, "isauto")) { |
---|
520 | if (owl_message_is_type_zephyr(m)) { |
---|
521 | owl_zephyr_zaway(m); |
---|
522 | } else if (owl_message_is_type_aim(m)) { |
---|
523 | if (owl_message_is_private(m)) { |
---|
524 | owl_function_send_aimawymsg(owl_message_get_sender(m), owl_global_get_zaway_msg(&g)); |
---|
525 | } |
---|
526 | } |
---|
527 | } |
---|
528 | |
---|
529 | /* ring the bell if it's a personal */ |
---|
530 | if (!strcmp(owl_global_get_personalbell(&g), "on")) { |
---|
531 | if (!owl_message_is_loginout(m) && |
---|
532 | !owl_message_is_mail(m) && |
---|
533 | owl_message_is_personal(m)) { |
---|
534 | owl_function_beep(); |
---|
535 | } |
---|
536 | } else if (!strcmp(owl_global_get_personalbell(&g), "off")) { |
---|
537 | /* do nothing */ |
---|
538 | } else { |
---|
539 | f=owl_global_get_filter(&g, owl_global_get_personalbell(&g)); |
---|
540 | if (f && owl_filter_message_match(f, m)) { |
---|
541 | owl_function_beep(); |
---|
542 | } |
---|
543 | } |
---|
544 | |
---|
545 | /* if it matches the alert filter, do the alert action */ |
---|
546 | f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g)); |
---|
547 | if (f && owl_filter_message_match(f, m)) { |
---|
548 | owl_function_command(owl_global_get_alert_action(&g)); |
---|
549 | } |
---|
550 | |
---|
551 | /* if it's a zephyr login or logout, update the zbuddylist */ |
---|
552 | if (owl_message_is_type_zephyr(m) && owl_message_is_loginout(m)) { |
---|
553 | if (owl_message_is_login(m)) { |
---|
554 | owl_zbuddylist_adduser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m)); |
---|
555 | } else if (owl_message_is_logout(m)) { |
---|
556 | owl_zbuddylist_deluser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m)); |
---|
557 | } else { |
---|
558 | owl_function_error("Internal error: received login notice that is neither login nor logout"); |
---|
559 | } |
---|
560 | } |
---|
561 | } |
---|
562 | |
---|
563 | /* let perl know about it */ |
---|
564 | owl_perlconfig_newmsg(m, NULL); |
---|
565 | /* log the message if we need to */ |
---|
566 | owl_log_message(m); |
---|
567 | |
---|
568 | return 1; |
---|
569 | } |
---|
570 | |
---|
571 | void owl_process_input(owl_dispatch *d) |
---|
572 | { |
---|
573 | owl_input j; |
---|
574 | WINDOW *typwin; |
---|
575 | |
---|
576 | typwin = owl_global_get_curs_typwin(&g); |
---|
577 | |
---|
578 | while (1) { |
---|
579 | j.ch = wgetch(typwin); |
---|
580 | if (j.ch == ERR) return; |
---|
581 | |
---|
582 | j.uch = '\0'; |
---|
583 | if (j.ch >= KEY_MIN && j.ch <= KEY_MAX) { |
---|
584 | /* This is a curses control character. */ |
---|
585 | } |
---|
586 | else if (j.ch > 0x7f && j.ch < 0xfe) { |
---|
587 | /* Pull in a full utf-8 character. */ |
---|
588 | int bytes, i; |
---|
589 | char utf8buf[7]; |
---|
590 | memset(utf8buf, '\0', 7); |
---|
591 | |
---|
592 | utf8buf[0] = j.ch; |
---|
593 | |
---|
594 | if ((j.ch & 0xc0) && (~j.ch & 0x20)) bytes = 2; |
---|
595 | else if ((j.ch & 0xe0) && (~j.ch & 0x10)) bytes = 3; |
---|
596 | else if ((j.ch & 0xf0) && (~j.ch & 0x08)) bytes = 4; |
---|
597 | else if ((j.ch & 0xf8) && (~j.ch & 0x04)) bytes = 5; |
---|
598 | else if ((j.ch & 0xfc) && (~j.ch & 0x02)) bytes = 6; |
---|
599 | else bytes = 1; |
---|
600 | |
---|
601 | for (i = 1; i < bytes; i++) { |
---|
602 | int tmp = wgetch(typwin); |
---|
603 | /* If what we got was not a byte, or not a continuation byte */ |
---|
604 | if (tmp > 0xff || !(tmp & 0x80 && ~tmp & 0x40)) { |
---|
605 | /* ill-formed UTF-8 code unit subsequence, put back the |
---|
606 | char we just got. */ |
---|
607 | ungetch(tmp); |
---|
608 | j.ch = ERR; |
---|
609 | break; |
---|
610 | } |
---|
611 | utf8buf[i] = tmp; |
---|
612 | } |
---|
613 | |
---|
614 | if (j.ch != ERR) { |
---|
615 | if (g_utf8_validate(utf8buf, -1, NULL)) { |
---|
616 | j.uch = g_utf8_get_char(utf8buf); |
---|
617 | } |
---|
618 | else { |
---|
619 | j.ch = ERR; |
---|
620 | } |
---|
621 | } |
---|
622 | } |
---|
623 | else if (j.ch <= 0x7f) { |
---|
624 | j.uch = j.ch; |
---|
625 | } |
---|
626 | |
---|
627 | owl_process_input_char(j); |
---|
628 | } |
---|
629 | } |
---|
630 | |
---|
631 | void sig_handler(int sig, siginfo_t *si, void *data) |
---|
632 | { |
---|
633 | if (sig==SIGWINCH) { |
---|
634 | /* we can't inturrupt a malloc here, so it just sets a flag |
---|
635 | * schedulding a resize for later |
---|
636 | */ |
---|
637 | owl_function_resize(); |
---|
638 | } else if (sig==SIGPIPE || sig==SIGCHLD) { |
---|
639 | /* Set a flag and some info that we got the sigpipe |
---|
640 | * so we can record that we got it and why... */ |
---|
641 | owl_global_set_errsignal(&g, sig, si); |
---|
642 | } else if (sig==SIGTERM || sig==SIGHUP) { |
---|
643 | owl_function_quit(); |
---|
644 | } |
---|
645 | } |
---|
646 | |
---|
647 | void sigint_handler(int sig, siginfo_t *si, void *data) |
---|
648 | { |
---|
649 | owl_global_set_interrupted(&g); |
---|
650 | } |
---|
651 | |
---|
652 | void usage() |
---|
653 | { |
---|
654 | fprintf(stderr, "Barnowl version %s\n", OWL_VERSION_STRING); |
---|
655 | fprintf(stderr, "Usage: barnowl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-s <confdir>] [-t <ttyname>]\n"); |
---|
656 | fprintf(stderr, " -n don't load zephyr subscriptions\n"); |
---|
657 | fprintf(stderr, " -d enable debugging\n"); |
---|
658 | fprintf(stderr, " -D enable debugging and delete previous debug file\n"); |
---|
659 | fprintf(stderr, " -v print the Barnowl version number and exit\n"); |
---|
660 | fprintf(stderr, " -h print this help message\n"); |
---|
661 | fprintf(stderr, " -c specify an alternate config file\n"); |
---|
662 | fprintf(stderr, " -s specify an alternate config dir (default ~/.owl)\n"); |
---|
663 | fprintf(stderr, " -t set the tty name\n"); |
---|
664 | } |
---|
665 | |
---|
666 | #if OWL_STDERR_REDIR |
---|
667 | |
---|
668 | /* Replaces stderr with a pipe so that we can read from it. |
---|
669 | * Returns the fd of the pipe from which stderr can be read. */ |
---|
670 | int stderr_replace(void) |
---|
671 | { |
---|
672 | int pipefds[2]; |
---|
673 | if (0 != pipe(pipefds)) { |
---|
674 | perror("pipe"); |
---|
675 | owl_function_debugmsg("stderr_replace: pipe FAILED\n"); |
---|
676 | return -1; |
---|
677 | } |
---|
678 | owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]); |
---|
679 | if (-1 == dup2(pipefds[1], 2 /*stderr*/)) { |
---|
680 | owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno)); |
---|
681 | perror("dup2"); |
---|
682 | return -1; |
---|
683 | } |
---|
684 | return pipefds[0]; |
---|
685 | } |
---|
686 | |
---|
687 | /* Sends stderr (read from rfd) messages to the error console */ |
---|
688 | void stderr_redirect_handler(owl_dispatch *d) |
---|
689 | { |
---|
690 | int navail, bread; |
---|
691 | char buf[4096]; |
---|
692 | int rfd = d->fd; |
---|
693 | char *err; |
---|
694 | |
---|
695 | if (rfd<0) return; |
---|
696 | if (-1 == ioctl(rfd, FIONREAD, &navail)) { |
---|
697 | return; |
---|
698 | } |
---|
699 | /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/ |
---|
700 | if (navail <= 0) return; |
---|
701 | if (navail > sizeof(buf)-1) { |
---|
702 | navail = sizeof(buf)-1; |
---|
703 | } |
---|
704 | bread = read(rfd, buf, navail); |
---|
705 | if (buf[navail-1] != '\0') { |
---|
706 | buf[navail] = '\0'; |
---|
707 | } |
---|
708 | |
---|
709 | err = owl_sprintf("[stderr]\n%s", buf); |
---|
710 | |
---|
711 | owl_function_log_err(err); |
---|
712 | owl_free(err); |
---|
713 | } |
---|
714 | |
---|
715 | #endif /* OWL_STDERR_REDIR */ |
---|
716 | |
---|
717 | void owl_zephyr_buddycheck_timer(owl_timer *t, void *data) |
---|
718 | { |
---|
719 | if (owl_global_is_pseudologins(&g)) { |
---|
720 | owl_function_debugmsg("Doing zephyr buddy check"); |
---|
721 | owl_function_zephyr_buddy_check(1); |
---|
722 | } |
---|
723 | } |
---|