source: owl.c @ cc6f009

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since cc6f009 was 0b7082c, checked in by Nelson Elhage <nelhage@mit.edu>, 17 years ago
Nuke the vt style as well
  • Property mode set to 100644
File size: 24.9 KB
Line 
1/*  Copyright (c) 2004 James Kretchmar. All rights reserved.
2 *
3 *  Redistribution and use in source and binary forms, with or without
4 *  modification, are permitted provided that the following conditions are
5 *  met:
6 * 
7 *    * Redistributions of source code must retain the above copyright
8 *      notice, this list of conditions and the following disclaimer.
9 * 
10 *    * Redistributions in binary form must reproduce the above copyright
11 *      notice, this list of conditions and the following disclaimer in
12 *      the documentation and/or other materials provided with the
13 *      distribution.
14 * 
15 *    * Redistributions in any form must be accompanied by information on
16 *      how to obtain complete source code for the Owl software and any
17 *      accompanying software that uses the Owl software. The source code
18 *      must either be included in the distribution or be available for no
19 *      more than the cost of distribution plus a nominal fee, and must be
20 *      freely redistributable under reasonable conditions. For an
21 *      executable file, complete source code means the source code for
22 *      all modules it contains. It does not include source code for
23 *      modules or files that typically accompany the major components of
24 *      the operating system on which the executable file runs.
25 * 
26 *
27 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 *  WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
30 *  NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
31 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
34 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
36 *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
37 *  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#include <stdio.h>
41#include <unistd.h>
42#include <stdlib.h>
43#include <string.h>
44#include <signal.h>
45#include <time.h>
46#include <sys/param.h>
47#include <sys/types.h>
48#include <termios.h>
49#include <sys/stat.h>
50#include "owl.h"
51
52#if OWL_STDERR_REDIR
53#ifdef HAVE_SYS_IOCTL_H
54#include <sys/ioctl.h>
55#endif
56#ifdef HAVE_SYS_FILIO_H
57#include <sys/filio.h>
58#endif
59int stderr_replace(void);
60#endif
61
62static const char fileIdent[] = "$Id$";
63
64owl_global g;
65
66int main(int argc, char **argv, char **env)
67{
68  WINDOW *recwin, *sepwin, *typwin, *msgwin;
69  owl_editwin *tw;
70  owl_popwin *pw;
71  int j, ret, initialsubs, debug, argcsave, followlast;
72  int newmsgs, nexttimediff;
73  struct sigaction sigact;
74  char *configfile, *tty, *perlout, *perlerr, **argvsave, buff[LINE], startupmsg[LINE];
75  char *confdir;
76  owl_filter *f;
77  owl_style *s;
78  time_t nexttime, now;
79  struct tm *today;
80  char *dir;
81  struct termios tio;
82  owl_message *m;
83#if OWL_STDERR_REDIR
84  int newstderr;
85#endif
86
87  argcsave=argc;
88  argvsave=argv;
89  configfile=NULL;
90  confdir = NULL;
91  tty=NULL;
92  debug=0;
93  initialsubs=1;
94  if (argc>0) {
95    argv++;
96    argc--;
97  }
98  while (argc>0) {
99    if (!strcmp(argv[0], "-n")) {
100      initialsubs=0;
101      argv++;
102      argc--;
103    } else if (!strcmp(argv[0], "-c")) {
104      if (argc<2) {
105        fprintf(stderr, "Too few arguments to -c\n");
106        usage();
107        exit(1);
108      }
109      configfile=argv[1];
110      argv+=2;
111      argc-=2;
112    } else if (!strcmp(argv[0], "-t")) {
113      if (argc<2) {
114        fprintf(stderr, "Too few arguments to -t\n");
115        usage();
116        exit(1);
117      }
118      tty=argv[1];
119      argv+=2;
120      argc-=2;
121    } else if (!strcmp(argv[0], "-s")){
122      if (argc<2) {
123        fprintf(stderr, "Too few arguments to -s\n");
124        usage();
125        exit(1);
126      }
127      confdir = argv[1];
128      argv+=2;
129      argc-=2;
130    } else if (!strcmp(argv[0], "-d")) {
131      debug=1;
132      argv++;
133      argc--;
134    } else if (!strcmp(argv[0], "-D")) {
135      debug=1;
136      unlink(OWL_DEBUG_FILE);
137      argv++;
138      argc--;
139    } else if (!strcmp(argv[0], "-v")) {
140      printf("This is barnowl version %s\n", OWL_VERSION_STRING);
141      exit(0);
142    } else {
143      fprintf(stderr, "Uknown argument\n");
144      usage();       
145      exit(1);
146    }
147  }
148
149  owl_function_debugmsg("startup: Finished parsing arguments");
150
151  /* signal handler */
152  /*sigact.sa_handler=sig_handler;*/
153  sigact.sa_sigaction=sig_handler;
154  sigemptyset(&sigact.sa_mask);
155  sigact.sa_flags=SA_SIGINFO;
156  sigaction(SIGWINCH, &sigact, NULL);
157  sigaction(SIGALRM, &sigact, NULL);
158  sigaction(SIGPIPE, &sigact, NULL);
159  sigaction(SIGTERM, &sigact, NULL);
160  sigaction(SIGHUP, &sigact, NULL);
161
162  /* save initial terminal settings */
163  tcgetattr(0, owl_global_get_startup_tio(&g));
164
165  /* turn ISTRIP off */
166  tcgetattr(0, &tio);
167  tio.c_iflag &= ~ISTRIP;
168  tcsetattr(0, TCSAFLUSH, &tio);
169
170  /* screen init */
171  if (!getenv("TERMINFO")) {
172    owl_function_debugmsg("startup: Not setting TERMINFO");
173  } else {
174    owl_function_debugmsg("startup: leaving TERMINFO as %s from envrionment", getenv("TERMINFO"));
175  }
176  initscr();
177  start_color();
178#ifdef HAVE_USE_DEFAULT_COLORS
179  use_default_colors();
180#endif
181  raw();
182  noecho();
183
184  /* define simple color pairs */
185  if (has_colors() && COLOR_PAIRS>=8) {
186    int bg = COLOR_BLACK;
187#ifdef HAVE_USE_DEFAULT_COLORS
188    bg = -1;
189#endif
190    init_pair(OWL_COLOR_BLACK,   COLOR_BLACK,   bg);
191    init_pair(OWL_COLOR_RED,     COLOR_RED,     bg);
192    init_pair(OWL_COLOR_GREEN,   COLOR_GREEN,   bg);
193    init_pair(OWL_COLOR_YELLOW,  COLOR_YELLOW,  bg);
194    init_pair(OWL_COLOR_BLUE,    COLOR_BLUE,    bg);
195    init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, bg);
196    init_pair(OWL_COLOR_CYAN,    COLOR_CYAN,    bg);
197    init_pair(OWL_COLOR_WHITE,   COLOR_WHITE,   bg);
198  }
199
200  /* owl global init */
201  owl_global_init(&g);
202  if (debug) owl_global_set_debug_on(&g);
203  if (confdir) owl_global_set_confdir(&g, confdir);
204  owl_function_debugmsg("startup: first available debugging message");
205  owl_global_set_startupargs(&g, argcsave, argvsave);
206  owl_global_set_haveaim(&g);
207
208#ifdef HAVE_LIBZEPHYR
209  /* zephyr init */
210  ret=owl_zephyr_initialize();
211  if (!ret)
212      owl_global_set_havezephyr(&g);
213#endif
214
215#if OWL_STDERR_REDIR
216  /* Do this only after we've started curses up... */
217  owl_function_debugmsg("startup: doing stderr redirection");
218  newstderr = stderr_replace();
219  owl_muxevents_add(owl_global_get_muxevents(&g), newstderr, OWL_MUX_READ,
220                    stderr_redirect_handler, NULL);
221#endif   
222
223  /* create the owl directory, in case it does not exist */
224  owl_function_debugmsg("startup: creating owl directory, if not present");
225  dir=owl_global_get_confdir(&g);
226  mkdir(dir, S_IRWXU);
227
228  /* set the tty, either from the command line, or by figuring it out */
229  owl_function_debugmsg("startup: setting tty name");
230  if (tty) {
231    owl_global_set_tty(&g, tty);
232  } else {
233    owl_global_set_tty(&g, owl_util_get_default_tty());
234  }
235
236  /* Initialize perl */
237  owl_function_debugmsg("startup: processing config file");
238  owl_context_set_readconfig(owl_global_get_context(&g));
239  perlerr=owl_perlconfig_initperl(configfile);
240  if (perlerr) {
241    endwin();
242    owl_function_error("Internal perl error: %s\n", perlerr);
243    fprintf(stderr, "Internal perl error: %s\n", perlerr);
244    fflush(stderr);
245    printf("Internal perl error: %s\n", perlerr);
246    fflush(stdout);
247    exit(1);
248  }
249
250  /* setup the built-in styles */
251  owl_function_debugmsg("startup: creating built-in styles");
252
253  s=owl_malloc(sizeof(owl_style));
254  owl_style_create_internal(s, "basic", &owl_stylefunc_basic, "Basic message formatting.");
255  owl_global_add_style(&g, s);
256  s=owl_malloc(sizeof(owl_style));
257  owl_style_create_internal(s, "oneline", &owl_stylefunc_oneline, "Formats for one-line-per-message");
258  owl_global_add_style(&g, s);
259
260  /* setup the default filters */
261  /* the personal filter will need to change again when AIM chat's are
262   *  included.  Also, there should be an %aimme% */
263  owl_function_debugmsg("startup: creating default filters");
264  f=owl_malloc(sizeof(owl_filter));
265  owl_filter_init_fromstring(f, "personal", "isprivate ^true$ and ( not type ^zephyr$"
266                             " or ( class ^message and"
267                             " ( instance ^personal$ or instance ^urgent$ ) ) )");
268  owl_list_append_element(owl_global_get_filterlist(&g), f);
269
270  f=owl_malloc(sizeof(owl_filter));
271  owl_filter_init_fromstring(f, "wordwrap", "not ( type ^admin$ or type ^zephyr$ ) ");
272  owl_list_append_element(owl_global_get_filterlist(&g), f);
273
274  f=owl_malloc(sizeof(owl_filter));
275  owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )");
276  owl_list_append_element(owl_global_get_filterlist(&g), f);
277
278  f=owl_malloc(sizeof(owl_filter));
279  owl_filter_init_fromstring(f, "ping", "opcode ^ping$");
280  owl_list_append_element(owl_global_get_filterlist(&g), f);
281
282  f=owl_malloc(sizeof(owl_filter));
283  owl_filter_init_fromstring(f, "auto", "opcode ^auto$");
284  owl_list_append_element(owl_global_get_filterlist(&g), f);
285
286  f=owl_malloc(sizeof(owl_filter));
287  owl_filter_init_fromstring(f, "login", "not login ^none$");
288  owl_list_append_element(owl_global_get_filterlist(&g), f);
289
290  f=owl_malloc(sizeof(owl_filter));
291  owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$");
292  owl_list_append_element(owl_global_get_filterlist(&g), f);
293
294  f=owl_malloc(sizeof(owl_filter));
295  owl_filter_init_fromstring(f, "out", "direction ^out$");
296  owl_list_append_element(owl_global_get_filterlist(&g), f);
297
298  f=owl_malloc(sizeof(owl_filter));
299  owl_filter_init_fromstring(f, "aim", "type ^aim$");
300  owl_list_append_element(owl_global_get_filterlist(&g), f);
301
302  f=owl_malloc(sizeof(owl_filter));
303  owl_filter_init_fromstring(f, "zephyr", "type ^zephyr$");
304  owl_list_append_element(owl_global_get_filterlist(&g), f);
305
306  f=owl_malloc(sizeof(owl_filter));
307  owl_filter_init_fromstring(f, "none", "false");
308  owl_list_append_element(owl_global_get_filterlist(&g), f);
309
310  f=owl_malloc(sizeof(owl_filter));
311  owl_filter_init_fromstring(f, "all", "true");
312  owl_list_append_element(owl_global_get_filterlist(&g), f);
313
314  /* set the current view */
315  owl_function_debugmsg("startup: setting the current view");
316  owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default"));
317
318  /* AIM init */
319  owl_function_debugmsg("startup: doing AIM initialization");
320  owl_aim_init();
321
322  /* execute the startup function in the configfile */
323  owl_function_debugmsg("startup: executing perl startup, if applicable");
324  perlout = owl_perlconfig_execute("BarnOwl::Hooks::_startup();");
325  if (perlout) owl_free(perlout);
326
327  /* hold on to the window names for convenience */
328  msgwin=owl_global_get_curs_msgwin(&g);
329  recwin=owl_global_get_curs_recwin(&g);
330  sepwin=owl_global_get_curs_sepwin(&g);
331  typwin=owl_global_get_curs_typwin(&g);
332  tw=owl_global_get_typwin(&g);
333
334  /* welcome message */
335  owl_function_debugmsg("startup: creating splash message");
336  strcpy(startupmsg, "-----------------------------------------------------------------------\n");
337  sprintf(buff,      "Welcome to barnowl version %s.  Press 'h' for on-line help.            \n", OWL_VERSION_STRING);
338  strcat(startupmsg, buff);
339  strcat(startupmsg, "                                                                       \n");
340  strcat(startupmsg, "This is a development build of barnowl. If you are using this          \n");
341  strcat(startupmsg, "build regularly, please add yourself to barnowl-users@mit.edu          \n");
342  strcat(startupmsg, "                                                                 ^ ^   \n");
343  strcat(startupmsg, "                                                                 OvO   \n");
344  strcat(startupmsg, "Please report any bugs to dirty-owl-hackers@mit.edu             (   )  \n");
345  strcat(startupmsg, "-----------------------------------------------------------------m-m---\n");
346  owl_function_adminmsg("", startupmsg);
347  sepbar(NULL);
348
349  /* process the startup file */
350  owl_function_debugmsg("startup: processing startup file");
351  owl_function_source(NULL);
352
353  wrefresh(sepwin);
354
355  /* load zephyr subs */
356  if (initialsubs) {
357    int ret2;
358    owl_function_debugmsg("startup: loading initial zephyr subs");
359
360    /* load default subscriptions */
361    ret=owl_zephyr_loaddefaultsubs();
362   
363    /* load subscriptions from subs file */
364    ret2=owl_zephyr_loadsubs(NULL, 0);
365
366    if (ret || ret2) {
367      owl_function_adminmsg("", "Error loading zephyr subscriptions");
368    } else if (ret2!=-1) {
369      owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
370    }
371
372    /* load login subscriptions */
373    if (owl_global_is_loginsubs(&g)) {
374      owl_function_debugmsg("startup: loading login subs");
375      owl_function_loadloginsubs(NULL);
376    }
377  }
378
379  /* First buddy check to sync the list without notifications */
380  owl_function_debugmsg("startup: doing initial zephyr buddy check");
381  /* owl_function_zephyr_buddy_check(0); */
382
383  /* set the startup and default style, based on userclue and presence of a
384   * formatting function */
385  owl_function_debugmsg("startup: setting startup and default style");
386  if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) {
387    /* the style was set by the user: leave it alone */
388  } else if (owl_global_is_config_format(&g)) {
389    owl_global_set_default_style(&g, "perl");
390  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
391    owl_global_set_default_style(&g, "default");
392  } else {
393    owl_global_set_default_style(&g, "basic");
394  }
395
396  /* zlog in if we need to */
397  if (owl_global_is_startuplogin(&g)) {
398    owl_function_debugmsg("startup: doing zlog in");
399    owl_zephyr_zlog_in();
400  }
401
402  owl_function_debugmsg("startup: set style for the view: %s", owl_global_get_default_style(&g));
403  s = owl_global_get_style_by_name(&g, owl_global_get_default_style(&g));
404  if(s)
405      owl_view_set_style(owl_global_get_current_view(&g), s);
406  else
407      owl_function_error("No such style: %s", owl_global_get_default_style(&g));
408
409  owl_function_debugmsg("startup: setting context interactive");
410  owl_context_set_interactive(owl_global_get_context(&g));
411
412  nexttimediff=10;
413  nexttime=time(NULL);
414
415  owl_function_debugmsg("startup: entering main loop");
416  /* main loop */
417  while (1) {
418
419    /* if a resize has been scheduled, deal with it */
420    owl_global_resize(&g, 0, 0);
421
422    /* these are here in case a resize changes the windows */
423    msgwin=owl_global_get_curs_msgwin(&g);
424    recwin=owl_global_get_curs_recwin(&g);
425    sepwin=owl_global_get_curs_sepwin(&g);
426    typwin=owl_global_get_curs_typwin(&g);
427
428    followlast=owl_global_should_followlast(&g);
429   
430    /* Do AIM stuff */
431    if (owl_global_is_doaimevents(&g)) {
432      owl_aim_process_events();
433
434      if (owl_global_is_aimloggedin(&g)) {
435        if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
436          /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
437          owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
438        }
439      }
440    }
441
442    owl_perlconfig_mainloop();
443
444    /* little hack */
445    now=time(NULL);
446    today=localtime(&now);
447    if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) {
448      if (time(NULL)>nexttime) {
449        if (nexttimediff==1) {
450          nexttimediff=10;
451        } else {
452          nexttimediff=1;
453        }
454        nexttime+=nexttimediff;
455        owl_hack_animate();
456      }
457    }
458
459    owl_zephyr_process_events();
460   
461    /* Grab incoming messages. */
462    newmsgs=0;
463    while(owl_global_messagequeue_pending(&g)) {
464
465      m = owl_global_messagequeue_popmsg(&g);
466
467      if(owl_process_message(m))
468        newmsgs = 1;
469    }
470
471    /* is it time to check zbuddies? */
472    if (owl_global_is_pseudologins(&g)) {
473      if (owl_timer_is_expired(owl_global_get_zephyr_buddycheck_timer(&g))) {
474        owl_function_debugmsg("Doing zephyr buddy check");
475        owl_function_zephyr_buddy_check(1);
476        owl_timer_reset(owl_global_get_zephyr_buddycheck_timer(&g));
477      }
478    }
479
480    /* dispatch any muxevents */
481    owl_muxevents_dispatch(owl_global_get_muxevents(&g), 0);
482
483    /* follow the last message if we're supposed to */
484    if (newmsgs && followlast) {
485      owl_function_lastmsg_noredisplay();
486    }
487
488    /* do the newmsgproc thing */
489    if (newmsgs) {
490      owl_function_do_newmsgproc();
491    }
492   
493    /* redisplay if necessary */
494    /* this should be optimized to not run if the new messages won't be displayed */
495    if (newmsgs) {
496      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
497      sepbar(NULL);
498      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
499        owl_popwin_refresh(owl_global_get_popwin(&g));
500        /* TODO: this is a broken kludge */
501        if (owl_global_get_viewwin(&g)) {
502          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
503        }
504      }
505      owl_global_set_needrefresh(&g);
506    }
507
508    /* if a popwin just came up, refresh it */
509    pw=owl_global_get_popwin(&g);
510    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
511      owl_popwin_refresh(pw);
512      owl_popwin_no_needs_first_refresh(pw);
513      owl_global_set_needrefresh(&g);
514      /* TODO: this is a broken kludge */
515      if (owl_global_get_viewwin(&g)) {
516        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
517      }
518    }
519
520    /* update the terminal if we need to */
521    if (owl_global_is_needrefresh(&g)) {
522      /* leave the cursor in the appropriate window */
523      if (owl_global_is_typwin_active(&g)) {
524        owl_function_set_cursor(typwin);
525      } else {
526        owl_function_set_cursor(sepwin);
527      }
528      doupdate();
529      owl_global_set_noneedrefresh(&g);
530    }
531
532    /* Handle all keypresses.  If no key has been pressed, sleep for a
533     * little bit, but otherwise do not.  This lets input be grabbed
534     * as quickly as possbile */
535    j=wgetch(typwin);
536    if (j==ERR) {
537      usleep(10000);
538    } else {
539      owl_global_update_lastinputtime(&g);
540      /* find and activate the current keymap.
541       * TODO: this should really get fixed by activating
542       * keymaps as we switch between windows...
543       */
544      if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
545        owl_context_set_popless(owl_global_get_context(&g), 
546                                owl_global_get_viewwin(&g));
547        owl_function_activate_keymap("popless");
548      } else if (owl_global_is_typwin_active(&g) 
549                 && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
550        /*
551          owl_context_set_editline(owl_global_get_context(&g), tw);
552          owl_function_activate_keymap("editline");
553        */
554      } else if (owl_global_is_typwin_active(&g) 
555                 && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
556        owl_context_set_editmulti(owl_global_get_context(&g), tw);
557        owl_function_activate_keymap("editmulti");
558      } else {
559        owl_context_set_recv(owl_global_get_context(&g));
560        owl_function_activate_keymap("recv");
561      }
562      /* now actually handle the keypress */
563      ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
564      if (ret!=0 && ret!=1) {
565        owl_function_makemsg("Unable to handle keypress");
566      }
567    }
568
569    /* Log any error signals */
570    {
571      siginfo_t si;
572      int signum;
573      if ((signum = owl_global_get_errsignal_and_clear(&g, &si)) > 0) {
574        owl_function_error("Got unexpected signal: %d %s  (code: %d band: %d  errno: %d)", 
575                           signum, signum==SIGPIPE?"SIGPIPE":"SIG????",
576                           si.si_code, si.si_band, si.si_errno);
577      }
578    }
579
580  }
581}
582
583/*
584 * Process a new message passed to us on the message queue from some
585 * protocol. This includes adding it to the message list, updating the
586 * view and scrolling if appropriate, logging it, and so on.
587 *
588 * Either a pointer is kept to the message internally, or it is freed
589 * if unneeded. The caller no longer ``owns'' the message's memory.
590 *
591 * Returns 1 if the message was added to the message list, and 0 if it
592 * was ignored due to user settings or otherwise.
593 */
594int owl_process_message(owl_message *m) {
595  owl_filter *f;
596  /* if this message it on the puntlist, nuke it and continue */
597  if (owl_global_message_is_puntable(&g, m)) {
598    owl_message_free(m);
599    return 0;
600  }
601
602  /*  login or logout that should be ignored? */
603  if (owl_global_is_ignorelogins(&g)
604      && owl_message_is_loginout(m)) {
605    owl_message_free(m);
606    return 0;
607  }
608
609  if (!owl_global_is_displayoutgoing(&g)
610      && owl_message_is_direction_out(m)) {
611    owl_message_free(m);
612    return 0;
613  }
614
615  /* add it to the global list */
616  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
617  /* add it to any necessary views; right now there's only the current view */
618  owl_view_consider_message(owl_global_get_current_view(&g), m);
619
620  if(owl_message_is_direction_in(m)) {
621    /* let perl know about it*/
622    owl_perlconfig_getmsg(m, 0, NULL);
623
624    /* do we need to autoreply? */
625    if (owl_global_is_zaway(&g) && !owl_message_get_attribute_value(m, "isauto")) {
626      if (owl_message_is_type_zephyr(m)) {
627        owl_zephyr_zaway(m);
628      } else if (owl_message_is_type_aim(m)) {
629        if (owl_message_is_private(m)) {
630          owl_function_send_aimawymsg(owl_message_get_sender(m), owl_global_get_zaway_msg(&g));
631        }
632      }
633    }
634
635    /* ring the bell if it's a personal */
636    if (!strcmp(owl_global_get_personalbell(&g), "on")) {
637      if (!owl_message_is_loginout(m) &&
638          !owl_message_is_mail(m) &&
639          owl_message_is_personal(m)) {
640        owl_function_beep();
641      }
642    } else if (!strcmp(owl_global_get_personalbell(&g), "off")) {
643      /* do nothing */
644    } else {
645      f=owl_global_get_filter(&g, owl_global_get_personalbell(&g));
646      if (f && owl_filter_message_match(f, m)) {
647        owl_function_beep();
648      }
649    }
650
651    /* if it matches the alert filter, do the alert action */
652    f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g));
653    if (f && owl_filter_message_match(f, m)) {
654      owl_function_command(owl_global_get_alert_action(&g));
655    }
656
657    /* if it's a zephyr login or logout, update the zbuddylist */
658    if (owl_message_is_type_zephyr(m) && owl_message_is_loginout(m)) {
659      if (owl_message_is_login(m)) {
660        owl_zbuddylist_adduser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
661      } else if (owl_message_is_logout(m)) {
662        owl_zbuddylist_deluser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
663      } else {
664        owl_function_error("Internal error: received login notice that is neither login nor logout");
665      }
666    }
667
668    /* check for burning ears message */
669    /* this is an unsupported feature */
670    if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) {
671      char *buff;
672      buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m));
673      owl_function_adminmsg(buff, "");
674      owl_free(buff);
675      owl_function_beep();
676    }
677  }
678
679  /* log the message if we need to */
680  owl_log_message(m);
681
682  return 1;
683}
684
685void sig_handler(int sig, siginfo_t *si, void *data)
686{
687  if (sig==SIGWINCH) {
688    /* we can't inturrupt a malloc here, so it just sets a flag
689     * schedulding a resize for later
690     */
691    owl_function_resize();
692  } else if (sig==SIGPIPE || sig==SIGCHLD) {
693    /* Set a flag and some info that we got the sigpipe
694     * so we can record that we got it and why... */
695    owl_global_set_errsignal(&g, sig, si);
696  } else if (sig==SIGTERM || sig==SIGHUP) {
697    owl_function_quit();
698  }
699
700}
701
702void usage()
703{
704  fprintf(stderr, "Barnowl version %s\n", OWL_VERSION_STRING);
705  fprintf(stderr, "Usage: barnowl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-s <confdir>] [-t <ttyname>]\n");
706  fprintf(stderr, "  -n      don't load zephyr subscriptions\n");
707  fprintf(stderr, "  -d      enable debugging\n");
708  fprintf(stderr, "  -D      enable debugging and delete previous debug file\n");
709  fprintf(stderr, "  -v      print the Barnowl version number and exit\n");
710  fprintf(stderr, "  -h      print this help message\n");
711  fprintf(stderr, "  -c      specify an alternate config file\n");
712  fprintf(stderr, "  -s      specify an alternate config dir (default ~/.owl)\n");
713  fprintf(stderr, "  -t      set the tty name\n");
714}
715
716#if OWL_STDERR_REDIR
717
718/* Replaces stderr with a pipe so that we can read from it.
719 * Returns the fd of the pipe from which stderr can be read. */
720int stderr_replace(void)
721{
722  int pipefds[2];
723  if (0 != pipe(pipefds)) {
724    perror("pipe");
725    owl_function_debugmsg("stderr_replace: pipe FAILED\n");
726    return -1;
727  }
728    owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]);
729  if (-1 == dup2(pipefds[1], 2 /*stderr*/)) {
730    owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno));
731    perror("dup2");
732    return -1;
733  }
734  return pipefds[0];
735}
736
737/* Sends stderr (read from rfd) messages to the error console */
738void stderr_redirect_handler(int handle, int rfd, int eventmask, void *data) 
739{
740  int navail, bread;
741  char *buf;
742  /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/
743  if (rfd<0) return;
744  if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) {
745    return;
746  }
747  /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/
748  if (navail<=0) return;
749  if (navail>256) { navail = 256; }
750  buf = owl_malloc(navail+1);
751  bread = read(rfd, buf, navail);
752  if (buf[navail-1] != '\0') {
753    buf[navail] = '\0';
754  }
755  owl_function_error("Err: %s", buf);
756  owl_free(buf);
757}
758
759#endif /* OWL_STDERR_REDIR */
Note: See TracBrowser for help on using the repository browser.