source: global.c @ 75e3879

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