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 <termios.h> |
---|
49 | #include <sys/stat.h> |
---|
50 | #include "owl.h" |
---|
51 | |
---|
52 | #if OWL_STDERR_REDIR |
---|
53 | #ifdef HAVE_SYS_IOCTL_H |
---|
54 | #include <sys/ioctl.h> |
---|
55 | #endif |
---|
56 | #ifdef HAVE_SYS_FILIO_H |
---|
57 | #include <sys/filio.h> |
---|
58 | #endif |
---|
59 | int stderr_replace(void); |
---|
60 | #endif |
---|
61 | |
---|
62 | static const char fileIdent[] = "$Id$"; |
---|
63 | |
---|
64 | owl_global g; |
---|
65 | |
---|
66 | int main(int argc, char **argv, char **env) |
---|
67 | { |
---|
68 | WINDOW *recwin, *sepwin, *typwin, *msgwin; |
---|
69 | owl_editwin *tw; |
---|
70 | owl_popwin *pw; |
---|
71 | int j, ret, initialsubs, debug, argcsave, followlast; |
---|
72 | int newmsgs, zpendcount, nexttimediff; |
---|
73 | struct sigaction sigact; |
---|
74 | char *configfile, *tty, *perlout, *perlerr, **argvsave, buff[LINE], startupmsg[LINE]; |
---|
75 | owl_filter *f; |
---|
76 | owl_style *s; |
---|
77 | time_t nexttime, now; |
---|
78 | struct tm *today; |
---|
79 | char *dir; |
---|
80 | struct termios tio; |
---|
81 | #ifdef HAVE_LIBZEPHYR |
---|
82 | ZNotice_t notice; |
---|
83 | #endif |
---|
84 | #if OWL_STDERR_REDIR |
---|
85 | int newstderr; |
---|
86 | #endif |
---|
87 | |
---|
88 | argcsave=argc; |
---|
89 | argvsave=argv; |
---|
90 | configfile=NULL; |
---|
91 | tty=NULL; |
---|
92 | debug=0; |
---|
93 | initialsubs=1; |
---|
94 | if (argc>0) { |
---|
95 | argv++; |
---|
96 | argc--; |
---|
97 | } |
---|
98 | while (argc>0) { |
---|
99 | if (!strcmp(argv[0], "-n")) { |
---|
100 | initialsubs=0; |
---|
101 | argv++; |
---|
102 | argc--; |
---|
103 | } else if (!strcmp(argv[0], "-c")) { |
---|
104 | if (argc<2) { |
---|
105 | fprintf(stderr, "Too few arguments to -c\n"); |
---|
106 | usage(); |
---|
107 | exit(1); |
---|
108 | } |
---|
109 | configfile=argv[1]; |
---|
110 | argv+=2; |
---|
111 | argc-=2; |
---|
112 | } else if (!strcmp(argv[0], "-t")) { |
---|
113 | if (argc<2) { |
---|
114 | fprintf(stderr, "Too few arguments to -t\n"); |
---|
115 | usage(); |
---|
116 | exit(1); |
---|
117 | } |
---|
118 | tty=argv[1]; |
---|
119 | argv+=2; |
---|
120 | argc-=2; |
---|
121 | } else if (!strcmp(argv[0], "-d")) { |
---|
122 | debug=1; |
---|
123 | argv++; |
---|
124 | argc--; |
---|
125 | } else if (!strcmp(argv[0], "-D")) { |
---|
126 | debug=1; |
---|
127 | unlink(OWL_DEBUG_FILE); |
---|
128 | argv++; |
---|
129 | argc--; |
---|
130 | } else if (!strcmp(argv[0], "-v")) { |
---|
131 | printf("This is owl version %s\n", OWL_VERSION_STRING); |
---|
132 | exit(0); |
---|
133 | } else { |
---|
134 | fprintf(stderr, "Uknown argument\n"); |
---|
135 | usage(); |
---|
136 | exit(1); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | owl_function_debugmsg("startup: Finished parsing arguments"); |
---|
141 | |
---|
142 | /* signal handler */ |
---|
143 | /*sigact.sa_handler=sig_handler;*/ |
---|
144 | sigact.sa_sigaction=sig_handler; |
---|
145 | sigemptyset(&sigact.sa_mask); |
---|
146 | sigact.sa_flags=SA_SIGINFO; |
---|
147 | sigaction(SIGWINCH, &sigact, NULL); |
---|
148 | sigaction(SIGALRM, &sigact, NULL); |
---|
149 | sigaction(SIGPIPE, &sigact, NULL); |
---|
150 | sigaction(SIGTERM, &sigact, NULL); |
---|
151 | sigaction(SIGHUP, &sigact, NULL); |
---|
152 | |
---|
153 | /* save initial terminal settings */ |
---|
154 | tcgetattr(0, owl_global_get_startup_tio(&g)); |
---|
155 | |
---|
156 | /* turn ISTRIP off */ |
---|
157 | tcgetattr(0, &tio); |
---|
158 | tio.c_iflag &= ~ISTRIP; |
---|
159 | tcsetattr(0, TCSAFLUSH, &tio); |
---|
160 | |
---|
161 | /* screen init */ |
---|
162 | if (!getenv("TERMINFO")) { |
---|
163 | sprintf(buff, "TERMINFO=%s", TERMINFO); |
---|
164 | putenv(buff); |
---|
165 | owl_function_debugmsg("startup: setting TERMINFO to %s", TERMINFO); |
---|
166 | } else { |
---|
167 | owl_function_debugmsg("startup: leaving TERMINFO as %s from envrionment", getenv("TERMINFO")); |
---|
168 | } |
---|
169 | initscr(); |
---|
170 | start_color(); |
---|
171 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
172 | use_default_colors(); |
---|
173 | #endif |
---|
174 | raw(); |
---|
175 | noecho(); |
---|
176 | |
---|
177 | /* define simple color pairs */ |
---|
178 | if (has_colors() && COLOR_PAIRS>=8) { |
---|
179 | init_pair(OWL_COLOR_BLACK, COLOR_BLACK, -1); |
---|
180 | init_pair(OWL_COLOR_RED, COLOR_RED, -1); |
---|
181 | init_pair(OWL_COLOR_GREEN, COLOR_GREEN, -1); |
---|
182 | init_pair(OWL_COLOR_YELLOW, COLOR_YELLOW, -1); |
---|
183 | init_pair(OWL_COLOR_BLUE, COLOR_BLUE, -1); |
---|
184 | init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, -1); |
---|
185 | init_pair(OWL_COLOR_CYAN, COLOR_CYAN, -1); |
---|
186 | init_pair(OWL_COLOR_WHITE, COLOR_WHITE, -1); |
---|
187 | } |
---|
188 | |
---|
189 | /* owl global init */ |
---|
190 | owl_global_init(&g); |
---|
191 | if (debug) owl_global_set_debug_on(&g); |
---|
192 | owl_function_debugmsg("startup: first available debugging message"); |
---|
193 | owl_global_set_startupargs(&g, argcsave, argvsave); |
---|
194 | owl_global_set_haveaim(&g); |
---|
195 | |
---|
196 | #ifdef HAVE_LIBZEPHYR |
---|
197 | /* zephyr init */ |
---|
198 | ret=owl_zephyr_initialize(); |
---|
199 | if (!ret) |
---|
200 | owl_global_set_havezephyr(&g); |
---|
201 | #endif |
---|
202 | |
---|
203 | #if OWL_STDERR_REDIR |
---|
204 | /* Do this only after we've started curses up... */ |
---|
205 | owl_function_debugmsg("startup: doing stderr redirection"); |
---|
206 | newstderr = stderr_replace(); |
---|
207 | owl_muxevents_add(owl_global_get_muxevents(&g), newstderr, OWL_MUX_READ, |
---|
208 | stderr_redirect_handler, NULL); |
---|
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_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_CONFIG_DIR); |
---|
214 | mkdir(dir, S_IRWXU); |
---|
215 | owl_free(dir); |
---|
216 | |
---|
217 | /* set the tty, either from the command line, or by figuring it out */ |
---|
218 | owl_function_debugmsg("startup: setting tty name"); |
---|
219 | if (tty) { |
---|
220 | owl_global_set_tty(&g, tty); |
---|
221 | } else { |
---|
222 | owl_global_set_tty(&g, owl_util_get_default_tty()); |
---|
223 | } |
---|
224 | |
---|
225 | /* setup the built-in styles */ |
---|
226 | owl_function_debugmsg("startup: creating built-in styles"); |
---|
227 | s=owl_malloc(sizeof(owl_style)); |
---|
228 | owl_style_create_internal(s, "default", &owl_stylefunc_default, "Default message formatting"); |
---|
229 | owl_global_add_style(&g, s); |
---|
230 | |
---|
231 | s=owl_malloc(sizeof(owl_style)); |
---|
232 | owl_style_create_internal(s, "basic", &owl_stylefunc_basic, "Basic message formatting."); |
---|
233 | owl_global_add_style(&g, s); |
---|
234 | #if 0 |
---|
235 | s=owl_malloc(sizeof(owl_style)); |
---|
236 | owl_style_create_internal(s, "vt", &owl_stylefunc_vt, "VT message formatting."); |
---|
237 | owl_global_add_style(&g, s); |
---|
238 | #endif |
---|
239 | s=owl_malloc(sizeof(owl_style)); |
---|
240 | owl_style_create_internal(s, "oneline", &owl_stylefunc_oneline, "Formats for one-line-per-message"); |
---|
241 | owl_global_add_style(&g, s); |
---|
242 | |
---|
243 | /* setup the default filters */ |
---|
244 | /* the personal filter will need to change again when AIM chat's are |
---|
245 | * included. Also, there should be an %aimme% */ |
---|
246 | owl_function_debugmsg("startup: creating default filters"); |
---|
247 | f=owl_malloc(sizeof(owl_filter)); |
---|
248 | owl_filter_init_fromstring(f, "personal", "isprivate ^true$"); |
---|
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, "wordwrap", "not ( type ^admin$ or type ^zephyr$ ) "); |
---|
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, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )"); |
---|
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, "ping", "opcode ^ping$"); |
---|
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, "auto", "opcode ^auto$"); |
---|
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, "login", "not login ^none$"); |
---|
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, "reply-lockout", "class ^noc or class ^mail$"); |
---|
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, "out", "direction ^out$"); |
---|
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, "aim", "type ^aim$"); |
---|
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, "zephyr", "type ^zephyr$"); |
---|
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, "none", "false"); |
---|
289 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
290 | |
---|
291 | f=owl_malloc(sizeof(owl_filter)); |
---|
292 | owl_filter_init_fromstring(f, "all", "true"); |
---|
293 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
294 | |
---|
295 | /* set the current view */ |
---|
296 | owl_function_debugmsg("startup: setting the current view"); |
---|
297 | owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default")); |
---|
298 | |
---|
299 | /* AIM init */ |
---|
300 | owl_function_debugmsg("startup: doing AIM initialization"); |
---|
301 | owl_aim_init(); |
---|
302 | |
---|
303 | /* process the startup file */ |
---|
304 | owl_function_debugmsg("startup: processing startup file"); |
---|
305 | owl_function_source(NULL); |
---|
306 | |
---|
307 | /* read the config file */ |
---|
308 | owl_function_debugmsg("startup: processing config file"); |
---|
309 | owl_context_set_readconfig(owl_global_get_context(&g)); |
---|
310 | perlerr=owl_perlconfig_readconfig(configfile); |
---|
311 | if (perlerr) { |
---|
312 | endwin(); |
---|
313 | owl_function_error("Error parsing configfile: %s\n", perlerr); |
---|
314 | fprintf(stderr, "\nError parsing configfile: %s\n", perlerr); |
---|
315 | fflush(stderr); |
---|
316 | printf("\nError parsing configfile: %s\n", perlerr); |
---|
317 | fflush(stdout); |
---|
318 | exit(1); |
---|
319 | } |
---|
320 | |
---|
321 | /* if the config defines a formatting function, add 'perl' as a style */ |
---|
322 | if (owl_global_is_config_format(&g)) { |
---|
323 | owl_function_debugmsg("Found perl formatting"); |
---|
324 | s=owl_malloc(sizeof(owl_style)); |
---|
325 | owl_style_create_perl(s, "perl", "BarnOwl::_format_msg_legacy_wrap", |
---|
326 | "User-defined perl style that calls BarnOwl::format_msg" |
---|
327 | "with legacy global variable support"); |
---|
328 | owl_global_add_style(&g, s); |
---|
329 | owl_global_set_default_style(&g, "perl"); |
---|
330 | } |
---|
331 | |
---|
332 | /* execute the startup function in the configfile */ |
---|
333 | owl_function_debugmsg("startup: executing perl startup, if applicable"); |
---|
334 | perlout = owl_perlconfig_execute("BarnOwl::Hooks::startup();"); |
---|
335 | if (perlout) owl_free(perlout); |
---|
336 | |
---|
337 | /* hold on to the window names for convenience */ |
---|
338 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
339 | recwin=owl_global_get_curs_recwin(&g); |
---|
340 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
341 | typwin=owl_global_get_curs_typwin(&g); |
---|
342 | tw=owl_global_get_typwin(&g); |
---|
343 | |
---|
344 | /* welcome message */ |
---|
345 | owl_function_debugmsg("startup: creating splash message"); |
---|
346 | strcpy(startupmsg, "-----------------------------------------------------------------------\n"); |
---|
347 | sprintf(buff, "Welcome to Owl version %s. Press 'h' for on-line help. \n", OWL_VERSION_STRING); |
---|
348 | strcat(startupmsg, buff); |
---|
349 | strcat(startupmsg, " \n"); |
---|
350 | strcat(startupmsg, "This is an UNOFFICIAL DEVELOPMENT BUILD of owl. If you are using this \n"); |
---|
351 | strcat(startupmsg, "build regularly, please add yourself to dirty-owl-hackers@mit.edu \n"); |
---|
352 | strcat(startupmsg, " ^ ^ \n"); |
---|
353 | strcat(startupmsg, "Any bugs should be reported to dirty-owl-hackers@mit.edu OvO \n"); |
---|
354 | strcat(startupmsg, "Do not seek help with this build from bug-owl or ktools. ( ) \n"); |
---|
355 | strcat(startupmsg, "-----------------------------------------------------------------m-m---\n"); |
---|
356 | owl_function_adminmsg("", startupmsg); |
---|
357 | sepbar(NULL); |
---|
358 | |
---|
359 | wrefresh(sepwin); |
---|
360 | |
---|
361 | /* load zephyr subs */ |
---|
362 | if (initialsubs) { |
---|
363 | int ret2; |
---|
364 | owl_function_debugmsg("startup: loading initial zephyr subs"); |
---|
365 | |
---|
366 | /* load default subscriptions */ |
---|
367 | ret=owl_zephyr_loaddefaultsubs(); |
---|
368 | |
---|
369 | /* load subscriptions from subs file */ |
---|
370 | ret2=owl_zephyr_loadsubs(NULL, 0); |
---|
371 | |
---|
372 | if (ret || ret2) { |
---|
373 | owl_function_adminmsg("", "Error loading zephyr subscriptions"); |
---|
374 | } else if (ret2!=-1) { |
---|
375 | owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES); |
---|
376 | } |
---|
377 | |
---|
378 | /* load login subscriptions */ |
---|
379 | if (owl_global_is_loginsubs(&g)) { |
---|
380 | owl_function_debugmsg("startup: loading login subs"); |
---|
381 | owl_function_loadloginsubs(NULL); |
---|
382 | } |
---|
383 | } |
---|
384 | |
---|
385 | /* First buddy check to sync the list without notifications */ |
---|
386 | owl_function_debugmsg("startup: doing initial zephyr buddy check"); |
---|
387 | /* owl_function_zephyr_buddy_check(0); */ |
---|
388 | |
---|
389 | /* set the startup and default style, based on userclue and presence of a |
---|
390 | * formatting function */ |
---|
391 | owl_function_debugmsg("startup: setting startup and default style"); |
---|
392 | if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) { |
---|
393 | /* the style was set by the user: leave it alone */ |
---|
394 | } else if (owl_global_is_config_format(&g)) { |
---|
395 | owl_global_set_default_style(&g, "perl"); |
---|
396 | } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) { |
---|
397 | owl_global_set_default_style(&g, "default"); |
---|
398 | } else { |
---|
399 | owl_global_set_default_style(&g, "basic"); |
---|
400 | } |
---|
401 | |
---|
402 | /* zlog in if we need to */ |
---|
403 | if (owl_global_is_startuplogin(&g)) { |
---|
404 | owl_function_debugmsg("startup: doing zlog in"); |
---|
405 | owl_zephyr_zlog_in(); |
---|
406 | } |
---|
407 | |
---|
408 | owl_function_debugmsg("startup: set style for the view"); |
---|
409 | owl_view_set_style(owl_global_get_current_view(&g), |
---|
410 | owl_global_get_style_by_name(&g, owl_global_get_default_style(&g))); |
---|
411 | |
---|
412 | owl_function_debugmsg("startup: setting context interactive"); |
---|
413 | owl_context_set_interactive(owl_global_get_context(&g)); |
---|
414 | |
---|
415 | nexttimediff=10; |
---|
416 | nexttime=time(NULL); |
---|
417 | |
---|
418 | owl_function_debugmsg("startup: entering main loop"); |
---|
419 | /* main loop */ |
---|
420 | while (1) { |
---|
421 | |
---|
422 | /* if a resize has been scheduled, deal with it */ |
---|
423 | owl_global_resize(&g, 0, 0); |
---|
424 | |
---|
425 | /* these are here in case a resize changes the windows */ |
---|
426 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
427 | recwin=owl_global_get_curs_recwin(&g); |
---|
428 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
429 | typwin=owl_global_get_curs_typwin(&g); |
---|
430 | |
---|
431 | followlast=owl_global_should_followlast(&g); |
---|
432 | |
---|
433 | /* Do AIM stuff */ |
---|
434 | if (owl_global_is_doaimevents(&g)) { |
---|
435 | owl_aim_process_events(); |
---|
436 | |
---|
437 | if (owl_global_is_aimloggedin(&g)) { |
---|
438 | if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) { |
---|
439 | /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */ |
---|
440 | owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g)); |
---|
441 | } |
---|
442 | } |
---|
443 | } |
---|
444 | |
---|
445 | owl_perlconfig_mainloop(); |
---|
446 | |
---|
447 | /* little hack */ |
---|
448 | now=time(NULL); |
---|
449 | today=localtime(&now); |
---|
450 | if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) { |
---|
451 | if (time(NULL)>nexttime) { |
---|
452 | if (nexttimediff==1) { |
---|
453 | nexttimediff=10; |
---|
454 | } else { |
---|
455 | nexttimediff=1; |
---|
456 | } |
---|
457 | nexttime+=nexttimediff; |
---|
458 | owl_hack_animate(); |
---|
459 | } |
---|
460 | } |
---|
461 | |
---|
462 | /* Grab incoming messages. */ |
---|
463 | newmsgs=0; |
---|
464 | zpendcount=0; |
---|
465 | while(owl_zephyr_zpending() || owl_global_messagequeue_pending(&g)) { |
---|
466 | #ifdef HAVE_LIBZEPHYR |
---|
467 | struct sockaddr_in from; |
---|
468 | #endif |
---|
469 | owl_message *m=NULL; |
---|
470 | owl_filter *f; |
---|
471 | |
---|
472 | /* grab the new message, stick it in 'm' */ |
---|
473 | if (owl_zephyr_zpending()) { |
---|
474 | #ifdef HAVE_LIBZEPHYR |
---|
475 | /* grab a zephyr notice, but if we've done 20 without stopping, |
---|
476 | take a break to process keystrokes etc. */ |
---|
477 | if (zpendcount>20) break; |
---|
478 | ZReceiveNotice(¬ice, &from); |
---|
479 | zpendcount++; |
---|
480 | |
---|
481 | /* is this an ack from a zephyr we sent? */ |
---|
482 | if (owl_zephyr_notice_is_ack(¬ice)) { |
---|
483 | owl_zephyr_handle_ack(¬ice); |
---|
484 | continue; |
---|
485 | } |
---|
486 | |
---|
487 | /* if it's a ping and we're not viewing pings then skip it */ |
---|
488 | if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) { |
---|
489 | continue; |
---|
490 | } |
---|
491 | |
---|
492 | /* create the new message */ |
---|
493 | m=owl_malloc(sizeof(owl_message)); |
---|
494 | owl_message_create_from_znotice(m, ¬ice); |
---|
495 | #endif |
---|
496 | } else if (owl_global_messagequeue_pending(&g)) { |
---|
497 | /* pick up the non-zephyr message in the message queue */ |
---|
498 | m=owl_global_messageuque_popmsg(&g); |
---|
499 | } else { |
---|
500 | /* Not supposed to happen, but we seem to get here on resizes */ |
---|
501 | owl_function_debugmsg("Bottomed out looking for zephyr"); |
---|
502 | } |
---|
503 | |
---|
504 | /* If we didn't pick up a message for some reason, don't go on */ |
---|
505 | if (m==NULL) { |
---|
506 | owl_function_debugmsg("m is null in main loop"); |
---|
507 | continue; |
---|
508 | } |
---|
509 | |
---|
510 | /* if this message it on the puntlist, nuke it and continue */ |
---|
511 | if (owl_global_message_is_puntable(&g, m)) { |
---|
512 | owl_message_free(m); |
---|
513 | continue; |
---|
514 | } |
---|
515 | |
---|
516 | /* login or logout that should be ignored? */ |
---|
517 | if (owl_global_is_ignorelogins(&g) && owl_message_is_loginout(m)) { |
---|
518 | owl_message_free(m); |
---|
519 | continue; |
---|
520 | } |
---|
521 | |
---|
522 | /* otherwise add it to the global list */ |
---|
523 | owl_messagelist_append_element(owl_global_get_msglist(&g), m); |
---|
524 | newmsgs=1; |
---|
525 | |
---|
526 | /* let the config know the new message has been received */ |
---|
527 | owl_perlconfig_getmsg(m, 0, NULL); |
---|
528 | |
---|
529 | /* add it to any necessary views; right now there's only the current view */ |
---|
530 | owl_view_consider_message(owl_global_get_current_view(&g), m); |
---|
531 | |
---|
532 | /* do we need to autoreply? */ |
---|
533 | if (owl_global_is_zaway(&g) && !owl_message_get_attribute_value(m, "isauto")) { |
---|
534 | if (owl_message_is_type_zephyr(m)) { |
---|
535 | owl_zephyr_zaway(m); |
---|
536 | } else if (owl_message_is_type_aim(m)) { |
---|
537 | if (owl_message_is_private(m)) { |
---|
538 | owl_function_send_aimawymsg(owl_message_get_sender(m), owl_global_get_zaway_msg(&g)); |
---|
539 | } |
---|
540 | } |
---|
541 | } |
---|
542 | |
---|
543 | /* ring the bell if it's a personal */ |
---|
544 | if (!strcmp(owl_global_get_personalbell(&g), "on")) { |
---|
545 | if (!owl_message_is_loginout(m) && |
---|
546 | !owl_message_is_mail(m) && |
---|
547 | owl_message_is_private(m)) { |
---|
548 | owl_function_beep(); |
---|
549 | } |
---|
550 | } else if (!strcmp(owl_global_get_personalbell(&g), "off")) { |
---|
551 | /* do nothing */ |
---|
552 | } else { |
---|
553 | f=owl_global_get_filter(&g, owl_global_get_personalbell(&g)); |
---|
554 | if (f && owl_filter_message_match(f, m)) { |
---|
555 | owl_function_beep(); |
---|
556 | } |
---|
557 | } |
---|
558 | |
---|
559 | |
---|
560 | /* if it matches the alert filter, do the alert action */ |
---|
561 | f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g)); |
---|
562 | if (f && owl_filter_message_match(f, m)) { |
---|
563 | owl_function_command(owl_global_get_alert_action(&g)); |
---|
564 | } |
---|
565 | |
---|
566 | /* if it's a zephyr login or logout, update the zbuddylist */ |
---|
567 | if (owl_message_is_type_zephyr(m) && owl_message_is_loginout(m)) { |
---|
568 | if (owl_message_is_login(m)) { |
---|
569 | owl_zbuddylist_adduser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m)); |
---|
570 | } else if (owl_message_is_logout(m)) { |
---|
571 | owl_zbuddylist_deluser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m)); |
---|
572 | } else { |
---|
573 | owl_function_error("Internal error: received login notice that is neither login nor logout"); |
---|
574 | } |
---|
575 | } |
---|
576 | |
---|
577 | /* check for burning ears message */ |
---|
578 | /* this is an unsupported feature */ |
---|
579 | if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) { |
---|
580 | char *buff; |
---|
581 | buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m)); |
---|
582 | owl_function_adminmsg(buff, ""); |
---|
583 | owl_free(buff); |
---|
584 | owl_function_beep(); |
---|
585 | } |
---|
586 | |
---|
587 | /* log the message if we need to */ |
---|
588 | owl_log_message(m); |
---|
589 | } |
---|
590 | |
---|
591 | /* is it time to check zbuddies? */ |
---|
592 | if (owl_global_is_pseudologins(&g)) { |
---|
593 | if (owl_timer_is_expired(owl_global_get_zephyr_buddycheck_timer(&g))) { |
---|
594 | owl_function_debugmsg("Doing zephyr buddy check"); |
---|
595 | owl_function_zephyr_buddy_check(1); |
---|
596 | owl_timer_reset(owl_global_get_zephyr_buddycheck_timer(&g)); |
---|
597 | } |
---|
598 | } |
---|
599 | |
---|
600 | /* dispatch any muxevents */ |
---|
601 | owl_muxevents_dispatch(owl_global_get_muxevents(&g), 0); |
---|
602 | |
---|
603 | /* follow the last message if we're supposed to */ |
---|
604 | if (newmsgs && followlast) { |
---|
605 | owl_function_lastmsg_noredisplay(); |
---|
606 | } |
---|
607 | |
---|
608 | /* do the newmsgproc thing */ |
---|
609 | if (newmsgs) { |
---|
610 | owl_function_do_newmsgproc(); |
---|
611 | } |
---|
612 | |
---|
613 | /* redisplay if necessary */ |
---|
614 | /* this should be optimized to not run if the new messages won't be displayed */ |
---|
615 | if (newmsgs) { |
---|
616 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
617 | sepbar(NULL); |
---|
618 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
619 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
620 | /* TODO: this is a broken kludge */ |
---|
621 | if (owl_global_get_viewwin(&g)) { |
---|
622 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
623 | } |
---|
624 | } |
---|
625 | owl_global_set_needrefresh(&g); |
---|
626 | } |
---|
627 | |
---|
628 | /* if a popwin just came up, refresh it */ |
---|
629 | pw=owl_global_get_popwin(&g); |
---|
630 | if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) { |
---|
631 | owl_popwin_refresh(pw); |
---|
632 | owl_popwin_no_needs_first_refresh(pw); |
---|
633 | owl_global_set_needrefresh(&g); |
---|
634 | /* TODO: this is a broken kludge */ |
---|
635 | if (owl_global_get_viewwin(&g)) { |
---|
636 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
637 | } |
---|
638 | } |
---|
639 | |
---|
640 | /* update the terminal if we need to */ |
---|
641 | if (owl_global_is_needrefresh(&g)) { |
---|
642 | /* leave the cursor in the appropriate window */ |
---|
643 | if (owl_global_is_typwin_active(&g)) { |
---|
644 | owl_function_set_cursor(typwin); |
---|
645 | } else { |
---|
646 | owl_function_set_cursor(sepwin); |
---|
647 | } |
---|
648 | doupdate(); |
---|
649 | owl_global_set_noneedrefresh(&g); |
---|
650 | } |
---|
651 | |
---|
652 | /* Handle all keypresses. If no key has been pressed, sleep for a |
---|
653 | * little bit, but otherwise do not. This lets input be grabbed |
---|
654 | * as quickly as possbile */ |
---|
655 | j=wgetch(typwin); |
---|
656 | if (j==ERR) { |
---|
657 | usleep(10); |
---|
658 | } else { |
---|
659 | /* find and activate the current keymap. |
---|
660 | * TODO: this should really get fixed by activating |
---|
661 | * keymaps as we switch between windows... |
---|
662 | */ |
---|
663 | if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) { |
---|
664 | owl_context_set_popless(owl_global_get_context(&g), |
---|
665 | owl_global_get_viewwin(&g)); |
---|
666 | owl_function_activate_keymap("popless"); |
---|
667 | } else if (owl_global_is_typwin_active(&g) |
---|
668 | && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) { |
---|
669 | /* |
---|
670 | owl_context_set_editline(owl_global_get_context(&g), tw); |
---|
671 | owl_function_activate_keymap("editline"); |
---|
672 | */ |
---|
673 | } else if (owl_global_is_typwin_active(&g) |
---|
674 | && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) { |
---|
675 | owl_context_set_editmulti(owl_global_get_context(&g), tw); |
---|
676 | owl_function_activate_keymap("editmulti"); |
---|
677 | } else { |
---|
678 | owl_context_set_recv(owl_global_get_context(&g)); |
---|
679 | owl_function_activate_keymap("recv"); |
---|
680 | } |
---|
681 | /* now actually handle the keypress */ |
---|
682 | ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j); |
---|
683 | if (ret!=0 && ret!=1) { |
---|
684 | owl_function_makemsg("Unable to handle keypress"); |
---|
685 | } |
---|
686 | } |
---|
687 | |
---|
688 | /* Log any error signals */ |
---|
689 | { |
---|
690 | siginfo_t si; |
---|
691 | int signum; |
---|
692 | if ((signum = owl_global_get_errsignal_and_clear(&g, &si)) > 0) { |
---|
693 | owl_function_error("Got unexpected signal: %d %s (code: %d band: %d errno: %d)", |
---|
694 | signum, signum==SIGPIPE?"SIGPIPE":"SIG????", |
---|
695 | si.si_code, si.si_band, si.si_errno); |
---|
696 | } |
---|
697 | } |
---|
698 | |
---|
699 | } |
---|
700 | } |
---|
701 | |
---|
702 | void sig_handler(int sig, siginfo_t *si, void *data) |
---|
703 | { |
---|
704 | if (sig==SIGWINCH) { |
---|
705 | /* we can't inturrupt a malloc here, so it just sets a flag |
---|
706 | * schedulding a resize for later |
---|
707 | */ |
---|
708 | owl_function_resize(); |
---|
709 | } else if (sig==SIGPIPE || sig==SIGCHLD) { |
---|
710 | /* Set a flag and some info that we got the sigpipe |
---|
711 | * so we can record that we got it and why... */ |
---|
712 | owl_global_set_errsignal(&g, sig, si); |
---|
713 | } else if (sig==SIGTERM || sig==SIGHUP) { |
---|
714 | owl_function_quit(); |
---|
715 | } |
---|
716 | |
---|
717 | } |
---|
718 | |
---|
719 | void usage() |
---|
720 | { |
---|
721 | fprintf(stderr, "Owl version %s\n", OWL_VERSION_STRING); |
---|
722 | fprintf(stderr, "Usage: owl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-t <ttyname>]\n"); |
---|
723 | fprintf(stderr, " -n don't load zephyr subscriptions\n"); |
---|
724 | fprintf(stderr, " -d enable debugging\n"); |
---|
725 | fprintf(stderr, " -D enable debugging and delete previous debug file\n"); |
---|
726 | fprintf(stderr, " -v print the Owl version number and exit\n"); |
---|
727 | fprintf(stderr, " -h print this help message\n"); |
---|
728 | fprintf(stderr, " -c specify an alternate config file\n"); |
---|
729 | fprintf(stderr, " -t set the tty name\n"); |
---|
730 | } |
---|
731 | |
---|
732 | #if OWL_STDERR_REDIR |
---|
733 | |
---|
734 | /* Replaces stderr with a pipe so that we can read from it. |
---|
735 | * Returns the fd of the pipe from which stderr can be read. */ |
---|
736 | int stderr_replace(void) |
---|
737 | { |
---|
738 | int pipefds[2]; |
---|
739 | if (0 != pipe(pipefds)) { |
---|
740 | perror("pipe"); |
---|
741 | owl_function_debugmsg("stderr_replace: pipe FAILED\n"); |
---|
742 | return -1; |
---|
743 | } |
---|
744 | owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]); |
---|
745 | if (-1 == dup2(pipefds[1], 2 /*stderr*/)) { |
---|
746 | owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno)); |
---|
747 | perror("dup2"); |
---|
748 | return -1; |
---|
749 | } |
---|
750 | return pipefds[0]; |
---|
751 | } |
---|
752 | |
---|
753 | /* Sends stderr (read from rfd) messages to the error console */ |
---|
754 | void stderr_redirect_handler(int handle, int rfd, int eventmask, void *data) |
---|
755 | { |
---|
756 | int navail, bread; |
---|
757 | char *buf; |
---|
758 | /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/ |
---|
759 | if (rfd<0) return; |
---|
760 | if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) { |
---|
761 | return; |
---|
762 | } |
---|
763 | /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/ |
---|
764 | if (navail<=0) return; |
---|
765 | if (navail>256) { navail = 256; } |
---|
766 | buf = owl_malloc(navail+1); |
---|
767 | bread = read(rfd, buf, navail); |
---|
768 | if (buf[navail-1] != '\0') { |
---|
769 | buf[navail] = '\0'; |
---|
770 | } |
---|
771 | owl_function_error("Err: %s", buf); |
---|
772 | owl_free(buf); |
---|
773 | } |
---|
774 | |
---|
775 | #endif /* OWL_STDERR_REDIR */ |
---|