source: owl.c @ f094fc4

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