source: owl.c @ 75be7c0

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 75be7c0 was 0236842, checked in by James M. Kretchmar <kretch@mit.edu>, 22 years ago
Added filters "ping", "auto" and "login" by default.
  • Property mode set to 100644
File size: 11.5 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
37  argcsave=argc;
38  argvsave=argv;
39  configfile=NULL;
40  tty=NULL;
41  debug=0;
42  if (argc>0) {
43    argv++;
44    argc--;
45  }
46  while (argc>0) {
47    if (!strcmp(argv[0], "-n")) {
48      initialsubs=0;
49      argv++;
50      argc--;
51    } else if (!strcmp(argv[0], "-c")) {
52      if (argc<2) {
53        fprintf(stderr, "Too few arguments to -c\n");
54        usage();
55        exit(1);
56      }
57      configfile=argv[1];
58      argv+=2;
59      argc-=2;
60    } else if (!strcmp(argv[0], "-t")) {
61      if (argc<2) {
62        fprintf(stderr, "Too few arguments to -t\n");
63        usage();
64        exit(1);
65      }
66      tty=argv[1];
67      argv+=2;
68      argc-=2;
69    } else if (!strcmp(argv[0], "-d")) {
70      debug=1;
71      argv++;
72      argc--;
73    } else if (!strcmp(argv[0], "-v")) {
74      printf("This is owl version %s\n", OWL_VERSION_STRING);
75      exit(0);
76    } else {
77      fprintf(stderr, "Uknown argument\n");
78      usage();       
79      exit(1);
80    }
81  }
82
83  /* zephyr init */
84  if ((ret = ZInitialize()) != ZERR_NONE) {
85    com_err("owl",ret,"while initializing");
86    exit(1);
87  }
88  if ((ret = ZOpenPort(NULL)) != ZERR_NONE) {
89    com_err("owl",ret,"while opening port");
90    exit(1);
91  }
92
93  /* signal handler */
94  sigact.sa_handler=sig_handler;
95  sigemptyset(&sigact.sa_mask);
96  sigact.sa_flags=0;
97  sigaction(SIGWINCH, &sigact, NULL);
98  sigaction(SIGALRM, &sigact, NULL);
99
100  /* screen init */
101  sprintf(buff, "TERMINFO=%s", TERMINFO);
102  putenv(buff);
103  initscr();
104  start_color();
105  use_default_colors();
106  raw();
107  noecho();
108
109#if 0 /* Removed to prevent flashing of popwin */
110  intrflush(stdscr,FALSE);
111  keypad(stdscr,TRUE);
112  nodelay(stdscr,1);
113  meta(stdscr,TRUE);
114#endif /*0*/
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  /* owl init */
129  owl_global_init(&g);
130  if (debug) owl_global_set_debug_on(&g);
131  owl_global_set_startupargs(&g, argcsave, argvsave);
132  owl_global_set_tty(&g, tty);
133  owl_context_set_readconfig(owl_global_get_context(&g));
134
135  /* setup the default filters */
136  f=malloc(sizeof(owl_filter));
137  owl_filter_init_fromstring(f, "personal", "( class ^message$ and instance ^personal$ ) or ( type ^admin$ and recipient .+ )"); /* fix to use admintype */
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, "all", "class .*");
162  owl_list_append_element(owl_global_get_filterlist(&g), f);
163
164  /* set the current view */
165  owl_view_create(owl_global_get_current_view(&g), f);
166
167  /* read the config file */
168  ret=owl_readconfig(configfile);
169  if (ret) {
170    endwin();
171    printf("\nError parsing configfile\n");
172    exit(1);
173  }
174  perlout = owl_config_execute("owl::startup();");
175  if (perlout) owl_free(perlout);
176  owl_function_debugmsg("-- Owl Startup --");
177 
178  /* hold on to the window names for convenience */
179  msgwin=owl_global_get_curs_msgwin(&g);
180  recwin=owl_global_get_curs_recwin(&g);
181  sepwin=owl_global_get_curs_sepwin(&g);
182  typwin=owl_global_get_curs_typwin(&g);
183
184  tw=owl_global_get_typwin(&g);
185
186  wrefresh(sepwin);
187
188  /* load zephyr subs */
189  ret=owl_zephyr_loadsubs(NULL);
190  if (ret!=-1) {
191    owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
192  }
193
194  /* load login subs */
195  if (owl_global_is_loginsubs(&g)) {
196    loadloginsubs(NULL);
197  }
198
199  /* zlog in if we need to */
200  if (owl_global_is_startuplogin(&g)) {
201    owl_function_zlog_in();
202  }
203
204  /* welcome message */
205  sprintf(buff, "Welcome to owl version %s.\n", OWL_VERSION_STRING);
206  strcpy(startupmsg, "-------------------------------------------------------------------------\n");
207  strcat(startupmsg, buff);
208  strcat(startupmsg, "Press 'h' for on line help.                                   ^ ^        \n");
209  strcat(startupmsg, "                                                              OvO        \n");
210  strcat(startupmsg, "Please report any bugs or suggestions to bug-owl@mit.edu     (   )       \n");
211  strcat(startupmsg, "--------------------------------------------------------------m-m--------\n");
212 
213  owl_function_adminmsg("", startupmsg);
214  /* owl_function_makemsg(buff); */
215  sepbar(NULL);
216 
217  owl_context_set_interactive(owl_global_get_context(&g));
218
219  /* main loop */
220  while (1) {
221
222    /* if a resize has been scheduled, deal with it */
223    owl_global_resize(&g, 0, 0);
224
225    /* these are here in case a resize changes the windows */
226    msgwin=owl_global_get_curs_msgwin(&g);
227    recwin=owl_global_get_curs_recwin(&g);
228    sepwin=owl_global_get_curs_sepwin(&g);
229    typwin=owl_global_get_curs_typwin(&g);
230
231    followlast=owl_global_should_followlast(&g);
232
233    /* grab incoming zephyrs */
234    newzephyrs=0;
235    while(ZPending()) {
236      ZNotice_t notice;
237      struct sockaddr_in from;
238      owl_message *m;
239
240      ZReceiveNotice(&notice, &from);
241
242      /* is this an ack from a zephyr we sent? */
243      if (owl_zephyr_notice_is_ack(&notice)) {
244        owl_zephyr_handle_ack(&notice);
245        continue;
246      }
247     
248      /* if it's a ping and we're not viewing pings then skip it */
249      if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
250        continue;
251      }
252
253      /* create the new message */
254      m=owl_malloc(sizeof(owl_message));
255      owl_message_create_from_zephyr(m, &notice);
256     
257      /* if it's on the puntlist then, nuke it and continue */
258      if (owl_global_message_is_puntable(&g, m)) {
259        owl_message_free(m);
260        continue;
261      }
262
263      /* otherwise add it to the global list */
264      owl_messagelist_append_element(owl_global_get_msglist(&g), m);
265      newzephyrs=1;
266
267      /* let the config know the new message has been received */
268      owl_config_getmsg(m, 0);
269
270      /* add it to any necessary views; right now there's only the current view */
271      owl_view_consider_message(owl_global_get_current_view(&g), m);
272
273      /* do we need to autoreply? */
274      if (owl_global_is_zaway(&g)) {
275        owl_zephyr_zaway(m);
276      }
277
278      /* ring the bell if it's a personal */
279      if (owl_global_is_personalbell(&g) && owl_message_is_personal(m)) {
280        owl_function_beep();
281        owl_global_set_needrefresh(&g);
282      }
283
284      /* check for burning ears message */
285      /* this is an unsupported feature */
286      if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) {
287        char *buff;
288        buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m));
289        /* owl_function_makemsg(buff); */
290        owl_function_adminmsg(buff, "");
291        owl_free(buff);
292        owl_function_beep();
293      }
294
295      /* log the zephyr if we need to */
296      if (owl_global_is_logging(&g) || owl_global_is_classlogging(&g)) {
297        owl_log_incoming(m);
298      }
299    }
300
301    /* follow the last message if we're supposed to */
302    if (newzephyrs && followlast) {
303      owl_function_lastmsg_noredisplay();
304    }
305   
306    /* redisplay if necessary */
307    if (newzephyrs) {
308      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
309      sepbar(NULL);
310      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
311        owl_popwin_refresh(owl_global_get_popwin(&g));
312        /* TODO: this is a broken kludge */
313        if (owl_global_get_viewwin(&g)) {
314          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
315        }
316      }
317      owl_global_set_needrefresh(&g);
318    }
319
320    /* if a popwin just came up, refresh it */
321    pw=owl_global_get_popwin(&g);
322    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
323      owl_popwin_refresh(pw);
324      owl_popwin_no_needs_first_refresh(pw);
325      owl_global_set_needrefresh(&g);
326      /* TODO: this is a broken kludge */
327      if (owl_global_get_viewwin(&g)) {
328        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
329      }
330    }
331
332    /* update the terminal if we need to */
333    if (owl_global_is_needrefresh(&g)) {
334      /* leave the cursor in the appropriate window */
335      if (owl_global_is_typwin_active(&g)) {
336        owl_function_set_cursor(typwin);
337      } else {
338        owl_function_set_cursor(sepwin);
339      }
340      doupdate();
341      owl_global_set_noneedrefresh(&g);
342    }
343
344    /* handle all keypresses */
345    j=wgetch(typwin);
346    if (j==ERR) {
347      usleep(10);
348      continue;
349    }
350    /* find and activate the current keymap.
351     * TODO: this should really get fixed by activating
352     * keymaps as we switch between windows...
353     */
354    if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
355      owl_context_set_popless(owl_global_get_context(&g), 
356                              owl_global_get_viewwin(&g));
357      owl_function_activate_keymap("popless");
358    } else if (owl_global_is_typwin_active(&g) 
359               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
360      owl_context_set_editline(owl_global_get_context(&g), tw);
361      owl_function_activate_keymap("editline");
362    } else if (owl_global_is_typwin_active(&g) 
363               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
364      owl_context_set_editmulti(owl_global_get_context(&g), tw);
365      owl_function_activate_keymap("editmulti");
366    } else {
367      owl_context_set_recv(owl_global_get_context(&g));
368      owl_function_activate_keymap("recv");
369    }
370    /* now actually handle the keypress */
371    ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
372    if (ret!=0 && ret!=1) {
373      owl_function_makemsg("Unable to handle keypress");
374    }
375  }
376
377}
378
379void sig_handler(int sig) {
380  if (sig==SIGWINCH) {
381    /* we can't inturrupt a malloc here, so it just sets a flag */
382    owl_function_resize();
383  }
384}
385
386void usage() {
387  fprintf(stderr, "Usage: owl [-n] [-d] [-v] [-c <configfile>] [-t <ttyname>]\n");
388}
Note: See TracBrowser for help on using the repository browser.