Mercurial > xib
changeset 11:79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Mon, 17 Aug 2009 00:30:37 +0200 |
parents | 7cb790f5f243 |
children | fd695e2b5283 |
files | bot.py bridge.py participant.py |
diffstat | 3 files changed, 46 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/bot.py +++ b/bot.py @@ -43,6 +43,7 @@ class bot(Thread): def __init__(self, jid, password, nickname, error_fd=sys.stderr, debug=False): Thread.__init__(self) + self.commands = ['!xmpp_participants'] self.jid = xmpp.protocol.JID(jid=jid) self.nickname = nickname self.password = password @@ -143,23 +144,23 @@ class bot(Thread): if message.getType() == 'chat': self.error('==> Debug: Received XMPP message.', debug=True) self.error(message.__str__(fancy=1), debug=True) - if message.getTo() == self.jid: - xmpp_c.send(xmpp.protocol.Message(to=message.getFrom(), body=u'Sorry I am a bot I don\'t speak …', typ='chat')) - else: - from_bare_jid = unicode(message.getFrom().getNode()+'@'+message.getFrom().getDomain()) - for bridge in self.bridges: - if from_bare_jid == bridge.xmpp_room.room_jid: - # message comes from a room participant - try: - to_ = bridge.getParticipant(message.getTo().getResource()) - from_ = bridge.getParticipant(message.getFrom().getResource()) - except NoSuchParticipantException: - self.error('==> Debug: XMPP chat message not relayed, from_bare_jid='+from_bare_jid+' to='+str(message.getTo().getResource())+' from='+message.getFrom().getResource(), debug=True) + from_bare_jid = unicode(message.getFrom().getNode()+'@'+message.getFrom().getDomain()) + for bridge in self.bridges: + if from_bare_jid == bridge.xmpp_room.room_jid: + # message comes from a room participant + try: + from_ = bridge.getParticipant(message.getFrom().getResource()) + to_ = bridge.getParticipant(message.getTo().getResource()) + except NoSuchParticipantException: + if message.getTo() == self.jid: + xmpp_c.send(xmpp.protocol.Message(to=message.getFrom(), body=self.respond(message.getBody(), from_), typ='chat')) return - if from_.protocol in ['xmpp', 'both']: - from_.sayOnIRCTo(to_.nickname, message.getBody()) - else: - self.error('==> Debug: received XMPP chat message from a non-XMPP participant, WTF ?', debug=True) + self.error('==> Debug: XMPP chat message not relayed, from_bare_jid='+from_bare_jid+' to='+str(message.getTo().getResource())+' from='+message.getFrom().getResource(), debug=True) + return + if from_.protocol in ['xmpp', 'both']: + from_.sayOnIRCTo(to_.nickname, message.getBody()) + else: + self.error('==> Debug: received XMPP chat message from a non-XMPP participant, WTF ?', debug=True) elif message.getType() == 'groupchat': # message comes from a room @@ -269,7 +270,7 @@ class bot(Thread): connection.bridge.addParticipant('irc', nickname) return try: - if not '!' in event.source(): + if event.source() == None or not '!' in event.source(): return from_ = connection.bridge.getParticipant(event.source().split('!')[0]) if event.eventtype() == 'quit' or event.eventtype() == 'part' and event.target() == connection.bridge.irc_room: @@ -291,7 +292,7 @@ class bot(Thread): to_ = connection.bridge.getParticipant(event.target().split('!')[0]) except NoSuchParticipantException: if event.target().split('!')[0] == self.nickname: - connection.privmsg(from_.nickname, u'Sorry I am a bot I don\'t speak …') + connection.privmsg(from_.nickname, self.respond(event.arguments()[0], from_)) return if to_.protocol == 'xmpp': from_.sayOnXMPPTo(to_.nickname, event.arguments()[0]) @@ -311,6 +312,13 @@ class bot(Thread): del bridge + def respond(self, message, participant): + if message.strip() == '!xmpp_participants': + xmpp_participants_nicknames = participant.bridge.get_xmpp_participants_nicknames_list() + return 'participants on '+participant.bridge.xmpp_room.room_jid+': '+' '.join(xmpp_participants_nicknames) + else: + return 'commands: '+' '.join(self.commands) + def __del__(self): for bridge in bridges: del bridge \ No newline at end of file
--- a/bridge.py +++ b/bridge.py @@ -86,6 +86,9 @@ class bridge: self.bot.error('===> Debug: adding participant "'+nickname+'" from "'+protocol+'" to bridge "'+str(self)+'"', debug=True) p = participant(self, protocol, nickname) self.participants.append(p) + if self.mode != 'normal' and protocol == 'xmpp': + xmpp_participants_nicknames = self.get_xmpp_participants_nicknames_list() + self.say('[Info] Participants on XMPP: '+' '.join(xmpp_participants_nicknames), on_xmpp=False) return p @@ -97,6 +100,14 @@ class bridge: raise NoSuchParticipantException('there is no participant using the nickname "'+nickname+'" in this bridge') + def get_xmpp_participants_nicknames_list(self): + xmpp_participants_nicknames = [] + for p in self.participants: + if p.protocol == 'xmpp': + xmpp_participants_nicknames.append(p.nickname) + return xmpp_participants_nicknames + + def removeParticipant(self, protocol, nickname, leave_message): """Remove the participant using nickname from the bridge. Raises a NoSuchParticipantException if nickname is not used in the bridge.""" p = self.getParticipant(nickname) @@ -123,9 +134,11 @@ class bridge: del p - def say(self, message): - self.xmpp_room.say(message) - self.irc_connection.privmsg(self.irc_room, auto_encode(message)) + def say(self, message, on_irc=True, on_xmpp=True): + if on_xmpp == True: + self.xmpp_room.say(message) + if on_irc == True: + self.irc_connection.privmsg(self.irc_room, auto_encode(message)) def switchToNormalMode(self): @@ -157,6 +170,8 @@ class bridge: self.irc_connections_limit = i self.bot.error('===> Bridge is switching to limited mode.') self.say('[Warning] Bridge is switching to limited mode, it means that it will be transparent for XMPP users but not for IRC users, this is due to the IRC servers\' per-IP-address connections\' limit number.') + xmpp_participants_nicknames = self.get_xmpp_participants_nicknames_list() + self.say('[Info] Participants on XMPP: '+' '.join(xmpp_participants_nicknames), on_xmpp=False) def __str__(self):
--- a/participant.py +++ b/participant.py @@ -199,9 +199,9 @@ class participant: def leave(self, message): if message == None: message = '' - if self.muc: + if self.xmpp_c != None: self.muc.leave(message) - if self.irc_connection: + if self.irc_connection != None: self.irc_connection.closing = True self.irc_connection.disconnect(message) self.irc_connection = None