source: owl.c @ db0ac7e

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since db0ac7e was db0ac7e, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 17 years ago
Merged revisions 983-1032 via svnmerge from file:///afs/sipb.mit.edu/project/barnowl/src/svn/trunk ........ r985 | nelhage | 2008-03-11 12:38:20 -0400 (Tue, 11 Mar 2008) | 2 lines Generate less ugly error spew if a module fails to load ........ r986 | geofft | 2008-03-13 15:56:20 -0400 (Thu, 13 Mar 2008) | 1 line Added :webzephyr command with keybinding W. ........ r987 | asedeno | 2008-03-18 23:57:24 -0400 (Tue, 18 Mar 2008) | 2 lines Fix a race condition in which zephyrs received during init are not noticed until the next zephyr after entering the mainloop. ........ r988 | geofft | 2008-03-19 15:46:43 -0400 (Wed, 19 Mar 2008) | 6 lines IRC: explicitly include the network (-a $alias) in reply commands. This may be unnecessary because :irc-msg looks at getcurmsg() if it cannot automatically determine the network. ........ r989 | geofft | 2008-03-21 14:47:51 -0400 (Fri, 21 Mar 2008) | 1 line syntax error. ........ r990 | nelhage | 2008-03-26 02:24:26 -0400 (Wed, 26 Mar 2008) | 2 lines Fix sending jabbers to JIDs beginning with `+' ........ r994 | nelhage | 2008-03-29 17:02:05 -0400 (Sat, 29 Mar 2008) | 3 lines Compile zcrypt.c with -w so I don't get all these warnings in my compile output whenever I change any headers ........ r998 | geofft | 2008-03-31 01:59:47 -0400 (Mon, 31 Mar 2008) | 1 line Implement /me for outgoing IRC messages ........ r999 | geofft | 2008-03-31 09:58:56 -0400 (Mon, 31 Mar 2008) | 1 line I think this fixes the resizing bug. ........ r1000 | nelhage | 2008-03-31 11:29:29 -0400 (Mon, 31 Mar 2008) | 2 lines Make `svkversion' handle exported trees better ........ r1001 | nelhage | 2008-03-31 11:52:39 -0400 (Mon, 31 Mar 2008) | 2 lines Add a makefile rule to support emacs flymake-mode ........ r1015 | nelhage | 2008-04-04 14:58:45 -0400 (Fri, 04 Apr 2008) | 3 lines Bind the combinations the iPhone sends for arrow keys [probably other terminals, too] ........ r1018 | shadow | 2008-04-08 13:57:49 -0400 (Tue, 08 Apr 2008) | 2 lines avoid null pointer dereference if msg is NULL (or a 0 length is claimed) ........ r1019 | nelhage | 2008-04-09 18:08:26 -0400 (Wed, 09 Apr 2008) | 2 lines Fix some stupid typos ........ r1020 | chmrr | 2008-04-09 18:16:02 -0400 (Wed, 09 Apr 2008) | 3 lines r29300@kohr-ah: chmrr | 2008-04-09 18:14:37 -0400 * It's apparently a popular typo ........ r1022 | asedeno | 2008-04-16 17:05:36 -0400 (Wed, 16 Apr 2008) | 2 lines Jabber reply bugfix. Reported by several people, resolved by arolfe. ........ r1023 | nelhage | 2008-04-28 23:33:03 -0400 (Mon, 28 Apr 2008) | 3 lines Initialized merge tracking via "svnmerge" with revisions "735" from svn+ssh://lunatique.mit.edu/mit/barnowl/src/svn/branches/barnowl_unicode ........ r1024 | nelhage | 2008-04-28 23:33:12 -0400 (Mon, 28 Apr 2008) | 3 lines Initialized merge tracking via "svnmerge" with revisions "735" from svn+ssh://lunatique.mit.edu/mit/barnowl/src/svn/branches/barnowl_sqlite ........ r1025 | nelhage | 2008-04-29 01:21:12 -0400 (Tue, 29 Apr 2008) | 2 lines Clone owl_perl from the sqlite branch because I'm about to make use of it. ........ r1026 | nelhage | 2008-04-29 01:21:13 -0400 (Tue, 29 Apr 2008) | 4 lines Initial step of moving styles from the current mishmash of different options to a unified object interface. No backwards-compatibility support yet. ........ r1027 | nelhage | 2008-04-29 01:21:15 -0400 (Tue, 29 Apr 2008) | 2 lines Implement back-compat with old-style owl::format_msg() styles ........ r1028 | nelhage | 2008-04-29 01:21:16 -0400 (Tue, 29 Apr 2008) | 2 lines Remove nearly all references to the "basic" style. ........ r1029 | nelhage | 2008-04-29 01:21:17 -0400 (Tue, 29 Apr 2008) | 2 lines Support the `style' command again. Note that it is deprecated. ........ r1031 | nelhage | 2008-04-30 13:29:45 -0400 (Wed, 30 Apr 2008) | 2 lines Refactor default style code somewhat to be more easily extensible ........
  • Property mode set to 100644
File size: 26.4 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 default filters */
277  /* the personal filter will need to change again when AIM chat's are
278   *  included.  Also, there should be an %aimme% */
279  owl_function_debugmsg("startup: creating default filters");
280  f=owl_malloc(sizeof(owl_filter));
281  owl_filter_init_fromstring(f, "personal", "isprivate ^true$ and ( not type ^zephyr$"
282                             " or ( class ^message and"
283                             " ( instance ^personal$ or instance ^urgent$ ) ) )");
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, "wordwrap", "not ( type ^admin$ or type ^zephyr$ ) ");
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, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )");
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, "ping", "opcode ^ping$");
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, "auto", "opcode ^auto$");
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, "login", "not login ^none$");
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, "reply-lockout", "class ^noc or class ^mail$");
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, "out", "direction ^out$");
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, "aim", "type ^aim$");
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, "zephyr", "type ^zephyr$");
320  owl_list_append_element(owl_global_get_filterlist(&g), f);
321
322  f=owl_malloc(sizeof(owl_filter));
323  owl_filter_init_fromstring(f, "none", "false");
324  owl_list_append_element(owl_global_get_filterlist(&g), f);
325
326  f=owl_malloc(sizeof(owl_filter));
327  owl_filter_init_fromstring(f, "all", "true");
328  owl_list_append_element(owl_global_get_filterlist(&g), f);
329
330  /* set the current view */
331  owl_function_debugmsg("startup: setting the current view");
332  owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default"));
333
334  /* AIM init */
335  owl_function_debugmsg("startup: doing AIM initialization");
336  owl_aim_init();
337
338  /* execute the startup function in the configfile */
339  owl_function_debugmsg("startup: executing perl startup, if applicable");
340  perlout = owl_perlconfig_execute("BarnOwl::Hooks::_startup();");
341  if (perlout) owl_free(perlout);
342
343  /* hold on to the window names for convenience */
344  msgwin=owl_global_get_curs_msgwin(&g);
345  recwin=owl_global_get_curs_recwin(&g);
346  sepwin=owl_global_get_curs_sepwin(&g);
347  typwin=owl_global_get_curs_typwin(&g);
348  tw=owl_global_get_typwin(&g);
349
350  /* welcome message */
351  owl_function_debugmsg("startup: creating splash message");
352  strcpy(startupmsg, "-----------------------------------------------------------------------\n");
353  sprintf(buff,      "Welcome to barnowl version %s.  Press 'h' for on-line help.            \n", OWL_VERSION_STRING);
354  strcat(startupmsg, buff);
355  strcat(startupmsg, "                                                                       \n");
356  strcat(startupmsg, "This is a development build of barnowl. If you are using this          \n");
357  strcat(startupmsg, "build regularly, please add yourself to barnowl-users@mit.edu          \n");
358  strcat(startupmsg, "                                                                 ^ ^   \n");
359  strcat(startupmsg, "                                                                 OvO   \n");
360  strcat(startupmsg, "Please report any bugs to dirty-owl-hackers@mit.edu             (   )  \n");
361  strcat(startupmsg, "-----------------------------------------------------------------m-m---\n");
362  owl_function_adminmsg("", startupmsg);
363  sepbar(NULL);
364
365  /* process the startup file */
366  owl_function_debugmsg("startup: processing startup file");
367  owl_function_source(NULL);
368
369  wrefresh(sepwin);
370
371  /* load zephyr subs */
372  if (initialsubs) {
373    int ret2;
374    owl_function_debugmsg("startup: loading initial zephyr subs");
375
376    /* load default subscriptions */
377    ret=owl_zephyr_loaddefaultsubs();
378   
379    /* load subscriptions from subs file */
380    ret2=owl_zephyr_loadsubs(NULL, 0);
381
382    if (ret || ret2) {
383      owl_function_adminmsg("", "Error loading zephyr subscriptions");
384    } else if (ret2!=-1) {
385      owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
386    }
387
388    /* load login subscriptions */
389    if (owl_global_is_loginsubs(&g)) {
390      owl_function_debugmsg("startup: loading login subs");
391      owl_function_loadloginsubs(NULL);
392    }
393  }
394
395  /* First buddy check to sync the list without notifications */
396  owl_function_debugmsg("startup: doing initial zephyr buddy check");
397  /* owl_function_zephyr_buddy_check(0); */
398
399  /* set the startup and default style, based on userclue and presence of a
400   * formatting function */
401  owl_function_debugmsg("startup: setting startup and default style");
402  if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) {
403    /* the style was set by the user: leave it alone */
404  } else {
405    owl_global_set_default_style(&g, "default");
406  }
407
408  /* zlog in if we need to */
409  if (owl_global_is_startuplogin(&g)) {
410    owl_function_debugmsg("startup: doing zlog in");
411    owl_zephyr_zlog_in();
412  }
413
414  owl_function_debugmsg("startup: set style for the view: %s", owl_global_get_default_style(&g));
415  s = owl_global_get_style_by_name(&g, owl_global_get_default_style(&g));
416  if(s)
417      owl_view_set_style(owl_global_get_current_view(&g), s);
418  else
419      owl_function_error("No such style: %s", owl_global_get_default_style(&g));
420
421  owl_function_debugmsg("startup: setting context interactive");
422  owl_context_set_interactive(owl_global_get_context(&g));
423
424  nexttimediff=10;
425  nexttime=time(NULL);
426
427#ifdef HAVE_LIBZEPHYR
428  /* Check for any zephyrs that have come in while we've done init. */
429  owl_zephyr_process_events();
430#endif
431 
432  owl_function_debugmsg("startup: entering main loop");
433  /* main loop */
434  while (1) {
435
436    /* if a resize has been scheduled, deal with it */
437    owl_global_resize(&g, 0, 0);
438
439    /* these are here in case a resize changes the windows */
440    msgwin=owl_global_get_curs_msgwin(&g);
441    recwin=owl_global_get_curs_recwin(&g);
442    sepwin=owl_global_get_curs_sepwin(&g);
443    typwin=owl_global_get_curs_typwin(&g);
444
445    followlast=owl_global_should_followlast(&g);
446   
447    owl_perlconfig_mainloop();
448
449    /* little hack */
450    now=time(NULL);
451    today=localtime(&now);
452    if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) {
453      if (time(NULL)>nexttime) {
454        if (nexttimediff==1) {
455          nexttimediff=10;
456        } else {
457          nexttimediff=1;
458        }
459        nexttime+=nexttimediff;
460        owl_hack_animate();
461      }
462    }
463
464    /* Grab incoming messages. */
465    newmsgs=0;
466    while(owl_global_messagequeue_pending(&g)) {
467
468      m = owl_global_messagequeue_popmsg(&g);
469
470      if(owl_process_message(m))
471        newmsgs = 1;
472    }
473
474    /* is it time to check zbuddies? */
475    if (owl_global_is_pseudologins(&g)) {
476      if (owl_timer_is_expired(owl_global_get_zephyr_buddycheck_timer(&g))) {
477        owl_function_debugmsg("Doing zephyr buddy check");
478        owl_function_zephyr_buddy_check(1);
479        owl_timer_reset(owl_global_get_zephyr_buddycheck_timer(&g));
480      }
481    }
482
483    /* dispatch any muxevents */
484    owl_muxevents_dispatch(owl_global_get_muxevents(&g), 0);
485
486    /* follow the last message if we're supposed to */
487    if (newmsgs && followlast) {
488      owl_function_lastmsg_noredisplay();
489    }
490
491    /* do the newmsgproc thing */
492    if (newmsgs) {
493      owl_function_do_newmsgproc();
494    }
495   
496    /* redisplay if necessary */
497    /* this should be optimized to not run if the new messages won't be displayed */
498    if (newmsgs) {
499      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
500      sepbar(NULL);
501      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
502        owl_popwin_refresh(owl_global_get_popwin(&g));
503        /* TODO: this is a broken kludge */
504        if (owl_global_get_viewwin(&g)) {
505          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
506        }
507      }
508      owl_global_set_needrefresh(&g);
509    }
510
511    /* if a popwin just came up, refresh it */
512    pw=owl_global_get_popwin(&g);
513    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
514      owl_popwin_refresh(pw);
515      owl_popwin_no_needs_first_refresh(pw);
516      owl_global_set_needrefresh(&g);
517      /* TODO: this is a broken kludge */
518      if (owl_global_get_viewwin(&g)) {
519        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
520      }
521    }
522
523    /* update the terminal if we need to */
524    if (owl_global_is_needrefresh(&g)) {
525      /* leave the cursor in the appropriate window */
526      if (owl_global_is_typwin_active(&g)) {
527        owl_function_set_cursor(typwin);
528      } else {
529        owl_function_set_cursor(sepwin);
530      }
531      doupdate();
532      owl_global_set_noneedrefresh(&g);
533    }
534
535    /* select on FDs we know about. */
536    owl_select();
537
538    /* Log any error signals */
539    {
540      siginfo_t si;
541      int signum;
542      if ((signum = owl_global_get_errsignal_and_clear(&g, &si)) > 0) {
543        owl_function_error("Got unexpected signal: %d %s  (code: %d band: %d  errno: %d)", 
544                           signum, signum==SIGPIPE?"SIGPIPE":"SIG????",
545                           si.si_code, si.si_band, si.si_errno);
546      }
547    }
548
549  }
550}
551
552/*
553 * Process a new message passed to us on the message queue from some
554 * protocol. This includes adding it to the message list, updating the
555 * view and scrolling if appropriate, logging it, and so on.
556 *
557 * Either a pointer is kept to the message internally, or it is freed
558 * if unneeded. The caller no longer ``owns'' the message's memory.
559 *
560 * Returns 1 if the message was added to the message list, and 0 if it
561 * was ignored due to user settings or otherwise.
562 */
563int owl_process_message(owl_message *m) {
564  owl_filter *f;
565  /* if this message it on the puntlist, nuke it and continue */
566  if (owl_global_message_is_puntable(&g, m)) {
567    owl_message_free(m);
568    return 0;
569  }
570
571  /*  login or logout that should be ignored? */
572  if (owl_global_is_ignorelogins(&g)
573      && owl_message_is_loginout(m)) {
574    owl_message_free(m);
575    return 0;
576  }
577
578  if (!owl_global_is_displayoutgoing(&g)
579      && owl_message_is_direction_out(m)) {
580    owl_message_free(m);
581    return 0;
582  }
583
584  /* add it to the global list */
585  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
586  /* add it to any necessary views; right now there's only the current view */
587  owl_view_consider_message(owl_global_get_current_view(&g), m);
588
589  if(owl_message_is_direction_in(m)) {
590    /* let perl know about it*/
591    owl_perlconfig_getmsg(m, NULL);
592
593    /* do we need to autoreply? */
594    if (owl_global_is_zaway(&g) && !owl_message_get_attribute_value(m, "isauto")) {
595      if (owl_message_is_type_zephyr(m)) {
596        owl_zephyr_zaway(m);
597      } else if (owl_message_is_type_aim(m)) {
598        if (owl_message_is_private(m)) {
599          owl_function_send_aimawymsg(owl_message_get_sender(m), owl_global_get_zaway_msg(&g));
600        }
601      }
602    }
603
604    /* ring the bell if it's a personal */
605    if (!strcmp(owl_global_get_personalbell(&g), "on")) {
606      if (!owl_message_is_loginout(m) &&
607          !owl_message_is_mail(m) &&
608          owl_message_is_personal(m)) {
609        owl_function_beep();
610      }
611    } else if (!strcmp(owl_global_get_personalbell(&g), "off")) {
612      /* do nothing */
613    } else {
614      f=owl_global_get_filter(&g, owl_global_get_personalbell(&g));
615      if (f && owl_filter_message_match(f, m)) {
616        owl_function_beep();
617      }
618    }
619
620    /* if it matches the alert filter, do the alert action */
621    f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g));
622    if (f && owl_filter_message_match(f, m)) {
623      owl_function_command(owl_global_get_alert_action(&g));
624    }
625
626    /* if it's a zephyr login or logout, update the zbuddylist */
627    if (owl_message_is_type_zephyr(m) && owl_message_is_loginout(m)) {
628      if (owl_message_is_login(m)) {
629        owl_zbuddylist_adduser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
630      } else if (owl_message_is_logout(m)) {
631        owl_zbuddylist_deluser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
632      } else {
633        owl_function_error("Internal error: received login notice that is neither login nor logout");
634      }
635    }
636
637    /* check for burning ears message */
638    /* this is an unsupported feature */
639    if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) {
640      char *buff;
641      buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m));
642      owl_function_adminmsg(buff, "");
643      owl_free(buff);
644      owl_function_beep();
645    }
646  }
647
648  /* log the message if we need to */
649  owl_log_message(m);
650
651  return 1;
652}
653
654void owl_process_aim()
655{
656  if (owl_global_is_doaimevents(&g)) {
657    owl_aim_process_events();
658   
659    if (owl_global_is_aimloggedin(&g)) {
660      if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
661        /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
662        owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
663      }
664    }
665  }
666}
667
668void owl_process_input()
669{
670  int ret;
671  owl_input j;
672  owl_popwin *pw;
673  owl_editwin *tw;
674  WINDOW *typwin;
675
676  typwin = owl_global_get_curs_typwin(&g);
677  while (1) {
678    j.ch = wgetch(typwin);
679    if (j.ch == ERR) return;
680   
681    owl_global_set_lastinputtime(&g, time(NULL));
682    pw=owl_global_get_popwin(&g);
683    tw=owl_global_get_typwin(&g);
684
685    j.uch = '\0';
686    if (j.ch >= KEY_MIN && j.ch <= KEY_MAX) {
687      /* This is a curses control character. */
688    }
689    else if (j.ch > 0x7f && j.ch < 0xfe) {
690      /* Pull in a full utf-8 character. */
691      int bytes, i;
692      char utf8buf[7];
693      memset(utf8buf, '\0', 7);
694     
695      utf8buf[0] = j.ch;
696     
697      if ((j.ch & 0xc0) && (~j.ch & 0x20)) bytes = 2;
698      else if ((j.ch & 0xe0) && (~j.ch & 0x10)) bytes = 3;
699      else if ((j.ch & 0xf0) && (~j.ch & 0x08)) bytes = 4;
700      else if ((j.ch & 0xf8) && (~j.ch & 0x04)) bytes = 5;
701      else if ((j.ch & 0xfc) && (~j.ch & 0x02)) bytes = 6;
702      else bytes = 1;
703     
704      for (i = 1; i < bytes; i++) {
705        int tmp =  wgetch(typwin);
706        /* If what we got was not a byte, or not a continuation byte */
707        if (tmp > 0xff || !(tmp & 0x80 && ~tmp & 0x40)) {
708          /* ill-formed UTF-8 code unit subsequence, put back the
709             char we just got. */
710          ungetch(tmp);
711          j.ch = ERR;
712          break;
713        }
714        utf8buf[i] = tmp;
715      }
716     
717      if (j.ch != ERR) {
718        if (g_utf8_validate(utf8buf, -1, NULL)) {
719          j.uch = g_utf8_get_char(utf8buf);
720        }
721        else {
722          j.ch = ERR;
723        }
724      }
725    }
726    else if (j.ch <= 0x7f) {
727      j.uch = j.ch;
728    }
729   
730    owl_global_set_lastinputtime(&g, time(NULL));
731    /* find and activate the current keymap.
732     * TODO: this should really get fixed by activating
733     * keymaps as we switch between windows...
734     */
735    if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
736      owl_context_set_popless(owl_global_get_context(&g), 
737                              owl_global_get_viewwin(&g));
738      owl_function_activate_keymap("popless");
739    } else if (owl_global_is_typwin_active(&g) 
740               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
741      /*
742        owl_context_set_editline(owl_global_get_context(&g), tw);
743        owl_function_activate_keymap("editline");
744      */
745    } else if (owl_global_is_typwin_active(&g) 
746               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
747      owl_context_set_editmulti(owl_global_get_context(&g), tw);
748      owl_function_activate_keymap("editmulti");
749    } else {
750      owl_context_set_recv(owl_global_get_context(&g));
751      owl_function_activate_keymap("recv");
752    }
753    /* now actually handle the keypress */
754    ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
755    if (ret!=0 && ret!=1) {
756      owl_function_makemsg("Unable to handle keypress");
757    }
758  }
759}
760
761void sig_handler(int sig, siginfo_t *si, void *data)
762{
763  if (sig==SIGWINCH) {
764    /* we can't inturrupt a malloc here, so it just sets a flag
765     * schedulding a resize for later
766     */
767    owl_function_resize();
768  } else if (sig==SIGPIPE || sig==SIGCHLD) {
769    /* Set a flag and some info that we got the sigpipe
770     * so we can record that we got it and why... */
771    owl_global_set_errsignal(&g, sig, si);
772  } else if (sig==SIGTERM || sig==SIGHUP) {
773    owl_function_quit();
774  }
775}
776
777void usage()
778{
779  fprintf(stderr, "Barnowl version %s\n", OWL_VERSION_STRING);
780  fprintf(stderr, "Usage: barnowl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-s <confdir>] [-t <ttyname>]\n");
781  fprintf(stderr, "  -n      don't load zephyr subscriptions\n");
782  fprintf(stderr, "  -d      enable debugging\n");
783  fprintf(stderr, "  -D      enable debugging and delete previous debug file\n");
784  fprintf(stderr, "  -v      print the Barnowl version number and exit\n");
785  fprintf(stderr, "  -h      print this help message\n");
786  fprintf(stderr, "  -c      specify an alternate config file\n");
787  fprintf(stderr, "  -s      specify an alternate config dir (default ~/.owl)\n");
788  fprintf(stderr, "  -t      set the tty name\n");
789}
790
791#if OWL_STDERR_REDIR
792
793/* Replaces stderr with a pipe so that we can read from it.
794 * Returns the fd of the pipe from which stderr can be read. */
795int stderr_replace(void)
796{
797  int pipefds[2];
798  if (0 != pipe(pipefds)) {
799    perror("pipe");
800    owl_function_debugmsg("stderr_replace: pipe FAILED\n");
801    return -1;
802  }
803    owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]);
804  if (-1 == dup2(pipefds[1], 2 /*stderr*/)) {
805    owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno));
806    perror("dup2");
807    return -1;
808  }
809  return pipefds[0];
810}
811
812/* Sends stderr (read from rfd) messages to the error console */
813void stderr_redirect_handler(int handle, int rfd, int eventmask, void *data) 
814{
815  int navail, bread;
816  char *buf;
817  /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/
818  if (rfd<0) return;
819  if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) {
820    return;
821  }
822  /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/
823  if (navail<=0) return;
824  if (navail>256) { navail = 256; }
825  buf = owl_malloc(navail+1);
826  bread = read(rfd, buf, navail);
827  if (buf[navail-1] != '\0') {
828    buf[navail] = '\0';
829  }
830  owl_function_error("Err: %s", buf);
831  owl_free(buf);
832}
833
834#endif /* OWL_STDERR_REDIR */
Note: See TracBrowser for help on using the repository browser.