[ecf8fa8] | 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., 59 Temple Place, |
---|
| 25 | Suite 330, Boston, MA 02111-1307 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 { |
---|
[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 | { |
---|
[851a8c2] | 52 | if( loop == NULL ) |
---|
| 53 | loop = g_main_new( FALSE ); |
---|
[ba9edaa] | 54 | } |
---|
| 55 | |
---|
| 56 | void b_main_run() |
---|
| 57 | { |
---|
| 58 | g_main_run( loop ); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | void b_main_quit() |
---|
| 62 | { |
---|
| 63 | g_main_quit( loop ); |
---|
| 64 | } |
---|
| 65 | |
---|
[ecf8fa8] | 66 | static gboolean gaim_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data) |
---|
| 67 | { |
---|
| 68 | GaimIOClosure *closure = data; |
---|
[ba9edaa] | 69 | b_input_condition gaim_cond = 0; |
---|
[fc2ee0f] | 70 | gboolean st; |
---|
[9ff0c25] | 71 | |
---|
| 72 | if (condition & G_IO_NVAL) |
---|
| 73 | return FALSE; |
---|
[ecf8fa8] | 74 | |
---|
| 75 | if (condition & GAIM_READ_COND) |
---|
[e046390] | 76 | gaim_cond |= B_EV_IO_READ; |
---|
[ecf8fa8] | 77 | if (condition & GAIM_WRITE_COND) |
---|
[e046390] | 78 | gaim_cond |= B_EV_IO_WRITE; |
---|
[fc2ee0f] | 79 | |
---|
| 80 | event_debug( "gaim_io_invoke( %d, %d, 0x%x )\n", g_io_channel_unix_get_fd(source), condition, data ); |
---|
[ecf8fa8] | 81 | |
---|
[fc2ee0f] | 82 | st = closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond); |
---|
| 83 | |
---|
| 84 | if( !st ) |
---|
| 85 | event_debug( "Returned FALSE, cancelling.\n" ); |
---|
| 86 | |
---|
[c5c18c1] | 87 | if (closure->flags & B_EV_FLAG_FORCE_ONCE) |
---|
| 88 | return FALSE; |
---|
| 89 | else if (closure->flags & B_EV_FLAG_FORCE_REPEAT) |
---|
| 90 | return TRUE; |
---|
| 91 | else |
---|
| 92 | return st; |
---|
[ecf8fa8] | 93 | } |
---|
| 94 | |
---|
| 95 | static void gaim_io_destroy(gpointer data) |
---|
| 96 | { |
---|
[fc2ee0f] | 97 | event_debug( "gaim_io_destroy( 0x%x )\n", data ); |
---|
[ecf8fa8] | 98 | g_free(data); |
---|
| 99 | } |
---|
| 100 | |
---|
[ba9edaa] | 101 | gint b_input_add(gint source, b_input_condition condition, b_event_handler function, gpointer data) |
---|
[ecf8fa8] | 102 | { |
---|
| 103 | GaimIOClosure *closure = g_new0(GaimIOClosure, 1); |
---|
| 104 | GIOChannel *channel; |
---|
| 105 | GIOCondition cond = 0; |
---|
[f394500] | 106 | int st; |
---|
[ecf8fa8] | 107 | |
---|
| 108 | closure->function = function; |
---|
| 109 | closure->data = data; |
---|
[c5c18c1] | 110 | closure->flags = condition; |
---|
[ecf8fa8] | 111 | |
---|
[e046390] | 112 | if (condition & B_EV_IO_READ) |
---|
[ecf8fa8] | 113 | cond |= GAIM_READ_COND; |
---|
[e046390] | 114 | if (condition & B_EV_IO_WRITE) |
---|
[ecf8fa8] | 115 | cond |= GAIM_WRITE_COND; |
---|
| 116 | |
---|
| 117 | channel = g_io_channel_unix_new(source); |
---|
[f394500] | 118 | st = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, |
---|
| 119 | gaim_io_invoke, closure, gaim_io_destroy); |
---|
[ecf8fa8] | 120 | |
---|
[f394500] | 121 | event_debug( "b_input_add( %d, %d, 0x%x, 0x%x ) = %d (%p)\n", source, condition, function, data, st, closure ); |
---|
[fc2ee0f] | 122 | |
---|
[ecf8fa8] | 123 | g_io_channel_unref(channel); |
---|
[f394500] | 124 | return st; |
---|
[ecf8fa8] | 125 | } |
---|
| 126 | |
---|
[ba9edaa] | 127 | gint b_timeout_add(gint timeout, b_event_handler func, gpointer data) |
---|
| 128 | { |
---|
[04026d4] | 129 | /* GSourceFunc and the BitlBee event handler function aren't |
---|
| 130 | really the same, but they're "compatible". ;-) It will do |
---|
| 131 | for now, BitlBee only looks at the "data" argument. */ |
---|
| 132 | gint st = g_timeout_add(timeout, (GSourceFunc) func, data); |
---|
[fc2ee0f] | 133 | |
---|
| 134 | event_debug( "b_timeout_add( %d, %d, %d ) = %d\n", timeout, func, data, st ); |
---|
| 135 | |
---|
| 136 | return st; |
---|
[ba9edaa] | 137 | } |
---|
| 138 | |
---|
| 139 | void b_event_remove(gint tag) |
---|
[ecf8fa8] | 140 | { |
---|
[782d988] | 141 | event_debug( "b_event_remove( %d )\n", tag ); |
---|
[fc2ee0f] | 142 | |
---|
[ecf8fa8] | 143 | if (tag > 0) |
---|
| 144 | g_source_remove(tag); |
---|
| 145 | } |
---|
[25b5a4a] | 146 | |
---|
| 147 | void closesocket( int fd ) |
---|
| 148 | { |
---|
| 149 | close( fd ); |
---|
| 150 | } |
---|