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