# HG changeset patch # User Charly COSTE # Date 1267898707 -3600 # Node ID fbe40b397f6775253028e15f3527c56e0d42ae8d # Parent aa03c7ca53cb463603c5c0727b82b6a85856a681 created Bridge.say_on_behalf in order to avoid code repetition in participant.py Signed-off-by: Charly COSTE diff --git a/bot.py b/bot.py --- 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 diff --git a/bridge.py b/bridge.py --- 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=[]): diff --git a/muc.py b/muc.py --- 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() diff --git a/participant.py b/participant.py --- 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):