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 "owl.h" |
---|
25 | |
---|
26 | static const char fileIdent[] = "$Id$"; |
---|
27 | |
---|
28 | int main(int argc, char **argv, char **env) { |
---|
29 | WINDOW *recwin, *sepwin, *typwin, *msgwin; |
---|
30 | owl_editwin *tw; |
---|
31 | owl_popwin *pw; |
---|
32 | int j, ret, initialsubs, debug, newzephyrs, argcsave, followlast, nexttimediff; |
---|
33 | struct sigaction sigact; |
---|
34 | char *configfile, *tty, *perlout, **argvsave, buff[LINE], startupmsg[LINE]; |
---|
35 | owl_filter *f; |
---|
36 | time_t nexttime, now; |
---|
37 | struct tm *today; |
---|
38 | |
---|
39 | argcsave=argc; |
---|
40 | argvsave=argv; |
---|
41 | configfile=NULL; |
---|
42 | tty=NULL; |
---|
43 | debug=0; |
---|
44 | if (argc>0) { |
---|
45 | argv++; |
---|
46 | argc--; |
---|
47 | } |
---|
48 | while (argc>0) { |
---|
49 | if (!strcmp(argv[0], "-n")) { |
---|
50 | initialsubs=0; |
---|
51 | argv++; |
---|
52 | argc--; |
---|
53 | } else if (!strcmp(argv[0], "-c")) { |
---|
54 | if (argc<2) { |
---|
55 | fprintf(stderr, "Too few arguments to -c\n"); |
---|
56 | usage(); |
---|
57 | exit(1); |
---|
58 | } |
---|
59 | configfile=argv[1]; |
---|
60 | argv+=2; |
---|
61 | argc-=2; |
---|
62 | } else if (!strcmp(argv[0], "-t")) { |
---|
63 | if (argc<2) { |
---|
64 | fprintf(stderr, "Too few arguments to -t\n"); |
---|
65 | usage(); |
---|
66 | exit(1); |
---|
67 | } |
---|
68 | tty=argv[1]; |
---|
69 | argv+=2; |
---|
70 | argc-=2; |
---|
71 | } else if (!strcmp(argv[0], "-d")) { |
---|
72 | debug=1; |
---|
73 | argv++; |
---|
74 | argc--; |
---|
75 | } else if (!strcmp(argv[0], "-v")) { |
---|
76 | printf("This is owl version %s\n", OWL_VERSION_STRING); |
---|
77 | exit(0); |
---|
78 | } else { |
---|
79 | fprintf(stderr, "Uknown argument\n"); |
---|
80 | usage(); |
---|
81 | exit(1); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | /* zephyr init */ |
---|
86 | if ((ret = ZInitialize()) != ZERR_NONE) { |
---|
87 | com_err("owl",ret,"while initializing"); |
---|
88 | exit(1); |
---|
89 | } |
---|
90 | if ((ret = ZOpenPort(NULL)) != ZERR_NONE) { |
---|
91 | com_err("owl",ret,"while opening port"); |
---|
92 | exit(1); |
---|
93 | } |
---|
94 | |
---|
95 | /* signal handler */ |
---|
96 | sigact.sa_handler=sig_handler; |
---|
97 | sigemptyset(&sigact.sa_mask); |
---|
98 | sigact.sa_flags=0; |
---|
99 | sigaction(SIGWINCH, &sigact, NULL); |
---|
100 | sigaction(SIGALRM, &sigact, NULL); |
---|
101 | |
---|
102 | /* screen init */ |
---|
103 | sprintf(buff, "TERMINFO=%s", TERMINFO); |
---|
104 | putenv(buff); |
---|
105 | initscr(); |
---|
106 | start_color(); |
---|
107 | use_default_colors(); |
---|
108 | raw(); |
---|
109 | noecho(); |
---|
110 | |
---|
111 | /* define simple color pairs */ |
---|
112 | if (has_colors() && COLOR_PAIRS>=8) { |
---|
113 | init_pair(OWL_COLOR_BLACK, COLOR_BLACK, -1); |
---|
114 | init_pair(OWL_COLOR_RED, COLOR_RED, -1); |
---|
115 | init_pair(OWL_COLOR_GREEN, COLOR_GREEN, -1); |
---|
116 | init_pair(OWL_COLOR_YELLOW, COLOR_YELLOW, -1); |
---|
117 | init_pair(OWL_COLOR_BLUE, COLOR_BLUE, -1); |
---|
118 | init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, -1); |
---|
119 | init_pair(OWL_COLOR_CYAN, COLOR_CYAN, -1); |
---|
120 | init_pair(OWL_COLOR_WHITE, COLOR_WHITE, -1); |
---|
121 | } |
---|
122 | |
---|
123 | /* owl init */ |
---|
124 | owl_global_init(&g); |
---|
125 | if (debug) owl_global_set_debug_on(&g); |
---|
126 | owl_global_set_startupargs(&g, argcsave, argvsave); |
---|
127 | owl_context_set_readconfig(owl_global_get_context(&g)); |
---|
128 | |
---|
129 | if (tty) { |
---|
130 | owl_global_set_tty(&g, tty); |
---|
131 | } else { |
---|
132 | owl_global_set_tty(&g, owl_util_get_default_tty()); |
---|
133 | } |
---|
134 | |
---|
135 | /* setup the default filters */ |
---|
136 | f=malloc(sizeof(owl_filter)); |
---|
137 | owl_filter_init_fromstring(f, "personal", "class ^message$ and instance ^personal$ and ( recipient ^%me%$ or sender ^%me%$ )"); |
---|
138 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
139 | |
---|
140 | f=malloc(sizeof(owl_filter)); |
---|
141 | owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or class ^login$"); |
---|
142 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
143 | |
---|
144 | f=malloc(sizeof(owl_filter)); |
---|
145 | owl_filter_init_fromstring(f, "ping", "opcode ^ping$"); |
---|
146 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
147 | |
---|
148 | f=malloc(sizeof(owl_filter)); |
---|
149 | owl_filter_init_fromstring(f, "auto", "opcode ^auto$"); |
---|
150 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
151 | |
---|
152 | f=malloc(sizeof(owl_filter)); |
---|
153 | owl_filter_init_fromstring(f, "login", "class ^login$"); |
---|
154 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
155 | |
---|
156 | f=malloc(sizeof(owl_filter)); |
---|
157 | owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$"); |
---|
158 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
159 | |
---|
160 | f=malloc(sizeof(owl_filter)); |
---|
161 | owl_filter_init_fromstring(f, "out", "direction ^out$"); |
---|
162 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
163 | |
---|
164 | f=malloc(sizeof(owl_filter)); |
---|
165 | owl_filter_init_fromstring(f, "all", "class .*"); |
---|
166 | owl_list_append_element(owl_global_get_filterlist(&g), f); |
---|
167 | |
---|
168 | /* set the current view */ |
---|
169 | owl_view_create(owl_global_get_current_view(&g), f); |
---|
170 | |
---|
171 | /* read the config file */ |
---|
172 | ret=owl_readconfig(configfile); |
---|
173 | if (ret) { |
---|
174 | endwin(); |
---|
175 | printf("\nError parsing configfile\n"); |
---|
176 | exit(1); |
---|
177 | } |
---|
178 | perlout = owl_config_execute("owl::startup();"); |
---|
179 | if (perlout) owl_free(perlout); |
---|
180 | owl_function_debugmsg("-- Owl Startup --"); |
---|
181 | |
---|
182 | /* hold on to the window names for convenience */ |
---|
183 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
184 | recwin=owl_global_get_curs_recwin(&g); |
---|
185 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
186 | typwin=owl_global_get_curs_typwin(&g); |
---|
187 | tw=owl_global_get_typwin(&g); |
---|
188 | |
---|
189 | wrefresh(sepwin); |
---|
190 | |
---|
191 | /* load zephyr subs */ |
---|
192 | ret=owl_zephyr_loadsubs(NULL); |
---|
193 | if (ret!=-1) { |
---|
194 | owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES); |
---|
195 | } |
---|
196 | |
---|
197 | /* load login subs */ |
---|
198 | if (owl_global_is_loginsubs(&g)) { |
---|
199 | loadloginsubs(NULL); |
---|
200 | } |
---|
201 | |
---|
202 | /* zlog in if we need to */ |
---|
203 | if (owl_global_is_startuplogin(&g)) { |
---|
204 | owl_function_zlog_in(); |
---|
205 | } |
---|
206 | |
---|
207 | /* welcome message */ |
---|
208 | strcpy(startupmsg, "-------------------------------------------------------------------------\n"); |
---|
209 | sprintf(buff, "Welcome to owl version %s. Press 'h' for on line help. \n", OWL_VERSION_STRING); |
---|
210 | strcat(startupmsg, buff); |
---|
211 | strcat(startupmsg, " \n"); |
---|
212 | strcat(startupmsg, "If you would like to receive release announcments about owl you can join \n"); |
---|
213 | strcat(startupmsg, "the owl-users@mit.edu mailing list. MIT users can add themselves, \n"); |
---|
214 | strcat(startupmsg, "otherwise send a request to owner-owl-users@mit.edu. ^ ^ \n"); |
---|
215 | strcat(startupmsg, " OvO \n"); |
---|
216 | strcat(startupmsg, "Please report any bugs or suggestions to bug-owl@mit.edu ( ) \n"); |
---|
217 | strcat(startupmsg, "-------------------------------------------------------------------m-m---\n"); |
---|
218 | |
---|
219 | owl_function_adminmsg("", startupmsg); |
---|
220 | sepbar(NULL); |
---|
221 | |
---|
222 | owl_context_set_interactive(owl_global_get_context(&g)); |
---|
223 | |
---|
224 | nexttimediff=10; |
---|
225 | nexttime=time(NULL); |
---|
226 | |
---|
227 | /* main loop */ |
---|
228 | while (1) { |
---|
229 | |
---|
230 | /* if a resize has been scheduled, deal with it */ |
---|
231 | owl_global_resize(&g, 0, 0); |
---|
232 | |
---|
233 | /* these are here in case a resize changes the windows */ |
---|
234 | msgwin=owl_global_get_curs_msgwin(&g); |
---|
235 | recwin=owl_global_get_curs_recwin(&g); |
---|
236 | sepwin=owl_global_get_curs_sepwin(&g); |
---|
237 | typwin=owl_global_get_curs_typwin(&g); |
---|
238 | |
---|
239 | followlast=owl_global_should_followlast(&g); |
---|
240 | |
---|
241 | /* little hack */ |
---|
242 | now=time(NULL); |
---|
243 | today=localtime(&now); |
---|
244 | if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) { |
---|
245 | if (time(NULL)>nexttime) { |
---|
246 | if (nexttimediff==1) { |
---|
247 | nexttimediff=10; |
---|
248 | } else { |
---|
249 | nexttimediff=1; |
---|
250 | } |
---|
251 | nexttime+=nexttimediff; |
---|
252 | owl_hack_animate(); |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | /* grab incoming zephyrs */ |
---|
257 | newzephyrs=0; |
---|
258 | while(ZPending()) { |
---|
259 | ZNotice_t notice; |
---|
260 | struct sockaddr_in from; |
---|
261 | owl_message *m; |
---|
262 | |
---|
263 | ZReceiveNotice(¬ice, &from); |
---|
264 | |
---|
265 | /* is this an ack from a zephyr we sent? */ |
---|
266 | if (owl_zephyr_notice_is_ack(¬ice)) { |
---|
267 | owl_zephyr_handle_ack(¬ice); |
---|
268 | continue; |
---|
269 | } |
---|
270 | |
---|
271 | /* if it's a ping and we're not viewing pings then skip it */ |
---|
272 | if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) { |
---|
273 | continue; |
---|
274 | } |
---|
275 | |
---|
276 | /* create the new message */ |
---|
277 | m=owl_malloc(sizeof(owl_message)); |
---|
278 | owl_message_create_from_znotice(m, ¬ice); |
---|
279 | |
---|
280 | /* if it's on the puntlist then, nuke it and continue */ |
---|
281 | if (owl_global_message_is_puntable(&g, m)) { |
---|
282 | owl_message_free(m); |
---|
283 | continue; |
---|
284 | } |
---|
285 | |
---|
286 | /* otherwise add it to the global list */ |
---|
287 | owl_messagelist_append_element(owl_global_get_msglist(&g), m); |
---|
288 | newzephyrs=1; |
---|
289 | |
---|
290 | /* let the config know the new message has been received */ |
---|
291 | owl_config_getmsg(m, 0); |
---|
292 | |
---|
293 | /* add it to any necessary views; right now there's only the current view */ |
---|
294 | owl_view_consider_message(owl_global_get_current_view(&g), m); |
---|
295 | |
---|
296 | /* do we need to autoreply? */ |
---|
297 | if (owl_global_is_zaway(&g)) { |
---|
298 | owl_zephyr_zaway(m); |
---|
299 | } |
---|
300 | |
---|
301 | /* ring the bell if it's a personal */ |
---|
302 | if (owl_global_is_personalbell(&g) && owl_message_is_personal(m)) { |
---|
303 | owl_function_beep(); |
---|
304 | } |
---|
305 | |
---|
306 | /* check for burning ears message */ |
---|
307 | /* this is an unsupported feature */ |
---|
308 | if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) { |
---|
309 | char *buff; |
---|
310 | buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m)); |
---|
311 | /* owl_function_makemsg(buff); */ |
---|
312 | owl_function_adminmsg(buff, ""); |
---|
313 | owl_free(buff); |
---|
314 | owl_function_beep(); |
---|
315 | } |
---|
316 | |
---|
317 | /* log the zephyr if we need to */ |
---|
318 | if (owl_global_is_logging(&g) || owl_global_is_classlogging(&g)) { |
---|
319 | owl_log_incoming(m); |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | /* follow the last message if we're supposed to */ |
---|
324 | if (newzephyrs && followlast) { |
---|
325 | owl_function_lastmsg_noredisplay(); |
---|
326 | } |
---|
327 | |
---|
328 | /* check if newmsgproc is active, if not but the option is on, |
---|
329 | make it active */ |
---|
330 | if (newzephyrs) { |
---|
331 | owl_function_do_newmsgproc(); |
---|
332 | } |
---|
333 | |
---|
334 | /* redisplay if necessary */ |
---|
335 | if (newzephyrs) { |
---|
336 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
337 | sepbar(NULL); |
---|
338 | if (owl_popwin_is_active(owl_global_get_popwin(&g))) { |
---|
339 | owl_popwin_refresh(owl_global_get_popwin(&g)); |
---|
340 | /* TODO: this is a broken kludge */ |
---|
341 | if (owl_global_get_viewwin(&g)) { |
---|
342 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
343 | } |
---|
344 | } |
---|
345 | owl_global_set_needrefresh(&g); |
---|
346 | } |
---|
347 | |
---|
348 | /* if a popwin just came up, refresh it */ |
---|
349 | pw=owl_global_get_popwin(&g); |
---|
350 | if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) { |
---|
351 | owl_popwin_refresh(pw); |
---|
352 | owl_popwin_no_needs_first_refresh(pw); |
---|
353 | owl_global_set_needrefresh(&g); |
---|
354 | /* TODO: this is a broken kludge */ |
---|
355 | if (owl_global_get_viewwin(&g)) { |
---|
356 | owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0); |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | /* update the terminal if we need to */ |
---|
361 | if (owl_global_is_needrefresh(&g)) { |
---|
362 | /* leave the cursor in the appropriate window */ |
---|
363 | if (owl_global_is_typwin_active(&g)) { |
---|
364 | owl_function_set_cursor(typwin); |
---|
365 | } else { |
---|
366 | owl_function_set_cursor(sepwin); |
---|
367 | } |
---|
368 | doupdate(); |
---|
369 | owl_global_set_noneedrefresh(&g); |
---|
370 | } |
---|
371 | |
---|
372 | /* handle all keypresses */ |
---|
373 | j=wgetch(typwin); |
---|
374 | if (j==ERR) { |
---|
375 | usleep(10); |
---|
376 | continue; |
---|
377 | } |
---|
378 | /* find and activate the current keymap. |
---|
379 | * TODO: this should really get fixed by activating |
---|
380 | * keymaps as we switch between windows... |
---|
381 | */ |
---|
382 | if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) { |
---|
383 | owl_context_set_popless(owl_global_get_context(&g), |
---|
384 | owl_global_get_viewwin(&g)); |
---|
385 | owl_function_activate_keymap("popless"); |
---|
386 | } else if (owl_global_is_typwin_active(&g) |
---|
387 | && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) { |
---|
388 | owl_context_set_editline(owl_global_get_context(&g), tw); |
---|
389 | owl_function_activate_keymap("editline"); |
---|
390 | } else if (owl_global_is_typwin_active(&g) |
---|
391 | && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) { |
---|
392 | owl_context_set_editmulti(owl_global_get_context(&g), tw); |
---|
393 | owl_function_activate_keymap("editmulti"); |
---|
394 | } else { |
---|
395 | owl_context_set_recv(owl_global_get_context(&g)); |
---|
396 | owl_function_activate_keymap("recv"); |
---|
397 | } |
---|
398 | /* now actually handle the keypress */ |
---|
399 | ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j); |
---|
400 | if (ret!=0 && ret!=1) { |
---|
401 | owl_function_makemsg("Unable to handle keypress"); |
---|
402 | } |
---|
403 | } |
---|
404 | |
---|
405 | } |
---|
406 | |
---|
407 | void sig_handler(int sig) { |
---|
408 | if (sig==SIGWINCH) { |
---|
409 | /* we can't inturrupt a malloc here, so it just sets a flag */ |
---|
410 | owl_function_resize(); |
---|
411 | } |
---|
412 | } |
---|
413 | |
---|
414 | void usage() { |
---|
415 | fprintf(stderr, "Usage: owl [-n] [-d] [-v] [-c <configfile>] [-t <ttyname>]\n"); |
---|
416 | } |
---|