source: owl.c @ 4cbb1b4

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