source: owl.c @ f933403

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