Mercurial > xib
comparison bot.py @ 75:6034087b1d10
First hack at pings
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Thu, 03 Sep 2009 18:56:58 +0200 |
parents | 6c4b841144f6 |
children | 686724c5183c |
comparison
equal
deleted
inserted
replaced
74:676c2d2bf240 | 75:6034087b1d10 |
---|---|
21 | 21 |
22 version = 0, 1 | 22 version = 0, 1 |
23 | 23 |
24 | 24 |
25 import irclib | 25 import irclib |
26 import xmppony as xmpp | 26 import muc |
27 xmpp = muc.xmpp | |
28 del muc | |
27 import threading | 29 import threading |
28 from bridge import * | 30 from bridge import * |
29 from time import sleep | 31 from time import sleep |
30 import re | 32 import re |
31 import sys | 33 import sys |
70 self.error_fd.write('Error message cannot be transcoded.\n') | 72 self.error_fd.write('Error message cannot be transcoded.\n') |
71 | 73 |
72 | 74 |
73 def _xmpp_loop(self): | 75 def _xmpp_loop(self): |
74 """[Internal] XMPP infinite loop.""" | 76 """[Internal] XMPP infinite loop.""" |
77 i = 1 | |
75 while True: | 78 while True: |
76 try: | 79 try: |
77 if len(self.xmpp_connections) == 1: | 80 if len(self.xmpp_connections) == 1: |
78 sleep(0.5) # avoid bot connection being locked all the time | 81 sleep(0.5) # avoid bot connection being locked all the time |
82 j = 0 | |
79 for c in self.xmpp_connections.itervalues(): | 83 for c in self.xmpp_connections.itervalues(): |
84 i += 1 | |
85 j += 1 | |
80 if hasattr(c, 'lock'): | 86 if hasattr(c, 'lock'): |
81 c.lock.acquire() | 87 c.lock.acquire() |
88 if i == j: | |
89 ping = xmpp.protocol.Iq(typ='get') | |
90 ping.addChild(name='ping', namespace='urn:xmpp:ping') | |
91 self.error('=> Debug: sending XMPP ping', debug=True) | |
92 c.pings.append(c.send(ping)) | |
82 if hasattr(c, 'Process'): | 93 if hasattr(c, 'Process'): |
83 c.Process(0.01) | 94 c.Process(0.01) |
84 c.lock.release() | 95 c.lock.release() |
96 if i > 5000: | |
97 i = 0 | |
85 except RuntimeError: | 98 except RuntimeError: |
86 pass | 99 pass |
87 except (xml.parsers.expat.ExpatError, xmpp.protocol.XMLNotWellFormed): | 100 except (xml.parsers.expat.ExpatError, xmpp.protocol.XMLNotWellFormed): |
88 self.error('=> Debug: invalid stanza', debug=True) | 101 self.error('=> Debug: invalid stanza', debug=True) |
89 continue | |
90 except: | 102 except: |
91 self.error('[Error] Unkonwn exception on XMPP thread:') | 103 self.error('[Error] Unkonwn exception on XMPP thread:') |
92 traceback.print_exc() | 104 traceback.print_exc() |
93 | 105 |
94 | 106 |
148 return | 160 return |
149 | 161 |
150 | 162 |
151 def _xmpp_iq_handler(self, dispatcher, iq): | 163 def _xmpp_iq_handler(self, dispatcher, iq): |
152 """[Internal] Manage XMPP IQs.""" | 164 """[Internal] Manage XMPP IQs.""" |
153 self.error('=> Debug: Received XMPP iq.', debug=True) | 165 |
166 xmpp_c = dispatcher._owner | |
167 | |
168 # Ignore pongs | |
169 if iq.getType() in ['result', 'error'] and iq.getID() in xmpp_c.pings: | |
170 xmpp_c.pings.remove(iq.getID()) | |
171 self.error('=> Debug: received XMPP pong', debug=True) | |
172 return | |
173 | |
174 self.error('==> Debug: Received XMPP iq.', debug=True) | |
154 self.error(iq.__str__(fancy=1), debug=True) | 175 self.error(iq.__str__(fancy=1), debug=True) |
155 | 176 |
156 | 177 |
157 def _xmpp_message_handler(self, dispatcher, message): | 178 def _xmpp_message_handler(self, dispatcher, message): |
158 """[Internal] Manage XMPP messages.""" | 179 """[Internal] Manage XMPP messages.""" |
241 | 262 |
242 # Events we always want to ignore | 263 # Events we always want to ignore |
243 if 'all' in event.eventtype() or 'motd' in event.eventtype(): | 264 if 'all' in event.eventtype() or 'motd' in event.eventtype(): |
244 return | 265 return |
245 if event.eventtype() in ['pong', 'privnotice', 'ctcp', 'nochanmodes', 'notexttosend', 'currenttopic', 'topicinfo']: | 266 if event.eventtype() in ['pong', 'privnotice', 'ctcp', 'nochanmodes', 'notexttosend', 'currenttopic', 'topicinfo']: |
246 self.error('=> Debug: ignoring '+event.eventtype(), debug=True) | 267 self.error('=> Debug: ignoring IRC '+event.eventtype(), debug=True) |
247 return | 268 return |
248 | 269 |
249 | 270 |
250 nickname = None | 271 nickname = None |
251 if event.source() != None: | 272 if event.source() != None: |
513 c.lock = threading.RLock() | 534 c.lock = threading.RLock() |
514 c.lock.acquire() | 535 c.lock.acquire() |
515 self.xmpp_connections[nickname] = c | 536 self.xmpp_connections[nickname] = c |
516 c.used_by = 1 | 537 c.used_by = 1 |
517 c.nickname = nickname | 538 c.nickname = nickname |
539 c.pings = [] | |
518 c.connect() | 540 c.connect() |
519 c.auth(self.bare_jid.getNode(), self.password) | 541 c.auth(self.bare_jid.getNode(), self.password) |
520 c.RegisterHandler('presence', self._xmpp_presence_handler) | 542 c.RegisterHandler('presence', self._xmpp_presence_handler) |
521 c.RegisterHandler('iq', self._xmpp_iq_handler) | 543 c.RegisterHandler('iq', self._xmpp_iq_handler) |
522 c.RegisterHandler('message', self._xmpp_message_handler) | 544 c.RegisterHandler('message', self._xmpp_message_handler) |