comparison bot.py @ 105:d8acff763731

Handle MUC error messages Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sat, 21 Nov 2009 18:23:41 +0100
parents 23416c27b592
children dff9746aa508
comparison
equal deleted inserted replaced
104:60cc60f0058d 105:d8acff763731
365 return 365 return
366 366
367 participant_.sayOnIRC(message.getBody()) 367 participant_.sayOnIRC(message.getBody())
368 return 368 return
369 369
370 elif message.getType() == 'error':
371 for b in self.bridges:
372 if message.getFrom() == b.xmpp_room_jid:
373 # message comes from a room
374 for c in message.getChildren():
375 if c.getName() == 'error':
376 for cc in c.getChildren():
377 if cc.getNamespace() == 'urn:ietf:params:xml:ns:xmpp-stanzas' and cc.getName() != 'text':
378 err = cc.getName()
379 if err == 'not-acceptable':
380 # we sent a message to a room we are not in
381 # probable cause is a MUC server restart
382 # let's restart the bot
383 self.restart()
384 elif err == 'forbidden':
385 # we don't have the permission to speak
386 # let's remove the bridge and tell admins
387 self.error('[Error] Not allowed to speak on the XMPP MUC of bridge '+str(b)+', removing it', send_to_admins=True)
388 self.removeBridge(b, message='Not allowed to speak on the XMPP MUC, removing bridge.')
389 else:
390 self.error('==> Debug: recevied unknown error message', debug=True)
391 self.error(message.__str__(fancy=1), debug=True)
392 return
393
394 self.error('==> Debug: recevied unknown error message', debug=True)
395 self.error(message.__str__(fancy=1), debug=True)
396
370 else: 397 else:
371 self.error('==> Debug: Received XMPP message of unknown type "'+message.getType()+'".', debug=True) 398 self.error('==> Debug: Received XMPP message of unknown type "'+message.getType()+'".', debug=True)
372 self.error(message.__str__(fancy=1), debug=True) 399 self.error(message.__str__(fancy=1), debug=True)
373 400
374 401
747 else: 774 else:
748 c.lock.release() 775 c.lock.release()
749 self.error('===> Debug: XMPP connection for "'+nickname+'" is now used by '+str(c.used_by)+' bridges', debug=True) 776 self.error('===> Debug: XMPP connection for "'+nickname+'" is now used by '+str(c.used_by)+' bridges', debug=True)
750 777
751 778
752 def removeBridge(self, bridge): 779 def removeBridge(self, bridge, message='Removing bridge'):
753 self.bridges.remove(bridge) 780 self.bridges.remove(bridge)
754 bridge.__del__() 781 bridge.stop(message)
755 782
756 783
757 def respond(self, message, participant_=None, bot_admin=False): 784 def respond(self, message, participant_=None, bot_admin=False):
758 ret = '' 785 ret = ''
759 command = shlex.split(message) 786 command = shlex.split(message)
835 862
836 return 'XMPP admin added.' 863 return 'XMPP admin added.'
837 864
838 elif command == 'restart-bot': 865 elif command == 'restart-bot':
839 self.restart() 866 self.restart()
840 return 'Bot restarted.' 867 return
841 elif command == 'halt': 868 elif command == 'halt':
842 self.__del__() 869 self.__del__()
870 return
843 871
844 872
845 elif command in ['remove-bridge', 'restart-bridge']: 873 elif command in ['remove-bridge', 'restart-bridge']:
846 # we need to know which bridge the command is for 874 # we need to know which bridge the command is for
847 if len(args_array) == 0: 875 if len(args_array) == 0:
882 910
883 911
884 def restart(self): 912 def restart(self):
885 # Stop the bridges 913 # Stop the bridges
886 for b in self.bridges: 914 for b in self.bridges:
887 b.stop(message='Restarting bridge') 915 b.stop(message='Restarting bot')
888 916
889 # Reopen the bot's XMPP connection 917 # Reopen the bot's XMPP connection
890 self.reopen_xmpp_connection(self.xmpp_c) 918 self.reopen_xmpp_connection(self.xmpp_c)
891 919
892 # Restart the bridges 920 # Restart the bridges
893 for b in self.bridges: 921 for b in self.bridges:
894 b.init2() 922 b.init2()
895 923
896 sleep(1) 924 self.error('Bot restarted.', send_to_admins=True)
897 925
898 926
899 def __del__(self): 927 def __del__(self):
900 for bridge in self.bridges: 928 for bridge in self.bridges:
901 self.removeBridge(bridge) 929 self.removeBridge(bridge)