source: owl.c @ 38cf544c

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