Index: Makefile
===================================================================
--- Makefile	(revision be999a5385ff4e9ac7416de4cca3288f18cee85c)
+++ Makefile	(revision ad2d8bccb2a27688845d87b59eddff976ade5ef7)
@@ -10,5 +10,5 @@
 
 # Program variables
-objects = bitlbee.o dcc.o help.o ipc.o irc.o irc_im.o irc_channel.o irc_commands.o irc_send.o irc_user.o irc_util.o nick.o query.o root_commands.o set.o storage.o $(STORAGE_OBJS)
+objects = bitlbee.o dcc.o help.o ipc.o irc.o irc_im.o irc_channel.o irc_commands.o irc_send.o irc_user.o irc_util.o nick.o otr.o query.o root_commands.o set.o storage.o $(STORAGE_OBJS)
 headers = bitlbee.h commands.h conf.h config.h help.h ipc.h irc.h log.h nick.h query.h set.h sock.h storage.h lib/events.h lib/ftutil.h lib/http_client.h lib/ini.h lib/md5.h lib/misc.h lib/proxy.h lib/sha1.h lib/ssl_client.h lib/url.h protocols/account.h protocols/bee.h protocols/ft.h protocols/nogaim.h
 subdirs = lib protocols
Index: irc.h
===================================================================
--- irc.h	(revision be999a5385ff4e9ac7416de4cca3288f18cee85c)
+++ irc.h	(revision ad2d8bccb2a27688845d87b59eddff976ade5ef7)
@@ -97,4 +97,7 @@
 	/* Replaced with iu->last_channel IRC_USER_PRIVATE = 1, */
 	IRC_USER_AWAY = 2,
+	
+	IRC_USER_OTR_ENCRYPTED = 0x10000,
+	IRC_USER_OTR_TRUSTED   = 0x20000,
 } irc_user_flags_t;
 
Index: otr.c
===================================================================
--- otr.c	(revision be999a5385ff4e9ac7416de4cca3288f18cee85c)
+++ otr.c	(revision ad2d8bccb2a27688845d87b59eddff976ade5ef7)
@@ -146,7 +146,7 @@
 int hexval(char a);
 
-/* determine the user_t for a given handle/protocol pair
+/* determine the irc_user_t for a given handle/protocol pair
    returns NULL if not found */
-user_t *peeruser(irc_t *irc, const char *handle, const char *protocol);
+irc_user_t *peeruser(irc_t *irc, const char *handle, const char *protocol);
 
 /* handle SMP TLVs from a received message */
@@ -156,5 +156,5 @@
    returns 0 if neither op_buddies nor voice_buddies is set to "encrypted",
    i.e. msgstate should be announced seperately */
-int otr_update_modeflags(irc_t *irc, user_t *u);
+int otr_update_modeflags(irc_t *irc, irc_user_t *u);
 
 /* show general info about the OTR subsystem; called by 'otr info' */
@@ -238,10 +238,10 @@
 	int kg=0;
 
-	g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, irc->nick);
+	g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, irc->user->nick);
 	e = otrl_privkey_read(irc->otr->us, s);
 	if(e && e!=enoent) {
 		irc_usermsg(irc, "otr load: %s: %s", s, gcry_strerror(e));
 	}
-	g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->nick);
+	g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick);
 	e = otrl_privkey_read_fingerprints(irc->otr->us, s, NULL, NULL);
 	if(e && e!=enoent) {
@@ -250,5 +250,5 @@
 	
 	/* check for otr keys on all accounts */
-	for(a=irc->accounts; a; a=a->next) {
+	for(a=irc->b->accounts; a; a=a->next) {
 		kg = otr_check_for_key(a) || kg;
 	}
@@ -269,5 +269,5 @@
 	gcry_error_t e;
 
-	g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->nick);
+	g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick);
 	e = otrl_privkey_write_fingerprints(irc->otr->us, s);
 	if(e) {
@@ -301,5 +301,5 @@
 int otr_check_for_key(account_t *a)
 {
-	irc_t *irc = a->irc;
+	irc_t *irc = a->bee->ui_data;
 	OtrlPrivKey *k;
 	
@@ -329,4 +329,5 @@
 	OtrlTLV *tlvs = NULL;
 	char *colormsg;
+	irc_t *irc = ic->bee->ui_data;
 	
 	/* don't do OTR on certain (not classic IM) protocols, e.g. twitter */
@@ -335,5 +336,5 @@
 	}
 	
-	ignore_msg = otrl_message_receiving(ic->irc->otr->us, &global.otr_ops, ic,
+	ignore_msg = otrl_message_receiving(irc->otr->us, &global.otr_ops, ic,
 		ic->acc->user, ic->acc->prpl->name, handle, msg, &newmsg,
 		&tlvs, NULL, NULL);
@@ -349,8 +350,8 @@
 	} else {
 		/* OTR has processed this message */
-		ConnContext *context = otrl_context_find(ic->irc->otr->us, handle,
+		ConnContext *context = otrl_context_find(irc->otr->us, handle,
 			ic->acc->user, ic->acc->prpl->name, 0, NULL, NULL, NULL);
 		if(context && context->msgstate == OTRL_MSGSTATE_ENCRYPTED &&
-		   set_getbool(&ic->irc->set, "color_encrypted")) {
+		   set_getbool(&ic->bee->set, "color_encrypted")) {
 			/* color according to f'print trust */
 			int color;
@@ -381,11 +382,13 @@
 	char *otrmsg = NULL;
 	ConnContext *ctx = NULL;
+	irc_t *irc = ic->bee->ui_data;
 
 	/* don't do OTR on certain (not classic IM) protocols, e.g. twitter */
 	if(ic->acc->prpl->options & OPT_NOOTR) {
-		return (ic->acc->prpl->buddy_msg(ic, handle, msg, flags));
-	}
-	
-	st = otrl_message_sending(ic->irc->otr->us, &global.otr_ops, ic,
+		/* TODO(wilmer): const */
+		return (ic->acc->prpl->buddy_msg(ic, (char*) handle, (char*) msg, flags));
+	}
+	
+	st = otrl_message_sending(irc->otr->us, &global.otr_ops, ic,
 		ic->acc->user, ic->acc->prpl->name, handle,
 		msg, NULL, &otrmsg, NULL, NULL);
@@ -394,5 +397,5 @@
 	}
 
-	ctx = otrl_context_find(ic->irc->otr->us,
+	ctx = otrl_context_find(irc->otr->us,
 			handle, ic->acc->user, ic->acc->prpl->name,
 			1, NULL, NULL, NULL);
@@ -451,13 +454,14 @@
 {
 	struct im_connection *ic = check_imc(opdata, context->accountname, context->protocol);
+	irc_t *irc = ic->bee->ui_data;
 	const char *p;
 	
 	/* policy override during keygen: if we're missing the key for context but are currently
 	   generating it, then that's as much as we can do. => temporarily return NEVER. */
-	if(keygen_in_progress(ic->irc, context->accountname, context->protocol) &&
-	   !otrl_privkey_find(ic->irc->otr->us, context->accountname, context->protocol))
+	if(keygen_in_progress(irc, context->accountname, context->protocol) &&
+	   !otrl_privkey_find(irc->otr->us, context->accountname, context->protocol))
 		return OTRL_POLICY_NEVER;
 
-	p = set_getstr(&ic->irc->set, "otr_policy");
+	p = set_getstr(&ic->bee->set, "otr_policy");
 	if(!strcmp(p, "never"))
 		return OTRL_POLICY_NEVER;
@@ -476,7 +480,8 @@
 {
 	struct im_connection *ic = check_imc(opdata, accountname, protocol);
+	irc_t *irc = ic->bee->ui_data;
 	
 	/* will fail silently if keygen already in progress */
-	otr_keygen(ic->irc, accountname, protocol);
+	otr_keygen(irc, accountname, protocol);
 }
 
@@ -485,10 +490,10 @@
 {
 	struct im_connection *ic = check_imc(opdata, accountname, protocol);
-	user_t *u;
-
-	/* lookup the user_t for the given recipient */
-	u = user_findhandle(ic, recipient);
-	if(u) {
-		if(u->online)
+	bee_user_t *bu;
+
+	/* lookup the irc_user_t for the given recipient */
+	bu = bee_user_by_handle(ic->bee, ic, recipient);
+	if(bu) {
+		if(bu->flags & BEE_USER_ONLINE)
 			return 1;
 		else
@@ -503,8 +508,9 @@
 {
 	struct im_connection *ic = check_imc(opdata, accountname, protocol);
+	irc_t *irc = ic->bee->ui_data;
 
 	if (strcmp(accountname, recipient) == 0) {
 		/* huh? injecting messages to myself? */
-		irc_usermsg(ic->irc, "note to self: %s", message);
+		irc_usermsg(irc, "note to self: %s", message);
 	} else {
 		/* need to drop some consts here :-( */
@@ -520,7 +526,8 @@
 	struct im_connection *ic = check_imc(opdata, accountname, protocol);
 	char *msg = g_strdup(message);
+	irc_t *irc = ic->bee->ui_data;
 
 	strip_html(msg);
-	irc_usermsg(ic->irc, "otr: %s", msg);
+	irc_usermsg(irc, "otr: %s", msg);
 
 	g_free(msg);
@@ -533,9 +540,10 @@
 {
 	struct im_connection *ic = check_imc(opdata, accountname, protocol);
+	irc_t *irc = ic->bee->ui_data;
 	char hunam[45];		/* anybody looking? ;-) */
 	
 	otrl_privkey_hash_to_human(hunam, fingerprint);
-	irc_usermsg(ic->irc, "new fingerprint for %s: %s",
-		peernick(ic->irc, username, protocol), hunam);
+	irc_usermsg(irc, "new fingerprint for %s: %s",
+		peernick(irc, username, protocol), hunam);
 }
 
@@ -543,6 +551,7 @@
 {
 	struct im_connection *ic = (struct im_connection *)opdata;
-
-	otr_save(ic->irc);
+	irc_t *irc = ic->bee->ui_data;
+
+	otr_save(irc);
 }
 
@@ -551,11 +560,12 @@
 	struct im_connection *ic =
 		check_imc(opdata, context->accountname, context->protocol);
-	user_t *u;
+	irc_user_t *u;
+	irc_t *irc = ic->bee->ui_data;
 	const char *trust;
 
-	u = peeruser(ic->irc, context->username, context->protocol);
+	u = peeruser(irc, context->username, context->protocol);
 	if(!u) {
 		log_message(LOGLVL_ERROR,
-			"BUG: otr.c: op_gone_secure: user_t for %s/%s/%s not found!",
+			"BUG: otr.c: op_gone_secure: irc_user_t for %s/%s/%s not found!",
 			context->username, context->protocol, context->accountname);
 		return;
@@ -564,9 +574,9 @@
 	trust = context->active_fingerprint->trust;
 	if(trust && trust[0])
-		u->encrypted = 2;
+		u->flags |= IRC_USER_OTR_ENCRYPTED | IRC_USER_OTR_TRUSTED;
 	else
-		u->encrypted = 1;
-	if(!otr_update_modeflags(ic->irc, u))
-		irc_usermsg(ic->irc, "conversation with %s is now off the record", u->nick);
+		u->flags = ( u->flags & ~IRC_USER_OTR_TRUSTED ) | IRC_USER_OTR_ENCRYPTED;
+	if(!otr_update_modeflags(irc, u))
+		irc_usermsg(irc, "conversation with %s is now off the record", u->nick);
 }
 
@@ -575,16 +585,17 @@
 	struct im_connection *ic =
 		check_imc(opdata, context->accountname, context->protocol);
-	user_t *u;
-
-	u = peeruser(ic->irc, context->username, context->protocol);
+	irc_t *irc = ic->bee->ui_data;
+	irc_user_t *u;
+
+	u = peeruser(irc, context->username, context->protocol);
 	if(!u) {
 		log_message(LOGLVL_ERROR,
-			"BUG: otr.c: op_gone_insecure: user_t for %s/%s/%s not found!",
+			"BUG: otr.c: op_gone_insecure: irc_user_t for %s/%s/%s not found!",
 			context->username, context->protocol, context->accountname);
 		return;
 	}
-	u->encrypted = 0;
-	if(!otr_update_modeflags(ic->irc, u))
-		irc_usermsg(ic->irc, "conversation with %s is now in the clear", u->nick);
+	u->flags &= ~( IRC_USER_OTR_ENCRYPTED | IRC_USER_OTR_TRUSTED );
+	if(!otr_update_modeflags(irc, u))
+		irc_usermsg(irc, "conversation with %s is now in the clear", u->nick);
 }
 
@@ -593,19 +604,20 @@
 	struct im_connection *ic =
 		check_imc(opdata, context->accountname, context->protocol);
-	user_t *u;
-
-	u = peeruser(ic->irc, context->username, context->protocol);
+	irc_t *irc = ic->bee->ui_data;
+	irc_user_t *u;
+
+	u = peeruser(irc, context->username, context->protocol);
 	if(!u) {
 		log_message(LOGLVL_ERROR,
-			"BUG: otr.c: op_still_secure: user_t for %s/%s/%s not found!",
+			"BUG: otr.c: op_still_secure: irc_user_t for %s/%s/%s not found!",
 			context->username, context->protocol, context->accountname);
 		return;
 	}
 	if(context->active_fingerprint->trust[0])
-		u->encrypted = 2;
+		u->flags |= IRC_USER_OTR_ENCRYPTED | IRC_USER_OTR_TRUSTED;
 	else
-		u->encrypted = 1;
-	if(!otr_update_modeflags(ic->irc, u))
-		irc_usermsg(ic->irc, "otr connection with %s has been refreshed", u->nick);
+		u->flags = ( u->flags & ~IRC_USER_OTR_TRUSTED ) | IRC_USER_OTR_ENCRYPTED;
+	if(!otr_update_modeflags(irc, u))
+		irc_usermsg(irc, "otr connection with %s has been refreshed", u->nick);
 }
 
@@ -630,6 +642,7 @@
 {
 	struct im_connection *ic = (struct im_connection *)opdata;
-
-	return peernick(ic->irc, account, protocol);
+	irc_t *irc = ic->bee->ui_data;
+
+	return peernick(irc, account, protocol);
 }
 
@@ -639,8 +652,8 @@
 void cmd_otr_disconnect(irc_t *irc, char **args)
 {
-	user_t *u;
-
-	u = user_find(irc, args[1]);
-	if(!u || !u->ic) {
+	irc_user_t *u;
+
+	u = irc_user_by_name(irc, args[1]);
+	if(!u || !u->bu || !u->bu->ic) {
 		irc_usermsg(irc, "%s: unknown user", args[1]);
 		return;
@@ -648,15 +661,15 @@
 	
 	otrl_message_disconnect(irc->otr->us, &global.otr_ops,
-		u->ic, u->ic->acc->user, u->ic->acc->prpl->name, u->handle);
+		u->bu->ic, u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, u->bu->handle);
 	
 	/* for some reason, libotr (3.1.0) doesn't do this itself: */
-	if(u->encrypted) {
+	if(u->flags & IRC_USER_OTR_ENCRYPTED) {
 		ConnContext *ctx;
-		ctx = otrl_context_find(irc->otr->us, u->handle, u->ic->acc->user,
-			u->ic->acc->prpl->name, 0, NULL, NULL, NULL);
+		ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user,
+			u->bu->ic->acc->prpl->name, 0, NULL, NULL, NULL);
 		if(ctx)
-			op_gone_insecure(u->ic, ctx);
+			op_gone_insecure(u->bu->ic, ctx);
 		else /* huh? */
-			u->encrypted = 0;
+			u->flags &= ( IRC_USER_OTR_ENCRYPTED | IRC_USER_OTR_TRUSTED );
 	}
 }
@@ -664,36 +677,36 @@
 void cmd_otr_connect(irc_t *irc, char **args)
 {
-	user_t *u;
-
-	u = user_find(irc, args[1]);
-	if(!u || !u->ic) {
+	irc_user_t *u;
+
+	u = irc_user_by_name(irc, args[1]);
+	if(!u || !u->bu || !u->bu->ic) {
 		irc_usermsg(irc, "%s: unknown user", args[1]);
 		return;
 	}
-	if(!u->online) {
+	if(!(u->bu->flags & BEE_USER_ONLINE)) {
 		irc_usermsg(irc, "%s is offline", args[1]);
 		return;
 	}
 	
-	imc_buddy_msg(u->ic, u->handle, "?OTR?", 0);
+	/* TODO(wilmer): imc_buddy_msg(u->bu->ic, u->bu->handle, "?OTR?", 0); */
 }
 
 void cmd_otr_smp(irc_t *irc, char **args)
 {
-	user_t *u;
+	irc_user_t *u;
 	ConnContext *ctx;
 	
-	u = user_find(irc, args[1]);
-	if(!u || !u->ic) {
+	u = irc_user_by_name(irc, args[1]);
+	if(!u || !u->bu || !u->bu->ic) {
 		irc_usermsg(irc, "%s: unknown user", args[1]);
 		return;
 	}
-	if(!u->online) {
+	if(!(u->bu->flags & BEE_USER_ONLINE)) {
 		irc_usermsg(irc, "%s is offline", args[1]);
 		return;
 	}
 	
-	ctx = otrl_context_find(irc->otr->us, u->handle,
-		u->ic->acc->user, u->ic->acc->prpl->name, 1, NULL, NULL, NULL);
+	ctx = otrl_context_find(irc->otr->us, u->bu->handle,
+		u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, 1, NULL, NULL, NULL);
 	if(!ctx) {
 		/* huh? out of memory or what? */
@@ -705,5 +718,5 @@
 			"SMP already in phase %d, sending abort before reinitiating",
 			ctx->smstate->nextExpected+1);
-		otrl_message_abort_smp(irc->otr->us, &global.otr_ops, u->ic, ctx);
+		otrl_message_abort_smp(irc->otr->us, &global.otr_ops, u->bu->ic, ctx);
 		otrl_sm_state_free(ctx->smstate);
 	}
@@ -714,5 +727,5 @@
 		irc_usermsg(irc, "smp: initiating with %s...", u->nick);
 		otrl_message_initiate_smp(irc->otr->us, &global.otr_ops,
-			u->ic, ctx, (unsigned char *)args[2], strlen(args[2]));
+			u->bu->ic, ctx, (unsigned char *)args[2], strlen(args[2]));
 		/* smp is now in EXPECT2 */
 	} else {
@@ -721,5 +734,5 @@
 		irc_usermsg(irc, "smp: responding to %s...", u->nick);
 		otrl_message_respond_smp(irc->otr->us, &global.otr_ops,
-			u->ic, ctx, (unsigned char *)args[2], strlen(args[2]));
+			u->bu->ic, ctx, (unsigned char *)args[2], strlen(args[2]));
 		/* smp is now in EXPECT3 */
 	}
@@ -728,5 +741,5 @@
 void cmd_otr_trust(irc_t *irc, char **args)
 {
-	user_t *u;
+	irc_user_t *u;
 	ConnContext *ctx;
 	unsigned char raw[20];
@@ -734,12 +747,12 @@
 	int i,j;
 	
-	u = user_find(irc, args[1]);
-	if(!u || !u->ic) {
+	u = irc_user_by_name(irc, args[1]);
+	if(!u || !u->bu || !u->bu->ic) {
 		irc_usermsg(irc, "%s: unknown user", args[1]);
 		return;
 	}
 	
-	ctx = otrl_context_find(irc->otr->us, u->handle,
-		u->ic->acc->user, u->ic->acc->prpl->name, 0, NULL, NULL, NULL);
+	ctx = otrl_context_find(irc->otr->us, u->bu->handle,
+		u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, 0, NULL, NULL, NULL);
 	if(!ctx) {
 		irc_usermsg(irc, "%s: no otr context with user", args[1]);
@@ -780,6 +793,6 @@
 		otrl_context_set_trust(fp, trust);
 		irc_usermsg(irc, "fingerprint match, trust set to \"%s\"", trust);
-		if(u->encrypted)
-			u->encrypted = 2;
+		if(u->flags & IRC_USER_OTR_ENCRYPTED)
+			u->flags |= IRC_USER_OTR_TRUSTED;
 		otr_update_modeflags(irc, u);
 	}
@@ -812,12 +825,12 @@
 			}
 		} else {
-			user_t *u = user_find(irc, args[1]);
-			if(!u || !u->ic) {
+			irc_user_t *u = irc_user_by_name(irc, args[1]);
+			if(!u || !u->bu || !u->bu->ic) {
 				irc_usermsg(irc, "%s: unknown user", args[1]);
 				g_free(arg);
 				return;
 			}
-			ctx = otrl_context_find(irc->otr->us, u->handle, u->ic->acc->user,
-				u->ic->acc->prpl->name, 0, NULL, NULL, NULL);
+			ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user,
+				u->bu->ic->acc->prpl->name, 0, NULL, NULL, NULL);
 			if(!ctx) {
 				irc_usermsg(irc, "no otr context with %s", args[1]);
@@ -848,5 +861,5 @@
 	}
 	
-	a = irc->accounts;
+	a = irc->b->accounts;
 	for(i=0; i<n && a; i++, a=a->next);
 	if(!a) {
@@ -862,5 +875,5 @@
 	if(otrl_privkey_find(irc->otr->us, a->user, a->prpl->name)) {
 		char *s = g_strdup_printf("account %d already has a key, replace it?", n);
-		query_add(irc, NULL, s, yes_keygen, NULL, a);
+		query_add(irc, NULL, s, yes_keygen, NULL, NULL, a);
 		g_free(s);
 	} else {
@@ -918,5 +931,5 @@
 	if(!strcmp(args[1], "fingerprint"))
 	{
-		user_t *u;
+		irc_user_t *u;
 		ConnContext *ctx;
 		Fingerprint *fp;
@@ -931,12 +944,12 @@
 		
 		/* TODO: allow context specs ("user/proto/account") in 'otr forget fingerprint'? */
-		u = user_find(irc, args[2]);
-		if(!u || !u->ic) {
+		u = irc_user_by_name(irc, args[2]);
+		if(!u || !u->bu || !u->bu->ic) {
 			irc_usermsg(irc, "%s: unknown user", args[2]);
 			return;
 		}
 		
-		ctx = otrl_context_find(irc->otr->us, u->handle, u->ic->acc->user,
-			u->ic->acc->prpl->name, 0, NULL, NULL, NULL);
+		ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user,
+			u->bu->ic->acc->prpl->name, 0, NULL, NULL, NULL);
 		if(!ctx) {
 			irc_usermsg(irc, "no otr context with %s", args[2]);
@@ -962,5 +975,5 @@
 		p->fst = irc;
 		p->snd = fp;
-		query_add(irc, NULL, s, yes_forget_fingerprint, NULL, p);
+		query_add(irc, NULL, s, yes_forget_fingerprint, NULL, NULL, p);
 		g_free(s);
 	}
@@ -968,5 +981,5 @@
 	else if(!strcmp(args[1], "context"))
 	{
-		user_t *u;
+		irc_user_t *u;
 		ConnContext *ctx;
 		char *s;
@@ -974,12 +987,12 @@
 		
 		/* TODO: allow context specs ("user/proto/account") in 'otr forget contex'? */
-		u = user_find(irc, args[2]);
-		if(!u || !u->ic) {
+		u = irc_user_by_name(irc, args[2]);
+		if(!u || !u->bu || !u->bu->ic) {
 			irc_usermsg(irc, "%s: unknown user", args[2]);
 			return;
 		}
 		
-		ctx = otrl_context_find(irc->otr->us, u->handle, u->ic->acc->user,
-			u->ic->acc->prpl->name, 0, NULL, NULL, NULL);
+		ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user,
+			u->bu->ic->acc->prpl->name, 0, NULL, NULL, NULL);
 		if(!ctx) {
 			irc_usermsg(irc, "no otr context with %s", args[2]);
@@ -998,5 +1011,5 @@
 		p->fst = irc;
 		p->snd = ctx;
-		query_add(irc, NULL, s, yes_forget_context, NULL, p);
+		query_add(irc, NULL, s, yes_forget_context, NULL, NULL, p);
 		g_free(s);
 	}
@@ -1015,5 +1028,5 @@
 		s = g_strdup_printf("about to forget the private key for %s/%s, are you sure?",
 			key->accountname, key->protocol);
-		query_add(irc, NULL, s, yes_forget_key, NULL, key);
+		query_add(irc, NULL, s, yes_forget_key, NULL, NULL, key);
 		g_free(s);
 	}
@@ -1032,5 +1045,5 @@
 void otr_handle_smp(struct im_connection *ic, const char *handle, OtrlTLV *tlvs)
 {
-	irc_t *irc = ic->irc;
+	irc_t *irc = ic->bee->ui_data;
 	OtrlUserState us = irc->otr->us;
 	OtrlMessageAppOps *ops = &global.otr_ops;
@@ -1038,8 +1051,9 @@
 	ConnContext *context;
 	NextExpectedSMP nextMsg;
-	user_t *u;
-
-	u = user_findhandle(ic, handle);
-	if(!u) return;
+	irc_user_t *u;
+	bee_user_t *bu;
+
+	bu = bee_user_by_handle(ic->bee, ic, handle);
+	if(!bu || !(u = bu->ui_data)) return;
 	context = otrl_context_find(us, handle,
 		ic->acc->user, ic->acc->prpl->name, 1, NULL, NULL, NULL);
@@ -1054,5 +1068,5 @@
 		if (nextMsg != OTRL_SMP_EXPECT1) {
 			irc_usermsg(irc, "smp %s: spurious SMP1 received, aborting", u->nick);
-			otrl_message_abort_smp(us, ops, u->ic, context);
+			otrl_message_abort_smp(us, ops, u->bu->ic, context);
 			otrl_sm_state_free(context->smstate);
 		} else {
@@ -1067,5 +1081,5 @@
 		if (nextMsg != OTRL_SMP_EXPECT2) {
 			irc_usermsg(irc, "smp %s: spurious SMP2 received, aborting", u->nick);
-			otrl_message_abort_smp(us, ops, u->ic, context);
+			otrl_message_abort_smp(us, ops, u->bu->ic, context);
 			otrl_sm_state_free(context->smstate);
 		} else {
@@ -1078,5 +1092,5 @@
 		if (nextMsg != OTRL_SMP_EXPECT3) {
 			irc_usermsg(irc, "smp %s: spurious SMP3 received, aborting", u->nick);
-			otrl_message_abort_smp(us, ops, u->ic, context);
+			otrl_message_abort_smp(us, ops, u->bu->ic, context);
 			otrl_sm_state_free(context->smstate);
 		} else {
@@ -1098,5 +1112,5 @@
 		if (nextMsg != OTRL_SMP_EXPECT4) {
 			irc_usermsg(irc, "smp %s: spurious SMP4 received, aborting", u->nick);
-			otrl_message_abort_smp(us, ops, u->ic, context);
+			otrl_message_abort_smp(us, ops, u->bu->ic, context);
 			otrl_sm_state_free(context->smstate);
 		} else {
@@ -1143,16 +1157,17 @@
 }
 
-user_t *peeruser(irc_t *irc, const char *handle, const char *protocol)
-{
-	user_t *u;
-	
-	for(u=irc->users; u; u=u->next) {
+irc_user_t *peeruser(irc_t *irc, const char *handle, const char *protocol)
+{
+	GSList *l;
+	
+	for(l=irc->b->users; l; l = l->next) {
+		bee_user_t *bu = l->data;
 		struct prpl *prpl;
-		if(!u->ic || !u->handle)
+		if(!bu->ui_data || !bu->ic || !bu->handle)
 			continue;
-		prpl = u->ic->acc->prpl;
+		prpl = bu->ic->acc->prpl;
 		if(strcmp(prpl->name, protocol) == 0
-			&& prpl->handle_cmp(u->handle, handle) == 0) {
-			return u;
+			&& prpl->handle_cmp(bu->handle, handle) == 0) {
+			return bu->ui_data;
 		}
 	}
@@ -1179,5 +1194,5 @@
 	static char fallback[512];
 	
-	user_t *u = peeruser(irc, handle, protocol);
+	irc_user_t *u = peeruser(irc, handle, protocol);
 	if(u) {
 		return u->nick;
@@ -1188,62 +1203,6 @@
 }
 
-int otr_update_modeflags(irc_t *irc, user_t *u)
-{
-	char *vb = set_getstr(&irc->set, "voice_buddies");
-	char *hb = set_getstr(&irc->set, "halfop_buddies");
-	char *ob = set_getstr(&irc->set, "op_buddies");
-	int encrypted = u->encrypted;
-	int trusted = u->encrypted > 1;
-	char flags[7];
-	int nflags=0;
-	char *p = flags;
-	char *from;
-	int i;
-	
-	if(!strcmp(vb, "encrypted")) {
-		*(p++) = encrypted ? '+' : '-';
-		*(p++) = 'v';
-		nflags++;
-	} else if(!strcmp(vb, "trusted")) {
-		*(p++) = trusted ? '+' : '-';
-		*(p++) = 'v';
-		nflags++;
-	}
-	if(!strcmp(hb, "encrypted")) {
-		*(p++) = encrypted ? '+' : '-';
-		*(p++) = 'h';
-		nflags++;
-	} else if(!strcmp(hb, "trusted")) {
-		*(p++) = trusted ? '+' : '-';
-		*(p++) = 'h';
-		nflags++;
-	}
-	if(!strcmp(ob, "encrypted")) {
-		*(p++) = encrypted ? '+' : '-';
-		*(p++) = 'o';
-		nflags++;
-	} else if(!strcmp(ob, "trusted")) {
-		*(p++) = trusted ? '+' : '-';
-		*(p++) = 'o';
-		nflags++;
-	}
-	*p = '\0';
-	
-	p = g_malloc(nflags * (strlen(u->nick)+1) + 1);
-	*p = '\0';
-	if(!p)
-		return 0;
-	for(i=0; i<nflags; i++) {
-		strcat(p, " ");
-		strcat(p, u->nick);
-	}
-	if(set_getbool(&irc->set, "simulate_netsplit"))
-		from = g_strdup(irc->myhost);
-	else
-		from = g_strdup_printf("%s!%s@%s", irc->mynick, irc->mynick, irc->myhost);
-	irc_write(irc, ":%s MODE %s %s%s", from, irc->channel, flags, p);
-	g_free(from);
-	g_free(p);
-		
+int otr_update_modeflags(irc_t *irc, irc_user_t *u)
+{
 	return 1;
 }
@@ -1450,5 +1409,5 @@
 	irc_usermsg(irc, "\x1f" "connection contexts:\x1f (bold=currently encrypted)");
 	for(ctx=irc->otr->us->context_root; ctx; ctx=ctx->next) {\
-		user_t *u;
+		irc_user_t *u;
 		char *userstring;
 		
@@ -1576,5 +1535,5 @@
 		irc->otr->sent_protocol = NULL;
 		irc->otr->todo = NULL;
-		b_input_add(from[0], GAIM_INPUT_READ, keygen_finish_handler, irc);
+		b_input_add(from[0], B_EV_IO_READ, keygen_finish_handler, irc);
 	}
 	
@@ -1641,5 +1600,5 @@
 	irc_usermsg(irc, "%s", msg);
 	if(filename[0]) {
-		char *kf = g_strdup_printf("%s%s.otr_keys", global.conf->configdir, irc->nick);
+		char *kf = g_strdup_printf("%s%s.otr_keys", global.conf->configdir, irc->user->nick);
 		char *tmp = g_strdup_printf("%s.new", kf);
 		copyfile(filename, tmp);
@@ -1709,13 +1668,14 @@
 {
 	account_t *acc = (account_t *)data;
-	
-	if(keygen_in_progress(acc->irc, acc->user, acc->prpl->name)) {
-		irc_usermsg(acc->irc, "keygen for %s/%s already in progress",
+	irc_t *irc = acc->bee->ui_data;
+	
+	if(keygen_in_progress(irc, acc->user, acc->prpl->name)) {
+		irc_usermsg(irc, "keygen for %s/%s already in progress",
 			acc->user, acc->prpl->name);
 	} else {
-		irc_usermsg(acc->irc, "starting background keygen for %s/%s",
+		irc_usermsg(irc, "starting background keygen for %s/%s",
 			acc->user, acc->prpl->name);
-		irc_usermsg(acc->irc, "you will be notified when it completes");
-		otr_keygen(acc->irc, acc->user, acc->prpl->name);
+		irc_usermsg(irc, "you will be notified when it completes");
+		otr_keygen(irc, acc->user, acc->prpl->name);
 	}
 }
