[5ebff60] | 1 | /********************************************************************\ |
---|
[ecf8fa8] | 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; |
---|
[6f10697] | 24 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 25 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[ecf8fa8] | 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 { |
---|
[ba9edaa] | 43 | b_event_handler function; |
---|
[ecf8fa8] | 44 | gpointer data; |
---|
[c5c18c1] | 45 | guint flags; |
---|
[ecf8fa8] | 46 | } GaimIOClosure; |
---|
| 47 | |
---|
[851a8c2] | 48 | static GMainLoop *loop = NULL; |
---|
[ba9edaa] | 49 | |
---|
| 50 | void b_main_init() |
---|
| 51 | { |
---|
[5ebff60] | 52 | if (loop == NULL) { |
---|
| 53 | loop = g_main_new(FALSE); |
---|
| 54 | } |
---|
[ba9edaa] | 55 | } |
---|
| 56 | |
---|
| 57 | void b_main_run() |
---|
| 58 | { |
---|
[5ebff60] | 59 | g_main_run(loop); |
---|
[ba9edaa] | 60 | } |
---|
| 61 | |
---|
| 62 | void b_main_quit() |
---|
| 63 | { |
---|
[5ebff60] | 64 | g_main_quit(loop); |
---|
[ba9edaa] | 65 | } |
---|
| 66 | |
---|
[ecf8fa8] | 67 | static gboolean gaim_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data) |
---|
| 68 | { |
---|
| 69 | GaimIOClosure *closure = data; |
---|
[ba9edaa] | 70 | b_input_condition gaim_cond = 0; |
---|
[fc2ee0f] | 71 | gboolean st; |
---|
[5ebff60] | 72 | |
---|
| 73 | if (condition & G_IO_NVAL) { |
---|
[9ff0c25] | 74 | return FALSE; |
---|
[5ebff60] | 75 | } |
---|
[ecf8fa8] | 76 | |
---|
[5ebff60] | 77 | if (condition & GAIM_READ_COND) { |
---|
[e046390] | 78 | gaim_cond |= B_EV_IO_READ; |
---|
[5ebff60] | 79 | } |
---|
| 80 | if (condition & GAIM_WRITE_COND) { |
---|
[e046390] | 81 | gaim_cond |= B_EV_IO_WRITE; |
---|
[5ebff60] | 82 | } |
---|
| 83 | |
---|
| 84 | event_debug("gaim_io_invoke( %d, %d, 0x%x )\n", g_io_channel_unix_get_fd(source), condition, data); |
---|
[ecf8fa8] | 85 | |
---|
[fc2ee0f] | 86 | st = closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond); |
---|
[5ebff60] | 87 | |
---|
| 88 | if (!st) { |
---|
| 89 | event_debug("Returned FALSE, cancelling.\n"); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | if (closure->flags & B_EV_FLAG_FORCE_ONCE) { |
---|
[c5c18c1] | 93 | return FALSE; |
---|
[5ebff60] | 94 | } else if (closure->flags & B_EV_FLAG_FORCE_REPEAT) { |
---|
[c5c18c1] | 95 | return TRUE; |
---|
[5ebff60] | 96 | } else { |
---|
[c5c18c1] | 97 | return st; |
---|
[5ebff60] | 98 | } |
---|
[ecf8fa8] | 99 | } |
---|
| 100 | |
---|
| 101 | static void gaim_io_destroy(gpointer data) |
---|
| 102 | { |
---|
[5ebff60] | 103 | event_debug("gaim_io_destroy( 0x%x )\n", data); |
---|
[ecf8fa8] | 104 | g_free(data); |
---|
| 105 | } |
---|
| 106 | |
---|
[ba9edaa] | 107 | gint b_input_add(gint source, b_input_condition condition, b_event_handler function, gpointer data) |
---|
[ecf8fa8] | 108 | { |
---|
| 109 | GaimIOClosure *closure = g_new0(GaimIOClosure, 1); |
---|
| 110 | GIOChannel *channel; |
---|
| 111 | GIOCondition cond = 0; |
---|
[f394500] | 112 | int st; |
---|
[5ebff60] | 113 | |
---|
[ecf8fa8] | 114 | closure->function = function; |
---|
| 115 | closure->data = data; |
---|
[c5c18c1] | 116 | closure->flags = condition; |
---|
[5ebff60] | 117 | |
---|
| 118 | if (condition & B_EV_IO_READ) { |
---|
[ecf8fa8] | 119 | cond |= GAIM_READ_COND; |
---|
[5ebff60] | 120 | } |
---|
| 121 | if (condition & B_EV_IO_WRITE) { |
---|
[ecf8fa8] | 122 | cond |= GAIM_WRITE_COND; |
---|
[5ebff60] | 123 | } |
---|
| 124 | |
---|
[ecf8fa8] | 125 | channel = g_io_channel_unix_new(source); |
---|
[f394500] | 126 | st = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, |
---|
| 127 | gaim_io_invoke, closure, gaim_io_destroy); |
---|
[5ebff60] | 128 | |
---|
| 129 | event_debug("b_input_add( %d, %d, 0x%x, 0x%x ) = %d (%p)\n", source, condition, function, data, st, closure); |
---|
| 130 | |
---|
[ecf8fa8] | 131 | g_io_channel_unref(channel); |
---|
[f394500] | 132 | return st; |
---|
[ecf8fa8] | 133 | } |
---|
| 134 | |
---|
[ba9edaa] | 135 | gint b_timeout_add(gint timeout, b_event_handler func, gpointer data) |
---|
| 136 | { |
---|
[04026d4] | 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); |
---|
[5ebff60] | 141 | |
---|
| 142 | event_debug("b_timeout_add( %d, %d, %d ) = %d\n", timeout, func, data, st); |
---|
| 143 | |
---|
[fc2ee0f] | 144 | return st; |
---|
[ba9edaa] | 145 | } |
---|
| 146 | |
---|
| 147 | void b_event_remove(gint tag) |
---|
[ecf8fa8] | 148 | { |
---|
[5ebff60] | 149 | event_debug("b_event_remove( %d )\n", tag); |
---|
| 150 | |
---|
| 151 | if (tag > 0) { |
---|
[ecf8fa8] | 152 | g_source_remove(tag); |
---|
[5ebff60] | 153 | } |
---|
[ecf8fa8] | 154 | } |
---|
[25b5a4a] | 155 | |
---|
[5ebff60] | 156 | void closesocket(int fd) |
---|
[25b5a4a] | 157 | { |
---|
[5ebff60] | 158 | close(fd); |
---|
[25b5a4a] | 159 | } |
---|