source: owl.c @ 5639bf2

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