From 67d19a226d2db8f79cc1a93f92913424245b3dd5 Mon Sep 17 00:00:00 2001
From: Yann Leboulanger <asterix@lagaule.org>
Date: Mon, 3 Nov 2008 14:25:08 +0000
Subject: [PATCH] handle mood in messages, send mood in message if pep is not
 supported on server. Fixes #4449

---
 src/common/connection.py          |  9 ++++++-
 src/common/connection_handlers.py | 12 +++++++++
 src/common/pep.py                 | 41 +++++++++++++++++--------------
 src/dialogs.py                    | 15 ++++++++---
 src/roster_window.py              | 18 +++++++-------
 5 files changed, 63 insertions(+), 32 deletions(-)

diff --git a/src/common/connection.py b/src/common/connection.py
index 84c48f9ac2..b3d16dca2e 100644
--- a/src/common/connection.py
+++ b/src/common/connection.py
@@ -1132,9 +1132,16 @@ class Connection(ConnectionHandlers):
 
 		# XEP-0172: user_nickname
 		if user_nick:
-			msg_iq.setTag('nick', namespace = common.xmpp.NS_NICK).setData(
+			msg_iq.setTag('nick', namespace=common.xmpp.NS_NICK).setData(
 				user_nick)
 
+		# XEP-0107: User Mood
+		if 'mood' in self.mood and not self.pep_supported:
+			mood_iq = msg_iq.setTag('mood', namespace=common.xmpp.NS_MOOD)
+			mood_iq.setTag(self.mood['mood'])
+			if 'text' in self.mood and self.mood['text']:
+				mood_iq.setTagData('text', self.mood['text'])
+
 		# TODO: We might want to write a function so we don't need to
 		#	reproduce that ugly if somewhere else.
 		if resource:
diff --git a/src/common/connection_handlers.py b/src/common/connection_handlers.py
index 1aa57e95df..652f2a411a 100644
--- a/src/common/connection_handlers.py
+++ b/src/common/connection_handlers.py
@@ -1805,6 +1805,18 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
 			session.control.conv_textview.hide_xep0184_warning(
 				msg.getID())
 
+		# Check mood in message
+		if msg.getTag('mood', namespace=common.xmpp.NS_MOOD):
+			mood_iq = msg.getTag('mood', namespace=common.xmpp.NS_MOOD)
+			mood = None
+			text = None
+			for ch in mood_iq.getChildren():
+				if ch.getName() != 'text':
+					mood = ch.getName()
+				else:
+					text = ch.getData()
+			pep.handle_mood(self.name, jid, mood=mood, text=text, retract=False)
+
 		if encTag and self.USE_GPG:
 			encmsg = encTag.getData()
 
diff --git a/src/common/pep.py b/src/common/pep.py
index 6dc3bfd8c4..c844fbf5a8 100644
--- a/src/common/pep.py
+++ b/src/common/pep.py
@@ -201,45 +201,48 @@ def user_mood(items, name, jid):
 					text = ch.getData()
 	if items.getTag('retract') is not None:
 		retract = True
+	if has_child or retract:
+		handle_mood(name, jid, mood=mood, text=text, retract=retract)
 
-	if jid == gajim.get_jid_from_account(name):
-		acc = gajim.connections[name]
-		if has_child:
+def handle_mood(account, jid, mood=None, text=None, retract=False):
+	if jid == gajim.get_jid_from_account(account):
+		acc = gajim.connections[account]
+		if retract:
 			if 'mood' in acc.mood:
 				del acc.mood['mood']
 			if 'text' in acc.mood:
 				del acc.mood['text']
-			if mood is not None:
-				acc.mood['mood'] = mood
-			if text is not None:
-				acc.mood['text'] = text
-		elif retract:
+		else:
 			if 'mood' in acc.mood:
 				del acc.mood['mood']
 			if 'text' in acc.mood:
 				del acc.mood['text']
+			if mood is not None:
+				acc.mood['mood'] = mood
+			if text is not None:
+				acc.mood['text'] = text
 
 	(user, resource) = gajim.get_room_and_nick_from_fjid(jid)
-	for contact in gajim.contacts.get_contacts(name, user):
-		if has_child:
+	for contact in gajim.contacts.get_contacts(account, user):
+		if retract:
 			if 'mood' in contact.mood:
 				del contact.mood['mood']
 			if 'text' in contact.mood:
 				del contact.mood['text']
-			if mood is not None:
-				contact.mood['mood'] = mood
-			if text is not None:
-				contact.mood['text'] = text
-		elif retract:
+		else:
 			if 'mood' in contact.mood:
 				del contact.mood['mood']
 			if 'text' in contact.mood:
 				del contact.mood['text']
+			if mood is not None:
+				contact.mood['mood'] = mood
+			if text is not None:
+				contact.mood['text'] = text
 
-	if jid == gajim.get_jid_from_account(name):
-		gajim.interface.roster.draw_account(name)
-	gajim.interface.roster.draw_mood(user, name)
-	ctrl = gajim.interface.msg_win_mgr.get_control(user, name)
+	if jid == gajim.get_jid_from_account(account):
+		gajim.interface.roster.draw_account(account)
+	gajim.interface.roster.draw_mood(user, account)
+	ctrl = gajim.interface.msg_win_mgr.get_control(user, account)
 	if ctrl:
 		ctrl.update_mood()
 
diff --git a/src/dialogs.py b/src/dialogs.py
index 485ba838b7..c34c30f46f 100644
--- a/src/dialogs.py
+++ b/src/dialogs.py
@@ -528,10 +528,19 @@ class ChangeMoodDialog:
 	def on_ok_button_clicked(self, widget):
 		'''Return mood and messsage (None if no mood selected)'''
 		message = self.entry.get_text().decode('utf-8')
-		if self.mood is None:
-			pep.user_retract_mood(self.account)
+		if gajim.connections[self.account].pep_supported:
+			if self.mood is None:
+				pep.user_retract_mood(self.account)
+			else:
+				pep.user_send_mood(self.account, self.mood, message)
 		else:
-			pep.user_send_mood(self.account, self.mood, message)
+			# Do as we we sent through pep and got the answer
+			our_jid = gajim.get_jid_from_account(self.account)
+			if self.mood is None:
+				pep.handle_mood(self.account, our_jid, retract=True)
+			else:
+				pep.handle_mood(self.account, our_jid, mood=self.mood,
+					text=message)
 		self.window.destroy()
 
 	def on_cancel_button_clicked(self, widget):
diff --git a/src/roster_window.py b/src/roster_window.py
index 3421303e46..92ffe46c50 100644
--- a/src/roster_window.py
+++ b/src/roster_window.py
@@ -4872,11 +4872,11 @@ class RosterWindow:
 			item.connect('activate', self.change_status, account, 'offline')
 
 			pep_menuitem = xml.get_widget('pep_menuitem')
+			pep_submenu = gtk.Menu()
+			pep_menuitem.set_submenu(pep_submenu)
 			if gajim.connections[account].pep_supported:
 				have_tune = gajim.config.get_per('accounts', account,
 					'publish_tune')
-				pep_submenu = gtk.Menu()
-				pep_menuitem.set_submenu(pep_submenu)
 				item = gtk.CheckMenuItem(_('Publish Tune'))
 				pep_submenu.append(item)
 				if not dbus_support.supported:
@@ -4884,10 +4884,13 @@ class RosterWindow:
 				else:
 					item.set_active(have_tune)
 					item.connect('toggled', self.on_publish_tune_toggled, account)
-				item = gtk.CheckMenuItem(_('Mood'))
-				pep_submenu.append(item)
-				item.set_active(len(gajim.connections[account].mood) > 0)
-				item.connect('activate', self.on_change_mood_activate, account)
+
+			item = gtk.CheckMenuItem(_('Mood'))
+			pep_submenu.append(item)
+			item.set_active(len(gajim.connections[account].mood) > 0)
+			item.connect('activate', self.on_change_mood_activate, account)
+
+			if gajim.connections[account].pep_supported:
 				item = gtk.CheckMenuItem(_('Activity'))
 				pep_submenu.append(item)
 				item.set_active(len(gajim.connections[account].activity) > 0)
@@ -4904,9 +4907,6 @@ class RosterWindow:
 					gtk.ICON_SIZE_MENU)
 				pep_config.set_image(img)
 
-			else:
-				pep_menuitem.set_sensitive(False)
-
 			if not gajim.connections[account].gmail_url:
 				open_gmail_inbox_menuitem.set_no_show_all(True)
 				open_gmail_inbox_menuitem.hide()
-- 
GitLab