source: owl.c @ fe1f605

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since fe1f605 was fe1f605, checked in by James M. Kretchmar <kretch@mit.edu>, 17 years ago
Catch SIGHUP and SIGTERM and do a proper logout
  • Property mode set to 100644
File size: 24.8 KB
RevLine 
[5ca5f8e]1/*  Copyright (c) 2004 James Kretchmar. All rights reserved.
[7d4fbcd]2 *
[5ca5f8e]3 *  Redistribution and use in source and binary forms, with or without
4 *  modification, are permitted provided that the following conditions are
5 *  met:
6 * 
7 *    * Redistributions of source code must retain the above copyright
8 *      notice, this list of conditions and the following disclaimer.
9 * 
10 *    * Redistributions in binary form must reproduce the above copyright
11 *      notice, this list of conditions and the following disclaimer in
12 *      the documentation and/or other materials provided with the
13 *      distribution.
14 * 
15 *    * Redistributions in any form must be accompanied by information on
16 *      how to obtain complete source code for the Owl software and any
17 *      accompanying software that uses the Owl software. The source code
18 *      must either be included in the distribution or be available for no
19 *      more than the cost of distribution plus a nominal fee, and must be
20 *      freely redistributable under reasonable conditions. For an
21 *      executable file, complete source code means the source code for
22 *      all modules it contains. It does not include source code for
23 *      modules or files that typically accompany the major components of
24 *      the operating system on which the executable file runs.
25 * 
26 *
27 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 *  WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
30 *  NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
31 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
34 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
36 *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
37 *  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
[7d4fbcd]38 */
39
40#include <stdio.h>
41#include <unistd.h>
42#include <stdlib.h>
43#include <string.h>
44#include <signal.h>
[5145235]45#include <time.h>
[7d4fbcd]46#include <sys/param.h>
[700c712]47#include <sys/types.h>
[38cf544c]48#include <sys/stat.h>
[7d4fbcd]49#include "owl.h"
50
[2a2bb60]51#if OWL_STDERR_REDIR
[e8b95f8]52#ifdef HAVE_SYS_IOCTL_H
[2a2bb60]53#include <sys/ioctl.h>
[e8b95f8]54#endif
55#ifdef HAVE_SYS_FILIO_H
56#include <sys/filio.h>
57#endif
[2a2bb60]58int stderr_replace(void);
59#endif
60
[1aee7d9]61static const char fileIdent[] = "$Id$";
62
[b2b0773]63owl_global g;
64
[de22c3d]65int main(int argc, char **argv, char **env)
66{
[7d4fbcd]67  WINDOW *recwin, *sepwin, *typwin, *msgwin;
68  owl_editwin *tw;
69  owl_popwin *pw;
[ced25d1]70  int j, ret, initialsubs, debug, argcsave, followlast;
[d09e5a1]71  int newmsgs, zpendcount, nexttimediff;
[7d4fbcd]72  struct sigaction sigact;
[f1e629d]73  char *configfile, *tty, *perlout, *perlerr, **argvsave, buff[LINE], startupmsg[LINE];
[7d4fbcd]74  owl_filter *f;
[bd3f232]75  owl_style *s;
[a15a84f]76  time_t nexttime, now;
77  struct tm *today;
[38cf544c]78  char *dir;
[09489b89]79#ifdef HAVE_LIBZEPHYR
[be0a79f]80  ZNotice_t notice;
[09489b89]81#endif
[2a2bb60]82#if OWL_STDERR_REDIR
83  int newstderr;
84#endif
[7d4fbcd]85
86  argcsave=argc;
87  argvsave=argv;
88  configfile=NULL;
89  tty=NULL;
90  debug=0;
[bd3f232]91  initialsubs=1;
[7d4fbcd]92  if (argc>0) {
93    argv++;
94    argc--;
95  }
96  while (argc>0) {
97    if (!strcmp(argv[0], "-n")) {
98      initialsubs=0;
99      argv++;
100      argc--;
101    } else if (!strcmp(argv[0], "-c")) {
102      if (argc<2) {
103        fprintf(stderr, "Too few arguments to -c\n");
104        usage();
105        exit(1);
106      }
107      configfile=argv[1];
108      argv+=2;
109      argc-=2;
110    } else if (!strcmp(argv[0], "-t")) {
111      if (argc<2) {
112        fprintf(stderr, "Too few arguments to -t\n");
113        usage();
114        exit(1);
115      }
116      tty=argv[1];
117      argv+=2;
118      argc-=2;
119    } else if (!strcmp(argv[0], "-d")) {
120      debug=1;
121      argv++;
122      argc--;
[a68f05d]123    } else if (!strcmp(argv[0], "-D")) {
124      debug=1;
125      unlink(OWL_DEBUG_FILE);
126      argv++;
127      argc--;
[7d4fbcd]128    } else if (!strcmp(argv[0], "-v")) {
129      printf("This is owl version %s\n", OWL_VERSION_STRING);
130      exit(0);
131    } else {
132      fprintf(stderr, "Uknown argument\n");
133      usage();       
134      exit(1);
135    }
136  }
137
[be0a79f]138#ifdef HAVE_LIBZEPHYR
[7d4fbcd]139  /* zephyr init */
[be0a79f]140  ret=owl_zephyr_initialize();
141  if (ret) {
[7d4fbcd]142    exit(1);
143  }
[be0a79f]144#endif
145 
[7d4fbcd]146  /* signal handler */
[c9e72d1]147  /*sigact.sa_handler=sig_handler;*/
148  sigact.sa_sigaction=sig_handler;
[7d4fbcd]149  sigemptyset(&sigact.sa_mask);
[c9e72d1]150  sigact.sa_flags=SA_SIGINFO;
[7d4fbcd]151  sigaction(SIGWINCH, &sigact, NULL);
152  sigaction(SIGALRM, &sigact, NULL);
[c9e72d1]153  sigaction(SIGPIPE, &sigact, NULL);
[fe1f605]154  sigaction(SIGTERM, &sigact, NULL);
155  sigaction(SIGHUP, &sigact, NULL);
[7d4fbcd]156
157  /* screen init */
[b20de4f]158  if (!getenv("TERMINFO")) {
159    sprintf(buff, "TERMINFO=%s", TERMINFO);
160    putenv(buff);
161    owl_function_debugmsg("startup: setting TERMINFO to %s", TERMINFO);
162  } else {
163    owl_function_debugmsg("startup: leaving TERMINFO as %s from envrionment", getenv("TERMINFO"));
164  }
[7d4fbcd]165  initscr();
166  start_color();
[ac70242]167#ifdef HAVE_USE_DEFAULT_COLORS
[7d4fbcd]168  use_default_colors();
[ac70242]169#endif
[7d4fbcd]170  raw();
171  noecho();
172
173  /* define simple color pairs */
174  if (has_colors() && COLOR_PAIRS>=8) {
175    init_pair(OWL_COLOR_BLACK,   COLOR_BLACK,   -1);
176    init_pair(OWL_COLOR_RED,     COLOR_RED,     -1);
177    init_pair(OWL_COLOR_GREEN,   COLOR_GREEN,   -1);
178    init_pair(OWL_COLOR_YELLOW,  COLOR_YELLOW,  -1);
179    init_pair(OWL_COLOR_BLUE,    COLOR_BLUE,    -1);
180    init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, -1);
181    init_pair(OWL_COLOR_CYAN,    COLOR_CYAN,    -1);
182    init_pair(OWL_COLOR_WHITE,   COLOR_WHITE,   -1);
183  }
[38cf544c]184
[bd3f232]185  /* owl global init */
[7d4fbcd]186  owl_global_init(&g);
187  if (debug) owl_global_set_debug_on(&g);
[b20de4f]188  owl_function_debugmsg("startup: first available debugging message");
[7d4fbcd]189  owl_global_set_startupargs(&g, argcsave, argvsave);
[09489b89]190#ifdef HAVE_LIBZEPHYR
191  owl_global_set_havezephyr(&g);
192#endif
193  owl_global_set_haveaim(&g);
[2a2bb60]194
195#if OWL_STDERR_REDIR
[a0a5179]196  /* Do this only after we've started curses up... */
197  owl_function_debugmsg("startup: doing stderr redirection");
[2a2bb60]198  newstderr = stderr_replace();
[afbf668]199  owl_muxevents_add(owl_global_get_muxevents(&g), newstderr, OWL_MUX_READ,
200                    stderr_redirect_handler, NULL);
[2a2bb60]201#endif   
202
[38cf544c]203  /* create the owl directory, in case it does not exist */
[a0a5179]204  owl_function_debugmsg("startup: creating owl directory, if not present");
[38cf544c]205  dir=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_CONFIG_DIR);
206  mkdir(dir, S_IRWXU);
207  owl_free(dir);
[5145235]208
[ced25d1]209  /* set the tty, either from the command line, or by figuring it out */
[a0a5179]210  owl_function_debugmsg("startup: setting tty name");
[61e79a9]211  if (tty) {
212    owl_global_set_tty(&g, tty);
213  } else {
214    owl_global_set_tty(&g, owl_util_get_default_tty());
215  }
[7d4fbcd]216
[bd3f232]217  /* setup the built-in styles */
[a0a5179]218  owl_function_debugmsg("startup: creating built-in styles");
[bd3f232]219  s=owl_malloc(sizeof(owl_style));
[f1e629d]220  owl_style_create_internal(s, "default", &owl_stylefunc_default,
221                            "Default message formatting");
[bd3f232]222  owl_global_add_style(&g, s);
223
224  s=owl_malloc(sizeof(owl_style));
[f1e629d]225  owl_style_create_internal(s, "basic", &owl_stylefunc_basic,
226                            "Basic message formatting.");
[bd3f232]227  owl_global_add_style(&g, s);
[ec6ff52]228#if 0
229  s=owl_malloc(sizeof(owl_style));
230  owl_style_create_internal(s, "vt", &owl_stylefunc_vt,
231                            "VT message formatting.");
232  owl_global_add_style(&g, s);
233#endif
[8b32593]234  s=owl_malloc(sizeof(owl_style));
[f1e629d]235  owl_style_create_internal(s, "oneline", &owl_stylefunc_oneline,
236                            "Formats for one-line-per-message");
[8b32593]237  owl_global_add_style(&g, s);
238
[7d4fbcd]239  /* setup the default filters */
[3abf28b]240  /* the personal filter will need to change again when AIM chat's are
241   *  included.  Also, there should be an %aimme% */
[a0a5179]242  owl_function_debugmsg("startup: creating default filters");
[bd3f232]243  f=owl_malloc(sizeof(owl_filter));
[3abf28b]244  owl_filter_init_fromstring(f, "personal", "( type ^zephyr$ "
245                             "and class ^message$ and instance ^personal$ "
246                             "and ( recipient ^%me%$ or sender ^%me%$ ) ) "
[3723f31]247                             "or ( type ^aim$ and login ^none$ )");
[7d4fbcd]248  owl_list_append_element(owl_global_get_filterlist(&g), f);
249
[bd3f232]250  f=owl_malloc(sizeof(owl_filter));
[0c502e9]251  owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )");
[7d4fbcd]252  owl_list_append_element(owl_global_get_filterlist(&g), f);
253
[bd3f232]254  f=owl_malloc(sizeof(owl_filter));
[0236842]255  owl_filter_init_fromstring(f, "ping", "opcode ^ping$");
256  owl_list_append_element(owl_global_get_filterlist(&g), f);
257
[bd3f232]258  f=owl_malloc(sizeof(owl_filter));
[0236842]259  owl_filter_init_fromstring(f, "auto", "opcode ^auto$");
260  owl_list_append_element(owl_global_get_filterlist(&g), f);
261
[bd3f232]262  f=owl_malloc(sizeof(owl_filter));
[0c502e9]263  owl_filter_init_fromstring(f, "login", "not login ^none$");
[0236842]264  owl_list_append_element(owl_global_get_filterlist(&g), f);
265
[bd3f232]266  f=owl_malloc(sizeof(owl_filter));
[7d4fbcd]267  owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$");
268  owl_list_append_element(owl_global_get_filterlist(&g), f);
269
[bd3f232]270  f=owl_malloc(sizeof(owl_filter));
[5a6e6b9]271  owl_filter_init_fromstring(f, "out", "direction ^out$");
272  owl_list_append_element(owl_global_get_filterlist(&g), f);
273
[bd3f232]274  f=owl_malloc(sizeof(owl_filter));
[31e48a3]275  owl_filter_init_fromstring(f, "aim", "type ^aim$");
276  owl_list_append_element(owl_global_get_filterlist(&g), f);
277
[bd3f232]278  f=owl_malloc(sizeof(owl_filter));
[31e48a3]279  owl_filter_init_fromstring(f, "zephyr", "type ^zephyr$");
280  owl_list_append_element(owl_global_get_filterlist(&g), f);
281
[bd3f232]282  f=owl_malloc(sizeof(owl_filter));
[89426ab]283  owl_filter_init_fromstring(f, "none", "false");
284  owl_list_append_element(owl_global_get_filterlist(&g), f);
285
[bd3f232]286  f=owl_malloc(sizeof(owl_filter));
[89426ab]287  owl_filter_init_fromstring(f, "all", "true");
[7d4fbcd]288  owl_list_append_element(owl_global_get_filterlist(&g), f);
289
290  /* set the current view */
[a0a5179]291  owl_function_debugmsg("startup: setting the current view");
[c3ab155]292  owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default"));
[7d4fbcd]293
[96f8e5b]294  /* AIM init */
[a0a5179]295  owl_function_debugmsg("startup: doing AIM initialization");
[96f8e5b]296  owl_aim_init();
297
[38cf544c]298  /* process the startup file */
[a0a5179]299  owl_function_debugmsg("startup: processing startup file");
[2404c3a]300  owl_function_source(NULL);
[38cf544c]301
[7d4fbcd]302  /* read the config file */
[a0a5179]303  owl_function_debugmsg("startup: processing config file");
[38cf544c]304  owl_context_set_readconfig(owl_global_get_context(&g));
[f1e629d]305  perlerr=owl_perlconfig_readconfig(configfile);
306  if (perlerr) {
[7d4fbcd]307    endwin();
[c3acb0b]308    owl_function_error("Error parsing configfile\n");
[5c1393d]309    fprintf(stderr, "\nError parsing configfile. (%s)\n", perlerr);
[c3acb0b]310    fflush(stderr);
[5c1393d]311    printf("\nError parsing configfile. (%s)\n", perlerr);
[c3acb0b]312    fflush(stdout);
[7d4fbcd]313    exit(1);
314  }
[ced25d1]315
[e075479]316  /* if the config defines a formatting function, add 'perl' as a style */
[bd3f232]317  if (owl_global_is_config_format(&g)) {
318    owl_function_debugmsg("Found perl formatting");
319    s=owl_malloc(sizeof(owl_style));
[f1e629d]320    owl_style_create_perl(s, "perl", "owl::_format_msg_legacy_wrap",
321                          "User-defined perl style that calls owl::format_msg"
322                          "with legacy global variable support");
[bd3f232]323    owl_global_add_style(&g, s);
[e075479]324    owl_global_set_default_style(&g, "perl");
[bd3f232]325  }
326
[ced25d1]327  /* execute the startup function in the configfile */
[a0a5179]328  owl_function_debugmsg("startup: executing perl startup, if applicable");
[f1e629d]329  perlout = owl_perlconfig_execute("owl::startup();");
[7d4fbcd]330  if (perlout) owl_free(perlout);
331 
332  /* hold on to the window names for convenience */
333  msgwin=owl_global_get_curs_msgwin(&g);
334  recwin=owl_global_get_curs_recwin(&g);
335  sepwin=owl_global_get_curs_sepwin(&g);
336  typwin=owl_global_get_curs_typwin(&g);
337  tw=owl_global_get_typwin(&g);
338
[252a5c2]339  /* welcome message */
[a0a5179]340  owl_function_debugmsg("startup: creating splash message");
[52f3507]341  strcpy(startupmsg, "-----------------------------------------------------------------------\n");
[252a5c2]342  sprintf(buff,      "Welcome to Owl version %s.  Press 'h' for on-line help. \n", OWL_VERSION_STRING);
343  strcat(startupmsg, buff);
[52f3507]344  strcat(startupmsg, "                                                                       \n");
345  strcat(startupmsg, "If you would like to receive release announcements about Owl you can   \n");
[d9b0b972]346  strcat(startupmsg, "join the owl-users@mit.edu mailing list at http://web.mit.edu/ktools/  \n");
[52f3507]347  strcat(startupmsg, "                                                                 ^ ^   \n");
348  strcat(startupmsg, "                                                                 OvO   \n");
349  strcat(startupmsg, "Please report any bugs or suggestions to bug-owl@mit.edu        (   )  \n");
350  strcat(startupmsg, "-----------------------------------------------------------------m-m---\n");
[252a5c2]351  owl_function_adminmsg("", startupmsg);
352  sepbar(NULL);
353
[7d4fbcd]354  wrefresh(sepwin);
355
356  /* load zephyr subs */
[bd3f232]357  if (initialsubs) {
[4357be8]358    int ret2;
[a0a5179]359    owl_function_debugmsg("startup: loading initial zephyr subs");
[4357be8]360
361    /* load default subscriptions */
362    ret=owl_zephyr_loaddefaultsubs();
[252a5c2]363   
[4357be8]364    /* load subscriptions from subs file */
365    ret2=owl_zephyr_loadsubs(NULL);
366
367    if (ret || ret2) {
368      owl_function_adminmsg("", "Error loading zephyr subscriptions");
369    } else if (ret2!=-1) {
[bd3f232]370      owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
371    }
[7d4fbcd]372
[bd3f232]373    /* load login subscriptions */
374    if (owl_global_is_loginsubs(&g)) {
[a0a5179]375      owl_function_debugmsg("startup: loading login subs");
[bd3f232]376      owl_function_loadloginsubs(NULL);
377    }
[7d4fbcd]378  }
379
[5a95b69]380  /* First buddy check to sync the list without notifications */
381  owl_function_debugmsg("startup: doing initial zephyr buddy check");
[4357be8]382  /* owl_function_zephyr_buddy_check(0); */
[5a95b69]383
[f933403]384  /* set the startup and default style, based on userclue and presence of a
385   * formatting function */
[a0a5179]386  owl_function_debugmsg("startup: setting startup and default style");
[27c3a93]387  if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) {
388    /* the style was set by the user: leave it alone */
389  } else if (owl_global_is_config_format(&g)) {
[f933403]390    owl_global_set_default_style(&g, "perl");
391  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
392    owl_global_set_default_style(&g, "default");
393  } else {
394    owl_global_set_default_style(&g, "basic");
395  }
396
[7d4fbcd]397  /* zlog in if we need to */
398  if (owl_global_is_startuplogin(&g)) {
[a0a5179]399    owl_function_debugmsg("startup: doing zlog in");
[31e48a3]400    owl_zephyr_zlog_in();
[7d4fbcd]401  }
402
[a0a5179]403  owl_function_debugmsg("startup: set style for the view");
[f1e629d]404  owl_view_set_style(owl_global_get_current_view(&g), 
405                     owl_global_get_style_by_name(&g, owl_global_get_default_style(&g)));   
[a0a5179]406
407  owl_function_debugmsg("startup: setting context interactive");
[7d4fbcd]408  owl_context_set_interactive(owl_global_get_context(&g));
409
[a15a84f]410  nexttimediff=10;
[4b464a4]411  nexttime=time(NULL);
412
[a0a5179]413  owl_function_debugmsg("startup: entering main loop");
[7d4fbcd]414  /* main loop */
415  while (1) {
416
417    /* if a resize has been scheduled, deal with it */
418    owl_global_resize(&g, 0, 0);
419
420    /* these are here in case a resize changes the windows */
421    msgwin=owl_global_get_curs_msgwin(&g);
422    recwin=owl_global_get_curs_recwin(&g);
423    sepwin=owl_global_get_curs_sepwin(&g);
424    typwin=owl_global_get_curs_typwin(&g);
425
426    followlast=owl_global_should_followlast(&g);
[5789230]427   
[a352335c]428    /* Do AIM stuff */
429    if (owl_global_is_doaimevents(&g)) {
[5789230]430      owl_aim_process_events();
[de03334]431
[a352335c]432      if (owl_global_is_aimloggedin(&g)) {
433        if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
[f4d0975]434          /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
[a352335c]435          owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
436        }
[de03334]437      }
[5789230]438    }
439
[4b464a4]440    /* little hack */
[a15a84f]441    now=time(NULL);
442    today=localtime(&now);
443    if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) {
[4b464a4]444      if (time(NULL)>nexttime) {
445        if (nexttimediff==1) {
[a15a84f]446          nexttimediff=10;
[4b464a4]447        } else {
448          nexttimediff=1;
449        }
450        nexttime+=nexttimediff;
451        owl_hack_animate();
452      }
453    }
454
[aac889a]455    /* Grab incoming messages. */
[d09e5a1]456    newmsgs=0;
[ced25d1]457    zpendcount=0;
[be0a79f]458    while(owl_zephyr_zpending() || owl_global_messagequeue_pending(&g)) {
[09489b89]459#ifdef HAVE_LIBZEPHYR
[7d4fbcd]460      struct sockaddr_in from;
[09489b89]461#endif
[e6449bc]462      owl_message *m=NULL;
[ecd5dc5]463      owl_filter *f;
[09489b89]464
[bd3f232]465      /* grab the new message, stick it in 'm' */
[be0a79f]466      if (owl_zephyr_zpending()) {
[09489b89]467#ifdef HAVE_LIBZEPHYR
[d09e5a1]468        /* grab a zephyr notice, but if we've done 20 without stopping,
469           take a break to process keystrokes etc. */
470        if (zpendcount>20) break;
471        ZReceiveNotice(&notice, &from);
472        zpendcount++;
473       
474        /* is this an ack from a zephyr we sent? */
475        if (owl_zephyr_notice_is_ack(&notice)) {
476          owl_zephyr_handle_ack(&notice);
477          continue;
478        }
479       
480        /* if it's a ping and we're not viewing pings then skip it */
481        if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
482          continue;
483        }
[7d4fbcd]484
[d09e5a1]485        /* create the new message */
486        m=owl_malloc(sizeof(owl_message));
487        owl_message_create_from_znotice(m, &notice);
[09489b89]488#endif
[d09e5a1]489      } else if (owl_global_messagequeue_pending(&g)) {
490        m=owl_global_messageuque_popmsg(&g);
[7d4fbcd]491      }
492     
[bd3f232]493      /* if this message it on the puntlist, nuke it and continue */
[7d4fbcd]494      if (owl_global_message_is_puntable(&g, m)) {
495        owl_message_free(m);
496        continue;
497      }
498
[280ddc6]499      /*  login or logout that should be ignored? */
500      if (owl_global_is_ignorelogins(&g) && owl_message_is_loginout(m)) {
501        owl_message_free(m);
502        continue;
503      }
504
[7d4fbcd]505      /* otherwise add it to the global list */
506      owl_messagelist_append_element(owl_global_get_msglist(&g), m);
[d09e5a1]507      newmsgs=1;
[7d4fbcd]508
509      /* let the config know the new message has been received */
[f1e629d]510      owl_perlconfig_getmsg(m, 0, NULL);
[7d4fbcd]511
512      /* add it to any necessary views; right now there's only the current view */
513      owl_view_consider_message(owl_global_get_current_view(&g), m);
514
515      /* do we need to autoreply? */
[9854278]516      if (owl_global_is_zaway(&g) && !owl_message_get_attribute_value(m, "isauto")) {
[1077753b]517        if (owl_message_is_type_zephyr(m)) {
518          owl_zephyr_zaway(m);
519        } else if (owl_message_is_type_aim(m)) {
[e9f239b]520          if (owl_message_is_private(m)) {
521            owl_function_send_aimawymsg(owl_message_get_sender(m), owl_global_get_zaway_msg(&g));
522          }
[1077753b]523        }
[7d4fbcd]524      }
525
526      /* ring the bell if it's a personal */
[5d365f6]527      if (!strcmp(owl_global_get_personalbell(&g), "on")) {
528          if (!owl_message_is_loginout(m) &&
529              !owl_message_is_mail(m) &&
530              owl_message_is_private(m)) {
531            owl_function_beep();
532          }
533      } else if (!strcmp(owl_global_get_personalbell(&g), "off")) {
534        /* do nothing */
535      } else {
536        f=owl_global_get_filter(&g, owl_global_get_personalbell(&g));
537        if (f && owl_filter_message_match(f, m)) {
538          owl_function_beep();
539        }
[7d4fbcd]540      }
541
[5d365f6]542
[ecd5dc5]543      /* if it matches the alert filter, do the alert action */
544      f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g));
545      if (f && owl_filter_message_match(f, m)) {
546        owl_function_command(owl_global_get_alert_action(&g));
547      }
548
[5a95b69]549      /* if it's a zephyr login or logout, update the zbuddylist */
550      if (owl_message_is_type_zephyr(m) && owl_message_is_loginout(m)) {
551        if (owl_message_is_login(m)) {
552          owl_zbuddylist_adduser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
553        } else if (owl_message_is_logout(m)) {
554          owl_zbuddylist_deluser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
555        } else {
556          owl_function_error("Internal error: received login notice that is neither login nor logout");
557        }
558      }
559
[7d4fbcd]560      /* check for burning ears message */
561      /* this is an unsupported feature */
562      if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) {
[1c6c4d3]563        char *buff;
564        buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m));
[7d4fbcd]565        owl_function_adminmsg(buff, "");
[1c6c4d3]566        owl_free(buff);
[7d4fbcd]567        owl_function_beep();
568      }
569
[d09e5a1]570      /* log the message if we need to */
[7d4fbcd]571      if (owl_global_is_logging(&g) || owl_global_is_classlogging(&g)) {
572        owl_log_incoming(m);
573      }
574    }
575
[5a95b69]576    /* is it time to check zbuddies? */
577    if (owl_global_is_pseudologins(&g)) {
578      if (owl_timer_is_expired(owl_global_get_zephyr_buddycheck_timer(&g))) {
579        owl_function_debugmsg("Doing zephyr buddy check");
580        owl_function_zephyr_buddy_check(1);
581        owl_timer_reset(owl_global_get_zephyr_buddycheck_timer(&g));
582      }
583    }
584
[afbf668]585    /* dispatch any muxevents */
586    owl_muxevents_dispatch(owl_global_get_muxevents(&g), 0);
587
[7d4fbcd]588    /* follow the last message if we're supposed to */
[d09e5a1]589    if (newmsgs && followlast) {
[7d4fbcd]590      owl_function_lastmsg_noredisplay();
591    }
[700c712]592
[ced25d1]593    /* do the newmsgproc thing */
[d09e5a1]594    if (newmsgs) {
[8f44c6b]595      owl_function_do_newmsgproc();
[700c712]596    }
[7d4fbcd]597   
598    /* redisplay if necessary */
[d09e5a1]599    /* this should be optimized to not run if the new messages won't be displayed */
600    if (newmsgs) {
[7d4fbcd]601      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
602      sepbar(NULL);
603      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
604        owl_popwin_refresh(owl_global_get_popwin(&g));
605        /* TODO: this is a broken kludge */
606        if (owl_global_get_viewwin(&g)) {
607          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
608        }
609      }
610      owl_global_set_needrefresh(&g);
611    }
612
613    /* if a popwin just came up, refresh it */
614    pw=owl_global_get_popwin(&g);
615    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
616      owl_popwin_refresh(pw);
617      owl_popwin_no_needs_first_refresh(pw);
618      owl_global_set_needrefresh(&g);
619      /* TODO: this is a broken kludge */
620      if (owl_global_get_viewwin(&g)) {
621        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
622      }
623    }
624
625    /* update the terminal if we need to */
626    if (owl_global_is_needrefresh(&g)) {
627      /* leave the cursor in the appropriate window */
628      if (owl_global_is_typwin_active(&g)) {
629        owl_function_set_cursor(typwin);
630      } else {
631        owl_function_set_cursor(sepwin);
632      }
633      doupdate();
634      owl_global_set_noneedrefresh(&g);
635    }
636
[bd3f232]637    /* Handle all keypresses.  If no key has been pressed, sleep for a
638     * little bit, but otherwise do not.  This lets input be grabbed
639     * as quickly as possbile */
[7d4fbcd]640    j=wgetch(typwin);
641    if (j==ERR) {
642      usleep(10);
643    } else {
[1fd4cd3e]644      /* find and activate the current keymap.
645       * TODO: this should really get fixed by activating
646       * keymaps as we switch between windows...
647       */
648      if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
649        owl_context_set_popless(owl_global_get_context(&g), 
650                                owl_global_get_viewwin(&g));
651        owl_function_activate_keymap("popless");
652      } else if (owl_global_is_typwin_active(&g) 
653                 && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
654        /*
655          owl_context_set_editline(owl_global_get_context(&g), tw);
656          owl_function_activate_keymap("editline");
657        */
658      } else if (owl_global_is_typwin_active(&g) 
659                 && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
660        owl_context_set_editmulti(owl_global_get_context(&g), tw);
661        owl_function_activate_keymap("editmulti");
662      } else {
663        owl_context_set_recv(owl_global_get_context(&g));
664        owl_function_activate_keymap("recv");
665      }
666      /* now actually handle the keypress */
667      ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
668      if (ret!=0 && ret!=1) {
669        owl_function_makemsg("Unable to handle keypress");
670      }
[7d4fbcd]671    }
[2a2bb60]672
[c9e72d1]673    /* Log any error signals */
674    {
675      siginfo_t si;
676      int signum;
677      if ((signum = owl_global_get_errsignal_and_clear(&g, &si)) > 0) {
[81634b6]678        owl_function_error("Got unexpected signal: %d %s  (code: %d band: %d  errno: %d)", 
[afbf668]679                           signum, signum==SIGPIPE?"SIGPIPE":"SIG????",
[81634b6]680                           si.si_code, si.si_band, si.si_errno);
[c9e72d1]681      }
682    }
683
[7d4fbcd]684  }
685}
686
[de22c3d]687void sig_handler(int sig, siginfo_t *si, void *data)
688{
[7d4fbcd]689  if (sig==SIGWINCH) {
[bd3f232]690    /* we can't inturrupt a malloc here, so it just sets a flag
691     * schedulding a resize for later
692     */
[7d4fbcd]693    owl_function_resize();
[afbf668]694  } else if (sig==SIGPIPE || sig==SIGCHLD) {
[c9e72d1]695    /* Set a flag and some info that we got the sigpipe
696     * so we can record that we got it and why... */
697    owl_global_set_errsignal(&g, sig, si);
[fe1f605]698  } else if (sig==SIGTERM || sig==SIGHUP) {
699    owl_function_quit();
[7d4fbcd]700  }
[c9e72d1]701
[7d4fbcd]702}
703
[de22c3d]704void usage()
705{
[e7cc1c3]706  fprintf(stderr, "Owl version %s\n", OWL_VERSION_STRING);
[a68f05d]707  fprintf(stderr, "Usage: owl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-t <ttyname>]\n");
[e7cc1c3]708  fprintf(stderr, "  -n      don't load zephyr subscriptions\n");
709  fprintf(stderr, "  -d      enable debugging\n");
[a68f05d]710  fprintf(stderr, "  -D      enable debugging and delete previous debug file\n");
[e7cc1c3]711  fprintf(stderr, "  -v      print the Owl version number and exit\n");
712  fprintf(stderr, "  -h      print this help message\n");
713  fprintf(stderr, "  -c      specify an alternate config file\n");
714  fprintf(stderr, "  -t      set the tty name\n");
[7d4fbcd]715}
[2a2bb60]716
717#if OWL_STDERR_REDIR
718
719/* Replaces stderr with a pipe so that we can read from it.
720 * Returns the fd of the pipe from which stderr can be read. */
[de22c3d]721int stderr_replace(void)
722{
[2a2bb60]723  int pipefds[2];
724  if (0 != pipe(pipefds)) {
725    perror("pipe");
726    owl_function_debugmsg("stderr_replace: pipe FAILED\n");
727    return -1;
728  }
729    owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]);
730  if (-1 == dup2(pipefds[1], 2 /*stderr*/)) {
731    owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno));
732    perror("dup2");
733    return -1;
734  }
735  return pipefds[0];
736}
737
[afbf668]738/* Sends stderr (read from rfd) messages to the error console */
739void stderr_redirect_handler(int handle, int rfd, int eventmask, void *data) 
[de22c3d]740{
[2a2bb60]741  int navail, bread;
742  char *buf;
743  /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/
744  if (rfd<0) return;
745  if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) {
746    return;
747  }
748  /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/
749  if (navail<=0) return;
750  if (navail>256) { navail = 256; }
751  buf = owl_malloc(navail+1);
752  bread = read(rfd, buf, navail);
753  if (buf[navail-1] != '\0') {
754    buf[navail] = '\0';
755  }
756  owl_function_error("Err: %s", buf);
757  owl_free(buf);
758}
759
760#endif /* OWL_STDERR_REDIR */
Note: See TracBrowser for help on using the repository browser.