source: owl.c @ 4e0f545

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