source: global.c @ 09489b89

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