source: owl.c @ 095e717

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