source: owl.c @ 34509d5

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