source: owl.c @ b2b0773

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