source: owl.c @ 7d4fbcd

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