source: owl.c @ 060b3b4

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 060b3b4 was 89426ab, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
In filters 'true' and 'false' are now valid tokens. The 'all' filter has been redefinied to be 'true' and there is a 'none' filter defined as 'false'
  • Property mode set to 100644
File size: 12.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 <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, 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, "none", "false");
166  owl_list_append_element(owl_global_get_filterlist(&g), f);
167
168  f=malloc(sizeof(owl_filter));
169  owl_filter_init_fromstring(f, "all", "true");
170  owl_list_append_element(owl_global_get_filterlist(&g), f);
171
172  /* set the current view */
173  owl_view_create(owl_global_get_current_view(&g), f);
174
175  /* read the config file */
176  ret=owl_readconfig(configfile);
177  if (ret) {
178    endwin();
179    printf("\nError parsing configfile\n");
180    exit(1);
181  }
182  perlout = owl_config_execute("owl::startup();");
183  if (perlout) owl_free(perlout);
184  owl_function_debugmsg("-- Owl Startup --");
185 
186  /* hold on to the window names for convenience */
187  msgwin=owl_global_get_curs_msgwin(&g);
188  recwin=owl_global_get_curs_recwin(&g);
189  sepwin=owl_global_get_curs_sepwin(&g);
190  typwin=owl_global_get_curs_typwin(&g);
191  tw=owl_global_get_typwin(&g);
192
193  wrefresh(sepwin);
194
195  /* load zephyr subs */
196  ret=owl_zephyr_loadsubs(NULL);
197  if (ret!=-1) {
198    owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
199  }
200
201  /* load login subs */
202  if (owl_global_is_loginsubs(&g)) {
203    loadloginsubs(NULL);
204  }
205
206  /* zlog in if we need to */
207  if (owl_global_is_startuplogin(&g)) {
208    owl_function_zlog_in();
209  }
210
211  /* welcome message */
212  strcpy(startupmsg, "-------------------------------------------------------------------------\n");
213  sprintf(buff,      "Welcome to owl version %s.  Press 'h' for on line help. \n", OWL_VERSION_STRING);
214  strcat(startupmsg, buff);
215  strcat(startupmsg, "                                                                         \n");
216  strcat(startupmsg, "If you would like to receive release announcments about owl you can join \n");
217  strcat(startupmsg, "the owl-users@mit.edu mailing list.  MIT users can add themselves,       \n");
218  strcat(startupmsg, "otherwise send a request to owner-owl-users@mit.edu.               ^ ^   \n");
219  strcat(startupmsg, "                                                                   OvO   \n");
220  strcat(startupmsg, "Please report any bugs or suggestions to bug-owl@mit.edu          (   )  \n");
221  strcat(startupmsg, "-------------------------------------------------------------------m-m---\n");
222 
223  owl_function_adminmsg("", startupmsg);
224  sepbar(NULL);
225 
226  owl_context_set_interactive(owl_global_get_context(&g));
227
228  nexttimediff=10;
229  nexttime=time(NULL);
230
231  /* main loop */
232  while (1) {
233
234    /* if a resize has been scheduled, deal with it */
235    owl_global_resize(&g, 0, 0);
236
237    /* these are here in case a resize changes the windows */
238    msgwin=owl_global_get_curs_msgwin(&g);
239    recwin=owl_global_get_curs_recwin(&g);
240    sepwin=owl_global_get_curs_sepwin(&g);
241    typwin=owl_global_get_curs_typwin(&g);
242
243    followlast=owl_global_should_followlast(&g);
244
245    /* little hack */
246    now=time(NULL);
247    today=localtime(&now);
248    if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) {
249      if (time(NULL)>nexttime) {
250        if (nexttimediff==1) {
251          nexttimediff=10;
252        } else {
253          nexttimediff=1;
254        }
255        nexttime+=nexttimediff;
256        owl_hack_animate();
257      }
258    }
259
260    /* grab incoming zephyrs */
261    newzephyrs=0;
262    while(ZPending()) {
263      ZNotice_t notice;
264      struct sockaddr_in from;
265      owl_message *m;
266
267      ZReceiveNotice(&notice, &from);
268
269      /* is this an ack from a zephyr we sent? */
270      if (owl_zephyr_notice_is_ack(&notice)) {
271        owl_zephyr_handle_ack(&notice);
272        continue;
273      }
274     
275      /* if it's a ping and we're not viewing pings then skip it */
276      if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
277        continue;
278      }
279
280      /* create the new message */
281      m=owl_malloc(sizeof(owl_message));
282      owl_message_create_from_znotice(m, &notice);
283     
284      /* if it's on the puntlist then, nuke it and continue */
285      if (owl_global_message_is_puntable(&g, m)) {
286        owl_message_free(m);
287        continue;
288      }
289
290      /* otherwise add it to the global list */
291      owl_messagelist_append_element(owl_global_get_msglist(&g), m);
292      newzephyrs=1;
293
294      /* let the config know the new message has been received */
295      owl_config_getmsg(m, 0);
296
297      /* add it to any necessary views; right now there's only the current view */
298      owl_view_consider_message(owl_global_get_current_view(&g), m);
299
300      /* do we need to autoreply? */
301      if (owl_global_is_zaway(&g)) {
302        owl_zephyr_zaway(m);
303      }
304
305      /* ring the bell if it's a personal */
306      if (owl_global_is_personalbell(&g) && owl_message_is_personal(m)) {
307        owl_function_beep();
308      }
309
310      /* check for burning ears message */
311      /* this is an unsupported feature */
312      if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) {
313        char *buff;
314        buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m));
315        /* owl_function_makemsg(buff); */
316        owl_function_adminmsg(buff, "");
317        owl_free(buff);
318        owl_function_beep();
319      }
320
321      /* log the zephyr if we need to */
322      if (owl_global_is_logging(&g) || owl_global_is_classlogging(&g)) {
323        owl_log_incoming(m);
324      }
325    }
326
327    /* follow the last message if we're supposed to */
328    if (newzephyrs && followlast) {
329      owl_function_lastmsg_noredisplay();
330    }
331
332    /* check if newmsgproc is active, if not but the option is on,
333       make it active */
334    if (newzephyrs) {
335      owl_function_do_newmsgproc();
336    }
337   
338    /* redisplay if necessary */
339    if (newzephyrs) {
340      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
341      sepbar(NULL);
342      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
343        owl_popwin_refresh(owl_global_get_popwin(&g));
344        /* TODO: this is a broken kludge */
345        if (owl_global_get_viewwin(&g)) {
346          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
347        }
348      }
349      owl_global_set_needrefresh(&g);
350    }
351
352    /* if a popwin just came up, refresh it */
353    pw=owl_global_get_popwin(&g);
354    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
355      owl_popwin_refresh(pw);
356      owl_popwin_no_needs_first_refresh(pw);
357      owl_global_set_needrefresh(&g);
358      /* TODO: this is a broken kludge */
359      if (owl_global_get_viewwin(&g)) {
360        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
361      }
362    }
363
364    /* update the terminal if we need to */
365    if (owl_global_is_needrefresh(&g)) {
366      /* leave the cursor in the appropriate window */
367      if (owl_global_is_typwin_active(&g)) {
368        owl_function_set_cursor(typwin);
369      } else {
370        owl_function_set_cursor(sepwin);
371      }
372      doupdate();
373      owl_global_set_noneedrefresh(&g);
374    }
375
376    /* handle all keypresses */
377    j=wgetch(typwin);
378    if (j==ERR) {
379      usleep(10);
380      continue;
381    }
382    /* find and activate the current keymap.
383     * TODO: this should really get fixed by activating
384     * keymaps as we switch between windows...
385     */
386    if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
387      owl_context_set_popless(owl_global_get_context(&g), 
388                              owl_global_get_viewwin(&g));
389      owl_function_activate_keymap("popless");
390    } else if (owl_global_is_typwin_active(&g) 
391               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
392      owl_context_set_editline(owl_global_get_context(&g), tw);
393      owl_function_activate_keymap("editline");
394    } else if (owl_global_is_typwin_active(&g) 
395               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
396      owl_context_set_editmulti(owl_global_get_context(&g), tw);
397      owl_function_activate_keymap("editmulti");
398    } else {
399      owl_context_set_recv(owl_global_get_context(&g));
400      owl_function_activate_keymap("recv");
401    }
402    /* now actually handle the keypress */
403    ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
404    if (ret!=0 && ret!=1) {
405      owl_function_makemsg("Unable to handle keypress");
406    }
407  }
408
409}
410
411void sig_handler(int sig) {
412  if (sig==SIGWINCH) {
413    /* we can't inturrupt a malloc here, so it just sets a flag */
414    owl_function_resize();
415  }
416}
417
418void usage() {
419  fprintf(stderr, "Usage: owl [-n] [-d] [-v] [-c <configfile>] [-t <ttyname>]\n");
420}
Note: See TracBrowser for help on using the repository browser.