source: owl.c @ 8e5935d

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