source: owl.c @ 2a2bb60

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 2a2bb60 was 2a2bb60, checked in by Erik Nygren <nygren@mit.edu>, 18 years ago
2.0.9-pre-3 Added testing feature for capturing stderr messages from commands and displaying them in the errors buffer. This feature is disabled by default. Enable in owl.h with: OWL_STDERR_REDIR 1
  • Property mode set to 100644
File size: 18.8 KB
Line 
1/*
2 * Copyright 2001 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that the above copyright notice and this
7 * permission notice appear in all copies and in supporting
8 * documentation.  No representation is made about the suitability of
9 * this software for any purpose.  It is provided "as is" without
10 * express or implied warranty.
11 */
12
13#include <stdio.h>
14#include <unistd.h>
15#include <stdlib.h>
16#include <string.h>
17#include <signal.h>
18#include <time.h>
19#include <sys/param.h>
20#include <sys/types.h>
21#include <sys/stat.h>
22#include "owl.h"
23
24#if OWL_STDERR_REDIR
25#include <sys/ioctl.h>
26int stderr_replace(void);
27void stderr_redirect(int rfd);
28#endif
29
30static const char fileIdent[] = "$Id$";
31
32owl_global g;
33
34int main(int argc, char **argv, char **env) {
35  WINDOW *recwin, *sepwin, *typwin, *msgwin;
36  owl_editwin *tw;
37  owl_popwin *pw;
38  int j, ret, initialsubs, debug, argcsave, followlast;
39  int newmsgs, zpendcount, nexttimediff;
40  struct sigaction sigact;
41  char *configfile, *tty, *perlout, *perlerr, **argvsave, buff[LINE], startupmsg[LINE];
42  owl_filter *f;
43  owl_style *s;
44  time_t nexttime, now;
45  struct tm *today;
46  char *dir;
47#ifdef HAVE_LIBZEPHYR
48  ZNotice_t notice;
49#endif
50#if OWL_STDERR_REDIR
51  int newstderr;
52#endif
53
54  argcsave=argc;
55  argvsave=argv;
56  configfile=NULL;
57  tty=NULL;
58  debug=0;
59  initialsubs=1;
60  if (argc>0) {
61    argv++;
62    argc--;
63  }
64  while (argc>0) {
65    if (!strcmp(argv[0], "-n")) {
66      initialsubs=0;
67      argv++;
68      argc--;
69    } else if (!strcmp(argv[0], "-c")) {
70      if (argc<2) {
71        fprintf(stderr, "Too few arguments to -c\n");
72        usage();
73        exit(1);
74      }
75      configfile=argv[1];
76      argv+=2;
77      argc-=2;
78    } else if (!strcmp(argv[0], "-t")) {
79      if (argc<2) {
80        fprintf(stderr, "Too few arguments to -t\n");
81        usage();
82        exit(1);
83      }
84      tty=argv[1];
85      argv+=2;
86      argc-=2;
87    } else if (!strcmp(argv[0], "-d")) {
88      debug=1;
89      argv++;
90      argc--;
91    } else if (!strcmp(argv[0], "-v")) {
92      printf("This is owl version %s\n", OWL_VERSION_STRING);
93      exit(0);
94    } else {
95      fprintf(stderr, "Uknown argument\n");
96      usage();       
97      exit(1);
98    }
99  }
100
101#ifdef HAVE_LIBZEPHYR
102  /* zephyr init */
103  ret=owl_zephyr_initialize();
104  if (ret) {
105    exit(1);
106  }
107#endif
108 
109  /* signal handler */
110  sigact.sa_handler=sig_handler;
111  sigemptyset(&sigact.sa_mask);
112  sigact.sa_flags=0;
113  sigaction(SIGWINCH, &sigact, NULL);
114  sigaction(SIGALRM, &sigact, NULL);
115
116  /* screen init */
117  sprintf(buff, "TERMINFO=%s", TERMINFO);
118  putenv(buff);
119  initscr();
120  start_color();
121#ifdef HAVE_USE_DEFAULT_COLORS
122  use_default_colors();
123#endif
124  raw();
125  noecho();
126
127  /* define simple color pairs */
128  if (has_colors() && COLOR_PAIRS>=8) {
129    init_pair(OWL_COLOR_BLACK,   COLOR_BLACK,   -1);
130    init_pair(OWL_COLOR_RED,     COLOR_RED,     -1);
131    init_pair(OWL_COLOR_GREEN,   COLOR_GREEN,   -1);
132    init_pair(OWL_COLOR_YELLOW,  COLOR_YELLOW,  -1);
133    init_pair(OWL_COLOR_BLUE,    COLOR_BLUE,    -1);
134    init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, -1);
135    init_pair(OWL_COLOR_CYAN,    COLOR_CYAN,    -1);
136    init_pair(OWL_COLOR_WHITE,   COLOR_WHITE,   -1);
137  }
138
139  /* owl global init */
140  owl_global_init(&g);
141  if (debug) owl_global_set_debug_on(&g);
142  owl_global_set_startupargs(&g, argcsave, argvsave);
143#ifdef HAVE_LIBZEPHYR
144  owl_global_set_havezephyr(&g);
145#endif
146  owl_global_set_haveaim(&g);
147
148#if OWL_STDERR_REDIR
149  /* Do this only after we've started curses up... */ 
150  newstderr = stderr_replace();
151#endif   
152
153 
154  /* create the owl directory, in case it does not exist */
155  dir=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_CONFIG_DIR);
156  mkdir(dir, S_IRWXU);
157  owl_free(dir);
158
159  /* set the tty, either from the command line, or by figuring it out */
160  if (tty) {
161    owl_global_set_tty(&g, tty);
162  } else {
163    owl_global_set_tty(&g, owl_util_get_default_tty());
164  }
165
166  /* setup the built-in styles */
167  s=owl_malloc(sizeof(owl_style));
168  owl_style_create_internal(s, "default", &owl_stylefunc_default,
169                            "Default message formatting");
170  owl_global_add_style(&g, s);
171
172  s=owl_malloc(sizeof(owl_style));
173  owl_style_create_internal(s, "basic", &owl_stylefunc_basic,
174                            "Basic message formatting.");
175  owl_global_add_style(&g, s);
176#if 0
177  s=owl_malloc(sizeof(owl_style));
178  owl_style_create_internal(s, "vt", &owl_stylefunc_vt,
179                            "VT message formatting.");
180  owl_global_add_style(&g, s);
181#endif
182  s=owl_malloc(sizeof(owl_style));
183  owl_style_create_internal(s, "oneline", &owl_stylefunc_oneline,
184                            "Formats for one-line-per-message");
185  owl_global_add_style(&g, s);
186
187  /* setup the default filters */
188  /* the personal filter will need to change again when AIM chat's are
189   *  included.  Also, there should be an %aimme% */
190  f=owl_malloc(sizeof(owl_filter));
191  owl_filter_init_fromstring(f, "personal", "( type ^zephyr$ "
192                             "and class ^message$ and instance ^personal$ "
193                             "and ( recipient ^%me%$ or sender ^%me%$ ) ) "
194                             "or ( type ^aim$ and login ^none$ )");
195  owl_list_append_element(owl_global_get_filterlist(&g), f);
196
197  f=owl_malloc(sizeof(owl_filter));
198  owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )");
199  owl_list_append_element(owl_global_get_filterlist(&g), f);
200
201  f=owl_malloc(sizeof(owl_filter));
202  owl_filter_init_fromstring(f, "ping", "opcode ^ping$");
203  owl_list_append_element(owl_global_get_filterlist(&g), f);
204
205  f=owl_malloc(sizeof(owl_filter));
206  owl_filter_init_fromstring(f, "auto", "opcode ^auto$");
207  owl_list_append_element(owl_global_get_filterlist(&g), f);
208
209  f=owl_malloc(sizeof(owl_filter));
210  owl_filter_init_fromstring(f, "login", "not login ^none$");
211  owl_list_append_element(owl_global_get_filterlist(&g), f);
212
213  f=owl_malloc(sizeof(owl_filter));
214  owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$");
215  owl_list_append_element(owl_global_get_filterlist(&g), f);
216
217  f=owl_malloc(sizeof(owl_filter));
218  owl_filter_init_fromstring(f, "out", "direction ^out$");
219  owl_list_append_element(owl_global_get_filterlist(&g), f);
220
221  f=owl_malloc(sizeof(owl_filter));
222  owl_filter_init_fromstring(f, "aim", "type ^aim$");
223  owl_list_append_element(owl_global_get_filterlist(&g), f);
224
225  f=owl_malloc(sizeof(owl_filter));
226  owl_filter_init_fromstring(f, "zephyr", "type ^zephyr$");
227  owl_list_append_element(owl_global_get_filterlist(&g), f);
228
229  f=owl_malloc(sizeof(owl_filter));
230  owl_filter_init_fromstring(f, "none", "false");
231  owl_list_append_element(owl_global_get_filterlist(&g), f);
232
233  f=owl_malloc(sizeof(owl_filter));
234  owl_filter_init_fromstring(f, "all", "true");
235  owl_list_append_element(owl_global_get_filterlist(&g), f);
236
237  /* set the current view */
238  owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default"));
239
240  /* AIM init */
241  owl_aim_init();
242
243  /* process the startup file */
244  owl_function_source(NULL);
245
246  /* read the config file */
247  owl_context_set_readconfig(owl_global_get_context(&g));
248  perlerr=owl_perlconfig_readconfig(configfile);
249  if (perlerr) {
250    endwin();
251    fprintf(stderr, "\nError parsing configfile: %s\n", perlerr);
252    exit(1);
253  }
254
255  /* if the config defines a formatting function, add 'perl' as a style */
256  if (owl_global_is_config_format(&g)) {
257    owl_function_debugmsg("Found perl formatting");
258    s=owl_malloc(sizeof(owl_style));
259    owl_style_create_perl(s, "perl", "owl::_format_msg_legacy_wrap",
260                          "User-defined perl style that calls owl::format_msg"
261                          "with legacy global variable support");
262    owl_global_add_style(&g, s);
263    owl_global_set_default_style(&g, "perl");
264  }
265
266  /* execute the startup function in the configfile */
267  perlout = owl_perlconfig_execute("owl::startup();");
268  if (perlout) owl_free(perlout);
269  owl_function_debugmsg("-- Owl Startup --");
270 
271  /* hold on to the window names for convenience */
272  msgwin=owl_global_get_curs_msgwin(&g);
273  recwin=owl_global_get_curs_recwin(&g);
274  sepwin=owl_global_get_curs_sepwin(&g);
275  typwin=owl_global_get_curs_typwin(&g);
276  tw=owl_global_get_typwin(&g);
277
278  wrefresh(sepwin);
279
280  /* load zephyr subs */
281  if (initialsubs) {
282    /* load normal subscriptions */
283    ret=owl_zephyr_loadsubs(NULL);
284    if (ret!=-1) {
285      owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
286    }
287
288    /* load login subscriptions */
289    if (owl_global_is_loginsubs(&g)) {
290      owl_function_loadloginsubs(NULL);
291    }
292  }
293
294  /* set the startup and default style, based on userclue and presence of a
295   * formatting function */
296  if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) {
297    /* the style was set by the user: leave it alone */
298  } else if (owl_global_is_config_format(&g)) {
299    owl_global_set_default_style(&g, "perl");
300  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
301    owl_global_set_default_style(&g, "default");
302  } else {
303    owl_global_set_default_style(&g, "basic");
304  }
305
306  /* zlog in if we need to */
307  if (owl_global_is_startuplogin(&g)) {
308    owl_zephyr_zlog_in();
309  }
310
311  owl_view_set_style(owl_global_get_current_view(&g), 
312                     owl_global_get_style_by_name(&g, owl_global_get_default_style(&g)));   
313
314  /* welcome message */
315  strcpy(startupmsg, "-------------------------------------------------------------------------\n");
316  sprintf(buff,      "Welcome to Owl version %s.  Press 'h' for on-line help. \n", OWL_VERSION_STRING);
317  strcat(startupmsg, buff);
318  strcat(startupmsg, "                                                                         \n");
319  strcat(startupmsg, "If you would like to receive release announcements about owl you can join \n");
320  strcat(startupmsg, "the owl-users@mit.edu mailing list.  MIT users can add themselves,       \n");
321  strcat(startupmsg, "otherwise send a request to owner-owl-users@mit.edu.               ^ ^   \n");
322  strcat(startupmsg, "                                                                   OvO   \n");
323  strcat(startupmsg, "Please report any bugs or suggestions to bug-owl@mit.edu          (   )  \n");
324  strcat(startupmsg, "-------------------------------------------------------------------m-m---\n");
325  owl_function_adminmsg("", startupmsg);
326  sepbar(NULL);
327 
328  owl_context_set_interactive(owl_global_get_context(&g));
329
330  nexttimediff=10;
331  nexttime=time(NULL);
332
333  /* main loop */
334  while (1) {
335
336    /* if a resize has been scheduled, deal with it */
337    owl_global_resize(&g, 0, 0);
338
339    /* these are here in case a resize changes the windows */
340    msgwin=owl_global_get_curs_msgwin(&g);
341    recwin=owl_global_get_curs_recwin(&g);
342    sepwin=owl_global_get_curs_sepwin(&g);
343    typwin=owl_global_get_curs_typwin(&g);
344
345    followlast=owl_global_should_followlast(&g);
346   
347    /* Do AIM stuff */
348    if (owl_global_is_doaimevents(&g)) {
349      owl_aim_process_events();
350
351      if (owl_global_is_aimloggedin(&g)) {
352        if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
353          /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
354          owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
355        }
356      }
357    }
358
359    /* little hack */
360    now=time(NULL);
361    today=localtime(&now);
362    if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) {
363      if (time(NULL)>nexttime) {
364        if (nexttimediff==1) {
365          nexttimediff=10;
366        } else {
367          nexttimediff=1;
368        }
369        nexttime+=nexttimediff;
370        owl_hack_animate();
371      }
372    }
373
374    /* Grab incoming messages. */
375    newmsgs=0;
376    zpendcount=0;
377    while(owl_zephyr_zpending() || owl_global_messagequeue_pending(&g)) {
378#ifdef HAVE_LIBZEPHYR
379      struct sockaddr_in from;
380#endif
381      owl_message *m=NULL;
382      owl_filter *f;
383
384      /* grab the new message, stick it in 'm' */
385      if (owl_zephyr_zpending()) {
386#ifdef HAVE_LIBZEPHYR
387        /* grab a zephyr notice, but if we've done 20 without stopping,
388           take a break to process keystrokes etc. */
389        if (zpendcount>20) break;
390        ZReceiveNotice(&notice, &from);
391        zpendcount++;
392       
393        /* is this an ack from a zephyr we sent? */
394        if (owl_zephyr_notice_is_ack(&notice)) {
395          owl_zephyr_handle_ack(&notice);
396          continue;
397        }
398       
399        /* if it's a ping and we're not viewing pings then skip it */
400        if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
401          continue;
402        }
403
404        /* create the new message */
405        m=owl_malloc(sizeof(owl_message));
406        owl_message_create_from_znotice(m, &notice);
407#endif
408      } else if (owl_global_messagequeue_pending(&g)) {
409        m=owl_global_messageuque_popmsg(&g);
410      }
411     
412      /* if this message it on the puntlist, nuke it and continue */
413      if (owl_global_message_is_puntable(&g, m)) {
414        owl_message_free(m);
415        continue;
416      }
417
418      /* otherwise add it to the global list */
419      owl_messagelist_append_element(owl_global_get_msglist(&g), m);
420      newmsgs=1;
421
422      /* let the config know the new message has been received */
423      owl_perlconfig_getmsg(m, 0, NULL);
424
425      /* add it to any necessary views; right now there's only the current view */
426      owl_view_consider_message(owl_global_get_current_view(&g), m);
427
428      /* do we need to autoreply? */
429      if (owl_message_is_type_zephyr(m) && owl_global_is_zaway(&g)) {
430        owl_zephyr_zaway(m);
431      }
432
433      /* ring the bell if it's a personal */
434      if (owl_global_is_personalbell(&g) &&
435          !owl_message_is_loginout(m) &&
436          !owl_message_is_mail(m) &&
437          owl_message_is_private(m)) {
438        owl_function_beep();
439      }
440
441      /* if it matches the alert filter, do the alert action */
442      f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g));
443      if (f && owl_filter_message_match(f, m)) {
444        owl_function_command(owl_global_get_alert_action(&g));
445      }
446
447      /* check for burning ears message */
448      /* this is an unsupported feature */
449      if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) {
450        char *buff;
451        buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m));
452        owl_function_adminmsg(buff, "");
453        owl_free(buff);
454        owl_function_beep();
455      }
456
457      /* log the message if we need to */
458      if (owl_global_is_logging(&g) || owl_global_is_classlogging(&g)) {
459        owl_log_incoming(m);
460      }
461    }
462
463    /* follow the last message if we're supposed to */
464    if (newmsgs && followlast) {
465      owl_function_lastmsg_noredisplay();
466    }
467
468    /* do the newmsgproc thing */
469    if (newmsgs) {
470      owl_function_do_newmsgproc();
471    }
472   
473    /* redisplay if necessary */
474    /* this should be optimized to not run if the new messages won't be displayed */
475    if (newmsgs) {
476      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
477      sepbar(NULL);
478      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
479        owl_popwin_refresh(owl_global_get_popwin(&g));
480        /* TODO: this is a broken kludge */
481        if (owl_global_get_viewwin(&g)) {
482          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
483        }
484      }
485      owl_global_set_needrefresh(&g);
486    }
487
488    /* if a popwin just came up, refresh it */
489    pw=owl_global_get_popwin(&g);
490    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
491      owl_popwin_refresh(pw);
492      owl_popwin_no_needs_first_refresh(pw);
493      owl_global_set_needrefresh(&g);
494      /* TODO: this is a broken kludge */
495      if (owl_global_get_viewwin(&g)) {
496        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
497      }
498    }
499
500    /* update the terminal if we need to */
501    if (owl_global_is_needrefresh(&g)) {
502      /* leave the cursor in the appropriate window */
503      if (owl_global_is_typwin_active(&g)) {
504        owl_function_set_cursor(typwin);
505      } else {
506        owl_function_set_cursor(sepwin);
507      }
508      doupdate();
509      owl_global_set_noneedrefresh(&g);
510    }
511
512    /* Handle all keypresses.  If no key has been pressed, sleep for a
513     * little bit, but otherwise do not.  This lets input be grabbed
514     * as quickly as possbile */
515    j=wgetch(typwin);
516    if (j==ERR) {
517      usleep(10);
518      continue;
519    }
520    /* find and activate the current keymap.
521     * TODO: this should really get fixed by activating
522     * keymaps as we switch between windows...
523     */
524    if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
525      owl_context_set_popless(owl_global_get_context(&g), 
526                              owl_global_get_viewwin(&g));
527      owl_function_activate_keymap("popless");
528    } else if (owl_global_is_typwin_active(&g) 
529               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
530      /*
531      owl_context_set_editline(owl_global_get_context(&g), tw);
532      owl_function_activate_keymap("editline");
533      */
534    } else if (owl_global_is_typwin_active(&g) 
535               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
536      owl_context_set_editmulti(owl_global_get_context(&g), tw);
537      owl_function_activate_keymap("editmulti");
538    } else {
539      owl_context_set_recv(owl_global_get_context(&g));
540      owl_function_activate_keymap("recv");
541    }
542    /* now actually handle the keypress */
543    ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
544    if (ret!=0 && ret!=1) {
545      owl_function_makemsg("Unable to handle keypress");
546    }
547
548#if OWL_STDERR_REDIR
549    stderr_redirect(newstderr);
550#endif   
551
552  }
553}
554
555void sig_handler(int sig) {
556  if (sig==SIGWINCH) {
557    /* we can't inturrupt a malloc here, so it just sets a flag
558     * schedulding a resize for later
559     */
560    owl_function_resize();
561  }
562}
563
564void usage() {
565  fprintf(stderr, "Owl version %s\n", OWL_VERSION_STRING);
566  fprintf(stderr, "Usage: owl [-n] [-d] [-v] [-h] [-c <configfile>] [-t <ttyname>]\n");
567  fprintf(stderr, "  -n      don't load zephyr subscriptions\n");
568  fprintf(stderr, "  -d      enable debugging\n");
569  fprintf(stderr, "  -v      print the Owl version number and exit\n");
570  fprintf(stderr, "  -h      print this help message\n");
571  fprintf(stderr, "  -c      specify an alternate config file\n");
572  fprintf(stderr, "  -t      set the tty name\n");
573}
574
575
576
577#if OWL_STDERR_REDIR
578
579/* Replaces stderr with a pipe so that we can read from it.
580 * Returns the fd of the pipe from which stderr can be read. */
581int stderr_replace(void) {
582  int pipefds[2];
583  if (0 != pipe(pipefds)) {
584    perror("pipe");
585    owl_function_debugmsg("stderr_replace: pipe FAILED\n");
586    return -1;
587  }
588    owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]);
589  if (-1 == dup2(pipefds[1], 2 /*stderr*/)) {
590    owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno));
591    perror("dup2");
592    return -1;
593  }
594  return pipefds[0];
595}
596
597/* Sends stderr (read from rfd) messages to a file */
598void stderr_redirect(int rfd) {
599  int navail, bread;
600  char *buf;
601  /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/
602  if (rfd<0) return;
603  if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) {
604    return;
605  }
606  /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/
607  if (navail<=0) return;
608  if (navail>256) { navail = 256; }
609  buf = owl_malloc(navail+1);
610  bread = read(rfd, buf, navail);
611  if (buf[navail-1] != '\0') {
612    buf[navail] = '\0';
613  }
614  owl_function_error("Err: %s", buf);
615  owl_free(buf);
616}
617
618#endif /* OWL_STDERR_REDIR */
Note: See TracBrowser for help on using the repository browser.