source: owl.c @ 5ca5f8e

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