changeset 213:fbe40b397f67

created Bridge.say_on_behalf in order to avoid code repetition in participant.py Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sat, 06 Mar 2010 19:05:07 +0100
parents aa03c7ca53cb
children 0c99d38b84b8
files bot.py bridge.py muc.py participant.py
diffstat 4 files changed, 35 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/bot.py
+++ b/bot.py
@@ -599,8 +599,10 @@ class Bot(threading.Thread):
 						self.error(2, debug_str, debug=True)
 						message = event.arguments()[0]
 						if event.eventtype() == 'action':
-							message = '/me '+message
-						from_.say_on_xmpp(message)
+							action = True
+						else:
+							action = False
+						from_.say_on_xmpp(message, action=action)
 						return
 					else:
 						continue
--- a/bridge.py
+++ b/bridge.py
@@ -354,10 +354,30 @@ class Bridge:
 			message = self.bot.format_message(importance, message)
 			if importance < self.say_level:
 				return
-		if on_xmpp and isinstance(self.xmpp_room, xmpp.muc) and self.xmpp_room.connected:
-			self.xmpp_room.say(message)
-		if on_irc and isinstance(self.irc_connection, ServerConnection) and self.irc_connection.really_connected:
-			self.irc_connection.privmsg(self.irc_room, message)
+		if on_xmpp:
+			self._say_on_xmpp(message)
+		if on_irc:
+			self._say_on_irc(message)
+	
+	
+	def say_on_behalf(self, nickname, message, on_protocol, action=False):
+		if action:
+			message = '* '+nickname+' '+message
+		else:
+			message = '<'+nickname+'> '+message
+		
+		if on_protocol == 'xmpp':
+			self._say_on_xmpp(message)
+		elif on_protocol == 'irc':
+			self._say_on_irc(message)
+	
+	
+	def _say_on_irc(self, message):
+		self.irc_connection.privmsg(self.irc_room, message)
+	
+	
+	def _say_on_xmpp(self, message):
+		self.xmpp_room.say(message)
 	
 	
 	def show_participants_list_on(self, protocols=[]):
--- a/muc.py
+++ b/muc.py
@@ -109,9 +109,11 @@ class muc:
 				self.callback(errors)
 	
 	
-	def say(self, message):
+	def say(self, message, action=False):
 		"""Say message in the room"""
 		self.xmpp_c.lock.acquire()
+		if action:
+			message = '/me '+message
 		s = xmpp.protocol.Message(to=self.room_jid, typ='groupchat', body=message)
 		self._send(s)
 		self.xmpp_c.lock.release()
--- a/participant.py
+++ b/participant.py
@@ -293,10 +293,7 @@ class Participant:
 		elif not isinstance(self.xmpp_c, xmpp.client.Client):
 			bot_say = True
 		if bot_say:
-			if action:
-				self.bridge.say(-1, '* '+self.nickname+' '+message, on_xmpp=False)
-			else:
-				self.bridge.say(-1, '<'+self.nickname+'> '+message, on_xmpp=False)
+			self.bridge.say_on_behalf(self.nickname, message, 'irc', action=action)
 	
 	
 	def say_on_irc_to(self, to, message):
@@ -309,18 +306,11 @@ class Participant:
 				self.bridge.get_participant(to).say_on_xmpp_to(self.nickname, 'Sorry but you cannot send cross-protocol private messages because I don\'t have an IRC duplicate with your nickname.')
 	
 	
-	def say_on_xmpp(self, message):
+	def say_on_xmpp(self, message, action=False):
 		if isinstance(self.xmpp_c, xmpp.client.Client):
-			self.muc.say(message)
+			self.muc.say(message, action=action)
 		elif not isinstance(self.irc_connection, ServerConnection):
-			self.say_on_XMPP_through_bridge(message)
-	
-	
-	def say_on_XMPP_through_bridge(self, message):
-		if message[:4] == '/me ':
-			self.bridge.say(-1, '* '+self.nickname+' '+message[4:], on_irc=False)
-		else:
-			self.bridge.say(-1, '<'+self.nickname+'> '+message, on_irc=False)
+			self.bridge.say_on_behalf(self.nickname, message, 'xmpp', action=action)
 	
 	
 	def say_on_xmpp_to(self, to, message):