comparison bot.py @ 179:f6c6708c6c0e

handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Fri, 12 Feb 2010 14:37:48 +0100
parents d1686164b9e3
children 102f895347ff
comparison
equal deleted inserted replaced
178:891195c47350 179:f6c6708c6c0e
472 self.error('=> Debug: ignoring '+event.eventtype(), debug=True) 472 self.error('=> Debug: ignoring '+event.eventtype(), debug=True)
473 return 473 return
474 474
475 475
476 # A string representation of the event 476 # A string representation of the event
477 event_str = '==> Debug: Received IRC event.\nconnection='+connection.__str__()+'\neventtype='+event.eventtype()+'\nsource='+repr(event.source())+'\ntarget='+repr(event.target())+'\narguments='+repr(event.arguments()) 477 event_str = 'connection='+connection.__str__()+'\neventtype='+event.eventtype()+'\nsource='+repr(event.source())+'\ntarget='+repr(event.target())+'\narguments='+repr(event.arguments())
478 debug_str = '==> Debug: Received IRC event.\n'+event_str
479 printed_event = False
478 480
479 481
480 if event.eventtype() in ['pubmsg', 'action', 'privmsg', 'quit', 'part', 'nick', 'kick']: 482 if event.eventtype() in ['pubmsg', 'action', 'privmsg', 'quit', 'part', 'nick', 'kick']:
481 if nickname == None: 483 if nickname == None:
482 return 484 return
483 485
484 handled = False 486 handled = False
487
488 if event.eventtype() in ['quit', 'part'] and nickname == self.nickname:
489 return
485 490
486 if event.eventtype() in ['quit', 'part', 'nick', 'kick']: 491 if event.eventtype() in ['quit', 'part', 'nick', 'kick']:
487 if connection.get_nickname() != self.nickname: 492 if connection.get_nickname() != self.nickname:
488 self.error('=> Debug: ignoring IRC '+event.eventtype()+' not received on bot connection', debug=True) 493 self.error('=> Debug: ignoring IRC '+event.eventtype()+' not received on bot connection', debug=True)
489 return 494 return
490 else: 495 else:
491 self.error(event_str, debug=True) 496 self.error(debug_str, debug=True)
497 printed_event = True
492 498
493 if event.eventtype() == 'kick' and len(event.arguments()) < 1: 499 if event.eventtype() == 'kick' and len(event.arguments()) < 1:
494 self.error('=> Debug: length of arguments should be greater than 0 for a '+event.eventtype()+' event') 500 self.error('=> Debug: length of arguments should be greater than 0 for a '+event.eventtype()+' event')
495 return 501 return
496 502
519 if event.target() == None: 525 if event.target() == None:
520 return 526 return
521 527
522 try: 528 try:
523 to_ = bridge.getParticipant(event.target().split('!')[0]) 529 to_ = bridge.getParticipant(event.target().split('!')[0])
524 self.error(event_str, debug=True) 530 self.error(debug_str, debug=True)
525 from_.sayOnXMPPTo(to_.nickname, event.arguments()[0]) 531 from_.sayOnXMPPTo(to_.nickname, event.arguments()[0])
526 return 532 return
527 533
528 except Bridge.NoSuchParticipantException: 534 except Bridge.NoSuchParticipantException:
529 if event.target().split('!')[0] == self.nickname: 535 if event.target().split('!')[0] == self.nickname:
530 # Message is for the bot 536 # Message is for the bot
531 self.error(event_str, debug=True) 537 self.error(debug_str, debug=True)
532 connection.privmsg(from_.nickname, self.respond(event.arguments()[0])) 538 connection.privmsg(from_.nickname, self.respond(event.arguments()[0]))
533 return 539 return
534 else: 540 else:
535 continue 541 continue
536 542
580 586
581 587
582 # Chan message 588 # Chan message
583 if event.eventtype() in ['pubmsg', 'action']: 589 if event.eventtype() in ['pubmsg', 'action']:
584 if bridge.irc_room == event.target().lower() and bridge.irc_server == connection.server: 590 if bridge.irc_room == event.target().lower() and bridge.irc_server == connection.server:
585 self.error(event_str, debug=True) 591 self.error(debug_str, debug=True)
586 message = event.arguments()[0] 592 message = event.arguments()[0]
587 if event.eventtype() == 'action': 593 if event.eventtype() == 'action':
588 message = '/me '+message 594 message = '/me '+message
589 from_.sayOnXMPP(message) 595 from_.sayOnXMPP(message)
590 return 596 return
591 else: 597 else:
592 continue 598 continue
593 599
594 if handled == False: 600 if handled:
595 if not event.eventtype() in ['quit', 'part', 'nick', 'kick']: 601 return
596 self.error(event_str, debug=True)
597 self.error('=> Debug: event was not handled', debug=True)
598 return
599 602
600 603
601 # Handle bannedfromchan 604 # Handle bannedfromchan
602 if event.eventtype() == 'bannedfromchan': 605 if event.eventtype() == 'bannedfromchan':
603 if len(event.arguments()) < 1: 606 if len(event.arguments()) < 1:
614 else: 617 else:
615 try: 618 try:
616 banned = bridge.getParticipant(event.target()) 619 banned = bridge.getParticipant(event.target())
617 if banned.irc_connection != 'bannedfromchan': 620 if banned.irc_connection != 'bannedfromchan':
618 banned.irc_connection = 'bannedfromchan' 621 banned.irc_connection = 'bannedfromchan'
619 self.error(event_str, debug=True) 622 self.error(debug_str, debug=True)
620 bridge.say('[Warning] the nickname "'+event.target()+'" is banned from the IRC chan', log=True) 623 bridge.say('[Warning] the nickname "'+event.target()+'" is banned from the IRC chan', log=True)
621 else: 624 else:
622 self.error('=> Debug: ignoring '+event.eventtype(), debug=True) 625 self.error('=> Debug: ignoring '+event.eventtype(), debug=True)
623 except Bridge.NoSuchParticipantException: 626 except Bridge.NoSuchParticipantException:
624 self.error('=> Debug: no such participant. WTF ?') 627 self.error('=> Debug: no such participant. WTF ?')
641 bridge.addParticipant('irc', nickname) 644 bridge.addParticipant('irc', nickname)
642 return 645 return
643 elif event.eventtype() == 'join': 646 elif event.eventtype() == 'join':
644 bridges = self.getBridges(irc_room=event.target().lower(), irc_server=connection.server) 647 bridges = self.getBridges(irc_room=event.target().lower(), irc_server=connection.server)
645 if len(bridges) == 0: 648 if len(bridges) == 0:
646 self.error(event_str, debug=True) 649 self.error(debug_str, debug=True)
647 self.error('===> Debug: no bridge found for "'+event.target().lower()+' at '+connection.server+'"', debug=True) 650 self.error('===> Debug: no bridge found for "'+event.target().lower()+' at '+connection.server+'"', debug=True)
648 return 651 return
649 for bridge in bridges: 652 for bridge in bridges:
650 bridge.addParticipant('irc', nickname, irc_id=event.source()) 653 bridge.addParticipant('irc', nickname, irc_id=event.source())
651 return 654 return
652 655
653 656
654 if event.eventtype() in ['disconnect', 'kill', 'error']: 657 if event.eventtype() in ['disconnect', 'kill', 'error']:
655 if len(event.arguments()) > 0 and event.arguments()[0] == 'Connection reset by peer': 658 if len(event.arguments()) > 0 and event.arguments()[0] == 'Connection reset by peer':
656 self.error(event_str, debug=True) 659 self.error(debug_str, debug=True)
657 else: 660 else:
658 self.error(event_str, send_to_admins=True) 661 self.error(debug_str, send_to_admins=True)
662 return
663
664
665 if event.eventtype() in ['cannotsendtochan', 'notonchannel']:
666 self.error(debug_str, debug=True)
667 bridges = self.getBridges(irc_room=event.arguments()[0], irc_server=connection.server)
668 if len(bridges) > 1:
669 raise Exception, 'more than one bridge for one irc chan, WTF ?'
670 bridge = bridges[0]
671 if connection.get_nickname() == self.nickname:
672 bridge._join_irc_failed()
673 else:
674 p = bridge.getParticipant(connection.get_nickname())
675 p._close_irc_connection('')
676 p.irc_connection = error
659 return 677 return
660 678
661 679
662 # Unhandled events 680 # Unhandled events
663 self.error(event_str+'\n=> Debug: event not handled', debug=True) 681 if not printed_event:
682 self.error('[Debug] The following IRC event was not handled:\n'+event_str+'\n', send_to_admins=True)
683 else:
684 self.error('=> Debug: event not handled', debug=True)
685 self._send_message_to_admins('[Debug] The following IRC event was not handled:\n'+event_str)
664 686
665 687
666 def _send_message_to_admins(self, message): 688 def _send_message_to_admins(self, message):
667 """[Internal] Send XMPP Message to bot admin(s)""" 689 """[Internal] Send XMPP Message to bot admin(s)"""
668 for admin_jid in self.admins_jid: 690 for admin_jid in self.admins_jid: