source: owl.c @ 6e05655

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