source: owl.c @ e6449bc

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