source: owl.c @ 18a54ee

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