source: owl.c @ 55a4578

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