From 12bb72059f67c82b9cc7e6a850fe02ffebaa6ba8 Mon Sep 17 00:00:00 2001
From: Yann Leboulanger <asterix@lagaule.org>
Date: Sat, 23 Jun 2007 19:44:09 +0000
Subject: [PATCH] ability to forward unread messages through adhoc commands.
 fixes #1910

---
 src/common/commands.py            | 32 ++++++++++++++++++++++++++-
 src/common/connection.py          | 36 ++++++++++++++++++-------------
 src/common/connection_handlers.py |  6 ++++++
 3 files changed, 58 insertions(+), 16 deletions(-)

diff --git a/src/common/commands.py b/src/common/commands.py
index 4fdb6d4d72..8020958730 100644
--- a/src/common/commands.py
+++ b/src/common/commands.py
@@ -236,12 +236,42 @@ class LeaveGroupchatsCommand(AdHocCommand):
 		return False
 
 
+class ForwardMessagesCommand(AdHocCommand):
+	# http://www.xmpp.org/extensions/xep-0146.html#forward
+	commandnode = 'forward-messages'
+	commandname = _('Forward unread messages')
+
+	@staticmethod
+	def isVisibleFor(samejid):
+		''' Change status is visible only if the entity has the same bare jid. '''
+		return samejid
+
+	def execute(self, request):
+		account = self.connection.name
+		# Forward messages
+		events = gajim.events.get_events(account, types=['chat', 'normal'])
+		j, resource = gajim.get_room_and_nick_from_fjid(self.jid)
+		for jid in events:
+			for event in events[jid]:
+				self.connection.send_message(j, event.parameters[0], '',
+					type=event.type_, subject=event.parameters[1],
+					resource=resource, forward_from=jid)
+
+		# Inform other client of completion
+		response, cmd = self.buildResponse(request, status = 'completed')
+		cmd.addChild('note', {}, _('All unread messages have been forwarded.'))
+
+		self.connection.connection.send(response)
+
+		return False	# finish the session
+
 class ConnectionCommands:
 	''' This class depends on that it is a part of Connection() class. '''
 	def __init__(self):
 		# a list of all commands exposed: node -> command class
 		self.__commands = {}
-		for cmdobj in (ChangeStatusCommand, LeaveGroupchatsCommand):
+		for cmdobj in (ChangeStatusCommand, ForwardMessagesCommand,
+		LeaveGroupchatsCommand):
 			self.__commands[cmdobj.commandnode] = cmdobj
 
 		# a list of sessions; keys are tuples (jid, sessionid, node)
diff --git a/src/common/connection.py b/src/common/connection.py
index 4e44333d59..d58485cc80 100644
--- a/src/common/connection.py
+++ b/src/common/connection.py
@@ -808,7 +808,7 @@ class Connection(ConnectionHandlers):
 
 	def send_message(self, jid, msg, keyID, type = 'chat', subject='',
 	chatstate = None, msg_id = None, composing_xep = None, resource = None,
-	user_nick = None, xhtml = None):
+	user_nick = None, xhtml = None, forward_from = None):
 		if not self.connection:
 			return 1
 		if msg and not xhtml and gajim.config.get('rst_formatting_outgoing_messages'):
@@ -877,21 +877,27 @@ class Connection(ConnectionHandlers):
 				if chatstate is 'composing' or msgtxt: 
 					chatstate_node.addChild(name = 'composing') 
 
+		if forward_from:
+			addresses = msg_iq.addChild('addresses',
+				namespace=common.xmpp.NS_ADDRESS)
+			addresses.addChild('address', attrs = {'type': 'ofrom',
+				'jid': forward_from})
 		self.connection.send(msg_iq)
-		no_log_for = gajim.config.get_per('accounts', self.name, 'no_log_for')\
-			.split()
-		ji = gajim.get_jid_without_resource(jid)
-		if self.name not in no_log_for and ji not in no_log_for:
-			log_msg = msg
-			if subject:
-				log_msg = _('Subject: %s\n%s') % (subject, msg)
-			if log_msg:
-				if type == 'chat':
-					kind = 'chat_msg_sent'
-				else:
-					kind = 'single_msg_sent'
-				gajim.logger.write(kind, jid, log_msg)
-		self.dispatch('MSGSENT', (jid, msg, keyID))
+		if not forward_from:
+			no_log_for = gajim.config.get_per('accounts', self.name, 'no_log_for')\
+				.split()
+			ji = gajim.get_jid_without_resource(jid)
+			if self.name not in no_log_for and ji not in no_log_for:
+				log_msg = msg
+				if subject:
+					log_msg = _('Subject: %s\n%s') % (subject, msg)
+				if log_msg:
+					if type == 'chat':
+						kind = 'chat_msg_sent'
+					else:
+						kind = 'single_msg_sent'
+					gajim.logger.write(kind, jid, log_msg)
+			self.dispatch('MSGSENT', (jid, msg, keyID))
 	
 	def send_stanza(self, stanza):
 		''' send a stanza untouched '''
diff --git a/src/common/connection_handlers.py b/src/common/connection_handlers.py
index bef9a90333..13af916e8d 100644
--- a/src/common/connection_handlers.py
+++ b/src/common/connection_handlers.py
@@ -1427,6 +1427,12 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
 		tim = localtime(timegm(tim))
 		frm = helpers.get_full_jid_from_iq(msg)
 		jid = helpers.get_jid_from_iq(msg)
+		addressTag = msg.getTag('addresses', namespace = common.xmpp.NS_ADDRESS)
+		if addressTag:
+			address = addressTag.getTag('address', attrs={'type': 'ofrom'})
+			if address:
+				frm = address.getAttr('jid')
+				jid = gajim.get_jid_without_resource(frm)
 		no_log_for = gajim.config.get_per('accounts', self.name,
 			'no_log_for')
 		if not no_log_for:
-- 
GitLab