source: owl.c @ 47519e1b

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