source: owl.c @ d8671a1

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