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 | |
---|
11 | static void _owl_global_init_windows(owl_global *g); |
---|
12 | |
---|
13 | void owl_global_init(owl_global *g) { |
---|
14 | char *cd; |
---|
15 | const char *homedir; |
---|
16 | |
---|
17 | g_type_init(); |
---|
18 | g_thread_init(NULL); |
---|
19 | |
---|
20 | owl_select_init(); |
---|
21 | |
---|
22 | g->lines=LINES; |
---|
23 | g->cols=COLS; |
---|
24 | /* We shouldn't need this if we initialize lines and cols before the first |
---|
25 | * owl_window_get_screen, but to be safe, we synchronize. */ |
---|
26 | owl_window_resize(owl_window_get_screen(), g->lines, g->cols); |
---|
27 | |
---|
28 | g->context_stack = NULL; |
---|
29 | owl_global_push_context(g, OWL_CTX_STARTUP, NULL, NULL, NULL); |
---|
30 | |
---|
31 | g->curmsg=0; |
---|
32 | g->topmsg=0; |
---|
33 | g->markedmsgid=-1; |
---|
34 | g->startupargs=NULL; |
---|
35 | |
---|
36 | owl_variable_dict_setup(&(g->vars)); |
---|
37 | |
---|
38 | g->rightshift=0; |
---|
39 | |
---|
40 | g->pw = NULL; |
---|
41 | g->vw = NULL; |
---|
42 | g->tw = NULL; |
---|
43 | |
---|
44 | owl_keyhandler_init(&g->kh); |
---|
45 | owl_keys_setup_keymaps(&g->kh); |
---|
46 | |
---|
47 | owl_dict_create(&(g->filters)); |
---|
48 | g->filterlist = NULL; |
---|
49 | g->puntlist = g_ptr_array_new(); |
---|
50 | g->messagequeue = g_queue_new(); |
---|
51 | owl_dict_create(&(g->styledict)); |
---|
52 | g->curmsg_vert_offset=0; |
---|
53 | g->resizepending=0; |
---|
54 | g->direction=OWL_DIRECTION_DOWNWARDS; |
---|
55 | g->zaway=0; |
---|
56 | owl_fmtext_init_colorpair_mgr(&(g->cpmgr)); |
---|
57 | g->debug=OWL_DEBUG; |
---|
58 | owl_regex_init(&g->search_re); |
---|
59 | g->starttime=time(NULL); /* assumes we call init only a start time */ |
---|
60 | g->lastinputtime=g->starttime; |
---|
61 | g->newmsgproc_pid=0; |
---|
62 | |
---|
63 | owl_global_set_config_format(g, 0); |
---|
64 | owl_global_set_no_have_config(g); |
---|
65 | owl_history_init(&(g->msghist)); |
---|
66 | owl_history_init(&(g->cmdhist)); |
---|
67 | g->nextmsgid=0; |
---|
68 | |
---|
69 | /* Fill in some variables which don't have constant defaults */ |
---|
70 | |
---|
71 | /* glib's g_get_home_dir prefers passwd entries to $HOME, so we |
---|
72 | * explicitly check getenv first. */ |
---|
73 | homedir = getenv("HOME"); |
---|
74 | if (!homedir) |
---|
75 | homedir = g_get_home_dir(); |
---|
76 | g->homedir = g_strdup(homedir); |
---|
77 | |
---|
78 | g->confdir = NULL; |
---|
79 | g->startupfile = NULL; |
---|
80 | cd = g_strdup_printf("%s/%s", g->homedir, OWL_CONFIG_DIR); |
---|
81 | owl_global_set_confdir(g, cd); |
---|
82 | g_free(cd); |
---|
83 | |
---|
84 | owl_messagelist_create(&(g->msglist)); |
---|
85 | |
---|
86 | _owl_global_init_windows(g); |
---|
87 | |
---|
88 | g->aim_screenname=NULL; |
---|
89 | g->aim_screenname_for_filters=NULL; |
---|
90 | g->aim_loggedin=0; |
---|
91 | owl_buddylist_init(&(g->buddylist)); |
---|
92 | |
---|
93 | g->havezephyr=0; |
---|
94 | g->haveaim=0; |
---|
95 | g->ignoreaimlogin=0; |
---|
96 | owl_global_set_no_doaimevents(g); |
---|
97 | |
---|
98 | owl_errqueue_init(&(g->errqueue)); |
---|
99 | |
---|
100 | owl_zbuddylist_create(&(g->zbuddies)); |
---|
101 | |
---|
102 | g->zaldlist = NULL; |
---|
103 | g->pseudologin_notify = 0; |
---|
104 | |
---|
105 | owl_message_init_fmtext_cache(); |
---|
106 | g->kill_buffer = NULL; |
---|
107 | |
---|
108 | g->interrupt_count = 0; |
---|
109 | g->interrupt_lock = g_mutex_new(); |
---|
110 | } |
---|
111 | |
---|
112 | static void _owl_global_init_windows(owl_global *g) |
---|
113 | { |
---|
114 | /* Create the main window */ |
---|
115 | owl_mainpanel_init(&(g->mainpanel)); |
---|
116 | |
---|
117 | /* Create the widgets */ |
---|
118 | owl_mainwin_init(&(g->mw), g->mainpanel.recwin); |
---|
119 | owl_msgwin_init(&(g->msgwin), g->mainpanel.msgwin); |
---|
120 | owl_sepbar_init(g->mainpanel.sepwin); |
---|
121 | |
---|
122 | owl_window_set_default_cursor(g->mainpanel.sepwin); |
---|
123 | |
---|
124 | /* set up a pad for input */ |
---|
125 | g->input_pad = newpad(1, 1); |
---|
126 | nodelay(g->input_pad, 1); |
---|
127 | keypad(g->input_pad, 1); |
---|
128 | meta(g->input_pad, 1); |
---|
129 | } |
---|
130 | |
---|
131 | void owl_global_sepbar_dirty(owl_global *g) |
---|
132 | { |
---|
133 | owl_window_dirty(g->mainpanel.sepwin); |
---|
134 | } |
---|
135 | |
---|
136 | /* Called once perl has been initialized */ |
---|
137 | void owl_global_complete_setup(owl_global *g) |
---|
138 | { |
---|
139 | owl_cmddict_setup(&(g->cmds)); |
---|
140 | } |
---|
141 | |
---|
142 | owl_context *owl_global_get_context(const owl_global *g) { |
---|
143 | if (!g->context_stack) |
---|
144 | return NULL; |
---|
145 | return g->context_stack->data; |
---|
146 | } |
---|
147 | |
---|
148 | static void owl_global_activate_context(owl_global *g, owl_context *c) { |
---|
149 | if (!c) |
---|
150 | return; |
---|
151 | |
---|
152 | if (c->keymap) { |
---|
153 | if (!owl_keyhandler_activate(owl_global_get_keyhandler(g), c->keymap)) { |
---|
154 | owl_function_error("Unable to activate keymap '%s'", c->keymap); |
---|
155 | } |
---|
156 | } |
---|
157 | owl_window_set_cursor(c->cursor); |
---|
158 | } |
---|
159 | |
---|
160 | void owl_global_push_context(owl_global *g, int mode, void *data, const char *keymap, owl_window *cursor) { |
---|
161 | owl_context *c; |
---|
162 | c = owl_context_new(mode, data, keymap, cursor); |
---|
163 | owl_global_push_context_obj(g, c); |
---|
164 | } |
---|
165 | |
---|
166 | void owl_global_push_context_obj(owl_global *g, owl_context *c) |
---|
167 | { |
---|
168 | g->context_stack = g_list_prepend(g->context_stack, c); |
---|
169 | owl_global_activate_context(g, owl_global_get_context(g)); |
---|
170 | } |
---|
171 | |
---|
172 | /* Pops the current context from the context stack and returns it. Caller is |
---|
173 | * responsible for freeing. */ |
---|
174 | CALLER_OWN owl_context *owl_global_pop_context_no_delete(owl_global *g) |
---|
175 | { |
---|
176 | owl_context *c; |
---|
177 | if (!g->context_stack) |
---|
178 | return NULL; |
---|
179 | c = owl_global_get_context(g); |
---|
180 | owl_context_deactivate(c); |
---|
181 | g->context_stack = g_list_delete_link(g->context_stack, |
---|
182 | g->context_stack); |
---|
183 | owl_global_activate_context(g, owl_global_get_context(g)); |
---|
184 | return c; |
---|
185 | } |
---|
186 | |
---|
187 | /* Pops the current context from the context stack and deletes it. */ |
---|
188 | void owl_global_pop_context(owl_global *g) { |
---|
189 | owl_context *c; |
---|
190 | c = owl_global_pop_context_no_delete(g); |
---|
191 | if (c) |
---|
192 | owl_context_delete(c); |
---|
193 | } |
---|
194 | |
---|
195 | int owl_global_get_lines(const owl_global *g) { |
---|
196 | return(g->lines); |
---|
197 | } |
---|
198 | |
---|
199 | int owl_global_get_cols(const owl_global *g) { |
---|
200 | return(g->cols); |
---|
201 | } |
---|
202 | |
---|
203 | int owl_global_get_recwin_lines(const owl_global *g) { |
---|
204 | return g->mainpanel.recwinlines; |
---|
205 | } |
---|
206 | |
---|
207 | /* curmsg */ |
---|
208 | |
---|
209 | int owl_global_get_curmsg(const owl_global *g) { |
---|
210 | return(g->curmsg); |
---|
211 | } |
---|
212 | |
---|
213 | void owl_global_set_curmsg(owl_global *g, int i) { |
---|
214 | g->curmsg=i; |
---|
215 | /* we will reset the vertical offset from here */ |
---|
216 | /* we might want to move this out to the functions later */ |
---|
217 | owl_global_set_curmsg_vert_offset(g, 0); |
---|
218 | } |
---|
219 | |
---|
220 | /* topmsg */ |
---|
221 | |
---|
222 | int owl_global_get_topmsg(const owl_global *g) { |
---|
223 | return(g->topmsg); |
---|
224 | } |
---|
225 | |
---|
226 | void owl_global_set_topmsg(owl_global *g, int i) { |
---|
227 | g->topmsg=i; |
---|
228 | } |
---|
229 | |
---|
230 | /* markedmsgid */ |
---|
231 | |
---|
232 | int owl_global_get_markedmsgid(const owl_global *g) { |
---|
233 | return(g->markedmsgid); |
---|
234 | } |
---|
235 | |
---|
236 | void owl_global_set_markedmsgid(owl_global *g, int i) { |
---|
237 | g->markedmsgid=i; |
---|
238 | /* i; index of message in the current view. |
---|
239 | const owl_message *m; |
---|
240 | owl_view *v; |
---|
241 | |
---|
242 | v = owl_global_get_current_view(&g); |
---|
243 | m = owl_view_get_element(v, i); |
---|
244 | g->markedmsgid = m ? owl_message_get_id(m) : 0; |
---|
245 | */ |
---|
246 | } |
---|
247 | |
---|
248 | /* windows */ |
---|
249 | |
---|
250 | owl_mainwin *owl_global_get_mainwin(owl_global *g) { |
---|
251 | return(&(g->mw)); |
---|
252 | } |
---|
253 | |
---|
254 | owl_popwin *owl_global_get_popwin(owl_global *g) { |
---|
255 | return g->pw; |
---|
256 | } |
---|
257 | |
---|
258 | void owl_global_set_popwin(owl_global *g, owl_popwin *pw) { |
---|
259 | g->pw = pw; |
---|
260 | } |
---|
261 | |
---|
262 | /* msglist */ |
---|
263 | |
---|
264 | owl_messagelist *owl_global_get_msglist(owl_global *g) { |
---|
265 | return(&(g->msglist)); |
---|
266 | } |
---|
267 | |
---|
268 | /* keyhandler */ |
---|
269 | |
---|
270 | owl_keyhandler *owl_global_get_keyhandler(owl_global *g) { |
---|
271 | return(&(g->kh)); |
---|
272 | } |
---|
273 | |
---|
274 | /* Gets the currently active typwin out of the current context. */ |
---|
275 | owl_editwin *owl_global_current_typwin(const owl_global *g) { |
---|
276 | owl_context *ctx = owl_global_get_context(g); |
---|
277 | return owl_editcontext_get_editwin(ctx); |
---|
278 | } |
---|
279 | |
---|
280 | /* variable dictionary */ |
---|
281 | |
---|
282 | owl_vardict *owl_global_get_vardict(owl_global *g) { |
---|
283 | return &(g->vars); |
---|
284 | } |
---|
285 | |
---|
286 | /* command dictionary */ |
---|
287 | |
---|
288 | owl_cmddict *owl_global_get_cmddict(owl_global *g) { |
---|
289 | return &(g->cmds); |
---|
290 | } |
---|
291 | |
---|
292 | /* rightshift */ |
---|
293 | |
---|
294 | void owl_global_set_rightshift(owl_global *g, int i) { |
---|
295 | g->rightshift = i; |
---|
296 | owl_mainwin_redisplay(owl_global_get_mainwin(g)); |
---|
297 | } |
---|
298 | |
---|
299 | int owl_global_get_rightshift(const owl_global *g) { |
---|
300 | return(g->rightshift); |
---|
301 | } |
---|
302 | |
---|
303 | /* typwin */ |
---|
304 | |
---|
305 | owl_editwin *owl_global_set_typwin_active(owl_global *g, int style, owl_history *hist) { |
---|
306 | int d; |
---|
307 | d = owl_global_get_typewindelta(g); |
---|
308 | if (d > 0 && style == OWL_EDITWIN_STYLE_MULTILINE) |
---|
309 | owl_function_resize_typwin(owl_global_get_typwin_lines(g) + d); |
---|
310 | |
---|
311 | if (g->typwin_erase_id) { |
---|
312 | g_signal_handler_disconnect(g->mainpanel.typwin, g->typwin_erase_id); |
---|
313 | g->typwin_erase_id = 0; |
---|
314 | } |
---|
315 | |
---|
316 | g->tw = owl_editwin_new(g->mainpanel.typwin, |
---|
317 | owl_global_get_typwin_lines(g), |
---|
318 | g->cols, |
---|
319 | style, |
---|
320 | hist); |
---|
321 | return g->tw; |
---|
322 | } |
---|
323 | |
---|
324 | void owl_global_deactivate_editcontext(owl_context *ctx) { |
---|
325 | owl_global *g = ctx->cbdata; |
---|
326 | owl_global_set_typwin_inactive(g); |
---|
327 | } |
---|
328 | |
---|
329 | void owl_global_set_typwin_inactive(owl_global *g) { |
---|
330 | int d = owl_global_get_typewindelta(g); |
---|
331 | if (d > 0 && owl_editwin_get_style(g->tw) == OWL_EDITWIN_STYLE_MULTILINE) |
---|
332 | owl_function_resize_typwin(owl_global_get_typwin_lines(g) - d); |
---|
333 | |
---|
334 | if (!g->typwin_erase_id) { |
---|
335 | g->typwin_erase_id = |
---|
336 | g_signal_connect(g->mainpanel.typwin, "redraw", G_CALLBACK(owl_window_erase_cb), NULL); |
---|
337 | } |
---|
338 | owl_window_dirty(g->mainpanel.typwin); |
---|
339 | |
---|
340 | owl_editwin_unref(g->tw); |
---|
341 | g->tw = NULL; |
---|
342 | } |
---|
343 | |
---|
344 | /* resize */ |
---|
345 | |
---|
346 | void owl_global_set_resize_pending(owl_global *g) { |
---|
347 | g->resizepending = true; |
---|
348 | } |
---|
349 | |
---|
350 | const char *owl_global_get_homedir(const owl_global *g) { |
---|
351 | if (g->homedir) return(g->homedir); |
---|
352 | return("/"); |
---|
353 | } |
---|
354 | |
---|
355 | const char *owl_global_get_confdir(const owl_global *g) { |
---|
356 | if (g->confdir) return(g->confdir); |
---|
357 | return("/"); |
---|
358 | } |
---|
359 | |
---|
360 | /* |
---|
361 | * Setting this also sets startupfile to confdir/startup |
---|
362 | */ |
---|
363 | void owl_global_set_confdir(owl_global *g, const char *cd) { |
---|
364 | g_free(g->confdir); |
---|
365 | g->confdir = g_strdup(cd); |
---|
366 | g_free(g->startupfile); |
---|
367 | g->startupfile = g_strdup_printf("%s/startup", cd); |
---|
368 | } |
---|
369 | |
---|
370 | const char *owl_global_get_startupfile(const owl_global *g) { |
---|
371 | if(g->startupfile) return(g->startupfile); |
---|
372 | return("/"); |
---|
373 | } |
---|
374 | |
---|
375 | int owl_global_get_direction(const owl_global *g) { |
---|
376 | return(g->direction); |
---|
377 | } |
---|
378 | |
---|
379 | void owl_global_set_direction_downwards(owl_global *g) { |
---|
380 | g->direction=OWL_DIRECTION_DOWNWARDS; |
---|
381 | } |
---|
382 | |
---|
383 | void owl_global_set_direction_upwards(owl_global *g) { |
---|
384 | g->direction=OWL_DIRECTION_UPWARDS; |
---|
385 | } |
---|
386 | |
---|
387 | /* perl stuff */ |
---|
388 | |
---|
389 | void owl_global_set_perlinterp(owl_global *g, void *p) { |
---|
390 | g->perl=p; |
---|
391 | } |
---|
392 | |
---|
393 | void *owl_global_get_perlinterp(const owl_global *g) { |
---|
394 | return(g->perl); |
---|
395 | } |
---|
396 | |
---|
397 | int owl_global_is_config_format(const owl_global *g) { |
---|
398 | if (g->config_format) return(1); |
---|
399 | return(0); |
---|
400 | } |
---|
401 | |
---|
402 | void owl_global_set_config_format(owl_global *g, int state) { |
---|
403 | if (state==1) { |
---|
404 | g->config_format=1; |
---|
405 | } else { |
---|
406 | g->config_format=0; |
---|
407 | } |
---|
408 | } |
---|
409 | |
---|
410 | void owl_global_set_have_config(owl_global *g) { |
---|
411 | g->haveconfig=1; |
---|
412 | } |
---|
413 | |
---|
414 | void owl_global_set_no_have_config(owl_global *g) { |
---|
415 | g->haveconfig=0; |
---|
416 | } |
---|
417 | |
---|
418 | int owl_global_have_config(owl_global *g) { |
---|
419 | if (g->haveconfig) return(1); |
---|
420 | return(0); |
---|
421 | } |
---|
422 | |
---|
423 | /* |
---|
424 | * Compute the size of the terminal. Try a ioctl, fallback to other stuff on |
---|
425 | * fail. |
---|
426 | */ |
---|
427 | void owl_global_get_terminal_size(int *lines, int *cols) { |
---|
428 | struct winsize size; |
---|
429 | /* get the new size */ |
---|
430 | ioctl(STDIN_FILENO, TIOCGWINSZ, &size); |
---|
431 | if (size.ws_row) { |
---|
432 | *lines = size.ws_row; |
---|
433 | } else { |
---|
434 | *lines = LINES; |
---|
435 | } |
---|
436 | |
---|
437 | if (size.ws_col) { |
---|
438 | *cols = size.ws_col; |
---|
439 | } else { |
---|
440 | *cols = COLS; |
---|
441 | } |
---|
442 | } |
---|
443 | |
---|
444 | void owl_global_check_resize(owl_global *g) { |
---|
445 | /* resize the screen. If lines or cols is 0 use the terminal size */ |
---|
446 | if (!g->resizepending) return; |
---|
447 | g->resizepending = false; |
---|
448 | |
---|
449 | owl_global_get_terminal_size(&g->lines, &g->cols); |
---|
450 | owl_window_resize(owl_window_get_screen(), g->lines, g->cols); |
---|
451 | |
---|
452 | owl_function_debugmsg("New size is %i lines, %i cols.", g->lines, g->cols); |
---|
453 | } |
---|
454 | |
---|
455 | /* debug */ |
---|
456 | |
---|
457 | int owl_global_is_debug_fast(const owl_global *g) { |
---|
458 | if (g->debug) return(1); |
---|
459 | return(0); |
---|
460 | } |
---|
461 | |
---|
462 | /* starttime */ |
---|
463 | |
---|
464 | time_t owl_global_get_starttime(const owl_global *g) { |
---|
465 | return(g->starttime); |
---|
466 | } |
---|
467 | |
---|
468 | time_t owl_global_get_runtime(const owl_global *g) { |
---|
469 | return(time(NULL)-g->starttime); |
---|
470 | } |
---|
471 | |
---|
472 | time_t owl_global_get_lastinputtime(const owl_global *g) { |
---|
473 | return(g->lastinputtime); |
---|
474 | } |
---|
475 | |
---|
476 | void owl_global_set_lastinputtime(owl_global *g, time_t time) { |
---|
477 | g->lastinputtime = time; |
---|
478 | } |
---|
479 | |
---|
480 | time_t owl_global_get_idletime(const owl_global *g) { |
---|
481 | return(time(NULL)-g->lastinputtime); |
---|
482 | } |
---|
483 | |
---|
484 | /* viewwin */ |
---|
485 | |
---|
486 | owl_viewwin *owl_global_get_viewwin(owl_global *g) { |
---|
487 | return g->vw; |
---|
488 | } |
---|
489 | |
---|
490 | void owl_global_set_viewwin(owl_global *g, owl_viewwin *vw) { |
---|
491 | g->vw = vw; |
---|
492 | } |
---|
493 | |
---|
494 | |
---|
495 | /* vert offset */ |
---|
496 | |
---|
497 | int owl_global_get_curmsg_vert_offset(const owl_global *g) { |
---|
498 | return(g->curmsg_vert_offset); |
---|
499 | } |
---|
500 | |
---|
501 | void owl_global_set_curmsg_vert_offset(owl_global *g, int i) { |
---|
502 | g->curmsg_vert_offset = i; |
---|
503 | } |
---|
504 | |
---|
505 | /* startup args */ |
---|
506 | |
---|
507 | void owl_global_set_startupargs(owl_global *g, int argc, char **argv) { |
---|
508 | g_free(g->startupargs); |
---|
509 | g->startupargs = g_strjoinv(" ", argv); |
---|
510 | } |
---|
511 | |
---|
512 | const char *owl_global_get_startupargs(const owl_global *g) { |
---|
513 | if (g->startupargs) return(g->startupargs); |
---|
514 | return(""); |
---|
515 | } |
---|
516 | |
---|
517 | /* history */ |
---|
518 | |
---|
519 | owl_history *owl_global_get_msg_history(owl_global *g) { |
---|
520 | return(&(g->msghist)); |
---|
521 | } |
---|
522 | |
---|
523 | owl_history *owl_global_get_cmd_history(owl_global *g) { |
---|
524 | return(&(g->cmdhist)); |
---|
525 | } |
---|
526 | |
---|
527 | /* filterlist */ |
---|
528 | typedef struct _owl_global_filter_ent { /* noproto */ |
---|
529 | owl_global *g; |
---|
530 | owl_filter *f; |
---|
531 | } owl_global_filter_ent; |
---|
532 | |
---|
533 | owl_filter *owl_global_get_filter(const owl_global *g, const char *name) { |
---|
534 | owl_global_filter_ent *e = owl_dict_find_element(&(g->filters), name); |
---|
535 | if (e) return e->f; |
---|
536 | return NULL; |
---|
537 | } |
---|
538 | |
---|
539 | static void owl_global_delete_filter_ent(void *data) |
---|
540 | { |
---|
541 | owl_global_filter_ent *e = data; |
---|
542 | e->g->filterlist = g_list_remove(e->g->filterlist, e->f); |
---|
543 | owl_filter_delete(e->f); |
---|
544 | g_free(e); |
---|
545 | } |
---|
546 | |
---|
547 | void owl_global_add_filter(owl_global *g, owl_filter *f) { |
---|
548 | owl_global_filter_ent *e = g_new(owl_global_filter_ent, 1); |
---|
549 | e->g = g; |
---|
550 | e->f = f; |
---|
551 | |
---|
552 | owl_dict_insert_element(&(g->filters), owl_filter_get_name(f), |
---|
553 | e, owl_global_delete_filter_ent); |
---|
554 | g->filterlist = g_list_append(g->filterlist, f); |
---|
555 | } |
---|
556 | |
---|
557 | void owl_global_remove_filter(owl_global *g, const char *name) { |
---|
558 | owl_global_filter_ent *e = owl_dict_remove_element(&(g->filters), name); |
---|
559 | if (e) |
---|
560 | owl_global_delete_filter_ent(e); |
---|
561 | } |
---|
562 | |
---|
563 | /* nextmsgid */ |
---|
564 | |
---|
565 | int owl_global_get_nextmsgid(owl_global *g) { |
---|
566 | return(g->nextmsgid++); |
---|
567 | } |
---|
568 | |
---|
569 | /* current view */ |
---|
570 | |
---|
571 | owl_view *owl_global_get_current_view(owl_global *g) { |
---|
572 | return(&(g->current_view)); |
---|
573 | } |
---|
574 | |
---|
575 | owl_colorpair_mgr *owl_global_get_colorpair_mgr(owl_global *g) { |
---|
576 | return(&(g->cpmgr)); |
---|
577 | } |
---|
578 | |
---|
579 | /* puntlist */ |
---|
580 | |
---|
581 | GPtrArray *owl_global_get_puntlist(owl_global *g) { |
---|
582 | return g->puntlist; |
---|
583 | } |
---|
584 | |
---|
585 | int owl_global_message_is_puntable(owl_global *g, const owl_message *m) { |
---|
586 | const GPtrArray *pl; |
---|
587 | int i; |
---|
588 | |
---|
589 | pl = owl_global_get_puntlist(g); |
---|
590 | for (i = 0; i < pl->len; i++) { |
---|
591 | if (owl_filter_message_match(pl->pdata[i], m)) return 1; |
---|
592 | } |
---|
593 | return 0; |
---|
594 | } |
---|
595 | |
---|
596 | int owl_global_should_followlast(owl_global *g) { |
---|
597 | const owl_view *v; |
---|
598 | |
---|
599 | if (!owl_global_is__followlast(g)) return(0); |
---|
600 | |
---|
601 | v=owl_global_get_current_view(g); |
---|
602 | |
---|
603 | if (owl_global_get_curmsg(g)==owl_view_get_size(v)-1) return(1); |
---|
604 | return(0); |
---|
605 | } |
---|
606 | |
---|
607 | int owl_global_is_search_active(const owl_global *g) { |
---|
608 | if (owl_regex_is_set(&g->search_re)) return(1); |
---|
609 | return(0); |
---|
610 | } |
---|
611 | |
---|
612 | void owl_global_set_search_re(owl_global *g, const owl_regex *re) { |
---|
613 | if (owl_regex_is_set(&g->search_re)) { |
---|
614 | owl_regex_cleanup(&g->search_re); |
---|
615 | owl_regex_init(&g->search_re); |
---|
616 | } |
---|
617 | if (re != NULL) |
---|
618 | owl_regex_copy(re, &g->search_re); |
---|
619 | /* TODO: Emit a signal so we don't depend on the viewwin and mainwin */ |
---|
620 | if (owl_global_get_viewwin(g)) |
---|
621 | owl_viewwin_dirty(owl_global_get_viewwin(g)); |
---|
622 | owl_mainwin_redisplay(owl_global_get_mainwin(g)); |
---|
623 | } |
---|
624 | |
---|
625 | const owl_regex *owl_global_get_search_re(const owl_global *g) { |
---|
626 | return &g->search_re; |
---|
627 | } |
---|
628 | |
---|
629 | void owl_global_set_newmsgproc_pid(owl_global *g, pid_t i) { |
---|
630 | g->newmsgproc_pid=i; |
---|
631 | } |
---|
632 | |
---|
633 | pid_t owl_global_get_newmsgproc_pid(const owl_global *g) { |
---|
634 | return(g->newmsgproc_pid); |
---|
635 | } |
---|
636 | |
---|
637 | /* AIM stuff */ |
---|
638 | |
---|
639 | int owl_global_is_aimloggedin(const owl_global *g) |
---|
640 | { |
---|
641 | if (g->aim_loggedin) return(1); |
---|
642 | return(0); |
---|
643 | } |
---|
644 | |
---|
645 | const char *owl_global_get_aim_screenname(const owl_global *g) |
---|
646 | { |
---|
647 | if (owl_global_is_aimloggedin(g)) { |
---|
648 | return (g->aim_screenname); |
---|
649 | } |
---|
650 | return(""); |
---|
651 | } |
---|
652 | |
---|
653 | const char *owl_global_get_aim_screenname_for_filters(const owl_global *g) |
---|
654 | { |
---|
655 | if (owl_global_is_aimloggedin(g)) { |
---|
656 | return (g->aim_screenname_for_filters); |
---|
657 | } |
---|
658 | return(""); |
---|
659 | } |
---|
660 | |
---|
661 | void owl_global_set_aimloggedin(owl_global *g, const char *screenname) |
---|
662 | { |
---|
663 | char *sn_escaped; |
---|
664 | g->aim_loggedin=1; |
---|
665 | if (g->aim_screenname) g_free(g->aim_screenname); |
---|
666 | if (g->aim_screenname_for_filters) g_free(g->aim_screenname_for_filters); |
---|
667 | g->aim_screenname=g_strdup(screenname); |
---|
668 | sn_escaped = owl_text_quote(screenname, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH); |
---|
669 | g->aim_screenname_for_filters = owl_arg_quote(sn_escaped); |
---|
670 | g_free(sn_escaped); |
---|
671 | } |
---|
672 | |
---|
673 | void owl_global_set_aimnologgedin(owl_global *g) |
---|
674 | { |
---|
675 | g->aim_loggedin=0; |
---|
676 | } |
---|
677 | |
---|
678 | bool owl_global_is_doaimevents(const owl_global *g) |
---|
679 | { |
---|
680 | return g->aim_event_source != NULL; |
---|
681 | } |
---|
682 | |
---|
683 | void owl_global_set_doaimevents(owl_global *g) |
---|
684 | { |
---|
685 | if (g->aim_event_source) |
---|
686 | return; |
---|
687 | g->aim_event_source = owl_aim_event_source_new(owl_global_get_aimsess(g)); |
---|
688 | g_source_attach(g->aim_event_source, NULL); |
---|
689 | } |
---|
690 | |
---|
691 | void owl_global_set_no_doaimevents(owl_global *g) |
---|
692 | { |
---|
693 | if (!g->aim_event_source) |
---|
694 | return; |
---|
695 | g_source_destroy(g->aim_event_source); |
---|
696 | g_source_unref(g->aim_event_source); |
---|
697 | g->aim_event_source = NULL; |
---|
698 | } |
---|
699 | |
---|
700 | aim_session_t *owl_global_get_aimsess(owl_global *g) |
---|
701 | { |
---|
702 | return(&(g->aimsess)); |
---|
703 | } |
---|
704 | |
---|
705 | aim_conn_t *owl_global_get_bosconn(owl_global *g) |
---|
706 | { |
---|
707 | return(&(g->bosconn)); |
---|
708 | } |
---|
709 | |
---|
710 | void owl_global_set_bossconn(owl_global *g, aim_conn_t *conn) |
---|
711 | { |
---|
712 | g->bosconn=*conn; |
---|
713 | } |
---|
714 | |
---|
715 | /* message queue */ |
---|
716 | |
---|
717 | void owl_global_messagequeue_addmsg(owl_global *g, owl_message *m) |
---|
718 | { |
---|
719 | g_queue_push_tail(g->messagequeue, m); |
---|
720 | } |
---|
721 | |
---|
722 | /* pop off the first message and return it. Return NULL if the queue |
---|
723 | * is empty. The caller should free the message after using it, if |
---|
724 | * necessary. |
---|
725 | */ |
---|
726 | owl_message CALLER_OWN *owl_global_messagequeue_popmsg(owl_global *g) |
---|
727 | { |
---|
728 | owl_message *out; |
---|
729 | |
---|
730 | if (g_queue_is_empty(g->messagequeue)) |
---|
731 | return NULL; |
---|
732 | out = g_queue_pop_head(g->messagequeue); |
---|
733 | return out; |
---|
734 | } |
---|
735 | |
---|
736 | int owl_global_messagequeue_pending(owl_global *g) |
---|
737 | { |
---|
738 | return !g_queue_is_empty(g->messagequeue); |
---|
739 | } |
---|
740 | |
---|
741 | owl_buddylist *owl_global_get_buddylist(owl_global *g) |
---|
742 | { |
---|
743 | return(&(g->buddylist)); |
---|
744 | } |
---|
745 | |
---|
746 | /* style */ |
---|
747 | |
---|
748 | /* Return the style with name 'name'. If it does not exist return |
---|
749 | * NULL */ |
---|
750 | const owl_style *owl_global_get_style_by_name(const owl_global *g, const char *name) |
---|
751 | { |
---|
752 | return owl_dict_find_element(&(g->styledict), name); |
---|
753 | } |
---|
754 | |
---|
755 | CALLER_OWN GPtrArray *owl_global_get_style_names(const owl_global *g) |
---|
756 | { |
---|
757 | return owl_dict_get_keys(&g->styledict); |
---|
758 | } |
---|
759 | |
---|
760 | void owl_global_add_style(owl_global *g, owl_style *s) |
---|
761 | { |
---|
762 | /* |
---|
763 | * If we're redefining the current style, make sure to update |
---|
764 | * pointers to it. |
---|
765 | */ |
---|
766 | if(g->current_view.style |
---|
767 | && !strcmp(owl_style_get_name(g->current_view.style), |
---|
768 | owl_style_get_name(s))) |
---|
769 | g->current_view.style = s; |
---|
770 | owl_dict_insert_element(&(g->styledict), owl_style_get_name(s), |
---|
771 | s, (void (*)(void *))owl_style_delete); |
---|
772 | } |
---|
773 | |
---|
774 | void owl_global_set_haveaim(owl_global *g) |
---|
775 | { |
---|
776 | g->haveaim=1; |
---|
777 | } |
---|
778 | |
---|
779 | int owl_global_is_haveaim(const owl_global *g) |
---|
780 | { |
---|
781 | if (g->haveaim) return(1); |
---|
782 | return(0); |
---|
783 | } |
---|
784 | |
---|
785 | void owl_global_set_ignore_aimlogin(owl_global *g) |
---|
786 | { |
---|
787 | g->ignoreaimlogin = 1; |
---|
788 | } |
---|
789 | |
---|
790 | void owl_global_unset_ignore_aimlogin(owl_global *g) |
---|
791 | { |
---|
792 | g->ignoreaimlogin = 0; |
---|
793 | } |
---|
794 | |
---|
795 | int owl_global_is_ignore_aimlogin(const owl_global *g) |
---|
796 | { |
---|
797 | return g->ignoreaimlogin; |
---|
798 | } |
---|
799 | |
---|
800 | void owl_global_set_havezephyr(owl_global *g) |
---|
801 | { |
---|
802 | g->havezephyr=1; |
---|
803 | } |
---|
804 | |
---|
805 | int owl_global_is_havezephyr(const owl_global *g) |
---|
806 | { |
---|
807 | if (g->havezephyr) return(1); |
---|
808 | return(0); |
---|
809 | } |
---|
810 | |
---|
811 | owl_errqueue *owl_global_get_errqueue(owl_global *g) |
---|
812 | { |
---|
813 | return(&(g->errqueue)); |
---|
814 | } |
---|
815 | |
---|
816 | owl_zbuddylist *owl_global_get_zephyr_buddylist(owl_global *g) |
---|
817 | { |
---|
818 | return(&(g->zbuddies)); |
---|
819 | } |
---|
820 | |
---|
821 | GList **owl_global_get_zaldlist(owl_global *g) |
---|
822 | { |
---|
823 | return &(g->zaldlist); |
---|
824 | } |
---|
825 | |
---|
826 | int owl_global_get_pseudologin_notify(owl_global *g) |
---|
827 | { |
---|
828 | return g->pseudologin_notify; |
---|
829 | } |
---|
830 | |
---|
831 | void owl_global_set_pseudologin_notify(owl_global *g, int notify) |
---|
832 | { |
---|
833 | g->pseudologin_notify = notify; |
---|
834 | } |
---|
835 | |
---|
836 | struct termios *owl_global_get_startup_tio(owl_global *g) |
---|
837 | { |
---|
838 | return(&(g->startup_tio)); |
---|
839 | } |
---|
840 | |
---|
841 | void owl_global_setup_default_filters(owl_global *g) |
---|
842 | { |
---|
843 | int i; |
---|
844 | static const struct { |
---|
845 | const char *name; |
---|
846 | const char *desc; |
---|
847 | } filters[] = { |
---|
848 | { "personal", |
---|
849 | "isprivate ^true$ and ( not type ^zephyr$ or ( class ^message ) )" }, |
---|
850 | { "trash", |
---|
851 | "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )" }, |
---|
852 | { "wordwrap", "not ( type ^admin$ or type ^zephyr$ )" }, |
---|
853 | { "ping", "opcode ^ping$" }, |
---|
854 | { "auto", "opcode ^auto$" }, |
---|
855 | { "login", "not login ^none$" }, |
---|
856 | { "reply-lockout", "class ^noc or class ^mail$" }, |
---|
857 | { "out", "direction ^out$" }, |
---|
858 | { "aim", "type ^aim$" }, |
---|
859 | { "zephyr", "type ^zephyr$" }, |
---|
860 | { "none", "false" }, |
---|
861 | { "all", "true" }, |
---|
862 | { NULL, NULL } |
---|
863 | }; |
---|
864 | |
---|
865 | owl_function_debugmsg("startup: creating default filters"); |
---|
866 | |
---|
867 | for (i = 0; filters[i].name != NULL; i++) |
---|
868 | owl_global_add_filter(g, owl_filter_new_fromstring(filters[i].name, |
---|
869 | filters[i].desc)); |
---|
870 | } |
---|
871 | |
---|
872 | FILE *owl_global_get_debug_file_handle(owl_global *g) { |
---|
873 | static char *open_file = NULL; |
---|
874 | const char *filename = owl_global_get_debug_file(g); |
---|
875 | if (g->debug_file == NULL || |
---|
876 | (open_file && strcmp(filename, open_file) != 0)) { |
---|
877 | char *path; |
---|
878 | int fd; |
---|
879 | |
---|
880 | if (g->debug_file) |
---|
881 | fclose(g->debug_file); |
---|
882 | |
---|
883 | g->debug_file = NULL; |
---|
884 | |
---|
885 | path = g_strdup_printf("%s.%d", filename, getpid()); |
---|
886 | fd = open(path, O_CREAT|O_WRONLY|O_EXCL, 0600); |
---|
887 | g_free(path); |
---|
888 | |
---|
889 | if (fd >= 0) |
---|
890 | g->debug_file = fdopen(fd, "a"); |
---|
891 | |
---|
892 | g_free(open_file); |
---|
893 | open_file = g_strdup(filename); |
---|
894 | } |
---|
895 | return g->debug_file; |
---|
896 | } |
---|
897 | |
---|
898 | const char *owl_global_get_kill_buffer(owl_global *g) { |
---|
899 | return g->kill_buffer; |
---|
900 | } |
---|
901 | |
---|
902 | void owl_global_set_kill_buffer(owl_global *g, const char *kill, int len) { |
---|
903 | g_free(g->kill_buffer); |
---|
904 | g->kill_buffer = g_strndup(kill, len); |
---|
905 | } |
---|
906 | |
---|
907 | void owl_global_add_interrupt(owl_global *g) { |
---|
908 | /* TODO: This can almost certainly be done with atomic |
---|
909 | * operations. Whatever. */ |
---|
910 | g_mutex_lock(g->interrupt_lock); |
---|
911 | g->interrupt_count++; |
---|
912 | g_mutex_unlock(g->interrupt_lock); |
---|
913 | } |
---|
914 | |
---|
915 | bool owl_global_take_interrupt(owl_global *g) { |
---|
916 | bool ans = false; |
---|
917 | g_mutex_lock(g->interrupt_lock); |
---|
918 | if (g->interrupt_count > 0) { |
---|
919 | ans = true; |
---|
920 | g->interrupt_count--; |
---|
921 | } |
---|
922 | g_mutex_unlock(g->interrupt_lock); |
---|
923 | return ans; |
---|
924 | } |
---|