source: global.c @ 6a415e9

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