source: owl.c @ 7933748

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 7933748 was 7933748, checked in by James M. Kretchmar <kretch@mit.edu>, 18 years ago
Added a 'loadloginsubs' command to load login subscriptions from a file Added a 'loadsubs' command to eventually phase out the 'load-subs' command
  • Property mode set to 100644
File size: 12.9 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;
33  int newzephyrs, 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();
108  use_default_colors();
109  raw();
110  noecho();
111
112  /* define simple color pairs */
113  if (has_colors() && COLOR_PAIRS>=8) {
114    init_pair(OWL_COLOR_BLACK,   COLOR_BLACK,   -1);
115    init_pair(OWL_COLOR_RED,     COLOR_RED,     -1);
116    init_pair(OWL_COLOR_GREEN,   COLOR_GREEN,   -1);
117    init_pair(OWL_COLOR_YELLOW,  COLOR_YELLOW,  -1);
118    init_pair(OWL_COLOR_BLUE,    COLOR_BLUE,    -1);
119    init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, -1);
120    init_pair(OWL_COLOR_CYAN,    COLOR_CYAN,    -1);
121    init_pair(OWL_COLOR_WHITE,   COLOR_WHITE,   -1);
122  }
123   
124  /* owl init */
125  owl_global_init(&g);
126  if (debug) owl_global_set_debug_on(&g);
127  owl_global_set_startupargs(&g, argcsave, argvsave);
128  owl_context_set_readconfig(owl_global_get_context(&g));
[5145235]129
[ced25d1]130  /* set the tty, either from the command line, or by figuring it out */
[61e79a9]131  if (tty) {
132    owl_global_set_tty(&g, tty);
133  } else {
134    owl_global_set_tty(&g, owl_util_get_default_tty());
135  }
[7d4fbcd]136
137  /* setup the default filters */
138  f=malloc(sizeof(owl_filter));
[b45293f]139  owl_filter_init_fromstring(f, "personal", "class ^message$ and instance ^personal$ and ( recipient ^%me%$ or sender ^%me%$ )");
[7d4fbcd]140  owl_list_append_element(owl_global_get_filterlist(&g), f);
141
142  f=malloc(sizeof(owl_filter));
143  owl_filter_init_fromstring(f, "trash", "class ^mail$ or opcode ^ping$ or type ^admin$ or class ^login$");
144  owl_list_append_element(owl_global_get_filterlist(&g), f);
145
146  f=malloc(sizeof(owl_filter));
[0236842]147  owl_filter_init_fromstring(f, "ping", "opcode ^ping$");
148  owl_list_append_element(owl_global_get_filterlist(&g), f);
149
150  f=malloc(sizeof(owl_filter));
151  owl_filter_init_fromstring(f, "auto", "opcode ^auto$");
152  owl_list_append_element(owl_global_get_filterlist(&g), f);
153
154  f=malloc(sizeof(owl_filter));
155  owl_filter_init_fromstring(f, "login", "class ^login$");
156  owl_list_append_element(owl_global_get_filterlist(&g), f);
157
158  f=malloc(sizeof(owl_filter));
[7d4fbcd]159  owl_filter_init_fromstring(f, "reply-lockout", "class ^noc or class ^mail$");
160  owl_list_append_element(owl_global_get_filterlist(&g), f);
161
162  f=malloc(sizeof(owl_filter));
[5a6e6b9]163  owl_filter_init_fromstring(f, "out", "direction ^out$");
164  owl_list_append_element(owl_global_get_filterlist(&g), f);
165
166  f=malloc(sizeof(owl_filter));
[89426ab]167  owl_filter_init_fromstring(f, "none", "false");
168  owl_list_append_element(owl_global_get_filterlist(&g), f);
169
170  f=malloc(sizeof(owl_filter));
171  owl_filter_init_fromstring(f, "all", "true");
[7d4fbcd]172  owl_list_append_element(owl_global_get_filterlist(&g), f);
173
174  /* set the current view */
175  owl_view_create(owl_global_get_current_view(&g), f);
176
177  /* read the config file */
178  ret=owl_readconfig(configfile);
179  if (ret) {
180    endwin();
181    printf("\nError parsing configfile\n");
182    exit(1);
183  }
[ced25d1]184
185  /* execute the startup function in the configfile */
[7d4fbcd]186  perlout = owl_config_execute("owl::startup();");
187  if (perlout) owl_free(perlout);
188  owl_function_debugmsg("-- Owl Startup --");
189 
190  /* hold on to the window names for convenience */
191  msgwin=owl_global_get_curs_msgwin(&g);
192  recwin=owl_global_get_curs_recwin(&g);
193  sepwin=owl_global_get_curs_sepwin(&g);
194  typwin=owl_global_get_curs_typwin(&g);
195  tw=owl_global_get_typwin(&g);
196
197  wrefresh(sepwin);
198
199  /* load zephyr subs */
200  ret=owl_zephyr_loadsubs(NULL);
201  if (ret!=-1) {
202    owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
203  }
204
205  /* load login subs */
206  if (owl_global_is_loginsubs(&g)) {
[7933748]207    owl_function_loadloginsubs(NULL);
[7d4fbcd]208  }
209
210  /* zlog in if we need to */
211  if (owl_global_is_startuplogin(&g)) {
212    owl_function_zlog_in();
213  }
214
215  /* welcome message */
216  strcpy(startupmsg, "-------------------------------------------------------------------------\n");
[601a9e0]217  sprintf(buff,      "Welcome to owl version %s.  Press 'h' for on line help. \n", OWL_VERSION_STRING);
[7d4fbcd]218  strcat(startupmsg, buff);
[601a9e0]219  strcat(startupmsg, "                                                                         \n");
220  strcat(startupmsg, "If you would like to receive release announcments about owl you can join \n");
221  strcat(startupmsg, "the owl-users@mit.edu mailing list.  MIT users can add themselves,       \n");
222  strcat(startupmsg, "otherwise send a request to owner-owl-users@mit.edu.               ^ ^   \n");
223  strcat(startupmsg, "                                                                   OvO   \n");
224  strcat(startupmsg, "Please report any bugs or suggestions to bug-owl@mit.edu          (   )  \n");
225  strcat(startupmsg, "-------------------------------------------------------------------m-m---\n");
[7d4fbcd]226  owl_function_adminmsg("", startupmsg);
227  sepbar(NULL);
228 
229  owl_context_set_interactive(owl_global_get_context(&g));
230
[a15a84f]231  nexttimediff=10;
[4b464a4]232  nexttime=time(NULL);
233
[7d4fbcd]234  /* main loop */
235  while (1) {
236
237    /* if a resize has been scheduled, deal with it */
238    owl_global_resize(&g, 0, 0);
239
240    /* these are here in case a resize changes the windows */
241    msgwin=owl_global_get_curs_msgwin(&g);
242    recwin=owl_global_get_curs_recwin(&g);
243    sepwin=owl_global_get_curs_sepwin(&g);
244    typwin=owl_global_get_curs_typwin(&g);
245
246    followlast=owl_global_should_followlast(&g);
247
[4b464a4]248    /* little hack */
[a15a84f]249    now=time(NULL);
250    today=localtime(&now);
251    if (today->tm_mon==9 && today->tm_mday==31 && owl_global_get_runtime(&g)<600) {
[4b464a4]252      if (time(NULL)>nexttime) {
253        if (nexttimediff==1) {
[a15a84f]254          nexttimediff=10;
[4b464a4]255        } else {
256          nexttimediff=1;
257        }
258        nexttime+=nexttimediff;
259        owl_hack_animate();
260      }
261    }
262
[7d4fbcd]263    /* grab incoming zephyrs */
264    newzephyrs=0;
[ced25d1]265    zpendcount=0;
[7d4fbcd]266    while(ZPending()) {
267      ZNotice_t notice;
268      struct sockaddr_in from;
269      owl_message *m;
270
[ced25d1]271      /* grab a notice, but if we've done 20 without stopping, take
272         a break to process keystrokes etc. */
273      if (zpendcount>20) break;
[7d4fbcd]274      ZReceiveNotice(&notice, &from);
[ced25d1]275      zpendcount++;
[7d4fbcd]276
277      /* is this an ack from a zephyr we sent? */
278      if (owl_zephyr_notice_is_ack(&notice)) {
279        owl_zephyr_handle_ack(&notice);
280        continue;
281      }
282     
283      /* if it's a ping and we're not viewing pings then skip it */
284      if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
285        continue;
286      }
287
288      /* create the new message */
289      m=owl_malloc(sizeof(owl_message));
[b45293f]290      owl_message_create_from_znotice(m, &notice);
[7d4fbcd]291     
292      /* if it's on the puntlist then, nuke it and continue */
293      if (owl_global_message_is_puntable(&g, m)) {
294        owl_message_free(m);
295        continue;
296      }
297
298      /* otherwise add it to the global list */
299      owl_messagelist_append_element(owl_global_get_msglist(&g), m);
300      newzephyrs=1;
301
302      /* let the config know the new message has been received */
303      owl_config_getmsg(m, 0);
304
305      /* add it to any necessary views; right now there's only the current view */
306      owl_view_consider_message(owl_global_get_current_view(&g), m);
307
308      /* do we need to autoreply? */
309      if (owl_global_is_zaway(&g)) {
310        owl_zephyr_zaway(m);
311      }
312
313      /* ring the bell if it's a personal */
314      if (owl_global_is_personalbell(&g) && owl_message_is_personal(m)) {
315        owl_function_beep();
316      }
317
318      /* check for burning ears message */
319      /* this is an unsupported feature */
320      if (owl_global_is_burningears(&g) && owl_message_is_burningears(m)) {
[1c6c4d3]321        char *buff;
322        buff = owl_sprintf("@i(Burning ears message on class %s)", owl_message_get_class(m));
[7d4fbcd]323        /* owl_function_makemsg(buff); */
324        owl_function_adminmsg(buff, "");
[1c6c4d3]325        owl_free(buff);
[7d4fbcd]326        owl_function_beep();
327      }
328
329      /* log the zephyr if we need to */
330      if (owl_global_is_logging(&g) || owl_global_is_classlogging(&g)) {
331        owl_log_incoming(m);
332      }
333    }
334
335    /* follow the last message if we're supposed to */
336    if (newzephyrs && followlast) {
337      owl_function_lastmsg_noredisplay();
338    }
[700c712]339
[ced25d1]340    /* do the newmsgproc thing */
[700c712]341    if (newzephyrs) {
[8f44c6b]342      owl_function_do_newmsgproc();
[700c712]343    }
[7d4fbcd]344   
345    /* redisplay if necessary */
[ced25d1]346    /* maybe this should be optimized to not even run if
347       the zephyr won't be displayed */
[7d4fbcd]348    if (newzephyrs) {
349      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
350      sepbar(NULL);
351      if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
352        owl_popwin_refresh(owl_global_get_popwin(&g));
353        /* TODO: this is a broken kludge */
354        if (owl_global_get_viewwin(&g)) {
355          owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
356        }
357      }
358      owl_global_set_needrefresh(&g);
359    }
360
361    /* if a popwin just came up, refresh it */
362    pw=owl_global_get_popwin(&g);
363    if (owl_popwin_is_active(pw) && owl_popwin_needs_first_refresh(pw)) {
364      owl_popwin_refresh(pw);
365      owl_popwin_no_needs_first_refresh(pw);
366      owl_global_set_needrefresh(&g);
367      /* TODO: this is a broken kludge */
368      if (owl_global_get_viewwin(&g)) {
369        owl_viewwin_redisplay(owl_global_get_viewwin(&g), 0);
370      }
371    }
372
373    /* update the terminal if we need to */
374    if (owl_global_is_needrefresh(&g)) {
375      /* leave the cursor in the appropriate window */
376      if (owl_global_is_typwin_active(&g)) {
377        owl_function_set_cursor(typwin);
378      } else {
379        owl_function_set_cursor(sepwin);
380      }
381      doupdate();
382      owl_global_set_noneedrefresh(&g);
383    }
384
385    /* handle all keypresses */
386    j=wgetch(typwin);
387    if (j==ERR) {
388      usleep(10);
389      continue;
390    }
391    /* find and activate the current keymap.
392     * TODO: this should really get fixed by activating
393     * keymaps as we switch between windows...
394     */
395    if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
396      owl_context_set_popless(owl_global_get_context(&g), 
397                              owl_global_get_viewwin(&g));
398      owl_function_activate_keymap("popless");
399    } else if (owl_global_is_typwin_active(&g) 
400               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
401      owl_context_set_editline(owl_global_get_context(&g), tw);
402      owl_function_activate_keymap("editline");
403    } else if (owl_global_is_typwin_active(&g) 
404               && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
405      owl_context_set_editmulti(owl_global_get_context(&g), tw);
406      owl_function_activate_keymap("editmulti");
407    } else {
408      owl_context_set_recv(owl_global_get_context(&g));
409      owl_function_activate_keymap("recv");
410    }
411    /* now actually handle the keypress */
412    ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
413    if (ret!=0 && ret!=1) {
414      owl_function_makemsg("Unable to handle keypress");
415    }
416  }
417
418}
419
420void sig_handler(int sig) {
421  if (sig==SIGWINCH) {
422    /* we can't inturrupt a malloc here, so it just sets a flag */
423    owl_function_resize();
424  }
425}
426
427void usage() {
428  fprintf(stderr, "Usage: owl [-n] [-d] [-v] [-c <configfile>] [-t <ttyname>]\n");
429}
Note: See TracBrowser for help on using the repository browser.