source: owl.c @ b45293f

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