1 /********************************************************************\
2   * BitlBee -- An IRC to other IM-networks gateway                     *
3   *                                                                    *
4   * Copyright 2002-2006 Wilmer van der Gaast and others                *
5   \********************************************************************/
6 
7 /*
8  * Event handling (using GLib)
9  */
10 
11 /*
12   This program is free software; you can redistribute it and/or modify
13   it under the terms of the GNU General Public License as published by
14   the Free Software Foundation; either version 2 of the License, or
15   (at your option) any later version.
16 
17   This program is distributed in the hope that it will be useful,
18   but WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   GNU General Public License for more details.
21 
22   You should have received a copy of the GNU General Public License with
23   the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
24   if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
25   Fifth Floor, Boston, MA  02110-1301  USA
26 */
27 
28 #define BITLBEE_CORE
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netdb.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include "proxy.h"
41 
42 typedef struct _GaimIOClosure {
43 	b_event_handler function;
44 	gpointer data;
45 	guint flags;
46 } GaimIOClosure;
47 
48 static GMainLoop *loop = NULL;
49 
b_main_init()50 void b_main_init()
51 {
52 	if (loop == NULL) {
53 		loop = g_main_new(FALSE);
54 	}
55 }
56 
b_main_run()57 void b_main_run()
58 {
59 	g_main_run(loop);
60 }
61 
b_main_quit()62 void b_main_quit()
63 {
64 	g_main_quit(loop);
65 }
66 
gaim_io_invoke(GIOChannel * source,GIOCondition condition,gpointer data)67 static gboolean gaim_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
68 {
69 	GaimIOClosure *closure = data;
70 	b_input_condition gaim_cond = 0;
71 	gboolean st;
72 
73 	if (condition & G_IO_NVAL) {
74 		return FALSE;
75 	}
76 
77 	if (condition & GAIM_READ_COND) {
78 		gaim_cond |= B_EV_IO_READ;
79 	}
80 	if (condition & GAIM_WRITE_COND) {
81 		gaim_cond |= B_EV_IO_WRITE;
82 	}
83 
84 	event_debug("gaim_io_invoke( %d, %d, %p )\n", g_io_channel_unix_get_fd(source), condition, data);
85 
86 	st = closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond);
87 
88 	if (!st) {
89 		event_debug("Returned FALSE, cancelling.\n");
90 	}
91 
92 	if (closure->flags & B_EV_FLAG_FORCE_ONCE) {
93 		return FALSE;
94 	} else if (closure->flags & B_EV_FLAG_FORCE_REPEAT) {
95 		return TRUE;
96 	} else {
97 		return st;
98 	}
99 }
100 
gaim_io_destroy(gpointer data)101 static void gaim_io_destroy(gpointer data)
102 {
103 	event_debug("gaim_io_destroy( 0%p )\n", data);
104 	g_free(data);
105 }
106 
b_input_add(gint source,b_input_condition condition,b_event_handler function,gpointer data)107 gint b_input_add(gint source, b_input_condition condition, b_event_handler function, gpointer data)
108 {
109 	GaimIOClosure *closure = g_new0(GaimIOClosure, 1);
110 	GIOChannel *channel;
111 	GIOCondition cond = 0;
112 	int st;
113 
114 	closure->function = function;
115 	closure->data = data;
116 	closure->flags = condition;
117 
118 	if (condition & B_EV_IO_READ) {
119 		cond |= GAIM_READ_COND;
120 	}
121 	if (condition & B_EV_IO_WRITE) {
122 		cond |= GAIM_WRITE_COND;
123 	}
124 
125 	channel = g_io_channel_unix_new(source);
126 	st = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
127 	                         gaim_io_invoke, closure, gaim_io_destroy);
128 
129 	event_debug("b_input_add( %d, %d, %p, %p ) = %d (%p)\n", source, condition, function, data, st, closure);
130 
131 	g_io_channel_unref(channel);
132 	return st;
133 }
134 
b_timeout_add(gint timeout,b_event_handler func,gpointer data)135 gint b_timeout_add(gint timeout, b_event_handler func, gpointer data)
136 {
137 	/* GSourceFunc and the BitlBee event handler function aren't
138 	   really the same, but they're "compatible". ;-) It will do
139 	   for now, BitlBee only looks at the "data" argument. */
140 	gint st = g_timeout_add(timeout, (GSourceFunc) func, data);
141 
142 	event_debug("b_timeout_add( %d, %p, %p ) = %d\n", timeout, func, data, st);
143 
144 	return st;
145 }
146 
b_event_remove(gint tag)147 void b_event_remove(gint tag)
148 {
149 	event_debug("b_event_remove( %d )\n", tag);
150 
151 	if (tag > 0) {
152 		g_source_remove(tag);
153 	}
154 }
155 
closesocket(int fd)156 void closesocket(int fd)
157 {
158 	close(fd);
159 }
160