source: owl.c @ 93ee554

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 93ee554 was 93ee554, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 16 years ago
Merged revisions 734-776 via svnmerge from file:///afs/sipb.mit.edu/project/barnowl/src/svn/trunk ........ r738 | nelhage | 2007-07-07 17:42:45 -0400 (Sat, 07 Jul 2007) | 3 lines Make the built-in regression tests output TAP and add a perl wrapper to call it. ........ r739 | nelhage | 2007-07-07 17:43:22 -0400 (Sat, 07 Jul 2007) | 2 lines Forgot these in the last commit; Make all the tests output TAP ........ r740 | nelhage | 2007-07-07 19:46:16 -0400 (Sat, 07 Jul 2007) | 2 lines Clean up an unused var warning. ........ r741 | nelhage | 2007-07-08 15:37:43 -0400 (Sun, 08 Jul 2007) | 2 lines Adding a -s switch to change the location of the config dir (~/.owl) ........ r742 | nelhage | 2007-07-08 15:39:57 -0400 (Sun, 08 Jul 2007) | 2 lines I don't understand why this didn't commit last time. I blame psvn. ........ r743 | nelhage | 2007-07-11 22:37:16 -0400 (Wed, 11 Jul 2007) | 3 lines Don't allow you to go off the end of an empty message list. [fixes: #9] ........ r744 | nelhage | 2007-07-30 18:47:39 -0400 (Mon, 30 Jul 2007) | 2 lines Fix sending to -c message -i personal ........ r745 | chmrr | 2007-07-30 20:21:18 -0400 (Mon, 30 Jul 2007) | 3 lines r20981@zoq-fot-pik: chmrr | 2007-07-30 20:20:44 -0400 * message length 0 means no fields ........ r746 | nelhage | 2007-07-30 20:25:59 -0400 (Mon, 30 Jul 2007) | 4 lines * zero-len message means no fields * Actually use owl_zephyr_get_zsig to get the zsig, which handles 0- and 1- field zephyrs correctly. ........ r747 | nelhage | 2007-07-30 20:29:36 -0400 (Mon, 30 Jul 2007) | 2 lines Making that last patch actually compile. Bad me. ........ r748 | nelhage | 2007-08-01 01:42:53 -0400 (Wed, 01 Aug 2007) | 3 lines owl_message_get_text needs to make sure there's text to return before returning it. fixes: #15 ........ r749 | nelhage | 2007-08-01 01:52:08 -0400 (Wed, 01 Aug 2007) | 5 lines zephyr smartnarrow now uses the ``personal'' filter to decide whether to narrow to user or not, and the zephyr-user filter now checks for `filter personal' instead of <message,personal,*>. closes #2 ........ r750 | nelhage | 2007-08-01 02:27:30 -0400 (Wed, 01 Aug 2007) | 5 lines Changing the default personal filter to <message,personal,*> for zephyr. refs 2 ........ r751 | nelhage | 2007-08-01 22:58:31 -0400 (Wed, 01 Aug 2007) | 2 lines Display opcodes with the default style ........ r752 | nelhage | 2007-08-08 18:01:51 -0400 (Wed, 08 Aug 2007) | 2 lines Applying ctl's variable shuffling patch for better ANSI C-ness. closes #18 ........ r753 | nelhage | 2007-08-11 01:04:07 -0400 (Sat, 11 Aug 2007) | 3 lines Implement :punt and :unpunt to punt arbitrary filters, rather than just z-triplets. closes #6 ........ r754 | nelhage | 2007-08-11 01:18:37 -0400 (Sat, 11 Aug 2007) | 2 lines Show non-personal pings like stock owl does. closes #12 ........ r756 | asedeno | 2007-08-17 12:48:37 -0400 (Fri, 17 Aug 2007) | 8 lines Apply patch from: http://rt.cpan.org/Public/Bug/Display.html?id=17484 Fixing problems with jabber servers keeping the same stream id when negotiating TLS. Thanks to ghudson for tracking this down. ........ r757 | nelhage | 2007-08-17 17:26:44 -0400 (Fri, 17 Aug 2007) | 4 lines When we're narrowing to an instance, properly include un-instances. This fixes narrowing to any instance that starts with ``un-'' ........ r758 | nelhage | 2007-08-27 19:17:20 -0400 (Mon, 27 Aug 2007) | 2 lines Don't read before the start of the string for an instance ........ r759 | nelhage | 2007-09-07 00:13:45 -0400 (Fri, 07 Sep 2007) | 3 lines Adding an explicit -f - to the tar commands for FreeBSD compatibility (reported by ecprice) ........ r760 | ecprice | 2007-09-08 17:33:34 -0400 (Sat, 08 Sep 2007) | 3 lines Fixes for FreeBSD. ........ r761 | nelhage | 2007-09-10 20:00:45 -0400 (Mon, 10 Sep 2007) | 3 lines Use "" instead of `undef' as a default for messages with no reply command to hopefully squelch perl warnings. ........ r762 | nelhage | 2007-09-12 21:37:41 -0400 (Wed, 12 Sep 2007) | 2 lines Do ~-expansion in :loadsubs. closes #26 ........ r763 | nelhage | 2007-09-12 21:54:51 -0400 (Wed, 12 Sep 2007) | 2 lines Validate JIDs passed to jmuc join. closes #25 ........ r764 | nelhage | 2007-09-12 22:46:17 -0400 (Wed, 12 Sep 2007) | 2 lines Show full JIDs for users in non-anonymous JIDs in :jmuc presence. closes #24 ........ r766 | nelhage | 2007-10-02 00:38:49 -0400 (Tue, 02 Oct 2007) | 3 lines Don't crash if we hit `i' on iso-8859-*. This is not the right solution, but at least it doesn't SEGV. ........ r767 | asedeno | 2007-10-10 15:21:13 -0400 (Wed, 10 Oct 2007) | 1 line Fixing a typo pointed out by kchen. ........ r768 | matt | 2007-10-14 17:16:35 -0400 (Sun, 14 Oct 2007) | 1 line added -m flag to aimwrite ........ r769 | austein | 2007-10-14 18:16:44 -0400 (Sun, 14 Oct 2007) | 1 line aimwrite -m displays according to displayoutgoing ........ r770 | asedeno | 2007-12-06 14:38:05 -0500 (Thu, 06 Dec 2007) | 3 lines Making usleep call more reasonable. Responsiveness seems okay on linerva, no-knife, and darkmatter, and reported good on zephyr. ........ r771 | nelhage | 2007-12-10 21:34:46 -0500 (Mon, 10 Dec 2007) | 2 lines Add zip as build-depends ........ r772 | nelhage | 2007-12-10 21:36:25 -0500 (Mon, 10 Dec 2007) | 2 lines We're not ktools ........ r773 | nelhage | 2007-12-23 11:32:02 -0500 (Sun, 23 Dec 2007) | 2 lines bind END in popless windows. closes #41 ........ r774 | chmrr | 2007-12-23 15:16:26 -0500 (Sun, 23 Dec 2007) | 3 lines r1805@utwig: chmrr | 2007-12-23 15:15:33 -0500 * Allow C-r on outgoing messages (useful for CCs) ........ r775 | chmrr | 2007-12-23 15:16:29 -0500 (Sun, 23 Dec 2007) | 3 lines r1806@utwig: chmrr | 2007-12-23 15:15:50 -0500 * Identify ourselves as barnowl in a couple more places ........
  • Property mode set to 100644
File size: 25.2 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
67char * owl_get_datadir() {
68    char * datadir = getenv("BARNOWL_DATA_DIR");
69    if(datadir != NULL)
70        return strchr(datadir, '=') + 1;
71    return DATADIR;
72}
73
74int main(int argc, char **argv, char **env)
75{
76  WINDOW *recwin, *sepwin, *typwin, *msgwin;
77  owl_editwin *tw;
78  owl_popwin *pw;
79  int j, ret, initialsubs, debug, argcsave, followlast;
80  int newmsgs, nexttimediff;
81  struct sigaction sigact;
82  char *configfile, *tty, *perlout, *perlerr, **argvsave, buff[LINE], startupmsg[LINE];
83  char *confdir;
84  owl_filter *f;
85  owl_style *s;
86  time_t nexttime, now;
87  struct tm *today;
88  char *dir;
89  struct termios tio;
90  owl_message *m;
91#if OWL_STDERR_REDIR
92  int newstderr;
93#endif
94
95  argcsave=argc;
96  argvsave=argv;
97  configfile=NULL;
98  confdir = NULL;
99  tty=NULL;
100  debug=0;
101  initialsubs=1;
102
103  setlocale(LC_ALL, "");
104 
105  if (argc>0) {
106    argv++;
107    argc--;
108  }
109  while (argc>0) {
110    if (!strcmp(argv[0], "-n")) {
111      initialsubs=0;
112      argv++;
113      argc--;
114    } else if (!strcmp(argv[0], "-c")) {
115      if (argc<2) {
116        fprintf(stderr, "Too few arguments to -c\n");
117        usage();
118        exit(1);
119      }
120      configfile=argv[1];
121      argv+=2;
122      argc-=2;
123    } else if (!strcmp(argv[0], "-t")) {
124      if (argc<2) {
125        fprintf(stderr, "Too few arguments to -t\n");
126        usage();
127        exit(1);
128      }
129      tty=argv[1];
130      argv+=2;
131      argc-=2;
132    } else if (!strcmp(argv[0], "-s")){
133      if (argc<2) {
134        fprintf(stderr, "Too few arguments to -s\n");
135        usage();
136        exit(1);
137      }
138      confdir = argv[1];
139      argv+=2;
140      argc-=2;
141    } else if (!strcmp(argv[0], "-d")) {
142      debug=1;
143      argv++;
144      argc--;
145    } else if (!strcmp(argv[0], "-D")) {
146      debug=1;
147      unlink(OWL_DEBUG_FILE);
148      argv++;
149      argc--;
150    } else if (!strcmp(argv[0], "-v")) {
151      printf("This is barnowl version %s\n", OWL_VERSION_STRING);
152      exit(0);
153    } else {
154      fprintf(stderr, "Uknown argument\n");
155      usage();       
156      exit(1);
157    }
158  }
159
160  owl_function_debugmsg("startup: Finished parsing arguments");
161
162  /* signal handler */
163  /*sigact.sa_handler=sig_handler;*/
164  sigact.sa_sigaction=sig_handler;
165  sigemptyset(&sigact.sa_mask);
166  sigact.sa_flags=SA_SIGINFO;
167  sigaction(SIGWINCH, &sigact, NULL);
168  sigaction(SIGALRM, &sigact, NULL);
169  sigaction(SIGPIPE, &sigact, NULL);
170  sigaction(SIGTERM, &sigact, NULL);
171  sigaction(SIGHUP, &sigact, NULL);
172
173  /* save initial terminal settings */
174  tcgetattr(0, owl_global_get_startup_tio(&g));
175
176  /* turn ISTRIP off */
177  tcgetattr(0, &tio);
178  tio.c_iflag &= ~ISTRIP;
179  tcsetattr(0, TCSAFLUSH, &tio);
180
181  /* screen init */
182  if (!getenv("TERMINFO")) {
183    owl_function_debugmsg("startup: Not setting TERMINFO");
184  } else {
185    owl_function_debugmsg("startup: leaving TERMINFO as %s from envrionment", getenv("TERMINFO"));
186  }
187  initscr();
188  start_color();
189#ifdef HAVE_USE_DEFAULT_COLORS
190  use_default_colors();
191#endif
192  raw();
193  noecho();
194
195  /* define simple color pairs */
196  if (has_colors() && COLOR_PAIRS>=8) {
197    int bg = COLOR_BLACK;
198#ifdef HAVE_USE_DEFAULT_COLORS
199    bg = -1;
200#endif
201    init_pair(OWL_COLOR_BLACK,   COLOR_BLACK,   bg);
202    init_pair(OWL_COLOR_RED,     COLOR_RED,     bg);
203    init_pair(OWL_COLOR_GREEN,   COLOR_GREEN,   bg);
204    init_pair(OWL_COLOR_YELLOW,  COLOR_YELLOW,  bg);
205    init_pair(OWL_COLOR_BLUE,    COLOR_BLUE,    bg);
206    init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, bg);
207    init_pair(OWL_COLOR_CYAN,    COLOR_CYAN,    bg);
208    init_pair(OWL_COLOR_WHITE,   COLOR_WHITE,   bg);
209  }
210
211  /* owl global init */
212  owl_global_init(&g);
213  if (debug) owl_global_set_debug_on(&g);
214  if (confdir) owl_global_set_confdir(&g, confdir);
215  owl_function_debugmsg("startup: first available debugging message");
216  owl_global_set_startupargs(&g, argcsave, argvsave);
217  owl_global_set_haveaim(&g);
218
219#ifdef HAVE_LIBZEPHYR
220  /* zephyr init */
221  ret=owl_zephyr_initialize();
222  if (!ret)
223      owl_global_set_havezephyr(&g);
224#endif
225
226#if OWL_STDERR_REDIR
227  /* Do this only after we've started curses up... */
228  owl_function_debugmsg("startup: doing stderr redirection");
229  newstderr = stderr_replace();
230  owl_muxevents_add(owl_global_get_muxevents(&g), newstderr, OWL_MUX_READ,
231                    stderr_redirect_handler, NULL);
232#endif   
233
234  /* create the owl directory, in case it does not exist */
235  owl_function_debugmsg("startup: creating owl directory, if not present");
236  dir=owl_global_get_confdir(&g);
237  mkdir(dir, S_IRWXU);
238
239  /* set the tty, either from the command line, or by figuring it out */
240  owl_function_debugmsg("startup: setting tty name");
241  if (tty) {
242    owl_global_set_tty(&g, tty);
243  } else {
244    owl_global_set_tty(&g, owl_util_get_default_tty());
245  }
246
247  /* Initialize perl */
248  owl_function_debugmsg("startup: processing config file");
249  owl_context_set_readconfig(owl_global_get_context(&g));
250  perlerr=owl_perlconfig_initperl(configfile);
251  if (perlerr) {
252    endwin();
253    owl_function_error("Internal perl error: %s\n", perlerr);
254    fprintf(stderr, "Internal perl error: %s\n", perlerr);
255    fflush(stderr);
256    printf("Internal perl error: %s\n", perlerr);
257    fflush(stdout);
258    exit(1);
259  }
260
261  /* setup the built-in styles */
262  owl_function_debugmsg("startup: creating built-in styles");
263
264  s=owl_malloc(sizeof(owl_style));
265  owl_style_create_internal(s, "basic", &owl_stylefunc_basic, "Basic message formatting.");
266  owl_global_add_style(&g, s);
267#if 0
268  s=owl_malloc(sizeof(owl_style));
269  owl_style_create_internal(s, "vt", &owl_stylefunc_vt, "VT message formatting.");
270  owl_global_add_style(&g, s);
271#endif
272  s=owl_malloc(sizeof(owl_style));
273  owl_style_create_internal(s, "oneline", &owl_stylefunc_oneline, "Formats for one-line-per-message");
274  owl_global_add_style(&g, s);
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 if (owl_global_is_config_format(&g)) {
405    owl_global_set_default_style(&g, "perl");
406  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
407    owl_global_set_default_style(&g, "default");
408  } else {
409    owl_global_set_default_style(&g, "basic");
410  }
411
412  /* zlog in if we need to */
413  if (owl_global_is_startuplogin(&g)) {
414    owl_function_debugmsg("startup: doing zlog in");
415    owl_zephyr_zlog_in();
416  }
417
418  owl_function_debugmsg("startup: set style for the view: %s", owl_global_get_default_style(&g));
419  s = owl_global_get_style_by_name(&g, owl_global_get_default_style(&g));
420  if(s)
421      owl_view_set_style(owl_global_get_current_view(&g), s);
422  else
423      owl_function_error("No such style: %s", owl_global_get_default_style(&g));
424
425  owl_function_debugmsg("startup: setting context interactive");
426  owl_context_set_interactive(owl_global_get_context(&g));
427
428  nexttimediff=10;
429  nexttime=time(NULL);
430
431  owl_function_debugmsg("startup: entering main loop");
432  /* main loop */
433  while (1) {
434
435    /* if a resize has been scheduled, deal with it */
436    owl_global_resize(&g, 0, 0);
437
438    /* these are here in case a resize changes the windows */
439    msgwin=owl_global_get_curs_msgwin(&g);
440    recwin=owl_global_get_curs_recwin(&g);
441    sepwin=owl_global_get_curs_sepwin(&g);
442    typwin=owl_global_get_curs_typwin(&g);
443
444    followlast=owl_global_should_followlast(&g);
445   
446    /* Do AIM stuff */
447    if (owl_global_is_doaimevents(&g)) {
448      owl_aim_process_events();
449
450      if (owl_global_is_aimloggedin(&g)) {
451        if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
452          /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
453          owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
454        }
455      }
456    }
457
458    owl_perlconfig_mainloop();
459
460    /* little hack */
461    now=time(NULL);
462    today=localtime(&now);
463    if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) {
464      if (time(NULL)>nexttime) {
465        if (nexttimediff==1) {
466          nexttimediff=10;
467        } else {
468          nexttimediff=1;
469        }
470        nexttime+=nexttimediff;
471        owl_hack_animate();
472      }
473    }
474
475    owl_zephyr_process_events();
476   
477    /* Grab incoming messages. */
478    newmsgs=0;
479    while(owl_global_messagequeue_pending(&g)) {
480
481      m = owl_global_messagequeue_popmsg(&g);
482
483      if(owl_process_message(m))
484        newmsgs = 1;
485    }
486
487    /* is it time to check zbuddies? */
488    if (owl_global_is_pseudologins(&g)) {
489      if (owl_timer_is_expired(owl_global_get_zephyr_buddycheck_timer(&g))) {
490        owl_function_debugmsg("Doing zephyr buddy check");
491        owl_function_zephyr_buddy_check(1);
492        owl_timer_reset(owl_global_get_zephyr_buddycheck_timer(&g));
493      }
494    }
495
496    /* dispatch any muxevents */
497    owl_muxevents_dispatch(owl_global_get_muxevents(&g), 0);
498
499    /* follow the last message if we're supposed to */
500    if (newmsgs && followlast) {
501      owl_function_lastmsg_noredisplay();
502    }
503
504    /* do the newmsgproc thing */
505    if (newmsgs) {
506      owl_function_do_newmsgproc();
507    }
508   
509    /* redisplay if necessary */
510    /* this should be optimized to not run if the new messages won't be displayed */
511    if (newmsgs) {
512      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
513      sepbar(NULL);
514      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
515        owl_popwin_refresh(owl_global_get_popwin(&g));
516        /* TODO: this is a broken kludge */
517        if (owl_global_get_viewwin(&g)) {
518          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
519        }
520      }
521      owl_global_set_needrefresh(&g);
522    }
523
524    /* if a popwin just came up, refresh it */
525    pw=owl_global_get_popwin(&g);
526    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
527      owl_popwin_refresh(pw);
528      owl_popwin_no_needs_first_refresh(pw);
529      owl_global_set_needrefresh(&g);
530      /* TODO: this is a broken kludge */
531      if (owl_global_get_viewwin(&g)) {
532        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
533      }
534    }
535
536    /* update the terminal if we need to */
537    if (owl_global_is_needrefresh(&g)) {
538      /* leave the cursor in the appropriate window */
539      if (owl_global_is_typwin_active(&g)) {
540        owl_function_set_cursor(typwin);
541      } else {
542        owl_function_set_cursor(sepwin);
543      }
544      doupdate();
545      owl_global_set_noneedrefresh(&g);
546    }
547
548    /* Handle all keypresses.  If no key has been pressed, sleep for a
549     * little bit, but otherwise do not.  This lets input be grabbed
550     * as quickly as possbile */
551    j=wgetch(typwin);
552    if (j==ERR) {
553      usleep(10000);
554    } else {
555      owl_global_update_lastinputtime(&g);
556      /* find and activate the current keymap.
557       * TODO: this should really get fixed by activating
558       * keymaps as we switch between windows...
559       */
560      if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
561        owl_context_set_popless(owl_global_get_context(&g), 
562                                owl_global_get_viewwin(&g));
563        owl_function_activate_keymap("popless");
564      } else if (owl_global_is_typwin_active(&g) 
565                 && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
566        /*
567          owl_context_set_editline(owl_global_get_context(&g), tw);
568          owl_function_activate_keymap("editline");
569        */
570      } else if (owl_global_is_typwin_active(&g) 
571                 && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
572        owl_context_set_editmulti(owl_global_get_context(&g), tw);
573        owl_function_activate_keymap("editmulti");
574      } else {
575        owl_context_set_recv(owl_global_get_context(&g));
576        owl_function_activate_keymap("recv");
577      }
578      /* now actually handle the keypress */
579      ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
580      if (ret!=0 && ret!=1) {
581        owl_function_makemsg("Unable to handle keypress");
582      }
583    }
584
585    /* Log any error signals */
586    {
587      siginfo_t si;
588      int signum;
589      if ((signum = owl_global_get_errsignal_and_clear(&g, &si)) > 0) {
590        owl_function_error("Got unexpected signal: %d %s  (code: %d band: %d  errno: %d)", 
591                           signum, signum==SIGPIPE?"SIGPIPE":"SIG????",
592                           si.si_code, si.si_band, si.si_errno);
593      }
594    }
595
596  }
597}
598
599/*
600 * Process a new message passed to us on the message queue from some
601 * protocol. This includes adding it to the message list, updating the
602 * view and scrolling if appropriate, logging it, and so on.
603 *
604 * Either a pointer is kept to the message internally, or it is freed
605 * if unneeded. The caller no longer ``owns'' the message's memory.
606 *
607 * Returns 1 if the message was added to the message list, and 0 if it
608 * was ignored due to user settings or otherwise.
609 */
610int owl_process_message(owl_message *m) {
611  owl_filter *f;
612  /* if this message it on the puntlist, nuke it and continue */
613  if (owl_global_message_is_puntable(&g, m)) {
614    owl_message_free(m);
615    return 0;
616  }
617
618  /*  login or logout that should be ignored? */
619  if (owl_global_is_ignorelogins(&g)
620      && owl_message_is_loginout(m)) {
621    owl_message_free(m);
622    return 0;
623  }
624
625  if (!owl_global_is_displayoutgoing(&g)
626      && owl_message_is_direction_out(m)) {
627    owl_message_free(m);
628    return 0;
629  }
630
631  /* add it to the global list */
632  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
633  /* add it to any necessary views; right now there's only the current view */
634  owl_view_consider_message(owl_global_get_current_view(&g), m);
635
636  if(owl_message_is_direction_in(m)) {
637    /* let perl know about it*/
638    owl_perlconfig_getmsg(m, 0, NULL);
639
640    /* do we need to autoreply? */
641    if (owl_global_is_zaway(&g) && !owl_message_get_attribute_value(m, "isauto")) {
642      if (owl_message_is_type_zephyr(m)) {
643        owl_zephyr_zaway(m);
644      } else if (owl_message_is_type_aim(m)) {
645        if (owl_message_is_private(m)) {
646          owl_function_send_aimawymsg(owl_message_get_sender(m), owl_global_get_zaway_msg(&g));
647        }
648      }
649    }
650
651    /* ring the bell if it's a personal */
652    if (!strcmp(owl_global_get_personalbell(&g), "on")) {
653      if (!owl_message_is_loginout(m) &&
654          !owl_message_is_mail(m) &&
655          owl_message_is_personal(m)) {
656        owl_function_beep();
657      }
658    } else if (!strcmp(owl_global_get_personalbell(&g), "off")) {
659      /* do nothing */
660    } else {
661      f=owl_global_get_filter(&g, owl_global_get_personalbell(&g));
662      if (f && owl_filter_message_match(f, m)) {
663        owl_function_beep();
664      }
665    }
666
667    /* if it matches the alert filter, do the alert action */
668    f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g));
669    if (f && owl_filter_message_match(f, m)) {
670      owl_function_command(owl_global_get_alert_action(&g));
671    }
672
673    /* if it's a zephyr login or logout, update the zbuddylist */
674    if (owl_message_is_type_zephyr(m) && owl_message_is_loginout(m)) {
675      if (owl_message_is_login(m)) {
676        owl_zbuddylist_adduser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
677      } else if (owl_message_is_logout(m)) {
678        owl_zbuddylist_deluser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
679      } else {
680        owl_function_error("Internal error: received login notice that is neither login nor logout");
681      }
682    }
683
684    /* check for burning ears message */
685    /* this is an unsupported feature */
686    if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) {
687      char *buff;
688      buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m));
689      owl_function_adminmsg(buff, "");
690      owl_free(buff);
691      owl_function_beep();
692    }
693  }
694
695  /* log the message if we need to */
696  owl_log_message(m);
697
698  return 1;
699}
700
701void sig_handler(int sig, siginfo_t *si, void *data)
702{
703  if (sig==SIGWINCH) {
704    /* we can't inturrupt a malloc here, so it just sets a flag
705     * schedulding a resize for later
706     */
707    owl_function_resize();
708  } else if (sig==SIGPIPE || sig==SIGCHLD) {
709    /* Set a flag and some info that we got the sigpipe
710     * so we can record that we got it and why... */
711    owl_global_set_errsignal(&g, sig, si);
712  } else if (sig==SIGTERM || sig==SIGHUP) {
713    owl_function_quit();
714  }
715
716}
717
718void usage()
719{
720  fprintf(stderr, "Barnowl version %s\n", OWL_VERSION_STRING);
721  fprintf(stderr, "Usage: barnowl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-s <confdir>] [-t <ttyname>]\n");
722  fprintf(stderr, "  -n      don't load zephyr subscriptions\n");
723  fprintf(stderr, "  -d      enable debugging\n");
724  fprintf(stderr, "  -D      enable debugging and delete previous debug file\n");
725  fprintf(stderr, "  -v      print the Barnowl version number and exit\n");
726  fprintf(stderr, "  -h      print this help message\n");
727  fprintf(stderr, "  -c      specify an alternate config file\n");
728  fprintf(stderr, "  -s      specify an alternate config dir (default ~/.owl)\n");
729  fprintf(stderr, "  -t      set the tty name\n");
730}
731
732#if OWL_STDERR_REDIR
733
734/* Replaces stderr with a pipe so that we can read from it.
735 * Returns the fd of the pipe from which stderr can be read. */
736int stderr_replace(void)
737{
738  int pipefds[2];
739  if (0 != pipe(pipefds)) {
740    perror("pipe");
741    owl_function_debugmsg("stderr_replace: pipe FAILED\n");
742    return -1;
743  }
744    owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]);
745  if (-1 == dup2(pipefds[1], 2 /*stderr*/)) {
746    owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno));
747    perror("dup2");
748    return -1;
749  }
750  return pipefds[0];
751}
752
753/* Sends stderr (read from rfd) messages to the error console */
754void stderr_redirect_handler(int handle, int rfd, int eventmask, void *data) 
755{
756  int navail, bread;
757  char *buf;
758  /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/
759  if (rfd<0) return;
760  if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) {
761    return;
762  }
763  /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/
764  if (navail<=0) return;
765  if (navail>256) { navail = 256; }
766  buf = owl_malloc(navail+1);
767  bread = read(rfd, buf, navail);
768  if (buf[navail-1] != '\0') {
769    buf[navail] = '\0';
770  }
771  owl_function_error("Err: %s", buf);
772  owl_free(buf);
773}
774
775#endif /* OWL_STDERR_REDIR */
Note: See TracBrowser for help on using the repository browser.