comparison bot.py @ 32:8aa261545662

Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow. Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sat, 22 Aug 2009 14:41:37 +0200
parents c0fb916cb0a0
children cb6771a8c3ca
comparison
equal deleted inserted replaced
31:5afa22125334 32:8aa261545662
271 271
272 # A string representation of the event 272 # A string representation of the event
273 event_str = '==> Debug: Received IRC event.\nconnection='+str(connection)+'\neventtype='+event.eventtype()+'\nsource='+str(event.source())+'\ntarget='+str(event.target())+'\narguments='+str(event.arguments()) 273 event_str = '==> Debug: Received IRC event.\nconnection='+str(connection)+'\neventtype='+event.eventtype()+'\nsource='+str(event.source())+'\ntarget='+str(event.target())+'\narguments='+str(event.arguments())
274 274
275 275
276 if event.eventtype() in ['pubmsg', 'privmsg', 'quit', 'part', 'nick']: 276 if event.eventtype() in ['pubmsg', 'action', 'privmsg', 'quit', 'part', 'nick']:
277 if nickname == None: 277 if nickname == None:
278 return 278 return
279 279
280 # TODO: lock self.bridges for thread safety 280 # TODO: lock self.bridges for thread safety
281 for bridge in self.bridges: 281 for bridge in self.bridges:
282 if connection.server != bridge.irc_server:
283 continue
284
282 try: 285 try:
283 from_ = bridge.getParticipant(nickname) 286 from_ = bridge.getParticipant(nickname)
284 287
285 except NoSuchParticipantException: 288 except NoSuchParticipantException:
286 self.error('===> Debug: NoSuchParticipantException "'+nickname+'" in bridge "'+str(bridge)+'"', debug=True) 289 self.error('===> Debug: NoSuchParticipantException "'+nickname+'" in bridge "'+str(bridge)+'"', debug=True)
315 318
316 self.error(event_str, debug=True) 319 self.error(event_str, debug=True)
317 320
318 321
319 # Leaving events 322 # Leaving events
320 if connection.server == bridge.irc_server and (event.eventtype() == 'quit' or event.eventtype() == 'part' and event.target() == bridge.irc_room): 323 if event.eventtype() == 'quit' or event.eventtype() == 'part' and event.target() == bridge.irc_room:
321 if len(event.arguments()) > 0: 324 if len(event.arguments()) > 0:
322 leave_message = event.arguments()[0] 325 leave_message = event.arguments()[0]
323 elif event.eventtype() == 'quit': 326 elif event.eventtype() == 'quit':
324 leave_message = 'Left server.' 327 leave_message = 'Left server.'
325 elif event.eventtype() == 'part': 328 elif event.eventtype() == 'part':
335 from_.changeNickname(event.target(), 'xmpp') 338 from_.changeNickname(event.target(), 'xmpp')
336 continue 339 continue
337 340
338 341
339 # Chan message 342 # Chan message
340 if event.eventtype() == 'pubmsg': 343 if event.eventtype() in ['pubmsg', 'action']:
341 if bridge.irc_room == event.target() and bridge.irc_server == connection.server: 344 if bridge.irc_room == event.target() and bridge.irc_server == connection.server:
342 from_.sayOnXMPP(event.arguments()[0]) 345 message = event.arguments()[0]
346 if event.eventtype() == 'action':
347 message = '/me '+message
348 from_.sayOnXMPP(message)
343 return 349 return
344 else: 350 else:
345 continue 351 continue
346 352
347 return 353 return
402 return b 408 return b
403 409
404 410
405 def getBridges(self, irc_room=None, irc_server=None, xmpp_room_jid=None): 411 def getBridges(self, irc_room=None, irc_server=None, xmpp_room_jid=None):
406 bridges = [b for b in self.bridges] 412 bridges = [b for b in self.bridges]
407 if irc_room != None: 413 for bridge in [b for b in bridges]:
408 for bridge in bridges: 414 if irc_room != None and bridge.irc_room != irc_room:
409 if bridge.irc_room != irc_room: 415 bridges.remove(bridge)
410 if bridge in bridges: 416 continue
411 bridges.remove(bridge) 417 if irc_server != None and bridge.irc_server != irc_server:
412 if irc_server != None: 418 bridges.remove(bridge)
413 for bridge in bridges: 419 continue
414 if bridge.irc_server != irc_server: 420 if xmpp_room_jid != None and bridge.xmpp_room.room_jid != xmpp_room_jid:
415 if bridge in bridges: 421 bridges.remove(bridge)
416 bridges.remove(bridge) 422 continue
417 if xmpp_room_jid != None:
418 for bridge in bridges:
419 if bridge.xmpp_room.room_jid != xmpp_room_jid:
420 if bridge in bridges:
421 bridges.remove(bridge)
422 return bridges 423 return bridges
423 424
424 425
425 def get_xmpp_connection(self, nickname): 426 def get_xmpp_connection(self, nickname):
426 if self.xmpp_connections.has_key(nickname): 427 if self.xmpp_connections.has_key(nickname):
438 c.connect() 439 c.connect()
439 c.auth(self.bare_jid.getNode(), self.password) 440 c.auth(self.bare_jid.getNode(), self.password)
440 c.RegisterHandler('presence', self._xmpp_presence_handler) 441 c.RegisterHandler('presence', self._xmpp_presence_handler)
441 c.RegisterHandler('iq', self._xmpp_iq_handler) 442 c.RegisterHandler('iq', self._xmpp_iq_handler)
442 c.RegisterHandler('message', self._xmpp_message_handler) 443 c.RegisterHandler('message', self._xmpp_message_handler)
443 c.DisconnectHandler = self._xmpp_disconnect_handler(c) 444 c.RegisterDisconnectHandler(self._xmpp_disconnect_handler(c))
444 c.sendInitPresence() 445 c.sendInitPresence()
445 c.lock.release() 446 c.lock.release()
446 return c 447 return c
447 448
448 449