comparison bot.py @ 216:d527d7b75f56

merged Bot.find_bridges and Bot.get_bridges into Bot.iter_bridges, btw we don't need to lock Bot.bridges because lists don't raise RuntimeError Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sat, 06 Mar 2010 22:55:47 +0100
parents fbe40b397f67
children 63289aa1dea7
comparison
equal deleted inserted replaced
215:b900585bdbbb 216:d527d7b75f56
162 reason = d.getTag('reason') 162 reason = d.getTag('reason')
163 if reason: 163 if reason:
164 r = reason.getData() 164 r = reason.getData()
165 if r == 'The conference component is shutting down': 165 if r == 'The conference component is shutting down':
166 # MUC server is going down, try to restart the bridges in 1 minute 166 # MUC server is going down, try to restart the bridges in 1 minute
167 bridges = self.find_bridges([from_.getDomain()]) 167 bridges = self.iter_bridges(patterns=[from_.getDomain()])
168 m = 'The MUC server '+from_.getDomain()+' seems to be going down, the bot will try to recreate all bridges related to this server in 1 minute' 168 m = 'The MUC server '+from_.getDomain()+' seems to be going down, the bot will try to recreate all bridges related to this server in 1 minute'
169 error = [say_levels.warning, m] 169 error = [say_levels.warning, m]
170 self.restart_bridges_delayed(bridges, 60, error) 170 self.restart_bridges_delayed(bridges, 60, error)
171 return 171 return
172 elif r == '': 172 elif r == '':
270 if c.getNamespace() == 'urn:ietf:params:xml:ns:xmpp-stanzas' and c.getName() != 'text': 270 if c.getNamespace() == 'urn:ietf:params:xml:ns:xmpp-stanzas' and c.getName() != 'text':
271 err = error.getAttr('type')+' '+c.getName() 271 err = error.getAttr('type')+' '+c.getName()
272 if err == 'cancel remote-server-not-found': 272 if err == 'cancel remote-server-not-found':
273 # Remote server not found 273 # Remote server not found
274 # Stop bridges that depend on this server 274 # Stop bridges that depend on this server
275 bridges = self.find_bridges([from_.getDomain()]) 275 bridges = self.iter_bridges(patterns=[from_.getDomain()])
276 error = [say_levels.error, 'XMPP Remote server not found: '+from_.getDomain()] 276 error = [say_levels.error, 'XMPP Remote server not found: '+from_.getDomain()]
277 self.restart_bridges_delayed(bridges, 60, error) 277 self.restart_bridges_delayed(bridges, 60, error)
278 else: 278 else:
279 raise Exception(presence.__str__(fancy=1).encode('utf-8')) 279 raise Exception(presence.__str__(fancy=1).encode('utf-8'))
280 280
648 return 648 return
649 649
650 650
651 if event.eventtype() in ['cannotsendtochan', 'notonchannel', 'inviteonlychan']: 651 if event.eventtype() in ['cannotsendtochan', 'notonchannel', 'inviteonlychan']:
652 self.error(2, debug_str, debug=True) 652 self.error(2, debug_str, debug=True)
653 bridges = self.get_bridges(irc_room=event.arguments()[0], irc_server=connection.server) 653 bridges = self.iter_bridges(irc_room=event.arguments()[0], irc_server=connection.server)
654 if len(bridges) > 1: 654 if len(bridges) > 1:
655 raise Exception, 'more than one bridge for one irc chan, WTF ?' 655 raise Exception, 'more than one bridge for one irc chan, WTF ?'
656 bridge = bridges[0] 656 bridge = bridges[0]
657 if connection.get_nickname() == self.nickname: 657 if connection.get_nickname() == self.nickname:
658 bridge._join_irc_failed(event.eventtype()) 658 bridge._join_irc_failed(event.eventtype())
692 return 692 return
693 if event.arguments()[1] != self.nickname or not 'o' in event.arguments()[0]: 693 if event.arguments()[1] != self.nickname or not 'o' in event.arguments()[0]:
694 self.error(1, 'ignoring IRC mode "'+event.arguments()[0]+'" for "'+event.arguments()[1]+'"', debug=True) 694 self.error(1, 'ignoring IRC mode "'+event.arguments()[0]+'" for "'+event.arguments()[1]+'"', debug=True)
695 return 695 return
696 self.error(2, debug_str, debug=True) 696 self.error(2, debug_str, debug=True)
697 bridges = self.get_bridges(irc_room=event.target(), irc_server=connection.server) 697 bridges = self.iter_bridges(irc_room=event.target(), irc_server=connection.server)
698 if len(bridges) > 1: 698 if len(bridges) > 1:
699 raise Exception, 'more than one bridge for one irc chan, WTF ?' 699 raise Exception, 'more than one bridge for one irc chan, WTF ?'
700 bridge = bridges[0] 700 bridge = bridges[0]
701 if re.search('\+[^\-]*o', event.arguments()[0]): 701 if re.search('\+[^\-]*o', event.arguments()[0]):
702 # bot is channel operator 702 # bot is channel operator
737 b = Bridge(self, xmpp_room, irc_room, irc_server, mode, say_level, irc_port=irc_port, irc_connection_interval=irc_connection_interval, irc_charsets=irc_charsets) 737 b = Bridge(self, xmpp_room, irc_room, irc_server, mode, say_level, irc_port=irc_port, irc_connection_interval=irc_connection_interval, irc_charsets=irc_charsets)
738 self.bridges.append(b) 738 self.bridges.append(b)
739 return b 739 return b
740 740
741 741
742 def find_bridges(self, str_array):
743 # TODO: lock self.bridges for thread safety
744 bridges = [b for b in self.bridges]
745 for bridge in self.bridges:
746 for s in str_array:
747 if not s in str(bridge):
748 bridges.remove(bridge)
749 break
750 return bridges
751
752
753 def format_message(self, importance, message): 742 def format_message(self, importance, message):
754 if importance < 0 or importance >= len(say_levels.levels): 743 if importance < 0 or importance >= len(say_levels.levels):
755 raise Exception('[Internal Error] unknown message importance') 744 raise Exception('[Internal Error] unknown message importance')
756 return'['+str(say_levels.get(importance))+'] '+message 745 return'['+str(say_levels.get(importance))+'] '+message
757 746
758 747
759 def get_bridge(self, **kwargs): 748 def get_bridge(self, **kwargs):
760 """Calls self.get_bridges and raises exceptions when there are 0 or more than 1 matches 749 """Calls self.iter_bridges and raises exceptions when there are 0 or more than 1 matches
761 750
762 See Bot.get_bridges for the list of args""" 751 See Bot.iter_bridges for the list of args"""
763 752
764 bridges = self.get_bridges(**kwargs) 753 bridges = [b for b in self.iter_bridges(**kwargs)]
765 if len(bridges) == 0: 754 if len(bridges) == 0:
766 raise Exception, 'no bridge matching '+str(kwargs) 755 raise Exception, 'no bridge matching '+str(kwargs)
767 elif len(bridges) > 1: 756 elif len(bridges) > 1:
768 raise Exception, 'more than one bridge matching '+str(kwargs)+'\n'+'\n'.join([str(b) for b in bridges]) 757 raise Exception, 'more than one bridge matching '+str(kwargs)+'\n'+'\n'.join([str(b) for b in bridges])
769 return bridges[0] 758 return bridges[0]
770 759
771 760
772 def get_bridges(self, irc_room=None, irc_server=None, xmpp_room_jid=None): 761 def iter_bridges(self, irc_room=None, irc_server=None, xmpp_room_jid=None, patterns=None):
773 # TODO: lock self.bridges for thread safety 762 for bridge in self.bridges:
774 bridges = [b for b in self.bridges]
775 for bridge in [b for b in bridges]:
776 if irc_room != None and bridge.irc_room != irc_room: 763 if irc_room != None and bridge.irc_room != irc_room:
777 bridges.remove(bridge)
778 continue 764 continue
779 if irc_server != None and bridge.irc_server != irc_server: 765 if irc_server != None and bridge.irc_server != irc_server:
780 bridges.remove(bridge)
781 continue 766 continue
782 if xmpp_room_jid != None and bridge.xmpp_room_jid != xmpp_room_jid: 767 if xmpp_room_jid != None and bridge.xmpp_room_jid != xmpp_room_jid:
783 bridges.remove(bridge)
784 continue 768 continue
785 return bridges 769 if patterns != None:
770 for pattern in patterns:
771 if not pattern in str(bridge):
772 continue
773 yield bridge
786 774
787 775
788 def get_xmpp_connection(self, nickname): 776 def get_xmpp_connection(self, nickname):
789 if self.xmpp_connections.has_key(nickname): 777 if self.xmpp_connections.has_key(nickname):
790 c = self.xmpp_connections[nickname] 778 c = self.xmpp_connections[nickname]