source: owl.c @ 61e79a9

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 61e79a9 was 61e79a9, checked in by James M. Kretchmar <kretch@mit.edu>, 19 years ago
The tty variable now controls the zephyr location tty name
  • Property mode set to 100644
File size: 11.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 <sys/param.h>
22#include "owl.h"
23
24static const char fileIdent[] = "$Id$";
25
26int main(int argc, char **argv, char **env) {
27  WINDOW *recwin, *sepwin, *typwin, *msgwin;
28  owl_editwin *tw;
29  owl_popwin *pw;
30  int j, ret, initialsubs, debug, newzephyrs, argcsave, followlast;
31  struct sigaction sigact;
32  char *configfile, *tty, *perlout;
33  char buff[LINE], startupmsg[LINE];
34  char **argvsave;
35  owl_filter *f;
36  time_t nexttime;
37  int nexttimediff;
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#if 0 /* Removed to prevent flashing of popwin */
112  intrflush(stdscr,FALSE);
113  keypad(stdscr,TRUE);
114  nodelay(stdscr,1);
115  meta(stdscr,TRUE);
116#endif /*0*/
117
118  /* define simple color pairs */
119  if (has_colors() && COLOR_PAIRS>=8) {
120    init_pair(OWL_COLOR_BLACK,   COLOR_BLACK,   -1);
121    init_pair(OWL_COLOR_RED,     COLOR_RED,     -1);
122    init_pair(OWL_COLOR_GREEN,   COLOR_GREEN,   -1);
123    init_pair(OWL_COLOR_YELLOW,  COLOR_YELLOW,  -1);
124    init_pair(OWL_COLOR_BLUE,    COLOR_BLUE,    -1);
125    init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, -1);
126    init_pair(OWL_COLOR_CYAN,    COLOR_CYAN,    -1);
127    init_pair(OWL_COLOR_WHITE,   COLOR_WHITE,   -1);
128  }
129   
130  /* owl init */
131  owl_global_init(&g);
132  if (debug) owl_global_set_debug_on(&g);
133  owl_global_set_startupargs(&g, argcsave, argvsave);
134  owl_context_set_readconfig(owl_global_get_context(&g));
135  if (tty) {
136    owl_global_set_tty(&g, tty);
137  } else {
138    owl_global_set_tty(&g, owl_util_get_default_tty());
139  }
140
141  /* setup the default filters */
142  f=malloc(sizeof(owl_filter));
143  owl_filter_init_fromstring(f, "personal", "( class ^message$ and instance ^personal$ ) or ( type ^admin$ and recipient .+ )"); /* fix to use admintype */
144  owl_list_append_element(owl_global_get_filterlist(&g), f);
145
146  f=malloc(sizeof(owl_filter));
147  owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or class ^login$");
148  owl_list_append_element(owl_global_get_filterlist(&g), f);
149
150  f=malloc(sizeof(owl_filter));
151  owl_filter_init_fromstring(f, "ping", "opcode ^ping$");
152  owl_list_append_element(owl_global_get_filterlist(&g), f);
153
154  f=malloc(sizeof(owl_filter));
155  owl_filter_init_fromstring(f, "auto", "opcode ^auto$");
156  owl_list_append_element(owl_global_get_filterlist(&g), f);
157
158  f=malloc(sizeof(owl_filter));
159  owl_filter_init_fromstring(f, "login", "class ^login$");
160  owl_list_append_element(owl_global_get_filterlist(&g), f);
161
162  f=malloc(sizeof(owl_filter));
163  owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$");
164  owl_list_append_element(owl_global_get_filterlist(&g), f);
165
166  f=malloc(sizeof(owl_filter));
167  owl_filter_init_fromstring(f, "all", "class .*");
168  owl_list_append_element(owl_global_get_filterlist(&g), f);
169
170  /* set the current view */
171  owl_view_create(owl_global_get_current_view(&g), f);
172
173  /* read the config file */
174  ret=owl_readconfig(configfile);
175  if (ret) {
176    endwin();
177    printf("\nError parsing configfile\n");
178    exit(1);
179  }
180  perlout = owl_config_execute("owl::startup();");
181  if (perlout) owl_free(perlout);
182  owl_function_debugmsg("-- Owl Startup --");
183 
184  /* hold on to the window names for convenience */
185  msgwin=owl_global_get_curs_msgwin(&g);
186  recwin=owl_global_get_curs_recwin(&g);
187  sepwin=owl_global_get_curs_sepwin(&g);
188  typwin=owl_global_get_curs_typwin(&g);
189
190  tw=owl_global_get_typwin(&g);
191
192  wrefresh(sepwin);
193
194  /* load zephyr subs */
195  ret=owl_zephyr_loadsubs(NULL);
196  if (ret!=-1) {
197    owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
198  }
199
200  /* load login subs */
201  if (owl_global_is_loginsubs(&g)) {
202    loadloginsubs(NULL);
203  }
204
205  /* zlog in if we need to */
206  if (owl_global_is_startuplogin(&g)) {
207    owl_function_zlog_in();
208  }
209
210  /* welcome message */
211  sprintf(buff, "Welcome to owl version %s.\n", OWL_VERSION_STRING);
212  strcpy(startupmsg, "-------------------------------------------------------------------------\n");
213  strcat(startupmsg, buff);
214  strcat(startupmsg, "Press 'h' for on line help.                                   ^ ^        \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  /* owl_function_makemsg(buff); */
221  sepbar(NULL);
222 
223  owl_context_set_interactive(owl_global_get_context(&g));
224
225  nexttimediff=20;
226  nexttime=time(NULL);
227
228  /* main loop */
229  while (1) {
230
231    /* if a resize has been scheduled, deal with it */
232    owl_global_resize(&g, 0, 0);
233
234    /* these are here in case a resize changes the windows */
235    msgwin=owl_global_get_curs_msgwin(&g);
236    recwin=owl_global_get_curs_recwin(&g);
237    sepwin=owl_global_get_curs_sepwin(&g);
238    typwin=owl_global_get_curs_typwin(&g);
239
240    followlast=owl_global_should_followlast(&g);
241
242    /* little hack */
243    if (0 && owl_global_get_runtime(&g)<300) {
244      if (time(NULL)>nexttime) {
245        if (nexttimediff==1) {
246          nexttimediff=20;
247        } else {
248          nexttimediff=1;
249        }
250        nexttime+=nexttimediff;
251        owl_hack_animate();
252      }
253    }
254
255    /* grab incoming zephyrs */
256    newzephyrs=0;
257    while(ZPending()) {
258      ZNotice_t notice;
259      struct sockaddr_in from;
260      owl_message *m;
261
262      ZReceiveNotice(&notice, &from);
263
264      /* is this an ack from a zephyr we sent? */
265      if (owl_zephyr_notice_is_ack(&notice)) {
266        owl_zephyr_handle_ack(&notice);
267        continue;
268      }
269     
270      /* if it's a ping and we're not viewing pings then skip it */
271      if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
272        continue;
273      }
274
275      /* create the new message */
276      m=owl_malloc(sizeof(owl_message));
277      owl_message_create_from_zephyr(m, &notice);
278     
279      /* if it's on the puntlist then, nuke it and continue */
280      if (owl_global_message_is_puntable(&g, m)) {
281        owl_message_free(m);
282        continue;
283      }
284
285      /* otherwise add it to the global list */
286      owl_messagelist_append_element(owl_global_get_msglist(&g), m);
287      newzephyrs=1;
288
289      /* let the config know the new message has been received */
290      owl_config_getmsg(m, 0);
291
292      /* add it to any necessary views; right now there's only the current view */
293      owl_view_consider_message(owl_global_get_current_view(&g), m);
294
295      /* do we need to autoreply? */
296      if (owl_global_is_zaway(&g)) {
297        owl_zephyr_zaway(m);
298      }
299
300      /* ring the bell if it's a personal */
301      if (owl_global_is_personalbell(&g) && owl_message_is_personal(m)) {
302        owl_function_beep();
303        owl_global_set_needrefresh(&g);
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    /* redisplay if necessary */
329    if (newzephyrs) {
330      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
331      sepbar(NULL);
332      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
333        owl_popwin_refresh(owl_global_get_popwin(&g));
334        /* TODO: this is a broken kludge */
335        if (owl_global_get_viewwin(&g)) {
336          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
337        }
338      }
339      owl_global_set_needrefresh(&g);
340    }
341
342    /* if a popwin just came up, refresh it */
343    pw=owl_global_get_popwin(&g);
344    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
345      owl_popwin_refresh(pw);
346      owl_popwin_no_needs_first_refresh(pw);
347      owl_global_set_needrefresh(&g);
348      /* TODO: this is a broken kludge */
349      if (owl_global_get_viewwin(&g)) {
350        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
351      }
352    }
353
354    /* update the terminal if we need to */
355    if (owl_global_is_needrefresh(&g)) {
356      /* leave the cursor in the appropriate window */
357      if (owl_global_is_typwin_active(&g)) {
358        owl_function_set_cursor(typwin);
359      } else {
360        owl_function_set_cursor(sepwin);
361      }
362      doupdate();
363      owl_global_set_noneedrefresh(&g);
364    }
365
366    /* handle all keypresses */
367    j=wgetch(typwin);
368    if (j==ERR) {
369      usleep(10);
370      continue;
371    }
372    /* find and activate the current keymap.
373     * TODO: this should really get fixed by activating
374     * keymaps as we switch between windows...
375     */
376    if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
377      owl_context_set_popless(owl_global_get_context(&g), 
378                              owl_global_get_viewwin(&g));
379      owl_function_activate_keymap("popless");
380    } else if (owl_global_is_typwin_active(&g) 
381               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
382      owl_context_set_editline(owl_global_get_context(&g), tw);
383      owl_function_activate_keymap("editline");
384    } else if (owl_global_is_typwin_active(&g) 
385               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
386      owl_context_set_editmulti(owl_global_get_context(&g), tw);
387      owl_function_activate_keymap("editmulti");
388    } else {
389      owl_context_set_recv(owl_global_get_context(&g));
390      owl_function_activate_keymap("recv");
391    }
392    /* now actually handle the keypress */
393    ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
394    if (ret!=0 && ret!=1) {
395      owl_function_makemsg("Unable to handle keypress");
396    }
397  }
398
399}
400
401void sig_handler(int sig) {
402  if (sig==SIGWINCH) {
403    /* we can't inturrupt a malloc here, so it just sets a flag */
404    owl_function_resize();
405  }
406}
407
408void usage() {
409  fprintf(stderr, "Usage: owl [-n] [-d] [-v] [-c <configfile>] [-t <ttyname>]\n");
410}
Note: See TracBrowser for help on using the repository browser.