source: owl.h @ 8b2789c

release-1.10release-1.7release-1.8release-1.9
Last change on this file since 8b2789c was c6adf17, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Track names along with timers, add :show timers This will help people with BarnOwls eating CPU to diagnose timer leaks.
  • Property mode set to 100644
File size: 18.3 KB
Line 
1/*  Copyright (c) 2006-2010 The BarnOwl Developers. All rights reserved.
2 *  Copyright (c) 2004 James Kretchmar. All rights reserved.
3 *
4 *  This program is free software. You can redistribute it and/or
5 *  modify under the terms of the Sleepycat License. See the COPYING
6 *  file included with the distribution for more information.
7 */
8
9#ifndef INC_BARNOWL_OWL_H
10#define INC_BARNOWL_OWL_H
11
12#include "config.h"
13
14#ifdef HAVE_STDBOOL_H
15#include <stdbool.h>
16#else
17#ifndef HAVE__BOOL
18#define _Bool signed char
19#endif
20#define bool _Bool
21#define false 0
22#define true 1
23#define __bool_true_false_are_defined 1
24#endif  /* HAVE_STDBOOL_H */
25
26#ifndef OWL_PERL
27#include <curses.h>
28#include <panel.h>
29#endif
30#include <sys/param.h>
31#include <EXTERN.h>
32#include <netdb.h>
33#include <regex.h>
34#include <time.h>
35#include <signal.h>
36#include <termios.h>
37#include "libfaim/aim.h"
38#include <wchar.h>
39#include "glib.h"
40#ifdef HAVE_LIBZEPHYR
41#include <zephyr/zephyr.h>
42#endif
43#ifdef HAVE_COM_ERR_H
44#include <com_err.h>
45#endif
46
47/* Perl and curses don't play nice. */
48#ifdef OWL_PERL
49typedef void WINDOW;
50typedef void PANEL;
51/* logout is defined in FreeBSD. */
52#define logout logout_
53#define HAS_BOOL
54#include <perl.h>
55#include "owl_perl.h"
56#undef logout
57#include "XSUB.h"
58#else
59typedef void SV;
60typedef void AV;
61typedef void HV;
62#endif
63
64#include "window.h"
65
66#ifdef  GIT_VERSION
67#define stringify(x)       __stringify(x)
68#define __stringify(x)     #x
69#define OWL_VERSION_STRING stringify(GIT_VERSION)
70#else
71#define OWL_VERSION_STRING PACKAGE_VERSION
72#endif
73
74/* Feature that is being tested to redirect stderr through a pipe.
75 * There may still be some portability problems with this. */
76#define OWL_STDERR_REDIR 1
77
78#define OWL_DEBUG 0
79#define OWL_DEBUG_FILE "/var/tmp/barnowl-debug"
80
81#define OWL_CONFIG_DIR "/.owl"             /* this is relative to the user's home directory */
82#define OWL_STARTUP_FILE "/.owl/startup"   /* this is relative to the user's home directory */
83
84#define OWL_FMTEXT_ATTR_NONE      0
85#define OWL_FMTEXT_ATTR_BOLD      1
86#define OWL_FMTEXT_ATTR_REVERSE   2
87#define OWL_FMTEXT_ATTR_UNDERLINE 4
88
89#define OWL_FMTEXT_UC_BASE 0x100000 /* Unicode Plane 16 - Supplementary Private Use Area-B*/
90#define OWL_FMTEXT_UC_ATTR ( OWL_FMTEXT_UC_BASE | 0x800 )
91#define OWL_FMTEXT_UC_ATTR_MASK 0x7
92#define OWL_FMTEXT_UC_COLOR_BASE ( OWL_FMTEXT_UC_BASE | 0x400 )
93#define OWL_FMTEXT_UC_FGCOLOR OWL_FMTEXT_UC_COLOR_BASE
94#define OWL_FMTEXT_UC_BGCOLOR ( OWL_FMTEXT_UC_COLOR_BASE | 0x200 )
95#define OWL_FMTEXT_UC_DEFAULT_COLOR 0x100
96#define OWL_FMTEXT_UC_FGDEFAULT ( OWL_FMTEXT_UC_FGCOLOR | OWL_FMTEXT_UC_DEFAULT_COLOR )
97#define OWL_FMTEXT_UC_BGDEFAULT ( OWL_FMTEXT_UC_BGCOLOR | OWL_FMTEXT_UC_DEFAULT_COLOR )
98#define OWL_FMTEXT_UC_COLOR_MASK 0xFF
99#define OWL_FMTEXT_UC_ALLCOLOR_MASK ( OWL_FMTEXT_UC_COLOR_MASK | OWL_FMTEXT_UC_DEFAULT_COLOR | 0x200)
100#define OWL_FMTEXT_UC_STARTBYTE_UTF8 '\xf4'
101
102#define OWL_FMTEXT_UTF8_ATTR_NONE "\xf4\x80\xa0\x80"
103#define OWL_FMTEXT_UTF8_FGDEFAULT "\xf4\x80\x94\x80"
104#define OWL_FMTEXT_UTF8_BGDEFAULT "\xf4\x80\x9C\x80"
105
106#define OWL_COLOR_BLACK     0
107#define OWL_COLOR_RED       1
108#define OWL_COLOR_GREEN     2
109#define OWL_COLOR_YELLOW    3
110#define OWL_COLOR_BLUE      4
111#define OWL_COLOR_MAGENTA   5
112#define OWL_COLOR_CYAN      6
113#define OWL_COLOR_WHITE     7
114#define OWL_COLOR_DEFAULT   -1
115#define OWL_COLOR_INVALID   -2
116
117#define OWL_TAB_WIDTH 8
118
119#define OWL_EDITWIN_STYLE_MULTILINE 0
120#define OWL_EDITWIN_STYLE_ONELINE   1
121
122#define OWL_PROTOCOL_ZEPHYR         0
123#define OWL_PROTOCOL_AIM            1
124#define OWL_PROTOCOL_JABBER         2
125#define OWL_PROTOCOL_ICQ            3
126#define OWL_PROTOCOL_YAHOO          4
127#define OWL_PROTOCOL_MSN            5
128
129#define OWL_MESSAGE_DIRECTION_NONE  0
130#define OWL_MESSAGE_DIRECTION_IN    1
131#define OWL_MESSAGE_DIRECTION_OUT   2
132
133#define OWL_IO_READ   1
134#define OWL_IO_WRITE  2
135#define OWL_IO_EXCEPT 4
136
137#define OWL_DIRECTION_NONE      0
138#define OWL_DIRECTION_DOWNWARDS 1
139#define OWL_DIRECTION_UPWARDS   2
140
141#define OWL_LOGGING_DIRECTION_BOTH 0
142#define OWL_LOGGING_DIRECTION_IN   1
143#define OWL_LOGGING_DIRECTION_OUT  2
144
145#define OWL_SCROLLMODE_NORMAL      0
146#define OWL_SCROLLMODE_TOP         1
147#define OWL_SCROLLMODE_NEARTOP     2
148#define OWL_SCROLLMODE_CENTER      3
149#define OWL_SCROLLMODE_PAGED       4
150#define OWL_SCROLLMODE_PAGEDCENTER 5
151
152#define OWL_TAB               3  /* This *HAS* to be the size of TABSTR below */
153#define OWL_TABSTR        "   "
154#define OWL_MSGTAB            7
155#define OWL_TYPWIN_SIZE       8
156#define OWL_HISTORYSIZE       50
157
158/* Indicate current state, as well as what is allowed */
159#define OWL_CTX_ANY          0xffff
160/* Only one of these may be active at a time... */
161#define OWL_CTX_MODE_BITS    0x000f
162#define OWL_CTX_STARTUP      0x0001
163#define OWL_CTX_READCONFIG   0x0002
164#define OWL_CTX_INTERACTIVE  0x0004
165/* Only one of these may be active at a time... */
166#define OWL_CTX_ACTIVE_BITS  0xfff0
167#define OWL_CTX_POPWIN       0x00f0
168#define OWL_CTX_POPLESS      0x0010
169#define OWL_CTX_RECWIN       0x0f00
170#define OWL_CTX_RECV         0x0100
171#define OWL_CTX_TYPWIN       0xf000
172#define OWL_CTX_EDIT         0x7000
173#define OWL_CTX_EDITLINE     0x1000
174#define OWL_CTX_EDITMULTI    0x2000
175#define OWL_CTX_EDITRESPONSE 0x4000
176
177#define OWL_VARIABLE_OTHER      0
178#define OWL_VARIABLE_INT        1
179#define OWL_VARIABLE_BOOL       2
180#define OWL_VARIABLE_STRING     3
181
182#define OWL_OUTPUT_RETURN       0
183#define OWL_OUTPUT_POPUP        1
184#define OWL_OUTPUT_ADMINMSG     2
185
186#define OWL_FILTER_MAX_DEPTH    300
187
188#define OWL_KEYMAP_MAXSTACK     20
189
190#define OWL_KEYBINDING_COMMAND  1   /* command string */
191#define OWL_KEYBINDING_FUNCTION 2   /* function taking no args */
192
193#define OWL_DEFAULT_ZAWAYMSG    "I'm sorry, but I am currently away from the terminal and am\nnot able to receive your message.\n"
194#define OWL_DEFAULT_AAWAYMSG    "I'm sorry, but I am currently away from the terminal and am\nnot able to receive your message.\n"
195
196#define OWL_CMD_ALIAS_SUMMARY_PREFIX "command alias to: "
197
198#define OWL_WEBZEPHYR_PRINCIPAL "daemon.webzephyr"
199#define OWL_WEBZEPHYR_CLASS     "webzephyr"
200#define OWL_WEBZEPHYR_OPCODE    "webzephyr"
201
202#define OWL_REGEX_QUOTECHARS    "+*.?[]^\\${}()"
203#define OWL_REGEX_QUOTEWITH     "\\"
204
205#if defined(HAVE_DES_STRING_TO_KEY) && defined(HAVE_DES_KEY_SCHED) && defined(HAVE_DES_ECB_ENCRYPT)
206#define OWL_ENABLE_ZCRYPT 1
207#endif
208
209#define OWL_META(key) ((key)|010000)
210/* OWL_CTRL is definied in kepress.c */
211
212#define LINE 2048
213
214#ifdef HAVE_LIBZEPHYR
215/* libzephyr doesn't use const, so we appease the type system with this kludge.
216 * This just casts const char * to char * in a way that doesn't yield a warning
217 * from gcc -Wcast-qual. */
218static inline char *zstr(const char *str)
219{
220  union { char *rw; const char *ro; } u;
221  u.ro = str;
222  return u.rw;
223}
224#endif
225
226/* Convert char *const * into const char *const *.  This conversion is safe,
227 * and implicit in C++ (conv.qual 4) but for some reason not in C. */
228static inline const char *const *strs(char *const *pstr)
229{
230  return (const char *const *)pstr;
231}
232
233typedef struct _owl_variable {
234  char *name;
235  int   type;  /* OWL_VARIABLE_* */
236  void *pval_default;  /* for types other and string */
237  int   ival_default;  /* for types int and bool     */
238  char *validsettings;          /* documentation of valid settings */
239  char *summary;                /* summary of usage */
240  char *description;            /* detailed description */
241  void *val;                    /* current value */
242  int  (*validate_fn)(const struct _owl_variable *v, const void *newval);
243                                /* returns 1 if newval is valid */
244  int  (*set_fn)(struct _owl_variable *v, const void *newval); 
245                                /* sets the variable to a value
246                                 * of the appropriate type.
247                                 * unless documented, this
248                                 * should make a copy.
249                                 * returns 0 on success. */
250  int  (*set_fromstring_fn)(struct _owl_variable *v, const char *newval);
251                                /* sets the variable to a value
252                                 * of the appropriate type.
253                                 * unless documented, this
254                                 * should make a copy.
255                                 * returns 0 on success. */
256  const void *(*get_fn)(const struct _owl_variable *v);
257                                /* returns a reference to the current value.
258                                 * WARNING:  this approach is hard to make
259                                 * thread-safe... */
260  int  (*get_tostring_fn)(const struct _owl_variable *v, 
261                          char *buf, int bufsize, const void *val); 
262                                /* converts val to a string
263                                 * and puts into buf */
264  void (*delete_fn)(struct _owl_variable *v);
265                                /* frees val as needed */
266} owl_variable;
267
268typedef struct _owl_input {
269  int ch;
270  gunichar uch;
271} owl_input;
272
273typedef struct _owl_fmtext {
274  int textlen;
275  int bufflen;
276  char *textbuff;
277  char default_attrs;
278  short default_fgcolor;
279  short default_bgcolor;
280} owl_fmtext;
281
282typedef struct _owl_list {
283  int size;
284  int avail;
285  void **list;
286} owl_list;
287
288typedef struct _owl_dict_el {
289  char *k;                      /* key   */
290  void *v;                      /* value */
291} owl_dict_el;
292
293typedef struct _owl_dict {
294  int size;
295  int avail;
296  owl_dict_el *els;             /* invariant: sorted by k */
297} owl_dict;
298typedef owl_dict owl_vardict;   /* dict of variables */
299typedef owl_dict owl_cmddict;   /* dict of commands */
300
301typedef struct _owl_context {
302  int   mode;
303  void *data;           /* determined by mode */
304  char *keymap;
305  owl_window *cursor;
306  void (*deactivate_cb)(struct _owl_context*);
307  void (*delete_cb)(struct _owl_context*);
308  void *cbdata;
309} owl_context;
310
311typedef struct _owl_cmd {       /* command */
312  char *name;
313
314  char *summary;                /* one line summary of command */
315  char *usage;                  /* usage synopsis */
316  char *description;            /* long description of command */
317
318  int validctx;                 /* bitmask of valid contexts */
319
320  /* we should probably have a type here that says which of
321   * the following is valid, and maybe make the below into a union... */
322
323  /* Only one of these may be non-NULL ... */
324
325  char *cmd_aliased_to;         /* what this command is aliased to... */
326 
327  /* These don't take any context */
328  char *(*cmd_args_fn)(int argc, const char *const *argv, const char *buff); 
329                                /* takes argv and the full command as buff.
330                                 * caller must free return value if !NULL */
331  void (*cmd_v_fn)(void);       /* takes no args */
332  void (*cmd_i_fn)(int i);      /* takes an int as an arg */
333
334  /* The following also take the active context if it's valid */
335  char *(*cmd_ctxargs_fn)(void *ctx, int argc, const char *const *argv, const char *buff); 
336                                /* takes argv and the full command as buff.
337                                 * caller must free return value if !NULL */
338  void (*cmd_ctxv_fn)(void *ctx);               /* takes no args */
339  void (*cmd_ctxi_fn)(void *ctx, int i);        /* takes an int as an arg */
340  SV *cmd_perl;                                /* Perl closure that takes a list of args */
341} owl_cmd;
342
343
344typedef struct _owl_zwrite {
345  char *cmd;
346  char *zwriteline;
347  char *class;
348  char *inst;
349  char *realm;
350  char *opcode;
351  char *zsig;
352  char *message;
353  owl_list recips;
354  int cc;
355  int noping;
356} owl_zwrite;
357
358typedef struct _owl_pair {
359  const char *key;
360  char *value;
361} owl_pair;
362
363struct _owl_fmtext_cache;
364
365typedef struct _owl_message {
366  int id;
367  int direction;
368#ifdef HAVE_LIBZEPHYR
369  ZNotice_t notice;
370#endif
371  struct _owl_fmtext_cache * fmtext;
372  int delete;
373  const char *hostname;
374  owl_list attributes;            /* this is a list of pairs */
375  char *timestr;
376  time_t time;
377} owl_message;
378
379#define OWL_FMTEXT_CACHE_SIZE 1000
380/* We cache the saved fmtexts for the last bunch of messages we
381   rendered */
382typedef struct _owl_fmtext_cache {
383    owl_message * message;
384    owl_fmtext fmtext;
385} owl_fmtext_cache;
386
387typedef struct _owl_style {
388  char *name;
389  SV *perlobj;
390} owl_style;
391
392typedef struct _owl_mainwin {
393  int curtruncated;
394  int lasttruncated;
395  int lastdisplayed;
396  owl_window *window;
397} owl_mainwin;
398
399typedef struct _owl_editwin owl_editwin;
400typedef struct _owl_editwin_excursion owl_editwin_excursion;
401
402typedef struct _owl_viewwin {
403  owl_fmtext fmtext;
404  int textlines;
405  int topline;
406  int rightshift;
407  owl_window *window;
408  void (*onclose_hook) (struct _owl_viewwin *vwin, void *data);
409  void *onclose_hook_data;
410
411  gulong sig_resize_id;
412  owl_window *content;
413  gulong sig_content_redraw_id;
414  owl_window *status;
415  gulong sig_status_redraw_id;
416  owl_window *cmdwin;
417} owl_viewwin;
418 
419typedef struct _owl_popwin {
420  owl_window *border;
421  owl_window *content;
422  gulong sig_redraw_id;
423  gulong sig_resize_id;
424} owl_popwin;
425 
426typedef struct _owl_msgwin {
427  char *msg;
428  owl_window *window;
429  gulong redraw_id;
430} owl_msgwin;
431
432typedef struct _owl_messagelist {
433  owl_list list;
434} owl_messagelist;
435
436typedef struct _owl_regex {
437  int negate;
438  char *string;
439  regex_t re;
440} owl_regex;
441
442typedef struct _owl_filterelement {
443  int (*match_message)(const struct _owl_filterelement *fe, const owl_message *m);
444  /* Append a string representation of the filterelement onto buf*/
445  void (*print_elt)(const struct _owl_filterelement *fe, GString *buf);
446  /* Operands for and,or,not*/
447  struct _owl_filterelement *left, *right;
448  /* For regex filters*/
449  owl_regex re;
450  /* Used by regexes, filter references, and perl */
451  char *field;
452} owl_filterelement;
453
454typedef struct _owl_filter {
455  char *name;
456  owl_filterelement * root;
457  int fgcolor;
458  int bgcolor;
459} owl_filter;
460
461typedef struct _owl_view {
462  char *name;
463  owl_filter *filter;
464  owl_messagelist ml;
465  const owl_style *style;
466  int cachedmsgid;
467} owl_view;
468
469typedef struct _owl_history {
470  owl_list hist;
471  int cur;
472  int touched;
473  int partial;
474  int repeats;
475} owl_history;
476
477typedef struct _owl_mainpanel {
478  owl_window *panel;
479  owl_window *typwin;
480  owl_window *sepwin;
481  owl_window *msgwin;
482  owl_window *recwin;
483  int recwinlines;
484} owl_mainpanel;
485
486typedef struct _owl_keybinding {
487  int  *keys;                   /* keypress stack */
488  int   len;                    /* length of stack */
489  int   type;                   /* command or function? */
490  char *desc;                   /* description (or "*user*") */
491  char *command;                /* command, if of type command */
492  void (*function_fn)(void);    /* function ptr, if of type function */
493} owl_keybinding;
494
495typedef struct _owl_keymap {
496  char     *name;               /* name of keymap */
497  char     *desc;               /* description */
498  owl_list  bindings;           /* key bindings */
499  const struct _owl_keymap *parent;     /* parent */
500  void (*default_fn)(owl_input j);      /* default action (takes a keypress) */
501  void (*prealways_fn)(owl_input  j);   /* always called before a keypress is received */
502  void (*postalways_fn)(owl_input  j);  /* always called after keypress is processed */
503} owl_keymap;
504
505typedef struct _owl_keyhandler {
506  owl_dict  keymaps;            /* dictionary of keymaps */
507  const owl_keymap *active;             /* currently active keymap */
508  int       in_esc;             /* escape pressed? */
509  int       kpstack[OWL_KEYMAP_MAXSTACK+1]; /* current stack of keypresses */
510  int       kpstackpos;         /* location in stack (-1 = none) */
511} owl_keyhandler;
512
513typedef struct _owl_buddy {
514  int proto;
515  char *name;
516  int isidle;
517  int idlesince;
518} owl_buddy;
519
520typedef struct _owl_buddylist {
521  owl_list buddies;
522} owl_buddylist;
523
524typedef struct _owl_zbuddylist {
525  owl_list zusers;
526} owl_zbuddylist;
527
528typedef struct _owl_timer {
529  time_t time;
530  int interval;
531  void (*callback)(struct _owl_timer *, void *);
532  void (*destroy)(struct _owl_timer *);
533  void *data;
534  char *name;
535} owl_timer;
536
537typedef struct _owl_errqueue {
538  owl_list errlist;
539} owl_errqueue;
540
541typedef struct _owl_colorpair_mgr {
542  int next;
543  short **pairs;
544} owl_colorpair_mgr;
545
546typedef struct _owl_io_dispatch {
547  int fd;                                     /* FD to watch for dispatch. */
548  int mode;
549  int needs_gc;
550  void (*callback)(const struct _owl_io_dispatch *, void *); /* C function to dispatch to. */
551  void (*destroy)(const struct _owl_io_dispatch *);  /* Destructor */
552  void *data;
553} owl_io_dispatch;
554
555typedef struct _owl_ps_action {
556  int needs_gc;
557  int (*callback)(struct _owl_ps_action *, void *);
558  void (*destroy)(struct _owl_ps_action *);
559  void *data;
560} owl_ps_action;
561
562typedef struct _owl_popexec {
563  int refcount;
564  owl_viewwin *vwin;
565  int winactive;
566  pid_t pid;                    /* or 0 if it has terminated */
567  const owl_io_dispatch *dispatch;
568} owl_popexec;
569
570typedef struct _OwlGlobalNotifier OwlGlobalNotifier;
571
572typedef struct _owl_global {
573  owl_mainwin mw;
574  owl_popwin *pw;
575  owl_msgwin msgwin;
576  owl_history cmdhist;          /* command history */
577  owl_history msghist;          /* outgoing message history */
578  owl_keyhandler kh;
579  owl_dict filters;
580  GList *filterlist;
581  owl_list puntlist;
582  owl_vardict vars;
583  owl_cmddict cmds;
584  GList *context_stack;
585  owl_errqueue errqueue;
586  int lines, cols;
587  int curmsg, topmsg;
588  int markedmsgid;              /* for finding the marked message when it has moved. */
589  int curmsg_vert_offset;
590  owl_view current_view;
591  owl_messagelist msglist;
592  WINDOW *input_pad;
593  owl_mainpanel mainpanel;
594  gulong typwin_erase_id;
595  int rightshift;
596  volatile sig_atomic_t resizepending;
597  char *thishost;
598  char *homedir;
599  char *confdir;
600  char *startupfile;
601  int direction;
602  int zaway;
603  char *cur_zaway_msg;
604  int haveconfig;
605  int config_format;
606  void *buffercbdata;
607  owl_editwin *tw;
608  owl_viewwin *vw;
609  void *perl;
610  int debug;
611  time_t starttime;
612  time_t lastinputtime;
613  char *startupargs;
614  int nextmsgid;
615  int hascolors;
616  int colorpairs;
617  owl_colorpair_mgr cpmgr;
618  pid_t newmsgproc_pid;
619  owl_regex search_re;
620  aim_session_t aimsess;
621  aim_conn_t bosconn;
622  owl_timer aim_noop_timer;
623  owl_timer aim_ignorelogin_timer;
624  int aim_loggedin;         /* true if currently logged into AIM */
625  int aim_doprocessing;     /* true if we should process AIM events (like pending login) */
626  char *aim_screenname;     /* currently logged in AIM screen name */
627  char *aim_screenname_for_filters;     /* currently logged in AIM screen name */
628  owl_buddylist buddylist;  /* list of logged in AIM buddies */
629  GQueue *messagequeue;     /* for queueing up aim and other messages */
630  owl_dict styledict;       /* global dictionary of available styles */
631  char *response;           /* response to the last question asked */
632  int havezephyr;
633  int haveaim;
634  int ignoreaimlogin;
635  volatile sig_atomic_t got_err_signal; /* 1 if we got an unexpected signal */
636  volatile siginfo_t err_signal_info;
637  owl_zbuddylist zbuddies;
638  owl_timer zephyr_buddycheck_timer;
639  GList *zaldlist;
640  int pseudologin_notify;
641  struct termios startup_tio;
642  owl_list io_dispatch_list;
643  owl_list psa_list;
644  GList *timerlist;
645  owl_timer *aim_nop_timer;
646  int load_initial_subs;
647  volatile sig_atomic_t interrupted;
648  FILE *debug_file;
649} owl_global;
650
651/* globals */
652extern owl_global g;
653
654#include "owl_prototypes.h"
655
656/* these are missing from the zephyr includes for some reason */
657#ifdef HAVE_LIBZEPHYR
658int ZGetSubscriptions(ZSubscription_t *, int *);
659int ZGetLocations(ZLocations_t *,int *);
660#endif
661
662#endif /* INC_BARNOWL_OWL_H */
Note: See TracBrowser for help on using the repository browser.