source: owl.c @ f14a7ee

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since f14a7ee was bd3f232, checked in by James M. Kretchmar <kretch@mit.edu>, 18 years ago
Styles implemented It's still a little buggy ... if a format_msg(); is used in perl admin messages (or maybe just the first admin message) are not formatted.
  • Property mode set to 100644
File size: 15.6 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  /* setup the default filters */
159  /* the personal filter will need to change again when AIM chat's are
160   *  included.  Also, there should be an %aimme% */
161  f=owl_malloc(sizeof(owl_filter));
162  owl_filter_init_fromstring(f, "personal", "( type ^zephyr$ "
163                             "and class ^message$ and instance ^personal$ "
164                             "and ( recipient ^%me%$ or sender ^%me%$ ) ) "
165                             "or ( type ^aim$ )");
166  owl_list_append_element(owl_global_get_filterlist(&g), f);
167
168  f=owl_malloc(sizeof(owl_filter));
169  owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or class ^login$");
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, "ping", "opcode ^ping$");
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, "auto", "opcode ^auto$");
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, "login", "class ^login$");
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, "reply-lockout", "class ^noc or class ^mail$");
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, "out", "direction ^out$");
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, "aim", "type ^aim$");
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, "zephyr", "type ^zephyr$");
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, "none", "false");
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, "all", "true");
206  owl_list_append_element(owl_global_get_filterlist(&g), f);
207
208  /* set the current view */
209  owl_view_create(owl_global_get_current_view(&g), f);
210
211  /* AIM init */
212  owl_aim_init();
213
214  /* process the startup file */
215  owl_function_execstartup();
216
217  /* read the config file */
218  owl_context_set_readconfig(owl_global_get_context(&g));
219  ret=owl_readconfig(configfile);
220  if (ret) {
221    endwin();
222    printf("\nError parsing configfile\n");
223    exit(1);
224  }
225
226  /* if the config defaults a formatting function, add 'perl' as a style */
227  if (owl_global_is_config_format(&g)) {
228    owl_function_debugmsg("Found perl formatting");
229    s=owl_malloc(sizeof(owl_style));
230    owl_style_create_perl(s, "perl", "owl::format_msg");
231    owl_global_add_style(&g, s);
232  }
233
234  /* execute the startup function in the configfile */
235  perlout = owl_config_execute("owl::startup();");
236  if (perlout) owl_free(perlout);
237  owl_function_debugmsg("-- Owl Startup --");
238 
239  /* hold on to the window names for convenience */
240  msgwin=owl_global_get_curs_msgwin(&g);
241  recwin=owl_global_get_curs_recwin(&g);
242  sepwin=owl_global_get_curs_sepwin(&g);
243  typwin=owl_global_get_curs_typwin(&g);
244  tw=owl_global_get_typwin(&g);
245
246  wrefresh(sepwin);
247
248  /* load zephyr subs */
249  if (initialsubs) {
250    /* load normal subscriptions */
251    ret=owl_zephyr_loadsubs(NULL);
252    if (ret!=-1) {
253      owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
254    }
255
256    /* load login subscriptions */
257    if (owl_global_is_loginsubs(&g)) {
258      owl_function_loadloginsubs(NULL);
259    }
260  }
261
262  /* zlog in if we need to */
263  if (owl_global_is_startuplogin(&g)) {
264    owl_zephyr_zlog_in();
265  }
266
267  /* set the default style, based on userclue and presence of a
268   *  formatting function */
269  if (owl_global_is_config_format(&g)) {
270    owl_global_set_style(&g, "perl");
271  } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) {
272    owl_global_set_style(&g, "default");
273  } else {
274    owl_global_set_style(&g, "basic");
275  }
276
277  /* welcome message */
278  strcpy(startupmsg, "-------------------------------------------------------------------------\n");
279  sprintf(buff,      "Welcome to Owl version %s.  Press 'h' for on line help. \n", OWL_VERSION_STRING);
280  strcat(startupmsg, buff);
281  strcat(startupmsg, "                                                                         \n");
282  strcat(startupmsg, "If you would like to receive release announcments about owl you can join \n");
283  strcat(startupmsg, "the owl-users@mit.edu mailing list.  MIT users can add themselves,       \n");
284  strcat(startupmsg, "otherwise send a request to owner-owl-users@mit.edu.               ^ ^   \n");
285  strcat(startupmsg, "                                                                   OvO   \n");
286  strcat(startupmsg, "Please report any bugs or suggestions to bug-owl@mit.edu          (   )  \n");
287  strcat(startupmsg, "-------------------------------------------------------------------m-m---\n");
288  owl_function_adminmsg("", startupmsg);
289  sepbar(NULL);
290 
291  owl_context_set_interactive(owl_global_get_context(&g));
292
293  nexttimediff=10;
294  nexttime=time(NULL);
295
296  /* main loop */
297  while (1) {
298
299    /* if a resize has been scheduled, deal with it */
300    owl_global_resize(&g, 0, 0);
301
302    /* these are here in case a resize changes the windows */
303    msgwin=owl_global_get_curs_msgwin(&g);
304    recwin=owl_global_get_curs_recwin(&g);
305    sepwin=owl_global_get_curs_sepwin(&g);
306    typwin=owl_global_get_curs_typwin(&g);
307
308    followlast=owl_global_should_followlast(&g);
309   
310    /* If we're logged into AIM, do AIM stuff */
311    if (owl_global_is_aimloggedin(&g)) {
312      owl_function_debugmsg("Doing aim processing");
313      owl_aim_process_events();
314    }
315
316    /* little hack */
317    now=time(NULL);
318    today=localtime(&now);
319    if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) {
320      if (time(NULL)>nexttime) {
321        if (nexttimediff==1) {
322          nexttimediff=10;
323        } else {
324          nexttimediff=1;
325        }
326        nexttime+=nexttimediff;
327        owl_hack_animate();
328      }
329    }
330
331    /* Grab incoming messages. */
332    newmsgs=0;
333    zpendcount=0;
334    while(ZPending() || owl_global_messagequeue_pending(&g)) {
335      ZNotice_t notice;
336      struct sockaddr_in from;
337      owl_message *m;
338      owl_filter *f;
339     
340      /* grab the new message, stick it in 'm' */
341      if (ZPending()) {
342        /* grab a zephyr notice, but if we've done 20 without stopping,
343           take a break to process keystrokes etc. */
344        if (zpendcount>20) break;
345        ZReceiveNotice(&notice, &from);
346        zpendcount++;
347       
348        /* is this an ack from a zephyr we sent? */
349        if (owl_zephyr_notice_is_ack(&notice)) {
350          owl_zephyr_handle_ack(&notice);
351          continue;
352        }
353       
354        /* if it's a ping and we're not viewing pings then skip it */
355        if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
356          continue;
357        }
358
359        /* create the new message */
360        m=owl_malloc(sizeof(owl_message));
361        owl_message_create_from_znotice(m, &notice);
362      } else if (owl_global_messagequeue_pending(&g)) {
363        m=owl_global_messageuque_popmsg(&g);
364      }
365     
366      /* if this message it on the puntlist, nuke it and continue */
367      if (owl_global_message_is_puntable(&g, m)) {
368        owl_message_free(m);
369        continue;
370      }
371
372      /* otherwise add it to the global list */
373      owl_messagelist_append_element(owl_global_get_msglist(&g), m);
374      newmsgs=1;
375
376      /* let the config know the new message has been received */
377      owl_config_getmsg(m, "owl::receive_msg();");
378
379      /* add it to any necessary views; right now there's only the current view */
380      owl_view_consider_message(owl_global_get_current_view(&g), m);
381
382      /* do we need to autoreply? */
383      if (owl_message_is_type_zephyr(m) && owl_global_is_zaway(&g)) {
384        owl_zephyr_zaway(m);
385      }
386
387      /* ring the bell if it's a personal */
388      if (owl_global_is_personalbell(&g) &&
389          !owl_message_is_loginout(m) &&
390          !owl_message_is_mail(m) &&
391          owl_message_is_private(m)) {
392        owl_function_beep();
393      }
394
395      /* if it matches the alert filter, do the alert action */
396      f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g));
397      if (f && owl_filter_message_match(f, m)) {
398        owl_function_command(owl_global_get_alert_action(&g));
399      }
400
401      /* check for burning ears message */
402      /* this is an unsupported feature */
403      if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) {
404        char *buff;
405        buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m));
406        owl_function_adminmsg(buff, "");
407        owl_free(buff);
408        owl_function_beep();
409      }
410
411      /* log the message if we need to */
412      if (owl_global_is_logging(&g) || owl_global_is_classlogging(&g)) {
413        owl_log_incoming(m);
414      }
415    }
416
417    /* follow the last message if we're supposed to */
418    if (newmsgs && followlast) {
419      owl_function_lastmsg_noredisplay();
420    }
421
422    /* do the newmsgproc thing */
423    if (newmsgs) {
424      owl_function_do_newmsgproc();
425    }
426   
427    /* redisplay if necessary */
428    /* this should be optimized to not run if the new messages won't be displayed */
429    if (newmsgs) {
430      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
431      sepbar(NULL);
432      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
433        owl_popwin_refresh(owl_global_get_popwin(&g));
434        /* TODO: this is a broken kludge */
435        if (owl_global_get_viewwin(&g)) {
436          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
437        }
438      }
439      owl_global_set_needrefresh(&g);
440    }
441
442    /* if a popwin just came up, refresh it */
443    pw=owl_global_get_popwin(&g);
444    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
445      owl_popwin_refresh(pw);
446      owl_popwin_no_needs_first_refresh(pw);
447      owl_global_set_needrefresh(&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
454    /* update the terminal if we need to */
455    if (owl_global_is_needrefresh(&g)) {
456      /* leave the cursor in the appropriate window */
457      if (owl_global_is_typwin_active(&g)) {
458        owl_function_set_cursor(typwin);
459      } else {
460        owl_function_set_cursor(sepwin);
461      }
462      doupdate();
463      owl_global_set_noneedrefresh(&g);
464    }
465
466    /* Handle all keypresses.  If no key has been pressed, sleep for a
467     * little bit, but otherwise do not.  This lets input be grabbed
468     * as quickly as possbile */
469    j=wgetch(typwin);
470    if (j==ERR) {
471      usleep(10);
472      continue;
473    }
474    /* find and activate the current keymap.
475     * TODO: this should really get fixed by activating
476     * keymaps as we switch between windows...
477     */
478    if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
479      owl_context_set_popless(owl_global_get_context(&g), 
480                              owl_global_get_viewwin(&g));
481      owl_function_activate_keymap("popless");
482    } else if (owl_global_is_typwin_active(&g) 
483               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
484      owl_context_set_editline(owl_global_get_context(&g), tw);
485      owl_function_activate_keymap("editline");
486    } else if (owl_global_is_typwin_active(&g) 
487               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
488      owl_context_set_editmulti(owl_global_get_context(&g), tw);
489      owl_function_activate_keymap("editmulti");
490    } else {
491      owl_context_set_recv(owl_global_get_context(&g));
492      owl_function_activate_keymap("recv");
493    }
494    /* now actually handle the keypress */
495    ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
496    if (ret!=0 && ret!=1) {
497      owl_function_makemsg("Unable to handle keypress");
498    }
499  }
500}
501
502void sig_handler(int sig) {
503  if (sig==SIGWINCH) {
504    /* we can't inturrupt a malloc here, so it just sets a flag
505     * schedulding a resize for later
506     */
507    owl_function_resize();
508  }
509}
510
511void usage() {
512  fprintf(stderr, "Usage: owl [-n] [-d] [-v] [-c <configfile>] [-t <ttyname>]\n");
513}
Note: See TracBrowser for help on using the repository browser.