source: owl.c @ c55ad84

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