source: owl.c @ b1f2fb0

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