source: global.c @ bd3f232

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