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 | |
---|
50 | void b_main_init() |
---|
51 | { |
---|
52 | if (loop == NULL) { |
---|
53 | loop = g_main_loop_new(NULL, FALSE); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | void b_main_run() |
---|
58 | { |
---|
59 | g_main_loop_run(loop); |
---|
60 | } |
---|
61 | |
---|
62 | void b_main_quit() |
---|
63 | { |
---|
64 | g_main_loop_quit(loop); |
---|
65 | } |
---|
66 | |
---|
67 | void b_main_iteration() |
---|
68 | { |
---|
69 | g_main_context_iteration(NULL, FALSE); |
---|
70 | event_debug("b_main_iteration()\n"); |
---|
71 | } |
---|
72 | |
---|
73 | static gboolean gaim_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data) |
---|
74 | { |
---|
75 | GaimIOClosure *closure = data; |
---|
76 | b_input_condition gaim_cond = 0; |
---|
77 | gboolean st; |
---|
78 | |
---|
79 | if (condition & G_IO_NVAL) { |
---|
80 | return FALSE; |
---|
81 | } |
---|
82 | |
---|
83 | if (condition & GAIM_READ_COND) { |
---|
84 | gaim_cond |= B_EV_IO_READ; |
---|
85 | } |
---|
86 | if (condition & GAIM_WRITE_COND) { |
---|
87 | gaim_cond |= B_EV_IO_WRITE; |
---|
88 | } |
---|
89 | |
---|
90 | event_debug("gaim_io_invoke( %d, %d, %p )\n", g_io_channel_unix_get_fd(source), condition, data); |
---|
91 | |
---|
92 | st = closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond); |
---|
93 | |
---|
94 | if (!st) { |
---|
95 | event_debug("Returned FALSE, cancelling.\n"); |
---|
96 | } |
---|
97 | |
---|
98 | if (closure->flags & B_EV_FLAG_FORCE_ONCE) { |
---|
99 | return FALSE; |
---|
100 | } else if (closure->flags & B_EV_FLAG_FORCE_REPEAT) { |
---|
101 | return TRUE; |
---|
102 | } else { |
---|
103 | return st; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | static void gaim_io_destroy(gpointer data) |
---|
108 | { |
---|
109 | event_debug("gaim_io_destroy( 0%p )\n", data); |
---|
110 | g_free(data); |
---|
111 | } |
---|
112 | |
---|
113 | gint b_input_add(gint source, b_input_condition condition, b_event_handler function, gpointer data) |
---|
114 | { |
---|
115 | GaimIOClosure *closure = g_new0(GaimIOClosure, 1); |
---|
116 | GIOChannel *channel; |
---|
117 | GIOCondition cond = 0; |
---|
118 | int st; |
---|
119 | |
---|
120 | closure->function = function; |
---|
121 | closure->data = data; |
---|
122 | closure->flags = condition; |
---|
123 | |
---|
124 | if (condition & B_EV_IO_READ) { |
---|
125 | cond |= GAIM_READ_COND; |
---|
126 | } |
---|
127 | if (condition & B_EV_IO_WRITE) { |
---|
128 | cond |= GAIM_WRITE_COND; |
---|
129 | } |
---|
130 | |
---|
131 | channel = g_io_channel_unix_new(source); |
---|
132 | st = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, |
---|
133 | gaim_io_invoke, closure, gaim_io_destroy); |
---|
134 | |
---|
135 | event_debug("b_input_add( %d, %d, %p, %p ) = %d (%p)\n", source, condition, function, data, st, closure); |
---|
136 | |
---|
137 | g_io_channel_unref(channel); |
---|
138 | return st; |
---|
139 | } |
---|
140 | |
---|
141 | gint b_timeout_add(gint timeout, b_event_handler func, gpointer data) |
---|
142 | { |
---|
143 | /* GSourceFunc and the BitlBee event handler function aren't |
---|
144 | really the same, but they're "compatible". ;-) It will do |
---|
145 | for now, BitlBee only looks at the "data" argument. */ |
---|
146 | gint st = g_timeout_add(timeout, (GSourceFunc) func, data); |
---|
147 | |
---|
148 | event_debug("b_timeout_add( %d, %p, %p ) = %d\n", timeout, func, data, st); |
---|
149 | |
---|
150 | return st; |
---|
151 | } |
---|
152 | |
---|
153 | void b_event_remove(gint tag) |
---|
154 | { |
---|
155 | event_debug("b_event_remove( %d )\n", tag); |
---|
156 | |
---|
157 | if (tag > 0) { |
---|
158 | g_source_remove(tag); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | void closesocket(int fd) |
---|
163 | { |
---|
164 | close(fd); |
---|
165 | } |
---|