comparison bot.py @ 18:3cdf7bb580da

Fixed bot.respond(), boosted XMPP thread to lower latency, fixed mode notice. Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Thu, 20 Aug 2009 01:57:51 +0200
parents 32a35f7eff70
children c1b84196c100
comparison
equal deleted inserted replaced
17:32a35f7eff70 18:3cdf7bb580da
33 33
34 class bot(Thread): 34 class bot(Thread):
35 35
36 def __init__(self, jid, password, nickname, error_fd=sys.stderr, debug=False): 36 def __init__(self, jid, password, nickname, error_fd=sys.stderr, debug=False):
37 Thread.__init__(self) 37 Thread.__init__(self)
38 self.commands = ['!xmpp_participants'] 38 self.commands = ['!xmpp_participants', '!irc_participants']
39 self.bare_jid = xmpp.protocol.JID(jid=jid) 39 self.bare_jid = xmpp.protocol.JID(jid=jid)
40 self.bare_jid.setResource('') 40 self.bare_jid.setResource('')
41 self.jid = xmpp.protocol.JID(jid=jid) 41 self.jid = xmpp.protocol.JID(jid=jid)
42 self.nickname = nickname 42 self.nickname = nickname
43 self.jid.setResource(self.nickname) 43 self.jid.setResource(self.nickname)
71 71
72 72
73 def _xmpp_loop(self): 73 def _xmpp_loop(self):
74 """[Internal] XMPP infinite loop.""" 74 """[Internal] XMPP infinite loop."""
75 while True: 75 while True:
76 self.xmpp_c.Process(5) 76 self.xmpp_c.Process(0.5)
77 try: 77 try:
78 for c in self.xmpp_connections.itervalues(): 78 for c in self.xmpp_connections.itervalues():
79 if hasattr(c, 'Process'): 79 if hasattr(c, 'Process'):
80 c.Process(5) 80 c.Process(0.5)
81 else: 81 else:
82 sleep(1) 82 sleep(0.5)
83 except RuntimeError: 83 except RuntimeError:
84 pass 84 pass
85 85
86 86
87 def _xmpp_presence_handler(self, xmpp_c, presence): 87 def _xmpp_presence_handler(self, xmpp_c, presence):
163 else: 163 else:
164 self.error('==> Debug: received XMPP chat message from a non-XMPP participant, WTF ?', debug=True) 164 self.error('==> Debug: received XMPP chat message from a non-XMPP participant, WTF ?', debug=True)
165 165
166 except NoSuchParticipantException: 166 except NoSuchParticipantException:
167 if message.getTo() == self.jid: 167 if message.getTo() == self.jid:
168 xmpp_c.send(xmpp.protocol.Message(to=message.getFrom(), body=self.respond(message.getBody(), from_), typ='chat')) 168 xmpp_c.send(xmpp.protocol.Message(to=message.getFrom(), body=self.respond(message.getBody(), participant=from_), typ='chat'))
169 return 169 return
170 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) 170 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)
171 return 171 return
172 172
173 elif message.getType() == 'groupchat': 173 elif message.getType() == 'groupchat':
222 222
223 223
224 # Events we always want to ignore 224 # Events we always want to ignore
225 if 'all' in event.eventtype() or 'motd' in event.eventtype(): 225 if 'all' in event.eventtype() or 'motd' in event.eventtype():
226 return 226 return
227 if event.eventtype() in ['pong', 'privnotice', 'ctcp', 'nochanmodes']: 227 if event.eventtype() in ['pong', 'privnotice', 'ctcp', 'nochanmodes', 'notexttosend']:
228 self.error('=> Debug: ignoring '+event.eventtype(), debug=True) 228 self.error('=> Debug: ignoring '+event.eventtype(), debug=True)
229 return 229 return
230 230
231 231
232 nickname = None 232 nickname = None
278 278
279 except NoSuchParticipantException: 279 except NoSuchParticipantException:
280 if event.target().split('!')[0] == self.nickname: 280 if event.target().split('!')[0] == self.nickname:
281 # Message is for the bot 281 # Message is for the bot
282 self.error(event_str, debug=True) 282 self.error(event_str, debug=True)
283 connection.privmsg(from_.nickname, self.respond(event.arguments()[0], from_)) 283 connection.privmsg(from_.nickname, self.respond(event.arguments()[0]))
284 return 284 return
285 else: 285 else:
286 continue 286 continue
287 287
288 288
425 def removeBridge(self, bridge): 425 def removeBridge(self, bridge):
426 self.bridges.remove(bridge) 426 self.bridges.remove(bridge)
427 del bridge 427 del bridge
428 428
429 429
430 def respond(self, message, participant): 430 def respond(self, message, participant=None):
431 ret = ''
431 if message.strip() == '!xmpp_participants': 432 if message.strip() == '!xmpp_participants':
432 xmpp_participants_nicknames = participant.bridge.get_participants_nicknames_list(protocols=['xmpp']) 433 if participant == None:
433 return 'participants on '+participant.bridge.xmpp_room.room_jid+': '+' '.join(xmpp_participants_nicknames) 434 for bridge in self.bridges:
435 xmpp_participants_nicknames = bridge.get_participants_nicknames_list(protocols=['xmpp'])
436 ret += '\nparticipants on '+bridge.xmpp_room.room_jid+': '+' '.join(xmpp_participants_nicknames)
437 return ret
438 else:
439 xmpp_participants_nicknames = participant.bridge.get_participants_nicknames_list(protocols=['xmpp'])
440 return 'participants on '+participant.bridge.xmpp_room.room_jid+': '+' '.join(xmpp_participants_nicknames)
441 elif message.strip() == '!irc_participants':
442 if participant == None:
443 for bridge in self.bridges:
444 irc_participants_nicknames = bridge.get_participants_nicknames_list(protocols=['irc'])
445 ret += '\nparticipants on '+bridge.irc_room+' at '+bridge.irc_server+': '+' '.join(irc_participants_nicknames)
446 return ret
447 else:
448 irc_participants_nicknames = participant.bridge.get_participants_nicknames_list(protocols=['irc'])
449 return 'participants on '+participant.bridge.irc_room+' at '+participant.bridge.irc_server+': '+' '.join(irc_participants_nicknames)
434 else: 450 else:
435 return 'commands: '+' '.join(self.commands) 451 return 'commands: '+' '.join(self.commands)
436 452
437 453
438 def __del__(self): 454 def __del__(self):