Index: .bzrignore
===================================================================
--- .bzrignore	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ .bzrignore	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -13,2 +13,6 @@
 .gdb_history
 tests/check
+*.gcda
+*.gcov
+*.gcno
+*.o
Index: Makefile
===================================================================
--- Makefile	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ Makefile	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -40,11 +40,16 @@
 clean: $(subdirs)
 	rm -f *.o $(OUTFILE) core utils/bitlbeed encode decode
+	$(MAKE) -C tests clean
 
 distclean: clean $(subdirs)
 	rm -f Makefile.settings config.h bitlbee.pc
 	find . -name 'DEADJOE' -o -name '*.orig' -o -name '*.rej' -o -name '*~' -exec rm -f {} \;
+	$(MAKE) -C test distclean
 
-check:
+check: all
 	$(MAKE) -C tests
+
+gcov: check
+	gcov *.c
 
 install-doc:
Index: configure
===================================================================
--- configure	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ configure	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -28,4 +28,5 @@
 debug=0
 strip=1
+gcov=0
 ipv6=1
 
@@ -67,4 +68,5 @@
 --debug=0/1	Disable/enable debugging		$debug
 --strip=0/1	Disable/enable binary stripping		$strip
+--gcov=0/1	Disable/enable test coverage reporting	$gcov
 
 --ipv6=0/1	IPv6 socket support			$ipv6
@@ -366,4 +368,9 @@
 fi
 
+if [ "$gcov" = "1" ]; then
+	echo "CFLAGS+=-ftest-coverage -fprofile-arcs" >> Makefile.settings
+	echo "EFLAGS+=-lgcov" >> Makefile.settings
+fi
+
 echo
 if [ -z "$BITLBEE_VERSION" -a -d .bzr ] && type bzr > /dev/null 2> /dev/null; then
Index: doc/user-guide/docbook.xsl
===================================================================
--- doc/user-guide/docbook.xsl	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ doc/user-guide/docbook.xsl	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -83,4 +83,5 @@
 
 	<xsl:template name="cmd">
+		<xsl:param name="prefix"/>
 		<xsl:variable name="thiscmd"><xsl:value-of select="$prefix"/><xsl:value-of select="@name"/></xsl:variable>
 		<xsl:attribute name="id">
Index: doc/user-guide/help.xsl
===================================================================
--- doc/user-guide/help.xsl	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ doc/user-guide/help.xsl	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -31,4 +31,5 @@
 
 	<xsl:template name="subject">
+		<xsl:param name="id"/>
 		<xsl:message><xsl:text>Processing: </xsl:text><xsl:value-of select="$id"/></xsl:message>
 		<xsl:text>?</xsl:text><xsl:value-of select="$id"/><xsl:text>&#10;</xsl:text>
@@ -122,4 +123,5 @@
 
 	<xsl:template name="cmd">
+		<xsl:param name="prefix"/>
 		<xsl:variable name="thiscmd"><xsl:value-of select="$prefix"/><xsl:value-of select="@name"/></xsl:variable>
 		<xsl:message><xsl:text>Processing command '</xsl:text><xsl:value-of select="$thiscmd"/><xsl:text>'</xsl:text></xsl:message>
Index: ipc.c
===================================================================
--- ipc.c	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ ipc.c	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -99,5 +99,5 @@
 	{ "die",        0, ipc_master_cmd_die,        0 },
 	{ "wallops",    1, NULL,                      IPC_CMD_TO_CHILDREN },
-	{ "lilo",       1, NULL,                      IPC_CMD_TO_CHILDREN },
+	{ "wall",       1, NULL,                      IPC_CMD_TO_CHILDREN },
 	{ "opermsg",    1, NULL,                      IPC_CMD_TO_CHILDREN },
 	{ "rehash",     0, ipc_master_cmd_rehash,     0 },
@@ -122,5 +122,5 @@
 }
 
-static void ipc_child_cmd_lilo( irc_t *irc, char **cmd )
+static void ipc_child_cmd_wall( irc_t *irc, char **cmd )
 {
 	if( !( irc->status & USTATUS_LOGGED_IN ) )
@@ -175,5 +175,5 @@
 	{ "die",        0, ipc_child_cmd_die,         0 },
 	{ "wallops",    1, ipc_child_cmd_wallops,     0 },
-	{ "lilo",       1, ipc_child_cmd_lilo,        0 },
+	{ "wall",       1, ipc_child_cmd_wall,        0 },
 	{ "opermsg",    1, ipc_child_cmd_opermsg,     0 },
 	{ "rehash",     0, ipc_child_cmd_rehash,      0 },
Index: irc.c
===================================================================
--- irc.c	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ irc.c	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -585,6 +585,9 @@
 		   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 );
+		/* 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 ) ) */
+		
+		/* 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 );
 	}
 	
Index: irc_commands.c
===================================================================
--- irc_commands.c	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ irc_commands.c	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -593,5 +593,5 @@
 	{ "die",         0, NULL,                IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER },
 	{ "wallops",     1, NULL,                IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER },
-	{ "lilo",        1, NULL,                IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER },
+	{ "wall",        1, NULL,                IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER },
 	{ "rehash",      0, irc_cmd_rehash,      IRC_CMD_OPER_ONLY },
 	{ "restart",     0, NULL,                IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER },
Index: lib/Makefile
===================================================================
--- lib/Makefile	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ lib/Makefile	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -17,4 +17,7 @@
 # [SH] Phony targets
 all: lib.o
+check: all
+gcov:
+	gcov *.c
 
 .PHONY: all clean distclean
Index: protocols/Makefile
===================================================================
--- protocols/Makefile	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ protocols/Makefile	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -26,4 +26,7 @@
 # [SH] Phony targets
 all: protocols.o
+check: all
+gcov:
+	gcov *.c
 
 .PHONY: all clean distclean $(subdirs)
Index: protocols/jabber/Makefile
===================================================================
--- protocols/jabber/Makefile	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ protocols/jabber/Makefile	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -17,4 +17,7 @@
 # [SH] Phony targets
 all: jabber_mod.o
+check: all
+gcov: 
+	gcov *.c
 
 .PHONY: all clean distclean
Index: protocols/msn/Makefile
===================================================================
--- protocols/msn/Makefile	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ protocols/msn/Makefile	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -17,4 +17,7 @@
 # [SH] Phony targets
 all: msn_mod.o
+check: all
+gcov: 
+	gcov *.c
 	
 .PHONY: all clean distclean
Index: protocols/oscar/Makefile
===================================================================
--- protocols/oscar/Makefile	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ protocols/oscar/Makefile	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -17,4 +17,7 @@
 # [SH] Phony targets
 all: oscar_mod.o
+check: all
+gcov:
+	gcov *.c
 
 .PHONY: all clean distclean
Index: protocols/yahoo/Makefile
===================================================================
--- protocols/yahoo/Makefile	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ protocols/yahoo/Makefile	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -17,4 +17,7 @@
 # [SH] Phony targets
 all: yahoo_mod.o
+check: all
+gcov: 
+	gcov *.c
 
 .PHONY: all clean distclean
Index: storage_xml.c
===================================================================
--- storage_xml.c	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ storage_xml.c	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -395,5 +395,5 @@
 	
 	strcat( path, "~" );
-	if( ( fd = open( path, O_WRONLY | O_CREAT, 0600 ) ) < 0 )
+	if( ( fd = open( path, O_WRONLY | O_CREAT | O_TRUNC, 0600 ) ) < 0 )
 	{
 		irc_usermsg( irc, "Error while opening configuration file." );
Index: tests/Makefile
===================================================================
--- tests/Makefile	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ tests/Makefile	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -3,10 +3,15 @@
 LFLAGS +=-lcheck
 
-all: check
-	./check
+all: check 
+	./check $(CHECKFLAGS)
 
-main_objs = account.o bitlbee.o conf.o crypting.o help.o ini.o ipc.o irc.o irc_commands.o log.o nick.o query.o root_commands.o set.o storage.o storage_text.o url.o user.o util.o 
+clean:
+	rm -f check *.o
 
-check: check.o check_util.o check_nick.o $(addprefix ../, $(main_objs)) ../protocols/protocols.o
+main_objs = account.o bitlbee.o conf.o crypting.o help.o ipc.o irc.o irc_commands.o log.o nick.o query.o root_commands.o set.o storage.o storage_xml.o storage_text.o user.o 
+
+test_objs = check.o check_util.o check_nick.o check_md5.o check_irc.o
+
+check: $(test_objs) $(addprefix ../, $(main_objs)) ../protocols/protocols.o ../lib/lib.o
 	@echo '*' Linking $@
 	@$(CC) $(CFLAGS) -o $@ $^ $(LFLAGS) $(EFLAGS)
Index: tests/check.c
===================================================================
--- tests/check.c	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ tests/check.c	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -4,6 +4,20 @@
 #include <check.h>
 #include "bitlbee.h"
+#include "testsuite.h"
 
 global_t global;	/* Against global namespace pollution */
+
+gboolean g_io_channel_pair(GIOChannel **ch1, GIOChannel **ch2)
+{
+	int sock[2];
+	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNIX, sock) < 0) {
+		perror("socketpair");
+		return FALSE;
+	}
+
+	*ch1 = g_io_channel_unix_new(sock[0]);
+	*ch2 = g_io_channel_unix_new(sock[1]);
+	return TRUE;
+}
 
 double gettime()
@@ -21,10 +35,51 @@
 Suite *nick_suite(void);
 
-int main (void)
+/* From check_md5.c */
+Suite *md5_suite(void);
+
+/* From check_irc.c */
+Suite *irc_suite(void);
+
+int main (int argc, char **argv)
 {
 	int nf;
-	SRunner *sr = srunner_create(util_suite());
+	SRunner *sr;
+	GOptionContext *pc;
+	gboolean no_fork = FALSE;
+	gboolean verbose = FALSE;
+	GOptionEntry options[] = {
+		{"no-fork", 'n', 0, G_OPTION_ARG_NONE, &no_fork, "Don't fork" },
+		{"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
+		{ NULL }
+	};
+	int i;
+
+	pc = g_option_context_new("");
+	g_option_context_add_main_entries(pc, options, NULL);
+
+	if(!g_option_context_parse(pc, &argc, &argv, NULL))
+		return 1;
+
+	g_option_context_free(pc);
+
+	log_init();
+
+	if (verbose) {
+		log_link( LOGLVL_ERROR, LOGOUTPUT_CONSOLE );
+		log_link( LOGLVL_DEBUG, LOGOUTPUT_CONSOLE );
+		log_link( LOGLVL_INFO, LOGOUTPUT_CONSOLE );
+		log_link( LOGLVL_WARNING, LOGOUTPUT_CONSOLE );
+	}
+
+	global.conf = conf_load( 0, NULL);
+	global.conf->runmode = RUNMODE_DAEMON;
+
+	sr = srunner_create(util_suite());
 	srunner_add_suite(sr, nick_suite());
-	srunner_run_all (sr, CK_NORMAL);
+	srunner_add_suite(sr, md5_suite());
+	srunner_add_suite(sr, irc_suite());
+	if (no_fork)
+		srunner_set_fork_status(sr, CK_NOFORK);
+	srunner_run_all (sr, verbose?CK_VERBOSE:CK_NORMAL);
 	nf = srunner_ntests_failed(sr);
 	srunner_free(sr);
Index: tests/check_irc.c
===================================================================
--- tests/check_irc.c	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
+++ tests/check_irc.c	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -0,0 +1,65 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include <string.h>
+#include <stdio.h>
+#include "irc.h"
+#include "testsuite.h"
+
+START_TEST(test_connect)
+	GIOChannel *ch1, *ch2;
+	irc_t *irc;
+	char *raw;
+	fail_unless(g_io_channel_pair(&ch1, &ch2));
+
+	irc = irc_new(g_io_channel_unix_get_fd(ch1));
+
+	irc_free(irc);
+
+	fail_unless(g_io_channel_read_to_end(ch2, &raw, NULL, NULL) == G_IO_STATUS_NORMAL);
+	
+	fail_if(strcmp(raw, "") != 0);
+
+	g_free(raw);
+END_TEST
+
+START_TEST(test_login)
+	GIOChannel *ch1, *ch2;
+	irc_t *irc;
+	GError *error = NULL;
+	char *raw;
+	fail_unless(g_io_channel_pair(&ch1, &ch2));
+
+	g_io_channel_set_flags(ch1, G_IO_FLAG_NONBLOCK, NULL);
+	g_io_channel_set_flags(ch2, G_IO_FLAG_NONBLOCK, NULL);
+
+	irc = irc_new(g_io_channel_unix_get_fd(ch1));
+
+	fail_unless(g_io_channel_write_chars(ch2, "NICK bla\r\n"
+			"USER a a a a\r\n", -1, NULL, NULL) == G_IO_STATUS_NORMAL);
+	fail_unless(g_io_channel_flush(ch2, NULL) == G_IO_STATUS_NORMAL);
+
+	g_main_iteration(FALSE);
+	irc_free(irc);
+
+	fail_unless(g_io_channel_read_to_end(ch2, &raw, NULL, NULL) == G_IO_STATUS_NORMAL);
+	
+	fail_unless(strstr(raw, "001") != NULL);
+	fail_unless(strstr(raw, "002") != NULL);
+	fail_unless(strstr(raw, "003") != NULL);
+	fail_unless(strstr(raw, "004") != NULL);
+	fail_unless(strstr(raw, "005") != NULL);
+
+	g_free(raw);
+END_TEST
+
+Suite *irc_suite (void)
+{
+	Suite *s = suite_create("IRC");
+	TCase *tc_core = tcase_create("Core");
+	suite_add_tcase (s, tc_core);
+	tcase_add_test (tc_core, test_connect);
+	tcase_add_test (tc_core, test_login);
+	return s;
+}
Index: tests/check_md5.c
===================================================================
--- tests/check_md5.c	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
+++ tests/check_md5.c	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -0,0 +1,56 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include <string.h>
+#include <stdio.h>
+#include "md5.h"
+
+/* From RFC 1321 */
+struct md5_test {
+	const char *str;
+	md5_byte_t expected[16];
+} tests[] = {
+	{ "", 
+		{ 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } },
+	{ "a",
+		{ 0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8, 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61 } },
+	{ "abc", 
+		{ 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } },
+	{ "message digest", 
+		{ 0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d, 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 } },
+	{ "abcdefghijklmnopqrstuvwxyz", 
+		{ 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b } },
+	{ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
+	    { 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5, 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f } },
+	{ "12345678901234567890123456789012345678901234567890123456789012345678901234567890", 
+		{ 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55, 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a } },
+
+	{ NULL },
+};
+
+static void check_sums(int l)
+{
+	int i;
+	for (i = 0; tests[i].str; i++) {
+		md5_byte_t sum[16];
+  		tcase_fn_start (tests[i].str, __FILE__, __LINE__);
+		md5_state_t state;
+		int di;
+
+		md5_init(&state);
+		md5_append(&state, (const md5_byte_t *)tests[i].str, strlen(tests[i].str));
+		md5_finish(&state, sum);
+
+		fail_if(memcmp(tests[i].expected, sum, 16) != 0, "%s failed", tests[i].str);
+	}
+}
+
+Suite *md5_suite (void)
+{
+	Suite *s = suite_create("MD5");
+	TCase *tc_core = tcase_create("Core");
+	suite_add_tcase (s, tc_core);
+	tcase_add_test (tc_core, check_sums);
+	return s;
+}
Index: tests/check_nick.c
===================================================================
--- tests/check_nick.c	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ tests/check_nick.c	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -6,5 +6,5 @@
 #include "irc.h"
 #include "set.h"
-#include "util.h"
+#include "misc.h"
 
 START_TEST(test_nick_strip)
@@ -23,5 +23,5 @@
 
 	for (i = 0; get[i]; i++) {
-		char copy[30];
+		char copy[60];
 		strcpy(copy, get[i]);
 		nick_strip(copy);
Index: tests/check_util.c
===================================================================
--- tests/check_util.c	(revision bd28e6a2eec0333a866ef2e380d32b1e6ad0c80b)
+++ tests/check_util.c	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -6,5 +6,5 @@
 #include "irc.h"
 #include "set.h"
-#include "util.h"
+#include "misc.h"
 
 START_TEST(test_strip_linefeed)
Index: tests/testsuite.h
===================================================================
--- tests/testsuite.h	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
+++ tests/testsuite.h	(revision a3a37787188b68d17549e4db1ac1561cf7541979)
@@ -0,0 +1,6 @@
+#ifndef __BITLBEE_CHECK_H__
+#define __BITLBEE_CHECK_H__ 
+
+gboolean g_io_channel_pair(GIOChannel **ch1, GIOChannel **ch2);
+
+#endif /* __BITLBEE_CHECK_H__ */
