source: owl.c @ c1d9441

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