Index: Makefile
===================================================================
--- Makefile	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ Makefile	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -10,6 +10,6 @@
 
 # Program variables
-objects = account.o bitlbee.o chat.o crypting.o help.o ipc.o irc.o irc_commands.o nick.o query.o root_commands.o set.o storage.o $(STORAGE_OBJS) user.o
-headers = account.h bitlbee.h commands.h conf.h config.h crypting.h help.h ipc.h irc.h log.h nick.h query.h set.h sock.h storage.h user.h lib/events.h lib/http_client.h lib/ini.h lib/md5.h lib/misc.h lib/proxy.h lib/sha1.h lib/ssl_client.h lib/url.h protocols/nogaim.h
+objects = account.o bitlbee.o chat.o crypting.o dcc.o help.o ipc.o irc.o irc_commands.o nick.o query.o root_commands.o set.o storage.o $(STORAGE_OBJS) user.o
+headers = account.h bitlbee.h commands.h conf.h config.h crypting.h help.h ipc.h irc.h log.h nick.h query.h set.h sock.h storage.h user.h lib/events.h lib/ftutil.h lib/http_client.h lib/ini.h lib/md5.h lib/misc.h lib/proxy.h lib/sha1.h lib/ssl_client.h lib/url.h protocols/ft.h protocols/nogaim.h
 subdirs = lib protocols
 
Index: bitlbee.c
===================================================================
--- bitlbee.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ bitlbee.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -121,5 +121,5 @@
 	}
 	
-	global.listen_watch_source_id = b_input_add( global.listen_socket, GAIM_INPUT_READ, bitlbee_io_new_client, NULL );
+	global.listen_watch_source_id = b_input_add( global.listen_socket, B_EV_IO_READ, bitlbee_io_new_client, NULL );
 	
 #ifndef _WIN32
@@ -321,5 +321,5 @@
 			child->pid = client_pid;
 			child->ipc_fd = fds[0];
-			child->ipc_inpa = b_input_add( child->ipc_fd, GAIM_INPUT_READ, ipc_master_read, child );
+			child->ipc_inpa = b_input_add( child->ipc_fd, B_EV_IO_READ, ipc_master_read, child );
 			child_list = g_slist_append( child_list, child );
 			
@@ -349,5 +349,5 @@
 			/* We can store the IPC fd there now. */
 			global.listen_socket = fds[1];
-			global.listen_watch_source_id = b_input_add( fds[1], GAIM_INPUT_READ, ipc_child_read, irc );
+			global.listen_watch_source_id = b_input_add( fds[1], B_EV_IO_READ, ipc_child_read, irc );
 			
 			close( fds[0] );
Index: conf.c
===================================================================
--- conf.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ conf.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -63,4 +63,7 @@
 	conf->ping_timeout = 300;
 	conf->user = NULL;
+	conf->ft_max_size = SIZE_MAX;
+	conf->ft_max_kbps = G_MAXUINT;
+	conf->ft_listen = NULL;
 	conf->protocols = NULL;
 	proxytype = 0;
@@ -315,4 +318,28 @@
 				conf->user = g_strdup( ini->value );
 			}
+			else if( g_strcasecmp( ini->key, "ft_max_size" ) == 0 )
+			{
+				size_t ft_max_size;
+				if( sscanf( ini->value, "%zu", &ft_max_size ) != 1 )
+				{
+					fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value );
+					return 0;
+				}
+				conf->ft_max_size = ft_max_size;
+			}
+			else if( g_strcasecmp( ini->key, "ft_max_kbps" ) == 0 )
+			{
+				if( sscanf( ini->value, "%d", &i ) != 1 )
+				{
+					fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value );
+					return 0;
+				}
+				conf->ft_max_kbps = i;
+			}
+			else if( g_strcasecmp( ini->key, "ft_listen" ) == 0 )
+			{
+				g_free( conf->ft_listen );
+				conf->ft_listen = g_strdup( ini->value );
+			}
 			else if( g_strcasecmp( ini->key, "protocols" ) == 0 )
 			{
Index: conf.h
===================================================================
--- conf.h	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ conf.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -50,4 +50,7 @@
 	int ping_timeout;
 	char *user;
+	size_t ft_max_size;
+	int ft_max_kbps;
+	char *ft_listen;
 	char **protocols;
 } conf_t;
Index: configure
===================================================================
--- configure	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ configure	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -27,4 +27,5 @@
 yahoo=1
 twitter=1
+purple=0
 
 debug=0
@@ -68,4 +69,6 @@
 --yahoo=0/1	Disable/enable Yahoo part		$yahoo
 --twitter=0/1 Disable/enable Twitter part		$twitter
+
+--purple=0/1	Disable/enable libpurple support	$purple
 
 --debug=0/1	Disable/enable debugging		$debug
@@ -508,4 +511,28 @@
 protoobjs=''
 
+if [ "$purple" = 0 ]; then
+	echo '#undef WITH_PURPLE' >> config.h
+else
+	if ! $PKG_CONFIG purple; then
+		echo
+		echo 'Cannot find libpurple development libraries, aborting. (Install libpurple-dev?)'
+		exit 1
+	fi
+	echo '#define WITH_PURPLE' >> config.h
+	cat<<EOF>>Makefile.settings
+EFLAGS += $($PKG_CONFIG purple --libs)
+PURPLE_CFLAGS += $($PKG_CONFIG purple --cflags)
+EOF
+	protocols=$protocols'purple '
+	protoobjs=$protoobjs'purple_mod.o '
+
+	# Having both libpurple and native IM modules in one binary may
+	# do strange things. Let's not do that.
+	msn=0
+	jabber=0
+	oscar=0
+	yahoo=0
+fi
+
 if [ "$msn" = 0 ]; then
 	echo '#undef WITH_MSN' >> config.h
Index: dcc.c
===================================================================
--- dcc.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
+++ dcc.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -0,0 +1,638 @@
+/********************************************************************\
+* BitlBee -- An IRC to other IM-networks gateway                     *
+*                                                                    *
+* Copyright 2007 Uli Meis <a.sporto+bee@gmail.com>                   *
+\********************************************************************/
+
+/*
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License with
+  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
+  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+  Suite 330, Boston, MA  02111-1307  USA
+*/
+
+#define BITLBEE_CORE
+#include "bitlbee.h"
+#include "ft.h"
+#include "dcc.h"
+#include <netinet/tcp.h>
+#include <regex.h>
+#include "lib/ftutil.h"
+
+/* 
+ * Since that might be confusing a note on naming:
+ *
+ * Generic dcc functions start with 
+ *
+ * 	dcc_
+ *
+ * ,methods specific to DCC SEND start with
+ *
+ * 	dccs_
+ *
+ * . Since we can be on both ends of a DCC SEND,
+ * functions specific to one end are called
+ *
+ * 	dccs_send and dccs_recv
+ *
+ * ,respectively.
+ */
+
+
+/* 
+ * used to generate a unique local transfer id the user
+ * can use to reject/cancel transfers
+ */
+unsigned int local_transfer_id=1;
+
+/* 
+ * just for debugging the nr. of chunks we received from im-protocols and the total data
+ */
+unsigned int receivedchunks=0, receiveddata=0;
+
+static void dcc_finish( file_transfer_t *file );
+static void dcc_close( file_transfer_t *file );
+gboolean dccs_send_proto( gpointer data, gint fd, b_input_condition cond );
+int dccs_send_request( struct dcc_file_transfer *df, char *user_nick, struct sockaddr_storage *saddr );
+gboolean dccs_recv_start( file_transfer_t *ft );
+gboolean dccs_recv_proto( gpointer data, gint fd, b_input_condition cond);
+gboolean dccs_recv_write_request( file_transfer_t *ft );
+gboolean dcc_progress( gpointer data, gint fd, b_input_condition cond );
+gboolean dcc_abort( dcc_file_transfer_t *df, char *reason, ... );
+
+/* As defined in ft.h */
+file_transfer_t *imcb_file_send_start( struct im_connection *ic, char *handle, char *file_name, size_t file_size )
+{
+	user_t *u = user_findhandle( ic, handle );
+	/* one could handle this more intelligent like imcb_buddy_msg.
+	 * can't call it directly though cause it does some wrapping.
+	 * Maybe give imcb_buddy_msg a parameter NO_WRAPPING? */
+	if (!u) return NULL;
+
+	return dccs_send_start( ic, u->nick, file_name, file_size );
+};
+
+/* As defined in ft.h */
+void imcb_file_canceled( file_transfer_t *file, char *reason )
+{
+	if( file->canceled )
+		file->canceled( file, reason );
+
+	dcc_close( file );
+}
+
+/* As defined in ft.h */
+gboolean imcb_file_recv_start( file_transfer_t *ft )
+{
+	return dccs_recv_start( ft );
+}
+
+/* As defined in ft.h */
+void imcb_file_finished( file_transfer_t *file )
+{
+	dcc_file_transfer_t *df = file->priv;
+
+	if( file->bytes_transferred >= file->file_size )
+		dcc_finish( file );
+	else
+		df->proto_finished = TRUE;
+}
+
+dcc_file_transfer_t *dcc_alloc_transfer( char *file_name, size_t file_size, struct im_connection *ic )
+{
+	file_transfer_t *file = g_new0( file_transfer_t, 1 );
+	dcc_file_transfer_t *df = file->priv = g_new0( dcc_file_transfer_t, 1 );
+	
+	file->file_size = file_size;
+	file->file_name = g_strdup( file_name );
+	file->local_id = local_transfer_id++;
+	df->ic = ic;
+	df->ft = file;
+	
+	return df;
+}
+
+/* This is where the sending magic starts... */
+file_transfer_t *dccs_send_start( struct im_connection *ic, char *user_nick, char *file_name, size_t file_size )
+{
+	file_transfer_t *file;
+	dcc_file_transfer_t *df;
+	struct sockaddr_storage saddr;
+	char *errmsg;
+	char host[HOST_NAME_MAX];
+	char port[6];
+
+	if( file_size > global.conf->ft_max_size )
+		return NULL;
+	
+	df = dcc_alloc_transfer( file_name, file_size, ic );
+	file = df->ft;
+	file->write = dccs_send_write;
+
+	/* listen and request */
+
+	if( ( df->fd = ft_listen( &saddr, host, port, TRUE, &errmsg ) ) == -1 )
+	{
+		dcc_abort( df, "Failed to listen locally, check your ft_listen setting in bitlbee.conf: %s", errmsg );
+		return NULL;
+	}
+
+	file->status = FT_STATUS_LISTENING;
+
+	if( !dccs_send_request( df, user_nick, &saddr ) )
+		return NULL;
+
+	/* watch */
+	df->watch_in = b_input_add( df->fd, B_EV_IO_READ, dccs_send_proto, df );
+
+	df->ic->irc->file_transfers = g_slist_prepend( df->ic->irc->file_transfers, file );
+
+	df->progress_timeout = b_timeout_add( DCC_MAX_STALL * 1000, dcc_progress, df );
+
+	imcb_log( ic, "File transfer request from %s for %s (%zd kb).\n"
+	              "Accept the file transfer if you'd like the file. If you don't, "
+	              "issue the 'transfers reject' command.",
+	              user_nick, file_name, file_size / 1024 );
+
+	return file;
+}
+
+/* Used pretty much everywhere in the code to abort a transfer */
+gboolean dcc_abort( dcc_file_transfer_t *df, char *reason, ... )
+{
+	file_transfer_t *file = df->ft;
+	va_list params;
+	va_start( params, reason );
+	char *msg = g_strdup_vprintf( reason, params );
+	va_end( params );
+	
+	file->status |= FT_STATUS_CANCELED;
+	
+	if( file->canceled )
+		file->canceled( file, msg );
+
+	imcb_log( df->ic, "File %s: DCC transfer aborted: %s", file->file_name, msg );
+
+	g_free( msg );
+
+	dcc_close( df->ft );
+
+	return FALSE;
+}
+
+gboolean dcc_progress( gpointer data, gint fd, b_input_condition cond )
+{
+	struct dcc_file_transfer *df = data;
+
+	if( df->bytes_sent == df->progress_bytes_last )
+	{
+		/* no progress. cancel */
+		if( df->bytes_sent == 0 )
+			return dcc_abort( df, "Couldn't establish transfer within %d seconds", DCC_MAX_STALL );
+		else 
+			return dcc_abort( df, "Transfer stalled for %d seconds at %d kb", DCC_MAX_STALL, df->bytes_sent / 1024 );
+
+	}
+
+	df->progress_bytes_last = df->bytes_sent;
+
+	return TRUE;
+}
+
+/* used extensively for socket operations */
+#define ASSERTSOCKOP(op, msg) \
+	if( (op) == -1 ) \
+		return dcc_abort( df , msg ": %s", strerror( errno ) );
+
+/* Creates the "DCC SEND" line and sends it to the server */
+int dccs_send_request( struct dcc_file_transfer *df, char *user_nick, struct sockaddr_storage *saddr )
+{
+	char ipaddr[INET6_ADDRSTRLEN]; 
+	const void *netaddr;
+	int port;
+	char *cmd;
+
+	if( saddr->ss_family == AF_INET )
+	{
+		struct sockaddr_in *saddr_ipv4 = ( struct sockaddr_in *) saddr;
+
+		sprintf( ipaddr, "%d", 
+			 ntohl( saddr_ipv4->sin_addr.s_addr ) );
+		port = saddr_ipv4->sin_port;
+	}
+	else 
+	{
+		struct sockaddr_in6 *saddr_ipv6 = ( struct sockaddr_in6 *) saddr;
+
+		netaddr = &saddr_ipv6->sin6_addr.s6_addr;
+		port = saddr_ipv6->sin6_port;
+
+		/* 
+		 * Didn't find docs about this, but it seems that's the way irssi does it
+		 */
+		if( !inet_ntop( saddr->ss_family, netaddr, ipaddr, sizeof( ipaddr ) ) )
+			return dcc_abort( df, "inet_ntop failed: %s", strerror( errno ) );
+	}
+
+	port = ntohs( port );
+
+	cmd = g_strdup_printf( "\001DCC SEND %s %s %u %zu\001",
+				df->ft->file_name, ipaddr, port, df->ft->file_size );
+	
+	if ( !irc_msgfrom( df->ic->irc, user_nick, cmd ) )
+		return dcc_abort( df, "Couldn't send `DCC SEND' message to %s.", user_nick );
+
+	g_free( cmd );
+
+	return TRUE;
+}
+
+/*
+ * After setup, the transfer itself is handled entirely by this function.
+ * There are basically four things to handle: connect, receive, send, and error.
+ */
+gboolean dccs_send_proto( gpointer data, gint fd, b_input_condition cond )
+{
+	dcc_file_transfer_t *df = data;
+	file_transfer_t *file = df->ft;
+	
+	if( ( cond & B_EV_IO_READ ) &&
+	    ( file->status & FT_STATUS_LISTENING ) )
+	{ 	
+		struct sockaddr *clt_addr;
+		socklen_t ssize = sizeof( clt_addr );
+
+		/* Connect */
+
+		ASSERTSOCKOP( df->fd = accept( fd, (struct sockaddr *) &clt_addr, &ssize ), "Accepting connection" );
+
+		closesocket( fd );
+		fd = df->fd;
+		file->status = FT_STATUS_TRANSFERRING;
+		sock_make_nonblocking( fd );
+
+		/* IM protocol callback */
+		if( file->accept )
+			file->accept( file );
+
+		/* reschedule for reading on new fd */
+		df->watch_in = b_input_add( fd, B_EV_IO_READ, dccs_send_proto, df );
+
+		return FALSE;
+	}
+
+	if( cond & B_EV_IO_READ ) 
+	{
+		int ret;
+		
+		ASSERTSOCKOP( ret = recv( fd, ( (char*) &df->acked ) + df->acked_len,
+			sizeof( df->acked ) - df->acked_len, 0 ), "Receiving" );
+
+		if( ret == 0 )
+			return dcc_abort( df, "Remote end closed connection" );
+		
+		/* How likely is it that a 32-bit integer gets split accross
+		   packet boundaries? Chances are rarely 0 so let's be sure. */
+		if( ( df->acked_len = ( df->acked_len + ret ) % 4 ) > 0 )
+			return TRUE;
+
+		df->acked = ntohl( df->acked );
+
+		/* If any of this is actually happening, the receiver should buy a new IRC client */
+
+		if ( df->acked > df->bytes_sent )
+			return dcc_abort( df, "Receiver magically received more bytes than sent ( %d > %d ) (BUG at receiver?)", df->acked, df->bytes_sent );
+
+		if ( df->acked < file->bytes_transferred )
+			return dcc_abort( df, "Receiver lost bytes? ( has %d, had %d ) (BUG at receiver?)", df->acked, file->bytes_transferred );
+		
+		file->bytes_transferred = df->acked;
+	
+		if( file->bytes_transferred >= file->file_size ) {
+			if( df->proto_finished )
+				dcc_finish( file );
+			return FALSE;
+		}
+	
+		return TRUE;
+	}
+
+	return TRUE;
+}
+
+gboolean dccs_recv_start( file_transfer_t *ft )
+{
+	dcc_file_transfer_t *df = ft->priv;
+	struct sockaddr_storage *saddr = &df->saddr;
+	int fd;
+	char ipaddr[INET6_ADDRSTRLEN]; 
+	socklen_t sa_len = saddr->ss_family == AF_INET ? 
+		sizeof( struct sockaddr_in ) : sizeof( struct sockaddr_in6 );
+	
+	if( !ft->write )
+		return dcc_abort( df, "BUG: protocol didn't register write()" );
+	
+	ASSERTSOCKOP( fd = df->fd = socket( saddr->ss_family, SOCK_STREAM, 0 ), "Opening Socket" );
+
+	sock_make_nonblocking( fd );
+
+	if( ( connect( fd, (struct sockaddr *)saddr, sa_len ) == -1 ) &&
+	    ( errno != EINPROGRESS ) )
+		return dcc_abort( df, "Connecting to %s:%d : %s", 
+			inet_ntop( saddr->ss_family, 
+				saddr->ss_family == AF_INET ? 
+				    ( void* ) &( ( struct sockaddr_in *) saddr )->sin_addr.s_addr :
+				    ( void* ) &( ( struct sockaddr_in6 *) saddr )->sin6_addr.s6_addr,
+				ipaddr, 
+				sizeof( ipaddr ) ),
+			ntohs( saddr->ss_family == AF_INET ?
+			    ( ( struct sockaddr_in *) saddr )->sin_port :
+			    ( ( struct sockaddr_in6 *) saddr )->sin6_port ),
+			strerror( errno ) );
+
+	ft->status = FT_STATUS_CONNECTING;
+
+	/* watch */
+	df->watch_out = b_input_add( df->fd, B_EV_IO_WRITE, dccs_recv_proto, df );
+	ft->write_request = dccs_recv_write_request;
+
+	df->progress_timeout = b_timeout_add( DCC_MAX_STALL * 1000, dcc_progress, df );
+
+	return TRUE;
+}
+
+gboolean dccs_recv_proto( gpointer data, gint fd, b_input_condition cond )
+{
+	dcc_file_transfer_t *df = data;
+	file_transfer_t *ft = df->ft;
+
+	if( ( cond & B_EV_IO_WRITE ) &&
+	    ( ft->status & FT_STATUS_CONNECTING ) )
+	{
+		ft->status = FT_STATUS_TRANSFERRING;
+
+		//df->watch_in = b_input_add( df->fd, B_EV_IO_READ, dccs_recv_proto, df );
+
+		df->watch_out = 0;
+		return FALSE;
+	}
+
+	if( cond & B_EV_IO_READ )
+	{
+		int ret, done;
+
+		ASSERTSOCKOP( ret = recv( fd, ft->buffer, sizeof( ft->buffer ), 0 ), "Receiving" );
+
+		if( ret == 0 )
+			return dcc_abort( df, "Remote end closed connection" );
+
+		if( !ft->write( df->ft, ft->buffer, ret ) )
+			return FALSE;
+
+		df->bytes_sent += ret;
+
+		done = df->bytes_sent >= ft->file_size;
+
+		if( ( ( df->bytes_sent - ft->bytes_transferred ) > DCC_PACKET_SIZE ) ||
+		    done )
+		{
+			guint32 ack = htonl( ft->bytes_transferred = df->bytes_sent );
+			int ackret;
+
+			ASSERTSOCKOP( ackret = send( fd, &ack, 4, 0 ), "Sending DCC ACK" );
+			
+			if ( ackret != 4 )
+				return dcc_abort( df, "Error sending DCC ACK, sent %d instead of 4 bytes", ackret );
+		}
+		
+		if( df->bytes_sent == ret )
+			ft->started = time( NULL );
+
+		if( done )
+		{
+			if( df->watch_out )
+				b_event_remove( df->watch_out );
+
+			if( df->proto_finished )
+				dcc_finish( ft );
+
+			df->watch_in = 0;
+			return FALSE;
+		}
+
+		df->watch_in = 0;
+		return FALSE;
+	}
+
+	return TRUE;
+}
+
+gboolean dccs_recv_write_request( file_transfer_t *ft )
+{
+	dcc_file_transfer_t *df = ft->priv;
+
+	if( df->watch_in )
+		return dcc_abort( df, "BUG: write_request() called while watching" );
+
+	df->watch_in = b_input_add( df->fd, B_EV_IO_READ, dccs_recv_proto, df );
+
+	return TRUE;
+}
+
+gboolean dccs_send_can_write( gpointer data, gint fd, b_input_condition cond )
+{
+	struct dcc_file_transfer *df = data;
+	df->watch_out = 0;
+
+	df->ft->write_request( df->ft );
+	return FALSE;
+}
+
+/* 
+ * Incoming data.
+ * 
+ */
+gboolean dccs_send_write( file_transfer_t *file, char *data, unsigned int data_len )
+{
+	dcc_file_transfer_t *df = file->priv;
+	int ret;
+
+	receivedchunks++; receiveddata += data_len;
+
+	if( df->watch_out )
+		return dcc_abort( df, "BUG: write() called while watching" );
+
+	ASSERTSOCKOP( ret = send( df->fd, data, data_len, 0 ), "Sending data" );
+
+	if( ret == 0 )
+		return dcc_abort( df, "Remote end closed connection" );
+
+	/* TODO: this should really not be fatal */
+	if( ret < data_len )
+		return dcc_abort( df, "send() sent %d instead of %d", ret, data_len );
+
+	if( df->bytes_sent == 0 )
+		file->started = time( NULL );
+
+	df->bytes_sent += ret;
+
+	if( df->bytes_sent < df->ft->file_size )
+		df->watch_out = b_input_add( df->fd, B_EV_IO_WRITE, dccs_send_can_write, df );
+
+	return TRUE;
+}
+
+/*
+ * Cleans up after a transfer.
+ */
+static void dcc_close( file_transfer_t *file )
+{
+	dcc_file_transfer_t *df = file->priv;
+
+	if( file->free )
+		file->free( file );
+	
+	closesocket( df->fd );
+
+	if( df->watch_in )
+		b_event_remove( df->watch_in );
+
+	if( df->watch_out )
+		b_event_remove( df->watch_out );
+	
+	if( df->progress_timeout )
+		b_event_remove( df->progress_timeout );
+	
+	df->ic->irc->file_transfers = g_slist_remove( df->ic->irc->file_transfers, file );
+	
+	g_free( df );
+	g_free( file->file_name );
+	g_free( file );
+}
+
+void dcc_finish( file_transfer_t *file )
+{
+	dcc_file_transfer_t *df = file->priv;
+	time_t diff = time( NULL ) - file->started ? : 1;
+
+	file->status |= FT_STATUS_FINISHED;
+	
+	if( file->finished )
+		file->finished( file );
+
+	imcb_log( df->ic, "File %s transferred successfully at %d kb/s!" , file->file_name, (int) ( file->bytes_transferred / 1024 / diff ) );
+	dcc_close( file );
+}
+
+/* 
+ * DCC SEND <filename> <IP> <port> <filesize>
+ *
+ * filename can be in "" or not. If it is, " can probably be escaped...
+ * IP can be an unsigned int (IPV4) or something else (IPV6)
+ * 
+ */
+file_transfer_t *dcc_request( struct im_connection *ic, char *line )
+{
+	char *pattern = "SEND"
+		" (([^\"][^ ]*)|\"(([^\"]|\\\")*)\")"
+		" (([0-9]*)|([^ ]*))"
+		" ([0-9]*)"
+		" ([0-9]*)\001";
+	regmatch_t pmatch[10];
+	regex_t re;
+	file_transfer_t *ft;
+	dcc_file_transfer_t *df;
+	char errbuf[256];
+	int regerrcode, gret;
+
+	if( ( regerrcode = regcomp( &re, pattern, REG_EXTENDED ) ) ||
+	    ( regerrcode = regexec( &re, line, 10, pmatch, 0 ) ) ) {
+		regerror( regerrcode,&re,errbuf,sizeof( errbuf ) );
+		imcb_log( ic, 
+			  "DCC: error parsing 'DCC SEND': %s, line: %s", 
+			  errbuf, line );
+		return NULL;
+	}
+
+	if( ( pmatch[1].rm_so > 0 ) &&
+	    ( pmatch[5].rm_so > 0 ) &&
+	    ( pmatch[8].rm_so > 0 ) &&
+	    ( pmatch[9].rm_so > 0 ) )
+	{
+		char *input = g_strdup( line );
+		char *filename, *host, *port;
+		size_t filesize;
+		struct addrinfo hints, *rp;
+
+		/* "filename" or filename */
+		if ( pmatch[2].rm_so > 0 )
+		{
+			input[pmatch[2].rm_eo] = '\0';
+			filename = input + pmatch[2].rm_so;
+		} else
+		{
+			input[pmatch[3].rm_eo] = '\0';
+			filename = input + pmatch[3].rm_so;
+		}
+			
+		input[pmatch[5].rm_eo] = '\0';
+
+		/* number means ipv4, something else means ipv6 */
+		if ( pmatch[6].rm_so > 0 )
+		{
+			struct in_addr ipaddr = { .s_addr = htonl( strtoul( input + pmatch[5].rm_so, NULL, 10 ) ) };
+			host = inet_ntoa( ipaddr );
+		} else
+		{
+			/* Contains non-numbers, hopefully an IPV6 address */
+			host = input + pmatch[7].rm_so;
+		}
+
+		input[pmatch[8].rm_eo] = '\0';
+		input[pmatch[9].rm_eo] = '\0';
+
+		port = input + pmatch[8].rm_so;
+		filesize = atoll( input + pmatch[9].rm_so );
+
+		memset( &hints, 0, sizeof ( struct addrinfo ) );
+		hints.ai_socktype = SOCK_STREAM;
+		hints.ai_flags = AI_NUMERICSERV;
+
+		if ( ( gret = getaddrinfo( host, port, &hints, &rp ) ) )
+		{
+			g_free( input );
+			imcb_log( ic, "DCC: getaddrinfo() failed with %s "
+				  "when parsing incoming 'DCC SEND': "
+				  "host %s, port %s", 
+				  gai_strerror( gret ), host, port );
+			return NULL;
+		}
+
+		df = dcc_alloc_transfer( filename, filesize, ic );
+		ft = df->ft;
+		ft->sending = TRUE;
+		memcpy( &df->saddr, rp->ai_addr, rp->ai_addrlen );
+
+		freeaddrinfo( rp );
+		g_free( input );
+
+		df->ic->irc->file_transfers = g_slist_prepend( df->ic->irc->file_transfers, ft );
+
+		return ft;
+	}
+
+	imcb_log( ic, "DCC: couldnt parse 'DCC SEND' line: %s", line );
+
+	return NULL;
+}
+
Index: dcc.h
===================================================================
--- dcc.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
+++ dcc.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -0,0 +1,104 @@
+/********************************************************************\
+* BitlBee -- An IRC to other IM-networks gateway                     *
+*                                                                    *
+* Copyright 2006 Marijn Kruisselbrink and others                     *
+* Copyright 2007 Uli Meis <a.sporto+bee@gmail.com>                   *
+\********************************************************************/
+
+/*
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License with
+  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
+  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+  Suite 330, Boston, MA  02111-1307  USA
+*/
+
+/* 
+ * DCC SEND
+ *
+ * Historically, DCC means send 1024 Bytes and wait for a 4 byte reply
+ * acknowledging all transferred data. This is ridiculous for two reasons.  The
+ * first being that TCP is a stream oriented protocol that doesn't care much
+ * about your idea of a packet. The second reason being that TCP is a reliable
+ * transfer protocol with its own sophisticated ACK mechanism, making DCCs ACK
+ * mechanism look like a joke. For these reasons, DCCs requirements have
+ * (hopefully) been relaxed in most implementations and this implementation
+ * depends upon at least the following: The 1024 bytes need not be transferred
+ * at once, i.e. packets can be smaller. A second relaxation has apparently
+ * gotten the name "DCC SEND ahead" which basically means to not give a damn
+ * about those DCC ACKs and just send data as you please. This behaviour is
+ * enabled by default. Note that this also means that packets may be as large
+ * as the maximum segment size.
+ */ 
+
+#ifndef _DCC_H
+#define _DCC_H
+
+/* Send an ACK after receiving this amount of data */
+#define DCC_PACKET_SIZE 1024
+
+/* Time in seconds that a DCC transfer can be stalled before being aborted.
+ * By handling this here individual protocols don't have to think about this. */
+#define DCC_MAX_STALL 120
+
+typedef struct dcc_file_transfer {
+
+	struct im_connection *ic;
+
+	/*
+	 * Depending in the status of the file transfer, this is either the socket that is
+	 * being listened on for connections, or the socket over which the file transfer is
+	 * taking place.
+	 */
+	int fd;
+	
+	/*
+	 * IDs returned by b_input_add for watch_ing over the above socket.
+	 */
+	gint watch_in;   /* readable */
+	gint watch_out;  /* writable */
+	
+	/* the progress watcher cancels any file transfer if nothing happens within DCC_MAX_STALL */
+	gint progress_timeout;
+	size_t progress_bytes_last;
+
+	/*
+	 * The total amount of bytes that have been sent to the irc client.
+	 */
+	size_t bytes_sent;
+	
+	/*
+	 * Handle the wonderful sadly-not-deprecated ACKs.
+	 */
+	guint32 acked;
+	int acked_len;
+	
+	/* imc's handle */
+	file_transfer_t *ft;
+
+	/* if we're receiving, this is the sender's socket address */
+	struct sockaddr_storage saddr;
+
+	/* set to true if the protocol has finished 
+	 * (i.e. called imcb_file_finished)
+	 */
+	int proto_finished;
+} dcc_file_transfer_t;
+
+file_transfer_t *dccs_send_start( struct im_connection *ic, char *user_nick, char *file_name, size_t file_size );
+
+void dcc_canceled( file_transfer_t *file, char *reason );
+
+gboolean dccs_send_write( file_transfer_t *file, char *data, unsigned int data_size );
+
+file_transfer_t *dcc_request( struct im_connection *ic, char *line );
+#endif
Index: debian/rules
===================================================================
--- debian/rules	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ debian/rules	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -1,4 +1,5 @@
 #!/usr/bin/make -f
 
+BITLBEE_CONFIGURE_FLAGS ?=
 DEBUG ?= 0
 
@@ -14,5 +15,5 @@
 build-arch-stamp:
 	[ -d debian ]
-	./configure --debug=$(DEBUG) --prefix=/usr --etcdir=/etc/bitlbee --events=libevent
+	./configure --debug=$(DEBUG) --prefix=/usr --etcdir=/etc/bitlbee --events=libevent $(BITLBEE_CONFIGURE_FLAGS)
 	$(MAKE)
 #	$(MAKE) -C doc/ all
Index: doc/user-guide/commands.xml
===================================================================
--- doc/user-guide/commands.xml	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ doc/user-guide/commands.xml	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -1120,3 +1120,46 @@
 
 	</bitlbee-command>
+	
+	<bitlbee-command name="transfers">
+		<short-description>Monitor, cancel, or reject file transfers</short-description>
+		<syntax>transfers [&lt;cancel&gt; id | &lt;reject&gt;]</syntax>
+		
+		<description>
+			<para>
+				Without parameters the currently pending file transfers and their status will be listed. Available actions are <emphasis>cancel</emphasis> and <emphasis>reject</emphasis>. See <emphasis>help transfers &lt;action&gt;</emphasis> for more information.
+			</para>
+
+			<ircexample>
+				<ircline nick="ulim">transfers</ircline>
+			</ircexample>
+		</description>
+		
+		<bitlbee-command name="cancel">
+			<short-description>Cancels the file transfer with the given id</short-description>
+			<syntax>transfers &lt;cancel&gt; id</syntax>
+
+			<description>
+				<para>Cancels the file transfer with the given id</para>
+			</description>
+
+			<ircexample>
+				<ircline nick="ulim">transfers cancel 1</ircline>
+				<ircline nick="root">Canceling file transfer for test</ircline>
+			</ircexample>
+		</bitlbee-command>
+
+		<bitlbee-command name="reject">
+			<short-description>Rejects all incoming transfers</short-description>
+			<syntax>transfers &lt;reject&gt;</syntax>
+
+			<description>
+				<para>Rejects all incoming (not already transferring) file transfers. Since you probably have only one incoming transfer at a time, no id is neccessary. Or is it?</para>
+			</description>
+
+			<ircexample>
+				<ircline nick="ulim">transfers reject</ircline>
+			</ircexample>
+		</bitlbee-command>
+	</bitlbee-command>
+	
 </chapter>
Index: help.c
===================================================================
--- help.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ help.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -2,5 +2,5 @@
   * BitlBee -- An IRC to other IM-networks gateway                     *
   *                                                                    *
-  * Copyright 2002-2005 Wilmer van der Gaast and others                *
+  * Copyright 2002-2009 Wilmer van der Gaast and others                *
   \********************************************************************/
 
@@ -169,2 +169,26 @@
 	return NULL;
 }
+
+int help_add_mem( help_t **help, const char *title, const char *content )
+{
+	help_t *h, *l = NULL;
+	
+	for( h = *help; h; h = h->next )
+	{
+		if( g_strcasecmp( h->title, title ) == 0 )
+			return 0;
+		
+		l = h;
+	}
+	
+	if( l )
+		h = l->next = g_new0( struct help, 1 );
+	else
+		*help = h = g_new0( struct help, 1 );
+	h->fd = -1;
+	h->title = g_strdup( title );
+	h->length = strlen( content );
+	h->offset.mem_offset = g_strdup( content );
+	
+	return 1;
+}
Index: help.h
===================================================================
--- help.h	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ help.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -46,4 +46,5 @@
 void help_free( help_t **help );
 char *help_get( help_t **help, char *title );
+int help_add_mem( help_t **help, const char *title, const char *content_ );
 
 #endif
Index: ipc.c
===================================================================
--- ipc.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ ipc.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -514,5 +514,5 @@
 	}
 		
-	child->ipc_inpa = b_input_add( child->ipc_fd, GAIM_INPUT_READ, ipc_master_read, child );
+	child->ipc_inpa = b_input_add( child->ipc_fd, B_EV_IO_READ, ipc_master_read, child );
 	
 	child_list = g_slist_append( child_list, child );
@@ -552,5 +552,5 @@
 	}
 	
-	b_input_add( serversock, GAIM_INPUT_READ, new_ipc_client, NULL );
+	b_input_add( serversock, B_EV_IO_READ, new_ipc_client, NULL );
 	
 	return 1;
@@ -597,5 +597,5 @@
 			return 0;
 		}
-		child->ipc_inpa = b_input_add( child->ipc_fd, GAIM_INPUT_READ, ipc_master_read, child );
+		child->ipc_inpa = b_input_add( child->ipc_fd, B_EV_IO_READ, ipc_master_read, child );
 		
 		child_list = g_slist_append( child_list, child );
Index: irc.c
===================================================================
--- irc.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ irc.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -29,4 +29,5 @@
 #include "crypting.h"
 #include "ipc.h"
+#include "dcc.h"
 
 static gboolean irc_userping( gpointer _irc, int fd, b_input_condition cond );
@@ -125,5 +126,5 @@
 	sock_make_nonblocking( irc->fd );
 	
-	irc->r_watch_source_id = b_input_add( irc->fd, GAIM_INPUT_READ, bitlbee_io_current_client_read, irc );
+	irc->r_watch_source_id = b_input_add( irc->fd, B_EV_IO_READ, bitlbee_io_current_client_read, irc );
 	
 	irc->status = USTATUS_OFFLINE;
@@ -213,4 +214,6 @@
 	/* Evaluator sets the iconv/oconv structures. */
 	set_eval_charset( set_find( &irc->set, "charset" ), set_getstr( &irc->set, "charset" ) );
+	
+	nogaim_init();
 	
 	return( irc );
@@ -695,8 +698,8 @@
 		   in the event queue. */
 		/* Really can't be done as long as the code doesn't do error checking very well:
-		if( bitlbee_io_current_client_write( irc, irc->fd, GAIM_INPUT_WRITE ) ) */
+		if( bitlbee_io_current_client_write( irc, irc->fd, B_EV_IO_WRITE ) ) */
 		
 		/* So just always do it via the event handler. */
-		irc->w_watch_source_id = b_input_add( irc->fd, GAIM_INPUT_WRITE, bitlbee_io_current_client_write, irc );
+		irc->w_watch_source_id = b_input_add( irc->fd, B_EV_IO_WRITE, bitlbee_io_current_client_write, irc );
 	}
 	
@@ -724,5 +727,5 @@
 		if( now )
 		{
-			bitlbee_io_current_client_write( irc, irc->fd, GAIM_INPUT_WRITE );
+			bitlbee_io_current_client_write( irc, irc->fd, B_EV_IO_WRITE );
 		}
 		temp = temp->next;
@@ -1125,7 +1128,17 @@
 			return( 1 );
 		}
+		else if( g_strncasecmp( s + 1, "DCC", 3 ) == 0 )
+		{
+			if( u && u->ic && u->ic->acc->prpl->transfer_request )
+			{
+				file_transfer_t *ft = dcc_request( u->ic, s + 5 );
+				if ( ft )
+					u->ic->acc->prpl->transfer_request( u->ic, ft, u->handle );
+			}
+			return( 1 );
+		}		
 		else
 		{
-			irc_usermsg( irc, "Non-ACTION CTCP's aren't supported" );
+			irc_usermsg( irc, "Supported CTCPs are ACTION, VERSION, PING, TYPING, DCC" );
 			return( 0 );
 		}
Index: irc.h
===================================================================
--- irc.h	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ irc.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -83,4 +83,5 @@
 	struct query *queries;
 	struct account *accounts;
+	GSList *file_transfers;
 	struct chat *chatrooms;
 	
Index: lib/Makefile
===================================================================
--- lib/Makefile	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ lib/Makefile	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -10,5 +10,5 @@
 
 # [SH] Program variables
-objects = arc.o base64.o $(EVENT_HANDLER) http_client.o ini.o md5.o misc.o proxy.o sha1.o $(SSL_CLIENT) url.o xmltree.o
+objects = arc.o base64.o $(EVENT_HANDLER) http_client.o ini.o md5.o misc.o proxy.o sha1.o $(SSL_CLIENT) url.o xmltree.o ftutil.o
 
 CFLAGS += -Wall
Index: lib/events.h
===================================================================
--- lib/events.h	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ lib/events.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -48,6 +48,8 @@
    the given callback function. */
 typedef enum {
-	GAIM_INPUT_READ = 1 << 1,
-	GAIM_INPUT_WRITE = 1 << 2
+	B_EV_IO_READ = 1 << 0,
+	B_EV_IO_WRITE = 1 << 1,
+	B_EV_FLAG_FORCE_ONCE = 1 << 16,
+	B_EV_FLAG_FORCE_REPEAT = 1 << 17,
 } b_input_condition;
 typedef gboolean (*b_event_handler)(gpointer data, gint fd, b_input_condition cond);
Index: lib/events_glib.c
===================================================================
--- lib/events_glib.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ lib/events_glib.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -49,4 +49,5 @@
 	b_event_handler function;
 	gpointer data;
+	guint flags;
 } GaimIOClosure;
 
@@ -76,7 +77,7 @@
 
 	if (condition & GAIM_READ_COND)
-		gaim_cond |= GAIM_INPUT_READ;
+		gaim_cond |= B_EV_IO_READ;
 	if (condition & GAIM_WRITE_COND)
-		gaim_cond |= GAIM_INPUT_WRITE;
+		gaim_cond |= B_EV_IO_WRITE;
 	
 	event_debug( "gaim_io_invoke( %d, %d, 0x%x )\n", g_io_channel_unix_get_fd(source), condition, data );
@@ -87,5 +88,10 @@
 		event_debug( "Returned FALSE, cancelling.\n" );
 	
-	return st;
+	if (closure->flags & B_EV_FLAG_FORCE_ONCE)
+		return FALSE;
+	else if (closure->flags & B_EV_FLAG_FORCE_REPEAT)
+		return TRUE;
+	else
+		return st;
 }
 
@@ -105,8 +111,9 @@
 	closure->function = function;
 	closure->data = data;
+	closure->flags = condition;
 	
-	if (condition & GAIM_INPUT_READ)
+	if (condition & B_EV_IO_READ)
 		cond |= GAIM_READ_COND;
-	if (condition & GAIM_INPUT_WRITE)
+	if (condition & B_EV_IO_WRITE)
 		cond |= GAIM_WRITE_COND;
 	
Index: lib/events_libevent.c
===================================================================
--- lib/events_libevent.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ lib/events_libevent.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -60,4 +60,5 @@
 	b_event_handler function;
 	void *data;
+	guint flags;
 };
 
@@ -126,7 +127,7 @@
 	{
 		if( event & EV_READ )
-			cond |= GAIM_INPUT_READ;
+			cond |= B_EV_IO_READ;
 		if( event & EV_WRITE )
-			cond |= GAIM_INPUT_WRITE;
+			cond |= B_EV_IO_WRITE;
 	}
 	
@@ -150,5 +151,5 @@
 		return;
 	}
-	else if( !st )
+	else if( !st && !( b_ev->flags & B_EV_FLAG_FORCE_REPEAT ) )
 	{
 		event_debug( "Handler returned FALSE: " );
@@ -174,6 +175,6 @@
 	event_debug( "b_input_add( %d, %d, 0x%x, 0x%x ) ", fd, condition, function, data );
 	
-	if( ( condition & GAIM_INPUT_READ  && ( b_ev = g_hash_table_lookup( read_hash,  &fd ) ) ) ||
-	    ( condition & GAIM_INPUT_WRITE && ( b_ev = g_hash_table_lookup( write_hash, &fd ) ) ) )
+	if( ( condition & B_EV_IO_READ  && ( b_ev = g_hash_table_lookup( read_hash,  &fd ) ) ) ||
+	    ( condition & B_EV_IO_WRITE && ( b_ev = g_hash_table_lookup( write_hash, &fd ) ) ) )
 	{
 		/* We'll stick with this libevent entry, but give it a new BitlBee id. */
@@ -198,7 +199,7 @@
 		
 		out_cond = EV_PERSIST;
-		if( condition & GAIM_INPUT_READ )
+		if( condition & B_EV_IO_READ )
 			out_cond |= EV_READ;
-		if( condition & GAIM_INPUT_WRITE )
+		if( condition & B_EV_IO_WRITE )
 			out_cond |= EV_WRITE;
 		
@@ -212,4 +213,5 @@
 	}
 	
+	b_ev->flags = condition;
 	g_hash_table_insert( id_hash, &b_ev->id, b_ev );
 	return b_ev->id;
Index: lib/ftutil.c
===================================================================
--- lib/ftutil.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
+++ lib/ftutil.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -0,0 +1,134 @@
+/***************************************************************************\
+*                                                                           *
+*  BitlBee - An IRC to IM gateway                                           *
+*  Utility functions for file transfer                                      *
+*                                                                           *
+*  Copyright 2008 Uli Meis <a.sporto+bee@gmail.com>                         *
+*                                                                           *
+*  This program is free software; you can redistribute it and/or modify     *
+*  it under the terms of the GNU General Public License as published by     *
+*  the Free Software Foundation; either version 2 of the License, or        *
+*  (at your option) any later version.                                      *
+*                                                                           *
+*  This program is distributed in the hope that it will be useful,          *
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
+*  GNU General Public License for more details.                             *
+*                                                                           *
+*  You should have received a copy of the GNU General Public License along  *
+*  with this program; if not, write to the Free Software Foundation, Inc.,  *
+*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              *
+*                                                                           *
+\***************************************************************************/
+
+#define BITLBEE_CORE
+#include "bitlbee.h"
+#include <poll.h>
+#include <netinet/tcp.h>
+#include "lib/ftutil.h"
+
+#define ASSERTSOCKOP(op, msg) \
+	if( (op) == -1 ) {\
+		g_snprintf( errmsg, sizeof( errmsg ), msg ": %s", strerror( errno ) ); \
+		return -1; }
+
+/*
+ * Creates a listening socket and returns it in saddr_ptr.
+ */
+int ft_listen( struct sockaddr_storage *saddr_ptr, char *host, char *port, int for_bitlbee_client, char **errptr )
+{
+	int fd, gret, saddrlen;
+	struct addrinfo hints, *rp;
+	socklen_t ssize = sizeof( struct sockaddr_storage );
+	struct sockaddr_storage saddrs, *saddr = &saddrs;
+	static char errmsg[1024];
+	char *ftlisten = global.conf->ft_listen;
+
+	if( errptr )
+		*errptr = errmsg;
+
+	strcpy( port, "0" );
+
+	/* Format is <IP-A>[:<Port-A>];<IP-B>[:<Port-B>] where
+	 * A is for connections with the bitlbee client (DCC)
+	 * and B is for connections with IM peers.
+	 */
+	if( ftlisten )
+	{
+		char *scolon = strchr( ftlisten, ';' );
+		char *colon;
+
+		if( scolon )
+		{
+			if( for_bitlbee_client )
+			{
+				*scolon = '\0';
+				strncpy( host, ftlisten, HOST_NAME_MAX );
+				*scolon = ';';
+			}
+			else
+			{
+				strncpy( host, scolon + 1, HOST_NAME_MAX );
+			}
+		}
+		else
+		{
+			strncpy( host, ftlisten, HOST_NAME_MAX );
+		}
+
+		if( ( colon = strchr( host, ':' ) ) )
+		{
+			*colon = '\0';
+			strncpy( port, colon + 1, 5 );
+		}
+	}
+	else
+	{
+		ASSERTSOCKOP( gethostname( host, HOST_NAME_MAX + 1 ), "gethostname()" );
+	}
+
+	memset( &hints, 0, sizeof( struct addrinfo ) );
+	hints.ai_socktype = SOCK_STREAM;
+	hints.ai_flags = AI_NUMERICSERV;
+
+	if ( ( gret = getaddrinfo( host, port, &hints, &rp ) ) != 0 )
+	{
+		sprintf( errmsg, "getaddrinfo() failed: %s", gai_strerror( gret ) );
+		return -1;
+	}
+
+	saddrlen = rp->ai_addrlen;
+
+	memcpy( saddr, rp->ai_addr, saddrlen );
+
+	freeaddrinfo( rp );
+
+	ASSERTSOCKOP( fd = socket( saddr->ss_family, SOCK_STREAM, 0 ), "Opening socket" );
+	ASSERTSOCKOP( bind( fd, ( struct sockaddr *)saddr, saddrlen ), "Binding socket" );
+	ASSERTSOCKOP( listen( fd, 1 ), "Making socket listen" );
+
+	if ( !inet_ntop( saddr->ss_family, saddr->ss_family == AF_INET ?
+			( void * )&( ( struct sockaddr_in * ) saddr )->sin_addr.s_addr :
+			( void * )&( ( struct sockaddr_in6 * ) saddr )->sin6_addr.s6_addr,
+			host, HOST_NAME_MAX ) )
+	{
+		strcpy( errmsg, "inet_ntop failed on listening socket" );
+		return -1;
+	}
+
+	ASSERTSOCKOP( getsockname( fd, ( struct sockaddr *)saddr, &ssize ), "Getting socket name" );
+
+	if( saddr->ss_family == AF_INET )
+		g_snprintf( port, 6, "%d", ntohs( ( (struct sockaddr_in *) saddr )->sin_port ) );
+	else
+		g_snprintf( port, 6, "%d", ntohs( ( (struct sockaddr_in6 *) saddr )->sin6_port ) );
+
+	if( saddr_ptr )
+		memcpy( saddr_ptr, saddr, saddrlen );
+
+	/* I hate static-length strings.. */
+	host[HOST_NAME_MAX] = '\0';
+	port[5] = '\0';
+	
+	return fd;
+}
Index: lib/ftutil.h
===================================================================
--- lib/ftutil.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
+++ lib/ftutil.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -0,0 +1,40 @@
+/***************************************************************************\
+*                                                                           *
+*  BitlBee - An IRC to IM gateway                                           *
+*  Utility functions for file transfer                                      *
+*                                                                           *
+*  Copyright 2008 Uli Meis <a.sporto+bee@gmail.com>                         *
+*                                                                           *
+*  This program is free software; you can redistribute it and/or modify     *
+*  it under the terms of the GNU General Public License as published by     *
+*  the Free Software Foundation; either version 2 of the License, or        *
+*  (at your option) any later version.                                      *
+*                                                                           *
+*  This program is distributed in the hope that it will be useful,          *
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
+*  GNU General Public License for more details.                             *
+*                                                                           *
+*  You should have received a copy of the GNU General Public License along  *
+*  with this program; if not, write to the Free Software Foundation, Inc.,  *
+*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              *
+*                                                                           *
+\***************************************************************************/
+
+#ifndef AI_NUMERICSERV
+#define AI_NUMERICSERV 0x0400   /* Don't use name resolution.  */
+#endif
+
+/* Some ifdefs for ulibc and apparently also BSD (Thanks to Whoopie) */
+#ifndef HOST_NAME_MAX
+#include <sys/param.h>
+#ifdef MAXHOSTNAMELEN
+#define HOST_NAME_MAX MAXHOSTNAMELEN
+#else
+#define HOST_NAME_MAX 255
+#endif
+#endif
+
+/* This function should be used with care. host should be AT LEAST a
+   char[HOST_NAME_MAX+1] and port AT LEAST a char[6]. */
+int ft_listen( struct sockaddr_storage *saddr_ptr, char *host, char *port, int for_bitlbee_client, char **errptr );
Index: lib/http_client.c
===================================================================
--- lib/http_client.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ lib/http_client.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -149,8 +149,8 @@
 	if( req->bytes_written < req->request_length )
 		req->inpa = b_input_add( source,
-		                         req->ssl ? ssl_getdirection( req->ssl ) : GAIM_INPUT_WRITE,
+		                         req->ssl ? ssl_getdirection( req->ssl ) : B_EV_IO_WRITE,
 	        	                 http_connected, req );
 	else
-		req->inpa = b_input_add( source, GAIM_INPUT_READ, http_incoming_data, req );
+		req->inpa = b_input_add( source, B_EV_IO_READ, http_incoming_data, req );
 	
 	return FALSE;
@@ -234,5 +234,5 @@
 	/* There will be more! */
 	req->inpa = b_input_add( req->fd,
-	                         req->ssl ? ssl_getdirection( req->ssl ) : GAIM_INPUT_READ,
+	                         req->ssl ? ssl_getdirection( req->ssl ) : B_EV_IO_READ,
 	                         http_incoming_data, req );
 	
Index: lib/proxy.c
===================================================================
--- lib/proxy.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ lib/proxy.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -91,7 +91,7 @@
 		b_event_remove(phb->inpa);
 		if( phb->proxy_func )
-			phb->proxy_func(phb->proxy_data, -1, GAIM_INPUT_READ);
+			phb->proxy_func(phb->proxy_data, -1, B_EV_IO_READ);
 		else {
-			phb->func(phb->data, -1, GAIM_INPUT_READ);
+			phb->func(phb->data, -1, B_EV_IO_READ);
 			g_free(phb);
 		}
@@ -102,7 +102,7 @@
 	b_event_remove(phb->inpa);
 	if( phb->proxy_func )
-		phb->proxy_func(phb->proxy_data, source, GAIM_INPUT_READ);
+		phb->proxy_func(phb->proxy_data, source, B_EV_IO_READ);
 	else {
-		phb->func(phb->data, source, GAIM_INPUT_READ);
+		phb->func(phb->data, source, B_EV_IO_READ);
 		g_free(phb);
 	}
@@ -147,5 +147,5 @@
 		return -1;
 	} else {
-		phb->inpa = b_input_add(fd, GAIM_INPUT_WRITE, gaim_io_connected, phb);
+		phb->inpa = b_input_add(fd, B_EV_IO_WRITE, gaim_io_connected, phb);
 		phb->fd = fd;
 		
@@ -179,5 +179,5 @@
 	if ((memcmp(HTTP_GOODSTRING, inputline, strlen(HTTP_GOODSTRING)) == 0) ||
 	    (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) {
-		phb->func(phb->data, source, GAIM_INPUT_READ);
+		phb->func(phb->data, source, B_EV_IO_READ);
 		g_free(phb->host);
 		g_free(phb);
@@ -186,5 +186,5 @@
 
 	close(source);
-	phb->func(phb->data, -1, GAIM_INPUT_READ);
+	phb->func(phb->data, -1, B_EV_IO_READ);
 	g_free(phb->host);
 	g_free(phb);
@@ -204,5 +204,5 @@
 	if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
 		close(source);
-		phb->func(phb->data, -1, GAIM_INPUT_READ);
+		phb->func(phb->data, -1, B_EV_IO_READ);
 		g_free(phb->host);
 		g_free(phb);
@@ -215,5 +215,5 @@
 	if (send(source, cmd, strlen(cmd), 0) < 0) {
 		close(source);
-		phb->func(phb->data, -1, GAIM_INPUT_READ);
+		phb->func(phb->data, -1, B_EV_IO_READ);
 		g_free(phb->host);
 		g_free(phb);
@@ -230,5 +230,5 @@
 		if (send(source, cmd, strlen(cmd), 0) < 0) {
 			close(source);
-			phb->func(phb->data, -1, GAIM_INPUT_READ);
+			phb->func(phb->data, -1, B_EV_IO_READ);
 			g_free(phb->host);
 			g_free(phb);
@@ -240,11 +240,11 @@
 	if (send(source, cmd, strlen(cmd), 0) < 0) {
 		close(source);
-		phb->func(phb->data, -1, GAIM_INPUT_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
-	}
-
-	phb->inpa = b_input_add(source, GAIM_INPUT_READ, http_canread, phb);
+		phb->func(phb->data, -1, B_EV_IO_READ);
+		g_free(phb->host);
+		g_free(phb);
+		return FALSE;
+	}
+
+	phb->inpa = b_input_add(source, B_EV_IO_READ, http_canread, phb);
 	
 	return FALSE;
@@ -273,5 +273,5 @@
 	memset(packet, 0, sizeof(packet));
 	if (read(source, packet, 9) >= 4 && packet[1] == 90) {
-		phb->func(phb->data, source, GAIM_INPUT_READ);
+		phb->func(phb->data, source, B_EV_IO_READ);
 		g_free(phb->host);
 		g_free(phb);
@@ -280,5 +280,5 @@
 
 	close(source);
-	phb->func(phb->data, -1, GAIM_INPUT_READ);
+	phb->func(phb->data, -1, B_EV_IO_READ);
 	g_free(phb->host);
 	g_free(phb);
@@ -299,5 +299,5 @@
 	if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
 		close(source);
-		phb->func(phb->data, -1, GAIM_INPUT_READ);
+		phb->func(phb->data, -1, B_EV_IO_READ);
 		g_free(phb->host);
 		g_free(phb);
@@ -309,5 +309,5 @@
 	if (!(hp = gethostbyname(phb->host))) {
 		close(source);
-		phb->func(phb->data, -1, GAIM_INPUT_READ);
+		phb->func(phb->data, -1, B_EV_IO_READ);
 		g_free(phb->host);
 		g_free(phb);
@@ -326,11 +326,11 @@
 	if (write(source, packet, 9) != 9) {
 		close(source);
-		phb->func(phb->data, -1, GAIM_INPUT_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
-	}
-
-	phb->inpa = b_input_add(source, GAIM_INPUT_READ, s4_canread, phb);
+		phb->func(phb->data, -1, B_EV_IO_READ);
+		g_free(phb->host);
+		g_free(phb);
+		return FALSE;
+	}
+
+	phb->inpa = b_input_add(source, B_EV_IO_READ, s4_canread, phb);
 	
 	return FALSE;
@@ -359,5 +359,5 @@
 	if (read(source, buf, 10) < 10) {
 		close(source);
-		phb->func(phb->data, -1, GAIM_INPUT_READ);
+		phb->func(phb->data, -1, B_EV_IO_READ);
 		g_free(phb->host);
 		g_free(phb);
@@ -366,11 +366,11 @@
 	if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
 		close(source);
-		phb->func(phb->data, -1, GAIM_INPUT_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
-	}
-
-	phb->func(phb->data, source, GAIM_INPUT_READ);
+		phb->func(phb->data, -1, B_EV_IO_READ);
+		g_free(phb->host);
+		g_free(phb);
+		return FALSE;
+	}
+
+	phb->func(phb->data, source, B_EV_IO_READ);
 	g_free(phb->host);
 	g_free(phb);
@@ -396,5 +396,5 @@
 	if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) {
 		close(source);
-		phb->func(phb->data, -1, GAIM_INPUT_READ);
+		phb->func(phb->data, -1, B_EV_IO_READ);
 		g_free(phb->host);
 		g_free(phb);
@@ -402,5 +402,5 @@
 	}
 
-	phb->inpa = b_input_add(source, GAIM_INPUT_READ, s5_canread_again, phb);
+	phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread_again, phb);
 }
 
@@ -414,5 +414,5 @@
 	if (read(source, buf, 2) < 2) {
 		close(source);
-		phb->func(phb->data, -1, GAIM_INPUT_READ);
+		phb->func(phb->data, -1, B_EV_IO_READ);
 		g_free(phb->host);
 		g_free(phb);
@@ -422,5 +422,5 @@
 	if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
 		close(source);
-		phb->func(phb->data, -1, GAIM_INPUT_READ);
+		phb->func(phb->data, -1, B_EV_IO_READ);
 		g_free(phb->host);
 		g_free(phb);
@@ -442,5 +442,5 @@
 	if (read(source, buf, 2) < 2) {
 		close(source);
-		phb->func(phb->data, -1, GAIM_INPUT_READ);
+		phb->func(phb->data, -1, B_EV_IO_READ);
 		g_free(phb->host);
 		g_free(phb);
@@ -450,5 +450,5 @@
 	if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
 		close(source);
-		phb->func(phb->data, -1, GAIM_INPUT_READ);
+		phb->func(phb->data, -1, B_EV_IO_READ);
 		g_free(phb->host);
 		g_free(phb);
@@ -465,5 +465,5 @@
 		if (write(source, buf, 3 + i + j) < 3 + i + j) {
 			close(source);
-			phb->func(phb->data, -1, GAIM_INPUT_READ);
+			phb->func(phb->data, -1, B_EV_IO_READ);
 			g_free(phb->host);
 			g_free(phb);
@@ -471,5 +471,5 @@
 		}
 
-		phb->inpa = b_input_add(source, GAIM_INPUT_READ, s5_readauth, phb);
+		phb->inpa = b_input_add(source, B_EV_IO_READ, s5_readauth, phb);
 	} else {
 		s5_sendconnect(phb, source);
@@ -491,5 +491,5 @@
 	if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
 		close(source);
-		phb->func(phb->data, -1, GAIM_INPUT_READ);
+		phb->func(phb->data, -1, B_EV_IO_READ);
 		g_free(phb->host);
 		g_free(phb);
@@ -513,11 +513,11 @@
 	if (write(source, buf, i) < i) {
 		close(source);
-		phb->func(phb->data, -1, GAIM_INPUT_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
-	}
-
-	phb->inpa = b_input_add(source, GAIM_INPUT_READ, s5_canread, phb);
+		phb->func(phb->data, -1, B_EV_IO_READ);
+		g_free(phb->host);
+		g_free(phb);
+		return FALSE;
+	}
+
+	phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread, phb);
 	
 	return FALSE;
Index: lib/ssl_bogus.c
===================================================================
--- lib/ssl_bogus.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ lib/ssl_bogus.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -59,5 +59,5 @@
 b_input_condition ssl_getdirection( void *conn )
 {
-	return GAIM_INPUT_READ;
+	return B_EV_IO_READ;
 }
 
Index: lib/ssl_client.h
===================================================================
--- lib/ssl_client.h	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ lib/ssl_client.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -71,5 +71,5 @@
 G_MODULE_EXPORT int ssl_getfd( void *conn );
 
-/* This function returns GAIM_INPUT_READ/WRITE. With SSL connections it's
+/* This function returns B_EV_IO_READ/WRITE. With SSL connections it's
    possible that something has to be read while actually were trying to
    write something (think about key exchange/refresh/etc). So when an
Index: lib/ssl_gnutls.c
===================================================================
--- lib/ssl_gnutls.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ lib/ssl_gnutls.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -106,5 +106,5 @@
 	struct scd *conn = data;
 	
-	return ssl_connected( conn, conn->fd, GAIM_INPUT_WRITE );
+	return ssl_connected( conn, conn->fd, B_EV_IO_WRITE );
 }
 
@@ -244,4 +244,4 @@
 {
 	return( gnutls_record_get_direction( ((struct scd*)conn)->session ) ?
-	        GAIM_INPUT_WRITE : GAIM_INPUT_READ );
-}
+	        B_EV_IO_WRITE : B_EV_IO_READ );
+}
Index: lib/ssl_nss.c
===================================================================
--- lib/ssl_nss.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ lib/ssl_nss.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -193,4 +193,4 @@
 {
 	/* Just in case someone calls us, let's return the most likely case: */
-	return GAIM_INPUT_READ;
+	return B_EV_IO_READ;
 }
Index: lib/ssl_openssl.c
===================================================================
--- lib/ssl_openssl.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ lib/ssl_openssl.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -102,5 +102,5 @@
 	struct scd *conn = data;
 	
-	return ssl_connected( conn, conn->fd, GAIM_INPUT_WRITE );
+	return ssl_connected( conn, conn->fd, B_EV_IO_WRITE );
 }
 
@@ -270,4 +270,4 @@
 b_input_condition ssl_getdirection( void *conn )
 {
-	return( ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_WRITE ? GAIM_INPUT_WRITE : GAIM_INPUT_READ );
-}
+	return( ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_WRITE ? B_EV_IO_WRITE : B_EV_IO_READ );
+}
Index: lib/ssl_sspi.c
===================================================================
--- lib/ssl_sspi.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ lib/ssl_sspi.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -275,4 +275,4 @@
 GaimInputCondition ssl_getdirection( void *conn )
 {
-	return GAIM_INPUT_WRITE; /* FIXME: or GAIM_INPUT_READ */
-}
+	return B_EV_IO_WRITE; /* FIXME: or B_EV_IO_READ */
+}
Index: protocols/ft.h
===================================================================
--- protocols/ft.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
+++ protocols/ft.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -0,0 +1,175 @@
+/********************************************************************\
+* BitlBee -- An IRC to other IM-networks gateway                     *
+*                                                                    *
+* Copyright 2006 Marijn Kruisselbrink and others                     *
+\********************************************************************/
+
+/* Generic file transfer header                                     */
+
+/*
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License with
+  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
+  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+  Suite 330, Boston, MA  02111-1307  USA
+*/
+
+#ifndef _FT_H
+#define _FT_H
+
+/*
+ * One buffer is needed for each transfer. The receiver stores a message
+ * in it and gives it to the sender. The sender will stall the receiver
+ * till the buffer has been sent out.
+ */
+#define FT_BUFFER_SIZE 2048
+
+typedef enum {
+	FT_STATUS_LISTENING	= 1,
+	FT_STATUS_TRANSFERRING	= 2,
+	FT_STATUS_FINISHED	= 4,
+	FT_STATUS_CANCELED	= 8,
+	FT_STATUS_CONNECTING	= 16
+} file_status_t;
+
+/*
+ * This structure holds all irc specific information regarding an incoming (from the point of view of
+ * the irc client) file transfer. New instances of this struct should only be created by calling the
+ * imcb_file_send_start() method, which will initialize most of the fields. The data field and the various
+ * methods are zero-initialized. Instances will automatically be deleted once the transfer is completed,
+ * canceled, or the connection to the irc client has been lost (note that also if only the irc connection
+ * and not the file transfer connection is lost, the file transfer will still be canceled and freed).
+ *
+ * The following (poor ascii-art) diagram illustrates what methods are called for which status-changes:
+ *
+ *	                        /-----------\                    /----------\
+ *	               -------> | LISTENING | -----------------> | CANCELED |
+ *	                        \-----------/  [canceled,]free   \----------/
+ *	                              |
+ *	                              | accept
+ *	                              V
+ *	               /------ /-------------\                    /------------------------\
+ *	   out_of_data |       | TRANSFERING | -----------------> | TRANSFERING | CANCELED |
+ *	               \-----> \-------------/  [canceled,]free   \------------------------/
+ *	                              |
+ *	                              | finished,free
+ *	                              V
+ *	                 /------------------------\
+ *	                 | TRANSFERING | FINISHED |
+ *	                 \------------------------/
+ */
+typedef struct file_transfer {
+
+	/* Are we sending something? */
+	int sending;
+
+	/*
+	 * The current status of this file transfer.
+	 */ 
+	file_status_t status;
+	
+	/*
+	 * file size
+	 */
+	size_t file_size;
+	
+	/*
+	 * Number of bytes that have been successfully transferred.
+	 */
+	size_t bytes_transferred;
+
+	/*
+	 * Time started. Used to calculate kb/s.
+	 */
+	time_t started;
+
+	/*
+	 * file name
+	 */
+	char *file_name;
+
+	/*
+	 * A unique local ID for this file transfer.
+	 */
+	unsigned int local_id;
+
+	/*
+	 * IM-protocol specific data associated with this file transfer.
+	 */
+	gpointer data;
+	
+	/*
+	 * Private data.
+	 */
+	gpointer priv;
+	
+	/*
+	 * If set, called after succesful connection setup.
+	 */
+	void (*accept) ( struct file_transfer *file );
+	
+	/*
+	 * If set, called when the transfer is canceled or finished.
+	 * Subsequently, this structure will be freed.
+	 *
+	 */
+	void (*free) ( struct file_transfer *file );
+	
+	/*
+	 * If set, called when the transfer is finished and successful.
+	 */
+	void (*finished) ( struct file_transfer *file );
+	
+	/*
+	 * If set, called when the transfer is canceled.
+	 * ( canceled either by the transfer implementation or by
+	 *  a call to imcb_file_canceled )
+	 */
+	void (*canceled) ( struct file_transfer *file, char *reason );
+	
+	/*
+	 * called by the sending side to indicate that it is writable.
+	 * The callee should check if data is available and call the 
+	 * function(as seen below) if that is the case.
+	 */
+	gboolean (*write_request) ( struct file_transfer *file );
+
+	/*
+	 * When sending files, protocols register this function to receive data.
+	 * This should only be called once after write_request is called. The caller
+	 * should not read more data until write_request is called again. This technique
+	 * avoids buffering.
+	 */
+	gboolean (*write) (struct file_transfer *file, char *buffer, unsigned int len );
+
+	/* The send buffer associated with this transfer.
+	 * Since receivers always wait for a write_request call one is enough.
+	 */
+	char buffer[FT_BUFFER_SIZE];
+
+} file_transfer_t;
+
+/*
+ * This starts a file transfer from bitlbee to the user.
+ */
+file_transfer_t *imcb_file_send_start( struct im_connection *ic, char *user_nick, char *file_name, size_t file_size );
+
+/*
+ * This should be called by a protocol when the transfer is canceled. Note that
+ * the canceled() and free() callbacks given in file will be called by this function.
+ */
+void imcb_file_canceled( file_transfer_t *file, char *reason );
+
+gboolean imcb_file_recv_start( file_transfer_t *ft );
+
+void imcb_file_finished( file_transfer_t *file );
+#endif
Index: protocols/jabber/Makefile
===================================================================
--- protocols/jabber/Makefile	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ protocols/jabber/Makefile	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -10,5 +10,5 @@
 
 # [SH] Program variables
-objects = conference.o io.o iq.o jabber.o jabber_util.o message.o presence.o sasl.o
+objects = conference.o io.o iq.o jabber.o jabber_util.o message.o presence.o s5bytestream.o sasl.o si.o
 
 CFLAGS += -Wall
Index: protocols/jabber/io.c
===================================================================
--- protocols/jabber/io.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ protocols/jabber/io.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -64,5 +64,5 @@
 		   most cases it probably won't be necessary.) */
 		if( ( ret = jabber_write_queue( ic ) ) && jd->tx_len > 0 )
-			jd->w_inpa = b_input_add( jd->fd, GAIM_INPUT_WRITE, jabber_write_callback, ic );
+			jd->w_inpa = b_input_add( jd->fd, B_EV_IO_WRITE, jabber_write_callback, ic );
 	}
 	else
@@ -504,5 +504,5 @@
 	
 	if( jd->r_inpa <= 0 )
-		jd->r_inpa = b_input_add( jd->fd, GAIM_INPUT_READ, jabber_read_callback, ic );
+		jd->r_inpa = b_input_add( jd->fd, B_EV_IO_READ, jabber_read_callback, ic );
 	
 	greet = g_strdup_printf( "%s<stream:stream to=\"%s\" xmlns=\"jabber:client\" "
Index: protocols/jabber/iq.c
===================================================================
--- protocols/jabber/iq.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ protocols/jabber/iq.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -91,7 +91,7 @@
 			pack = 0;
 		}
-		else if( strcmp( s, XMLNS_DISCOVER ) == 0 )
-		{
-			const char *features[] = { XMLNS_DISCOVER,
+		else if( strcmp( s, XMLNS_DISCO_INFO ) == 0 )
+		{
+			const char *features[] = { XMLNS_DISCO_INFO,
 			                           XMLNS_VERSION,
 			                           XMLNS_TIME,
@@ -99,4 +99,7 @@
 			                           XMLNS_MUC,
 			                           XMLNS_PING,
+			                           XMLNS_SI,
+			                           XMLNS_BYTESTREAMS,
+			                           XMLNS_FILETRANSFER,
 			                           NULL };
 			const char **f;
@@ -118,5 +121,5 @@
 		{
 			xt_free_node( reply );
-			reply = jabber_make_error_packet( node, "feature-not-implemented", "cancel" );
+			reply = jabber_make_error_packet( node, "feature-not-implemented", "cancel", NULL );
 			pack = 0;
 		}
@@ -124,16 +127,21 @@
 	else if( strcmp( type, "set" ) == 0 )
 	{
-		if( !( c = xt_find_node( node->children, "query" ) ) ||
-		    !( s = xt_find_attr( c, "xmlns" ) ) )
+		if( ( c = xt_find_node( node->children, "si" ) ) &&
+		    ( s = xt_find_attr( c, "xmlns" ) ) &&
+		    ( strcmp( s, XMLNS_SI ) == 0 ) )
+		{
+			return jabber_si_handle_request( ic, node, c );
+		}
+		else if( !( c = xt_find_node( node->children, "query" ) ) ||
+		         !( s = xt_find_attr( c, "xmlns" ) ) )
 		{
 			imcb_log( ic, "Warning: Received incomplete IQ-%s packet", type );
 			return XT_HANDLED;
 		}
-		
+		else if( strcmp( s, XMLNS_ROSTER ) == 0 )
+		{
 		/* This is a roster push. XMPP servers send this when someone
 		   was added to (or removed from) the buddy list. AFAIK they're
 		   sent even if we added this buddy in our own session. */
-		if( strcmp( s, XMLNS_ROSTER ) == 0 )
-		{
 			int bare_len = strlen( ic->acc->user );
 			
@@ -152,12 +160,17 @@
 				
 				xt_free_node( reply );
-				reply = jabber_make_error_packet( node, "not-allowed", "cancel" );
+				reply = jabber_make_error_packet( node, "not-allowed", "cancel", NULL );
 				pack = 0;
 			}
 		}
+		else if( strcmp( s, XMLNS_BYTESTREAMS ) == 0 )
+		{
+			/* Bytestream Request (stage 2 of file transfer) */
+			return jabber_bs_recv_request( ic, node, c );
+		}
 		else
 		{
 			xt_free_node( reply );
-			reply = jabber_make_error_packet( node, "feature-not-implemented", "cancel" );
+			reply = jabber_make_error_packet( node, "feature-not-implemented", "cancel", NULL );
 			pack = 0;
 		}
@@ -609,2 +622,174 @@
 	return st;
 }
+
+xt_status jabber_iq_parse_features( struct im_connection *ic, struct xt_node *node, struct xt_node *orig );
+
+xt_status jabber_iq_query_features( struct im_connection *ic, char *bare_jid )
+{
+	struct xt_node *node, *query;
+	struct jabber_buddy *bud;
+	
+	if( ( bud = jabber_buddy_by_jid( ic, bare_jid , 0 ) ) == NULL )
+	{
+		/* Who cares about the unknown... */
+		imcb_log( ic, "Couldn't find buddy: %s", bare_jid);
+		return XT_HANDLED;
+	}
+	
+	if( bud->features ) /* been here already */
+		return XT_HANDLED;
+	
+	node = xt_new_node( "query", NULL, NULL );
+	xt_add_attr( node, "xmlns", XMLNS_DISCO_INFO );
+	
+	if( !( query = jabber_make_packet( "iq", "get", bare_jid, node ) ) )
+	{
+		imcb_log( ic, "WARNING: Couldn't generate feature query" );
+		xt_free_node( node );
+		return XT_HANDLED;
+	}
+
+	jabber_cache_add( ic, query, jabber_iq_parse_features );
+
+	return jabber_write_packet( ic, query ) ? XT_HANDLED : XT_ABORT;
+}
+
+xt_status jabber_iq_parse_features( struct im_connection *ic, struct xt_node *node, struct xt_node *orig )
+{
+	struct xt_node *c;
+	struct jabber_buddy *bud;
+	char *feature, *xmlns, *from;
+
+	if( !( from = xt_find_attr( node, "from" ) ) ||
+	    !( c = xt_find_node( node->children, "query" ) ) ||
+	    !( xmlns = xt_find_attr( c, "xmlns" ) ) ||
+	    !( strcmp( xmlns, XMLNS_DISCO_INFO ) == 0 ) )
+	{
+		imcb_log( ic, "WARNING: Received incomplete IQ-result packet for discover" );
+		return XT_HANDLED;
+	}
+	if( ( bud = jabber_buddy_by_jid( ic, from, 0 ) ) == NULL )
+	{
+		/* Who cares about the unknown... */
+		imcb_log( ic, "Couldn't find buddy: %s", from );
+		return XT_HANDLED;
+	}
+	
+	c = c->children;
+	while( ( c = xt_find_node( c, "feature" ) ) )
+	{
+		feature = xt_find_attr( c, "var" );
+		if( feature )
+			bud->features = g_slist_append( bud->features, g_strdup( feature ) );
+		c = c->next;
+	}
+
+	return XT_HANDLED;
+}
+
+xt_status jabber_iq_parse_server_features( struct im_connection *ic, struct xt_node *node, struct xt_node *orig );
+
+xt_status jabber_iq_query_server( struct im_connection *ic, char *jid, char *xmlns )
+{
+	struct xt_node *node, *query;
+	struct jabber_data *jd = ic->proto_data;
+	
+	node = xt_new_node( "query", NULL, NULL );
+	xt_add_attr( node, "xmlns", xmlns );
+	
+	if( !( query = jabber_make_packet( "iq", "get", jid, node ) ) )
+	{
+		imcb_log( ic, "WARNING: Couldn't generate server query" );
+		xt_free_node( node );
+	}
+
+	jd->have_streamhosts--;
+	jabber_cache_add( ic, query, jabber_iq_parse_server_features );
+
+	return jabber_write_packet( ic, query ) ? XT_HANDLED : XT_ABORT;
+}
+
+/*
+ * Query the server for "items", query each "item" for identities, query each "item" that's a proxy for it's bytestream info
+ */
+xt_status jabber_iq_parse_server_features( struct im_connection *ic, struct xt_node *node, struct xt_node *orig )
+{
+	struct xt_node *c;
+	struct jabber_data *jd = ic->proto_data;
+	char *xmlns, *from;
+
+	if( !( c = xt_find_node( node->children, "query" ) ) ||
+	    !( from = xt_find_attr( node, "from" ) ) ||
+	    !( xmlns = xt_find_attr( c, "xmlns" ) ) )
+	{
+		imcb_log( ic, "WARNING: Received incomplete IQ-result packet for discover" );
+		return XT_HANDLED;
+	}
+
+	jd->have_streamhosts++;
+
+	if( strcmp( xmlns, XMLNS_DISCO_ITEMS ) == 0 )
+	{
+		char *itemjid;
+
+		/* answer from server */
+	
+		c = c->children;
+		while( ( c = xt_find_node( c, "item" ) ) )
+		{
+			itemjid = xt_find_attr( c, "jid" );
+			
+			if( itemjid )
+				jabber_iq_query_server( ic, itemjid, XMLNS_DISCO_INFO );
+
+			c = c->next;
+		}
+	}
+	else if( strcmp( xmlns, XMLNS_DISCO_INFO ) == 0 )
+	{
+		char *category, *type;
+
+		/* answer from potential proxy */
+
+		c = c->children;
+		while( ( c = xt_find_node( c, "identity" ) ) )
+		{
+			category = xt_find_attr( c, "category" );
+			type = xt_find_attr( c, "type" );
+
+			if( type && ( strcmp( type, "bytestreams" ) == 0 ) &&
+			    category && ( strcmp( category, "proxy" ) == 0 ) )
+				jabber_iq_query_server( ic, from, XMLNS_BYTESTREAMS );
+
+			c = c->next;
+		}
+	}
+	else if( strcmp( xmlns, XMLNS_BYTESTREAMS ) == 0 )
+	{
+		char *host, *jid, *port_s;
+		int port;
+
+		/* answer from proxy */
+
+		if( ( c = xt_find_node( c->children, "streamhost" ) ) &&
+		    ( host = xt_find_attr( c, "host" ) ) &&
+		    ( port_s = xt_find_attr( c, "port" ) ) &&
+		    ( sscanf( port_s, "%d", &port ) == 1 ) &&
+		    ( jid = xt_find_attr( c, "jid" ) ) )
+		{
+			jabber_streamhost_t *sh = g_new0( jabber_streamhost_t, 1 );
+			
+			sh->jid = g_strdup( jid );
+			sh->host = g_strdup( host );
+			g_snprintf( sh->port, sizeof( sh->port ), "%u", port );
+
+			imcb_log( ic, "Proxy found: jid %s host %s port %u", jid, host, port );
+			jd->streamhosts = g_slist_append( jd->streamhosts, sh );
+		}
+	}
+
+	if( jd->have_streamhosts == 0 )
+		jd->have_streamhosts++;
+
+	return XT_HANDLED;
+}
Index: protocols/jabber/jabber.c
===================================================================
--- protocols/jabber/jabber.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ protocols/jabber/jabber.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -65,4 +65,6 @@
 	
 	s = set_add( &acc->set, "priority", "0", set_eval_priority, acc );
+
+	s = set_add( &acc->set, "proxy", "<local>;<auto>", NULL, acc );
 	
 	s = set_add( &acc->set, "resource", "BitlBee", NULL, acc );
@@ -264,4 +266,16 @@
 	struct jabber_data *jd = ic->proto_data;
 	
+	while( jd->filetransfers )
+		imcb_file_canceled( ( ( struct jabber_transfer *) jd->filetransfers->data )->ft, "Logging out" );
+
+	while( jd->streamhosts )
+	{
+		jabber_streamhost_t *sh = jd->streamhosts->data;
+		jd->streamhosts = g_slist_remove( jd->streamhosts, sh );
+		g_free( sh->jid );
+		g_free( sh->host );
+		g_free( sh );
+	}
+
 	if( jd->fd >= 0 )
 		jabber_end_stream( ic );
@@ -544,4 +558,5 @@
 	ret->send_typing = jabber_send_typing;
 	ret->handle_cmp = g_strcasecmp;
+	ret->transfer_request = jabber_si_transfer_request;
 
 	register_protocol( ret );
Index: protocols/jabber/jabber.h
===================================================================
--- protocols/jabber/jabber.h	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ protocols/jabber/jabber.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -61,4 +61,12 @@
 } jabber_buddy_flags_t;
 
+/* Stores a streamhost's (a.k.a. proxy) data */
+typedef struct
+{
+	char *jid;
+	char *host;
+	char port[6];
+} jabber_streamhost_t;
+
 typedef enum
 {
@@ -91,4 +99,8 @@
 	GHashTable *node_cache;
 	GHashTable *buddies;
+
+	GSList *filetransfers;
+	GSList *streamhosts;
+	int have_streamhosts;
 };
 
@@ -127,4 +139,5 @@
 	struct jabber_away_state *away_state;
 	char *away_message;
+	GSList *features;
 	
 	time_t last_msg;
@@ -140,4 +153,34 @@
 	char *my_full_jid; /* Separate copy because of case sensitivity. */
 	struct jabber_buddy *me;
+};
+
+struct jabber_transfer
+{
+	/* bitlbee's handle for this transfer */
+	file_transfer_t *ft;
+
+	/* the stream's private handle */
+	gpointer streamhandle;
+
+	/* timeout for discover queries */
+	gint disco_timeout;
+	gint disco_timeout_fired;
+
+	struct im_connection *ic;
+
+	struct jabber_buddy *bud;
+
+	int watch_in;
+	int watch_out;
+
+	char *ini_jid;
+	char *tgt_jid;
+	char *iq_id;
+	char *sid;
+	int accepted;
+
+	size_t bytesread, byteswritten;
+	int fd;
+	struct sockaddr_storage saddr;
 };
 
@@ -167,15 +210,22 @@
 
 /* Some supported extensions/legacy stuff */
-#define XMLNS_AUTH         "jabber:iq:auth"                     /* XEP-0078 */
-#define XMLNS_VERSION      "jabber:iq:version"                  /* XEP-0092 */
-#define XMLNS_TIME         "jabber:iq:time"                     /* XEP-0090 */
-#define XMLNS_PING         "urn:xmpp:ping"                      /* XEP-0199 */
-#define XMLNS_VCARD        "vcard-temp"                         /* XEP-0054 */
-#define XMLNS_DELAY        "jabber:x:delay"                     /* XEP-0091 */
-#define XMLNS_CHATSTATES   "http://jabber.org/protocol/chatstates"  /* 0085 */
-#define XMLNS_DISCOVER     "http://jabber.org/protocol/disco#info"  /* 0030 */
-#define XMLNS_MUC          "http://jabber.org/protocol/muc"     /* XEP-0045 */
-#define XMLNS_MUC_USER     "http://jabber.org/protocol/muc#user"/* XEP-0045 */
-#define XMLNS_CAPS         "http://jabber.org/protocol/caps"    /* XEP-0115 */
+#define XMLNS_AUTH         "jabber:iq:auth"                                      /* XEP-0078 */
+#define XMLNS_VERSION      "jabber:iq:version"                                   /* XEP-0092 */
+#define XMLNS_TIME         "jabber:iq:time"                                      /* XEP-0090 */
+#define XMLNS_PING         "urn:xmpp:ping"                                       /* XEP-0199 */
+#define XMLNS_VCARD        "vcard-temp"                                          /* XEP-0054 */
+#define XMLNS_DELAY        "jabber:x:delay"                                      /* XEP-0091 */
+#define XMLNS_XDATA        "jabber:x:data"                                       /* XEP-0004 */
+#define XMLNS_CHATSTATES   "http://jabber.org/protocol/chatstates"               /* XEP-0085 */
+#define XMLNS_DISCO_INFO   "http://jabber.org/protocol/disco#info"               /* XEP-0030 */
+#define XMLNS_DISCO_ITEMS  "http://jabber.org/protocol/disco#items"              /* XEP-0030 */
+#define XMLNS_MUC          "http://jabber.org/protocol/muc"                      /* XEP-0045 */
+#define XMLNS_MUC_USER     "http://jabber.org/protocol/muc#user"                 /* XEP-0045 */
+#define XMLNS_CAPS         "http://jabber.org/protocol/caps"                     /* XEP-0115 */
+#define XMLNS_FEATURE      "http://jabber.org/protocol/feature-neg"              /* XEP-0020 */
+#define XMLNS_SI           "http://jabber.org/protocol/si"                       /* XEP-0095 */
+#define XMLNS_FILETRANSFER "http://jabber.org/protocol/si/profile/file-transfer" /* XEP-0096 */
+#define XMLNS_BYTESTREAMS  "http://jabber.org/protocol/bytestreams"              /* XEP-0065 */
+#define XMLNS_IBB          "http://jabber.org/protocol/ibb"                      /* XEP-0047 */
 
 /* iq.c */
@@ -187,4 +237,16 @@
 int jabber_add_to_roster( struct im_connection *ic, char *handle, char *name );
 int jabber_remove_from_roster( struct im_connection *ic, char *handle );
+xt_status jabber_iq_query_features( struct im_connection *ic, char *bare_jid );
+xt_status jabber_iq_query_server( struct im_connection *ic, char *jid, char *xmlns );
+
+/* si.c */
+int jabber_si_handle_request( struct im_connection *ic, struct xt_node *node, struct xt_node *sinode );
+void jabber_si_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *who );
+void jabber_si_free_transfer( file_transfer_t *ft);
+
+/* s5bytestream.c */
+int jabber_bs_recv_request( struct im_connection *ic, struct xt_node *node, struct xt_node *qnode);
+gboolean jabber_bs_send_start( struct jabber_transfer *tf );
+gboolean jabber_bs_send_write( file_transfer_t *ft, char *buffer, unsigned int len );
 
 /* message.c */
@@ -200,5 +262,5 @@
 char *set_eval_tls( set_t *set, char *value );
 struct xt_node *jabber_make_packet( char *name, char *type, char *to, struct xt_node *children );
-struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type );
+struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type, char *err_code );
 void jabber_cache_add( struct im_connection *ic, struct xt_node *node, jabber_cache_event func );
 struct xt_node *jabber_cache_get( struct im_connection *ic, char *id );
Index: protocols/jabber/jabber_util.c
===================================================================
--- protocols/jabber/jabber_util.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ protocols/jabber/jabber_util.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -99,5 +99,5 @@
 }
 
-struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type )
+struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type, char *err_code )
 {
 	struct xt_node *node, *c;
@@ -111,4 +111,8 @@
 	c = xt_new_node( "error", NULL, c );
 	xt_add_attr( c, "type", err_type );
+	
+	/* Add the error code, if present */
+	if (err_code)
+		xt_add_attr( c, "code", err_code );
 	
 	/* To make the actual error packet, we copy the original packet and
Index: protocols/jabber/s5bytestream.c
===================================================================
--- protocols/jabber/s5bytestream.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
+++ protocols/jabber/s5bytestream.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -0,0 +1,1153 @@
+/***************************************************************************\
+*                                                                           *
+*  BitlBee - An IRC to IM gateway                                           *
+*  Jabber module - SOCKS5 Bytestreams ( XEP-0065 )                          *
+*                                                                           *
+*  Copyright 2007 Uli Meis <a.sporto+bee@gmail.com>                         *
+*                                                                           *
+*  This program is free software; you can redistribute it and/or modify     *
+*  it under the terms of the GNU General Public License as published by     *
+*  the Free Software Foundation; either version 2 of the License, or        *
+*  (at your option) any later version.                                      *
+*                                                                           *
+*  This program is distributed in the hope that it will be useful,          *
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
+*  GNU General Public License for more details.                             *
+*                                                                           *
+*  You should have received a copy of the GNU General Public License along  *
+*  with this program; if not, write to the Free Software Foundation, Inc.,  *
+*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              *
+*                                                                           *
+\***************************************************************************/
+
+#include "jabber.h"
+#include "sha1.h"
+#include "lib/ftutil.h"
+#include <poll.h>
+
+struct bs_transfer {
+
+	struct jabber_transfer *tf;
+
+	jabber_streamhost_t *sh;
+	GSList *streamhosts;
+
+	enum 
+	{ 
+		BS_PHASE_CONNECT, 
+		BS_PHASE_CONNECTED, 
+		BS_PHASE_REQUEST, 
+		BS_PHASE_REPLY
+	} phase;
+
+	/* SHA1( SID + Initiator JID + Target JID) */
+	char *pseudoadr;
+
+	gint connect_timeout;
+	
+	char peek_buf[64];
+	int peek_buf_len;
+};
+
+struct socks5_message
+{
+	unsigned char ver;
+	union
+	{
+		unsigned char cmd;
+		unsigned char rep;
+	} cmdrep;
+	unsigned char rsv;
+	unsigned char atyp;
+	unsigned char addrlen;
+	unsigned char address[40];
+	in_port_t port;
+} __attribute__ ((packed)); 
+
+char *socks5_reply_code[] = {
+	"succeeded",
+	"general SOCKS server failure",
+	"connection not allowed by ruleset",
+	"Network unreachable",
+	"Host unreachable",
+	"Connection refused",
+	"TTL expired",
+	"Command not supported",
+	"Address type not supported",
+	"unassigned"};
+
+/* connect() timeout in seconds. */
+#define JABBER_BS_CONTIMEOUT 15
+/* listen timeout */
+#define JABBER_BS_LISTEN_TIMEOUT  90
+
+/* very useful */
+#define ASSERTSOCKOP(op, msg) \
+	if( (op) == -1 ) \
+		return jabber_bs_abort( bt , msg ": %s", strerror( errno ) );
+
+gboolean jabber_bs_abort( struct bs_transfer *bt, char *format, ... );
+void jabber_bs_canceled( file_transfer_t *ft , char *reason );
+void jabber_bs_free_transfer( file_transfer_t *ft );
+gboolean jabber_bs_connect_timeout( gpointer data, gint fd, b_input_condition cond );
+gboolean jabber_bs_poll( struct bs_transfer *bt, int fd, short *revents );
+gboolean jabber_bs_peek( struct bs_transfer *bt, void *buffer, int buflen );
+
+void jabber_bs_recv_answer_request( struct bs_transfer *bt );
+gboolean jabber_bs_recv_read( gpointer data, gint fd, b_input_condition cond );
+gboolean jabber_bs_recv_write_request( file_transfer_t *ft );
+gboolean jabber_bs_recv_handshake( gpointer data, gint fd, b_input_condition cond );
+gboolean jabber_bs_recv_handshake_abort( struct bs_transfer *bt, char *error );
+int jabber_bs_recv_request( struct im_connection *ic, struct xt_node *node, struct xt_node *qnode );
+
+gboolean jabber_bs_send_handshake_abort( struct bs_transfer *bt, char *error );
+gboolean jabber_bs_send_request( struct jabber_transfer *tf, GSList *streamhosts );
+gboolean jabber_bs_send_handshake( gpointer data, gint fd, b_input_condition cond );
+static xt_status jabber_bs_send_handle_activate( struct im_connection *ic, struct xt_node *node, struct xt_node *orig );
+void jabber_bs_send_activate( struct bs_transfer *bt );
+
+/*
+ * Frees a bs_transfer struct and calls the SI free function
+ */
+void jabber_bs_free_transfer( file_transfer_t *ft) {
+	struct jabber_transfer *tf = ft->data;
+	struct bs_transfer *bt = tf->streamhandle;
+	jabber_streamhost_t *sh;
+
+	if ( bt->connect_timeout )
+	{
+		b_event_remove( bt->connect_timeout );
+		bt->connect_timeout = 0;
+	}
+
+	if ( tf->watch_in )
+		b_event_remove( tf->watch_in );
+	
+	if( tf->watch_out )
+		b_event_remove( tf->watch_out );
+	
+	g_free( bt->pseudoadr );
+
+	while( bt->streamhosts )
+	{
+		sh = bt->streamhosts->data;
+		bt->streamhosts = g_slist_remove( bt->streamhosts, sh );
+		g_free( sh->jid );
+		g_free( sh->host );
+		g_free( sh );
+	}
+	
+	g_free( bt );
+
+	jabber_si_free_transfer( ft );
+}
+
+/*
+ * Checks if buflen data is available on the socket and
+ * writes it to buffer if that's the case.
+ */
+gboolean jabber_bs_peek( struct bs_transfer *bt, void *buffer, int buflen )
+{
+	int ret;
+	int fd = bt->tf->fd;
+
+	if( buflen > sizeof( bt->peek_buf ) )
+		return jabber_bs_abort( bt, "BUG: %d > sizeof(peek_buf)", buflen );
+
+	ASSERTSOCKOP( ret = recv( fd, bt->peek_buf + bt->peek_buf_len,
+		buflen - bt->peek_buf_len, 0 ), "recv() on SOCKS5 connection" );
+
+	if( ret == 0 )
+		return jabber_bs_abort( bt, "Remote end closed connection" );
+	
+	bt->peek_buf_len += ret;
+	memcpy( buffer, bt->peek_buf, bt->peek_buf_len );
+	
+	if( bt->peek_buf_len == buflen )
+	{
+		/* If we have everything the caller wanted, reset the peek buffer. */
+		bt->peek_buf_len = 0;
+		return buflen;
+	}
+	else
+		return bt->peek_buf_len;
+}
+
+
+/* 
+ * This function is scheduled in bs_handshake via b_timeout_add after a (non-blocking) connect().
+ */
+gboolean jabber_bs_connect_timeout( gpointer data, gint fd, b_input_condition cond )
+{
+	struct bs_transfer *bt = data;
+
+	bt->connect_timeout = 0;
+
+	jabber_bs_abort( bt, "no connection after %d seconds", bt->tf->ft->sending ? JABBER_BS_LISTEN_TIMEOUT : JABBER_BS_CONTIMEOUT );
+
+	return FALSE;
+}
+
+/* 
+ * Polls the socket, checks for errors and removes a connect timer
+ * if there is one.
+ */
+gboolean jabber_bs_poll( struct bs_transfer *bt, int fd, short *revents )
+{
+	struct pollfd pfd = { .fd = fd, .events = POLLHUP|POLLERR };
+	
+	if ( bt->connect_timeout )
+	{
+		b_event_remove( bt->connect_timeout );
+		bt->connect_timeout = 0;
+	}
+
+	ASSERTSOCKOP( poll( &pfd, 1, 0 ), "poll()" )
+
+	if( pfd.revents & POLLERR )
+	{
+		int sockerror;
+		socklen_t errlen = sizeof( sockerror );
+
+		if ( getsockopt( fd, SOL_SOCKET, SO_ERROR, &sockerror, &errlen ) )
+			return jabber_bs_abort( bt, "getsockopt() failed, unknown socket error during SOCKS5 handshake (weird!)" );
+
+		if ( bt->phase == BS_PHASE_CONNECTED )
+			return jabber_bs_abort( bt, "connect failed: %s", strerror( sockerror ) );
+
+		return jabber_bs_abort( bt, "Socket error during SOCKS5 handshake(weird!): %s", strerror( sockerror ) );
+	}
+
+	if( pfd.revents & POLLHUP )
+		return jabber_bs_abort( bt, "Remote end closed connection" );
+	
+	*revents = pfd.revents;
+	
+	return TRUE;
+}
+
+/*
+ * Used for receive and send path.
+ */
+gboolean jabber_bs_abort( struct bs_transfer *bt, char *format, ... )
+{
+	va_list params;
+	va_start( params, format );
+	char error[128];
+
+	if( vsnprintf( error, 128, format, params ) < 0 )
+		sprintf( error, "internal error parsing error string (BUG)" );
+	va_end( params );
+	if( bt->tf->ft->sending )
+		return jabber_bs_send_handshake_abort( bt, error );
+	else
+		return jabber_bs_recv_handshake_abort( bt, error );
+}
+
+/* Bad luck */
+void jabber_bs_canceled( file_transfer_t *ft , char *reason )
+{
+	struct jabber_transfer *tf = ft->data;
+
+	imcb_log( tf->ic, "File transfer aborted: %s", reason );
+}
+
+/*
+ * Parses an incoming bytestream request and calls jabber_bs_handshake on success.
+ */
+int jabber_bs_recv_request( struct im_connection *ic, struct xt_node *node, struct xt_node *qnode)
+{
+	char *sid, *ini_jid, *tgt_jid, *mode, *iq_id;
+	struct jabber_data *jd = ic->proto_data;
+	struct jabber_transfer *tf = NULL;
+	GSList *tflist;
+	struct bs_transfer *bt;
+	GSList *shlist=NULL;
+	struct xt_node *shnode;
+
+	sha1_state_t sha;
+	char hash_hex[41];
+	unsigned char hash[20];
+	int i;
+	
+	if( !(iq_id   = xt_find_attr( node, "id" ) ) ||
+	    !(ini_jid = xt_find_attr( node, "from" ) ) ||
+	    !(tgt_jid = xt_find_attr( node, "to" ) ) ||
+	    !(sid     = xt_find_attr( qnode, "sid" ) ) )
+	{
+		imcb_log( ic, "WARNING: Received incomplete SI bytestream request");
+		return XT_HANDLED;
+	}
+
+	if( ( mode = xt_find_attr( qnode, "mode" ) ) &&
+	      ( strcmp( mode, "tcp" ) != 0 ) ) 
+	{
+		imcb_log( ic, "WARNING: Received SI Request for unsupported bytestream mode %s", xt_find_attr( qnode, "mode" ) );
+		return XT_HANDLED;
+	}
+
+	shnode = qnode->children;
+	while( ( shnode = xt_find_node( shnode, "streamhost" ) ) )
+	{
+		char *jid, *host, *port_s;
+		int port;
+		if( ( jid = xt_find_attr( shnode, "jid" ) ) &&
+		    ( host = xt_find_attr( shnode, "host" ) ) &&
+		    ( port_s = xt_find_attr( shnode, "port" ) ) &&
+		    ( sscanf( port_s, "%d", &port ) == 1 ) )
+		{
+			jabber_streamhost_t *sh = g_new0( jabber_streamhost_t, 1 );
+			sh->jid = g_strdup(jid);
+			sh->host = g_strdup(host);
+			sprintf( sh->port, "%u", port );
+			shlist = g_slist_append( shlist, sh );
+		}
+		shnode = shnode->next;
+	}
+	
+	if( !shlist )
+	{
+		imcb_log( ic, "WARNING: Received incomplete SI bytestream request, no parseable streamhost entries");
+		return XT_HANDLED;
+	}
+
+	/* Let's see if we can find out what this bytestream should be for... */
+
+	for( tflist = jd->filetransfers ; tflist; tflist = g_slist_next(tflist) )
+	{
+		struct jabber_transfer *tft = tflist->data;
+		if( ( strcmp( tft->sid, sid ) == 0 ) &&
+		    ( strcmp( tft->ini_jid, ini_jid ) == 0 ) &&
+		    ( strcmp( tft->tgt_jid, tgt_jid ) == 0 ) )
+		{
+		    	tf = tft;
+			break;
+		}
+	}
+
+	if (!tf) 
+	{
+		imcb_log( ic, "WARNING: Received bytestream request from %s that doesn't match an SI request", ini_jid );
+		return XT_HANDLED;
+	}
+
+	/* iq_id and canceled can be reused since SI is done */
+	g_free( tf->iq_id );
+	tf->iq_id = g_strdup( iq_id );
+
+	tf->ft->canceled = jabber_bs_canceled;
+
+	/* SHA1( SID + Initiator JID + Target JID ) is given to the streamhost which it will match against the initiator's value */
+	sha1_init( &sha );
+	sha1_append( &sha, (unsigned char*) sid, strlen( sid ) );
+	sha1_append( &sha, (unsigned char*) ini_jid, strlen( ini_jid ) );
+	sha1_append( &sha, (unsigned char*) tgt_jid, strlen( tgt_jid ) );
+	sha1_finish( &sha, hash );
+	
+	for( i = 0; i < 20; i ++ )
+		sprintf( hash_hex + i * 2, "%02x", hash[i] );
+		
+	bt = g_new0( struct bs_transfer, 1 );
+	bt->tf = tf;
+	bt->streamhosts = shlist;
+	bt->sh = shlist->data;
+	bt->phase = BS_PHASE_CONNECT;
+	bt->pseudoadr = g_strdup( hash_hex );
+	tf->streamhandle = bt;
+	tf->ft->free = jabber_bs_free_transfer;
+
+	jabber_bs_recv_handshake( bt, -1, 0 ); 
+
+	return XT_HANDLED;
+}
+
+/*
+ * This is what a protocol handshake can look like in cooperative multitasking :)
+ * Might be confusing at first because it's called from different places and is recursing.
+ * (places being the event thread, bs_request, bs_handshake_abort, and itself)
+ *
+ * All in all, it turned out quite nice :)
+ */
+gboolean jabber_bs_recv_handshake( gpointer data, gint fd, b_input_condition cond )
+{
+
+	struct bs_transfer *bt = data;
+	short revents;
+	int gret;
+
+	if ( ( fd != -1 ) && !jabber_bs_poll( bt, fd, &revents ) )
+		return FALSE;
+	
+	switch( bt->phase ) 
+	{
+	case BS_PHASE_CONNECT:
+		{
+			struct addrinfo hints, *rp;
+
+			memset( &hints, 0, sizeof( struct addrinfo ) );
+			hints.ai_socktype = SOCK_STREAM;
+
+			if ( ( gret = getaddrinfo( bt->sh->host, bt->sh->port, &hints, &rp ) ) != 0 )
+				return jabber_bs_abort( bt, "getaddrinfo() failed: %s", gai_strerror( gret ) );
+
+			ASSERTSOCKOP( bt->tf->fd = fd = socket( rp->ai_family, rp->ai_socktype, 0 ), "Opening socket" );
+
+			sock_make_nonblocking( fd );
+
+			imcb_log( bt->tf->ic, "File %s: Connecting to streamhost %s:%s", bt->tf->ft->file_name, bt->sh->host, bt->sh->port );
+
+			if( ( connect( fd, rp->ai_addr, rp->ai_addrlen ) == -1 ) &&
+			    ( errno != EINPROGRESS ) )
+				return jabber_bs_abort( bt , "connect() failed: %s", strerror( errno ) );
+
+			freeaddrinfo( rp );
+
+			bt->phase = BS_PHASE_CONNECTED;
+			
+			bt->tf->watch_out = b_input_add( fd, GAIM_INPUT_WRITE, jabber_bs_recv_handshake, bt );
+
+			/* since it takes forever(3mins?) till connect() fails on itself we schedule a timeout */
+			bt->connect_timeout = b_timeout_add( JABBER_BS_CONTIMEOUT * 1000, jabber_bs_connect_timeout, bt );
+
+			bt->tf->watch_in = 0;
+			return FALSE;
+		}
+	case BS_PHASE_CONNECTED:
+		{
+			struct {
+				unsigned char ver;
+				unsigned char nmethods;
+				unsigned char method;
+			} socks5_hello = {
+				.ver = 5,
+				.nmethods = 1,
+				.method = 0x00 /* no auth */
+				/* one could also implement username/password. If you know
+				 * a jabber client or proxy that actually does it, tell me.
+				 */
+			};
+			
+			ASSERTSOCKOP( send( fd, &socks5_hello, sizeof( socks5_hello ) , 0 ), "Sending auth request" );
+
+			bt->phase = BS_PHASE_REQUEST;
+
+			bt->tf->watch_in = b_input_add( fd, GAIM_INPUT_READ, jabber_bs_recv_handshake, bt );
+
+			bt->tf->watch_out = 0;
+			return FALSE;
+		}
+	case BS_PHASE_REQUEST:
+		{
+			struct socks5_message socks5_connect = 
+			{
+				.ver = 5,
+				.cmdrep.cmd = 0x01,
+				.rsv = 0,
+				.atyp = 0x03,
+				.addrlen = strlen( bt->pseudoadr ),
+				.port = 0
+			};
+			int ret;
+			char buf[2];
+
+			/* If someone's trying to be funny and sends only one byte at a time we'll fail :) */
+			ASSERTSOCKOP( ret = recv( fd, buf, 2, 0 ) , "Receiving auth reply" );
+
+			if( !( ret == 2 ) ||
+			    !( buf[0] == 5 ) ||
+			    !( buf[1] == 0 ) )
+				return jabber_bs_abort( bt, "Auth not accepted by streamhost (reply: len=%d, ver=%d, status=%d)",
+									ret, buf[0], buf[1] );
+
+			/* copy hash into connect message */
+			memcpy( socks5_connect.address, bt->pseudoadr, socks5_connect.addrlen );
+
+			ASSERTSOCKOP( send( fd, &socks5_connect, sizeof( struct socks5_message ), 0 ) , "Sending SOCKS5 Connect" );
+
+			bt->phase = BS_PHASE_REPLY;
+
+			return TRUE;
+		}
+	case BS_PHASE_REPLY:
+		{
+			struct socks5_message socks5_reply;
+			int ret;
+
+			if ( !( ret = jabber_bs_peek( bt, &socks5_reply, sizeof( struct socks5_message ) ) ) )
+				return FALSE;
+
+			if ( ret < 5 ) /* header up to address length */
+				return TRUE;
+			else if( ret < sizeof( struct socks5_message ) )
+			{
+				/* Either a buggy proxy or just one that doesnt regard the SHOULD in XEP-0065
+				 * saying the reply SHOULD contain the address */
+
+				ASSERTSOCKOP( ret = recv( fd, &socks5_reply, ret, 0 ), "Dequeuing after MSG_PEEK" );
+			}
+
+			if( !( socks5_reply.ver == 5 ) ||
+			    !( socks5_reply.cmdrep.rep == 0 ) ) {
+			    	char errstr[128] = "";
+				if( ( socks5_reply.ver == 5 ) && ( socks5_reply.cmdrep.rep < 
+				    ( sizeof( socks5_reply_code ) / sizeof( socks5_reply_code[0] ) ) ) ) {
+					sprintf( errstr, "with \"%s\" ", socks5_reply_code[ socks5_reply.cmdrep.rep ] );
+				}
+				return jabber_bs_abort( bt, "SOCKS5 CONNECT failed %s(reply: ver=%d, rep=%d, atyp=%d, addrlen=%d)", 
+					errstr,
+					socks5_reply.ver,
+					socks5_reply.cmdrep.rep,
+					socks5_reply.atyp,
+					socks5_reply.addrlen);
+			}
+			
+			/* usually a proxy sends back the 40 bytes address but I encountered at least one (of jabber.cz) 
+			 * that sends atyp=0 addrlen=0 and only 6 bytes (one less than one would expect).
+			 * Therefore I removed the wait for more bytes. Since we don't care about what else the proxy
+			 * is sending, it shouldnt matter */
+
+			if( bt->tf->ft->sending )
+				jabber_bs_send_activate( bt );
+			else
+				jabber_bs_recv_answer_request( bt );
+
+			return FALSE;
+		}
+	default:
+		/* BUG */
+		imcb_log( bt->tf->ic, "BUG in file transfer code: undefined handshake phase" );
+
+		bt->tf->watch_in = 0;
+		return FALSE;
+	}
+}
+
+/*
+ * If the handshake failed we can try the next streamhost, if there is one.
+ * An intelligent sender would probably specify himself as the first streamhost and
+ * a proxy as the second (Kopete and PSI are examples here). That way, a (potentially) 
+ * slow proxy is only used if neccessary. This of course also means, that the timeout
+ * per streamhost should be kept short. If one or two firewalled adresses are specified,
+ * they have to timeout first before a proxy is tried.
+ */
+gboolean jabber_bs_recv_handshake_abort( struct bs_transfer *bt, char *error )
+{
+	struct jabber_transfer *tf = bt->tf;
+	struct xt_node *reply, *iqnode;
+	GSList *shlist;
+
+	imcb_log( tf->ic, "Transferring file %s: connection to streamhost %s:%s failed (%s)", 
+		  tf->ft->file_name, 
+		  bt->sh->host,
+		  bt->sh->port,
+		  error );
+
+	/* Alright, this streamhost failed, let's try the next... */
+	bt->phase = BS_PHASE_CONNECT;
+	shlist = g_slist_find( bt->streamhosts, bt->sh );
+	if( shlist && shlist->next )
+	{
+		bt->sh = shlist->next->data;
+		return jabber_bs_recv_handshake( bt, -1, 0 );
+	}
+
+
+	/* out of stream hosts */
+
+	iqnode = jabber_make_packet( "iq", "result", tf->ini_jid, NULL );
+	reply = jabber_make_error_packet( iqnode, "item-not-found", "cancel" , "404" );
+	xt_free_node( iqnode );
+
+	xt_add_attr( reply, "id", tf->iq_id );
+		
+	if( !jabber_write_packet( tf->ic, reply ) )
+		imcb_log( tf->ic, "WARNING: Error transmitting bytestream response" );
+	xt_free_node( reply );
+
+	imcb_file_canceled( tf->ft, "couldn't connect to any streamhosts" );
+
+	bt->tf->watch_in = 0;
+	/* MUST always return FALSE! */
+	return FALSE;
+}
+
+/* 
+ * After the SOCKS5 handshake succeeds we need to inform the initiator which streamhost we chose.
+ * If he is the streamhost himself, he might already know that. However, if it's a proxy,
+ * the initiator will have to make a connection himself.
+ */
+void jabber_bs_recv_answer_request( struct bs_transfer *bt )
+{
+	struct jabber_transfer *tf = bt->tf;
+	struct xt_node *reply;
+
+	imcb_log( tf->ic, "File %s: established SOCKS5 connection to %s:%s", 
+		  tf->ft->file_name, 
+		  bt->sh->host,
+		  bt->sh->port );
+
+	tf->ft->data = tf;
+	tf->watch_in = b_input_add( tf->fd, GAIM_INPUT_READ, jabber_bs_recv_read, bt );
+	tf->ft->write_request = jabber_bs_recv_write_request;
+
+	reply = xt_new_node( "streamhost-used", NULL, NULL );
+	xt_add_attr( reply, "jid", bt->sh->jid );
+
+	reply = xt_new_node( "query", NULL, reply );
+	xt_add_attr( reply, "xmlns", XMLNS_BYTESTREAMS );
+
+	reply = jabber_make_packet( "iq", "result", tf->ini_jid, reply );
+
+	xt_add_attr( reply, "id", tf->iq_id );
+		
+	if( !jabber_write_packet( tf->ic, reply ) )
+		imcb_file_canceled( tf->ft, "Error transmitting bytestream response" );
+	xt_free_node( reply );
+}
+
+/* 
+ * This function is called from write_request directly. If no data is available, it will install itself
+ * as a watcher for input on fd and once that happens, deliver the data and unschedule itself again.
+ */
+gboolean jabber_bs_recv_read( gpointer data, gint fd, b_input_condition cond )
+{
+	int ret;
+	struct bs_transfer *bt = data;
+	struct jabber_transfer *tf = bt->tf;
+
+	if( fd != -1 ) /* called via event thread */
+	{
+		tf->watch_in = 0;
+		ASSERTSOCKOP( ret = recv( fd, tf->ft->buffer, sizeof( tf->ft->buffer ), 0 ) , "Receiving" );
+	}
+	else
+	{
+		/* called directly. There might not be any data available. */
+		if( ( ( ret = recv( tf->fd, tf->ft->buffer, sizeof( tf->ft->buffer ), 0 ) ) == -1 ) &&
+		    ( errno != EAGAIN ) )
+		    return jabber_bs_abort( bt, "Receiving: %s", strerror( errno ) );
+
+		if( ( ret == -1 ) && ( errno == EAGAIN ) )
+		{
+			tf->watch_in = b_input_add( tf->fd, GAIM_INPUT_READ, jabber_bs_recv_read, bt );
+			return FALSE;
+		}
+	}
+
+	/* shouldn't happen since we know the file size */
+	if( ret == 0 )
+		return jabber_bs_abort( bt, "Remote end closed connection" );
+	
+	tf->bytesread += ret;
+
+	if( tf->bytesread >= tf->ft->file_size )
+		imcb_file_finished( tf->ft );
+
+	tf->ft->write( tf->ft, tf->ft->buffer, ret );	
+
+	return FALSE;
+}
+
+/* 
+ * imc callback that is invoked when it is ready to receive some data.
+ */
+gboolean jabber_bs_recv_write_request( file_transfer_t *ft )
+{
+	struct jabber_transfer *tf = ft->data;
+
+	if( tf->watch_in )
+	{
+		imcb_file_canceled( ft, "BUG in jabber file transfer: write_request called when already watching for input" );
+		return FALSE;
+	}
+	
+	jabber_bs_recv_read( tf->streamhandle, -1 , 0 );
+
+	return TRUE;
+}
+
+/* 
+ * Issues a write_request to imc.
+ * */
+gboolean jabber_bs_send_can_write( gpointer data, gint fd, b_input_condition cond )
+{
+	struct bs_transfer *bt = data;
+
+	bt->tf->watch_out = 0;
+
+	bt->tf->ft->write_request( bt->tf->ft );
+
+	return FALSE;
+}
+
+/*
+ * This should only be called if we can write, so just do it.
+ * Add a write watch so we can write more during the next cycle (if possible).
+ */
+gboolean jabber_bs_send_write( file_transfer_t *ft, char *buffer, unsigned int len )
+{
+	struct jabber_transfer *tf = ft->data;
+	struct bs_transfer *bt = tf->streamhandle;
+	int ret;
+
+	if( tf->watch_out )
+		return jabber_bs_abort( bt, "BUG: write() called while watching " );
+	
+	/* TODO: catch broken pipe */
+	ASSERTSOCKOP( ret = send( tf->fd, buffer, len, 0 ), "Sending" );
+
+	tf->byteswritten += ret;
+	
+	/* TODO: this should really not be fatal */
+	if( ret < len )
+		return jabber_bs_abort( bt, "send() sent %d instead of %d (send buffer too big!)", ret, len );
+
+	if( tf->byteswritten >= ft->file_size )
+		imcb_file_finished( ft );
+	else
+		bt->tf->watch_out = b_input_add( tf->fd, GAIM_INPUT_WRITE, jabber_bs_send_can_write, bt );
+		
+	return TRUE;
+}
+
+/*
+ * Handles the reply by the receiver containing the used streamhost.
+ */
+static xt_status jabber_bs_send_handle_reply(struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) {
+	struct jabber_transfer *tf = NULL;
+	struct jabber_data *jd = ic->proto_data;
+	struct bs_transfer *bt;
+	GSList *tflist;
+	struct xt_node *c;
+	char *sid, *jid;
+
+	if( !( c = xt_find_node( node->children, "query" ) ) ||
+	    !( c = xt_find_node( c->children, "streamhost-used" ) ) ||
+	    !( jid = xt_find_attr( c, "jid" ) ) )
+
+	{
+		imcb_log( ic, "WARNING: Received incomplete bytestream reply" );
+		return XT_HANDLED;
+	}
+	
+	if( !( c = xt_find_node( orig->children, "query" ) ) ||
+	    !( sid = xt_find_attr( c, "sid" ) ) )
+	{
+		imcb_log( ic, "WARNING: Error parsing request corresponding to the incoming bytestream reply" );
+		return XT_HANDLED;
+	}
+
+	/* Let's see if we can find out what this bytestream should be for... */
+
+	for( tflist = jd->filetransfers ; tflist; tflist = g_slist_next(tflist) )
+	{
+		struct jabber_transfer *tft = tflist->data;
+		if( ( strcmp( tft->sid, sid ) == 0 ) )
+		{
+		    	tf = tft;
+			break;
+		}
+	}
+
+	if( !tf )
+	{
+		imcb_log( ic, "WARNING: Received SOCKS5 bytestream reply to unknown request" );
+		return XT_HANDLED;
+	}
+
+	bt = tf->streamhandle;
+
+	tf->accepted = TRUE;
+
+	if( strcmp( jid, tf->ini_jid ) == 0 )
+	{
+		/* we're streamhost and target */
+		if( bt->phase == BS_PHASE_REPLY )
+		{
+			/* handshake went through, let's start transferring */
+			tf->ft->write_request( tf->ft );
+		}
+	} else
+	{
+		/* using a proxy, abort listen */
+
+		if( tf->watch_in )
+		{
+			b_event_remove( tf->watch_in );
+			tf->watch_in = 0;
+		}
+		
+		if( tf->fd != -1 ) {
+			closesocket( tf->fd );
+			tf->fd = -1;
+		}
+
+		if ( bt->connect_timeout )
+		{
+			b_event_remove( bt->connect_timeout );
+			bt->connect_timeout = 0;
+		}
+
+		GSList *shlist;
+		for( shlist = jd->streamhosts ; shlist ; shlist = g_slist_next( shlist ) )
+		{
+			jabber_streamhost_t *sh = shlist->data;
+			if( strcmp( sh->jid, jid ) == 0 )
+			{
+				bt->sh = sh;
+				jabber_bs_recv_handshake( bt, -1, 0 );
+				return XT_HANDLED;
+			}
+		}
+
+		imcb_log( ic, "WARNING: Received SOCKS5 bytestream reply with unknown streamhost %s", jid );
+	}
+
+	return XT_HANDLED;
+}
+
+/* 
+ * Tell the proxy to activate the stream. Looks like this:
+ *
+ * <iq type=set>
+ * 	<query xmlns=bs sid=sid>
+ * 		<activate>tgt_jid</activate>
+ * 	</query>
+ * </iq>
+ */
+void jabber_bs_send_activate( struct bs_transfer *bt )
+{
+	struct xt_node *node;
+
+	node = xt_new_node( "activate", bt->tf->tgt_jid, NULL );
+	node = xt_new_node( "query", NULL, node );
+	xt_add_attr( node, "xmlns", XMLNS_BYTESTREAMS );
+	xt_add_attr( node, "sid", bt->tf->sid );
+	node = jabber_make_packet( "iq", "set", bt->sh->jid, node );
+
+	jabber_cache_add( bt->tf->ic, node, jabber_bs_send_handle_activate );
+
+	jabber_write_packet( bt->tf->ic, node );
+}
+
+/*
+ * The proxy has activated the bytestream.
+ * We can finally start pushing some data out.
+ */
+static xt_status jabber_bs_send_handle_activate( struct im_connection *ic, struct xt_node *node, struct xt_node *orig )
+{
+	char *sid;
+	GSList *tflist;
+	struct jabber_transfer *tf = NULL;
+	struct xt_node *query;
+	struct jabber_data *jd = ic->proto_data;
+
+	query = xt_find_node( orig->children, "query" );
+	sid = xt_find_attr( query, "sid" );
+
+	for( tflist = jd->filetransfers ; tflist; tflist = g_slist_next(tflist) )
+	{
+		struct jabber_transfer *tft = tflist->data;
+		if( ( strcmp( tft->sid, sid ) == 0 ) )
+		{
+		    	tf = tft;
+			break;
+		}
+	}
+
+	if( !tf )
+	{
+		imcb_log( ic, "WARNING: Received SOCKS5 bytestream activation for unknown stream" );
+		return XT_HANDLED;
+	}
+
+	imcb_log( tf->ic, "File %s: SOCKS5 handshake and activation successful! Transfer about to start...", tf->ft->file_name );
+
+	/* handshake went through, let's start transferring */
+	tf->ft->write_request( tf->ft );
+
+	return XT_HANDLED;
+}
+
+jabber_streamhost_t *jabber_si_parse_proxy( struct im_connection *ic, char *proxy )
+{
+	char *host, *port, *jid;
+	jabber_streamhost_t *sh;
+
+	if( ( ( host = strchr( proxy, ',' ) ) == 0 ) ||
+	     ( ( port = strchr( host+1, ',' ) ) == 0 ) ) {
+		imcb_log( ic, "Error parsing proxy setting: \"%s\" (ignored)", proxy );
+		return NULL;
+	}
+	
+	jid = proxy;
+	*host++ = '\0';
+	*port++ = '\0';
+
+	sh = g_new0( jabber_streamhost_t, 1 );
+	sh->jid = g_strdup( jid );
+	sh->host = g_strdup( host );
+	strcpy( sh->port, port );
+
+	return sh;
+}
+
+void jabber_si_set_proxies( struct bs_transfer *bt )
+{
+	struct jabber_transfer *tf = bt->tf;
+	struct jabber_data *jd = tf->ic->proto_data;
+	char *proxysetting = g_strdup ( set_getstr( &tf->ic->acc->set, "proxy" ) );
+	char *proxy, *next, *errmsg = NULL;
+	char port[6];
+	char host[HOST_NAME_MAX+1];
+	jabber_streamhost_t *sh, *sh2;
+	GSList *streamhosts = jd->streamhosts;
+
+	proxy = proxysetting;
+	while ( proxy && ( *proxy!='\0' ) ) {
+		if( ( next = strchr( proxy, ';' ) ) )
+			*next++ = '\0';	
+		
+		if( strcmp( proxy, "<local>" ) == 0 ) {
+			if( ( tf->fd = ft_listen( &tf->saddr, host, port, FALSE, &errmsg ) ) != -1 ) {
+				sh = g_new0( jabber_streamhost_t, 1 );
+				sh->jid = g_strdup( tf->ini_jid );
+				sh->host = g_strdup( host );
+				strcpy( sh->port, port );
+				bt->streamhosts = g_slist_append( bt->streamhosts, sh );
+
+				bt->tf->watch_in = b_input_add( tf->fd, GAIM_INPUT_READ, jabber_bs_send_handshake, bt );
+				bt->connect_timeout = b_timeout_add( JABBER_BS_LISTEN_TIMEOUT * 1000, jabber_bs_connect_timeout, bt );
+			} else {
+				imcb_log( tf->ic, "Transferring file %s: couldn't listen locally(non fatal, check your ft_listen setting in bitlbee.conf): %s",
+					  tf->ft->file_name,
+					  errmsg );
+			}
+		} else if( strcmp( proxy, "<auto>" ) == 0 ) {
+			while ( streamhosts ) {
+				sh = g_new0( jabber_streamhost_t, 1 );
+				sh2 = streamhosts->data;
+				sh->jid = g_strdup( sh2->jid );
+				sh->host = g_strdup( sh2->host );
+				strcpy( sh->port, sh2->port );
+				bt->streamhosts = g_slist_append( bt->streamhosts, sh );
+				streamhosts = g_slist_next( streamhosts );
+			}
+		} else if( ( sh = jabber_si_parse_proxy( tf->ic, proxy ) ) )
+			bt->streamhosts = g_slist_append( bt->streamhosts, sh );
+		proxy = next;
+	}
+}
+
+/*
+ * Starts a bytestream.
+ */
+gboolean jabber_bs_send_start( struct jabber_transfer *tf )
+{
+	struct bs_transfer *bt;
+	sha1_state_t sha;
+	char hash_hex[41];
+	unsigned char hash[20];
+	int i,ret;
+
+	/* SHA1( SID + Initiator JID + Target JID ) is given to the streamhost which it will match against the initiator's value */
+	sha1_init( &sha );
+	sha1_append( &sha, (unsigned char*) tf->sid, strlen( tf->sid ) );
+	sha1_append( &sha, (unsigned char*) tf->ini_jid, strlen( tf->ini_jid ) );
+	sha1_append( &sha, (unsigned char*) tf->tgt_jid, strlen( tf->tgt_jid ) );
+	sha1_finish( &sha, hash );
+	
+	for( i = 0; i < 20; i ++ )
+		sprintf( hash_hex + i * 2, "%02x", hash[i] );
+		
+	bt = g_new0( struct bs_transfer, 1 );
+	bt->tf = tf;
+	bt->phase = BS_PHASE_CONNECT;
+	bt->pseudoadr = g_strdup( hash_hex );
+	tf->streamhandle = bt;
+	tf->ft->free = jabber_bs_free_transfer;
+	tf->ft->canceled = jabber_bs_canceled;
+
+	jabber_si_set_proxies( bt );
+
+	ret = jabber_bs_send_request( tf, bt->streamhosts);
+
+	return ret;
+}
+
+gboolean jabber_bs_send_request( struct jabber_transfer *tf, GSList *streamhosts )
+{
+	struct xt_node *shnode, *query, *iq;
+
+	query = xt_new_node( "query", NULL, NULL );
+	xt_add_attr( query, "xmlns", XMLNS_BYTESTREAMS );
+	xt_add_attr( query, "sid", tf->sid );
+	xt_add_attr( query, "mode", "tcp" );
+
+	while( streamhosts ) {
+		jabber_streamhost_t *sh = streamhosts->data;
+		shnode = xt_new_node( "streamhost", NULL, NULL );
+		xt_add_attr( shnode, "jid", sh->jid );
+		xt_add_attr( shnode, "host", sh->host );
+		xt_add_attr( shnode, "port", sh->port );
+
+		xt_add_child( query, shnode );
+
+		streamhosts = g_slist_next( streamhosts );
+	}
+
+
+	iq = jabber_make_packet( "iq", "set", tf->tgt_jid, query );
+	xt_add_attr( iq, "from", tf->ini_jid );
+
+	jabber_cache_add( tf->ic, iq, jabber_bs_send_handle_reply );
+
+	if( !jabber_write_packet( tf->ic, iq ) )
+		imcb_file_canceled( tf->ft, "Error transmitting bytestream request" );
+	return TRUE;
+}
+
+gboolean jabber_bs_send_handshake_abort(struct bs_transfer *bt, char *error )
+{
+	struct jabber_transfer *tf = bt->tf;
+	struct jabber_data *jd = tf->ic->proto_data;
+
+	/* TODO: did the receiver get here somehow??? */
+	imcb_log( tf->ic, "Transferring file %s: SOCKS5 handshake failed: %s", 
+		  tf->ft->file_name, 
+		  error );
+
+	if( jd->streamhosts==NULL ) /* we're done here unless we have a proxy to try */
+		imcb_file_canceled( tf->ft, error );
+
+	/* MUST always return FALSE! */
+	return FALSE;
+}
+
+/*
+ * SOCKS5BYTESTREAM protocol for the sender
+ */
+gboolean jabber_bs_send_handshake( gpointer data, gint fd, b_input_condition cond )
+{
+	struct bs_transfer *bt = data;
+	struct jabber_transfer *tf = bt->tf;
+	short revents;
+
+	if ( !jabber_bs_poll( bt, fd, &revents ) )
+		return FALSE;
+	
+	switch( bt->phase ) 
+	{
+	case BS_PHASE_CONNECT:
+		{
+			struct sockaddr_storage clt_addr;
+			socklen_t ssize = sizeof( clt_addr );
+			
+			/* Connect */
+
+			ASSERTSOCKOP( tf->fd = accept( fd, (struct sockaddr *) &clt_addr, &ssize ), "Accepting connection" );
+
+			closesocket( fd );
+			fd = tf->fd;
+			sock_make_nonblocking( fd );
+			
+			bt->phase = BS_PHASE_CONNECTED;
+
+			bt->tf->watch_in = b_input_add( fd, GAIM_INPUT_READ, jabber_bs_send_handshake, bt );
+			return FALSE;
+		}
+	case BS_PHASE_CONNECTED:
+		{
+			int ret, have_noauth=FALSE;
+			struct {
+				unsigned char ver;
+				unsigned char method;
+			} socks5_auth_reply = { .ver = 5, .method = 0 };
+			struct {
+				unsigned char ver;
+				unsigned char nmethods;
+				unsigned char method;
+			} socks5_hello;
+
+			if( !( ret = jabber_bs_peek( bt, &socks5_hello, sizeof( socks5_hello ) ) ) )
+				return FALSE;
+
+			if( ret < sizeof( socks5_hello ) )
+				return TRUE;
+
+			if( !( socks5_hello.ver == 5 ) ||
+			    !( socks5_hello.nmethods >= 1 ) ||
+			    !( socks5_hello.nmethods < 32 ) )
+				return jabber_bs_abort( bt, "Invalid auth request ver=%d nmethods=%d method=%d", socks5_hello.ver, socks5_hello.nmethods, socks5_hello.method );
+
+			have_noauth = socks5_hello.method == 0;
+
+			if( socks5_hello.nmethods > 1 )
+			{
+				char mbuf[32];
+				int i;
+				ASSERTSOCKOP( ret = recv( fd, mbuf, socks5_hello.nmethods - 1, 0 ) , "Receiving auth methods" );
+				if( ret < ( socks5_hello.nmethods - 1 ) )
+					return jabber_bs_abort( bt, "Partial auth request");
+				for( i = 0 ; !have_noauth && ( i < socks5_hello.nmethods - 1 ) ; i ++ )
+					if( mbuf[i] == 0 )
+						have_noauth = TRUE;
+			}
+			
+			if( !have_noauth )
+				return jabber_bs_abort( bt, "Auth request didn't include no authentication" );
+
+			ASSERTSOCKOP( send( fd, &socks5_auth_reply, sizeof( socks5_auth_reply ) , 0 ), "Sending auth reply" );
+
+			bt->phase = BS_PHASE_REQUEST;
+
+			return TRUE;
+		}
+	case BS_PHASE_REQUEST:
+		{
+			struct socks5_message socks5_connect;
+			int msgsize = sizeof( struct socks5_message );
+			int ret;
+
+			if( !( ret = jabber_bs_peek( bt, &socks5_connect, msgsize ) ) )
+				return FALSE;
+
+			if( ret < msgsize )
+				return TRUE;
+
+			if( !( socks5_connect.ver == 5) ||
+			    !( socks5_connect.cmdrep.cmd == 1 ) ||
+			    !( socks5_connect.atyp == 3 ) ||
+			    !(socks5_connect.addrlen == 40 ) )
+				return jabber_bs_abort( bt, "Invalid SOCKS5 Connect message (addrlen=%d, ver=%d, cmd=%d, atyp=%d)", socks5_connect.addrlen, socks5_connect.ver, socks5_connect.cmdrep.cmd, socks5_connect.atyp );
+			if( !( memcmp( socks5_connect.address, bt->pseudoadr, 40 ) == 0 ) )
+				return jabber_bs_abort( bt, "SOCKS5 Connect message contained wrong digest");
+
+			socks5_connect.cmdrep.rep = 0;
+
+			ASSERTSOCKOP( send( fd, &socks5_connect, msgsize, 0 ), "Sending connect reply" );
+
+			bt->phase = BS_PHASE_REPLY;
+
+			imcb_log( tf->ic, "File %s: SOCKS5 handshake successful! Transfer about to start...", tf->ft->file_name );
+
+			if( tf->accepted )
+			{
+				/* streamhost-used message came already in(possible?), let's start sending */
+				tf->ft->write_request( tf->ft );
+			}
+
+			tf->watch_in = 0;
+			return FALSE;
+
+		}
+	default:
+		/* BUG */
+		imcb_log( bt->tf->ic, "BUG in file transfer code: undefined handshake phase" );
+
+		bt->tf->watch_in = 0;
+		return FALSE;
+	}
+}
+#undef ASSERTSOCKOP
Index: protocols/jabber/si.c
===================================================================
--- protocols/jabber/si.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
+++ protocols/jabber/si.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -0,0 +1,529 @@
+/***************************************************************************\
+*                                                                           *
+*  BitlBee - An IRC to IM gateway                                           *
+*  Jabber module - SI packets                                               *
+*                                                                           *
+*  Copyright 2007 Uli Meis <a.sporto+bee@gmail.com>                         *
+*                                                                           *
+*  This program is free software; you can redistribute it and/or modify     *
+*  it under the terms of the GNU General Public License as published by     *
+*  the Free Software Foundation; either version 2 of the License, or        *
+*  (at your option) any later version.                                      *
+*                                                                           *
+*  This program is distributed in the hope that it will be useful,          *
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
+*  GNU General Public License for more details.                             *
+*                                                                           *
+*  You should have received a copy of the GNU General Public License along  *
+*  with this program; if not, write to the Free Software Foundation, Inc.,  *
+*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              *
+*                                                                           *
+\***************************************************************************/
+
+#include "jabber.h"
+#include "sha1.h"
+
+void jabber_si_answer_request( file_transfer_t *ft );
+int jabber_si_send_request(struct im_connection *ic, char *who, struct jabber_transfer *tf );
+
+/* file_transfer free() callback */
+void jabber_si_free_transfer( file_transfer_t *ft)
+{
+	struct jabber_transfer *tf = ft->data;
+	struct jabber_data *jd = tf->ic->proto_data;
+
+	if ( tf->watch_in )
+		b_event_remove( tf->watch_in );
+
+	jd->filetransfers = g_slist_remove( jd->filetransfers, tf );
+
+	if( tf->fd != -1 )
+	{
+		closesocket( tf->fd );
+		tf->fd = -1;
+	}
+
+	if( tf->disco_timeout )
+		b_event_remove( tf->disco_timeout );
+	
+	g_free( tf->ini_jid );
+	g_free( tf->tgt_jid );
+	g_free( tf->iq_id );
+	g_free( tf->sid );
+	g_free( tf );
+}
+
+/* file_transfer canceled() callback */
+void jabber_si_canceled( file_transfer_t *ft, char *reason )
+{
+	struct jabber_transfer *tf = ft->data;
+	struct xt_node *reply, *iqnode;
+
+	if( tf->accepted )
+		return;
+	
+	iqnode = jabber_make_packet( "iq", "error", tf->ini_jid, NULL );
+	xt_add_attr( iqnode, "id", tf->iq_id );
+	reply = jabber_make_error_packet( iqnode, "forbidden", "cancel", "403" );
+	xt_free_node( iqnode );
+	
+	if( !jabber_write_packet( tf->ic, reply ) )
+		imcb_log( tf->ic, "WARNING: Error generating reply to file transfer request" );
+	xt_free_node( reply );
+
+}
+
+int jabber_si_check_features( struct jabber_transfer *tf, GSList *features ) {
+	int foundft = FALSE, foundbt = FALSE, foundsi = FALSE;
+
+	while ( features )
+	{
+		if( !strcmp( features->data, XMLNS_FILETRANSFER ) )
+			foundft = TRUE;
+		if( !strcmp( features->data, XMLNS_BYTESTREAMS ) )
+			foundbt = TRUE;
+		if( !strcmp( features->data, XMLNS_SI ) )
+			foundsi = TRUE;
+
+		features = g_slist_next(features);
+	}
+
+	if( !foundft )
+		imcb_file_canceled( tf->ft, "Buddy's client doesn't feature file transfers" );
+	else if( !foundbt )
+		imcb_file_canceled( tf->ft, "Buddy's client doesn't feature byte streams (required)" );
+	else if( !foundsi )
+		imcb_file_canceled( tf->ft, "Buddy's client doesn't feature stream initiation (required)" );
+		
+	return foundft && foundbt && foundsi;
+}
+
+void jabber_si_transfer_start( struct jabber_transfer *tf ) {
+
+	if( !jabber_si_check_features( tf, tf->bud->features ) )
+		return;
+		
+	/* send the request to our buddy */
+	jabber_si_send_request( tf->ic, tf->bud->full_jid, tf );
+
+	/* and start the receive logic */
+	imcb_file_recv_start( tf->ft );
+
+}
+
+gboolean jabber_si_waitfor_disco( gpointer data, gint fd, b_input_condition cond )
+{
+	struct jabber_transfer *tf = data;
+	struct jabber_data *jd = tf->ic->proto_data;
+
+	tf->disco_timeout_fired++;
+
+	if( tf->bud->features && jd->have_streamhosts==1 ) {
+		tf->disco_timeout = 0;
+		jabber_si_transfer_start( tf );
+		return FALSE;
+	}
+
+	/* 8 seconds should be enough for server and buddy to respond */
+	if ( tf->disco_timeout_fired < 16 )
+		return TRUE;
+	
+	if( !tf->bud->features && jd->have_streamhosts!=1 )
+		imcb_log( tf->ic, "Couldn't get buddy's features nor discover all services of the server" );
+	else if( !tf->bud->features )
+		imcb_log( tf->ic, "Couldn't get buddy's features" );
+	else
+		imcb_log( tf->ic, "Couldn't discover some of the server's services" );
+	
+	tf->disco_timeout = 0;
+	jabber_si_transfer_start( tf );
+	return FALSE;
+}
+
+void jabber_si_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *who ) 
+{
+	struct jabber_transfer *tf;
+	struct jabber_data *jd = ic->proto_data;
+	struct jabber_buddy *bud;
+	char *server = jd->server, *s;
+
+	if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) )
+		bud = jabber_buddy_by_ext_jid( ic, who, 0 );
+	else
+		bud = jabber_buddy_by_jid( ic, who, 0 );
+
+	if( bud == NULL )
+	{
+		imcb_file_canceled( ft, "Couldn't find buddy (BUG?)" );
+		return;
+	}
+	
+	imcb_log( ic, "Trying to send %s(%zd bytes) to %s", ft->file_name, ft->file_size, who );
+
+	tf = g_new0( struct jabber_transfer, 1 );
+
+	tf->ic = ic;
+	tf->ft = ft;
+	tf->fd = -1;
+	tf->ft->data = tf;
+	tf->ft->free = jabber_si_free_transfer;
+	tf->bud = bud;
+	ft->write = jabber_bs_send_write;
+
+	jd->filetransfers = g_slist_prepend( jd->filetransfers, tf );
+
+	/* query buddy's features and server's streaming proxies if neccessary */
+
+	if( !tf->bud->features )
+		jabber_iq_query_features( ic, bud->full_jid );
+
+	/* If <auto> is not set don't check for proxies */
+	if( ( jd->have_streamhosts!=1 ) && ( jd->streamhosts==NULL ) &&
+	    ( strstr( set_getstr( &ic->acc->set, "proxy" ), "<auto>" ) != NULL ) ) {
+		jd->have_streamhosts = 0;
+		jabber_iq_query_server( ic, server, XMLNS_DISCO_ITEMS );
+	} else if ( jd->streamhosts!=NULL )
+		jd->have_streamhosts = 1;
+
+	/* if we had to do a query, wait for the result. 
+	 * Otherwise fire away. */
+	if( !tf->bud->features || jd->have_streamhosts!=1 )
+		tf->disco_timeout = b_timeout_add( 500, jabber_si_waitfor_disco, tf );
+	else
+		jabber_si_transfer_start( tf );
+}
+
+/*
+ * First function that gets called when a file transfer request comes in.
+ * A lot to parse.
+ *
+ * We choose a stream type from the options given by the initiator.
+ * Then we wait for imcb to call the accept or cancel callbacks.
+ */
+int jabber_si_handle_request( struct im_connection *ic, struct xt_node *node, struct xt_node *sinode)
+{
+	struct xt_node *c, *d, *reply;
+	char *sid, *ini_jid, *tgt_jid, *iq_id, *s, *ext_jid, *size_s;
+	struct jabber_buddy *bud;
+	int requestok = FALSE;
+	char *name, *cmp;
+	size_t size;
+	struct jabber_transfer *tf;
+	struct jabber_data *jd = ic->proto_data;
+	file_transfer_t *ft;
+	
+	/* All this means we expect something like this: ( I think )
+	 * <iq from=... to=... id=...>
+	 * 	<si id=id xmlns=si profile=ft>
+	 * 		<file xmlns=ft/>
+	 * 		<feature xmlns=feature>
+	 * 			<x xmlns=xdata type=submit>
+	 * 				<field var=stream-method>
+	 *
+	 */
+	if( !( ini_jid 		= xt_find_attr(   node, "from" ) 			) ||
+	    !( tgt_jid 		= xt_find_attr(   node, "to" ) 				) ||
+	    !( iq_id 		= xt_find_attr(   node, "id" ) 				) ||
+	    !( sid 		= xt_find_attr( sinode, "id" ) 				) ||
+	    !( cmp              = xt_find_attr( sinode, "profile" )                     ) ||
+	    !( 0               == strcmp( cmp, XMLNS_FILETRANSFER )			) ||
+	    !( d 		= xt_find_node( sinode->children, "file" ) 		) ||
+	    !( cmp = xt_find_attr( d, "xmlns" )						) ||
+	    !( 0               == strcmp( cmp, XMLNS_FILETRANSFER )			) ||
+	    !( name 		= xt_find_attr( d, "name" ) 				) ||
+	    !( size_s           = xt_find_attr( d, "size" )                             ) ||
+	    !( 1               == sscanf( size_s, "%zd", &size )                        ) ||
+	    !( d 		= xt_find_node( sinode->children, "feature" ) 		) ||
+	    !( cmp              = xt_find_attr( d, "xmlns" )				) ||
+	    !( 0               == strcmp( cmp, XMLNS_FEATURE )				) ||
+	    !( d 		= xt_find_node( d->children, "x" ) 			) ||
+	    !( cmp              = xt_find_attr( d, "xmlns" )				) ||
+	    !( 0               == strcmp( cmp, XMLNS_XDATA )				) ||
+	    !( cmp              = xt_find_attr( d, "type" )				) ||
+	    !( 0               == strcmp( cmp, "form" )					) ||
+	    !( d 		= xt_find_node( d->children, "field" ) 			) ||
+	    !( cmp              = xt_find_attr( d, "var" )				) ||
+	    !( 0               == strcmp( cmp, "stream-method" )			) )
+	{
+		imcb_log( ic, "WARNING: Received incomplete Stream Initiation request" );
+	}
+	else
+	{
+		/* Check if we support one of the options */
+
+		c = d->children;
+		while( ( c = xt_find_node( c, "option" ) ) )
+			if( ( d = xt_find_node( c->children, "value" ) ) &&
+			    ( d->text != NULL ) &&
+			    ( strcmp( d->text, XMLNS_BYTESTREAMS ) == 0 ) )
+			{
+				requestok = TRUE;
+				break;
+			}
+
+		if ( !requestok )
+			imcb_log( ic, "WARNING: Unsupported file transfer request from %s", ini_jid);
+	}
+	
+	if( requestok )
+	{
+		/* Figure out who the transfer should come frome... */
+
+		ext_jid = ini_jid;
+		if( ( s = strchr( ini_jid, '/' ) ) )
+		{
+			if( ( bud = jabber_buddy_by_jid( ic, ini_jid, GET_BUDDY_EXACT ) ) )
+			{
+				bud->last_act = time( NULL );
+				ext_jid = bud->ext_jid ? : bud->bare_jid;
+			}
+			else
+				*s = 0; /* We need to generate a bare JID now. */
+		}
+
+		if( !( ft = imcb_file_send_start( ic, ext_jid, name, size ) ) )
+		{ 
+			imcb_log( ic, "WARNING: Error handling transfer request from %s", ini_jid);
+			requestok = FALSE;
+		}
+
+		*s = '/';
+	}
+	else
+	{ 
+		reply = jabber_make_error_packet( node, "item-not-found", "cancel", NULL );
+		if (!jabber_write_packet( ic, reply ))
+			imcb_log( ic, "WARNING: Error generating reply to file transfer request" );
+		xt_free_node( reply );
+		return XT_HANDLED;
+	}
+
+	/* Request is fine. */
+
+	tf = g_new0( struct jabber_transfer, 1 );
+
+	tf->ini_jid = g_strdup( ini_jid );
+	tf->tgt_jid = g_strdup( tgt_jid );
+	tf->iq_id = g_strdup( iq_id );
+	tf->sid = g_strdup( sid );
+	tf->ic = ic;
+	tf->ft = ft;
+	tf->fd = -1;
+	tf->ft->data = tf;
+	tf->ft->accept = jabber_si_answer_request;
+	tf->ft->free = jabber_si_free_transfer;
+	tf->ft->canceled = jabber_si_canceled;
+
+	jd->filetransfers = g_slist_prepend( jd->filetransfers, tf );
+
+	return XT_HANDLED;
+}
+
+/*
+ * imc called the accept callback which probably means that the user accepted this file transfer.
+ * We send our response to the initiator.
+ * In the next step, the initiator will send us a request for the given stream type.
+ * (currently that can only be a SOCKS5 bytestream)
+ */
+void jabber_si_answer_request( file_transfer_t *ft ) {
+	struct jabber_transfer *tf = ft->data;
+	struct xt_node *node, *sinode, *reply;
+
+	/* generate response, start with the SI tag */
+	sinode = xt_new_node( "si", NULL, NULL );
+	xt_add_attr( sinode, "xmlns", XMLNS_SI );
+	xt_add_attr( sinode, "profile", XMLNS_FILETRANSFER );
+	xt_add_attr( sinode, "id", tf->sid );
+
+	/* now the file tag */
+	node = xt_new_node( "file", NULL, NULL );
+	xt_add_attr( node, "xmlns", XMLNS_FILETRANSFER );
+
+	xt_add_child( sinode, node );
+
+	/* and finally the feature tag */
+	node = xt_new_node( "field", NULL, NULL );
+	xt_add_attr( node, "var", "stream-method" );
+	xt_add_attr( node, "type", "list-single" );
+
+	/* Currently all we can do. One could also implement in-band (IBB) */
+	xt_add_child( node, xt_new_node( "value", XMLNS_BYTESTREAMS, NULL ) );
+
+	node = xt_new_node( "x", NULL, node );
+	xt_add_attr( node, "xmlns", XMLNS_XDATA );
+	xt_add_attr( node, "type", "submit" );
+
+	node = xt_new_node( "feature", NULL, node );
+	xt_add_attr( node, "xmlns", XMLNS_FEATURE );
+
+	xt_add_child( sinode, node );
+
+	reply = jabber_make_packet( "iq", "result", tf->ini_jid, sinode );
+	xt_add_attr( reply, "id", tf->iq_id );
+	
+	if( !jabber_write_packet( tf->ic, reply ) )
+		imcb_log( tf->ic, "WARNING: Error generating reply to file transfer request" );
+	else
+		tf->accepted = TRUE;
+	xt_free_node( reply );
+}
+
+static xt_status jabber_si_handle_response(struct im_connection *ic, struct xt_node *node, struct xt_node *orig )
+{
+	struct xt_node *c, *d;
+	char *ini_jid, *tgt_jid, *iq_id, *cmp;
+	GSList *tflist;
+	struct jabber_transfer *tf=NULL;
+	struct jabber_data *jd = ic->proto_data;
+
+	if( !( tgt_jid = xt_find_attr( node, "from" ) ) ||
+	    !( ini_jid = xt_find_attr( node, "to" ) ) )
+	{
+		imcb_log( ic, "Invalid SI response from=%s to=%s", tgt_jid, ini_jid );
+		return XT_HANDLED;
+	}
+	
+	/* All this means we expect something like this: ( I think )
+	 * <iq from=... to=... id=...>
+	 * 	<si xmlns=si>
+	 * 	[	<file xmlns=ft/>    ] <-- not neccessary
+	 * 		<feature xmlns=feature>
+	 * 			<x xmlns=xdata type=submit>
+	 * 				<field var=stream-method>
+	 * 					<value>
+	 */
+	if( !( tgt_jid = xt_find_attr( node, "from" ) ) ||
+	    !( ini_jid = xt_find_attr( node, "to" ) ) ||
+	    !( iq_id   = xt_find_attr( node, "id" ) ) ||
+	    !( c = xt_find_node( node->children, "si" ) ) ||
+	    !( cmp = xt_find_attr( c, "xmlns" ) ) ||
+	    !( strcmp( cmp, XMLNS_SI ) == 0 ) ||
+	    !( d = xt_find_node( c->children, "feature" ) ) ||
+	    !( cmp = xt_find_attr( d, "xmlns" ) ) ||
+	    !( strcmp( cmp, XMLNS_FEATURE ) == 0 ) ||
+	    !( d = xt_find_node( d->children, "x" ) ) ||
+	    !( cmp = xt_find_attr( d, "xmlns" ) ) ||
+	    !( strcmp( cmp, XMLNS_XDATA ) == 0 ) ||
+	    !( cmp = xt_find_attr( d, "type" ) ) ||
+	    !( strcmp( cmp, "submit" ) == 0 ) ||
+	    !( d = xt_find_node( d->children, "field" ) ) ||
+	    !( cmp = xt_find_attr( d, "var" ) ) ||
+	    !( strcmp( cmp, "stream-method" ) == 0 ) ||
+	    !( d = xt_find_node( d->children, "value" ) ) )
+	{
+		imcb_log( ic, "WARNING: Received incomplete Stream Initiation response" );
+		return XT_HANDLED;
+	}
+
+	if( !( strcmp( d->text, XMLNS_BYTESTREAMS ) == 0 ) ) { 
+		/* since we should only have advertised what we can do and the peer should
+		 * only have chosen what we offered, this should never happen */
+		imcb_log( ic, "WARNING: Received invalid Stream Initiation response, method %s", d->text );
+			
+		return XT_HANDLED;
+	}
+	
+	/* Let's see if we can find out what this bytestream should be for... */
+
+	for( tflist = jd->filetransfers ; tflist; tflist = g_slist_next(tflist) )
+	{
+		struct jabber_transfer *tft = tflist->data;
+		if( ( strcmp( tft->iq_id, iq_id ) == 0 ) )
+		{
+		    	tf = tft;
+			break;
+		}
+	}
+
+	if (!tf) 
+	{
+		imcb_log( ic, "WARNING: Received bytestream request from %s that doesn't match an SI request", ini_jid );
+		return XT_HANDLED;
+	}
+
+	tf->ini_jid = g_strdup( ini_jid );
+	tf->tgt_jid = g_strdup( tgt_jid );
+
+	imcb_log( ic, "File %s: %s accepted the transfer!", tf->ft->file_name, tgt_jid );
+
+	jabber_bs_send_start( tf );
+
+	return XT_HANDLED;
+}
+
+int jabber_si_send_request(struct im_connection *ic, char *who, struct jabber_transfer *tf )
+{
+	struct xt_node *node, *sinode;
+	struct jabber_buddy *bud;
+
+	/* who knows how many bits the future holds :) */
+	char filesizestr[ 1 + ( int ) ( 0.301029995663981198f * sizeof( size_t ) * 8 ) ];
+
+	const char *methods[] = 
+	{  	
+		XMLNS_BYTESTREAMS,
+		//XMLNS_IBB,
+		NULL 
+	};
+	const char **m;
+	char *s;
+
+	/* Maybe we should hash this? */
+	tf->sid = g_strdup_printf( "BitlBeeJabberSID%d", tf->ft->local_id );
+	
+	if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) )
+		bud = jabber_buddy_by_ext_jid( ic, who, 0 );
+	else
+		bud = jabber_buddy_by_jid( ic, who, 0 );
+
+	/* start with the SI tag */
+	sinode = xt_new_node( "si", NULL, NULL );
+	xt_add_attr( sinode, "xmlns", XMLNS_SI );
+	xt_add_attr( sinode, "profile", XMLNS_FILETRANSFER );
+	xt_add_attr( sinode, "id", tf->sid );
+
+/*	if( mimetype ) 
+		xt_add_attr( node, "mime-type", mimetype ); */
+
+	/* now the file tag */
+/*	if( desc )
+ 		node = xt_new_node( "desc", descr, NULL ); */
+	node = xt_new_node( "range", NULL, NULL );
+
+	sprintf( filesizestr, "%zd", tf->ft->file_size );
+	node = xt_new_node( "file", NULL, node );
+	xt_add_attr( node, "xmlns", XMLNS_FILETRANSFER );
+	xt_add_attr( node, "name", tf->ft->file_name );
+	xt_add_attr( node, "size", filesizestr );
+/*	if (hash)
+		xt_add_attr( node, "hash", hash );
+	if (date)
+		xt_add_attr( node, "date", date ); */
+
+	xt_add_child( sinode, node );
+
+	/* and finally the feature tag */
+	node = xt_new_node( "field", NULL, NULL );
+	xt_add_attr( node, "var", "stream-method" );
+	xt_add_attr( node, "type", "list-single" );
+
+	for ( m = methods ; *m ; m ++ )
+		xt_add_child( node, xt_new_node( "option", NULL, xt_new_node( "value", (char *)*m, NULL ) ) );
+
+	node = xt_new_node( "x", NULL, node );
+	xt_add_attr( node, "xmlns", XMLNS_XDATA );
+	xt_add_attr( node, "type", "form" );
+
+	node = xt_new_node( "feature", NULL, node );
+	xt_add_attr( node, "xmlns", XMLNS_FEATURE );
+
+	xt_add_child( sinode, node );
+
+	/* and we are there... */
+	node = jabber_make_packet( "iq", "set", bud ? bud->full_jid : who, sinode );
+	jabber_cache_add( ic, node, jabber_si_handle_response );
+	tf->iq_id = g_strdup( xt_find_attr( node, "id" ) );
+	
+	return jabber_write_packet( ic, node );
+}
Index: protocols/msn/Makefile
===================================================================
--- protocols/msn/Makefile	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ protocols/msn/Makefile	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -10,5 +10,5 @@
 
 # [SH] Program variables
-objects = msn.o msn_util.o ns.o passport.o sb.o tables.o
+objects = invitation.o msn.o msn_util.o ns.o passport.o sb.o tables.o
 
 CFLAGS += -Wall
Index: protocols/msn/invitation.c
===================================================================
--- protocols/msn/invitation.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
+++ protocols/msn/invitation.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -0,0 +1,622 @@
+/********************************************************************\
+* BitlBee -- An IRC to other IM-networks gateway                     *
+*                                                                    *
+* Copyright 2008 Uli Meis					     *
+* Copyright 2006 Marijn Kruisselbrink and others                     *
+\********************************************************************/
+
+/* MSN module - File transfer support             */
+
+/*
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+ 
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+ 
+ You should have received a copy of the GNU General Public License with
+ the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
+ if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+ Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "bitlbee.h"
+#include "invitation.h"
+#include "msn.h"
+#include "lib/ftutil.h"
+
+#ifdef debug
+#undef debug
+#endif
+#define debug(msg...) log_message( LOGLVL_INFO, msg )
+
+static void msn_ftp_free( file_transfer_t *file );
+static void msn_ftpr_accept( file_transfer_t *file );
+static void msn_ftp_finished( file_transfer_t *file );
+static void msn_ftp_canceled( file_transfer_t *file, char *reason );
+static gboolean msn_ftpr_write_request( file_transfer_t *file );
+
+static gboolean msn_ftp_connected( gpointer data, gint fd, b_input_condition cond );
+static gboolean msn_ftp_read( gpointer data, gint fd, b_input_condition cond );
+gboolean msn_ftps_write( file_transfer_t *file, char *buffer, unsigned int len );
+
+/*
+ * Vararg wrapper for imcb_file_canceled().
+ */
+gboolean msn_ftp_abort( file_transfer_t *file, char *format, ... )
+{
+        va_list params;
+        va_start( params, format );
+        char error[128];
+
+        if( vsnprintf( error, 128, format, params ) < 0 )
+                sprintf( error, "internal error parsing error string (BUG)" );
+        va_end( params );
+	imcb_file_canceled( file, error );
+	return FALSE;
+}
+
+/* very useful */
+#define ASSERTSOCKOP(op, msg) \
+	if( (op) == -1 ) \
+		return msn_ftp_abort( file , msg ": %s", strerror( errno ) );
+
+void msn_ftp_invitation_cmd( struct im_connection *ic, char *who, int cookie, char *icmd,
+			     char *trailer )
+{
+	struct msn_message *m = g_new0( struct msn_message, 1 );
+	
+	m->text = g_strdup_printf( "%s"
+		    "Invitation-Command: %s\r\n"
+		    "Invitation-Cookie: %u\r\n"
+		    "%s",
+		    MSN_INVITE_HEADERS,
+		    icmd,
+		    cookie,
+		    trailer);
+	
+	m->who = g_strdup( who );
+
+	msn_sb_write_msg( ic, m );
+}
+
+void msn_ftp_cancel_invite( struct im_connection *ic, char *who,  int cookie, char *code )
+{
+	char buf[64];
+
+	g_snprintf( buf, sizeof( buf ), "Cancel-Code: %s\r\n", code );
+	msn_ftp_invitation_cmd( ic, who, cookie, "CANCEL", buf );
+}
+
+void msn_ftp_transfer_request( struct im_connection *ic, file_transfer_t *file, char *who )
+{
+	unsigned int cookie = time( NULL ); /* TODO: randomize */
+	char buf[2048];
+
+	msn_filetransfer_t *msn_file = g_new0( msn_filetransfer_t, 1 );
+	file->data = msn_file;
+	file->free = msn_ftp_free;
+	file->canceled = msn_ftp_canceled;
+	file->write = msn_ftps_write;
+	msn_file->md = ic->proto_data;
+	msn_file->invite_cookie = cookie;
+	msn_file->handle = g_strdup( who );
+	msn_file->dcc = file;
+	msn_file->md->filetransfers = g_slist_prepend( msn_file->md->filetransfers, msn_file->dcc );
+	msn_file->fd = -1;
+	msn_file->sbufpos = 3;
+
+	g_snprintf( buf, sizeof( buf ), 
+		"Application-Name: File Transfer\r\n"
+		"Application-GUID: {5D3E02AB-6190-11d3-BBBB-00C04F795683}\r\n"
+		"Application-File: %s\r\n"
+		"Application-FileSize: %zd\r\n",
+		file->file_name,
+		file->file_size);
+
+	msn_ftp_invitation_cmd( msn_file->md->ic, msn_file->handle, cookie, "INVITE", buf );
+
+	imcb_file_recv_start( file );
+}
+
+void msn_invitation_invite( struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen )
+{
+	char *itype = msn_findheader( body, "Application-GUID:", blen );
+	char *name, *size, *invitecookie, *reject = NULL;
+	user_t *u;
+	size_t isize;
+	file_transfer_t *file;
+	
+	if( !itype || strcmp( itype, "{5D3E02AB-6190-11d3-BBBB-00C04F795683}" ) != 0 ) {
+		/* Don't know what that is - don't care */
+		char *iname = msn_findheader( body, "Application-Name:", blen );
+		imcb_log( sb->ic, "Received unknown MSN invitation %s (%s) from %s",
+			  itype ? : "with no GUID", iname ? iname : "no application name", handle );
+		g_free( iname );
+		reject = "REJECT_NOT_INSTALLED";
+	} else if ( 
+		!( name = msn_findheader( body, "Application-File:", blen )) || 
+		!( size = msn_findheader( body, "Application-FileSize:", blen )) || 
+		!( invitecookie = msn_findheader( body, "Invitation-Cookie:", blen)) ||
+		!( isize = atoll( size ) ) ) { 
+		imcb_log( sb->ic, "Received corrupted transfer request from %s"
+			  "(name=%s, size=%s, invitecookie=%s)",
+			  handle, name, size, invitecookie );
+		reject = "REJECT";
+	} else if ( !( u = user_findhandle( sb->ic, handle ) ) ) {
+		imcb_log( sb->ic, "Error in parsing transfer request, User '%s'"
+			  "is not in contact list", handle );
+		reject = "REJECT";
+	} else if ( !( file = imcb_file_send_start( sb->ic, handle, name, isize ) ) ) {
+		imcb_log( sb->ic, "Error initiating transfer for request from %s for %s",
+			  handle, name );
+		reject = "REJECT";
+	} else {
+		msn_filetransfer_t *msn_file = g_new0( msn_filetransfer_t, 1 );
+		file->data = msn_file;
+		file->accept = msn_ftpr_accept;
+		file->free = msn_ftp_free;
+		file->finished = msn_ftp_finished;
+		file->canceled = msn_ftp_canceled;
+		file->write_request = msn_ftpr_write_request;
+		msn_file->md = sb->ic->proto_data;
+		msn_file->invite_cookie = cookie;
+		msn_file->handle = g_strdup( handle );
+		msn_file->dcc = file;
+		msn_file->md->filetransfers = g_slist_prepend( msn_file->md->filetransfers, msn_file->dcc );
+		msn_file->fd = -1;
+	}
+
+	if( reject )
+		msn_ftp_cancel_invite( sb->ic, sb->who, cookie, reject );
+
+	g_free( name );
+	g_free( size );
+	g_free( invitecookie );
+	g_free( itype );
+}
+
+msn_filetransfer_t* msn_find_filetransfer( struct msn_data *md, unsigned int cookie, char *handle )
+{
+	GSList *l;
+	
+	for( l = md->filetransfers; l; l = l->next ) {
+		msn_filetransfer_t *file = ( (file_transfer_t*) l->data )->data;
+		if( file->invite_cookie == cookie && strcmp( handle, file->handle ) == 0 ) {
+			return file;
+		}
+	}
+	return NULL;
+}
+
+gboolean msn_ftps_connected( gpointer data, gint fd, b_input_condition cond )
+{
+	file_transfer_t *file = data;
+	msn_filetransfer_t *msn_file = file->data;
+	struct sockaddr_storage clt_addr;
+	socklen_t ssize = sizeof( clt_addr );
+	
+	debug( "Connected to MSNFTP client" );
+	
+	ASSERTSOCKOP( msn_file->fd = accept( fd, (struct sockaddr *) &clt_addr, &ssize ), "Accepting connection" );
+
+	closesocket( fd );
+	fd = msn_file->fd;
+	sock_make_nonblocking( fd );
+
+	msn_file->r_event_id = b_input_add( fd, GAIM_INPUT_READ, msn_ftp_read, file );
+
+	return FALSE;
+}
+
+void msn_invitations_accept( msn_filetransfer_t *msn_file, struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen )
+{
+	file_transfer_t *file = msn_file->dcc;
+	char buf[1024];
+	unsigned int acookie = time ( NULL );
+	char host[HOST_NAME_MAX+1];
+	char port[6];
+	char *errmsg;
+
+	msn_file->auth_cookie = acookie;
+
+	if( ( msn_file->fd = ft_listen( NULL, host, port, FALSE, &errmsg ) ) == -1 ) {
+		msn_ftp_abort( file, "Failed to listen locally, check your ft_listen setting in bitlbee.conf: %s", errmsg );
+		return;
+	}
+
+	msn_file->r_event_id = b_input_add( msn_file->fd, GAIM_INPUT_READ, msn_ftps_connected, file );
+
+	g_snprintf( buf, sizeof( buf ),
+		    "IP-Address: %s\r\n"
+		    "Port: %s\r\n"
+		    "AuthCookie: %d\r\n"
+		    "Launch-Application: FALSE\r\n"
+		    "Request-Data: IP-Address:\r\n\r\n",
+		    host,
+		    port,
+		    msn_file->auth_cookie );
+
+	msn_ftp_invitation_cmd( msn_file->md->ic, handle, msn_file->invite_cookie, "ACCEPT", buf );
+}
+
+void msn_invitationr_accept( msn_filetransfer_t *msn_file, struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen ) {
+	file_transfer_t *file = msn_file->dcc;
+	char *authcookie, *ip, *port;
+
+	if( !( authcookie = msn_findheader( body, "AuthCookie:", blen ) ) ||
+	    !( ip = msn_findheader( body, "IP-Address:", blen ) ) ||
+	    !( port = msn_findheader( body, "Port:", blen ) ) ) {
+		msn_ftp_abort( file, "Received invalid accept reply" );
+	} else if( 
+		( msn_file->fd = proxy_connect( ip, atoi( port ), msn_ftp_connected, file ) )
+		< 0 ) {
+			msn_ftp_abort( file, "Error connecting to MSN client" );
+	} else
+		msn_file->auth_cookie = strtoul( authcookie, NULL, 10 );
+
+	g_free( authcookie );
+	g_free( ip );
+	g_free( port );
+}
+
+void msn_invitation_accept( struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen )
+{
+	msn_filetransfer_t *msn_file = msn_find_filetransfer( sb->ic->proto_data, cookie, handle );
+	file_transfer_t *file = msn_file ? msn_file->dcc : NULL;
+	
+	if( !msn_file  )
+		imcb_log( sb->ic, "Received invitation ACCEPT message for unknown invitation (already aborted?)" );
+	else if( file->sending ) 
+		msn_invitations_accept( msn_file, sb, handle, cookie, body, blen );
+	else
+		msn_invitationr_accept( msn_file, sb, handle, cookie, body, blen );
+}
+
+void msn_invitation_cancel( struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen )
+{
+	msn_filetransfer_t *msn_file = msn_find_filetransfer( sb->ic->proto_data, cookie, handle );
+	
+	if( !msn_file )
+		imcb_log( sb->ic, "Received invitation CANCEL message for unknown invitation (already aborted?)" );
+	else
+		msn_ftp_abort( msn_file->dcc, msn_findheader( body, "Cancel-Code:", blen ) );
+}
+
+int msn_ftp_write( file_transfer_t *file, char *format, ... )
+{
+	msn_filetransfer_t *msn_file = file->data;
+	va_list params;
+	int st;
+	char *s;
+	
+	va_start( params, format );
+	s = g_strdup_vprintf( format, params );
+	va_end( params );
+	
+	st = write( msn_file->fd, s, strlen( s ) );
+	if( st != strlen( s ) )
+		return msn_ftp_abort( file, "Error sending data over MSNFTP connection: %s",
+				strerror( errno ) );
+	
+	g_free( s );
+	return 1;
+}
+
+gboolean msn_ftp_connected( gpointer data, gint fd, b_input_condition cond )
+{
+	file_transfer_t *file = data;
+	msn_filetransfer_t *msn_file = file->data;
+	
+	debug( "Connected to MSNFTP server, starting authentication" );
+	if( !msn_ftp_write( file, "VER MSNFTP\r\n" ) ) 
+		return FALSE;
+	
+	sock_make_nonblocking( msn_file->fd );
+	msn_file->r_event_id = b_input_add( msn_file->fd, GAIM_INPUT_READ, msn_ftp_read, file );
+	
+	return FALSE;
+}
+
+gboolean msn_ftp_handle_command( file_transfer_t *file, char* line )
+{
+	msn_filetransfer_t *msn_file = file->data;
+	char **cmd = msn_linesplit( line );
+	int count = 0;
+	if( cmd[0] ) while( cmd[++count] );
+	
+	if( count < 1 )
+		return msn_ftp_abort( file, "Missing command in MSNFTP communication" );
+	
+	if( strcmp( cmd[0], "VER" ) == 0 ) {
+		if( strcmp( cmd[1], "MSNFTP" ) != 0 )
+			return msn_ftp_abort( file, "Unsupported filetransfer protocol: %s", cmd[1] );
+		if( file->sending )
+			msn_ftp_write( file, "VER MSNFTP\r\n" );
+		else 
+			msn_ftp_write( file, "USR %s %u\r\n", msn_file->md->ic->acc->user, msn_file->auth_cookie );
+	} else if( strcmp( cmd[0], "FIL" ) == 0 ) {
+		if( strtoul( cmd[1], NULL, 10 ) != file->file_size )
+			return msn_ftp_abort( file, "FIL reply contains a different file size than the size in the invitation" );
+		msn_ftp_write( file, "TFR\r\n" );
+		msn_file->status |= MSN_TRANSFER_RECEIVING;
+	} else if( strcmp( cmd[0], "USR" ) == 0 ) {
+		if( ( strcmp( cmd[1], msn_file->handle ) != 0 ) ||
+		    ( strtoul( cmd[2], NULL, 10 ) != msn_file->auth_cookie ) )
+			msn_ftp_abort( file, "Authentication failed. "
+				"Expected handle: %s (got %s), cookie: %u (got %s)",
+				msn_file->handle, cmd[1],
+				msn_file->auth_cookie, cmd[2] );
+		msn_ftp_write( file, "FIL %zu\r\n", file->file_size);
+	} else if( strcmp( cmd[0], "TFR" ) == 0 ) {
+		file->write_request( file );
+	} else if( strcmp( cmd[0], "BYE" ) == 0 ) {
+		unsigned int retcode = count > 1 ? atoi(cmd[1]) : 1;
+
+		if( ( retcode==16777989 ) || ( retcode==16777987 ) )
+			imcb_file_finished( file );
+		else if( retcode==2147942405 )
+			imcb_file_canceled( file, "Failure: receiver is out of disk space" );
+		else if( retcode==2164261682 )
+			imcb_file_canceled( file, "Failure: receiver cancelled the transfer" );
+		else if( retcode==2164261683 )
+			imcb_file_canceled( file, "Failure: sender has cancelled the transfer" );
+		else if( retcode==2164261694 )
+			imcb_file_canceled( file, "Failure: connection is blocked" );
+		else {
+			char buf[128];
+
+			sprintf( buf, "Failure: unknown BYE code: %d", retcode);
+			imcb_file_canceled( file, buf );
+		}
+	} else if( strcmp( cmd[0], "CCL" ) == 0 ) {
+		imcb_file_canceled( file, "Failure: receiver cancelled the transfer" );
+	} else {
+		msn_ftp_abort( file, "Received invalid command %s from msn client", cmd[0] );
+	}
+	return TRUE;
+}
+
+gboolean msn_ftp_send( gpointer data, gint fd, b_input_condition cond )
+{
+	file_transfer_t *file = data;
+	msn_filetransfer_t *msn_file = file->data;
+
+	msn_file->w_event_id = 0;
+
+	file->write_request( file );
+
+	return FALSE;
+}
+
+/*
+ * This should only be called if we can write, so just do it.
+ * Add a write watch so we can write more during the next cycle (if possible).
+ * This got a bit complicated because (at least) amsn expects packets of size 2045.
+ */
+gboolean msn_ftps_write( file_transfer_t *file, char *buffer, unsigned int len )
+{
+	msn_filetransfer_t *msn_file = file->data;
+        int ret, overflow;
+
+	/* what we can't send now */
+	overflow = msn_file->sbufpos + len - MSNFTP_PSIZE;
+
+	/* append what we can do the send buffer */
+	memcpy( msn_file->sbuf + msn_file->sbufpos, buffer, MIN( len, MSNFTP_PSIZE - msn_file->sbufpos ) );
+	msn_file->sbufpos += MIN( len, MSNFTP_PSIZE - msn_file->sbufpos );
+
+	/* if we don't have enough for a full packet and there's more wait for it */
+	if( ( msn_file->sbufpos < MSNFTP_PSIZE ) && 
+	    ( msn_file->data_sent + msn_file->sbufpos - 3 < file->file_size ) ) {
+		if( !msn_file->w_event_id )
+			msn_file->w_event_id = b_input_add( msn_file->fd, GAIM_INPUT_WRITE, msn_ftp_send, file );
+		return TRUE;
+	}
+
+	/* Accumulated enough data, lets send something out */
+
+	msn_file->sbuf[0] = 0;
+	msn_file->sbuf[1] = ( msn_file->sbufpos - 3 ) & 0xff;
+	msn_file->sbuf[2] = ( ( msn_file->sbufpos - 3 ) >> 8 ) & 0xff;
+
+        ASSERTSOCKOP( ret = send( msn_file->fd, msn_file->sbuf, msn_file->sbufpos, 0 ), "Sending" );
+
+        msn_file->data_sent += ret - 3;
+
+        /* TODO: this should really not be fatal */
+        if( ret < msn_file->sbufpos )
+                return msn_ftp_abort( file, "send() sent %d instead of %d (send buffer full!)", ret, msn_file->sbufpos );
+
+	msn_file->sbufpos = 3;
+
+	if( overflow > 0 ) {
+		while( overflow > ( MSNFTP_PSIZE - 3 ) ) {
+			if( !msn_ftps_write( file, buffer + len - overflow, MSNFTP_PSIZE - 3 ) )
+				return FALSE;
+			overflow -= MSNFTP_PSIZE - 3;
+		}
+		return msn_ftps_write( file, buffer + len - overflow, overflow );
+	}
+
+	if( msn_file->data_sent == file->file_size ) {
+		if( msn_file->w_event_id ) {
+			b_event_remove( msn_file->w_event_id );
+			msn_file->w_event_id = 0;
+		}
+	} else {
+		/* we might already be listening if this is data from an overflow */
+		if( !msn_file->w_event_id )
+			msn_file->w_event_id = b_input_add( msn_file->fd, GAIM_INPUT_WRITE, msn_ftp_send, file );
+	}
+
+        return TRUE;
+}
+
+/* Binary part of the file transfer protocol */
+gboolean msn_ftpr_read( file_transfer_t *file ) 
+{
+	msn_filetransfer_t *msn_file = file->data;
+	int st;
+	unsigned char buf[3];
+
+	if( msn_file->data_remaining ) {
+		msn_file->r_event_id = 0;
+
+		ASSERTSOCKOP( st = read( msn_file->fd, file->buffer, MIN( sizeof( file->buffer ), msn_file->data_remaining ) ), "Receiving" );
+
+		if( st == 0 )
+			return msn_ftp_abort( file, "Remote end closed connection");
+
+		msn_file->data_sent += st;
+
+		msn_file->data_remaining -= st;
+
+		file->write( file, file->buffer, st );
+
+		if( msn_file->data_sent >= file->file_size )
+			imcb_file_finished( file );
+
+		return FALSE;
+	} else {
+		ASSERTSOCKOP( st = read( msn_file->fd, buf, 1 ), "Receiving" );
+		if( st == 0 ) {
+			return msn_ftp_abort( file, "read returned EOF while reading data header from msn client" );
+		} else if( buf[0] == '\r' || buf[0] == '\n' ) {
+			debug( "Discarding extraneous newline" );
+		} else if( buf[0] != 0 ) {
+			msn_ftp_abort( file, "Remote end canceled the transfer");
+			/* don't really care about these last 2 (should be 0,0) */
+			read( msn_file->fd, buf, 2 );
+			return FALSE;
+		} else {
+			unsigned int size;
+			ASSERTSOCKOP( st = read( msn_file->fd, buf, 2 ), "Receiving" );
+			if( st < 2 )
+				return msn_ftp_abort( file, "read returned EOF while reading data header from msn client" );
+
+			size = buf[0] + ((unsigned int) buf[1] << 8);
+			msn_file->data_remaining = size;
+		}
+	}
+	return TRUE;
+}
+
+/* Text mode part of the file transfer protocol */
+gboolean msn_ftp_txtproto( file_transfer_t *file )
+{
+	msn_filetransfer_t *msn_file = file->data;
+	int i = msn_file->tbufpos, st;
+	char *tbuf = msn_file->tbuf;
+
+	ASSERTSOCKOP( st = read( msn_file->fd, 
+				 tbuf + msn_file->tbufpos, 
+				 sizeof( msn_file->tbuf ) - msn_file->tbufpos ),
+				 "Receiving" );
+
+	if( st == 0 )
+		return msn_ftp_abort( file, "read returned EOF while reading text from msn client" );
+
+	msn_file->tbufpos += st;
+
+	do {
+		for( ;i < msn_file->tbufpos; i++ ) {
+			if( tbuf[i] == '\n' || tbuf[i] == '\r' ) {
+				tbuf[i] = '\0';
+				if( i > 0 )
+					msn_ftp_handle_command( file, tbuf );
+				else
+					while( tbuf[i] == '\n' || tbuf[i] == '\r' ) i++;
+				memmove( tbuf, tbuf + i + 1, msn_file->tbufpos - i - 1 );
+				msn_file->tbufpos -= i + 1;
+				i = 0;
+				break;
+			}
+		}
+	} while ( i < msn_file->tbufpos );
+
+	if( msn_file->tbufpos == sizeof( msn_file->tbuf ) )
+		return msn_ftp_abort( file, 
+				      "Line exceeded %d bytes in text protocol", 
+				      sizeof( msn_file->tbuf ) );
+	return TRUE;
+}
+
+gboolean msn_ftp_read( gpointer data, gint fd, b_input_condition cond )
+{
+	file_transfer_t *file = data;
+	msn_filetransfer_t *msn_file = file->data;
+	
+	if( msn_file->status & MSN_TRANSFER_RECEIVING )
+		return msn_ftpr_read( file );
+	else
+		return msn_ftp_txtproto( file );
+}
+
+void msn_ftp_free( file_transfer_t *file )
+{
+	msn_filetransfer_t *msn_file = file->data;
+	
+	if( msn_file->r_event_id )
+		b_event_remove( msn_file->r_event_id );
+
+	if( msn_file->w_event_id )
+		b_event_remove( msn_file->w_event_id );
+
+	if( msn_file->fd != -1 )
+		closesocket( msn_file->fd );
+
+	msn_file->md->filetransfers = g_slist_remove( msn_file->md->filetransfers, msn_file->dcc );
+	
+	g_free( msn_file->handle );
+	
+	g_free( msn_file );
+}
+
+void msn_ftpr_accept( file_transfer_t *file )
+{
+	msn_filetransfer_t *msn_file = file->data;
+
+	msn_ftp_invitation_cmd( msn_file->md->ic, msn_file->handle, msn_file->invite_cookie, "ACCEPT", 
+				"Launch-Application: FALSE\r\n" 
+				"Request-Data: IP-Address:\r\n");
+}
+
+void msn_ftp_finished( file_transfer_t *file )
+{
+	msn_ftp_write( file, "BYE 16777989\r\n" );
+}
+
+void msn_ftp_canceled( file_transfer_t *file, char *reason )
+{
+	msn_filetransfer_t *msn_file = file->data;
+
+	msn_ftp_cancel_invite( msn_file->md->ic, msn_file->handle, 
+			       msn_file->invite_cookie, 
+			       file->status & FT_STATUS_TRANSFERRING ? 
+					"FTTIMEOUT" : 
+					"FAIL" );
+
+	imcb_log( msn_file->md->ic, "File transfer aborted: %s", reason );
+}
+
+gboolean msn_ftpr_write_request( file_transfer_t *file )
+{
+	msn_filetransfer_t *msn_file = file->data;
+	if( msn_file->r_event_id != 0 ) {
+		msn_ftp_abort( file, 
+					"BUG in MSN file transfer:"
+					"write_request called when"
+					"already watching for input" );
+		return FALSE;
+	}
+
+	msn_file->r_event_id = 
+		b_input_add( msn_file->fd, GAIM_INPUT_READ, msn_ftp_read, file );
+
+	return TRUE;
+}
Index: protocols/msn/invitation.h
===================================================================
--- protocols/msn/invitation.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
+++ protocols/msn/invitation.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -0,0 +1,82 @@
+/********************************************************************\
+* BitlBee -- An IRC to other IM-networks gateway                     *
+*                                                                    *
+* Copyright 2006 Marijn Kruisselbrink and others                     *
+\********************************************************************/
+
+/* MSN module - File transfer support             */
+
+/*
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+ 
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+ 
+ You should have received a copy of the GNU General Public License with
+ the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
+ if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+ Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef _MSN_INVITATION_H
+#define _MSN_INVITATION_H
+
+#include "msn.h"
+
+#define MSN_INVITE_HEADERS	"MIME-Version: 1.0\r\n" \
+				"Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n" \
+				"\r\n"
+
+#define MSNFTP_PSIZE 2048
+
+typedef enum {
+	MSN_TRANSFER_RECEIVING	= 1,
+	MSN_TRANSFER_SENDING	= 2
+} msn_filetransfer_status_t;
+
+typedef struct msn_filetransfer
+{
+/* Generic invitation data */	
+	/* msn_data instance this invitation was received with. */
+	struct msn_data *md;
+	/* Cookie specifying this invitation. */
+	unsigned int invite_cookie;
+	/* Handle of user that started this invitation. */
+	char *handle;
+
+/* File transfer specific data */
+	/* Current status of the file transfer. */
+	msn_filetransfer_status_t status;
+	/* Pointer to the dcc structure for this transfer. */
+	file_transfer_t *dcc;
+	/* Socket the transfer is taking place over. */
+	int fd;
+	/* Cookie received in the original invitation, this must be sent as soon as
+	   a connection has been established. */
+	unsigned int auth_cookie;
+	/* Data remaining to be received in the current packet. */
+	unsigned int data_remaining;
+	/* Buffer containing received, but unprocessed text. */
+	char tbuf[256];
+	unsigned int tbufpos;
+	
+	unsigned int data_sent;
+
+	gint r_event_id;
+	gint w_event_id;
+	
+	unsigned char sbuf[2048];
+	int sbufpos;
+
+} msn_filetransfer_t;
+
+void msn_invitation_invite( struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen );
+void msn_invitation_accept( struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen );
+void msn_invitation_cancel( struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen );
+
+#endif
Index: protocols/msn/msn.c
===================================================================
--- protocols/msn/msn.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ protocols/msn/msn.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -79,4 +79,8 @@
 	if( md )
 	{
+		while( md->filetransfers ) {
+			imcb_file_canceled( md->filetransfers->data, "Closing connection" );
+		}
+		
 		if( md->fd >= 0 )
 			closesocket( md->fd );
@@ -328,4 +332,5 @@
 	ret->send_typing = msn_send_typing;
 	ret->handle_cmp = g_strcasecmp;
+	ret->transfer_request = msn_ftp_transfer_request;
 
 	register_protocol(ret);
Index: protocols/msn/msn.h
===================================================================
--- protocols/msn/msn.h	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ protocols/msn/msn.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -74,4 +74,5 @@
 	int sb_failures;
 	time_t first_sb_failure;
+	GSList *filetransfers;
 	
 	const struct msn_away_state *away_state;
@@ -189,3 +190,6 @@
 void msn_sb_stop_keepalives( struct msn_switchboard *sb );
 
+/* invitation.c */
+void msn_ftp_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *who );
+
 #endif //_MSN_H
Index: protocols/msn/ns.c
===================================================================
--- protocols/msn/ns.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ protocols/msn/ns.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -76,5 +76,5 @@
 	if( msn_write( ic, s, strlen( s ) ) )
 	{
-		ic->inpa = b_input_add( md->fd, GAIM_INPUT_READ, msn_ns_callback, ic );
+		ic->inpa = b_input_add( md->fd, B_EV_IO_READ, msn_ns_callback, ic );
 		imcb_log( ic, "Connected to server, waiting for reply" );
 	}
Index: protocols/msn/sb.c
===================================================================
--- protocols/msn/sb.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ protocols/msn/sb.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -29,4 +29,5 @@
 #include "passport.h"
 #include "md5.h"
+#include "invitation.h"
 
 static gboolean msn_sb_callback( gpointer data, gint source, b_input_condition cond );
@@ -173,4 +174,9 @@
 			buf = g_new0( char, i );
 			i = g_snprintf( buf, i, MSN_TYPING_HEADERS, sb->ic->acc->user );
+		}
+		else if( strncmp( text, MSN_INVITE_HEADERS, sizeof( MSN_INVITE_HEADERS ) - 1 ) == 0 ) 
+		{
+			buf = g_strdup( text );
+			i = strlen( buf );
 		}
 		else if( strcmp( text, SB_KEEPALIVE_MESSAGE ) == 0 )
@@ -315,5 +321,5 @@
 	
 	if( msn_sb_write( sb, buf, strlen( buf ) ) )
-		sb->inp = b_input_add( sb->fd, GAIM_INPUT_READ, msn_sb_callback, sb );
+		sb->inp = b_input_add( sb->fd, B_EV_IO_READ, msn_sb_callback, sb );
 	else
 		debug( "Error %d while connecting to switchboard server", 2 );
@@ -699,60 +705,39 @@
 		else if( g_strncasecmp( ct, "text/x-msmsgsinvite", 19 ) == 0 )
 		{
-			char *itype = msn_findheader( body, "Application-GUID:", blen );
-			char buf[1024];
+			char *command = msn_findheader( body, "Invitation-Command:", blen );
+			char *cookie = msn_findheader( body, "Invitation-Cookie:", blen );
+			unsigned int icookie;
 			
 			g_free( ct );
 			
-			*buf = 0;
-			
-			if( !itype )
-				return( 1 );
-			
-			/* File transfer. */
-			if( strcmp( itype, "{5D3E02AB-6190-11d3-BBBB-00C04F795683}" ) == 0 )
-			{
-				char *name = msn_findheader( body, "Application-File:", blen );
-				char *size = msn_findheader( body, "Application-FileSize:", blen );
-				
-				if( name && size )
-				{
-					g_snprintf( buf, sizeof( buf ), "<< \x02""BitlBee\x02"" - Filetransfer: `%s', %s bytes >>\n"
-					            "Filetransfers are not supported by BitlBee for now...", name, size );
-				}
-				else
-				{
-					strcpy( buf, "<< \x02""BitlBee\x02"" - Corrupted MSN filetransfer invitation message >>" );
-				}
-				
-				if( name ) g_free( name );
-				if( size ) g_free( size );
-			}
-			else
-			{
-				char *iname = msn_findheader( body, "Application-Name:", blen );
-				
-				g_snprintf( buf, sizeof( buf ), "<< \x02""BitlBee\x02"" - Unknown MSN invitation - %s (%s) >>",
-				                                itype, iname ? iname : "no name" );
-				
-				if( iname ) g_free( iname );
-			}
-			
-			g_free( itype );
-			
-			if( !*buf )
-				return( 1 );
-			
-			if( sb->who )
-			{
-				imcb_buddy_msg( ic, cmd[1], buf, 0, 0 );
-			}
-			else if( sb->chat )
-			{
-				imcb_chat_msg( sb->chat, cmd[1], buf, 0, 0 );
-			}
-			else
-			{
-				/* PANIC! */
-			}
+			/* Every invite should have both a Command and Cookie header */
+			if( !command || !cookie ) {
+				g_free( command );
+				g_free( cookie );
+				imcb_log( ic, "Warning: No command or cookie from %s", sb->who );
+				return 1;
+			}
+			
+			icookie = strtoul( cookie, NULL, 10 );
+			g_free( cookie );
+			
+			if( g_strncasecmp( command, "INVITE", 6 ) == 0 ) {
+				msn_invitation_invite( sb, cmd[1], icookie, body, blen );
+			} else if( g_strncasecmp( command, "ACCEPT", 6 ) == 0 ) {
+				msn_invitation_accept( sb, cmd[1], icookie, body, blen );
+			} else if( g_strncasecmp( command, "CANCEL", 6 ) == 0 ) {
+				msn_invitation_cancel( sb, cmd[1], icookie, body, blen );
+			} else {
+				imcb_log( ic, "Warning: Received invalid invitation with "
+						"command %s from %s", command, sb->who );
+			}
+			
+			g_free( command );
+		}
+		else if( g_strncasecmp( ct, "application/x-msnmsgrp2p", 24 ) == 0 ) 
+		{
+			imcb_error( sb->ic, "Cannot receive file from %s: BitlBee does not "
+					"support msnmsgrp2p yet.", sb->who );
+			g_free( ct );
 		}
 		else if( g_strncasecmp( ct, "text/x-msmsgscontrol", 20 ) == 0 )
Index: protocols/nogaim.c
===================================================================
--- protocols/nogaim.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ protocols/nogaim.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -117,10 +117,13 @@
 {
 	GList *gl;
-	for (gl = protocols; gl; gl = gl->next) 
+	
+	for( gl = protocols; gl; gl = gl->next )
  	{
  		struct prpl *proto = gl->data;
- 		if(!g_strcasecmp(proto->name, name)) 
+ 		
+ 		if( g_strcasecmp( proto->name, name ) == 0 )
 			return proto;
  	}
+ 	
  	return NULL;
 }
@@ -134,4 +137,5 @@
 	extern void jabber_initmodule();
 	extern void twitter_initmodule();
+	extern void purple_initmodule();
 
 #ifdef WITH_MSN
@@ -153,4 +157,8 @@
 #ifdef WITH_TWITTER
 	twitter_initmodule();
+#endif
+	
+#ifdef WITH_PURPLE
+	purple_initmodule();
 #endif
 
Index: protocols/nogaim.h
===================================================================
--- protocols/nogaim.h	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ protocols/nogaim.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -45,4 +45,6 @@
 #include "proxy.h"
 #include "query.h"
+#include "md5.h"
+#include "ft.h"
 
 #define BUDDY_ALIAS_MAXLEN 388   /* because MSN names can be 387 characters */
@@ -132,4 +134,5 @@
 	 * - The user sees this name ie. when imcb_log() is used. */
 	const char *name;
+	void *data;
 
 	/* Added this one to be able to add per-account settings, don't think
@@ -228,4 +231,7 @@
 	void (* auth_allow)	(struct im_connection *, const char *who);
 	void (* auth_deny)	(struct im_connection *, const char *who);
+
+	/* Incoming transfer request */
+	void (* transfer_request) (struct im_connection *, file_transfer_t *ft, char *handle );
 };
 
Index: protocols/oscar/oscar.c
===================================================================
--- protocols/oscar/oscar.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ protocols/oscar/oscar.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -291,5 +291,5 @@
 	odata = (struct oscar_data *)ic->proto_data;
 
-	if (condition & GAIM_INPUT_READ) {
+	if (condition & B_EV_IO_READ) {
 		if (aim_get_command(odata->sess, conn) >= 0) {
 			aim_rxdispatch(odata->sess);
@@ -363,5 +363,5 @@
 
 	aim_conn_completeconnect(sess, conn);
-	ic->inpa = b_input_add(conn->fd, GAIM_INPUT_READ,
+	ic->inpa = b_input_add(conn->fd, B_EV_IO_READ,
 			oscar_callback, conn);
 	
@@ -493,5 +493,5 @@
 
 	aim_conn_completeconnect(sess, bosconn);
-	ic->inpa = b_input_add(bosconn->fd, GAIM_INPUT_READ,
+	ic->inpa = b_input_add(bosconn->fd, B_EV_IO_READ,
 			oscar_callback, bosconn);
 	imcb_log(ic, _("Connection established, cookie sent"));
@@ -669,5 +669,5 @@
 	if (pos->modname)
 		g_free(pos->modname);
-	pos->inpa = b_input_add(pos->fd, GAIM_INPUT_READ, damn_you, pos);
+	pos->inpa = b_input_add(pos->fd, B_EV_IO_READ, damn_you, pos);
 	return FALSE;
 }
@@ -838,5 +838,5 @@
 
 	aim_conn_completeconnect(sess, tstconn);
-	odata->cnpa = b_input_add(tstconn->fd, GAIM_INPUT_READ,
+	odata->cnpa = b_input_add(tstconn->fd, B_EV_IO_READ,
 					oscar_callback, tstconn);
 	
@@ -866,5 +866,5 @@
 
 	aim_conn_completeconnect(sess, tstconn);
-	odata->paspa = b_input_add(tstconn->fd, GAIM_INPUT_READ,
+	odata->paspa = b_input_add(tstconn->fd, B_EV_IO_READ,
 				oscar_callback, tstconn);
 	
@@ -902,5 +902,5 @@
 	aim_conn_completeconnect(sess, ccon->conn);
 	ccon->inpa = b_input_add(tstconn->fd,
-			GAIM_INPUT_READ,
+			B_EV_IO_READ,
 			oscar_callback, tstconn);
 	odata->oscar_chats = g_slist_append(odata->oscar_chats, ccon);
Index: protocols/purple/Makefile
===================================================================
--- protocols/purple/Makefile	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
+++ protocols/purple/Makefile	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -0,0 +1,41 @@
+###########################
+## Makefile for BitlBee  ##
+##                       ##
+## Copyright 2002 Lintux ##
+###########################
+
+### DEFINITIONS
+
+-include ../../Makefile.settings
+
+# [SH] Program variables
+objects = purple.o
+
+CFLAGS += -Wall $(PURPLE_CFLAGS)
+LFLAGS += -r
+
+# [SH] Phony targets
+all: purple_mod.o
+check: all
+lcov: check
+gcov: 
+	gcov *.c
+
+.PHONY: all clean distclean
+
+clean:
+	rm -f *.o core
+
+distclean: clean
+
+### MAIN PROGRAM
+
+$(objects): ../../Makefile.settings Makefile
+
+$(objects): %.o: %.c
+	@echo '*' Compiling $<
+	@$(CC) -c $(CFLAGS) $< -o $@
+
+purple_mod.o: $(objects)
+	@echo '*' Linking purple_mod.o
+	$(LD) $(LFLAGS) $(objects) -o purple_mod.o
Index: protocols/purple/purple.c
===================================================================
--- protocols/purple/purple.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
+++ protocols/purple/purple.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -0,0 +1,958 @@
+/***************************************************************************\
+*                                                                           *
+*  BitlBee - An IRC to IM gateway                                           *
+*  libpurple module - Main file                                             *
+*                                                                           *
+*  Copyright 2009-2010 Wilmer van der Gaast <wilmer@gaast.net>              *
+*                                                                           *
+*  This program is free software; you can redistribute it and/or modify     *
+*  it under the terms of the GNU General Public License as published by     *
+*  the Free Software Foundation; either version 2 of the License, or        *
+*  (at your option) any later version.                                      *
+*                                                                           *
+*  This program is distributed in the hope that it will be useful,          *
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
+*  GNU General Public License for more details.                             *
+*                                                                           *
+*  You should have received a copy of the GNU General Public License along  *
+*  with this program; if not, write to the Free Software Foundation, Inc.,  *
+*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              *
+*                                                                           *
+\***************************************************************************/
+
+#include "bitlbee.h"
+#include "help.h"
+
+#include <stdarg.h>
+
+#include <glib.h>
+#include <purple.h>
+
+GSList *purple_connections;
+
+/* This makes me VERY sad... :-( But some libpurple callbacks come in without
+   any context so this is the only way to get that. Don't want to support
+   libpurple in daemon mode anyway. */
+static irc_t *local_irc;
+
+static struct im_connection *purple_ic_by_pa( PurpleAccount *pa )
+{
+	GSList *i;
+	
+	for( i = purple_connections; i; i = i->next )
+		if( ((struct im_connection *)i->data)->proto_data == pa )
+			return i->data;
+	
+	return NULL;
+}
+
+static struct im_connection *purple_ic_by_gc( PurpleConnection *gc )
+{
+	return purple_ic_by_pa( purple_connection_get_account( gc ) );
+}
+
+static void purple_init( account_t *acc )
+{
+	PurplePlugin *prpl = purple_plugins_find_with_id( (char*) acc->prpl->data );
+	PurplePluginProtocolInfo *pi = prpl->info->extra_info;
+	PurpleAccount *pa;
+	GList *i, *st;
+	set_t *s;
+	char help_title[64];
+	GString *help;
+	
+	help = g_string_new( "" );
+	g_string_printf( help, "BitlBee libpurple module %s (%s).\n\nSupported settings:",
+	                        (char*) acc->prpl->name, prpl->info->name );
+	
+	/* Convert all protocol_options into per-account setting variables. */
+	for( i = pi->protocol_options; i; i = i->next )
+	{
+		PurpleAccountOption *o = i->data;
+		const char *name;
+		char *def = NULL;
+		set_eval eval = NULL;
+		void *eval_data = NULL;
+		GList *io = NULL;
+		GSList *opts = NULL;
+		
+		name = purple_account_option_get_setting( o );
+		
+		switch( purple_account_option_get_type( o ) )
+		{
+		case PURPLE_PREF_STRING:
+			def = g_strdup( purple_account_option_get_default_string( o ) );
+			
+			g_string_append_printf( help, "\n* %s (%s), %s, default: %s",
+			                        name, purple_account_option_get_text( o ),
+			                        "string", def );
+			
+			break;
+		
+		case PURPLE_PREF_INT:
+			def = g_strdup_printf( "%d", purple_account_option_get_default_int( o ) );
+			eval = set_eval_int;
+			
+			g_string_append_printf( help, "\n* %s (%s), %s, default: %s",
+			                        name, purple_account_option_get_text( o ),
+			                        "integer", def );
+			
+			break;
+		
+		case PURPLE_PREF_BOOLEAN:
+			if( purple_account_option_get_default_bool( o ) )
+				def = g_strdup( "true" );
+			else
+				def = g_strdup( "false" );
+			eval = set_eval_bool;
+			
+			g_string_append_printf( help, "\n* %s (%s), %s, default: %s",
+			                        name, purple_account_option_get_text( o ),
+			                        "boolean", def );
+			
+			break;
+		
+		case PURPLE_PREF_STRING_LIST:
+			def = g_strdup( purple_account_option_get_default_list_value( o ) );
+			
+			g_string_append_printf( help, "\n* %s (%s), %s, default: %s",
+			                        name, purple_account_option_get_text( o ),
+			                        "list", def );
+			g_string_append( help, "\n  Possible values: " );
+			
+			for( io = purple_account_option_get_list( o ); io; io = io->next )
+			{
+				PurpleKeyValuePair *kv = io->data;
+				opts = g_slist_append( opts, kv->key );
+				g_string_append_printf( help, "%s, ", kv->key );
+			}
+			g_string_truncate( help, help->len - 2 );
+			eval = set_eval_list;
+			eval_data = opts;
+			
+			break;
+			
+		default:
+			irc_usermsg( acc->irc, "Setting with unknown type: %s (%d) Expect stuff to break..\n",
+			             name, purple_account_option_get_type( o ) );
+			name = NULL;
+		}
+		
+		if( name != NULL )
+		{
+			s = set_add( &acc->set, name, def, eval, acc );
+			s->flags |= ACC_SET_OFFLINE_ONLY;
+			s->eval_data = eval_data;
+			g_free( def );
+		}
+	}
+	
+	g_snprintf( help_title, sizeof( help_title ), "purple %s", (char*) acc->prpl->name );
+	help_add_mem( &global.help, help_title, help->str );
+	g_string_free( help, TRUE );
+	
+	if( pi->options & OPT_PROTO_MAIL_CHECK )
+	{
+		s = set_add( &acc->set, "mail_notifications", "false", set_eval_bool, acc );
+		s->flags |= ACC_SET_OFFLINE_ONLY;
+	}
+	
+	/* Go through all away states to figure out if away/status messages
+	   are possible. */
+	pa = purple_account_new( acc->user, (char*) acc->prpl->data );
+	for( st = purple_account_get_status_types( pa ); st; st = st->next )
+	{
+		PurpleStatusPrimitive prim = purple_status_type_get_primitive( st->data );
+		
+		if( prim == PURPLE_STATUS_AVAILABLE )
+		{
+			if( purple_status_type_get_attr( st->data, "message" ) )
+				acc->flags |= ACC_FLAG_STATUS_MESSAGE;
+		}
+		else if( prim != PURPLE_STATUS_OFFLINE )
+		{
+			if( purple_status_type_get_attr( st->data, "message" ) )
+				acc->flags |= ACC_FLAG_AWAY_MESSAGE;
+		}
+	}
+	purple_accounts_remove( pa );
+}
+
+static void purple_sync_settings( account_t *acc, PurpleAccount *pa )
+{
+	PurplePlugin *prpl = purple_plugins_find_with_id( pa->protocol_id );
+	PurplePluginProtocolInfo *pi = prpl->info->extra_info;
+	GList *i;
+	
+	for( i = pi->protocol_options; i; i = i->next )
+	{
+		PurpleAccountOption *o = i->data;
+		const char *name;
+		set_t *s;
+		
+		name = purple_account_option_get_setting( o );
+		s = set_find( &acc->set, name );
+		if( s->value == NULL )
+			continue;
+		
+		switch( purple_account_option_get_type( o ) )
+		{
+		case PURPLE_PREF_STRING:
+		case PURPLE_PREF_STRING_LIST:
+			purple_account_set_string( pa, name, set_getstr( &acc->set, name ) );
+			break;
+		
+		case PURPLE_PREF_INT:
+			purple_account_set_int( pa, name, set_getint( &acc->set, name ) );
+			break;
+		
+		case PURPLE_PREF_BOOLEAN:
+			purple_account_set_bool( pa, name, set_getbool( &acc->set, name ) );
+			break;
+		
+		default:
+			break;
+		}
+	}
+	
+	if( pi->options & OPT_PROTO_MAIL_CHECK )
+		purple_account_set_check_mail( pa, set_getbool( &acc->set, "mail_notifications" ) );
+}
+
+static void purple_login( account_t *acc )
+{
+	struct im_connection *ic = imcb_new( acc );
+	PurpleAccount *pa;
+	
+	if( local_irc != NULL && local_irc != acc->irc )
+	{
+		irc_usermsg( acc->irc, "Daemon mode detected. Do *not* try to use libpurple in daemon mode! "
+		                       "Please use inetd or ForkDaemon mode instead." );
+		return;
+	}
+	local_irc = acc->irc;
+	
+	/* For now this is needed in the _connected() handlers if using
+	   GLib event handling, to make sure we're not handling events
+	   on dead connections. */
+	purple_connections = g_slist_prepend( purple_connections, ic );
+	
+	ic->proto_data = pa = purple_account_new( acc->user, (char*) acc->prpl->data );
+	purple_account_set_password( pa, acc->pass );
+	purple_sync_settings( acc, pa );
+	
+	purple_account_set_enabled( pa, "BitlBee", TRUE );
+}
+
+static void purple_logout( struct im_connection *ic )
+{
+	PurpleAccount *pa = ic->proto_data;
+	
+	purple_account_set_enabled( pa, "BitlBee", FALSE );
+	purple_connections = g_slist_remove( purple_connections, ic );
+	purple_accounts_remove( pa );
+}
+
+static int purple_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
+{
+	PurpleConversation *conv;
+	
+	if( ( conv = purple_find_conversation_with_account( PURPLE_CONV_TYPE_IM,
+	                                                    who, ic->proto_data ) ) == NULL )
+	{
+		conv = purple_conversation_new( PURPLE_CONV_TYPE_IM,
+		                                ic->proto_data, who );
+	}
+	
+	purple_conv_im_send( purple_conversation_get_im_data( conv ), message );
+	
+	return 1;
+}
+
+static GList *purple_away_states( struct im_connection *ic )
+{
+	PurpleAccount *pa = ic->proto_data;
+	GList *st, *ret = NULL;
+	
+	for( st = purple_account_get_status_types( pa ); st; st = st->next )
+	{
+		PurpleStatusPrimitive prim = purple_status_type_get_primitive( st->data );
+		if( prim != PURPLE_STATUS_AVAILABLE && prim != PURPLE_STATUS_OFFLINE )
+			ret = g_list_append( ret, (void*) purple_status_type_get_name( st->data ) );
+	}
+	
+	return ret;
+}
+
+static void purple_set_away( struct im_connection *ic, char *state_txt, char *message )
+{
+	PurpleAccount *pa = ic->proto_data;
+	GList *status_types = purple_account_get_status_types( pa ), *st;
+	PurpleStatusType *pst = NULL;
+	GList *args = NULL;
+	
+	for( st = status_types; st; st = st->next )
+	{
+		pst = st->data;
+		
+		if( state_txt == NULL &&
+		    purple_status_type_get_primitive( pst ) == PURPLE_STATUS_AVAILABLE )
+			break;
+
+		if( state_txt != NULL &&
+		    g_strcasecmp( state_txt, purple_status_type_get_name( pst ) ) == 0 )
+			break;
+	}
+	
+	if( message && purple_status_type_get_attr( pst, "message" ) )
+	{
+		args = g_list_append( args, "message" );
+		args = g_list_append( args, message );
+	}
+	
+	purple_account_set_status_list( pa, st ? purple_status_type_get_id( pst ) : "away",
+		                        TRUE, args );
+
+	g_list_free( args );
+}
+
+static void purple_add_buddy( struct im_connection *ic, char *who, char *group )
+{
+	PurpleBuddy *pb;
+	
+	pb = purple_buddy_new( (PurpleAccount*) ic->proto_data, who, NULL );
+	purple_blist_add_buddy( pb, NULL, NULL, NULL );
+	purple_account_add_buddy( (PurpleAccount*) ic->proto_data, pb );
+}
+
+static void purple_remove_buddy( struct im_connection *ic, char *who, char *group )
+{
+	PurpleBuddy *pb;
+	
+	pb = purple_find_buddy( (PurpleAccount*) ic->proto_data, who );
+	if( pb != NULL )
+	{
+		purple_account_remove_buddy( (PurpleAccount*) ic->proto_data, pb, NULL );
+		purple_blist_remove_buddy( pb );
+	}
+}
+
+static void purple_keepalive( struct im_connection *ic )
+{
+}
+
+static int purple_send_typing( struct im_connection *ic, char *who, int flags )
+{
+	PurpleTypingState state = PURPLE_NOT_TYPING;
+	PurpleConversation *conv;
+	
+	if( flags & OPT_TYPING )
+		state = PURPLE_TYPING;
+	else if( flags & OPT_THINKING )
+		state = PURPLE_TYPED;
+	
+	if( ( conv = purple_find_conversation_with_account( PURPLE_CONV_TYPE_IM,
+	                                                    who, ic->proto_data ) ) == NULL )
+	{
+		purple_conv_im_set_typing_state( purple_conversation_get_im_data( conv ), state );
+		return 1;
+	}
+	else
+	{
+		return 0;
+	}
+}
+
+void purple_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *handle );
+
+static void purple_ui_init();
+
+static PurpleCoreUiOps bee_core_uiops = 
+{
+	NULL,
+	NULL,
+	purple_ui_init,
+	NULL,
+};
+
+static void prplcb_conn_progress( PurpleConnection *gc, const char *text, size_t step, size_t step_count )
+{
+	struct im_connection *ic = purple_ic_by_gc( gc );
+	
+	imcb_log( ic, "%s", text );
+}
+
+static void prplcb_conn_connected( PurpleConnection *gc )
+{
+	struct im_connection *ic = purple_ic_by_gc( gc );
+	
+	imcb_connected( ic );
+	
+	if( gc->flags & PURPLE_CONNECTION_HTML )
+		ic->flags |= OPT_DOES_HTML;
+}
+
+static void prplcb_conn_disconnected( PurpleConnection *gc )
+{
+	struct im_connection *ic = purple_ic_by_gc( gc );
+	
+	if( ic != NULL )
+	{
+		imc_logout( ic, TRUE );
+	}
+}
+
+static void prplcb_conn_notice( PurpleConnection *gc, const char *text )
+{
+	struct im_connection *ic = purple_ic_by_gc( gc );
+	
+	if( ic != NULL )
+		imcb_log( ic, "%s", text );
+}
+
+static void prplcb_conn_report_disconnect_reason( PurpleConnection *gc, PurpleConnectionError reason, const char *text )
+{
+	struct im_connection *ic = purple_ic_by_gc( gc );
+	
+	/* PURPLE_CONNECTION_ERROR_NAME_IN_USE means concurrent login,
+	   should probably handle that. */
+	if( ic != NULL )
+		imcb_error( ic, "%s", text );
+}
+
+static PurpleConnectionUiOps bee_conn_uiops =
+{
+	prplcb_conn_progress,
+	prplcb_conn_connected,
+	prplcb_conn_disconnected,
+	prplcb_conn_notice,
+	NULL,
+	NULL,
+	NULL,
+	prplcb_conn_report_disconnect_reason,
+};
+
+static void prplcb_blist_new( PurpleBlistNode *node )
+{
+	PurpleBuddy *bud = (PurpleBuddy*) node;
+	
+	if( node->type == PURPLE_BLIST_BUDDY_NODE )
+	{
+		struct im_connection *ic = purple_ic_by_pa( bud->account );
+		
+		if( ic == NULL )
+			return;
+		
+		imcb_add_buddy( ic, bud->name, NULL );
+		if( bud->server_alias )
+		{
+			imcb_rename_buddy( ic, bud->name, bud->server_alias );
+			imcb_buddy_nick_hint( ic, bud->name, bud->server_alias );
+		}
+	}
+}
+
+static void prplcb_blist_update( PurpleBuddyList *list, PurpleBlistNode *node )
+{
+	PurpleBuddy *bud = (PurpleBuddy*) node;
+	
+	if( node->type == PURPLE_BLIST_BUDDY_NODE )
+	{
+		struct im_connection *ic = purple_ic_by_pa( bud->account );
+		PurpleStatus *as;
+		int flags = 0;
+		
+		if( ic == NULL )
+			return;
+		
+		if( bud->server_alias )
+			imcb_rename_buddy( ic, bud->name, bud->server_alias );
+		
+		flags |= purple_presence_is_online( bud->presence ) ? OPT_LOGGED_IN : 0;
+		flags |= purple_presence_is_available( bud->presence ) ? 0 : OPT_AWAY;
+		
+		as = purple_presence_get_active_status( bud->presence );
+		
+		imcb_buddy_status( ic, bud->name, flags, purple_status_get_name( as ),
+		                   purple_status_get_attr_string( as, "message" ) );
+	}
+}
+
+static void prplcb_blist_remove( PurpleBuddyList *list, PurpleBlistNode *node )
+{
+	/*
+	PurpleBuddy *bud = (PurpleBuddy*) node;
+	
+	if( node->type == PURPLE_BLIST_BUDDY_NODE )
+	{
+		struct im_connection *ic = purple_ic_by_pa( bud->account );
+		
+		if( ic == NULL )
+			return;
+		
+		imcb_remove_buddy( ic, bud->name, NULL );
+	}
+	*/
+}
+
+static PurpleBlistUiOps bee_blist_uiops =
+{
+	NULL,
+	prplcb_blist_new,
+	NULL,
+	prplcb_blist_update,
+	prplcb_blist_remove,
+};
+
+static void prplcb_conv_im( PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, time_t mtime )
+{
+	struct im_connection *ic = purple_ic_by_pa( conv->account );
+	PurpleBuddy *buddy;
+	
+	/* ..._SEND means it's an outgoing message, no need to echo those. */
+	if( flags & PURPLE_MESSAGE_SEND )
+		return;
+	
+	buddy = purple_find_buddy( conv->account, who );
+	if( buddy != NULL )
+		who = purple_buddy_get_name( buddy );
+	
+	imcb_buddy_msg( ic, (char*) who, (char*) message, 0, mtime );
+}
+
+static PurpleConversationUiOps bee_conv_uiops = 
+{
+	NULL,                      /* create_conversation  */
+	NULL,                      /* destroy_conversation */
+	NULL,                      /* write_chat           */
+	prplcb_conv_im,            /* write_im             */
+	NULL,                      /* write_conv           */
+	NULL,                      /* chat_add_users       */
+	NULL,                      /* chat_rename_user     */
+	NULL,                      /* chat_remove_users    */
+	NULL,                      /* chat_update_user     */
+	NULL,                      /* present              */
+	NULL,                      /* has_focus            */
+	NULL,                      /* custom_smiley_add    */
+	NULL,                      /* custom_smiley_write  */
+	NULL,                      /* custom_smiley_close  */
+	NULL,                      /* send_confirm         */
+};
+
+struct prplcb_request_action_data
+{
+	void *user_data, *bee_data;
+	PurpleRequestActionCb yes, no;
+	int yes_i, no_i;
+};
+
+static void prplcb_request_action_yes( void *data )
+{
+	struct prplcb_request_action_data *pqad = data;
+	
+	pqad->yes( pqad->user_data, pqad->yes_i );
+	g_free( pqad );
+}
+
+static void prplcb_request_action_no( void *data )
+{
+	struct prplcb_request_action_data *pqad = data;
+	
+	pqad->no( pqad->user_data, pqad->no_i );
+	g_free( pqad );
+}
+
+static void *prplcb_request_action( const char *title, const char *primary, const char *secondary,
+                                    int default_action, PurpleAccount *account, const char *who,
+                                    PurpleConversation *conv, void *user_data, size_t action_count,
+                                    va_list actions )
+{
+	struct prplcb_request_action_data *pqad; 
+	int i;
+	char *q;
+	
+	pqad = g_new0( struct prplcb_request_action_data, 1 );
+	
+	for( i = 0; i < action_count; i ++ )
+	{
+		char *caption;
+		void *fn;
+		
+		caption = va_arg( actions, char* );
+		fn = va_arg( actions, void* );
+		
+		if( strstr( caption, "Accept" ) )
+		{
+			pqad->yes = fn;
+			pqad->yes_i = i;
+		}
+		else if( strstr( caption, "Reject" ) || strstr( caption, "Cancel" ) )
+		{
+			pqad->no = fn;
+			pqad->no_i = i;
+		}
+	}
+	
+	pqad->user_data = user_data;
+	
+	q = g_strdup_printf( "Request: %s\n\n%s\n\n%s", title, primary, secondary );
+	pqad->bee_data = query_add( local_irc, purple_ic_by_pa( account ), q,
+		prplcb_request_action_yes, prplcb_request_action_no, pqad );
+	
+	g_free( q );
+	
+	return pqad;
+}
+
+static PurpleRequestUiOps bee_request_uiops =
+{
+	NULL,
+	NULL,
+	prplcb_request_action,
+	NULL,
+	NULL,
+	NULL,
+	NULL,
+};
+
+static void prplcb_debug_print( PurpleDebugLevel level, const char *category, const char *arg_s )
+{
+	fprintf( stderr, "DEBUG %s: %s", category, arg_s );
+}
+
+static PurpleDebugUiOps bee_debug_uiops =
+{
+	prplcb_debug_print,
+};
+
+static guint prplcb_ev_timeout_add( guint interval, GSourceFunc func, gpointer udata )
+{
+	return b_timeout_add( interval, (b_event_handler) func, udata );
+}
+
+static guint prplcb_ev_input_add( int fd, PurpleInputCondition cond, PurpleInputFunction func, gpointer udata )
+{
+	return b_input_add( fd, cond | B_EV_FLAG_FORCE_REPEAT, (b_event_handler) func, udata );
+}
+
+static gboolean prplcb_ev_remove( guint id )
+{
+	b_event_remove( (gint) id );
+	return TRUE;
+}
+
+static PurpleEventLoopUiOps glib_eventloops = 
+{
+	prplcb_ev_timeout_add,
+	prplcb_ev_remove,
+	prplcb_ev_input_add,
+	prplcb_ev_remove,
+};
+
+static void *prplcb_notify_email( PurpleConnection *gc, const char *subject, const char *from,
+                                  const char *to, const char *url )
+{
+	struct im_connection *ic = purple_ic_by_gc( gc );
+	
+	imcb_log( ic, "Received e-mail from %s for %s: %s <%s>", from, to, subject, url );
+	
+	return NULL;
+}
+
+static PurpleNotifyUiOps bee_notify_uiops =
+{
+        NULL,
+        prplcb_notify_email,
+};
+
+
+struct prpl_xfer_data
+{
+	PurpleXfer *xfer;
+	file_transfer_t *ft;
+	gint ready_timer;
+	char *buf;
+	int buf_len;
+};
+
+static file_transfer_t *next_ft;
+
+/* Glorious hack: We seem to have to remind at least some libpurple plugins
+   that we're ready because this info may get lost if we give it too early.
+   So just do it ten times a second. :-/ */
+static gboolean prplcb_xfer_write_request_cb( gpointer data, gint fd, b_input_condition cond )
+{
+	struct prpl_xfer_data *px = data;
+	
+	purple_xfer_ui_ready( px->xfer );
+	
+	return purple_xfer_get_type( px->xfer ) == PURPLE_XFER_RECEIVE;
+}
+
+static gboolean prpl_xfer_write_request( struct file_transfer *ft )
+{
+	struct prpl_xfer_data *px = ft->data;
+	px->ready_timer = b_timeout_add( 100, prplcb_xfer_write_request_cb, px );
+	return TRUE;
+}
+
+static gssize prplcb_xfer_write( PurpleXfer *xfer, const guchar *buffer, gssize size )
+{
+	struct prpl_xfer_data *px = xfer->ui_data;
+	gboolean st;
+	
+	b_event_remove( px->ready_timer );
+	px->ready_timer = 0;
+	
+	st = px->ft->write( px->ft, (char*) buffer, size );
+	
+	if( st && xfer->bytes_remaining == size )
+		imcb_file_finished( px->ft );
+	
+	return st ? size : 0;
+}
+
+static gboolean prpl_xfer_write( struct file_transfer *ft, char *buffer, unsigned int len )
+{
+	struct prpl_xfer_data *px = ft->data;
+	
+	px->buf = g_memdup( buffer, len );
+	px->buf_len = len;
+	
+	//purple_xfer_ui_ready( px->xfer );
+	px->ready_timer = b_timeout_add( 0, prplcb_xfer_write_request_cb, px );
+	
+	return TRUE;
+}
+
+static void prpl_xfer_accept( struct file_transfer *ft )
+{
+	struct prpl_xfer_data *px = ft->data;
+	purple_xfer_request_accepted( px->xfer, NULL );
+	prpl_xfer_write_request( ft );
+}
+
+static void prpl_xfer_canceled( struct file_transfer *ft, char *reason )
+{
+	struct prpl_xfer_data *px = ft->data;
+	purple_xfer_request_denied( px->xfer );
+}
+
+static gboolean prplcb_xfer_new_send_cb( gpointer data, gint fd, b_input_condition cond )
+{
+	PurpleXfer *xfer = data;
+	struct im_connection *ic = purple_ic_by_pa( xfer->account );
+	struct prpl_xfer_data *px = g_new0( struct prpl_xfer_data, 1 );
+	PurpleBuddy *buddy;
+	const char *who;
+	
+	buddy = purple_find_buddy( xfer->account, xfer->who );
+	who = buddy ? purple_buddy_get_name( buddy ) : xfer->who;
+	
+	/* TODO(wilmer): After spreading some more const goodness in BitlBee,
+	   remove the evil cast below. */
+	px->ft = imcb_file_send_start( ic, (char*) who, xfer->filename, xfer->size );
+	px->ft->data = px;
+	px->xfer = data;
+	px->xfer->ui_data = px;
+	
+	px->ft->accept = prpl_xfer_accept;
+	px->ft->canceled = prpl_xfer_canceled;
+	px->ft->write_request = prpl_xfer_write_request;
+	
+	return FALSE;
+}
+
+static void prplcb_xfer_new( PurpleXfer *xfer )
+{
+	if( purple_xfer_get_type( xfer ) == PURPLE_XFER_RECEIVE )
+	{
+		/* This should suppress the stupid file dialog. */
+		purple_xfer_set_local_filename( xfer, "/tmp/wtf123" );
+		
+		/* Sadly the xfer struct is still empty ATM so come back after
+		   the caller is done. */
+		b_timeout_add( 0, prplcb_xfer_new_send_cb, xfer );
+	}
+	else
+	{
+		struct prpl_xfer_data *px = g_new0( struct prpl_xfer_data, 1 );
+		
+		px->ft = next_ft;
+		px->ft->data = px;
+		px->xfer = xfer;
+		px->xfer->ui_data = px;
+		
+		purple_xfer_set_filename( xfer, px->ft->file_name );
+		purple_xfer_set_size( xfer, px->ft->file_size );
+		
+		next_ft = NULL;
+	}
+}
+
+static void prplcb_xfer_dbg( PurpleXfer *xfer )
+{
+	fprintf( stderr, "prplcb_xfer_dbg 0x%p\n", xfer );
+}
+
+gssize prplcb_xfer_read( PurpleXfer *xfer, guchar **buffer, gssize size )
+{
+	struct prpl_xfer_data *px = xfer->ui_data;
+	
+	fprintf( stderr, "xfer_read %d %d\n", size, px->buf_len );
+
+	if( px->buf )
+	{
+		*buffer = px->buf;
+		px->buf = NULL;
+		
+		px->ft->write_request( px->ft );
+		
+		return px->buf_len;
+	}
+	
+	return 0;
+}
+
+static PurpleXferUiOps bee_xfer_uiops =
+{
+	prplcb_xfer_new,
+	prplcb_xfer_dbg,
+	prplcb_xfer_dbg,
+	prplcb_xfer_dbg,
+	prplcb_xfer_dbg,
+	prplcb_xfer_dbg,
+	prplcb_xfer_write,
+	prplcb_xfer_read,
+	prplcb_xfer_dbg,
+};
+
+static gboolean prplcb_xfer_send_cb( gpointer data, gint fd, b_input_condition cond );
+
+void purple_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *handle )
+{
+	PurpleAccount *pa = ic->proto_data;
+	struct prpl_xfer_data *px;
+	
+	/* xfer_new() will pick up this variable. It's a hack but we're not
+	   multi-threaded anyway. */
+	next_ft = ft;
+	serv_send_file( purple_account_get_connection( pa ), handle, ft->file_name );
+	
+	ft->write = prpl_xfer_write;
+	
+	px = ft->data;
+	imcb_file_recv_start( ft );
+	
+	px->ready_timer = b_timeout_add( 100, prplcb_xfer_send_cb, px );
+}
+
+static gboolean prplcb_xfer_send_cb( gpointer data, gint fd, b_input_condition cond )
+{
+	struct prpl_xfer_data *px = data;
+	
+	if( px->ft->status & FT_STATUS_TRANSFERRING )
+	{
+		fprintf( stderr, "The ft, it is ready...\n" );
+		px->ft->write_request( px->ft );
+		
+		return FALSE;
+	}
+	
+	return TRUE;
+}
+
+static void purple_ui_init()
+{
+	purple_blist_set_ui_ops( &bee_blist_uiops );
+	purple_connections_set_ui_ops( &bee_conn_uiops );
+	purple_conversations_set_ui_ops( &bee_conv_uiops );
+	purple_request_set_ui_ops( &bee_request_uiops );
+	purple_notify_set_ui_ops( &bee_notify_uiops );
+	purple_xfers_set_ui_ops( &bee_xfer_uiops );
+	//purple_debug_set_ui_ops( &bee_debug_uiops );
+}
+
+void purple_initmodule()
+{
+	struct prpl funcs;
+	GList *prots;
+	GString *help;
+	
+	if( B_EV_IO_READ != PURPLE_INPUT_READ ||
+	    B_EV_IO_WRITE != PURPLE_INPUT_WRITE )
+	{
+		/* FIXME FIXME FIXME FIXME FIXME :-) */
+		exit( 1 );
+	}
+	
+	purple_util_set_user_dir("/tmp");
+	purple_debug_set_enabled(FALSE);
+	purple_core_set_ui_ops(&bee_core_uiops);
+	purple_eventloop_set_ui_ops(&glib_eventloops);
+	if( !purple_core_init( "BitlBee") )
+	{
+		/* Initializing the core failed. Terminate. */
+		fprintf( stderr, "libpurple initialization failed.\n" );
+		abort();
+	}
+	
+	/* This seems like stateful shit we don't want... */
+	purple_set_blist(purple_blist_new());
+	purple_blist_load();
+	
+	/* Meh? */
+	purple_prefs_load();
+	
+	memset( &funcs, 0, sizeof( funcs ) );
+	funcs.login = purple_login;
+	funcs.init = purple_init;
+	funcs.logout = purple_logout;
+	funcs.buddy_msg = purple_buddy_msg;
+	funcs.away_states = purple_away_states;
+	funcs.set_away = purple_set_away;
+	funcs.add_buddy = purple_add_buddy;
+	funcs.remove_buddy = purple_remove_buddy;
+	funcs.keepalive = purple_keepalive;
+	funcs.send_typing = purple_send_typing;
+	funcs.handle_cmp = g_strcasecmp;
+	/* TODO(wilmer): Set this one only for protocols that support it? */
+	funcs.transfer_request = purple_transfer_request;
+	
+	help = g_string_new("BitlBee libpurple module supports the following IM protocols:\n");
+	
+	/* Add a protocol entry to BitlBee's structures for every protocol
+	   supported by this libpurple instance. */	
+	for( prots = purple_plugins_get_protocols(); prots; prots = prots->next )
+	{
+		PurplePlugin *prot = prots->data;
+		struct prpl *ret;
+		
+		ret = g_memdup( &funcs, sizeof( funcs ) );
+		ret->name = ret->data = prot->info->id;
+		if( strncmp( ret->name, "prpl-", 5 ) == 0 )
+			ret->name += 5;
+		register_protocol( ret );
+		
+		g_string_append_printf( help, "\n* %s (%s)", ret->name, prot->info->name );
+		
+		/* libpurple doesn't define a protocol called OSCAR, but we
+		   need it to be compatible with normal BitlBee. */
+		if( g_strcasecmp( prot->info->id, "prpl-aim" ) == 0 )
+		{
+			ret = g_memdup( &funcs, sizeof( funcs ) );
+			ret->name = "oscar";
+			ret->data = prot->info->id;
+			register_protocol( ret );
+		}
+	}
+	
+	g_string_append( help, "\n\nFor used protocols, more information about available "
+	                 "settings can be found using \x02help purple <protocol name>\x02" );
+	
+	/* Add a simple dynamically-generated help item listing all
+	   the supported protocols. */
+	help_add_mem( &global.help, "purple", help->str );
+	g_string_free( help, TRUE );
+}
Index: protocols/yahoo/yahoo.c
===================================================================
--- protocols/yahoo/yahoo.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ protocols/yahoo/yahoo.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -681,5 +681,5 @@
 		
 		inp->d = d;
-		d->tag = inp->h = b_input_add( fd, GAIM_INPUT_READ, (b_event_handler) byahoo_read_ready_callback, (gpointer) d );
+		d->tag = inp->h = b_input_add( fd, B_EV_IO_READ, (b_event_handler) byahoo_read_ready_callback, (gpointer) d );
 	}
 	else if( cond == YAHOO_INPUT_WRITE )
@@ -692,5 +692,5 @@
 		
 		inp->d = d;
-		d->tag = inp->h = b_input_add( fd, GAIM_INPUT_WRITE, (b_event_handler) byahoo_write_ready_callback, (gpointer) d );
+		d->tag = inp->h = b_input_add( fd, B_EV_IO_WRITE, (b_event_handler) byahoo_write_ready_callback, (gpointer) d );
 	}
 	else
Index: root_commands.c
===================================================================
--- root_commands.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ root_commands.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -1154,4 +1154,66 @@
 	{
 		irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "chat", cmd[1] );
+	}
+}
+
+static void cmd_transfer( irc_t *irc, char **cmd )
+{
+	GSList *files = irc->file_transfers;
+	enum { LIST, REJECT, CANCEL };
+	int subcmd = LIST;
+	int fid;
+
+	if( !files )
+	{
+		irc_usermsg( irc, "No pending transfers" );
+		return;
+	}
+
+	if( cmd[1] && ( strcmp( cmd[1], "reject" ) == 0 ) )
+	{
+		subcmd = REJECT;
+	}
+	else if( cmd[1] && ( strcmp( cmd[1], "cancel" ) == 0 ) && 
+		 cmd[2] && ( sscanf( cmd[2], "%d", &fid ) == 1 ) )
+	{
+		subcmd = CANCEL;
+	}
+
+	for( ; files; files = g_slist_next( files ) )
+	{
+		file_transfer_t *file = files->data;
+		
+		switch( subcmd ) {
+		case LIST:
+			if ( file->status == FT_STATUS_LISTENING )
+				irc_usermsg( irc, 
+					"Pending file(id %d): %s (Listening...)", file->local_id, file->file_name);
+			else 
+			{
+				int kb_per_s = 0;
+				time_t diff = time( NULL ) - file->started ? : 1;
+				if ( ( file->started > 0 ) && ( file->bytes_transferred > 0 ) )
+					kb_per_s = file->bytes_transferred / 1024 / diff;
+					
+				irc_usermsg( irc, 
+					"Pending file(id %d): %s (%10zd/%zd kb, %d kb/s)", file->local_id, file->file_name, 
+					file->bytes_transferred/1024, file->file_size/1024, kb_per_s);
+			}
+			break;
+		case REJECT:
+			if( file->status == FT_STATUS_LISTENING )
+			{
+				irc_usermsg( irc, "Rejecting file transfer for %s", file->file_name );
+				imcb_file_canceled( file, "Denied by user" );
+			}
+			break;
+		case CANCEL:
+			if( file->local_id == fid )
+			{
+				irc_usermsg( irc, "Canceling file transfer for %s", file->file_name );
+				imcb_file_canceled( file, "Canceled by user" );
+			}
+			break;
+		}
 	}
 }
@@ -1178,4 +1240,5 @@
 	{ "join_chat",      2, cmd_join_chat,      0 },
 	{ "chat",           1, cmd_chat,           0 },
+	{ "transfer",       0, cmd_transfer,       0 },
 	{ NULL }
 };
Index: set.c
===================================================================
--- set.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ set.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -29,5 +29,5 @@
 char *SET_INVALID = "nee";
 
-set_t *set_add( set_t **head, char *key, char *def, set_eval eval, void *data )
+set_t *set_add( set_t **head, const char *key, const char *def, set_eval eval, void *data )
 {
 	set_t *s = set_find( head, key );
@@ -63,5 +63,5 @@
 }
 
-set_t *set_find( set_t **head, char *key )
+set_t *set_find( set_t **head, const char *key )
 {
 	set_t *s = *head;
@@ -77,5 +77,5 @@
 }
 
-char *set_getstr( set_t **head, char *key )
+char *set_getstr( set_t **head, const char *key )
 {
 	set_t *s = set_find( head, key );
@@ -87,5 +87,5 @@
 }
 
-int set_getint( set_t **head, char *key )
+int set_getint( set_t **head, const char *key )
 {
 	char *s = set_getstr( head, key );
@@ -101,5 +101,5 @@
 }
 
-int set_getbool( set_t **head, char *key )
+int set_getbool( set_t **head, const char *key )
 {
 	char *s = set_getstr( head, key );
@@ -111,5 +111,5 @@
 }
 
-int set_setstr( set_t **head, char *key, char *value )
+int set_setstr( set_t **head, const char *key, char *value )
 {
 	set_t *s = set_find( head, key );
@@ -150,5 +150,5 @@
 }
 
-int set_setint( set_t **head, char *key, int value )
+int set_setint( set_t **head, const char *key, int value )
 {
 	char s[24];	/* Not quite 128-bit clean eh? ;-) */
@@ -158,5 +158,5 @@
 }
 
-void set_del( set_t **head, char *key )
+void set_del( set_t **head, const char *key )
 {
 	set_t *s = *head, *t = NULL;
@@ -182,5 +182,5 @@
 }
 
-int set_reset( set_t **head, char *key )
+int set_reset( set_t **head, const char *key )
 {
 	set_t *s;
@@ -211,4 +211,19 @@
 {
 	return is_bool( value ) ? value : SET_INVALID;
+}
+
+char *set_eval_list( set_t *set, char *value )
+{
+	GSList *options = set->eval_data, *opt;
+	
+	for( opt = options; opt; opt = opt->next )
+		if( strcmp( value, opt->data ) == 0 )
+			return value;
+	
+	/* TODO: It'd be nice to show the user a list of allowed values,
+	         but we don't have enough context here to do that. May
+	         want to fix that. */
+	
+	return NULL;
 }
 
Index: set.h
===================================================================
--- set.h	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ set.h	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -69,32 +69,36 @@
 	   set_setstr() should be able to free() the returned string! */
 	set_eval eval;
+	void *eval_data;
 	struct set *next;
 } set_t;
 
 /* Should be pretty clear. */
-set_t *set_add( set_t **head, char *key, char *def, set_eval eval, void *data );
+set_t *set_add( set_t **head, const char *key, const char *def, set_eval eval, void *data );
 
 /* Returns the raw set_t. Might be useful sometimes. */
-set_t *set_find( set_t **head, char *key );
+set_t *set_find( set_t **head, const char *key );
 
 /* Returns a pointer to the string value of this setting. Don't modify the
    returned string, and don't free() it! */
-G_MODULE_EXPORT char *set_getstr( set_t **head, char *key );
+G_MODULE_EXPORT char *set_getstr( set_t **head, const char *key );
 
 /* Get an integer. In previous versions set_getint() was also used to read
    boolean values, but this SHOULD be done with set_getbool() now! */
-G_MODULE_EXPORT int set_getint( set_t **head, char *key );
-G_MODULE_EXPORT int set_getbool( set_t **head, char *key );
+G_MODULE_EXPORT int set_getint( set_t **head, const char *key );
+G_MODULE_EXPORT int set_getbool( set_t **head, const char *key );
 
 /* set_setstr() strdup()s the given value, so after using this function
    you can free() it, if you want. */
-int set_setstr( set_t **head, char *key, char *value );
-int set_setint( set_t **head, char *key, int value );
-void set_del( set_t **head, char *key );
-int set_reset( set_t **head, char *key );
+int set_setstr( set_t **head, const char *key, char *value );
+int set_setint( set_t **head, const char *key, int value );
+void set_del( set_t **head, const char *key );
+int set_reset( set_t **head, const char *key );
 
 /* Two very useful generic evaluators. */
 char *set_eval_int( set_t *set, char *value );
 char *set_eval_bool( set_t *set, char *value );
+
+/* Another more complicated one. */
+char *set_eval_list( set_t *set, char *value );
 
 /* Some not very generic evaluators that really shouldn't be here... */
Index: unix.c
===================================================================
--- unix.c	(revision f1b7711f566163ff27a8f13ae3ccc7214a24fe70)
+++ unix.c	(revision ae3dc9996f5a678d6364005cab1517b6324eb67a)
@@ -56,4 +56,5 @@
 	
 	log_init();
+	
 	global.conf_file = g_strdup( CONF_FILE_DEF );
 	global.conf = conf_load( argc, argv );
@@ -62,8 +63,17 @@
 	
 	b_main_init();
-	nogaim_init();
 	
 	srand( time( NULL ) ^ getpid() );
+	
 	global.helpfile = g_strdup( HELP_FILE );
+	if( help_init( &global.help, global.helpfile ) == NULL )
+		log_message( LOGLVL_WARNING, "Error opening helpfile %s.", HELP_FILE );
+
+	global.storage = storage_init( global.conf->primary_storage, global.conf->migrate_storage );
+	if( global.storage == NULL )
+	{
+		log_message( LOGLVL_ERROR, "Unable to load storage backend '%s'", global.conf->primary_storage );
+		return( 1 );
+	}
 	
 	if( global.conf->runmode == RUNMODE_INETD )
@@ -116,11 +126,4 @@
 			setuid( pw->pw_uid );
 		}
-	}
-
-	global.storage = storage_init( global.conf->primary_storage, global.conf->migrate_storage );
-	if( global.storage == NULL )
-	{
-		log_message( LOGLVL_ERROR, "Unable to load storage backend '%s'", global.conf->primary_storage );
-		return( 1 );
 	}
  	
@@ -142,6 +145,4 @@
 	if( !getuid() || !geteuid() )
 		log_message( LOGLVL_WARNING, "BitlBee is running with root privileges. Why?" );
-	if( help_init( &global.help, global.helpfile ) == NULL )
-		log_message( LOGLVL_WARNING, "Error opening helpfile %s.", HELP_FILE );
 	
 	b_main_run();
