source: owl.c @ 13a3c1db

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