source: global.c @ cf83b7a

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