source: owl.c @ ddb8252

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