source: owl.c @ e8c6d8f

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