Mercurial > xib
comparison bot.py @ 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 | 1a1f2a0d35c7 |
comparison
equal
deleted
inserted
replaced
10:7cb790f5f243 | 11:79b0a7f48a3e |
---|---|
41 | 41 |
42 class bot(Thread): | 42 class bot(Thread): |
43 | 43 |
44 def __init__(self, jid, password, nickname, error_fd=sys.stderr, debug=False): | 44 def __init__(self, jid, password, nickname, error_fd=sys.stderr, debug=False): |
45 Thread.__init__(self) | 45 Thread.__init__(self) |
46 self.commands = ['!xmpp_participants'] | |
46 self.jid = xmpp.protocol.JID(jid=jid) | 47 self.jid = xmpp.protocol.JID(jid=jid) |
47 self.nickname = nickname | 48 self.nickname = nickname |
48 self.password = password | 49 self.password = password |
49 self.error_fd = error_fd | 50 self.error_fd = error_fd |
50 self.debug = debug | 51 self.debug = debug |
141 def _xmpp_message_handler(self, xmpp_c, message): | 142 def _xmpp_message_handler(self, xmpp_c, message): |
142 """[Internal] Manage XMPP messages.""" | 143 """[Internal] Manage XMPP messages.""" |
143 if message.getType() == 'chat': | 144 if message.getType() == 'chat': |
144 self.error('==> Debug: Received XMPP message.', debug=True) | 145 self.error('==> Debug: Received XMPP message.', debug=True) |
145 self.error(message.__str__(fancy=1), debug=True) | 146 self.error(message.__str__(fancy=1), debug=True) |
146 if message.getTo() == self.jid: | 147 from_bare_jid = unicode(message.getFrom().getNode()+'@'+message.getFrom().getDomain()) |
147 xmpp_c.send(xmpp.protocol.Message(to=message.getFrom(), body=u'Sorry I am a bot I don\'t speak …', typ='chat')) | 148 for bridge in self.bridges: |
148 else: | 149 if from_bare_jid == bridge.xmpp_room.room_jid: |
149 from_bare_jid = unicode(message.getFrom().getNode()+'@'+message.getFrom().getDomain()) | 150 # message comes from a room participant |
150 for bridge in self.bridges: | 151 try: |
151 if from_bare_jid == bridge.xmpp_room.room_jid: | 152 from_ = bridge.getParticipant(message.getFrom().getResource()) |
152 # message comes from a room participant | 153 to_ = bridge.getParticipant(message.getTo().getResource()) |
153 try: | 154 except NoSuchParticipantException: |
154 to_ = bridge.getParticipant(message.getTo().getResource()) | 155 if message.getTo() == self.jid: |
155 from_ = bridge.getParticipant(message.getFrom().getResource()) | 156 xmpp_c.send(xmpp.protocol.Message(to=message.getFrom(), body=self.respond(message.getBody(), from_), typ='chat')) |
156 except NoSuchParticipantException: | |
157 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) | |
158 return | 157 return |
159 if from_.protocol in ['xmpp', 'both']: | 158 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) |
160 from_.sayOnIRCTo(to_.nickname, message.getBody()) | 159 return |
161 else: | 160 if from_.protocol in ['xmpp', 'both']: |
162 self.error('==> Debug: received XMPP chat message from a non-XMPP participant, WTF ?', debug=True) | 161 from_.sayOnIRCTo(to_.nickname, message.getBody()) |
162 else: | |
163 self.error('==> Debug: received XMPP chat message from a non-XMPP participant, WTF ?', debug=True) | |
163 | 164 |
164 elif message.getType() == 'groupchat': | 165 elif message.getType() == 'groupchat': |
165 # message comes from a room | 166 # message comes from a room |
166 if message.getTo() != self.jid: | 167 if message.getTo() != self.jid: |
167 self.error('=> Debug: Skipping XMPP MUC message not received on bot connection.', debug=True) | 168 self.error('=> Debug: Skipping XMPP MUC message not received on bot connection.', debug=True) |
267 connection.bridge.getParticipant(nickname) | 268 connection.bridge.getParticipant(nickname) |
268 except NoSuchParticipantException: | 269 except NoSuchParticipantException: |
269 connection.bridge.addParticipant('irc', nickname) | 270 connection.bridge.addParticipant('irc', nickname) |
270 return | 271 return |
271 try: | 272 try: |
272 if not '!' in event.source(): | 273 if event.source() == None or not '!' in event.source(): |
273 return | 274 return |
274 from_ = connection.bridge.getParticipant(event.source().split('!')[0]) | 275 from_ = connection.bridge.getParticipant(event.source().split('!')[0]) |
275 if event.eventtype() == 'quit' or event.eventtype() == 'part' and event.target() == connection.bridge.irc_room: | 276 if event.eventtype() == 'quit' or event.eventtype() == 'part' and event.target() == connection.bridge.irc_room: |
276 if from_.protocol in ['irc', 'both']: | 277 if from_.protocol in ['irc', 'both']: |
277 connection.bridge.removeParticipant('irc', from_.nickname, event.arguments()[0]) | 278 connection.bridge.removeParticipant('irc', from_.nickname, event.arguments()[0]) |
289 return | 290 return |
290 try: | 291 try: |
291 to_ = connection.bridge.getParticipant(event.target().split('!')[0]) | 292 to_ = connection.bridge.getParticipant(event.target().split('!')[0]) |
292 except NoSuchParticipantException: | 293 except NoSuchParticipantException: |
293 if event.target().split('!')[0] == self.nickname: | 294 if event.target().split('!')[0] == self.nickname: |
294 connection.privmsg(from_.nickname, u'Sorry I am a bot I don\'t speak …') | 295 connection.privmsg(from_.nickname, self.respond(event.arguments()[0], from_)) |
295 return | 296 return |
296 if to_.protocol == 'xmpp': | 297 if to_.protocol == 'xmpp': |
297 from_.sayOnXMPPTo(to_.nickname, event.arguments()[0]) | 298 from_.sayOnXMPPTo(to_.nickname, event.arguments()[0]) |
298 | 299 |
299 | 300 |
309 self.new_bridge(bridge.xmpp_room.room_jid, bridge.irc_room, bridge.irc_server) | 310 self.new_bridge(bridge.xmpp_room.room_jid, bridge.irc_room, bridge.irc_server) |
310 self.bridges.remove(bridge) | 311 self.bridges.remove(bridge) |
311 del bridge | 312 del bridge |
312 | 313 |
313 | 314 |
315 def respond(self, message, participant): | |
316 if message.strip() == '!xmpp_participants': | |
317 xmpp_participants_nicknames = participant.bridge.get_xmpp_participants_nicknames_list() | |
318 return 'participants on '+participant.bridge.xmpp_room.room_jid+': '+' '.join(xmpp_participants_nicknames) | |
319 else: | |
320 return 'commands: '+' '.join(self.commands) | |
321 | |
314 def __del__(self): | 322 def __del__(self): |
315 for bridge in bridges: | 323 for bridge in bridges: |
316 del bridge | 324 del bridge |