source: owl.c @ 5bc0f68

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