Index: protocols/nogaim.c
===================================================================
--- protocols/nogaim.c	(revision 6cdecc7e477b92c31448636fa48af10209b55e9b)
+++ protocols/nogaim.c	(revision 66aefeb3192e3afb906d88e5361508b6c7131d63)
@@ -132,4 +132,5 @@
 	extern void twitter_initmodule();
 	extern void purple_initmodule();
+	extern void rpc_initmodule();
 
 #ifdef WITH_MSN
@@ -155,4 +156,8 @@
 #ifdef WITH_PURPLE
 	purple_initmodule();
+#endif
+
+#ifdef WITH_RPC
+	rpc_initmodule();
 #endif
 
Index: protocols/rpc/rpc.c
===================================================================
--- protocols/rpc/rpc.c	(revision 6cdecc7e477b92c31448636fa48af10209b55e9b)
+++ protocols/rpc/rpc.c	(revision 66aefeb3192e3afb906d88e5361508b6c7131d63)
@@ -1,2 +1,5 @@
+#include <sys/socket.h>
+#include <sys/un.h>
+
 #include "bitlbee.h"
 #include "bee.h"
@@ -15,9 +18,10 @@
 	char *buf;
 	int buflen;
+	GHashTable *groupchats;
 };
 
 struct rpc_groupchat {
-	struct groupchat *bee_gc;
-	char *rpc_handle;
+	int id;
+	struct groupchat *gc;
 };
 
@@ -41,9 +45,25 @@
 
 /** Sends an RPC object. Takes ownership (i.e. frees it when done). */
-static void rpc_send(struct im_connection *ic, JSON_Value *rpc) {
+static gboolean rpc_send(struct im_connection *ic, JSON_Value *rpc) {
+	struct rpc_connection *rd = ic->proto_data;
 	char *buf = json_serialize_to_string(rpc);
-
+	int len = strlen(buf);
+	int st;
+
+	buf = g_realloc(buf, len + 3);
+	strcpy(buf + len, "\r\n");
+	len += 2;
+
+	st = write(rd->fd, buf, len);
 	g_free(buf);
 	json_value_free(rpc);
+
+	if (st != len) {
+		imcb_log(ic, "Write error");
+		imc_logout(ic, TRUE);
+		return FALSE;
+	}
+
+	return TRUE;
 }
 
@@ -53,5 +73,6 @@
 	json_object_set_string(o, "user", acc->user);
 	json_object_set_string(o, "pass", acc->user);
-	json_object_set_string(o, "server", acc->server);
+	if (acc->server)
+		json_object_set_string(o, "server", acc->server);
 	return v;
 }
@@ -75,4 +96,5 @@
 	}
 	ic->inpa = b_input_add(rd->fd, B_EV_IO_WRITE, rpc_login_cb, ic);
+	rd->groupchats = g_hash_table_new(g_int_hash, g_int_equal);
 }
 
@@ -97,4 +119,11 @@
 	RPC_OUT_INIT("logout");
 	rpc_send(ic, rpc);
+
+	struct rpc_connection *rd = ic->proto_data;
+	b_event_remove(ic->inpa);
+	closesocket(rd->fd);
+	g_free(rd->buf);
+	g_hash_table_destroy(rd->groupchats);
+	g_free(rd);
 }
 
@@ -171,4 +200,6 @@
 static void rpc_chat_invite(struct groupchat *gc, char *who, char *message) {
 	RPC_OUT_INIT("chat_invite");
+	struct rpc_groupchat *rc = gc->data;
+	json_array_append_number(params, rc->id);
 	json_array_append_string(params, who);
 	json_array_append_string(params, message);
@@ -178,4 +209,6 @@
 static void rpc_chat_kick(struct groupchat *gc, char *who, const char *message) {
 	RPC_OUT_INIT("chat_kick");
+	struct rpc_groupchat *rc = gc->data;
+	json_array_append_number(params, rc->id);
 	json_array_append_string(params, who);
 	json_array_append_string(params, message);
@@ -185,4 +218,6 @@
 static void rpc_chat_leave(struct groupchat *gc) {
 	RPC_OUT_INIT("chat_leave");
+	struct rpc_groupchat *rc = gc->data;
+	json_array_append_number(params, rc->id);
 	rpc_send(gc->ic, rpc);
 }
@@ -190,6 +225,6 @@
 static void rpc_chat_msg(struct groupchat *gc, char *msg, int flags) {
 	RPC_OUT_INIT("chat_msg");	
-	struct rpc_groupchat *data = gc->data;
-	json_array_append_string(params, data->rpc_handle);
+	struct rpc_groupchat *rc = gc->data;
+	json_array_append_number(params, rc->id);
 	json_array_append_string(params, msg);
 	json_array_append_number(params, flags);
@@ -197,6 +232,17 @@
 }
 
+static struct groupchat *rpc_groupchat_new(struct im_connection *ic, const char *handle) {
+	struct groupchat *gc = imcb_chat_new(ic, handle);
+	struct rpc_groupchat *rc = gc->data = g_new0(struct rpc_groupchat, 1);
+	rc->id = next_rpc_id;
+	rc->gc = gc;
+	return gc;
+}
+
 static struct groupchat *rpc_chat_with(struct im_connection *ic, char *who) {
 	RPC_OUT_INIT("chat_with");
+	struct groupchat *gc = rpc_groupchat_new(ic, who);
+	struct rpc_groupchat *rc = gc->data;
+	json_array_append_number(params, rc->id);
 	json_array_append_string(params, who);
 	rpc_send(ic, rpc);
@@ -208,4 +254,7 @@
                                        const char *password, set_t **sets) {
 	RPC_OUT_INIT("chat_join");
+	struct groupchat *gc = rpc_groupchat_new(ic, room);
+	struct rpc_groupchat *rc = gc->data;
+	json_array_append_number(params, rc->id);
 	json_array_append_string(params, room);
 	json_array_append_string(params, nick);
@@ -219,4 +268,6 @@
 static void rpc_chat_topic(struct groupchat *gc, char *topic) {
 	RPC_OUT_INIT("chat_topic");
+	struct rpc_groupchat *rc = gc->data;
+	json_array_append_number(params, rc->id);
 	json_array_append_string(params, topic);
 	rpc_send(gc->ic, rpc);
@@ -315,6 +366,10 @@
 	json_array_append_value(params, d);
 	char *s = json_serialize_to_string(rpc);
-
-	if ((st = write(fd, s, strlen(s))) != strlen(s)) {
+	int len = strlen(s);
+	s = g_realloc(s, len + 3);
+	strcpy(s + len, "\r\n");
+	len += 2;
+
+	if ((st = write(fd, s, len)) != len) {
 		// LOG ERROR
 		return;
@@ -337,4 +392,5 @@
 		if (st == 0) {
 			// LOG ERROR
+			closesocket(fd);
 			return;
 		}
@@ -348,4 +404,5 @@
 				continue;
 			// LOG ERROR
+			closesocket(fd);
 			return;
 		}
@@ -354,4 +411,5 @@
 	}
 	while (!(parsed = json_parse_string(resp)));
+	closesocket(fd);
 
 	JSON_Object *isup = json_object_get_object(json_object(parsed), "result");
@@ -410,5 +468,29 @@
 }
 
+#define PDIR "/tmp/rpcplugins"
+
+/* YA RLY :-/ */
+#ifndef UNIX_PATH_MAX
+struct sockaddr_un sizecheck;
+#define UNIX_PATH_MAX sizeof(sizecheck.sun_path)
+#endif
+
 void rpc_initmodule() {
-}
-
+	DIR *pdir = opendir(PDIR);
+	struct dirent *de;
+
+	if (!pdir)
+		return;
+
+	while ((de = readdir(pdir))) {
+		char *fn = g_build_filename(PDIR, de->d_name, NULL);
+		struct sockaddr_un su;
+
+		strncpy(su.sun_path, fn, UNIX_PATH_MAX);
+		su.sun_path[UNIX_PATH_MAX-1] = '\0';
+		su.sun_family = AF_UNIX;
+		rpc_initmodule_sock((struct sockaddr*) &su, sizeof(su));
+		g_free(fn);
+	}
+}
+
