Index: irc.c
===================================================================
--- irc.c	(revision dd89a55a9b54e29da43d6adea00fc2c42e3e7ebd)
+++ irc.c	(revision 0356ae3aa10bb6556d0ea881988831cad5e71f38)
@@ -29,5 +29,5 @@
 #include "ipc.h"
 
-static gboolean irc_userping( gpointer _irc );
+static gboolean irc_userping( gpointer _irc, int fd, b_input_condition cond );
 
 GSList *irc_connection_list = NULL;
@@ -54,13 +54,7 @@
 	
 	irc->fd = fd;
-	irc->io_channel = g_io_channel_unix_new( fd );
-#ifdef GLIB2
-	g_io_channel_set_encoding (irc->io_channel, NULL, NULL);
-	g_io_channel_set_buffered (irc->io_channel, FALSE);
-	g_io_channel_set_flags( irc->io_channel, G_IO_FLAG_NONBLOCK, NULL );
-#else
-	fcntl( irc->fd, F_SETFL, O_NONBLOCK);
-#endif
-	irc->r_watch_source_id = g_io_add_watch( irc->io_channel, G_IO_IN | G_IO_ERR | G_IO_HUP, bitlbee_io_current_client_read, irc );
+	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->status = USTATUS_OFFLINE;
@@ -120,5 +114,5 @@
 
 	if( global.conf->ping_interval > 0 && global.conf->ping_timeout > 0 )
-		irc->ping_source_id = g_timeout_add( global.conf->ping_interval * 1000, irc_userping, irc );
+		irc->ping_source_id = b_timeout_add( global.conf->ping_interval * 1000, irc_userping, irc );
 	
 	irc_write( irc, ":%s NOTICE AUTH :%s", irc->myhost, "BitlBee-IRCd initialized, please go on" );
@@ -190,6 +184,6 @@
 		   bitlbee_.._write doesn't do it first. */
 		
-		g_source_remove( irc->r_watch_source_id );
-		irc->r_watch_source_id = g_timeout_add_full( G_PRIORITY_HIGH, 1000, (GSourceFunc) irc_free, irc, NULL );
+		b_event_remove( irc->r_watch_source_id );
+		irc->r_watch_source_id = b_timeout_add( 1000, (b_event_handler) irc_free, irc );
 	}
 	else
@@ -224,10 +218,9 @@
 	
 	if( irc->ping_source_id > 0 )
-		g_source_remove( irc->ping_source_id );
-	g_source_remove( irc->r_watch_source_id );
+		b_event_remove( irc->ping_source_id );
+	b_event_remove( irc->r_watch_source_id );
 	if( irc->w_watch_source_id > 0 )
-		g_source_remove( irc->w_watch_source_id );
-	
-	g_io_channel_unref( irc->io_channel );
+		b_event_remove( irc->w_watch_source_id );
+	
 	irc_connection_list = g_slist_remove( irc_connection_list, irc );
 	
@@ -279,5 +272,5 @@
 			if(user->host!=user->nick) g_free(user->host);
 			if(user->realname!=user->nick) g_free(user->realname);
-			gaim_input_remove(user->sendbuf_timer);
+			b_event_remove(user->sendbuf_timer);
 					
 			usertmp = user;
@@ -329,5 +322,5 @@
 	
 	if( global.conf->runmode == RUNMODE_INETD || global.conf->runmode == RUNMODE_FORKDAEMON )
-		g_main_quit( global.loop );
+		b_main_quit();
 }
 
@@ -557,5 +550,5 @@
 	
 	u = user_find( irc, irc->mynick );
-	is_private = u->is_private;
+	if( u ) is_private = u->is_private;
 	
 	va_start( params, format );
@@ -583,5 +576,6 @@
 	char line[IRC_MAX_LINE+1], *cs;
 		
-	if( irc->quit )
+	/* Don't try to write anything new anymore when shutting down. */
+	if( irc->status == USTATUS_SHUTDOWN )
 		return;
 	
@@ -600,15 +594,23 @@
 	strcat( line, "\r\n" );
 	
-	if( irc->sendbuffer != NULL ) {
+	if( irc->sendbuffer != NULL )
+	{
 		size = strlen( irc->sendbuffer ) + strlen( line );
 		irc->sendbuffer = g_renew ( char, irc->sendbuffer, size + 1 );
 		strcpy( ( irc->sendbuffer + strlen( irc->sendbuffer ) ), line );
 	}
-	else 
-		irc->sendbuffer = g_strdup(line);	
+	else
+	{
+		irc->sendbuffer = g_strdup(line);
+	}
 	
 	if( irc->w_watch_source_id == 0 )
 	{
-		irc->w_watch_source_id = g_io_add_watch( irc->io_channel, G_IO_OUT, bitlbee_io_current_client_write, irc );
+		/* If the buffer is empty we can probably write, so call the write event handler
+		   immediately. If it returns TRUE, it should be called again, so add the event to
+		   the queue. If it's FALSE, we emptied the buffer and saved ourselves some work
+		   in the event queue. */
+		if( bitlbee_io_current_client_write( irc, irc->fd, GAIM_INPUT_WRITE ) )
+			irc->w_watch_source_id = b_input_add( irc->fd, GAIM_INPUT_WRITE, bitlbee_io_current_client_write, irc );
 	}
 	
@@ -636,5 +638,5 @@
 		if( now )
 		{
-			bitlbee_io_current_client_write( irc->io_channel, G_IO_OUT, irc );
+			bitlbee_io_current_client_write( irc, irc->fd, GAIM_INPUT_WRITE );
 		}
 		temp = temp->next;
@@ -1017,5 +1019,5 @@
 }
 
-gboolean buddy_send_handler_delayed( gpointer data )
+static gboolean buddy_send_handler_delayed( gpointer data, gint fd, b_input_condition cond )
 {
 	user_t *u = data;
@@ -1030,5 +1032,5 @@
 	u->sendbuf_flags = 0;
 	
-	return( FALSE );
+	return FALSE;
 }
 
@@ -1044,6 +1046,6 @@
 		{
 			//Flush the buffer
-			g_source_remove( u->sendbuf_timer );
-			buddy_send_handler_delayed( u );
+			b_event_remove( u->sendbuf_timer );
+			buddy_send_handler_delayed( u, -1, 0 );
 		}
 
@@ -1069,6 +1071,6 @@
 		
 		if( u->sendbuf_timer > 0 )
-			g_source_remove( u->sendbuf_timer );
-		u->sendbuf_timer = g_timeout_add( delay, buddy_send_handler_delayed, u );
+			b_event_remove( u->sendbuf_timer );
+		u->sendbuf_timer = b_timeout_add( delay, buddy_send_handler_delayed, u );
 	}
 	else
@@ -1154,5 +1156,5 @@
    pongs from the user. When not connected yet, we don't ping but drop the
    connection when the user fails to connect in IRC_LOGIN_TIMEOUT secs. */
-static gboolean irc_userping( gpointer _irc )
+static gboolean irc_userping( gpointer _irc, gint fd, b_input_condition cond )
 {
 	irc_t *irc = _irc;
