source: owl.c @ dab82f29

owl
Last change on this file since dab82f29 was dab82f29, checked in by James M. Kretchmar <kretch@mit.edu>, 15 years ago
Don't crash on mobile device AIM messages [BZ 20] Don't crash when sending to someone not on AIM buddy list [BZ 94] Fix overflow vulerability in zwrite.c and zcrypt.c Add stack protector and -D_FORTIFY_SOURCE=2 where possible Fix hang [barnowl 4c46dfdebc294ca24fef59924f01688b8ee07dee] Fix segfault on missing subs [barnowl 99dabeed7de6d19acb50f1e73aa0bfe5f9469c02] Fix zcrypt bufffer size bug [barnowl 559076cd86edc3a8317819814dd5877b8bc7c3cb] Fix double free [barnowl e97c4a306ae2c9d2147d83da29fee59918198e70] Memory leak fix [barnowl c0a90c2c7ab97b9465c9873757faf312faa0021e] Memory leak fix [barnowl 95caa16b2e9ba10770d87d0955198c14c2d0e16a] Memory leak fix [barnowl 1716fed8c2650e46892cf6571555eac937266c6e] Add getstyle command [barnowl 216c73421653e3ef0e957aa9a9804e208a77c80e] Binary search for msgid [barnowl 0c8ab5eadbb6ecc97a120c91b9a824b33538c764] File-handle leak [barnowl e78397d6ac5da0de31a4e269c0ba7f3d691857a3] Fix delay in jump from top to bottom [barnowl 801b7ac63b962640debbcfd422cb9a60da5fea31] Load subs in chunks [barnowl 93e883d60051b80bf6d35391f9d76fd7dfd198e3] Load subs in chunks [barnowl f6050ee9b0a171a5031f84409eb181062afacd18] Better zsig logging [barnowl d857b667a5a9b108b1a2a26b4a5513bef2b53f80] free() -> owlfree() [barnowl d524c838ac7c115628424b8ac171c3489622ea3a] Escape AIM users in smartfilters [barnowl af9b92e3e7ccb7de276a94b5c7e5861b24e71eff] Better regex escape chars [barnowl 80e54a7631f091be8c9762adb9746bad38104738] Deal with quotes in smart filters [barnowl 4099cf83702763fa8d1efc4f1582a605431bdb77] Deal with 0 length zephyr fields [barnowl 128171aaf7cefa91a4bb1eada93a19d8fd0c355c] Deal with 0 length zephyr fields [barnowl 50e29e35c64d64e223d378d350a7bc4f038d78f5] Deal with 0 length zephyr fields [barnowl 804ab8af8b6d00bcd7e2402df892db8fbd61a3ec] Leave curmsg on screen after resize [barnowl c0f9e3009bc03e80a44de64cd5f2b4033290236e] Rip out brower stuff [barnowl 8e5935d11c699a7ce5a3e6e9a47799564c696d6a] Rip out browser stuff [barnowl 4f15e8e9ceada0d4b2cc969ebf43b0a1fb3709ea] No passwords in command history [barnowl 6e400cc71aa59e041dce677aadf50dc1f25228e2] Format NOC mssages [barnowl a1bb1980e4bca23b8329cc0e7c0bd5027055ea0a] Expand ~ in loadsubs [barnowl 27d8d835dc6d58c08fae10e75aae306c49215143] Expand ~ in source [barnowl 10d67d57cb29221f63a43a30643c697fc7b38911] Only use resizeterm() if it's available Debian backports Change license to GPLv3
  • Property mode set to 100644
File size: 23.8 KB
RevLine 
[dab82f29]1/* Copyright (c) 2002,2003,2004,2009 James M. Kretchmar
[7d4fbcd]2 *
[dab82f29]3 * This file is part of Owl.
4 *
5 * Owl is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * Owl is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Owl.  If not, see <http://www.gnu.org/licenses/>.
17 *
18 * ---------------------------------------------------------------
[5ca5f8e]19 *
[dab82f29]20 * As of Owl version 2.1.12 there are patches contributed by
21 * developers of the the branched BarnOwl project, Copyright (c)
22 * 2006-2008 The BarnOwl Developers. All rights reserved.
[7d4fbcd]23 */
24
25#include <stdio.h>
26#include <unistd.h>
27#include <stdlib.h>
28#include <string.h>
29#include <signal.h>
[5145235]30#include <time.h>
[7d4fbcd]31#include <sys/param.h>
[700c712]32#include <sys/types.h>
[8232149]33#include <termios.h>
[38cf544c]34#include <sys/stat.h>
[7d4fbcd]35#include "owl.h"
36
[2a2bb60]37#if OWL_STDERR_REDIR
[e8b95f8]38#ifdef HAVE_SYS_IOCTL_H
[2a2bb60]39#include <sys/ioctl.h>
[e8b95f8]40#endif
41#ifdef HAVE_SYS_FILIO_H
42#include <sys/filio.h>
43#endif
[2a2bb60]44int stderr_replace(void);
45#endif
46
[dab82f29]47#define STDIN 0
48
[1aee7d9]49static const char fileIdent[] = "$Id$";
50
[b2b0773]51owl_global g;
52
[de22c3d]53int main(int argc, char **argv, char **env)
54{
[7d4fbcd]55  WINDOW *recwin, *sepwin, *typwin, *msgwin;
56  owl_editwin *tw;
57  owl_popwin *pw;
[dab82f29]58  int ret, initialsubs, debug, argcsave, followlast;
59  int newmsgs, nexttimediff;
[7d4fbcd]60  struct sigaction sigact;
[f1e629d]61  char *configfile, *tty, *perlout, *perlerr, **argvsave, buff[LINE], startupmsg[LINE];
[7d4fbcd]62  owl_filter *f;
[bd3f232]63  owl_style *s;
[a15a84f]64  time_t nexttime, now;
65  struct tm *today;
[38cf544c]66  char *dir;
[8232149]67  struct termios tio;
[2a2bb60]68#if OWL_STDERR_REDIR
69  int newstderr;
70#endif
[7d4fbcd]71
72  argcsave=argc;
73  argvsave=argv;
74  configfile=NULL;
75  tty=NULL;
76  debug=0;
[bd3f232]77  initialsubs=1;
[7d4fbcd]78  if (argc>0) {
79    argv++;
80    argc--;
81  }
82  while (argc>0) {
83    if (!strcmp(argv[0], "-n")) {
84      initialsubs=0;
85      argv++;
86      argc--;
87    } else if (!strcmp(argv[0], "-c")) {
88      if (argc<2) {
89        fprintf(stderr, "Too few arguments to -c\n");
90        usage();
91        exit(1);
92      }
93      configfile=argv[1];
94      argv+=2;
95      argc-=2;
96    } else if (!strcmp(argv[0], "-t")) {
97      if (argc<2) {
98        fprintf(stderr, "Too few arguments to -t\n");
99        usage();
100        exit(1);
101      }
102      tty=argv[1];
103      argv+=2;
104      argc-=2;
105    } else if (!strcmp(argv[0], "-d")) {
106      debug=1;
107      argv++;
108      argc--;
[a68f05d]109    } else if (!strcmp(argv[0], "-D")) {
110      debug=1;
111      unlink(OWL_DEBUG_FILE);
112      argv++;
113      argc--;
[7d4fbcd]114    } else if (!strcmp(argv[0], "-v")) {
115      printf("This is owl version %s\n", OWL_VERSION_STRING);
116      exit(0);
117    } else {
118      fprintf(stderr, "Uknown argument\n");
119      usage();       
120      exit(1);
121    }
122  }
123
[176d3443]124  owl_function_debugmsg("startup: Finished parsing arguments");
125
[be0a79f]126#ifdef HAVE_LIBZEPHYR
[7d4fbcd]127  /* zephyr init */
[be0a79f]128  ret=owl_zephyr_initialize();
129  if (ret) {
[7d4fbcd]130    exit(1);
131  }
[be0a79f]132#endif
133 
[7d4fbcd]134  /* signal handler */
[c9e72d1]135  /*sigact.sa_handler=sig_handler;*/
136  sigact.sa_sigaction=sig_handler;
[7d4fbcd]137  sigemptyset(&sigact.sa_mask);
[c9e72d1]138  sigact.sa_flags=SA_SIGINFO;
[7d4fbcd]139  sigaction(SIGWINCH, &sigact, NULL);
140  sigaction(SIGALRM, &sigact, NULL);
[c9e72d1]141  sigaction(SIGPIPE, &sigact, NULL);
[fe1f605]142  sigaction(SIGTERM, &sigact, NULL);
143  sigaction(SIGHUP, &sigact, NULL);
[7d4fbcd]144
[8232149]145  /* save initial terminal settings */
146  tcgetattr(0, owl_global_get_startup_tio(&g));
147
148  /* turn ISTRIP off */
149  tcgetattr(0, &tio);
150  tio.c_iflag &= ~ISTRIP;
151  tcsetattr(0, TCSAFLUSH, &tio);
152
[7d4fbcd]153  /* screen init */
[b20de4f]154  if (!getenv("TERMINFO")) {
155    sprintf(buff, "TERMINFO=%s", TERMINFO);
156    putenv(buff);
157    owl_function_debugmsg("startup: setting TERMINFO to %s", TERMINFO);
158  } else {
159    owl_function_debugmsg("startup: leaving TERMINFO as %s from envrionment", getenv("TERMINFO"));
160  }
[7d4fbcd]161  initscr();
162  start_color();
[ac70242]163#ifdef HAVE_USE_DEFAULT_COLORS
[7d4fbcd]164  use_default_colors();
[ac70242]165#endif
[7d4fbcd]166  raw();
167  noecho();
168
169  /* define simple color pairs */
170  if (has_colors() && COLOR_PAIRS>=8) {
171    init_pair(OWL_COLOR_BLACK,   COLOR_BLACK,   -1);
172    init_pair(OWL_COLOR_RED,     COLOR_RED,     -1);
173    init_pair(OWL_COLOR_GREEN,   COLOR_GREEN,   -1);
174    init_pair(OWL_COLOR_YELLOW,  COLOR_YELLOW,  -1);
175    init_pair(OWL_COLOR_BLUE,    COLOR_BLUE,    -1);
176    init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, -1);
177    init_pair(OWL_COLOR_CYAN,    COLOR_CYAN,    -1);
178    init_pair(OWL_COLOR_WHITE,   COLOR_WHITE,   -1);
179  }
[38cf544c]180
[bd3f232]181  /* owl global init */
[7d4fbcd]182  owl_global_init(&g);
183  if (debug) owl_global_set_debug_on(&g);
[b20de4f]184  owl_function_debugmsg("startup: first available debugging message");
[7d4fbcd]185  owl_global_set_startupargs(&g, argcsave, argvsave);
[dab82f29]186  owl_global_set_haveaim(&g);
187
188  /* prepare stdin dispatch */
189  {
190    owl_dispatch *d = owl_malloc(sizeof(owl_dispatch));
191    owl_function_debugmsg("startup: creating input selector");
192    d->fd = STDIN;
193    d->cfunc = &owl_process_input;
194    d->destroy = NULL;
195    owl_select_add_dispatch(d);
196  }
197
[09489b89]198#ifdef HAVE_LIBZEPHYR
199  owl_global_set_havezephyr(&g);
[dab82f29]200  if (!ret) {
201    owl_dispatch *d = owl_malloc(sizeof(owl_dispatch));
202    owl_function_debugmsg("startup: creating zephyr selector");
203    d->fd = ZGetFD();
204    d->cfunc = &owl_zephyr_process_events;
205    d->destroy = NULL;
206    owl_select_add_dispatch(d);
207    owl_global_set_havezephyr(&g);
208  }
[09489b89]209#endif
[2a2bb60]210
211#if OWL_STDERR_REDIR
[a0a5179]212  /* Do this only after we've started curses up... */
[dab82f29]213  {
214    owl_dispatch *d = owl_malloc(sizeof(owl_dispatch));
215    owl_function_debugmsg("startup: doing stderr redirection");
216    d->fd = stderr_replace();
217    d->cfunc = stderr_redirect_handler;
218    d->destroy = NULL;
219    owl_function_debugmsg("testing: 1");
220    owl_select_add_dispatch(d);
221    owl_function_debugmsg("testing: 2");
222  }
223
[2a2bb60]224#endif   
225
[38cf544c]226  /* create the owl directory, in case it does not exist */
[a0a5179]227  owl_function_debugmsg("startup: creating owl directory, if not present");
[38cf544c]228  dir=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_CONFIG_DIR);
229  mkdir(dir, S_IRWXU);
230  owl_free(dir);
[5145235]231
[ced25d1]232  /* set the tty, either from the command line, or by figuring it out */
[a0a5179]233  owl_function_debugmsg("startup: setting tty name");
[61e79a9]234  if (tty) {
235    owl_global_set_tty(&g, tty);
236  } else {
237    owl_global_set_tty(&g, owl_util_get_default_tty());
238  }
[7d4fbcd]239
[bd3f232]240  /* setup the built-in styles */
[a0a5179]241  owl_function_debugmsg("startup: creating built-in styles");
[bd3f232]242  s=owl_malloc(sizeof(owl_style));
[176d3443]243  owl_style_create_internal(s, "default", &owl_stylefunc_default, "Default message formatting");
[bd3f232]244  owl_global_add_style(&g, s);
245
246  s=owl_malloc(sizeof(owl_style));
[176d3443]247  owl_style_create_internal(s, "basic", &owl_stylefunc_basic, "Basic message formatting.");
[bd3f232]248  owl_global_add_style(&g, s);
[ec6ff52]249#if 0
250  s=owl_malloc(sizeof(owl_style));
[176d3443]251  owl_style_create_internal(s, "vt", &owl_stylefunc_vt, "VT message formatting.");
[ec6ff52]252  owl_global_add_style(&g, s);
253#endif
[8b32593]254  s=owl_malloc(sizeof(owl_style));
[176d3443]255  owl_style_create_internal(s, "oneline", &owl_stylefunc_oneline, "Formats for one-line-per-message");
[8b32593]256  owl_global_add_style(&g, s);
257
[7d4fbcd]258  /* setup the default filters */
[3abf28b]259  /* the personal filter will need to change again when AIM chat's are
260   *  included.  Also, there should be an %aimme% */
[a0a5179]261  owl_function_debugmsg("startup: creating default filters");
[bd3f232]262  f=owl_malloc(sizeof(owl_filter));
[3abf28b]263  owl_filter_init_fromstring(f, "personal", "( type ^zephyr$ "
264                             "and class ^message$ and instance ^personal$ "
265                             "and ( recipient ^%me%$ or sender ^%me%$ ) ) "
[3723f31]266                             "or ( type ^aim$ and login ^none$ )");
[7d4fbcd]267  owl_list_append_element(owl_global_get_filterlist(&g), f);
268
[bd3f232]269  f=owl_malloc(sizeof(owl_filter));
[0c502e9]270  owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )");
[7d4fbcd]271  owl_list_append_element(owl_global_get_filterlist(&g), f);
272
[bd3f232]273  f=owl_malloc(sizeof(owl_filter));
[0236842]274  owl_filter_init_fromstring(f, "ping", "opcode ^ping$");
275  owl_list_append_element(owl_global_get_filterlist(&g), f);
276
[bd3f232]277  f=owl_malloc(sizeof(owl_filter));
[0236842]278  owl_filter_init_fromstring(f, "auto", "opcode ^auto$");
279  owl_list_append_element(owl_global_get_filterlist(&g), f);
280
[bd3f232]281  f=owl_malloc(sizeof(owl_filter));
[0c502e9]282  owl_filter_init_fromstring(f, "login", "not login ^none$");
[0236842]283  owl_list_append_element(owl_global_get_filterlist(&g), f);
284
[bd3f232]285  f=owl_malloc(sizeof(owl_filter));
[7d4fbcd]286  owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$");
287  owl_list_append_element(owl_global_get_filterlist(&g), f);
288
[bd3f232]289  f=owl_malloc(sizeof(owl_filter));
[5a6e6b9]290  owl_filter_init_fromstring(f, "out", "direction ^out$");
291  owl_list_append_element(owl_global_get_filterlist(&g), f);
292
[bd3f232]293  f=owl_malloc(sizeof(owl_filter));
[31e48a3]294  owl_filter_init_fromstring(f, "aim", "type ^aim$");
295  owl_list_append_element(owl_global_get_filterlist(&g), f);
296
[bd3f232]297  f=owl_malloc(sizeof(owl_filter));
[31e48a3]298  owl_filter_init_fromstring(f, "zephyr", "type ^zephyr$");
299  owl_list_append_element(owl_global_get_filterlist(&g), f);
300
[bd3f232]301  f=owl_malloc(sizeof(owl_filter));
[89426ab]302  owl_filter_init_fromstring(f, "none", "false");
303  owl_list_append_element(owl_global_get_filterlist(&g), f);
304
[bd3f232]305  f=owl_malloc(sizeof(owl_filter));
[89426ab]306  owl_filter_init_fromstring(f, "all", "true");
[7d4fbcd]307  owl_list_append_element(owl_global_get_filterlist(&g), f);
308
309  /* set the current view */
[a0a5179]310  owl_function_debugmsg("startup: setting the current view");
[c3ab155]311  owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default"));
[7d4fbcd]312
[96f8e5b]313  /* AIM init */
[a0a5179]314  owl_function_debugmsg("startup: doing AIM initialization");
[96f8e5b]315  owl_aim_init();
316
[38cf544c]317  /* process the startup file */
[a0a5179]318  owl_function_debugmsg("startup: processing startup file");
[2404c3a]319  owl_function_source(NULL);
[38cf544c]320
[7d4fbcd]321  /* read the config file */
[a0a5179]322  owl_function_debugmsg("startup: processing config file");
[38cf544c]323  owl_context_set_readconfig(owl_global_get_context(&g));
[f1e629d]324  perlerr=owl_perlconfig_readconfig(configfile);
325  if (perlerr) {
[7d4fbcd]326    endwin();
[4e0f545]327    owl_function_error("Error parsing configfile: %s\n", perlerr);
328    fprintf(stderr, "\nError parsing configfile: %s\n", perlerr);
[c3acb0b]329    fflush(stderr);
[4e0f545]330    printf("\nError parsing configfile: %s\n", perlerr);
[c3acb0b]331    fflush(stdout);
[7d4fbcd]332    exit(1);
333  }
[ced25d1]334
[e075479]335  /* if the config defines a formatting function, add 'perl' as a style */
[bd3f232]336  if (owl_global_is_config_format(&g)) {
337    owl_function_debugmsg("Found perl formatting");
338    s=owl_malloc(sizeof(owl_style));
[f1e629d]339    owl_style_create_perl(s, "perl", "owl::_format_msg_legacy_wrap",
340                          "User-defined perl style that calls owl::format_msg"
341                          "with legacy global variable support");
[bd3f232]342    owl_global_add_style(&g, s);
[e075479]343    owl_global_set_default_style(&g, "perl");
[bd3f232]344  }
345
[ced25d1]346  /* execute the startup function in the configfile */
[a0a5179]347  owl_function_debugmsg("startup: executing perl startup, if applicable");
[f1e629d]348  perlout = owl_perlconfig_execute("owl::startup();");
[7d4fbcd]349  if (perlout) owl_free(perlout);
350 
351  /* hold on to the window names for convenience */
352  msgwin=owl_global_get_curs_msgwin(&g);
353  recwin=owl_global_get_curs_recwin(&g);
354  sepwin=owl_global_get_curs_sepwin(&g);
355  typwin=owl_global_get_curs_typwin(&g);
356  tw=owl_global_get_typwin(&g);
357
[252a5c2]358  /* welcome message */
[a0a5179]359  owl_function_debugmsg("startup: creating splash message");
[52f3507]360  strcpy(startupmsg, "-----------------------------------------------------------------------\n");
[252a5c2]361  sprintf(buff,      "Welcome to Owl version %s.  Press 'h' for on-line help. \n", OWL_VERSION_STRING);
362  strcat(startupmsg, buff);
[52f3507]363  strcat(startupmsg, "                                                                       \n");
364  strcat(startupmsg, "If you would like to receive release announcements about Owl you can   \n");
[7fad773]365  strcat(startupmsg, "join the owl-users mailing list at http://www.ktools.org/              \n");
[52f3507]366  strcat(startupmsg, "                                                                 ^ ^   \n");
367  strcat(startupmsg, "                                                                 OvO   \n");
[405d5e6]368  strcat(startupmsg, "Please report any bugs or suggestions to bug-owl@ktools.org     (   )  \n");
[52f3507]369  strcat(startupmsg, "-----------------------------------------------------------------m-m---\n");
[252a5c2]370  owl_function_adminmsg("", startupmsg);
371  sepbar(NULL);
372
[7d4fbcd]373  wrefresh(sepwin);
374
375  /* load zephyr subs */
[bd3f232]376  if (initialsubs) {
[4357be8]377    int ret2;
[a0a5179]378    owl_function_debugmsg("startup: loading initial zephyr subs");
[4357be8]379
380    /* load default subscriptions */
381    ret=owl_zephyr_loaddefaultsubs();
[252a5c2]382   
[4357be8]383    /* load subscriptions from subs file */
[95474d7]384    ret2=owl_zephyr_loadsubs(NULL, 0);
[4357be8]385
386    if (ret || ret2) {
387      owl_function_adminmsg("", "Error loading zephyr subscriptions");
388    } else if (ret2!=-1) {
[bd3f232]389      owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
390    }
[7d4fbcd]391
[bd3f232]392    /* load login subscriptions */
393    if (owl_global_is_loginsubs(&g)) {
[a0a5179]394      owl_function_debugmsg("startup: loading login subs");
[bd3f232]395      owl_function_loadloginsubs(NULL);
396    }
[7d4fbcd]397  }
398
[5a95b69]399  /* First buddy check to sync the list without notifications */
400  owl_function_debugmsg("startup: doing initial zephyr buddy check");
[4357be8]401  /* owl_function_zephyr_buddy_check(0); */
[5a95b69]402
[f933403]403  /* set the startup and default style, based on userclue and presence of a
404   * formatting function */
[a0a5179]405  owl_function_debugmsg("startup: setting startup and default style");
[27c3a93]406  if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) {
407    /* the style was set by the user: leave it alone */
408  } else if (owl_global_is_config_format(&g)) {
[f933403]409    owl_global_set_default_style(&g, "perl");
410  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
411    owl_global_set_default_style(&g, "default");
412  } else {
413    owl_global_set_default_style(&g, "basic");
414  }
415
[7d4fbcd]416  /* zlog in if we need to */
417  if (owl_global_is_startuplogin(&g)) {
[a0a5179]418    owl_function_debugmsg("startup: doing zlog in");
[31e48a3]419    owl_zephyr_zlog_in();
[7d4fbcd]420  }
421
[a0a5179]422  owl_function_debugmsg("startup: set style for the view");
[f1e629d]423  owl_view_set_style(owl_global_get_current_view(&g), 
424                     owl_global_get_style_by_name(&g, owl_global_get_default_style(&g)));   
[a0a5179]425
426  owl_function_debugmsg("startup: setting context interactive");
[7d4fbcd]427  owl_context_set_interactive(owl_global_get_context(&g));
428
[a15a84f]429  nexttimediff=10;
[4b464a4]430  nexttime=time(NULL);
431
[a0a5179]432  owl_function_debugmsg("startup: entering main loop");
[7d4fbcd]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);
[5789230]446   
[a352335c]447    /* Do AIM stuff */
448    if (owl_global_is_doaimevents(&g)) {
[5789230]449      owl_aim_process_events();
[de03334]450
[a352335c]451      if (owl_global_is_aimloggedin(&g)) {
452        if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
[f4d0975]453          /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
[a352335c]454          owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
455        }
[de03334]456      }
[5789230]457    }
458
[4b464a4]459    /* little hack */
[a15a84f]460    now=time(NULL);
461    today=localtime(&now);
462    if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) {
[4b464a4]463      if (time(NULL)>nexttime) {
464        if (nexttimediff==1) {
[a15a84f]465          nexttimediff=10;
[4b464a4]466        } else {
467          nexttimediff=1;
468        }
469        nexttime+=nexttimediff;
470        owl_hack_animate();
471      }
472    }
473
[aac889a]474    /* Grab incoming messages. */
[d09e5a1]475    newmsgs=0;
[dab82f29]476    while(owl_global_messagequeue_pending(&g)) {
[e6449bc]477      owl_message *m=NULL;
[ecd5dc5]478      owl_filter *f;
[09489b89]479
[dab82f29]480      m=owl_global_messageuque_popmsg(&g);
[7d4fbcd]481     
[bd3f232]482      /* if this message it on the puntlist, nuke it and continue */
[7d4fbcd]483      if (owl_global_message_is_puntable(&g, m)) {
484        owl_message_free(m);
485        continue;
486      }
487
[280ddc6]488      /*  login or logout that should be ignored? */
489      if (owl_global_is_ignorelogins(&g) && owl_message_is_loginout(m)) {
490        owl_message_free(m);
491        continue;
492      }
493
[7d4fbcd]494      /* otherwise add it to the global list */
495      owl_messagelist_append_element(owl_global_get_msglist(&g), m);
[d09e5a1]496      newmsgs=1;
[7d4fbcd]497
498      /* let the config know the new message has been received */
[f1e629d]499      owl_perlconfig_getmsg(m, 0, NULL);
[7d4fbcd]500
501      /* add it to any necessary views; right now there's only the current view */
502      owl_view_consider_message(owl_global_get_current_view(&g), m);
503
504      /* do we need to autoreply? */
[9854278]505      if (owl_global_is_zaway(&g) && !owl_message_get_attribute_value(m, "isauto")) {
[1077753b]506        if (owl_message_is_type_zephyr(m)) {
507          owl_zephyr_zaway(m);
508        } else if (owl_message_is_type_aim(m)) {
[e9f239b]509          if (owl_message_is_private(m)) {
510            owl_function_send_aimawymsg(owl_message_get_sender(m), owl_global_get_zaway_msg(&g));
511          }
[1077753b]512        }
[7d4fbcd]513      }
514
515      /* ring the bell if it's a personal */
[5d365f6]516      if (!strcmp(owl_global_get_personalbell(&g), "on")) {
517          if (!owl_message_is_loginout(m) &&
518              !owl_message_is_mail(m) &&
519              owl_message_is_private(m)) {
520            owl_function_beep();
521          }
522      } else if (!strcmp(owl_global_get_personalbell(&g), "off")) {
523        /* do nothing */
524      } else {
525        f=owl_global_get_filter(&g, owl_global_get_personalbell(&g));
526        if (f && owl_filter_message_match(f, m)) {
527          owl_function_beep();
528        }
[7d4fbcd]529      }
530
[5d365f6]531
[ecd5dc5]532      /* if it matches the alert filter, do the alert action */
533      f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g));
534      if (f && owl_filter_message_match(f, m)) {
535        owl_function_command(owl_global_get_alert_action(&g));
536      }
537
[5a95b69]538      /* if it's a zephyr login or logout, update the zbuddylist */
539      if (owl_message_is_type_zephyr(m) && owl_message_is_loginout(m)) {
540        if (owl_message_is_login(m)) {
541          owl_zbuddylist_adduser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
542        } else if (owl_message_is_logout(m)) {
543          owl_zbuddylist_deluser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m));
544        } else {
545          owl_function_error("Internal error: received login notice that is neither login nor logout");
546        }
547      }
548
[7d4fbcd]549      /* check for burning ears message */
550      /* this is an unsupported feature */
551      if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) {
[1c6c4d3]552        char *buff;
553        buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m));
[7d4fbcd]554        owl_function_adminmsg(buff, "");
[1c6c4d3]555        owl_free(buff);
[7d4fbcd]556        owl_function_beep();
557      }
558
[d09e5a1]559      /* log the message if we need to */
[15b34fd]560      owl_log_message(m);
[7d4fbcd]561    }
562
[5a95b69]563    /* is it time to check zbuddies? */
564    if (owl_global_is_pseudologins(&g)) {
565      if (owl_timer_is_expired(owl_global_get_zephyr_buddycheck_timer(&g))) {
566        owl_function_debugmsg("Doing zephyr buddy check");
567        owl_function_zephyr_buddy_check(1);
568        owl_timer_reset(owl_global_get_zephyr_buddycheck_timer(&g));
569      }
570    }
571
[7d4fbcd]572    /* follow the last message if we're supposed to */
[d09e5a1]573    if (newmsgs && followlast) {
[7d4fbcd]574      owl_function_lastmsg_noredisplay();
575    }
[700c712]576
[ced25d1]577    /* do the newmsgproc thing */
[d09e5a1]578    if (newmsgs) {
[8f44c6b]579      owl_function_do_newmsgproc();
[700c712]580    }
[7d4fbcd]581   
582    /* redisplay if necessary */
[d09e5a1]583    /* this should be optimized to not run if the new messages won't be displayed */
584    if (newmsgs) {
[7d4fbcd]585      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
586      sepbar(NULL);
587      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
588        owl_popwin_refresh(owl_global_get_popwin(&g));
589        /* TODO: this is a broken kludge */
590        if (owl_global_get_viewwin(&g)) {
591          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
592        }
593      }
594      owl_global_set_needrefresh(&g);
595    }
596
597    /* if a popwin just came up, refresh it */
598    pw=owl_global_get_popwin(&g);
599    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
600      owl_popwin_refresh(pw);
601      owl_popwin_no_needs_first_refresh(pw);
602      owl_global_set_needrefresh(&g);
603      /* TODO: this is a broken kludge */
604      if (owl_global_get_viewwin(&g)) {
605        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
606      }
607    }
608
609    /* update the terminal if we need to */
610    if (owl_global_is_needrefresh(&g)) {
611      /* leave the cursor in the appropriate window */
612      if (owl_global_is_typwin_active(&g)) {
613        owl_function_set_cursor(typwin);
614      } else {
615        owl_function_set_cursor(sepwin);
616      }
617      doupdate();
618      owl_global_set_noneedrefresh(&g);
619    }
620
[dab82f29]621    /* select on FDs we know about. */
622    owl_select();
[2a2bb60]623
[c9e72d1]624    /* Log any error signals */
625    {
626      siginfo_t si;
627      int signum;
628      if ((signum = owl_global_get_errsignal_and_clear(&g, &si)) > 0) {
[81634b6]629        owl_function_error("Got unexpected signal: %d %s  (code: %d band: %d  errno: %d)", 
[afbf668]630                           signum, signum==SIGPIPE?"SIGPIPE":"SIG????",
[81634b6]631                           si.si_code, si.si_band, si.si_errno);
[c9e72d1]632      }
633    }
634
[7d4fbcd]635  }
636}
637
[dab82f29]638void owl_process_input(owl_dispatch *d)
639{
640  int ret, j;
641  owl_popwin *pw;
642  owl_editwin *tw;
643  WINDOW *typwin;
644
645  typwin = owl_global_get_curs_typwin(&g);
646  while (1) {
647    j=wgetch(typwin);
648    if (j==ERR) return;
649
650    pw=owl_global_get_popwin(&g);
651    tw=owl_global_get_typwin(&g);
652   
653    /* find and activate the current keymap.
654     * TODO: this should really get fixed by activating
655     * keymaps as we switch between windows...
656     */
657    if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
658      owl_context_set_popless(owl_global_get_context(&g), 
659                              owl_global_get_viewwin(&g));
660      owl_function_activate_keymap("popless");
661    } else if (owl_global_is_typwin_active(&g) 
662               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
663      /*
664        owl_context_set_editline(owl_global_get_context(&g), tw);
665        owl_function_activate_keymap("editline");
666      */
667    } else if (owl_global_is_typwin_active(&g) 
668               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
669      owl_context_set_editmulti(owl_global_get_context(&g), tw);
670      owl_function_activate_keymap("editmulti");
671    } else {
672      owl_context_set_recv(owl_global_get_context(&g));
673      owl_function_activate_keymap("recv");
674    }
675    /* now actually handle the keypress */
676    ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
677    if (ret!=0 && ret!=1) {
678      owl_function_makemsg("Unable to handle keypress");
679    }
680  }
681}
682
[de22c3d]683void sig_handler(int sig, siginfo_t *si, void *data)
684{
[7d4fbcd]685  if (sig==SIGWINCH) {
[bd3f232]686    /* we can't inturrupt a malloc here, so it just sets a flag
687     * schedulding a resize for later
688     */
[7d4fbcd]689    owl_function_resize();
[afbf668]690  } else if (sig==SIGPIPE || sig==SIGCHLD) {
[c9e72d1]691    /* Set a flag and some info that we got the sigpipe
692     * so we can record that we got it and why... */
693    owl_global_set_errsignal(&g, sig, si);
[fe1f605]694  } else if (sig==SIGTERM || sig==SIGHUP) {
695    owl_function_quit();
[7d4fbcd]696  }
[c9e72d1]697
[7d4fbcd]698}
699
[de22c3d]700void usage()
701{
[e7cc1c3]702  fprintf(stderr, "Owl version %s\n", OWL_VERSION_STRING);
[a68f05d]703  fprintf(stderr, "Usage: owl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-t <ttyname>]\n");
[e7cc1c3]704  fprintf(stderr, "  -n      don't load zephyr subscriptions\n");
705  fprintf(stderr, "  -d      enable debugging\n");
[a68f05d]706  fprintf(stderr, "  -D      enable debugging and delete previous debug file\n");
[e7cc1c3]707  fprintf(stderr, "  -v      print the Owl version number and exit\n");
708  fprintf(stderr, "  -h      print this help message\n");
709  fprintf(stderr, "  -c      specify an alternate config file\n");
710  fprintf(stderr, "  -t      set the tty name\n");
[7d4fbcd]711}
[2a2bb60]712
713#if OWL_STDERR_REDIR
714
715/* Replaces stderr with a pipe so that we can read from it.
716 * Returns the fd of the pipe from which stderr can be read. */
[de22c3d]717int stderr_replace(void)
718{
[2a2bb60]719  int pipefds[2];
720  if (0 != pipe(pipefds)) {
721    perror("pipe");
722    owl_function_debugmsg("stderr_replace: pipe FAILED\n");
723    return -1;
724  }
[dab82f29]725    owl_function_debugmsg("stderr_replace: pipe: %d,%d", pipefds[0], pipefds[1]);
[2a2bb60]726  if (-1 == dup2(pipefds[1], 2 /*stderr*/)) {
[dab82f29]727    owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)", strerror(errno));
[2a2bb60]728    perror("dup2");
729    return -1;
730  }
731  return pipefds[0];
732}
733
[afbf668]734/* Sends stderr (read from rfd) messages to the error console */
735void stderr_redirect_handler(int handle, int rfd, int eventmask, void *data) 
[de22c3d]736{
[2a2bb60]737  int navail, bread;
738  char *buf;
739  /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/
740  if (rfd<0) return;
741  if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) {
742    return;
743  }
744  /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/
745  if (navail<=0) return;
746  if (navail>256) { navail = 256; }
747  buf = owl_malloc(navail+1);
748  bread = read(rfd, buf, navail);
749  if (buf[navail-1] != '\0') {
750    buf[navail] = '\0';
751  }
752  owl_function_error("Err: %s", buf);
753  owl_free(buf);
754}
755
756#endif /* OWL_STDERR_REDIR */
Note: See TracBrowser for help on using the repository browser.