source: global.c @ 52f3507

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