source: owl.c @ 440ce01

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