source: global.c @ 2a2bb60

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 2a2bb60 was ec6ff52, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
Added the 'show errors' command Added the error queue Converted most makemsg's to error instead Started writing an internal VT style (far from done)
  • Property mode set to 100644
File size: 17.2 KB
Line 
1#include <stdio.h>
2#include <unistd.h>
3#include <stdlib.h>
4#include <string.h>
5#include <netdb.h>
6#include <termios.h>
7#include <sys/ioctl.h>
8#include <time.h>
9#include "owl.h"
10
11static const char fileIdent[] = "$Id$";
12
13#ifndef MAXHOSTNAMELEN
14#define MAXHOSTNAMELEN 256
15#endif
16
17void owl_global_init(owl_global *g) {
18  struct hostent *hent;
19  char hostname[MAXHOSTNAMELEN];
20
21  g->malloced=0;
22  g->freed=0;
23
24  gethostname(hostname, MAXHOSTNAMELEN);
25  hent=gethostbyname(hostname);
26  if (!hent) {
27    g->thishost=owl_strdup("localhost");
28  } else {
29    g->thishost=owl_strdup(hent->h_name);
30  }
31
32  owl_context_init(&g->ctx);
33  owl_context_set_startup(&g->ctx);
34  g->curmsg=0;
35  g->topmsg=0;
36  g->needrefresh=1;
37  g->startupargs=NULL;
38
39  owl_variable_dict_setup(&(g->vars));
40  owl_cmddict_setup(&(g->cmds));
41
42  g->lines=LINES;
43  g->cols=COLS;
44
45  g->rightshift=0;
46
47  owl_editwin_init(&(g->tw), NULL, owl_global_get_typwin_lines(g), g->cols, OWL_EDITWIN_STYLE_ONELINE, NULL);
48
49  owl_keyhandler_init(&g->kh);
50  owl_keys_setup_keymaps(&g->kh);
51
52  owl_list_create(&(g->filterlist));
53  owl_list_create(&(g->puntlist));
54  owl_list_create(&(g->messagequeue));
55  owl_dict_create(&(g->styledict));
56  g->curmsg_vert_offset=0;
57  g->resizepending=0;
58  g->typwinactive=0;
59  g->direction=OWL_DIRECTION_DOWNWARDS;
60  g->zaway=0;
61  if (has_colors()) {
62    g->hascolors=1;
63  }
64  g->colorpairs=COLOR_PAIRS;
65  g->debug=OWL_DEBUG;
66  g->searchactive=0;
67  g->searchstring=NULL;
68  g->starttime=time(NULL); /* assumes we call init only a start time */
69  g->buffercommand=NULL;
70  g->newmsgproc_pid=0;
71 
72  owl_global_set_config_format(g, 0);
73  owl_global_set_userclue(g, OWL_USERCLUE_NONE);
74  owl_global_set_no_have_config(g);
75  owl_history_init(&(g->msghist));
76  owl_history_init(&(g->cmdhist));
77  g->nextmsgid=0;
78
79  owl_filterelement_create_true(&(g->fe_true));
80  owl_filterelement_create_false(&(g->fe_false));
81  owl_filterelement_create_null(&(g->fe_null));
82
83  _owl_global_setup_windows(g);
84
85  /* Fill in some variables which don't have constant defaults */
86  /* TODO: come back later and check passwd file first */
87  g->homedir=owl_strdup(getenv("HOME"));
88
89  owl_messagelist_create(&(g->msglist));
90  owl_mainwin_init(&(g->mw));
91  owl_popwin_init(&(g->pw));
92
93  g->aim_screenname=NULL;
94  g->aim_loggedin=0;
95  owl_timer_create_countdown(&(g->aim_noop_timer), 30);
96  owl_timer_create_countdown(&(g->aim_ignorelogin_timer), 0);
97  owl_timer_create_countdown(&(g->aim_buddyinfo_timer), 60);
98  owl_buddylist_init(&(g->buddylist));
99   
100  g->response=NULL;
101  g->havezephyr=0;
102  g->haveaim=0;
103  owl_global_set_no_doaimevents(g);
104
105  owl_errqueue_init(&(g->errqueue));
106}
107
108void _owl_global_setup_windows(owl_global *g) {
109  int cols, typwin_lines;
110
111  cols=g->cols;
112  typwin_lines=owl_global_get_typwin_lines(g);
113
114  /* set the new window sizes */
115  g->recwinlines=g->lines-(typwin_lines+2);
116  if (g->recwinlines<1) {
117    /* this will screw things up.  I'm not sure what to do yet,
118       but this is better than nothing */
119    /* g->recwinlines=1; */
120  }
121
122  /* create the new windows */
123  g->recwin=newwin(g->recwinlines, cols, 0, 0);
124  if (g->recwin==NULL) {
125    owl_function_debugmsg("\n\nI just received an error on creating a new receive window\n");
126    owl_function_debugmsg("newwin was called with arguments (%i, %i, 0, 0) and returned NULL\n",
127           g->recwinlines, cols);
128    endwin();
129
130    exit(50);
131  }
132     
133  g->sepwin=newwin(1, cols, g->recwinlines, 0);
134  g->msgwin=newwin(1, cols, g->recwinlines+1, 0);
135  g->typwin=newwin(typwin_lines, cols, g->recwinlines+2, 0);
136
137  owl_editwin_set_curswin(&(g->tw), g->typwin, typwin_lines, g->cols);
138
139  idlok(g->typwin, FALSE);
140  idlok(g->recwin, FALSE);
141  idlok(g->sepwin, FALSE);
142  idlok(g->msgwin, FALSE);
143
144  nodelay(g->typwin, 1);
145  keypad(g->typwin, TRUE);
146  wmove(g->typwin, 0, 0);
147
148  meta(g->typwin, TRUE);
149}
150
151owl_context *owl_global_get_context(owl_global *g) {
152  return(&g->ctx);
153}
154                         
155int owl_global_get_lines(owl_global *g) {
156  return(g->lines);
157}
158
159int owl_global_get_cols(owl_global *g) {
160  return(g->cols);
161}
162
163int owl_global_get_recwin_lines(owl_global *g) {
164  return(g->recwinlines);
165}
166
167/* curmsg */
168
169int owl_global_get_curmsg(owl_global *g) {
170  return(g->curmsg);
171}
172
173void owl_global_set_curmsg(owl_global *g, int i) {
174  g->curmsg=i;
175  /* we will reset the vertical offset from here */
176  /* we might want to move this out to the functions later */
177  owl_global_set_curmsg_vert_offset(g, 0);
178}
179
180/* topmsg */
181
182int owl_global_get_topmsg(owl_global *g) {
183  return(g->topmsg);
184}
185
186void owl_global_set_topmsg(owl_global *g, int i) {
187  g->topmsg=i;
188}
189
190/* windows */
191
192owl_mainwin *owl_global_get_mainwin(owl_global *g) {
193  return(&(g->mw));
194}
195
196owl_popwin *owl_global_get_popwin(owl_global *g) {
197  return(&(g->pw));
198}
199
200/* msglist */
201
202owl_messagelist *owl_global_get_msglist(owl_global *g) {
203  return(&(g->msglist));
204}
205
206/* keyhandler */
207
208owl_keyhandler *owl_global_get_keyhandler(owl_global *g) {
209  return(&(g->kh));
210}
211
212/* curses windows */
213
214WINDOW *owl_global_get_curs_recwin(owl_global *g) {
215  return(g->recwin);
216}
217
218WINDOW *owl_global_get_curs_sepwin(owl_global *g) {
219  return(g->sepwin);
220}
221
222WINDOW *owl_global_get_curs_msgwin(owl_global *g) {
223  return(g->msgwin);
224}
225
226WINDOW *owl_global_get_curs_typwin(owl_global *g) {
227  return(g->typwin);
228}
229
230/* typwin */
231
232owl_editwin *owl_global_get_typwin(owl_global *g) {
233  return(&(g->tw));
234}
235
236/* buffercommand */
237
238void owl_global_set_buffercommand(owl_global *g, char *command) {
239  if (g->buffercommand) owl_free(g->buffercommand);
240  g->buffercommand=owl_strdup(command);
241}
242
243char *owl_global_get_buffercommand(owl_global *g) {
244  if (g->buffercommand) return(g->buffercommand);
245  return("");
246}
247
248/* refresh */
249
250int owl_global_is_needrefresh(owl_global *g) {
251  if (g->needrefresh==1) return(1);
252  return(0);
253}
254
255void owl_global_set_needrefresh(owl_global *g) {
256  g->needrefresh=1;
257}
258
259void owl_global_set_noneedrefresh(owl_global *g) {
260  g->needrefresh=0;
261}
262
263/* variable dictionary */
264
265owl_vardict *owl_global_get_vardict(owl_global *g) {
266  return &(g->vars);
267}
268
269/* command dictionary */
270
271owl_cmddict *owl_global_get_cmddict(owl_global *g) {
272  return &(g->cmds);
273}
274
275/* rightshift */
276
277void owl_global_set_rightshift(owl_global *g, int i) {
278  g->rightshift=i;
279}
280
281int owl_global_get_rightshift(owl_global *g) {
282  return(g->rightshift);
283}
284
285/* typwin */
286
287int owl_global_is_typwin_active(owl_global *g) {
288  if (g->typwinactive==1) return(1);
289  return(0);
290}
291
292void owl_global_set_typwin_active(owl_global *g) {
293  g->typwinactive=1;
294}
295
296void owl_global_set_typwin_inactive(owl_global *g) {
297  g->typwinactive=0;
298}
299
300/* resize */
301
302void owl_global_set_resize_pending(owl_global *g) {
303  g->resizepending=1;
304}
305
306char *owl_global_get_homedir(owl_global *g) {
307  if (g->homedir) return(g->homedir);
308  return("/");
309}
310
311int owl_global_get_direction(owl_global *g) {
312  return(g->direction);
313}
314
315void owl_global_set_direction_downwards(owl_global *g) {
316  g->direction=OWL_DIRECTION_DOWNWARDS;
317}
318
319void owl_global_set_direction_upwards(owl_global *g) {
320  g->direction=OWL_DIRECTION_UPWARDS;
321}
322
323/* perl stuff */
324
325void owl_global_set_perlinterp(owl_global *g, void *p) {
326  g->perl=p;
327}
328
329void *owl_global_get_perlinterp(owl_global *g) {
330  return(g->perl);
331}
332
333int owl_global_is_config_format(owl_global *g) {
334  if (g->config_format) return(1);
335  return(0);
336}
337
338void owl_global_set_config_format(owl_global *g, int state) {
339  if (state==1) {
340    g->config_format=1;
341  } else {
342    g->config_format=0;
343  }
344}
345
346void owl_global_set_have_config(owl_global *g) {
347  g->haveconfig=1;
348}
349
350void owl_global_set_no_have_config(owl_global *g) {
351  g->haveconfig=0;
352}
353
354int owl_global_have_config(owl_global *g) {
355  if (g->haveconfig) return(1);
356  return(0);
357}
358
359void owl_global_resize(owl_global *g, int x, int y) {
360  /* resize the screen.  If x or y is 0 use the terminal size */
361  struct winsize size;
362   
363  if (!g->resizepending) return;
364
365  /* delete the current windows */
366  delwin(g->recwin);
367  delwin(g->sepwin);
368  delwin(g->msgwin);
369  delwin(g->typwin);
370  if (!isendwin()) {
371    endwin();
372  }
373
374  refresh();
375
376  /* get the new size */
377  ioctl(STDIN_FILENO, TIOCGWINSZ, &size);
378  if (x==0) {
379    g->lines=size.ws_row;
380  } else {
381    g->lines=x;
382  }
383
384  if (y==0) {
385    g->cols=size.ws_col;
386  } else {
387    g->cols=y;
388  }
389
390  /* resizeterm(size.ws_row, size.ws_col); */
391
392  /* re-initialize the windows */
393  _owl_global_setup_windows(g);
394
395  /* in case any styles rely on the current width */
396  owl_messagelist_invalidate_formats(owl_global_get_msglist(g));
397
398  /* refresh stuff */
399  g->needrefresh=1;
400  owl_mainwin_redisplay(&(g->mw));
401  sepbar(NULL);
402
403  if (owl_global_is_typwin_active(g)) {
404    owl_editwin_redisplay(&(g->tw), 0);
405  }     
406  /* TODO: this should handle other forms of popwins */
407  if (owl_popwin_is_active(owl_global_get_popwin(g)) 
408      && owl_global_get_viewwin(g)) {
409    owl_popwin_refresh(owl_global_get_popwin(g));
410    owl_viewwin_redisplay(owl_global_get_viewwin(g), 0);
411  }
412
413  owl_function_debugmsg("New size is %i lines, %i cols.", size.ws_row, size.ws_col);
414  owl_function_makemsg("");
415  g->resizepending=0;
416}
417
418/* debug */
419
420int owl_global_is_debug_fast(owl_global *g) {
421  if (g->debug) return(1);
422  return(0);
423}
424
425/* starttime */
426
427time_t owl_global_get_starttime(owl_global *g) {
428  return(g->starttime);
429}
430
431time_t owl_global_get_runtime(owl_global *g) {
432  return(time(NULL)-g->starttime);
433}
434
435void owl_global_get_runtime_string(owl_global *g, char *buff) {
436  time_t diff;
437
438  diff=time(NULL)-owl_global_get_starttime(g);
439
440  /* print something nicer later */   
441  sprintf(buff, "%i seconds", (int) diff);
442}
443
444char *owl_global_get_hostname(owl_global *g) {
445  if (g->thishost) return(g->thishost);
446  return("");
447}
448
449/* userclue */
450
451void owl_global_set_userclue(owl_global *g, int clue) {
452  g->userclue=clue;
453}
454
455void owl_global_add_userclue(owl_global *g, int clue) {
456  g->userclue|=clue;
457}
458
459int owl_global_get_userclue(owl_global *g) {
460  return(g->userclue);
461}
462
463int owl_global_is_userclue(owl_global *g, int clue) {
464  if (g->userclue & clue) return(1);
465  return(0);
466}
467
468/* viewwin */
469
470owl_viewwin *owl_global_get_viewwin(owl_global *g) {
471  return(&(g->vw));
472}
473
474
475/* vert offset */
476
477int owl_global_get_curmsg_vert_offset(owl_global *g) {
478  return(g->curmsg_vert_offset);
479}
480
481void owl_global_set_curmsg_vert_offset(owl_global *g, int i) {
482  g->curmsg_vert_offset=i;
483}
484
485/* startup args */
486
487void owl_global_set_startupargs(owl_global *g, int argc, char **argv) {
488  int i, len;
489
490  if (g->startupargs) owl_free(g->startupargs);
491 
492  len=0;
493  for (i=0; i<argc; i++) {
494    len+=strlen(argv[i])+5;
495  }
496  g->startupargs=malloc(len+5);
497
498  strcpy(g->startupargs, "");
499  for (i=0; i<argc; i++) {
500    sprintf(g->startupargs, "%s%s ", g->startupargs, argv[i]);
501  }
502  g->startupargs[strlen(g->startupargs)-1]='\0';
503}
504
505char *owl_global_get_startupargs(owl_global *g) {
506  if (g->startupargs) return(g->startupargs);
507  return("");
508}
509
510/* history */
511
512owl_history *owl_global_get_msg_history(owl_global *g) {
513  return(&(g->msghist));
514}
515
516owl_history *owl_global_get_cmd_history(owl_global *g) {
517  return(&(g->cmdhist));
518}
519
520/* filterlist */
521
522owl_list *owl_global_get_filterlist(owl_global *g) {
523  return(&(g->filterlist));
524}
525
526owl_filter *owl_global_get_filter(owl_global *g, char *name) {
527  int i, j;
528  owl_filter *f;
529
530  j=owl_list_get_size(&(g->filterlist));
531  for (i=0; i<j; i++) {
532    f=owl_list_get_element(&(g->filterlist), i);
533    if (!strcmp(name, owl_filter_get_name(f))) {
534      return(f);
535    }
536  }
537  return(NULL);
538}
539
540void owl_global_add_filter(owl_global *g, owl_filter *f) {
541  owl_list_append_element(&(g->filterlist), f);
542}
543
544void owl_global_remove_filter(owl_global *g, char *name) {
545  int i, j;
546  owl_filter *f;
547
548  j=owl_list_get_size(&(g->filterlist));
549  for (i=0; i<j; i++) {
550    f=owl_list_get_element(&(g->filterlist), i);
551    if (!strcmp(name, owl_filter_get_name(f))) {
552      owl_filter_free(f);
553      owl_list_remove_element(&(g->filterlist), i);
554      break;
555    }
556  }
557}
558
559/* nextmsgid */
560
561int owl_global_get_nextmsgid(owl_global *g) {
562  return(g->nextmsgid++);
563}
564
565/* current view */
566
567owl_view *owl_global_get_current_view(owl_global *g) {
568  return(&(g->current_view));
569}
570
571owl_filterelement *owl_global_get_filterelement_true(owl_global *g) {
572  return(&(g->fe_true));
573}
574
575owl_filterelement *owl_global_get_filterelement_false(owl_global *g) {
576  return(&(g->fe_false));
577}
578
579owl_filterelement *owl_global_get_filterelement_null(owl_global *g) {
580  return(&(g->fe_null));
581}
582
583/* has colors */
584
585int owl_global_get_hascolors(owl_global *g) {
586  if (g->hascolors) return(1);
587  return(0);
588}
589
590/* color pairs */
591
592int owl_global_get_colorpairs(owl_global *g) {
593  return(g->colorpairs);
594}
595
596/* puntlist */
597
598owl_list *owl_global_get_puntlist(owl_global *g) {
599  return(&(g->puntlist));
600}
601
602int owl_global_message_is_puntable(owl_global *g, owl_message *m) {
603  owl_list *pl;
604  int i, j;
605
606  pl=owl_global_get_puntlist(g);
607  j=owl_list_get_size(pl);
608  for (i=0; i<j; i++) {
609    if (owl_filter_message_match(owl_list_get_element(pl, i), m)) return(1);
610  }
611  return(0);
612}
613
614int owl_global_should_followlast(owl_global *g) {
615  owl_view *v;
616 
617  if (!owl_global_is__followlast(g)) return(0);
618 
619  v=owl_global_get_current_view(g);
620 
621  if (owl_global_get_curmsg(g)==owl_view_get_size(v)-1) return(1);
622  return(0);
623}
624
625int owl_global_is_search_active(owl_global *g) {
626  if (g->searchactive) return(1);
627  return(0);
628}
629
630void owl_global_set_search_active(owl_global *g, char *string) {
631  g->searchactive=1;
632  if (g->searchstring != NULL) owl_free(g->searchstring);
633  g->searchstring=owl_strdup(string);
634}
635
636void owl_global_set_search_inactive(owl_global *g) {
637  g->searchactive=0;
638}
639
640char *owl_global_get_search_string(owl_global *g) {
641  if (g->searchstring==NULL) return("");
642  return(g->searchstring);
643}
644
645void owl_global_set_newmsgproc_pid(owl_global *g, int i) {
646  g->newmsgproc_pid=i;
647}
648
649int owl_global_get_newmsgproc_pid(owl_global *g) {
650  return(g->newmsgproc_pid);
651}
652
653void owl_global_add_to_malloced(owl_global *g, int i) {
654  g->malloced+=i;
655}
656
657void owl_global_add_to_freed(owl_global *g, int i) {
658  g->freed+=1;
659}
660
661int owl_global_get_malloced(owl_global *g) {
662  return(g->malloced);
663}
664
665int owl_global_get_freed(owl_global *g) {
666  return(g->freed);
667}
668
669int owl_global_get_meminuse(owl_global *g) {
670  return(g->malloced-g->freed);
671}
672
673/* AIM stuff */
674
675int owl_global_is_aimloggedin(owl_global *g)
676{
677  if (g->aim_loggedin) return(1);
678  return(0);
679}
680
681char *owl_global_get_aim_screenname(owl_global *g)
682{
683  if (owl_global_is_aimloggedin(g)) {
684    return (g->aim_screenname);
685  }
686  return("");
687}
688
689void owl_global_set_aimloggedin(owl_global *g, char *screenname)
690{
691  g->aim_loggedin=1;
692  if (g->aim_screenname) owl_free(g->aim_screenname);
693  g->aim_screenname=owl_strdup(screenname);
694}
695
696void owl_global_set_aimnologgedin(owl_global *g)
697{
698  g->aim_loggedin=0;
699}
700
701int owl_global_is_doaimevents(owl_global *g)
702{
703  if (g->aim_doprocessing) return(1);
704  return(0);
705}
706
707void owl_global_set_doaimevents(owl_global *g)
708{
709  g->aim_doprocessing=1;
710}
711
712void owl_global_set_no_doaimevents(owl_global *g)
713{
714  g->aim_doprocessing=0;
715}
716
717
718aim_session_t *owl_global_get_aimsess(owl_global *g)
719{
720  return(&(g->aimsess));
721}
722
723aim_conn_t *owl_global_get_waitingconn(owl_global *g)
724{
725  return(&(g->waitingconn));
726}
727
728int owl_global_is_aimnop_time(owl_global *g)
729{
730  if (owl_timer_is_expired(&(g->aim_noop_timer))) return(1);
731  return(0);
732}
733
734void owl_global_aimnop_sent(owl_global *g)
735{
736  owl_timer_reset(&(g->aim_noop_timer));
737}
738
739owl_timer *owl_global_get_aim_login_timer(owl_global *g)
740{
741  return(&(g->aim_ignorelogin_timer));
742}
743
744/* message queue */
745
746void owl_global_messagequeue_addmsg(owl_global *g, owl_message *m)
747{
748  owl_list_append_element(&(g->messagequeue), m);
749}
750
751/* pop off the first message and return it.  Return NULL if the queue
752 * is empty.  The caller should free the message after using it, if
753 * necessary.
754 */
755owl_message *owl_global_messageuque_popmsg(owl_global *g)
756{
757  owl_message *out;
758
759  if (owl_list_get_size(&(g->messagequeue))==0) return(NULL);
760  out=owl_list_get_element(&(g->messagequeue), 0);
761  owl_list_remove_element(&(g->messagequeue), 0);
762  return(out);
763}
764
765int owl_global_messagequeue_pending(owl_global *g)
766{
767  if (owl_list_get_size(&(g->messagequeue))==0) return(0);
768  return(1);
769}
770
771owl_buddylist *owl_global_get_buddylist(owl_global *g)
772{
773  return(&(g->buddylist));
774}
775 
776/* style */
777
778/* Return the style with name 'name'.  If it does not exist return
779 * NULL */
780owl_style *owl_global_get_style_by_name(owl_global *g, char *name)
781{
782  return owl_dict_find_element(&(g->styledict), name);
783}
784
785/* creates a list and fills it in with keys.  duplicates the keys,
786 * so they will need to be freed by the caller. */
787int owl_global_get_style_names(owl_global *g, owl_list *l) {
788  return owl_dict_get_keys(&(g->styledict), l);
789}
790
791void owl_global_add_style(owl_global *g, owl_style *s)
792{
793  owl_dict_insert_element(&(g->styledict), owl_style_get_name(s), 
794                          s, (void(*)(void*))owl_style_free);
795}
796
797char *owl_global_get_response(owl_global *g)
798{
799  if (g->response==NULL) return("");
800  return(g->response);
801}
802
803void owl_global_set_response(owl_global *g, char *resp)
804{
805  if (g->response) owl_free(g->response);
806  g->response=owl_strdup(resp);
807}
808
809
810void owl_global_set_haveaim(owl_global *g)
811{
812  g->haveaim=1;
813}
814
815int owl_global_is_haveaim(owl_global *g)
816{
817  if (g->haveaim) return(1);
818  return(0);
819}
820
821void owl_global_set_havezephyr(owl_global *g)
822{
823  g->havezephyr=1;
824}
825
826int owl_global_is_havezephyr(owl_global *g)
827{
828  if (g->havezephyr) return(1);
829  return(0);
830}
831
832owl_timer *owl_global_get_aim_buddyinfo_timer(owl_global *g)
833{
834  return(&(g->aim_buddyinfo_timer));
835}
836
837owl_errqueue *owl_global_get_errqueue(owl_global *g)
838{
839  return(&(g->errqueue));
840}
Note: See TracBrowser for help on using the repository browser.