Index: protocols/Makefile
===================================================================
--- protocols/Makefile	(revision 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/Makefile	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -8,4 +8,7 @@
 
 -include ../Makefile.settings
+ifdef SRCDIR
+SRCDIR := $(SRCDIR)protocols/
+endif
 
 # [SH] Program variables
@@ -50,5 +53,5 @@
 $(objects): ../Makefile.settings Makefile
 
-$(objects): %.o: %.c
+$(objects): %.o: $(SRCDIR)%.c
 	@echo '*' Compiling $<
 	@$(CC) -c $(CFLAGS) $< -o $@
Index: protocols/jabber/Makefile
===================================================================
--- protocols/jabber/Makefile	(revision 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/jabber/Makefile	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -8,4 +8,7 @@
 
 -include ../../Makefile.settings
+ifdef SRCDIR
+SRCDIR := $(SRCDIR)protocols/jabber/
+endif
 
 # [SH] Program variables
@@ -33,5 +36,5 @@
 $(objects): ../../Makefile.settings Makefile
 
-$(objects): %.o: %.c
+$(objects): %.o: $(SRCDIR)%.c
 	@echo '*' Compiling $<
 	@$(CC) -c $(CFLAGS) $< -o $@
Index: protocols/jabber/io.c
===================================================================
--- protocols/jabber/io.c	(revision 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/jabber/io.c	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -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/s5bytestream.c
===================================================================
--- protocols/jabber/s5bytestream.c	(revision 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/jabber/s5bytestream.c	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -406,5 +406,5 @@
 			bt->phase = BS_PHASE_CONNECTED;
 			
-			bt->tf->watch_out = b_input_add( fd, GAIM_INPUT_WRITE, jabber_bs_recv_handshake, bt );
+			bt->tf->watch_out = b_input_add( fd, B_EV_IO_WRITE, jabber_bs_recv_handshake, bt );
 
 			/* since it takes forever(3mins?) till connect() fails on itself we schedule a timeout */
@@ -433,5 +433,5 @@
 			bt->phase = BS_PHASE_REQUEST;
 
-			bt->tf->watch_in = b_input_add( fd, GAIM_INPUT_READ, jabber_bs_recv_handshake, bt );
+			bt->tf->watch_in = b_input_add( fd, B_EV_IO_READ, jabber_bs_recv_handshake, bt );
 
 			bt->tf->watch_out = 0;
@@ -590,5 +590,5 @@
 
 	tf->ft->data = tf;
-	tf->watch_in = b_input_add( tf->fd, GAIM_INPUT_READ, jabber_bs_recv_read, bt );
+	tf->watch_in = b_input_add( tf->fd, B_EV_IO_READ, jabber_bs_recv_read, bt );
 	tf->ft->write_request = jabber_bs_recv_write_request;
 
@@ -632,5 +632,5 @@
 		if( ( ret == -1 ) && ( errno == EAGAIN ) )
 		{
-			tf->watch_in = b_input_add( tf->fd, GAIM_INPUT_READ, jabber_bs_recv_read, bt );
+			tf->watch_in = b_input_add( tf->fd, B_EV_IO_READ, jabber_bs_recv_read, bt );
 			return FALSE;
 		}
@@ -708,5 +708,5 @@
 		imcb_file_finished( tf->ic, ft );
 	else
-		bt->tf->watch_out = b_input_add( tf->fd, GAIM_INPUT_WRITE, jabber_bs_send_can_write, bt );
+		bt->tf->watch_out = b_input_add( tf->fd, B_EV_IO_WRITE, jabber_bs_send_can_write, bt );
 		
 	return TRUE;
@@ -919,5 +919,5 @@
 				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->tf->watch_in = b_input_add( tf->fd, B_EV_IO_READ, jabber_bs_send_handshake, bt );
 				bt->connect_timeout = b_timeout_add( JABBER_BS_LISTEN_TIMEOUT * 1000, jabber_bs_connect_timeout, bt );
 			} else {
@@ -1056,5 +1056,5 @@
 			bt->phase = BS_PHASE_CONNECTED;
 
-			bt->tf->watch_in = b_input_add( fd, GAIM_INPUT_READ, jabber_bs_send_handshake, bt );
+			bt->tf->watch_in = b_input_add( fd, B_EV_IO_READ, jabber_bs_send_handshake, bt );
 			return FALSE;
 		}
Index: protocols/msn/Makefile
===================================================================
--- protocols/msn/Makefile	(revision 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/msn/Makefile	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -8,4 +8,7 @@
 
 -include ../../Makefile.settings
+ifdef SRCDIR
+SRCDIR := $(SRCDIR)protocols/msn/
+endif
 
 # [SH] Program variables
@@ -33,5 +36,5 @@
 $(objects): ../../Makefile.settings Makefile
 
-$(objects): %.o: %.c
+$(objects): %.o: $(SRCDIR)%.c
 	@echo '*' Compiling $<
 	@$(CC) -c $(CFLAGS) $< -o $@
Index: protocols/msn/invitation.c
===================================================================
--- protocols/msn/invitation.c	(revision 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/msn/invitation.c	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -209,5 +209,5 @@
 	sock_make_nonblocking( fd );
 
-	msn_file->r_event_id = b_input_add( fd, GAIM_INPUT_READ, msn_ftp_read, file );
+	msn_file->r_event_id = b_input_add( fd, B_EV_IO_READ, msn_ftp_read, file );
 
 	return FALSE;
@@ -230,5 +230,5 @@
 	}
 
-	msn_file->r_event_id = b_input_add( msn_file->fd, GAIM_INPUT_READ, msn_ftps_connected, file );
+	msn_file->r_event_id = b_input_add( msn_file->fd, B_EV_IO_READ, msn_ftps_connected, file );
 
 	g_snprintf( buf, sizeof( buf ),
@@ -318,5 +318,5 @@
 	
 	sock_make_nonblocking( msn_file->fd );
-	msn_file->r_event_id = b_input_add( msn_file->fd, GAIM_INPUT_READ, msn_ftp_read, file );
+	msn_file->r_event_id = b_input_add( msn_file->fd, B_EV_IO_READ, msn_ftp_read, file );
 	
 	return FALSE;
@@ -415,5 +415,5 @@
 	    ( 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 );
+			msn_file->w_event_id = b_input_add( msn_file->fd, B_EV_IO_WRITE, msn_ftp_send, file );
 		return TRUE;
 	}
@@ -452,5 +452,5 @@
 		/* 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 );
+			msn_file->w_event_id = b_input_add( msn_file->fd, B_EV_IO_WRITE, msn_ftp_send, file );
 	}
 
@@ -617,5 +617,5 @@
 
 	msn_file->r_event_id = 
-		b_input_add( msn_file->fd, GAIM_INPUT_READ, msn_ftp_read, file );
+		b_input_add( msn_file->fd, B_EV_IO_READ, msn_ftp_read, file );
 
 	return TRUE;
Index: protocols/msn/ns.c
===================================================================
--- protocols/msn/ns.c	(revision 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/msn/ns.c	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -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 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/msn/sb.c	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -327,5 +327,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 );
Index: protocols/nogaim.c
===================================================================
--- protocols/nogaim.c	(revision 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/nogaim.c	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -112,10 +112,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;
 }
@@ -128,4 +131,5 @@
 	extern void jabber_initmodule();
 	extern void twitter_initmodule();
+	extern void purple_initmodule();
 
 #ifdef WITH_MSN
@@ -147,4 +151,8 @@
 #ifdef WITH_TWITTER
 	twitter_initmodule();
+#endif
+
+#ifdef WITH_PURPLE
+	purple_initmodule();
 #endif
 
Index: protocols/nogaim.h
===================================================================
--- protocols/nogaim.h	(revision 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/nogaim.h	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -134,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
@@ -321,4 +322,5 @@
 
 /* Misc. stuff */
+char *set_eval_timezone( set_t *set, char *value );
 char *set_eval_away_devoice( set_t *set, char *value );
 gboolean auto_reconnect( gpointer data, gint fd, b_input_condition cond );
Index: protocols/oscar/Makefile
===================================================================
--- protocols/oscar/Makefile	(revision 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/oscar/Makefile	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -8,4 +8,8 @@
 
 -include ../../Makefile.settings
+ifdef SRCDIR
+SRCDIR := $(SRCDIR)protocols/oscar/
+CFLAGS += -I$(SRCDIR)
+endif
 
 # [SH] Program variables
@@ -33,5 +37,5 @@
 $(objects): ../../Makefile.settings Makefile
 
-$(objects): %.o: %.c
+$(objects): %.o: $(SRCDIR)%.c
 	@echo '*' Compiling $<
 	@$(CC) -c $(CFLAGS) $< -o $@
Index: protocols/oscar/auth.c
===================================================================
--- protocols/oscar/auth.c	(revision 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/oscar/auth.c	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -120,9 +120,10 @@
 	aim_snacid_t snacid;
 	aim_tlvlist_t *tl = NULL;
+	struct im_connection *ic = sess->aux_data;
 	
 	if (!sess || !conn || !sn)
 		return -EINVAL;
 
-	if ((sn[0] >= '0') && (sn[0] <= '9'))
+	if (isdigit(sn[0]) && set_getbool(&ic->acc->set, "old_icq_auth"))
 		return goddamnicq(sess, conn, sn);
 
Index: protocols/oscar/oscar.c
===================================================================
--- protocols/oscar/oscar.c	(revision 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/oscar/oscar.c	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -288,5 +288,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);
@@ -360,5 +360,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);
 	
@@ -372,4 +372,5 @@
 	if (isdigit(acc->user[0])) {
 		set_add(&acc->set, "ignore_auth_requests", "false", set_eval_bool, acc);
+		set_add(&acc->set, "old_icq_auth", "false", set_eval_bool, acc);
 	}
 	
@@ -490,5 +491,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"));
@@ -706,5 +707,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);
 	
@@ -734,5 +735,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);
 	
@@ -770,5 +771,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 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
+++ protocols/purple/Makefile	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -0,0 +1,44 @@
+###########################
+## Makefile for BitlBee  ##
+##                       ##
+## Copyright 2002 Lintux ##
+###########################
+
+### DEFINITIONS
+
+-include ../../Makefile.settings
+ifdef SRCDIR
+SRCDIR := $(SRCDIR)protocols/purple/
+endif
+
+# [SH] Program variables
+objects = ft.o 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: $(SRCDIR)%.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/ft-direct.c
===================================================================
--- protocols/purple/ft-direct.c	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
+++ protocols/purple/ft-direct.c	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -0,0 +1,239 @@
+/***************************************************************************\
+*                                                                           *
+*  BitlBee - An IRC to IM gateway                                           *
+*  libpurple module - File transfer stuff                                   *
+*                                                                           *
+*  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.              *
+*                                                                           *
+\***************************************************************************/
+
+/* This code tries to do direct file transfers, i.e. without caching the file
+   locally on disk first. Since libpurple can only do this since version 2.6.0
+   and even then very unreliably (and not with all IM modules), I'm canning
+   this code for now. */
+
+#include "bitlbee.h"
+
+#include <stdarg.h>
+
+#include <glib.h>
+#include <purple.h>
+
+struct prpl_xfer_data
+{
+	PurpleXfer *xfer;
+	file_transfer_t *ft;
+	gint ready_timer;
+	char *buf;
+	int buf_len;
+};
+
+static file_transfer_t *next_ft;
+
+struct im_connection *purple_ic_by_pa( PurpleAccount *pa );
+
+/* 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 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_progress( PurpleXfer *xfer, double percent )
+{
+	fprintf( stderr, "prplcb_xfer_dbg 0x%p %f\n", xfer, percent );
+}
+
+static void prplcb_xfer_dbg( PurpleXfer *xfer )
+{
+	fprintf( stderr, "prplcb_xfer_dbg 0x%p\n", xfer );
+}
+
+static gssize prplcb_xfer_write( PurpleXfer *xfer, const guchar *buffer, gssize size )
+{
+	struct prpl_xfer_data *px = xfer->ui_data;
+	gboolean st;
+	
+	fprintf( stderr, "xfer_write %d %d\n", size, px->buf_len );
+	
+	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;
+}
+
+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;
+}
+
+PurpleXferUiOps bee_xfer_uiops =
+{
+	prplcb_xfer_new,
+	prplcb_xfer_dbg,
+	prplcb_xfer_dbg,
+	prplcb_xfer_progress,
+	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;
+}
Index: protocols/purple/ft.c
===================================================================
--- protocols/purple/ft.c	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
+++ protocols/purple/ft.c	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -0,0 +1,355 @@
+/***************************************************************************\
+*                                                                           *
+*  BitlBee - An IRC to IM gateway                                           *
+*  libpurple module - File transfer stuff                                   *
+*                                                                           *
+*  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.              *
+*                                                                           *
+\***************************************************************************/
+
+/* Do file transfers via disk for now, since libpurple was really designed
+   for straight-to/from disk fts and is only just learning how to pass the
+   file contents the the UI instead (2.6.0 and higher it seems, and with
+   varying levels of success). */
+
+#include "bitlbee.h"
+
+#include <stdarg.h>
+
+#include <glib.h>
+#include <purple.h>
+
+struct prpl_xfer_data
+{
+	PurpleXfer *xfer;
+	file_transfer_t *ft;
+	struct im_connection *ic;
+	int fd;
+	char *fn, *handle;
+	gboolean ui_wants_data;
+};
+
+static file_transfer_t *next_ft;
+
+struct im_connection *purple_ic_by_pa( PurpleAccount *pa );
+static gboolean prplcb_xfer_new_send_cb( gpointer data, gint fd, b_input_condition cond );
+static gboolean prpl_xfer_write_request( struct file_transfer *ft );
+
+
+/* Receiving files (IM->UI): */
+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 void prplcb_xfer_new( PurpleXfer *xfer )
+{
+	if( purple_xfer_get_type( xfer ) == PURPLE_XFER_RECEIVE )
+	{
+		struct prpl_xfer_data *px = g_new0( struct prpl_xfer_data, 1 );
+		
+		xfer->ui_data = px;
+		px->xfer = xfer;
+		px->fn = mktemp( g_strdup( "/tmp/bitlbee-purple-ft.XXXXXX" ) );
+		px->fd = -1;
+		px->ic = purple_ic_by_pa( xfer->account );
+		
+		purple_xfer_set_local_filename( xfer, px->fn );
+		
+		/* 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 file_transfer *ft = next_ft;
+		struct prpl_xfer_data *px = ft->data;
+		
+		xfer->ui_data = px;
+		px->xfer = xfer;
+		
+		next_ft = NULL;
+	}
+}
+
+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 = xfer->ui_data;
+	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->ft->accept = prpl_xfer_accept;
+	px->ft->canceled = prpl_xfer_canceled;
+	px->ft->write_request = prpl_xfer_write_request;
+	
+	return FALSE;
+}
+
+gboolean try_write_to_ui( gpointer data, gint fd, b_input_condition cond )
+{
+	struct file_transfer *ft = data;
+	struct prpl_xfer_data *px = ft->data;
+	struct stat fs;
+	off_t tx_bytes;
+	
+	/* If we don't have the file opened yet, there's no data so wait. */
+	if( px->fd < 0 || !px->ui_wants_data )
+		return FALSE;
+	
+	tx_bytes = lseek( px->fd, 0, SEEK_CUR );
+	fstat( px->fd, &fs );
+	
+	if( fs.st_size > tx_bytes )
+	{
+		char buf[1024];
+		size_t n = MIN( fs.st_size - tx_bytes, sizeof( buf ) );
+		
+		if( read( px->fd, buf, n ) == n && ft->write( ft, buf, n ) )
+		{
+			px->ui_wants_data = FALSE;
+		}
+		else
+		{
+			purple_xfer_cancel_local( px->xfer );
+			imcb_file_canceled( px->ic, ft, "Read error" );
+		}
+	}
+	
+	if( lseek( px->fd, 0, SEEK_CUR ) == px->xfer->size )
+	{
+		/*purple_xfer_end( px->xfer );*/
+		imcb_file_finished( px->ic, ft );
+	}
+	
+	return FALSE;
+}
+
+/* UI calls this when its buffer is empty and wants more data to send to the user. */
+static gboolean prpl_xfer_write_request( struct file_transfer *ft )
+{
+	struct prpl_xfer_data *px = ft->data;
+	
+	px->ui_wants_data = TRUE;
+	try_write_to_ui( ft, 0, 0 );
+	
+	return FALSE;
+}
+
+
+/* Generic (IM<>UI): */
+static void prplcb_xfer_destroy( PurpleXfer *xfer )
+{
+	struct prpl_xfer_data *px = xfer->ui_data;
+	
+	g_free( px->fn );
+	g_free( px->handle );
+	if( px->fd >= 0 )
+		close( px->fd );
+	g_free( px );
+}
+
+static void prplcb_xfer_progress( PurpleXfer *xfer, double percent )
+{
+	struct prpl_xfer_data *px = xfer->ui_data;
+	
+	if( px == NULL )
+		return;
+	
+	if( purple_xfer_get_type( xfer ) == PURPLE_XFER_SEND )
+	{
+		if( *px->fn )
+		{
+			char *slash;
+			
+			unlink( px->fn );
+			if( ( slash = strrchr( px->fn, '/' ) ) )
+			{
+				*slash = '\0';
+				rmdir( px->fn );
+			}
+			*px->fn = '\0';
+		}
+		
+		return;
+	}
+	
+	if( px->fd == -1 && percent > 0 )
+	{
+		/* Weeeeeeeee, we're getting data! That means the file exists
+		   by now so open it and start sending to the UI. */
+		px->fd = open( px->fn, O_RDONLY );
+		
+		/* Unlink it now, because we don't need it after this. */
+		unlink( px->fn );
+	}
+	
+	if( percent < 1 )
+		try_write_to_ui( px->ft, 0, 0 );
+	else
+		/* Another nice problem: If we have the whole file, it only
+		   gets closed when we return. Problem: There may still be
+		   stuff buffered and not written, we'll only see it after
+		   the caller close()s the file. So poll the file after that. */
+		b_timeout_add( 0, try_write_to_ui, px->ft );
+}
+
+static void prplcb_xfer_cancel_remote( PurpleXfer *xfer )
+{
+	struct prpl_xfer_data *px = xfer->ui_data;
+	
+	if( px->ft )
+		imcb_file_canceled( px->ic, px->ft, "Canceled by remote end" );
+	else
+		/* px->ft == NULL for sends, because of the two stages. :-/ */
+		imcb_error( px->ic, "File transfer cancelled by remote end" );
+}
+
+static void prplcb_xfer_dbg( PurpleXfer *xfer )
+{
+	fprintf( stderr, "prplcb_xfer_dbg 0x%p\n", xfer );
+}
+
+
+/* Sending files (UI->IM): */
+static gboolean prpl_xfer_write( struct file_transfer *ft, char *buffer, unsigned int len );
+static gboolean purple_transfer_request_cb( gpointer data, gint fd, b_input_condition cond );
+
+void purple_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *handle )
+{
+	struct prpl_xfer_data *px = g_new0( struct prpl_xfer_data, 1 );
+	char *dir, *basename;
+	
+	ft->data = px;
+	px->ft = ft;
+	
+	dir = g_strdup( "/tmp/bitlbee-purple-ft.XXXXXX" );
+	if( !mkdtemp( dir ) )
+	{
+		imcb_error( ic, "Could not create temporary file for file transfer" );
+		g_free( px );
+		g_free( dir );
+		return;
+	}
+	
+	if( ( basename = strrchr( ft->file_name, '/' ) ) )
+		basename++;
+	else
+		basename = ft->file_name;
+	px->fn = g_strdup_printf( "%s/%s", dir, basename );
+	px->fd = open( px->fn, O_WRONLY | O_CREAT, 0600 );
+	g_free( dir );
+	
+	if( px->fd < 0 )
+	{
+		imcb_error( ic, "Could not create temporary file for file transfer" );
+		g_free( px );
+		g_free( px->fn );
+		return;
+	}
+	
+	px->ic = ic;
+	px->handle = g_strdup( handle );
+	
+	imcb_log( ic, "Due to libpurple limitations, the file has to be cached locally before proceeding with the actual file transfer. Please wait..." );
+	
+	b_timeout_add( 0, purple_transfer_request_cb, ft );
+}
+
+static void purple_transfer_forward( struct file_transfer *ft )
+{
+	struct prpl_xfer_data *px = ft->data;
+	PurpleAccount *pa = px->ic->proto_data;
+	
+	/* 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 ), px->handle, px->fn );
+}
+
+static gboolean purple_transfer_request_cb( gpointer data, gint fd, b_input_condition cond )
+{
+	file_transfer_t *ft = data;
+	struct prpl_xfer_data *px = ft->data;
+	
+	if( ft->write == NULL )
+	{
+		ft->write = prpl_xfer_write;
+		imcb_file_recv_start( px->ic, ft );
+	}
+	
+	ft->write_request( ft );
+	
+	return FALSE;
+}
+
+static gboolean prpl_xfer_write( struct file_transfer *ft, char *buffer, unsigned int len )
+{
+	struct prpl_xfer_data *px = ft->data;
+	
+	if( write( px->fd, buffer, len ) != len )
+	{
+		imcb_file_canceled( px->ic, ft, "Error while writing temporary file" );
+		return FALSE;
+	}
+	
+	if( lseek( px->fd, 0, SEEK_CUR ) >= ft->file_size )
+	{
+		close( px->fd );
+		px->fd = -1;
+		
+		purple_transfer_forward( ft );
+		imcb_file_finished( px->ic, ft );
+		px->ft = NULL;
+	}
+	else
+		b_timeout_add( 0, purple_transfer_request_cb, ft );
+	
+	return TRUE;
+}
+
+
+
+PurpleXferUiOps bee_xfer_uiops =
+{
+	prplcb_xfer_new,
+	prplcb_xfer_destroy,
+	NULL, /* prplcb_xfer_add, */
+	prplcb_xfer_progress,
+	prplcb_xfer_dbg,
+	prplcb_xfer_cancel_remote,
+	NULL,
+	NULL,
+	prplcb_xfer_dbg,
+};
Index: protocols/purple/purple.c
===================================================================
--- protocols/purple/purple.c	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
+++ protocols/purple/purple.c	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -0,0 +1,1148 @@
+/***************************************************************************\
+*                                                                           *
+*  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 bee_t *local_bee;
+
+static char *set_eval_display_name( set_t *set, char *value );
+
+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 gboolean purple_menu_cmp( const char *a, const char *b )
+{
+	while( *a && *b )
+	{
+		while( *a == '_' ) a ++;
+		while( *b == '_' ) b ++;
+		if( tolower( *a ) != tolower( *b ) )
+			return FALSE;
+		
+		a ++;
+		b ++;
+	}
+	
+	return ( *a == '\0' && *b == '\0' );
+}
+
+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->value );
+				/* TODO: kv->value is not a char*, WTF? */
+				if( strcmp( kv->value, kv->key ) != 0 )
+					g_string_append_printf( help, "%s (%s), ", (char*) kv->value, kv->key );
+				else
+					g_string_append_printf( help, "%s, ", (char*) kv->value );
+			}
+			g_string_truncate( help, help->len - 2 );
+			eval = set_eval_list;
+			eval_data = opts;
+			
+			break;
+			
+		default:
+			/** No way to talk to the user right now, invent one when
+			this becomes important.
+			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 );
+	
+	s = set_add( &acc->set, "display_name", NULL, set_eval_display_name, acc );
+	s->flags |= ACC_SET_ONLINE_ONLY;
+	
+	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_bee != NULL && local_bee != acc->bee )
+	{
+		imcb_error( ic,  "Daemon mode detected. Do *not* try to use libpurple in daemon mode! "
+		                 "Please use inetd or ForkDaemon mode instead." );
+		imc_logout( ic, FALSE );
+		return;
+	}
+	local_bee = acc->bee;
+	
+	/* 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 char *set_eval_display_name( set_t *set, char *value )
+{
+	account_t *acc = set->data;
+	struct im_connection *ic = acc->ic;
+	
+	return NULL;
+}
+
+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_add_permit( struct im_connection *ic, char *who )
+{
+	PurpleAccount *pa = ic->proto_data;
+	
+	purple_privacy_permit_add( pa, who, FALSE );
+}
+
+static void purple_add_deny( struct im_connection *ic, char *who )
+{
+	PurpleAccount *pa = ic->proto_data;
+	
+	purple_privacy_deny_add( pa, who, FALSE );
+}
+
+static void purple_rem_permit( struct im_connection *ic, char *who )
+{
+	PurpleAccount *pa = ic->proto_data;
+	
+	purple_privacy_permit_remove( pa, who, FALSE );
+}
+
+static void purple_rem_deny( struct im_connection *ic, char *who )
+{
+	PurpleAccount *pa = ic->proto_data;
+	
+	purple_privacy_deny_remove( pa, who, FALSE );
+}
+
+static void purple_get_info( struct im_connection *ic, char *who )
+{
+	serv_get_info( purple_account_get_connection( ic->proto_data ), who );
+}
+
+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;
+	}
+}
+
+static void purple_chat_msg( struct groupchat *gc, char *message, int flags )
+{
+	PurpleConversation *pc = gc->data;
+	
+	purple_conv_chat_send( purple_conversation_get_chat_data( pc ), message );
+}
+
+struct groupchat *purple_chat_with( struct im_connection *ic, char *who )
+{
+	/* No, "of course" this won't work this way. Or in fact, it almost
+	   does, but it only lets you send msgs to it, you won't receive
+	   any. Instead, we have to click the virtual menu item.
+	PurpleAccount *pa = ic->proto_data;
+	PurpleConversation *pc;
+	PurpleConvChat *pcc;
+	struct groupchat *gc;
+	
+	gc = imcb_chat_new( ic, "BitlBee-libpurple groupchat" );
+	gc->data = pc = purple_conversation_new( PURPLE_CONV_TYPE_CHAT, pa, "BitlBee-libpurple groupchat" );
+	pc->ui_data = gc;
+	
+	pcc = PURPLE_CONV_CHAT( pc );
+	purple_conv_chat_add_user( pcc, ic->acc->user, "", 0, TRUE );
+	purple_conv_chat_invite_user( pcc, who, "Please join my chat", FALSE );
+	//purple_conv_chat_add_user( pcc, who, "", 0, TRUE );
+	*/
+	
+	/* There went my nice afternoon. :-( */
+	
+	PurpleAccount *pa = ic->proto_data;
+	PurplePlugin *prpl = purple_plugins_find_with_id( pa->protocol_id );
+	PurplePluginProtocolInfo *pi = prpl->info->extra_info;
+	PurpleBuddy *pb = purple_find_buddy( (PurpleAccount*) ic->proto_data, who );
+	PurpleMenuAction *mi;
+	GList *menu;
+	void (*callback)(PurpleBlistNode *, gpointer); /* FFFFFFFFFFFFFUUUUUUUUUUUUUU */
+	
+	if( !pb || !pi || !pi->blist_node_menu )
+		return NULL;
+	
+	menu = pi->blist_node_menu( &pb->node );
+	while( menu )
+	{
+		mi = menu->data;
+		if( purple_menu_cmp( mi->label, "initiate chat" ) ||
+		    purple_menu_cmp( mi->label, "initiate conference" ) )
+			break;
+		menu = menu->next;
+	}
+	
+	if( menu == NULL )
+		return NULL;
+	
+	/* Call the fucker. */
+	callback = (void*) mi->callback;
+	callback( &pb->node, menu->data );
+	
+	return NULL;
+}
+
+void purple_chat_invite( struct groupchat *gc, char *who, char *message )
+{
+	PurpleConversation *pc = gc->data;
+	PurpleConvChat *pcc = PURPLE_CONV_CHAT( pc );
+	
+	serv_chat_invite( purple_account_get_connection( gc->ic->proto_data ),
+	                  purple_conv_chat_get_id( pcc ), 
+	                  message && *message ? message : "Please join my chat",
+	                  who );
+}
+
+void purple_chat_leave( struct groupchat *gc )
+{
+	PurpleConversation *pc = gc->data;
+	
+	purple_conversation_destroy( pc );
+}
+
+struct groupchat *purple_chat_join( struct im_connection *ic, const char *room, const char *nick, const char *password )
+{
+	PurpleAccount *pa = ic->proto_data;
+	PurplePlugin *prpl = purple_plugins_find_with_id( pa->protocol_id );
+	PurplePluginProtocolInfo *pi = prpl->info->extra_info;
+	GHashTable *chat_hash;
+	PurpleConversation *conv;
+	GList *info, *l;
+	
+	if( !pi->chat_info || !pi->chat_info_defaults ||
+	    !( info = pi->chat_info( purple_account_get_connection( pa ) ) ) )
+	{
+		imcb_error( ic, "Joining chatrooms not supported by this protocol" );
+		return NULL;
+	}
+	
+	if( ( conv = purple_find_conversation_with_account( PURPLE_CONV_TYPE_CHAT, room, pa ) ) )
+		purple_conversation_destroy( conv );
+	
+	chat_hash = pi->chat_info_defaults( purple_account_get_connection( pa ), room );
+	
+	for( l = info; l; l = l->next )
+	{
+		struct proto_chat_entry *pce = l->data;
+		
+		if( strcmp( pce->identifier, "handle" ) == 0 )
+			g_hash_table_replace( chat_hash, "handle", g_strdup( nick ) );
+		else if( strcmp( pce->identifier, "password" ) == 0 )
+			g_hash_table_replace( chat_hash, "password", g_strdup( password ) );
+		else if( strcmp( pce->identifier, "passwd" ) == 0 )
+			g_hash_table_replace( chat_hash, "passwd", g_strdup( password ) );
+	}
+	
+	serv_join_chat( purple_account_get_connection( pa ), chat_hash );
+	
+	return NULL;
+}
+
+void purple_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *handle );
+
+static void purple_ui_init();
+
+GHashTable *prplcb_ui_info()
+{
+	static GHashTable *ret;
+	
+	if( ret == NULL )
+	{
+		ret = g_hash_table_new(g_str_hash, g_str_equal);
+		g_hash_table_insert( ret, "name", "BitlBee" );
+		g_hash_table_insert( ret, "version", BITLBEE_VERSION );
+	}
+	
+	return ret;
+}
+
+static PurpleCoreUiOps bee_core_uiops = 
+{
+	NULL,
+	NULL,
+	purple_ui_init,
+	NULL,
+	prplcb_ui_info,
+};
+
+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 );
+	const char *dn;
+	set_t *s;
+	
+	imcb_connected( ic );
+	
+	if( ( dn = purple_connection_get_display_name( gc ) ) &&
+	    ( s = set_find( &ic->acc->set, "display_name" ) ) )
+	{
+		g_free( s->value );
+		s->value = g_strdup( dn );
+	}
+	
+	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, !gc->wants_to_die );
+	}
+}
+
+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,
+};
+
+void prplcb_conv_new( PurpleConversation *conv )
+{
+	if( conv->type == PURPLE_CONV_TYPE_CHAT )
+	{
+		struct im_connection *ic = purple_ic_by_pa( conv->account );
+		struct groupchat *gc;
+		
+		gc = imcb_chat_new( ic, conv->name );
+		conv->ui_data = gc;
+		gc->data = conv;
+		
+		/* libpurple brokenness: Whatever. Show that we join right away,
+		   there's no clear "This is you!" signaling in _add_users so
+		   don't even try. */
+		imcb_chat_add_buddy( gc, gc->ic->acc->user );
+	}
+}
+
+void prplcb_conv_free( PurpleConversation *conv )
+{
+	struct groupchat *gc = conv->ui_data;
+	
+	imcb_chat_free( gc );
+}
+
+void prplcb_conv_add_users( PurpleConversation *conv, GList *cbuddies, gboolean new_arrivals )
+{
+	struct groupchat *gc = conv->ui_data;
+	GList *b;
+	
+	for( b = cbuddies; b; b = b->next )
+	{
+		PurpleConvChatBuddy *pcb = b->data;
+		
+		imcb_chat_add_buddy( gc, pcb->name );
+	}
+}
+
+void prplcb_conv_del_users( PurpleConversation *conv, GList *cbuddies )
+{
+	struct groupchat *gc = conv->ui_data;
+	GList *b;
+	
+	for( b = cbuddies; b; b = b->next )
+		imcb_chat_remove_buddy( gc, b->data, "" );
+}
+
+void prplcb_conv_chat_msg( PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, time_t mtime )
+{
+	struct groupchat *gc = conv->ui_data;
+	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_chat_msg( gc, who, (char*) message, 0, mtime );
+}
+
+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 = 
+{
+	prplcb_conv_new,           /* create_conversation  */
+	prplcb_conv_free,          /* destroy_conversation */
+	prplcb_conv_chat_msg,      /* write_chat           */
+	prplcb_conv_im,            /* write_im             */
+	NULL,                      /* write_conv           */
+	prplcb_conv_add_users,     /* chat_add_users       */
+	NULL,                      /* chat_rename_user     */
+	prplcb_conv_del_users,     /* 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;
+	
+	/* TODO: IRC stuff here :-( */
+	q = g_strdup_printf( "Request: %s\n\n%s\n\n%s", title, primary, secondary );
+	pqad->bee_data = query_add( local_bee->ui_data, 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_privacy_permit_added( PurpleAccount *account, const char *name )
+{
+	struct im_connection *ic = purple_ic_by_pa( account );
+	
+	if( !g_slist_find_custom( ic->permit, name, (GCompareFunc) ic->acc->prpl->handle_cmp ) )
+		ic->permit = g_slist_prepend( ic->permit, g_strdup( name ) );
+}
+
+static void prplcb_privacy_permit_removed( PurpleAccount *account, const char *name )
+{
+	struct im_connection *ic = purple_ic_by_pa( account );
+	void *n;
+	
+	n = g_slist_find_custom( ic->permit, name, (GCompareFunc) ic->acc->prpl->handle_cmp );
+	ic->permit = g_slist_remove( ic->permit, n );
+}
+
+static void prplcb_privacy_deny_added( PurpleAccount *account, const char *name )
+{
+	struct im_connection *ic = purple_ic_by_pa( account );
+	
+	if( !g_slist_find_custom( ic->deny, name, (GCompareFunc) ic->acc->prpl->handle_cmp ) )
+		ic->deny = g_slist_prepend( ic->deny, g_strdup( name ) );
+}
+
+static void prplcb_privacy_deny_removed( PurpleAccount *account, const char *name )
+{
+	struct im_connection *ic = purple_ic_by_pa( account );
+	void *n;
+	
+	n = g_slist_find_custom( ic->deny, name, (GCompareFunc) ic->acc->prpl->handle_cmp );
+	ic->deny = g_slist_remove( ic->deny, n );
+}
+
+static PurplePrivacyUiOps bee_privacy_uiops =
+{
+	prplcb_privacy_permit_added,
+	prplcb_privacy_permit_removed,
+	prplcb_privacy_deny_added,
+	prplcb_privacy_deny_removed,
+};
+
+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 void *prplcb_notify_userinfo( PurpleConnection *gc, const char *who, PurpleNotifyUserInfo *user_info )
+{
+	struct im_connection *ic = purple_ic_by_gc( gc );
+	GString *info = g_string_new( "" );
+	GList *l = purple_notify_user_info_get_entries( user_info );
+	char *key;
+	const char *value;
+	int n;
+	
+	while( l )
+	{
+		PurpleNotifyUserInfoEntry *e = l->data;
+		
+		switch( purple_notify_user_info_entry_get_type( e ) )
+		{
+		case PURPLE_NOTIFY_USER_INFO_ENTRY_PAIR:
+		case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_HEADER:
+			key = g_strdup( purple_notify_user_info_entry_get_label( e ) );
+			value = purple_notify_user_info_entry_get_value( e );
+			
+			if( key )
+			{
+				strip_html( key );
+				g_string_append_printf( info, "%s: ", key );
+				
+				if( value )
+				{
+					n = strlen( value ) - 1;
+					while( isspace( value[n] ) )
+						n --;
+					g_string_append_len( info, value, n + 1 );
+				}
+				g_string_append_c( info, '\n' );
+				g_free( key );
+			}
+			
+			break;
+		case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_BREAK:
+			g_string_append( info, "------------------------\n" );
+			break;
+		}
+		
+		l = l->next;
+	}
+	
+	imcb_log( ic, "User %s info:\n%s", who, info->str );
+	g_string_free( info, TRUE );
+	
+	return NULL;
+}
+
+static PurpleNotifyUiOps bee_notify_uiops =
+{
+        NULL,
+        prplcb_notify_email,
+        NULL,
+        NULL,
+        NULL,
+        NULL,
+        prplcb_notify_userinfo,
+};
+
+extern PurpleXferUiOps bee_xfer_uiops;
+
+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_privacy_set_ui_ops( &bee_privacy_uiops );
+	
+	if( getenv( "BITLBEE_DEBUG" ) )
+		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.add_permit = purple_add_permit;
+	funcs.add_deny = purple_add_deny;
+	funcs.rem_permit = purple_rem_permit;
+	funcs.rem_deny = purple_rem_deny;
+	funcs.get_info = purple_get_info;
+	funcs.keepalive = purple_keepalive;
+	funcs.send_typing = purple_send_typing;
+	funcs.handle_cmp = g_strcasecmp;
+	/* TODO(wilmer): Set these only for protocols that support them? */
+	funcs.chat_msg = purple_chat_msg;
+	funcs.chat_with = purple_chat_with;
+	funcs.chat_invite = purple_chat_invite;
+	funcs.chat_leave = purple_chat_leave;
+	funcs.chat_join = purple_chat_join;
+	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/twitter/Makefile
===================================================================
--- protocols/twitter/Makefile	(revision 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/twitter/Makefile	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -8,4 +8,7 @@
 
 -include ../../Makefile.settings
+ifdef SRCDIR
+SRCDIR := $(SRCDIR)protocols/twitter/
+endif
 
 # [SH] Program variables
@@ -33,5 +36,5 @@
 $(objects): ../../Makefile.settings Makefile
 
-$(objects): %.o: %.c
+$(objects): %.o: $(SRCDIR)%.c
 	@echo '*' Compiling $<
 	@$(CC) -c $(CFLAGS) $< -o $@
Index: protocols/yahoo/Makefile
===================================================================
--- protocols/yahoo/Makefile	(revision 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/yahoo/Makefile	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -8,4 +8,7 @@
 
 -include ../../Makefile.settings
+ifdef SRCDIR
+SRCDIR := $(SRCDIR)protocols/yahoo/
+endif
 
 # [SH] Program variables
@@ -33,5 +36,5 @@
 $(objects): ../../Makefile.settings Makefile
 
-$(objects): %.o: %.c
+$(objects): %.o: $(SRCDIR)%.c
 	@echo '*' Compiling $<
 	@$(CC) -c $(CFLAGS) $< -o $@
Index: protocols/yahoo/yahoo.c
===================================================================
--- protocols/yahoo/yahoo.c	(revision 0d9d53ed0b3eb068cf57355a4d1465beaf191f8a)
+++ protocols/yahoo/yahoo.c	(revision 4aa0f6bc5645e124738ab15ad1eb65d4147dba25)
@@ -686,5 +686,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 )
@@ -697,5 +697,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
