Index: protocols/jabber/Makefile
===================================================================
--- protocols/jabber/Makefile	(revision 59c1fe7fce52811f6cd99345373bee26a478c27b)
+++ protocols/jabber/Makefile	(revision 0e4c3dd2cd5495d2f5d1247471c348e7676612b4)
@@ -13,5 +13,5 @@
 
 # [SH] Program variables
-objects = conference.o io.o iq.o jabber.o jabber_util.o message.o presence.o s5bytestream.o sasl.o si.o
+objects = conference.o io.o iq.o jabber.o jabber_util.o message.o presence.o s5bytestream.o sasl.o si.o hipchat.o
 
 LFLAGS += -r
Index: protocols/jabber/hipchat.c
===================================================================
--- protocols/jabber/hipchat.c	(revision 0e4c3dd2cd5495d2f5d1247471c348e7676612b4)
+++ protocols/jabber/hipchat.c	(revision 0e4c3dd2cd5495d2f5d1247471c348e7676612b4)
@@ -0,0 +1,181 @@
+/***************************************************************************\
+*                                                                           *
+*  BitlBee - An IRC to IM gateway                                           *
+*  Jabber module - HipChat specific functions                               *
+*                                                                           *
+*  Copyright 2015 Xamarin Inc                                               *
+*                                                                           *
+*  This program is free software; you can redistribute it and/or modify     *
+*  it under the terms of the GNU General Public License as published by     *
+*  the Free Software Foundation; either version 2 of the License, or        *
+*  (at your option) any later version.                                      *
+*                                                                           *
+*  This program is distributed in the hope that it will be useful,          *
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
+*  GNU General Public License for more details.                             *
+*                                                                           *
+*  You should have received a copy of the GNU General Public License along  *
+*  with this program; if not, write to the Free Software Foundation, Inc.,  *
+*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              *
+*                                                                           *
+\***************************************************************************/
+
+#include "jabber.h"
+
+xt_status hipchat_handle_success(struct im_connection *ic, struct xt_node *node)
+{
+	struct jabber_data *jd = ic->proto_data;
+	char *sep, *jid, *muc_host;
+
+	jid = xt_find_attr(node, "jid");
+	muc_host = xt_find_attr(node, "muc_host");
+
+	sep = strchr(jid, '/');
+	if (sep) {
+		*sep = '\0';
+	}
+
+	jabber_set_me(ic, jid);
+	imcb_log(ic, "Setting Hipchat JID to %s", jid);
+
+	if (sep) {
+		*sep = '/';
+	}
+
+	/* Hipchat's auth doesn't expect a restart here */
+	jd->flags &= ~JFLAG_STREAM_RESTART;
+
+	if (!jabber_get_roster(ic)) {
+		return XT_ABORT;
+	}
+	if (!jabber_iq_disco_server(ic)) {
+		return XT_ABORT;
+	}
+	if (!jabber_get_hipchat_profile(ic)) {
+		return XT_ABORT;
+	}
+	if (!jabber_iq_disco_muc(ic, muc_host)) {
+		return XT_ABORT;
+	}
+
+	return XT_HANDLED;
+}
+
+int jabber_get_hipchat_profile(struct im_connection *ic)
+{
+	struct jabber_data *jd = ic->proto_data;
+	struct xt_node *node;
+	int st;
+
+	imcb_log(ic, "Fetching hipchat profile for %s", jd->me);
+
+	node = xt_new_node("query", NULL, NULL);
+	xt_add_attr(node, "xmlns", XMLNS_HIPCHAT_PROFILE);
+	node = jabber_make_packet("iq", "get", jd->me, node);
+
+	jabber_cache_add(ic, node, jabber_parse_hipchat_profile);
+	st = jabber_write_packet(ic, node);
+
+	return st;
+}
+
+xt_status jabber_parse_hipchat_profile(struct im_connection *ic, struct xt_node *node, struct xt_node *orig)
+{
+	struct xt_node *query, *name_node;
+
+	//char *name;
+
+	if (!(query = xt_find_node(node->children, "query"))) {
+		imcb_log(ic, "Warning: Received NULL profile packet");
+		return XT_ABORT;
+	}
+
+	name_node = xt_find_node(query->children, "name");
+	if (!name_node) {
+		imcb_log(ic, "Warning: Can't find real name in profile. Joining groupchats will not be possible.");
+		return XT_ABORT;
+	}
+
+	set_setstr(&ic->acc->set, "display_name", name_node->text);
+	return XT_HANDLED;
+
+}
+
+int jabber_iq_disco_muc(struct im_connection *ic, char *muc_server)
+{
+	struct xt_node *node;
+	int st;
+
+	imcb_log(ic, "Fetching MUC list");
+
+	node = xt_new_node("query", NULL, NULL);
+	xt_add_attr(node, "xmlns", XMLNS_DISCO_ITEMS);
+	node = jabber_make_packet("iq", "get", muc_server, node);
+
+	jabber_cache_add(ic, node, jabber_parse_muc_list);
+	st = jabber_write_packet(ic, node);
+
+	return st;
+}
+
+xt_status jabber_parse_muc_list(struct im_connection *ic, struct xt_node *node, struct xt_node *orig)
+{
+	struct xt_node *query, *c;
+
+	if (!(query = xt_find_node(node->children, "query"))) {
+		imcb_log(ic, "Warning: Received NULL MUC list packet");
+		return XT_HANDLED;
+	}
+
+	c = query->children;
+	while ((c = xt_find_node(c, "item"))) {
+		struct xt_node *c2;
+		struct groupchat *gc;
+		struct irc_channel *ircc;
+		//char *participants = NULL;
+		char *topic = NULL;
+		gboolean new_room = FALSE;
+		char *jid = xt_find_attr(c, "jid");
+		char *name = xt_find_attr(c, "name");
+
+		imcb_log(ic, "Debug: adding MUC to channel list: %s - '%s'", jid, name);
+
+		c2 = xt_find_node_by_attr(c->children, "x", "xmlns", XMLNS_HIPCHAT_MUC);
+
+		if (c2) {
+			struct xt_node *node;
+			/*
+			if ( ( node = xt_find_node( c2->children, "num_participants" ) ) ) {
+			        participants = node->text;
+			}
+			*/
+			if ((node = xt_find_node(c2->children, "topic"))) {
+				topic = node->text;
+			}
+		}
+
+		gc = bee_chat_by_title(ic->bee, ic, jid);
+		if (!gc) {
+			gc = imcb_chat_new(ic, jid);
+			new_room = TRUE;
+		}
+		imcb_chat_name_hint(gc, name);
+		imcb_chat_topic(gc, NULL, topic, 0);
+
+		ircc = gc->ui_data;
+		set_setstr(&ircc->set, "account", ic->acc->tag);
+		set_setstr(&ircc->set, "room", jid);
+		set_setstr(&ircc->set, "chat_type", "room");
+
+		if (new_room) {
+			/* This cleans everything but leaves the irc channel around,
+			 * since it just graduated to a room.*/
+			imcb_chat_free(gc);
+		}
+
+		c = c->next;
+	}
+	return XT_HANDLED;
+
+}
Index: protocols/jabber/iq.c
===================================================================
--- protocols/jabber/iq.c	(revision 59c1fe7fce52811f6cd99345373bee26a478c27b)
+++ protocols/jabber/iq.c	(revision 0e4c3dd2cd5495d2f5d1247471c348e7676612b4)
@@ -27,5 +27,4 @@
 static xt_status jabber_parse_roster(struct im_connection *ic, struct xt_node *node, struct xt_node *orig);
 static xt_status jabber_iq_display_vcard(struct im_connection *ic, struct xt_node *node, struct xt_node *orig);
-static int jabber_iq_disco_server(struct im_connection *ic);
 
 xt_status jabber_pkt_iq(struct xt_node *node, gpointer data)
@@ -861,5 +860,5 @@
                                                  struct xt_node *node, struct xt_node *orig);
 
-static int jabber_iq_disco_server(struct im_connection *ic)
+int jabber_iq_disco_server(struct im_connection *ic)
 {
 	struct xt_node *node, *iq;
Index: protocols/jabber/jabber.h
===================================================================
--- protocols/jabber/jabber.h	(revision 59c1fe7fce52811f6cd99345373bee26a478c27b)
+++ protocols/jabber/jabber.h	(revision 0e4c3dd2cd5495d2f5d1247471c348e7676612b4)
@@ -250,4 +250,9 @@
 #define XMLNS_IBB          "http://jabber.org/protocol/ibb"                      /* XEP-0047 */
 
+/* Hipchat protocol extensions*/
+#define XMLNS_HIPCHAT         "http://hipchat.com"
+#define XMLNS_HIPCHAT_PROFILE "http://hipchat.com/protocol/profile"
+#define XMLNS_HIPCHAT_MUC     "http://hipchat.com/protocol/muc#room"
+
 /* jabber.c */
 void jabber_connect(struct im_connection *ic);
@@ -259,4 +264,5 @@
 int jabber_get_roster(struct im_connection *ic);
 int jabber_get_vcard(struct im_connection *ic, char *bare_jid);
+int jabber_iq_disco_muc(struct im_connection *ic, char *muc_server);
 int jabber_add_to_roster(struct im_connection *ic, const char *handle, const char *name, const char *group);
 int jabber_remove_from_roster(struct im_connection *ic, char *handle);
@@ -264,4 +270,5 @@
 xt_status jabber_iq_query_server(struct im_connection *ic, char *jid, char *xmlns);
 void jabber_iq_version_send(struct im_connection *ic, struct jabber_buddy *bud, void *data);
+int jabber_iq_disco_server(struct im_connection *ic);
 
 /* si.c */
@@ -357,3 +364,9 @@
 void jabber_chat_invite(struct groupchat *c, char *who, char *message);
 
+/* hipchat.c */
+int jabber_get_hipchat_profile(struct im_connection *ic);
+xt_status jabber_parse_hipchat_profile(struct im_connection *ic, struct xt_node *node, struct xt_node *orig);
+xt_status hipchat_handle_success(struct im_connection *ic, struct xt_node *node);
+xt_status jabber_parse_muc_list(struct im_connection *ic, struct xt_node *node, struct xt_node *orig);
+
 #endif
Index: protocols/jabber/sasl.c
===================================================================
--- protocols/jabber/sasl.c	(revision 59c1fe7fce52811f6cd99345373bee26a478c27b)
+++ protocols/jabber/sasl.c	(revision 0e4c3dd2cd5495d2f5d1247471c348e7676612b4)
@@ -55,5 +55,5 @@
 	char *s;
 	int sup_plain = 0, sup_digest = 0, sup_gtalk = 0, sup_fb = 0;
-	int want_oauth = FALSE;
+	int want_oauth = FALSE, want_hipchat = FALSE;
 	GString *mechs;
 
@@ -75,4 +75,5 @@
 
 	want_oauth = set_getbool(&ic->acc->set, "oauth");
+	want_hipchat = (jd->subproto == JSUB_HIPCHAT);
 
 	mechs = g_string_new("");
@@ -111,5 +112,9 @@
 
 	reply = xt_new_node("auth", NULL, NULL);
-	xt_add_attr(reply, "xmlns", XMLNS_SASL);
+	if (!want_hipchat) {
+		xt_add_attr(reply, "xmlns", XMLNS_SASL);
+	} else {
+		xt_add_attr(reply, "xmlns", XMLNS_HIPCHAT);
+	}
 
 	if (sup_gtalk && want_oauth) {
@@ -143,13 +148,31 @@
 	} else if (sup_plain) {
 		int len;
-
-		xt_add_attr(reply, "mechanism", "PLAIN");
+		GString *gs;
+		char *username;
+
+		if (!want_hipchat) {
+			xt_add_attr(reply, "mechanism", "PLAIN");
+			username = jd->username;
+		} else {
+			username = jd->me;
+		}
+
+		/* set an arbitrary initial size to avoid reallocations */
+		gs = g_string_sized_new(128);
 
 		/* With SASL PLAIN in XMPP, the text should be b64(\0user\0pass) */
-		len = strlen(jd->username) + strlen(ic->acc->pass) + 2;
-		s = g_malloc(len + 1);
-		s[0] = 0;
-		strcpy(s + 1, jd->username);
-		strcpy(s + 2 + strlen(jd->username), ic->acc->pass);
+		g_string_append_c(gs, '\0');
+		g_string_append(gs, username);
+		g_string_append_c(gs, '\0');
+		g_string_append(gs, ic->acc->pass);
+		if (want_hipchat) {
+			/* Hipchat's variation adds \0resource at the end */
+			g_string_append_c(gs, '\0');
+			g_string_append(gs, set_getstr(&ic->acc->set, "resource"));
+		}
+
+		len = gs->len;
+		s = g_string_free(gs, FALSE);
+
 		reply->text = base64_encode((unsigned char *) s, len);
 		reply->text_len = strlen(reply->text);
@@ -397,4 +420,8 @@
 		imcb_log(ic, "Authentication finished");
 		jd->flags |= JFLAG_AUTHENTICATED | JFLAG_STREAM_RESTART;
+
+		if (jd->subproto == JSUB_HIPCHAT) {
+			return hipchat_handle_success(ic, node);
+		}
 	} else if (strcmp(node->name, "failure") == 0) {
 		imcb_error(ic, "Authentication failure");
