source: owl.c @ b70d24f

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