source: owl.c @ 1077753b

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 1077753b was 1077753b, checked in by James M. Kretchmar <kretch@mit.edu>, 20 years ago
Admin message for being logged out of AIM
  • Property mode set to 100644
File size: 19.0 KB
Line 
1/*
2 * Copyright 2001 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that the above copyright notice and this
7 * permission notice appear in all copies and in supporting
8 * documentation.  No representation is made about the suitability of
9 * this software for any purpose.  It is provided "as is" without
10 * express or implied warranty.
11 */
12
13#include <stdio.h>
14#include <unistd.h>
15#include <stdlib.h>
16#include <string.h>
17#include <signal.h>
18#include <time.h>
19#include <sys/param.h>
20#include <sys/types.h>
21#include <sys/stat.h>
22#include "owl.h"
23
24#if OWL_STDERR_REDIR
25#ifdef HAVE_SYS_IOCTL_H
26#include <sys/ioctl.h>
27#endif
28#ifdef HAVE_SYS_FILIO_H
29#include <sys/filio.h>
30#endif
31int stderr_replace(void);
32void stderr_redirect(int rfd);
33#endif
34
35static const char fileIdent[] = "$Id$";
36
37owl_global g;
38
39int main(int argc, char **argv, char **env) {
40  WINDOW *recwin, *sepwin, *typwin, *msgwin;
41  owl_editwin *tw;
42  owl_popwin *pw;
43  int j, ret, initialsubs, debug, argcsave, followlast;
44  int newmsgs, zpendcount, nexttimediff;
45  struct sigaction sigact;
46  char *configfile, *tty, *perlout, *perlerr, **argvsave, buff[LINE], startupmsg[LINE];
47  owl_filter *f;
48  owl_style *s;
49  time_t nexttime, now;
50  struct tm *today;
51  char *dir;
52#ifdef HAVE_LIBZEPHYR
53  ZNotice_t notice;
54#endif
55#if OWL_STDERR_REDIR
56  int newstderr;
57#endif
58
59  argcsave=argc;
60  argvsave=argv;
61  configfile=NULL;
62  tty=NULL;
63  debug=0;
64  initialsubs=1;
65  if (argc>0) {
66    argv++;
67    argc--;
68  }
69  while (argc>0) {
70    if (!strcmp(argv[0], "-n")) {
71      initialsubs=0;
72      argv++;
73      argc--;
74    } else if (!strcmp(argv[0], "-c")) {
75      if (argc<2) {
76        fprintf(stderr, "Too few arguments to -c\n");
77        usage();
78        exit(1);
79      }
80      configfile=argv[1];
81      argv+=2;
82      argc-=2;
83    } else if (!strcmp(argv[0], "-t")) {
84      if (argc<2) {
85        fprintf(stderr, "Too few arguments to -t\n");
86        usage();
87        exit(1);
88      }
89      tty=argv[1];
90      argv+=2;
91      argc-=2;
92    } else if (!strcmp(argv[0], "-d")) {
93      debug=1;
94      argv++;
95      argc--;
96    } else if (!strcmp(argv[0], "-v")) {
97      printf("This is owl version %s\n", OWL_VERSION_STRING);
98      exit(0);
99    } else {
100      fprintf(stderr, "Uknown argument\n");
101      usage();       
102      exit(1);
103    }
104  }
105
106#ifdef HAVE_LIBZEPHYR
107  /* zephyr init */
108  ret=owl_zephyr_initialize();
109  if (ret) {
110    exit(1);
111  }
112#endif
113 
114  /* signal handler */
115  sigact.sa_handler=sig_handler;
116  sigemptyset(&sigact.sa_mask);
117  sigact.sa_flags=0;
118  sigaction(SIGWINCH, &sigact, NULL);
119  sigaction(SIGALRM, &sigact, NULL);
120
121  /* screen init */
122  sprintf(buff, "TERMINFO=%s", TERMINFO);
123  putenv(buff);
124  initscr();
125  start_color();
126#ifdef HAVE_USE_DEFAULT_COLORS
127  use_default_colors();
128#endif
129  raw();
130  noecho();
131
132  /* define simple color pairs */
133  if (has_colors() && COLOR_PAIRS>=8) {
134    init_pair(OWL_COLOR_BLACK,   COLOR_BLACK,   -1);
135    init_pair(OWL_COLOR_RED,     COLOR_RED,     -1);
136    init_pair(OWL_COLOR_GREEN,   COLOR_GREEN,   -1);
137    init_pair(OWL_COLOR_YELLOW,  COLOR_YELLOW,  -1);
138    init_pair(OWL_COLOR_BLUE,    COLOR_BLUE,    -1);
139    init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, -1);
140    init_pair(OWL_COLOR_CYAN,    COLOR_CYAN,    -1);
141    init_pair(OWL_COLOR_WHITE,   COLOR_WHITE,   -1);
142  }
143
144  /* owl global init */
145  owl_global_init(&g);
146  if (debug) owl_global_set_debug_on(&g);
147  owl_global_set_startupargs(&g, argcsave, argvsave);
148#ifdef HAVE_LIBZEPHYR
149  owl_global_set_havezephyr(&g);
150#endif
151  owl_global_set_haveaim(&g);
152
153#if OWL_STDERR_REDIR
154  /* Do this only after we've started curses up... */ 
155  newstderr = stderr_replace();
156#endif   
157
158 
159  /* create the owl directory, in case it does not exist */
160  dir=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_CONFIG_DIR);
161  mkdir(dir, S_IRWXU);
162  owl_free(dir);
163
164  /* set the tty, either from the command line, or by figuring it out */
165  if (tty) {
166    owl_global_set_tty(&g, tty);
167  } else {
168    owl_global_set_tty(&g, owl_util_get_default_tty());
169  }
170
171  /* setup the built-in styles */
172  s=owl_malloc(sizeof(owl_style));
173  owl_style_create_internal(s, "default", &owl_stylefunc_default,
174                            "Default message formatting");
175  owl_global_add_style(&g, s);
176
177  s=owl_malloc(sizeof(owl_style));
178  owl_style_create_internal(s, "basic", &owl_stylefunc_basic,
179                            "Basic message formatting.");
180  owl_global_add_style(&g, s);
181#if 0
182  s=owl_malloc(sizeof(owl_style));
183  owl_style_create_internal(s, "vt", &owl_stylefunc_vt,
184                            "VT message formatting.");
185  owl_global_add_style(&g, s);
186#endif
187  s=owl_malloc(sizeof(owl_style));
188  owl_style_create_internal(s, "oneline", &owl_stylefunc_oneline,
189                            "Formats for one-line-per-message");
190  owl_global_add_style(&g, s);
191
192  /* setup the default filters */
193  /* the personal filter will need to change again when AIM chat's are
194   *  included.  Also, there should be an %aimme% */
195  f=owl_malloc(sizeof(owl_filter));
196  owl_filter_init_fromstring(f, "personal", "( type ^zephyr$ "
197                             "and class ^message$ and instance ^personal$ "
198                             "and ( recipient ^%me%$ or sender ^%me%$ ) ) "
199                             "or ( type ^aim$ and login ^none$ )");
200  owl_list_append_element(owl_global_get_filterlist(&g), f);
201
202  f=owl_malloc(sizeof(owl_filter));
203  owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )");
204  owl_list_append_element(owl_global_get_filterlist(&g), f);
205
206  f=owl_malloc(sizeof(owl_filter));
207  owl_filter_init_fromstring(f, "ping", "opcode ^ping$");
208  owl_list_append_element(owl_global_get_filterlist(&g), f);
209
210  f=owl_malloc(sizeof(owl_filter));
211  owl_filter_init_fromstring(f, "auto", "opcode ^auto$");
212  owl_list_append_element(owl_global_get_filterlist(&g), f);
213
214  f=owl_malloc(sizeof(owl_filter));
215  owl_filter_init_fromstring(f, "login", "not login ^none$");
216  owl_list_append_element(owl_global_get_filterlist(&g), f);
217
218  f=owl_malloc(sizeof(owl_filter));
219  owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$");
220  owl_list_append_element(owl_global_get_filterlist(&g), f);
221
222  f=owl_malloc(sizeof(owl_filter));
223  owl_filter_init_fromstring(f, "out", "direction ^out$");
224  owl_list_append_element(owl_global_get_filterlist(&g), f);
225
226  f=owl_malloc(sizeof(owl_filter));
227  owl_filter_init_fromstring(f, "aim", "type ^aim$");
228  owl_list_append_element(owl_global_get_filterlist(&g), f);
229
230  f=owl_malloc(sizeof(owl_filter));
231  owl_filter_init_fromstring(f, "zephyr", "type ^zephyr$");
232  owl_list_append_element(owl_global_get_filterlist(&g), f);
233
234  f=owl_malloc(sizeof(owl_filter));
235  owl_filter_init_fromstring(f, "none", "false");
236  owl_list_append_element(owl_global_get_filterlist(&g), f);
237
238  f=owl_malloc(sizeof(owl_filter));
239  owl_filter_init_fromstring(f, "all", "true");
240  owl_list_append_element(owl_global_get_filterlist(&g), f);
241
242  /* set the current view */
243  owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default"));
244
245  /* AIM init */
246  owl_aim_init();
247
248  /* process the startup file */
249  owl_function_source(NULL);
250
251  /* read the config file */
252  owl_context_set_readconfig(owl_global_get_context(&g));
253  perlerr=owl_perlconfig_readconfig(configfile);
254  if (perlerr) {
255    endwin();
256    fprintf(stderr, "\nError parsing configfile: %s\n", perlerr);
257    exit(1);
258  }
259
260  /* if the config defines a formatting function, add 'perl' as a style */
261  if (owl_global_is_config_format(&g)) {
262    owl_function_debugmsg("Found perl formatting");
263    s=owl_malloc(sizeof(owl_style));
264    owl_style_create_perl(s, "perl", "owl::_format_msg_legacy_wrap",
265                          "User-defined perl style that calls owl::format_msg"
266                          "with legacy global variable support");
267    owl_global_add_style(&g, s);
268    owl_global_set_default_style(&g, "perl");
269  }
270
271  /* execute the startup function in the configfile */
272  perlout = owl_perlconfig_execute("owl::startup();");
273  if (perlout) owl_free(perlout);
274  owl_function_debugmsg("-- Owl Startup --");
275 
276  /* hold on to the window names for convenience */
277  msgwin=owl_global_get_curs_msgwin(&g);
278  recwin=owl_global_get_curs_recwin(&g);
279  sepwin=owl_global_get_curs_sepwin(&g);
280  typwin=owl_global_get_curs_typwin(&g);
281  tw=owl_global_get_typwin(&g);
282
283  wrefresh(sepwin);
284
285  /* load zephyr subs */
286  if (initialsubs) {
287    /* load normal subscriptions */
288    ret=owl_zephyr_loadsubs(NULL);
289    if (ret!=-1) {
290      owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
291    }
292
293    /* load login subscriptions */
294    if (owl_global_is_loginsubs(&g)) {
295      owl_function_loadloginsubs(NULL);
296    }
297  }
298
299  /* set the startup and default style, based on userclue and presence of a
300   * formatting function */
301  if (0 != strcmp(owl_global_get_default_style(&g), "__unspecified__")) {
302    /* the style was set by the user: leave it alone */
303  } else if (owl_global_is_config_format(&g)) {
304    owl_global_set_default_style(&g, "perl");
305  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
306    owl_global_set_default_style(&g, "default");
307  } else {
308    owl_global_set_default_style(&g, "basic");
309  }
310
311  /* zlog in if we need to */
312  if (owl_global_is_startuplogin(&g)) {
313    owl_zephyr_zlog_in();
314  }
315
316  owl_view_set_style(owl_global_get_current_view(&g), 
317                     owl_global_get_style_by_name(&g, owl_global_get_default_style(&g)));   
318
319  /* welcome message */
320  strcpy(startupmsg, "-------------------------------------------------------------------------\n");
321  sprintf(buff,      "Welcome to Owl version %s.  Press 'h' for on-line help. \n", OWL_VERSION_STRING);
322  strcat(startupmsg, buff);
323  strcat(startupmsg, "                                                                         \n");
324  strcat(startupmsg, "If you would like to receive release announcements about owl you can join \n");
325  strcat(startupmsg, "the owl-users@mit.edu mailing list.  MIT users can add themselves,       \n");
326  strcat(startupmsg, "otherwise send a request to owner-owl-users@mit.edu.               ^ ^   \n");
327  strcat(startupmsg, "                                                                   OvO   \n");
328  strcat(startupmsg, "Please report any bugs or suggestions to bug-owl@mit.edu          (   )  \n");
329  strcat(startupmsg, "-------------------------------------------------------------------m-m---\n");
330  owl_function_adminmsg("", startupmsg);
331  sepbar(NULL);
332 
333  owl_context_set_interactive(owl_global_get_context(&g));
334
335  nexttimediff=10;
336  nexttime=time(NULL);
337
338  /* main loop */
339  while (1) {
340
341    /* if a resize has been scheduled, deal with it */
342    owl_global_resize(&g, 0, 0);
343
344    /* these are here in case a resize changes the windows */
345    msgwin=owl_global_get_curs_msgwin(&g);
346    recwin=owl_global_get_curs_recwin(&g);
347    sepwin=owl_global_get_curs_sepwin(&g);
348    typwin=owl_global_get_curs_typwin(&g);
349
350    followlast=owl_global_should_followlast(&g);
351   
352    /* Do AIM stuff */
353    if (owl_global_is_doaimevents(&g)) {
354      owl_aim_process_events();
355
356      if (owl_global_is_aimloggedin(&g)) {
357        if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
358          /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
359          owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
360        }
361      }
362    }
363
364    /* little hack */
365    now=time(NULL);
366    today=localtime(&now);
367    if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) {
368      if (time(NULL)>nexttime) {
369        if (nexttimediff==1) {
370          nexttimediff=10;
371        } else {
372          nexttimediff=1;
373        }
374        nexttime+=nexttimediff;
375        owl_hack_animate();
376      }
377    }
378
379    /* Grab incoming messages. */
380    newmsgs=0;
381    zpendcount=0;
382    while(owl_zephyr_zpending() || owl_global_messagequeue_pending(&g)) {
383#ifdef HAVE_LIBZEPHYR
384      struct sockaddr_in from;
385#endif
386      owl_message *m=NULL;
387      owl_filter *f;
388
389      /* grab the new message, stick it in 'm' */
390      if (owl_zephyr_zpending()) {
391#ifdef HAVE_LIBZEPHYR
392        /* grab a zephyr notice, but if we've done 20 without stopping,
393           take a break to process keystrokes etc. */
394        if (zpendcount>20) break;
395        ZReceiveNotice(&notice, &from);
396        zpendcount++;
397       
398        /* is this an ack from a zephyr we sent? */
399        if (owl_zephyr_notice_is_ack(&notice)) {
400          owl_zephyr_handle_ack(&notice);
401          continue;
402        }
403       
404        /* if it's a ping and we're not viewing pings then skip it */
405        if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
406          continue;
407        }
408
409        /* create the new message */
410        m=owl_malloc(sizeof(owl_message));
411        owl_message_create_from_znotice(m, &notice);
412#endif
413      } else if (owl_global_messagequeue_pending(&g)) {
414        m=owl_global_messageuque_popmsg(&g);
415      }
416     
417      /* if this message it on the puntlist, nuke it and continue */
418      if (owl_global_message_is_puntable(&g, m)) {
419        owl_message_free(m);
420        continue;
421      }
422
423      /* otherwise add it to the global list */
424      owl_messagelist_append_element(owl_global_get_msglist(&g), m);
425      newmsgs=1;
426
427      /* let the config know the new message has been received */
428      owl_perlconfig_getmsg(m, 0, NULL);
429
430      /* add it to any necessary views; right now there's only the current view */
431      owl_view_consider_message(owl_global_get_current_view(&g), m);
432
433      /* do we need to autoreply? */
434      if (owl_global_is_zaway(&g)) {
435        if (owl_message_is_type_zephyr(m)) {
436          owl_zephyr_zaway(m);
437        } else if (owl_message_is_type_aim(m)) {
438          /* can't do this, there's no mechanism not to start a loop */
439          /* owl_aim_send_im(owl_message_get_sender(m), owl_global_get_zaway_msg(&g)); */
440        }
441      }
442
443      /* ring the bell if it's a personal */
444      if (owl_global_is_personalbell(&g) &&
445          !owl_message_is_loginout(m) &&
446          !owl_message_is_mail(m) &&
447          owl_message_is_private(m)) {
448        owl_function_beep();
449      }
450
451      /* if it matches the alert filter, do the alert action */
452      f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g));
453      if (f && owl_filter_message_match(f, m)) {
454        owl_function_command(owl_global_get_alert_action(&g));
455      }
456
457      /* check for burning ears message */
458      /* this is an unsupported feature */
459      if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) {
460        char *buff;
461        buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m));
462        owl_function_adminmsg(buff, "");
463        owl_free(buff);
464        owl_function_beep();
465      }
466
467      /* log the message if we need to */
468      if (owl_global_is_logging(&g) || owl_global_is_classlogging(&g)) {
469        owl_log_incoming(m);
470      }
471    }
472
473    /* follow the last message if we're supposed to */
474    if (newmsgs && followlast) {
475      owl_function_lastmsg_noredisplay();
476    }
477
478    /* do the newmsgproc thing */
479    if (newmsgs) {
480      owl_function_do_newmsgproc();
481    }
482   
483    /* redisplay if necessary */
484    /* this should be optimized to not run if the new messages won't be displayed */
485    if (newmsgs) {
486      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
487      sepbar(NULL);
488      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
489        owl_popwin_refresh(owl_global_get_popwin(&g));
490        /* TODO: this is a broken kludge */
491        if (owl_global_get_viewwin(&g)) {
492          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
493        }
494      }
495      owl_global_set_needrefresh(&g);
496    }
497
498    /* if a popwin just came up, refresh it */
499    pw=owl_global_get_popwin(&g);
500    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
501      owl_popwin_refresh(pw);
502      owl_popwin_no_needs_first_refresh(pw);
503      owl_global_set_needrefresh(&g);
504      /* TODO: this is a broken kludge */
505      if (owl_global_get_viewwin(&g)) {
506        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
507      }
508    }
509
510    /* update the terminal if we need to */
511    if (owl_global_is_needrefresh(&g)) {
512      /* leave the cursor in the appropriate window */
513      if (owl_global_is_typwin_active(&g)) {
514        owl_function_set_cursor(typwin);
515      } else {
516        owl_function_set_cursor(sepwin);
517      }
518      doupdate();
519      owl_global_set_noneedrefresh(&g);
520    }
521
522    /* Handle all keypresses.  If no key has been pressed, sleep for a
523     * little bit, but otherwise do not.  This lets input be grabbed
524     * as quickly as possbile */
525    j=wgetch(typwin);
526    if (j==ERR) {
527      usleep(10);
528      continue;
529    }
530    /* find and activate the current keymap.
531     * TODO: this should really get fixed by activating
532     * keymaps as we switch between windows...
533     */
534    if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
535      owl_context_set_popless(owl_global_get_context(&g), 
536                              owl_global_get_viewwin(&g));
537      owl_function_activate_keymap("popless");
538    } else if (owl_global_is_typwin_active(&g) 
539               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
540      /*
541      owl_context_set_editline(owl_global_get_context(&g), tw);
542      owl_function_activate_keymap("editline");
543      */
544    } else if (owl_global_is_typwin_active(&g) 
545               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
546      owl_context_set_editmulti(owl_global_get_context(&g), tw);
547      owl_function_activate_keymap("editmulti");
548    } else {
549      owl_context_set_recv(owl_global_get_context(&g));
550      owl_function_activate_keymap("recv");
551    }
552    /* now actually handle the keypress */
553    ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
554    if (ret!=0 && ret!=1) {
555      owl_function_makemsg("Unable to handle keypress");
556    }
557
558#if OWL_STDERR_REDIR
559    stderr_redirect(newstderr);
560#endif   
561
562  }
563}
564
565void sig_handler(int sig) {
566  if (sig==SIGWINCH) {
567    /* we can't inturrupt a malloc here, so it just sets a flag
568     * schedulding a resize for later
569     */
570    owl_function_resize();
571  }
572}
573
574void usage() {
575  fprintf(stderr, "Owl version %s\n", OWL_VERSION_STRING);
576  fprintf(stderr, "Usage: owl [-n] [-d] [-v] [-h] [-c <configfile>] [-t <ttyname>]\n");
577  fprintf(stderr, "  -n      don't load zephyr subscriptions\n");
578  fprintf(stderr, "  -d      enable debugging\n");
579  fprintf(stderr, "  -v      print the Owl version number and exit\n");
580  fprintf(stderr, "  -h      print this help message\n");
581  fprintf(stderr, "  -c      specify an alternate config file\n");
582  fprintf(stderr, "  -t      set the tty name\n");
583}
584
585
586
587#if OWL_STDERR_REDIR
588
589/* Replaces stderr with a pipe so that we can read from it.
590 * Returns the fd of the pipe from which stderr can be read. */
591int stderr_replace(void) {
592  int pipefds[2];
593  if (0 != pipe(pipefds)) {
594    perror("pipe");
595    owl_function_debugmsg("stderr_replace: pipe FAILED\n");
596    return -1;
597  }
598    owl_function_debugmsg("stderr_replace: pipe: %d,%d\n", pipefds[0], pipefds[1]);
599  if (-1 == dup2(pipefds[1], 2 /*stderr*/)) {
600    owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)\n", strerror(errno));
601    perror("dup2");
602    return -1;
603  }
604  return pipefds[0];
605}
606
607/* Sends stderr (read from rfd) messages to a file */
608void stderr_redirect(int rfd) {
609  int navail, bread;
610  char *buf;
611  /*owl_function_debugmsg("stderr_redirect: called with rfd=%d\n", rfd);*/
612  if (rfd<0) return;
613  if (-1 == ioctl(rfd, FIONREAD, (void*)&navail)) {
614    return;
615  }
616  /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/
617  if (navail<=0) return;
618  if (navail>256) { navail = 256; }
619  buf = owl_malloc(navail+1);
620  bread = read(rfd, buf, navail);
621  if (buf[navail-1] != '\0') {
622    buf[navail] = '\0';
623  }
624  owl_function_error("Err: %s", buf);
625  owl_free(buf);
626}
627
628#endif /* OWL_STDERR_REDIR */
Note: See TracBrowser for help on using the repository browser.