1 | /* Written by James Kretchmar, MIT |
---|
2 | * |
---|
3 | * Copyright 2001 Massachusetts Institute of Technology |
---|
4 | * |
---|
5 | * Permission to use, copy, modify, and distribute this software and |
---|
6 | * its documentation for any purpose and without fee is hereby |
---|
7 | * granted, provided that the above copyright notice and this |
---|
8 | * permission notice appear in all copies and in supporting |
---|
9 | * documentation. No representation is made about the suitability of |
---|
10 | * this software for any purpose. It is provided "as is" without |
---|
11 | * express or implied warranty. |
---|
12 | */ |
---|
13 | |
---|
14 | #include <stdio.h> |
---|
15 | #include <unistd.h> |
---|
16 | #include <stdlib.h> |
---|
17 | #include <string.h> |
---|
18 | #include <zephyr/zephyr.h> |
---|
19 | #include <com_err.h> |
---|
20 | #include <signal.h> |
---|
21 | #include <time.h> |
---|
22 | #include <sys/param.h> |
---|
23 | #include <sys/types.h> |
---|
24 | #include <sys/stat.h> |
---|
25 | #include "owl.h" |
---|
26 | |
---|
27 | static const char fileIdent[] = "$Id$"; |
---|
28 | |
---|
29 | int main(int argc, char **argv, char **env) { |
---|
30 | WINDOW *recwin, *sepwin, *typwin, *msgwin; |
---|
31 | owl_editwin *tw; |
---|
32 | owl_popwin *pw; |
---|
33 | int j, ret, initialsubs, debug, argcsave, followlast; |
---|
34 | int newmsgs, zpendcount, nexttimediff; |
---|
35 | struct sigaction sigact; |
---|
36 | char *configfile, *tty, *perlout, **argvsave, buff[LINE], startupmsg[LINE]; |
---|
37 | owl_filter *f; |
---|
38 | time_t nexttime, now; |
---|
39 | struct tm *today; |
---|
40 | char *dir; |
---|
41 | |
---|
42 | argcsave=argc; |
---|
43 | argvsave=argv; |
---|
44 | configfile=NULL; |
---|
45 | tty=NULL; |
---|
46 | debug=0; |
---|
47 | if (argc>0) { |
---|
48 | argv++; |
---|
49 | argc--; |
---|
50 | } |
---|
51 | while (argc>0) { |
---|
52 | if (!strcmp(argv[0], "-n")) { |
---|
53 | initialsubs=0; |
---|
54 | argv++; |
---|
55 | argc--; |
---|
56 | } else if (!strcmp(argv[0], "-c")) { |
---|
57 | if (argc<2) { |
---|
58 | fprintf(stderr, "Too few arguments to -c\n"); |
---|
59 | usage(); |
---|
60 | exit(1); |
---|
61 | } |
---|
62 | configfile=argv[1]; |
---|
63 | argv+=2; |
---|
64 | argc-=2; |
---|
65 | } else if (!strcmp(argv[0], "-t")) { |
---|
66 | if (argc<2) { |
---|
67 | fprintf(stderr, "Too few arguments to -t\n"); |
---|
68 | usage(); |
---|
69 | exit(1); |
---|
70 | } |
---|
71 | tty=argv[1]; |
---|
72 | argv+=2; |
---|
73 | argc-=2; |
---|
74 | } else if (!strcmp(argv[0], "-d")) { |
---|
75 | debug=1; |
---|
76 | argv++; |
---|
77 | argc--; |
---|
78 | } else if (!strcmp(argv[0], "-v")) { |
---|
79 | printf("This is owl version %s\n", OWL_VERSION_STRING); |
---|
80 | exit(0); |
---|
81 | } else { |
---|
82 | fprintf(stderr, "Uknown argument\n"); |
---|
83 | usage(); |
---|
84 | exit(1); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | /* zephyr init */ |
---|
89 | if ((ret = ZInitialize()) != ZERR_NONE) { |
---|
90 | com_err("owl",ret,"while initializing"); |
---|
91 | exit(1); |
---|
92 | } |
---|
93 | if ((ret = ZOpenPort(NULL)) != ZERR_NONE) { |
---|
94 | com_err("owl",ret,"while opening port"); |
---|
95 | exit(1); |
---|
96 | } |
---|
97 | |
---|
98 | /* signal handler */ |
---|
99 | sigact.sa_handler=sig_handler; |
---|
100 | sigemptyset(&sigact.sa_mask); |
---|
101 | sigact.sa_flags=0; |
---|
102 | sigaction(SIGWINCH, &sigact, NULL); |
---|
103 | sigaction(SIGALRM, &sigact, NULL); |
---|
104 | |
---|
105 | /* screen init */ |
---|
106 | sprintf(buff, "TERMINFO=%s", TERMINFO); |
---|
107 | putenv(buff); |
---|
108 | initscr(); |
---|
109 | start_color(); |
---|
110 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
111 | use_default_colors(); |
---|
112 | #endif |
---|
113 | raw(); |
---|
114 | noecho(); |
---|
115 | |
---|
116 | /* define simple color pairs */ |
---|
117 | if (has_colors() && COLOR_PAIRS>=8) { |
---|
118 | init_pair(OWL_COLOR_BLACK, COLOR_BLACK, -1); |
---|
119 | init_pair(OWL_COLOR_RED, COLOR_RED, -1); |
---|
120 | init_pair(OWL_COLOR_GREEN, COLOR_GREEN, -1); |
---|
121 | init_pair(OWL_COLOR_YELLOW, COLOR_YELLOW, -1); |
---|
122 | init_pair(OWL_COLOR_BLUE, COLOR_BLUE, -1); |
---|
123 | init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, -1); |
---|
124 | init_pair(OWL_COLOR_CYAN, COLOR_CYAN, -1); |
---|
125 | init_pair(OWL_COLOR_WHITE, COLOR_WHITE, -1); |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | /* owl init */ |
---|
130 | owl_global_init(&g); |
---|
131 | if (debug) owl_global_set_debug_on(&g); |
---|
132 | owl_global_set_startupargs(&g, argcsave, argvsave); |
---|
133 | |
---|
134 | /* create the owl directory, in case it does not exist */ |
---|
135 | dir=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_CONFIG_DIR); |
---|
136 | mkdir(dir, S_IRWXU); |
---|
137 | owl_free(dir); |
---|
138 | |
---|
139 | /* set the tty, either from the command line, or by figuring it out */ |
---|
140 | if (tty) { |
---|
141 | owl_global_set_tty(&g, tty); |
---|
142 | } else { |
---|
143 | owl_global_set_tty(&g, owl_util_get_default_tty()); |
---|
144 | } |
---|
145 | |
---|
146 | /* setup the default filters */ |
---|
147 | /* the personal filter will need to change again when AIM chat's are |
---|
148 | * included. Also, there should be an %aimme% */ |
---|
149 | f=malloc(sizeof(owl_filter)); |
---|
150 | owl_filter_init_fromstring(f, "personal", "( type ^zephyr$ " |
---|
151 | "and class ^message$ and instance ^personal$ " |
---|
152 | "and ( recipient ^%me%$ or sender ^%me%$ ) ) " |
---|
153 | "or ( type ^aim$ )"); |
---|
154 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
155 | |
---|
156 | f=malloc(sizeof(owl_filter)); |
---|
157 | owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or class ^login$"); |
---|
158 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
159 | |
---|
160 | f=malloc(sizeof(owl_filter)); |
---|
161 | owl_filter_init_fromstring(f, "ping", "opcode ^ping$"); |
---|
162 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
163 | |
---|
164 | f=malloc(sizeof(owl_filter)); |
---|
165 | owl_filter_init_fromstring(f, "auto", "opcode ^auto$"); |
---|
166 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
167 | |
---|
168 | f=malloc(sizeof(owl_filter)); |
---|
169 | owl_filter_init_fromstring(f, "login", "class ^login$"); |
---|
170 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
171 | |
---|
172 | f=malloc(sizeof(owl_filter)); |
---|
173 | owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$"); |
---|
174 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
175 | |
---|
176 | f=malloc(sizeof(owl_filter)); |
---|
177 | owl_filter_init_fromstring(f, "out", "direction ^out$"); |
---|
178 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
179 | |
---|
180 | f=malloc(sizeof(owl_filter)); |
---|
181 | owl_filter_init_fromstring(f, "aim", "type ^aim$"); |
---|
182 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
183 | |
---|
184 | f=malloc(sizeof(owl_filter)); |
---|
185 | owl_filter_init_fromstring(f, "zephyr", "type ^zephyr$"); |
---|
186 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
187 | |
---|
188 | f=malloc(sizeof(owl_filter)); |
---|
189 | owl_filter_init_fromstring(f, "none", "false"); |
---|
190 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
191 | |
---|
192 | f=malloc(sizeof(owl_filter)); |
---|
193 | owl_filter_init_fromstring(f, "all", "true"); |
---|
194 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
195 | |
---|
196 | /* set the current view */ |
---|
197 | owl_view_create(owl_global_get_current_view(&g), f); |
---|
198 | |
---|
199 | /* AIM init */ |
---|
200 | owl_aim_init(); |
---|
201 | |
---|
202 | /* process the startup file */ |
---|
203 | owl_function_execstartup(); |
---|
204 | |
---|
205 | /* read the config file */ |
---|
206 | owl_context_set_readconfig(owl_global_get_context(&g)); |
---|
207 | ret=owl_readconfig(configfile); |
---|
208 | if (ret) { |
---|
209 | endwin(); |
---|
210 | printf("\nError parsing configfile\n"); |
---|
211 | exit(1); |
---|
212 | } |
---|
213 | |
---|
214 | /* execute the startup function in the configfile */ |
---|
215 | perlout = owl_config_execute("owl::startup();"); |
---|
216 | if (perlout) owl_free(perlout); |
---|
217 | owl_function_debugmsg("-- Owl Startup --"); |
---|
218 | |
---|
219 | /* hold on to the window names for convenience */ |
---|
220 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
221 | recwin=owl_global_get_curs_recwin(&g); |
---|
222 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
223 | typwin=owl_global_get_curs_typwin(&g); |
---|
224 | tw=owl_global_get_typwin(&g); |
---|
225 | |
---|
226 | wrefresh(sepwin); |
---|
227 | |
---|
228 | /* load zephyr subs */ |
---|
229 | ret=owl_zephyr_loadsubs(NULL); |
---|
230 | if (ret!=-1) { |
---|
231 | owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES); |
---|
232 | } |
---|
233 | |
---|
234 | /* load login subs */ |
---|
235 | if (owl_global_is_loginsubs(&g)) { |
---|
236 | owl_function_loadloginsubs(NULL); |
---|
237 | } |
---|
238 | |
---|
239 | /* zlog in if we need to */ |
---|
240 | if (owl_global_is_startuplogin(&g)) { |
---|
241 | owl_zephyr_zlog_in(); |
---|
242 | } |
---|
243 | |
---|
244 | /* welcome message */ |
---|
245 | strcpy(startupmsg, "-------------------------------------------------------------------------\n"); |
---|
246 | sprintf(buff, "Welcome to owl version %s. Press 'h' for on line help. \n", OWL_VERSION_STRING); |
---|
247 | strcat(startupmsg, buff); |
---|
248 | strcat(startupmsg, " \n"); |
---|
249 | strcat(startupmsg, "If you would like to receive release announcments about owl you can join \n"); |
---|
250 | strcat(startupmsg, "the owl-users@mit.edu mailing list. MIT users can add themselves, \n"); |
---|
251 | strcat(startupmsg, "otherwise send a request to owner-owl-users@mit.edu. ^ ^ \n"); |
---|
252 | strcat(startupmsg, " OvO \n"); |
---|
253 | strcat(startupmsg, "Please report any bugs or suggestions to bug-owl@mit.edu ( ) \n"); |
---|
254 | strcat(startupmsg, "-------------------------------------------------------------------m-m---\n"); |
---|
255 | owl_function_adminmsg("", startupmsg); |
---|
256 | sepbar(NULL); |
---|
257 | |
---|
258 | owl_context_set_interactive(owl_global_get_context(&g)); |
---|
259 | |
---|
260 | nexttimediff=10; |
---|
261 | nexttime=time(NULL); |
---|
262 | |
---|
263 | /* main loop */ |
---|
264 | while (1) { |
---|
265 | |
---|
266 | /* if a resize has been scheduled, deal with it */ |
---|
267 | owl_global_resize(&g, 0, 0); |
---|
268 | |
---|
269 | /* these are here in case a resize changes the windows */ |
---|
270 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
271 | recwin=owl_global_get_curs_recwin(&g); |
---|
272 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
273 | typwin=owl_global_get_curs_typwin(&g); |
---|
274 | |
---|
275 | followlast=owl_global_should_followlast(&g); |
---|
276 | |
---|
277 | /* little hack */ |
---|
278 | now=time(NULL); |
---|
279 | today=localtime(&now); |
---|
280 | if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) { |
---|
281 | if (time(NULL)>nexttime) { |
---|
282 | if (nexttimediff==1) { |
---|
283 | nexttimediff=10; |
---|
284 | } else { |
---|
285 | nexttimediff=1; |
---|
286 | } |
---|
287 | nexttime+=nexttimediff; |
---|
288 | owl_hack_animate(); |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | /* grab incoming zephyrs */ |
---|
293 | newmsgs=0; |
---|
294 | zpendcount=0; |
---|
295 | while(ZPending() || owl_global_messagequeue_pending(&g)) { |
---|
296 | ZNotice_t notice; |
---|
297 | struct sockaddr_in from; |
---|
298 | owl_message *m; |
---|
299 | owl_filter *f; |
---|
300 | |
---|
301 | if (ZPending()) { |
---|
302 | /* grab a zephyr notice, but if we've done 20 without stopping, |
---|
303 | take a break to process keystrokes etc. */ |
---|
304 | if (zpendcount>20) break; |
---|
305 | ZReceiveNotice(¬ice, &from); |
---|
306 | zpendcount++; |
---|
307 | |
---|
308 | /* is this an ack from a zephyr we sent? */ |
---|
309 | if (owl_zephyr_notice_is_ack(¬ice)) { |
---|
310 | owl_zephyr_handle_ack(¬ice); |
---|
311 | continue; |
---|
312 | } |
---|
313 | |
---|
314 | /* if it's a ping and we're not viewing pings then skip it */ |
---|
315 | if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) { |
---|
316 | continue; |
---|
317 | } |
---|
318 | |
---|
319 | /* create the new message */ |
---|
320 | m=owl_malloc(sizeof(owl_message)); |
---|
321 | owl_message_create_from_znotice(m, ¬ice); |
---|
322 | } else if (owl_global_messagequeue_pending(&g)) { |
---|
323 | m=owl_global_messageuque_popmsg(&g); |
---|
324 | } |
---|
325 | |
---|
326 | /* if it's on the puntlist then, nuke it and continue */ |
---|
327 | if (owl_global_message_is_puntable(&g, m)) { |
---|
328 | owl_message_free(m); |
---|
329 | continue; |
---|
330 | } |
---|
331 | |
---|
332 | /* otherwise add it to the global list */ |
---|
333 | owl_messagelist_append_element(owl_global_get_msglist(&g), m); |
---|
334 | newmsgs=1; |
---|
335 | |
---|
336 | /* let the config know the new message has been received */ |
---|
337 | owl_config_getmsg(m, 0); |
---|
338 | |
---|
339 | /* add it to any necessary views; right now there's only the current view */ |
---|
340 | owl_view_consider_message(owl_global_get_current_view(&g), m); |
---|
341 | |
---|
342 | /* do we need to autoreply? */ |
---|
343 | if (owl_global_is_zaway(&g)) { |
---|
344 | owl_zephyr_zaway(m); |
---|
345 | } |
---|
346 | |
---|
347 | /* ring the bell if it's a personal */ |
---|
348 | if (owl_global_is_personalbell(&g) && |
---|
349 | (owl_message_is_personal(m) || |
---|
350 | (owl_message_is_type_aim(m) && owl_message_is_to_me(m)))) { |
---|
351 | owl_function_beep(); |
---|
352 | } |
---|
353 | |
---|
354 | /* if it matches the alert filter, do the alert action */ |
---|
355 | f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g)); |
---|
356 | if (f && owl_filter_message_match(f, m)) { |
---|
357 | owl_function_command(owl_global_get_alert_action(&g)); |
---|
358 | } |
---|
359 | |
---|
360 | /* check for burning ears message */ |
---|
361 | /* this is an unsupported feature */ |
---|
362 | if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) { |
---|
363 | char *buff; |
---|
364 | buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m)); |
---|
365 | /* owl_function_makemsg(buff); */ |
---|
366 | owl_function_adminmsg(buff, ""); |
---|
367 | owl_free(buff); |
---|
368 | owl_function_beep(); |
---|
369 | } |
---|
370 | |
---|
371 | /* log the message if we need to */ |
---|
372 | if (owl_global_is_logging(&g) || owl_global_is_classlogging(&g)) { |
---|
373 | owl_log_incoming(m); |
---|
374 | } |
---|
375 | } |
---|
376 | |
---|
377 | /* If we're logged into AIM, do AIM stuff */ |
---|
378 | if (owl_global_is_aimloggedin(&g)) { |
---|
379 | owl_function_debugmsg("Doing aim processing"); |
---|
380 | owl_aim_process_events(); |
---|
381 | } |
---|
382 | |
---|
383 | /* follow the last message if we're supposed to */ |
---|
384 | if (newmsgs && followlast) { |
---|
385 | owl_function_lastmsg_noredisplay(); |
---|
386 | } |
---|
387 | |
---|
388 | /* do the newmsgproc thing */ |
---|
389 | if (newmsgs) { |
---|
390 | owl_function_do_newmsgproc(); |
---|
391 | } |
---|
392 | |
---|
393 | /* redisplay if necessary */ |
---|
394 | /* this should be optimized to not run if the new messages won't be displayed */ |
---|
395 | if (newmsgs) { |
---|
396 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
397 | sepbar(NULL); |
---|
398 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
399 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
400 | /* TODO: this is a broken kludge */ |
---|
401 | if (owl_global_get_viewwin(&g)) { |
---|
402 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
403 | } |
---|
404 | } |
---|
405 | owl_global_set_needrefresh(&g); |
---|
406 | } |
---|
407 | |
---|
408 | /* if a popwin just came up, refresh it */ |
---|
409 | pw=owl_global_get_popwin(&g); |
---|
410 | if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) { |
---|
411 | owl_popwin_refresh(pw); |
---|
412 | owl_popwin_no_needs_first_refresh(pw); |
---|
413 | owl_global_set_needrefresh(&g); |
---|
414 | /* TODO: this is a broken kludge */ |
---|
415 | if (owl_global_get_viewwin(&g)) { |
---|
416 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
417 | } |
---|
418 | } |
---|
419 | |
---|
420 | /* update the terminal if we need to */ |
---|
421 | if (owl_global_is_needrefresh(&g)) { |
---|
422 | /* leave the cursor in the appropriate window */ |
---|
423 | if (owl_global_is_typwin_active(&g)) { |
---|
424 | owl_function_set_cursor(typwin); |
---|
425 | } else { |
---|
426 | owl_function_set_cursor(sepwin); |
---|
427 | } |
---|
428 | doupdate(); |
---|
429 | owl_global_set_noneedrefresh(&g); |
---|
430 | } |
---|
431 | |
---|
432 | /* Handle all keypresses. |
---|
433 | * If no key has been pressed sleep for a little bit, but |
---|
434 | * otherwise do not, this lets input be grabbed as quickly |
---|
435 | * as possbile */ |
---|
436 | j=wgetch(typwin); |
---|
437 | if (j==ERR) { |
---|
438 | usleep(10); |
---|
439 | continue; |
---|
440 | } |
---|
441 | /* find and activate the current keymap. |
---|
442 | * TODO: this should really get fixed by activating |
---|
443 | * keymaps as we switch between windows... |
---|
444 | */ |
---|
445 | if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) { |
---|
446 | owl_context_set_popless(owl_global_get_context(&g), |
---|
447 | owl_global_get_viewwin(&g)); |
---|
448 | owl_function_activate_keymap("popless"); |
---|
449 | } else if (owl_global_is_typwin_active(&g) |
---|
450 | && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) { |
---|
451 | owl_context_set_editline(owl_global_get_context(&g), tw); |
---|
452 | owl_function_activate_keymap("editline"); |
---|
453 | } else if (owl_global_is_typwin_active(&g) |
---|
454 | && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) { |
---|
455 | owl_context_set_editmulti(owl_global_get_context(&g), tw); |
---|
456 | owl_function_activate_keymap("editmulti"); |
---|
457 | } else { |
---|
458 | owl_context_set_recv(owl_global_get_context(&g)); |
---|
459 | owl_function_activate_keymap("recv"); |
---|
460 | } |
---|
461 | /* now actually handle the keypress */ |
---|
462 | ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j); |
---|
463 | if (ret!=0 && ret!=1) { |
---|
464 | owl_function_makemsg("Unable to handle keypress"); |
---|
465 | } |
---|
466 | } |
---|
467 | } |
---|
468 | |
---|
469 | void sig_handler(int sig) { |
---|
470 | if (sig==SIGWINCH) { |
---|
471 | /* we can't inturrupt a malloc here, so it just sets a flag */ |
---|
472 | owl_function_resize(); |
---|
473 | } |
---|
474 | } |
---|
475 | |
---|
476 | void usage() { |
---|
477 | fprintf(stderr, "Usage: owl [-n] [-d] [-v] [-c <configfile>] [-t <ttyname>]\n"); |
---|
478 | } |
---|