Index: otr.c
===================================================================
--- otr.c	(revision 69ef042b64ac1cb2f771d3ae02f27a7e64951222)
+++ otr.c	(revision f1cf01c5eb91fdd3effe04a27cdc5a268ff5ee66)
@@ -8,5 +8,5 @@
   OTR support (cf. http://www.cypherpunks.ca/otr/)
   
-  (c) 2008-2010 Sven Moritz Hallberg <pesco@khjk.org>
+  (c) 2008-2011 Sven Moritz Hallberg <pesco@khjk.org>
   (c) 2008 funded by stonedcoder.org
     
@@ -162,4 +162,7 @@
 void otr_smp_or_smpq(irc_t *irc, const char *nick, const char *question,
 		const char *secret);
+
+/* update flags within the irc_user structure to reflect OTR status of context */
+void otr_update_uflags(ConnContext *context, irc_user_t *u);
 
 /* update op/voice flag of given user according to encryption state and settings
@@ -237,4 +240,6 @@
 	l = g_slist_prepend( l, "always" );
 	s->eval_data = l;
+
+	s = set_add( &irc->b->set, "otr_does_html", "true", set_eval_bool, irc );
 	
 	return TRUE;
@@ -385,24 +390,36 @@
 		ConnContext *context = otrl_context_find(irc->otr->us, iu->bu->handle,
 			ic->acc->user, ic->acc->prpl->name, 0, NULL, NULL, NULL);
-		if(context && context->msgstate == OTRL_MSGSTATE_ENCRYPTED &&
-		   set_getbool(&ic->bee->set, "otr_color_encrypted")) {
-			/* color according to f'print trust */
-			int color;
-			const char *trust = context->active_fingerprint->trust;
-			if(trust && trust[0] != '\0')
-				color=3;   /* green */
-			else
-				color=5;   /* red */
-
-			if(newmsg[0] == ',') {
-				/* could be a problem with the color code */
-				/* insert a space between color spec and message */
-				colormsg = g_strdup_printf("\x03%.2d %s\x0F", color, newmsg);
-			} else {
-				colormsg = g_strdup_printf("\x03%.2d%s\x0F", color, newmsg);
+
+		if(context && context->msgstate == OTRL_MSGSTATE_ENCRYPTED) {
+			/* HTML decoding */
+			/* perform any necessary stripping that the top level would miss */
+			if(set_getbool(&ic->bee->set, "otr_does_html") &&
+			   !(ic->flags & OPT_DOES_HTML) &&
+			   set_getbool(&ic->bee->set, "strip_html")) {
+				strip_html(newmsg);
+			}
+
+			/* coloring */
+			if(set_getbool(&ic->bee->set, "otr_color_encrypted")) {
+				/* color according to f'print trust */
+				int color;
+				const char *trust = context->active_fingerprint->trust;
+				if(trust && trust[0] != '\0')
+					color=3;   /* green */
+				else
+					color=5;   /* red */
+
+				if(newmsg[0] == ',') {
+					/* could be a problem with the color code */
+					/* insert a space between color spec and message */
+					colormsg = g_strdup_printf("\x03%.2d %s\x0F", color, newmsg);
+				} else {
+					colormsg = g_strdup_printf("\x03%.2d%s\x0F", color, newmsg);
+				}
 			}
 		} else {
 			colormsg = g_strdup(newmsg);
 		}
+
 		otrl_message_free(newmsg);
 		return colormsg;
@@ -421,4 +438,11 @@
 	if(ic->acc->prpl->options & OPT_NOOTR) {
 		return msg;
+	}
+
+	/* HTML encoding */
+	/* consider OTR plaintext to be HTML if otr_does_html is set */
+	if(set_getbool(&ic->bee->set, "otr_does_html") &&
+	   (g_strncasecmp(msg, "<html>", 6) != 0)) {
+		msg = escape_html(msg);
 	}
 	
@@ -608,5 +632,4 @@
 	irc_user_t *u;
 	irc_t *irc = ic->bee->ui_data;
-	const char *trust;
 
 	u = peeruser(irc, context->username, context->protocol);
@@ -618,11 +641,9 @@
 	}
 	
-	trust = context->active_fingerprint->trust;
-	if(trust && trust[0])
-		u->flags |= IRC_USER_OTR_ENCRYPTED | IRC_USER_OTR_TRUSTED;
-	else
-		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);
+	otr_update_uflags(context, u);
+	if(!otr_update_modeflags(irc, u)) {
+		char *trust = u->flags & IRC_USER_OTR_TRUSTED ? "trusted" : "untrusted!";
+		irc_usermsg(irc, "conversation with %s is now off the record (%s)", u->nick, trust);
+	}
 }
 
@@ -641,5 +662,5 @@
 		return;
 	}
-	u->flags &= ~( IRC_USER_OTR_ENCRYPTED | IRC_USER_OTR_TRUSTED );
+	otr_update_uflags(context, u);
 	if(!otr_update_modeflags(irc, u))
 		irc_usermsg(irc, "conversation with %s is now in the clear", u->nick);
@@ -660,10 +681,10 @@
 		return;
 	}
-	if(context->active_fingerprint->trust[0])
-		u->flags |= IRC_USER_OTR_ENCRYPTED | IRC_USER_OTR_TRUSTED;
-	else
-		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);
+
+	otr_update_uflags(context, u);
+	if(!otr_update_modeflags(irc, u)) {
+		char *trust = u->flags & IRC_USER_OTR_TRUSTED ? "trusted" : "untrusted!";
+		irc_usermsg(irc, "otr connection with %s has been refreshed (%s)", u->nick, trust);
+	}
 }
 
@@ -1312,7 +1333,24 @@
 }
 
+void otr_update_uflags(ConnContext *context, irc_user_t *u)
+{
+	const char *trust;
+
+	if(context->active_fingerprint) {
+		u->flags |= IRC_USER_OTR_ENCRYPTED;
+
+		trust = context->active_fingerprint->trust;
+		if(trust && trust[0])
+			u->flags |= IRC_USER_OTR_TRUSTED;
+		else
+			u->flags &= ~IRC_USER_OTR_TRUSTED;
+	} else {
+		u->flags &= ~IRC_USER_OTR_ENCRYPTED;
+	}
+}
+
 int otr_update_modeflags(irc_t *irc, irc_user_t *u)
 {
-	return 1;
+	return 0;
 }
 
