source: owl.c @ 700c712

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 700c712 was 700c712, checked in by James M. Kretchmar <kretch@mit.edu>, 18 years ago
Started adding code for newmsgproc. It doesn't fully work yet! Don't use it!
  • 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, argc;
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           
363            parsed=owl_parseline(owl_global_get_newmsgproc(&g), &argc);
364            parsed=realloc(parsed, strlen(owl_global_get_newmsgproc(&g)+300));
365            parsed[argc]='\0';
366
367            owl_function_debugmsg("About to exec: -%s- with %i arguments", parsed[0], argc);
368
369            execvp(*parsed, parsed);
370           
371
372            /* was there an error exec'ing? */
373            owl_function_debugmsg("Error execing: %s", strerror(errno));
374            _exit(127);
375          }
376        }
377      }
378    }
379   
380    /* redisplay if necessary */
381    if (newzephyrs) {
382      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
383      sepbar(NULL);
384      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
385        owl_popwin_refresh(owl_global_get_popwin(&g));
386        /* TODO: this is a broken kludge */
387        if (owl_global_get_viewwin(&g)) {
388          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
389        }
390      }
391      owl_global_set_needrefresh(&g);
392    }
393
394    /* if a popwin just came up, refresh it */
395    pw=owl_global_get_popwin(&g);
396    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
397      owl_popwin_refresh(pw);
398      owl_popwin_no_needs_first_refresh(pw);
399      owl_global_set_needrefresh(&g);
400      /* TODO: this is a broken kludge */
401      if (owl_global_get_viewwin(&g)) {
402        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
403      }
404    }
405
406    /* update the terminal if we need to */
407    if (owl_global_is_needrefresh(&g)) {
408      /* leave the cursor in the appropriate window */
409      if (owl_global_is_typwin_active(&g)) {
410        owl_function_set_cursor(typwin);
411      } else {
412        owl_function_set_cursor(sepwin);
413      }
414      doupdate();
415      owl_global_set_noneedrefresh(&g);
416    }
417
418    /* handle all keypresses */
419    j=wgetch(typwin);
420    if (j==ERR) {
421      usleep(10);
422      continue;
423    }
424    /* find and activate the current keymap.
425     * TODO: this should really get fixed by activating
426     * keymaps as we switch between windows...
427     */
428    if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
429      owl_context_set_popless(owl_global_get_context(&g), 
430                              owl_global_get_viewwin(&g));
431      owl_function_activate_keymap("popless");
432    } else if (owl_global_is_typwin_active(&g) 
433               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
434      owl_context_set_editline(owl_global_get_context(&g), tw);
435      owl_function_activate_keymap("editline");
436    } else if (owl_global_is_typwin_active(&g) 
437               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
438      owl_context_set_editmulti(owl_global_get_context(&g), tw);
439      owl_function_activate_keymap("editmulti");
440    } else {
441      owl_context_set_recv(owl_global_get_context(&g));
442      owl_function_activate_keymap("recv");
443    }
444    /* now actually handle the keypress */
445    ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
446    if (ret!=0 && ret!=1) {
447      owl_function_makemsg("Unable to handle keypress");
448    }
449  }
450
451}
452
453void sig_handler(int sig) {
454  if (sig==SIGWINCH) {
455    /* we can't inturrupt a malloc here, so it just sets a flag */
456    owl_function_resize();
457  }
458}
459
460void usage() {
461  fprintf(stderr, "Usage: owl [-n] [-d] [-v] [-c <configfile>] [-t <ttyname>]\n");
462}
Note: See TracBrowser for help on using the repository browser.