source: owl.c @ 09489b89

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