source: owl.c @ 31e48a3

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