source: owl.c @ 1895c29

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 1895c29 was f36cd97, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
Add a void* data to owl_dispatch and pass it around. Add a void* data field for the use of dispatches, and pass around the owl_dispatch. This allows one dispatch function to be used for multiple fd's, and lets us eliminate perl-specific code from the dispatch loop. In addition, we need to add a destructor to owl_dispatch so that perl dispatches can have their SV* decref'd on destroy.
  • Property mode set to 100644
File size: 24.0 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;
[f36cd97]192    d->destroy = NULL;
[9c7a701]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;
[f36cd97]203    d->destroy = NULL;
[9c7a701]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);
[799b60e]324  strcat(startupmsg, "To see a quick introduction, type ':show quickstart'.                  \n");
[52f3507]325  strcat(startupmsg, "                                                                       \n");
[628c897]326  strcat(startupmsg, "BarnOwl is free software. Type ':show license' for more                \n");
[799b60e]327  strcat(startupmsg, "information.                                                     ^ ^   \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
[b7bb454]396
[c675b39]397  owl_select_add_timer(180, 180, owl_zephyr_buddycheck_timer, NULL, NULL);
[b7bb454]398
399  /* If we ever deprecate the mainloop hook, remove this. */
[c675b39]400  owl_select_add_timer(0, 1, owl_perlconfig_mainloop, NULL, NULL);
[b7bb454]401
402
[a0a5179]403  owl_function_debugmsg("startup: entering main loop");
[7d4fbcd]404  /* main loop */
405  while (1) {
406
407    /* if a resize has been scheduled, deal with it */
408    owl_global_resize(&g, 0, 0);
409
410    /* these are here in case a resize changes the windows */
411    msgwin=owl_global_get_curs_msgwin(&g);
412    recwin=owl_global_get_curs_recwin(&g);
413    sepwin=owl_global_get_curs_sepwin(&g);
414    typwin=owl_global_get_curs_typwin(&g);
415
416    followlast=owl_global_should_followlast(&g);
[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
[afbf668]443    /* dispatch any muxevents */
444    owl_muxevents_dispatch(owl_global_get_muxevents(&g), 0);
445
[7d4fbcd]446    /* follow the last message if we're supposed to */
[d09e5a1]447    if (newmsgs && followlast) {
[7d4fbcd]448      owl_function_lastmsg_noredisplay();
449    }
[700c712]450
[ced25d1]451    /* do the newmsgproc thing */
[d09e5a1]452    if (newmsgs) {
[8f44c6b]453      owl_function_do_newmsgproc();
[700c712]454    }
[7d4fbcd]455   
456    /* redisplay if necessary */
[d09e5a1]457    /* this should be optimized to not run if the new messages won't be displayed */
458    if (newmsgs) {
[7d4fbcd]459      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
460      sepbar(NULL);
461      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
462        owl_popwin_refresh(owl_global_get_popwin(&g));
463        /* TODO: this is a broken kludge */
464        if (owl_global_get_viewwin(&g)) {
465          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
466        }
467      }
468      owl_global_set_needrefresh(&g);
469    }
470
471    /* if a popwin just came up, refresh it */
472    pw=owl_global_get_popwin(&g);
473    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
474      owl_popwin_refresh(pw);
475      owl_popwin_no_needs_first_refresh(pw);
476      owl_global_set_needrefresh(&g);
477      /* TODO: this is a broken kludge */
478      if (owl_global_get_viewwin(&g)) {
479        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
480      }
481    }
482
483    /* update the terminal if we need to */
484    if (owl_global_is_needrefresh(&g)) {
485      /* leave the cursor in the appropriate window */
486      if (owl_global_is_typwin_active(&g)) {
487        owl_function_set_cursor(typwin);
488      } else {
489        owl_function_set_cursor(sepwin);
490      }
491      doupdate();
492      owl_global_set_noneedrefresh(&g);
493    }
494
[9c7a701]495    /* select on FDs we know about. */
496    owl_select();
[2a2bb60]497
[c9e72d1]498    /* Log any error signals */
499    {
500      siginfo_t si;
501      int signum;
502      if ((signum = owl_global_get_errsignal_and_clear(&g, &si)) > 0) {
[6eaf35b]503        owl_function_error("Got unexpected signal: %d %s  (code: %d band: %ld  errno: %d)",
[afbf668]504                           signum, signum==SIGPIPE?"SIGPIPE":"SIG????",
[81634b6]505                           si.si_code, si.si_band, si.si_errno);
[c9e72d1]506      }
507    }
508
[7d4fbcd]509  }
510}
511
[13a3c1db]512/*
513 * Process a new message passed to us on the message queue from some
514 * protocol. This includes adding it to the message list, updating the
515 * view and scrolling if appropriate, logging it, and so on.
516 *
517 * Either a pointer is kept to the message internally, or it is freed
518 * if unneeded. The caller no longer ``owns'' the message's memory.
519 *
520 * Returns 1 if the message was added to the message list, and 0 if it
521 * was ignored due to user settings or otherwise.
522 */
523int owl_process_message(owl_message *m) {
524  owl_filter *f;
525  /* if this message it on the puntlist, nuke it and continue */
526  if (owl_global_message_is_puntable(&g, m)) {
527    owl_message_free(m);
528    return 0;
529  }
530
531  /*  login or logout that should be ignored? */
532  if (owl_global_is_ignorelogins(&g)
533      && owl_message_is_loginout(m)) {
534    owl_message_free(m);
535    return 0;
536  }
537
538  if (!owl_global_is_displayoutgoing(&g)
539      && owl_message_is_direction_out(m)) {
540    owl_message_free(m);
541    return 0;
542  }
543
544  /* add it to the global list */
545  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
546  /* add it to any necessary views; right now there's only the current view */
547  owl_view_consider_message(owl_global_get_current_view(&g), m);
548
549  if(owl_message_is_direction_in(m)) {
550    /* let perl know about it*/
[b67ab6b]551    owl_perlconfig_getmsg(m, NULL);
[13a3c1db]552
553    /* do we need to autoreply? */
554    if (owl_global_is_zaway(&g) && !owl_message_get_attribute_value(m, "isauto")) {
555      if (owl_message_is_type_zephyr(m)) {
556        owl_zephyr_zaway(m);
557      } else if (owl_message_is_type_aim(m)) {
558        if (owl_message_is_private(m)) {
559          owl_function_send_aimawymsg(owl_message_get_sender(m), owl_global_get_zaway_msg(&g));
560        }
561      }
562    }
563
564    /* ring the bell if it's a personal */
565    if (!strcmp(owl_global_get_personalbell(&g), "on")) {
566      if (!owl_message_is_loginout(m) &&
567          !owl_message_is_mail(m) &&
568          owl_message_is_personal(m)) {
569        owl_function_beep();
570      }
571    } else if (!strcmp(owl_global_get_personalbell(&g), "off")) {
572      /* do nothing */
573    } else {
574      f=owl_global_get_filter(&g, owl_global_get_personalbell(&g));
575      if (f && owl_filter_message_match(f, m)) {
576        owl_function_beep();
577      }
578    }
579
580    /* if it matches the alert filter, do the alert action */
581    f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g));
582    if (f && owl_filter_message_match(f, m)) {
583      owl_function_command(owl_global_get_alert_action(&g));
584    }
585
586    /* if it's a zephyr login or logout, update the zbuddylist */
587    if (owl_message_is_type_zephyr(m) && owl_message_is_loginout(m)) {
588      if (owl_message_is_login(m)) {
589        owl_zbuddylist_adduser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
590      } else if (owl_message_is_logout(m)) {
591        owl_zbuddylist_deluser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
592      } else {
593        owl_function_error("Internal error: received login notice that is neither login nor logout");
594      }
595    }
596  }
597
[0f9eca7]598  /* let perl know about it */
599  owl_perlconfig_newmsg(m, NULL);
[13a3c1db]600  /* log the message if we need to */
601  owl_log_message(m);
602
603  return 1;
604}
605
[f36cd97]606void owl_process_input(owl_dispatch *d)
[9c7a701]607{
[5f3168a]608  int ret;
609  owl_input j;
[9c7a701]610  owl_popwin *pw;
611  owl_editwin *tw;
[5f3168a]612  WINDOW *typwin;
[9c7a701]613
[5f3168a]614  typwin = owl_global_get_curs_typwin(&g);
[4cc0ee0b]615  while (1) {
[07bfbc2]616    j.ch = wgetch(typwin);
617    if (j.ch == ERR) return;
618   
[4cc0ee0b]619    owl_global_set_lastinputtime(&g, time(NULL));
620    pw=owl_global_get_popwin(&g);
621    tw=owl_global_get_typwin(&g);
[9c7a701]622
[07bfbc2]623    j.uch = '\0';
624    if (j.ch >= KEY_MIN && j.ch <= KEY_MAX) {
625      /* This is a curses control character. */
[5f3168a]626    }
[07bfbc2]627    else if (j.ch > 0x7f && j.ch < 0xfe) {
628      /* Pull in a full utf-8 character. */
629      int bytes, i;
630      char utf8buf[7];
631      memset(utf8buf, '\0', 7);
632     
633      utf8buf[0] = j.ch;
634     
635      if ((j.ch & 0xc0) && (~j.ch & 0x20)) bytes = 2;
636      else if ((j.ch & 0xe0) && (~j.ch & 0x10)) bytes = 3;
637      else if ((j.ch & 0xf0) && (~j.ch & 0x08)) bytes = 4;
638      else if ((j.ch & 0xf8) && (~j.ch & 0x04)) bytes = 5;
639      else if ((j.ch & 0xfc) && (~j.ch & 0x02)) bytes = 6;
640      else bytes = 1;
641     
642      for (i = 1; i < bytes; i++) {
643        int tmp =  wgetch(typwin);
644        /* If what we got was not a byte, or not a continuation byte */
645        if (tmp > 0xff || !(tmp & 0x80 && ~tmp & 0x40)) {
646          /* ill-formed UTF-8 code unit subsequence, put back the
647             char we just got. */
648          ungetch(tmp);
649          j.ch = ERR;
650          break;
651        }
652        utf8buf[i] = tmp;
[5f3168a]653      }
[07bfbc2]654     
655      if (j.ch != ERR) {
656        if (g_utf8_validate(utf8buf, -1, NULL)) {
657          j.uch = g_utf8_get_char(utf8buf);
658        }
659        else {
660          j.ch = ERR;
661        }
[5f3168a]662      }
663    }
[07bfbc2]664    else if (j.ch <= 0x7f) {
665      j.uch = j.ch;
666    }
[4cc0ee0b]667   
[07bfbc2]668    owl_global_set_lastinputtime(&g, time(NULL));
[4cc0ee0b]669    /* find and activate the current keymap.
670     * TODO: this should really get fixed by activating
671     * keymaps as we switch between windows...
672     */
673    if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
674      owl_context_set_popless(owl_global_get_context(&g), 
675                              owl_global_get_viewwin(&g));
676      owl_function_activate_keymap("popless");
677    } else if (owl_global_is_typwin_active(&g) 
[07bfbc2]678               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
[4cc0ee0b]679      /*
680        owl_context_set_editline(owl_global_get_context(&g), tw);
681        owl_function_activate_keymap("editline");
682      */
683    } else if (owl_global_is_typwin_active(&g) 
[07bfbc2]684               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
[4cc0ee0b]685      owl_context_set_editmulti(owl_global_get_context(&g), tw);
686      owl_function_activate_keymap("editmulti");
687    } else {
688      owl_context_set_recv(owl_global_get_context(&g));
689      owl_function_activate_keymap("recv");
690    }
691    /* now actually handle the keypress */
692    ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
[07bfbc2]693    if (ret!=0 && ret!=1) {
[4cc0ee0b]694      owl_function_makemsg("Unable to handle keypress");
695    }
[9c7a701]696  }
697}
698
[de22c3d]699void sig_handler(int sig, siginfo_t *si, void *data)
700{
[7d4fbcd]701  if (sig==SIGWINCH) {
[bd3f232]702    /* we can't inturrupt a malloc here, so it just sets a flag
703     * schedulding a resize for later
704     */
[7d4fbcd]705    owl_function_resize();
[afbf668]706  } else if (sig==SIGPIPE || sig==SIGCHLD) {
[c9e72d1]707    /* Set a flag and some info that we got the sigpipe
708     * so we can record that we got it and why... */
709    owl_global_set_errsignal(&g, sig, si);
[fe1f605]710  } else if (sig==SIGTERM || sig==SIGHUP) {
711    owl_function_quit();
[7d4fbcd]712  }
713}
714
[de22c3d]715void usage()
716{
[78667f3]717  fprintf(stderr, "Barnowl version %s\n", OWL_VERSION_STRING);
718  fprintf(stderr, "Usage: barnowl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-s <confdir>] [-t <ttyname>]\n");
[e7cc1c3]719  fprintf(stderr, "  -n      don't load zephyr subscriptions\n");
720  fprintf(stderr, "  -d      enable debugging\n");
[a68f05d]721  fprintf(stderr, "  -D      enable debugging and delete previous debug file\n");
[78667f3]722  fprintf(stderr, "  -v      print the Barnowl version number and exit\n");
[e7cc1c3]723  fprintf(stderr, "  -h      print this help message\n");
724  fprintf(stderr, "  -c      specify an alternate config file\n");
[b363d83]725  fprintf(stderr, "  -s      specify an alternate config dir (default ~/.owl)\n");
[e7cc1c3]726  fprintf(stderr, "  -t      set the tty name\n");
[7d4fbcd]727}
[2a2bb60]728
729#if OWL_STDERR_REDIR
730
731/* Replaces stderr with a pipe so that we can read from it.
732 * Returns the fd of the pipe from which stderr can be read. */
[de22c3d]733int stderr_replace(void)
734{
[2a2bb60]735  int pipefds[2];
736  if (0 != pipe(pipefds)) {
737    perror("pipe");
738    owl_function_debugmsg("stderr_replace: pipe FAILED\n");
739    return -1;
740  }
741    owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]);
742  if (-1 == dup2(pipefds[1], 2 /*stderr*/)) {
743    owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno));
744    perror("dup2");
745    return -1;
746  }
747  return pipefds[0];
748}
749
[afbf668]750/* Sends stderr (read from rfd) messages to the error console */
751void stderr_redirect_handler(int handle, int rfd, int eventmask, void *data) 
[de22c3d]752{
[2a2bb60]753  int navail, bread;
754  char *buf;
755  /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/
756  if (rfd<0) return;
757  if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) {
758    return;
759  }
760  /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/
761  if (navail<=0) return;
762  if (navail>256) { navail = 256; }
763  buf = owl_malloc(navail+1);
764  bread = read(rfd, buf, navail);
765  if (buf[navail-1] != '\0') {
766    buf[navail] = '\0';
767  }
768  owl_function_error("Err: %s", buf);
769  owl_free(buf);
770}
771
772#endif /* OWL_STDERR_REDIR */
[b7bb454]773
774void owl_zephyr_buddycheck_timer(owl_timer *t, void *data)
775{
[80a465c]776  if (owl_global_is_pseudologins(&g)) {
[b7bb454]777    owl_function_debugmsg("Doing zephyr buddy check");
778    owl_function_zephyr_buddy_check(1);
[80a465c]779  }
[b7bb454]780}
Note: See TracBrowser for help on using the repository browser.