source: global.c @ f933403

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