Mercurial > xib
comparison bot.py @ 155:63db565438bd
fixed the halt command
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Sun, 17 Jan 2010 23:24:00 +0100 |
parents | 17305e57f71d |
children | 3f299ad4d452 |
comparison
equal
deleted
inserted
replaced
154:25f6778b8841 | 155:63db565438bd |
---|---|
35 | 35 |
36 class Bot(threading.Thread): | 36 class Bot(threading.Thread): |
37 | 37 |
38 def __init__(self, jid, password, nickname, admins_jid=[], error_fd=sys.stderr, debug=False): | 38 def __init__(self, jid, password, nickname, admins_jid=[], error_fd=sys.stderr, debug=False): |
39 threading.Thread.__init__(self) | 39 threading.Thread.__init__(self) |
40 self.halt = False | |
40 self.bare_jid = xmpp.protocol.JID(jid=jid) | 41 self.bare_jid = xmpp.protocol.JID(jid=jid) |
41 self.bare_jid.setResource('') | 42 self.bare_jid.setResource('') |
42 self.nickname = nickname | 43 self.nickname = nickname |
43 self.password = password | 44 self.password = password |
44 self.error_fd = error_fd | 45 self.error_fd = error_fd |
74 | 75 |
75 def _xmpp_loop(self): | 76 def _xmpp_loop(self): |
76 """[Internal] XMPP infinite loop.""" | 77 """[Internal] XMPP infinite loop.""" |
77 i = 1 | 78 i = 1 |
78 while True: | 79 while True: |
80 if self.halt: | |
81 s = len(self.xmpp_connections) | |
82 for i in range(s): | |
83 self.close_xmpp_connection(self.xmpp_connections.keys()[s-i-1], force=True) | |
84 break | |
79 unlock = False | 85 unlock = False |
80 try: | 86 try: |
81 if len(self.xmpp_connections) == 1: | 87 if len(self.xmpp_connections) == 1: |
82 sleep(0.5) # avoid bot connection being locked all the time | 88 sleep(0.5) # avoid bot connection being locked all the time |
83 for j, c in enumerate(self.xmpp_connections.itervalues()): | 89 for j, c in enumerate(self.xmpp_connections.itervalues()): |
778 for m in c.mucs: | 784 for m in c.mucs: |
779 m.rejoin() | 785 m.rejoin() |
780 return c | 786 return c |
781 | 787 |
782 | 788 |
783 def close_xmpp_connection(self, nickname): | 789 def close_xmpp_connection(self, nickname, force=False): |
784 if not self.xmpp_connections.has_key(nickname): | 790 if not self.xmpp_connections.has_key(nickname): |
785 return | 791 return |
786 c = self.xmpp_connections[nickname] | 792 c = self.xmpp_connections[nickname] |
787 c.lock.acquire() | 793 c.lock.acquire() |
788 c.used_by -= 1 | 794 c.used_by -= 1 |
789 if c.used_by < 1: | 795 if c.used_by < 1 or force: |
790 self.error('===> Debug: closing XMPP connection for "'+nickname+'"', debug=True) | 796 self.error('===> Debug: closing XMPP connection for "'+nickname+'"', debug=True) |
791 self.xmpp_connections.pop(nickname) | 797 self.xmpp_connections.pop(nickname) |
792 c.send(xmpp.protocol.Presence(typ='unavailable')) | 798 c.send(xmpp.protocol.Presence(typ='unavailable')) |
793 c.lock.release() | 799 c.lock.release() |
794 del c | 800 del c |
844 self.error(error_message, send_to_admins=True) | 850 self.error(error_message, send_to_admins=True) |
845 | 851 |
846 | 852 |
847 def stop(self, message='Stopping bot'): | 853 def stop(self, message='Stopping bot'): |
848 for bridge in self.bridges: | 854 for bridge in self.bridges: |
855 bridge.stop(message=message) | |
856 | |
857 | |
858 def __del__(self): | |
859 for bridge in self.bridges: | |
849 self.removeBridge(bridge, message=message) | 860 self.removeBridge(bridge, message=message) |
850 | 861 self.halt = True |
851 | |
852 def __del__(self): | |
853 self.stop() |