From d0c3b6fd440f523ba6a213e674420060cf413e08 Mon Sep 17 00:00:00 2001
From: Yann Leboulanger <asterix@lagaule.org>
Date: Sat, 13 Dec 2008 14:43:46 +0000
Subject: [PATCH] prevent sending messages to invalid JIDs. Fixes #4577

---
 src/common/connection.py          | 8 ++++++++
 src/common/connection_handlers.py | 8 ++++++--
 src/dialogs.py                    | 7 +++++++
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/src/common/connection.py b/src/common/connection.py
index 0393201d59..fa68745dc6 100644
--- a/src/common/connection.py
+++ b/src/common/connection.py
@@ -1074,6 +1074,14 @@ class Connection(ConnectionHandlers):
 	original_message=None, delayed=None):
 		if not self.connection:
 			return 1
+		try:
+			jid = helpers.parse_jid(jid)
+		except helpers.InvalidFormat:
+			self.dispatch('ERROR', (_('Invalid Jabber ID'),
+				_('It is not possible to send a message to %s, this JID is not '
+				'valid.') % jid))
+			return
+			
 		if msg and not xhtml and gajim.config.get(
 		'rst_formatting_outgoing_messages'):
 			from common.rst_xhtml_generator import create_xhtml
diff --git a/src/common/connection_handlers.py b/src/common/connection_handlers.py
index c463b62191..2531d26708 100644
--- a/src/common/connection_handlers.py
+++ b/src/common/connection_handlers.py
@@ -1682,8 +1682,12 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
 			self._HttpAuthCB(con, msg)
 			return
 
-		frm = helpers.get_full_jid_from_iq(msg)
-		jid = helpers.get_jid_from_iq(msg)
+		try:
+			frm = helpers.get_full_jid_from_iq(msg)
+			jid = helpers.get_jid_from_iq(msg)
+		except helpers.InvalidFormat:
+			self.dispatch('ERROR', (_('Invalid Jabber ID'),
+				_('A message from a non-valid JID arrived, it has been ignored.')))
 
 		addressTag = msg.getTag('addresses', namespace = common.xmpp.NS_ADDRESS)
 
diff --git a/src/dialogs.py b/src/dialogs.py
index 79613b0a92..7fcd44f406 100644
--- a/src/dialogs.py
+++ b/src/dialogs.py
@@ -2352,6 +2352,13 @@ class SingleMessageWindow:
 		for to_whom_jid in sender_list:
 			if to_whom_jid in self.completion_dict:
 				to_whom_jid = self.completion_dict[to_whom_jid].jid
+			try:
+				to_whom_jid = helpers.parse_jid(to_whom_jid)
+			except helpers.InvalidFormat:
+				ErrorDialog(_('Invalid Jabber ID'),
+					_('It is not possible to send a message to %s, this JID is not '
+					'valid.') % to_whom_jid)
+				return
 
 			subject = self.subject_entry.get_text().decode('utf-8')
 			begin, end = self.message_tv_buffer.get_bounds()
-- 
GitLab