Mercurial > xib
comparison bot.py @ 19:c1b84196c100
Changed format of non-debug error messages, fixed IRC namreply handling, prevented crash when receiving bad XMPP stanza.
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Thu, 20 Aug 2009 13:20:50 +0200 |
parents | 3cdf7bb580da |
children | 08cde283621a |
comparison
equal
deleted
inserted
replaced
18:3cdf7bb580da | 19:c1b84196c100 |
---|---|
53 self.irc_thread.start() | 53 self.irc_thread.start() |
54 # Open connection with XMPP server | 54 # Open connection with XMPP server |
55 try: | 55 try: |
56 self.xmpp_c = self.get_xmpp_connection(self.jid.getResource()) | 56 self.xmpp_c = self.get_xmpp_connection(self.jid.getResource()) |
57 except: | 57 except: |
58 self.error('Error: XMPP Connection failed') | 58 self.error('[Error] XMPP Connection failed') |
59 raise | 59 raise |
60 self.xmpp_thread = Thread(target=self._xmpp_loop) | 60 self.xmpp_thread = Thread(target=self._xmpp_loop) |
61 self.xmpp_thread.start() | 61 self.xmpp_thread.start() |
62 | 62 |
63 | 63 |
71 | 71 |
72 | 72 |
73 def _xmpp_loop(self): | 73 def _xmpp_loop(self): |
74 """[Internal] XMPP infinite loop.""" | 74 """[Internal] XMPP infinite loop.""" |
75 while True: | 75 while True: |
76 self.xmpp_c.Process(0.5) | |
77 try: | 76 try: |
78 for c in self.xmpp_connections.itervalues(): | 77 self.xmpp_c.Process(0.5) |
79 if hasattr(c, 'Process'): | 78 try: |
80 c.Process(0.5) | 79 for c in self.xmpp_connections.itervalues(): |
81 else: | 80 if hasattr(c, 'Process'): |
82 sleep(0.5) | 81 c.Process(0.5) |
83 except RuntimeError: | 82 else: |
84 pass | 83 sleep(0.5) |
84 except RuntimeError: | |
85 pass | |
86 except ExpatError: | |
87 self.error('=> Debug: received invalid stanza', debug=True) | |
88 continue | |
85 | 89 |
86 | 90 |
87 def _xmpp_presence_handler(self, xmpp_c, presence): | 91 def _xmpp_presence_handler(self, xmpp_c, presence): |
88 """[Internal] Manage XMPP presence.""" | 92 """[Internal] Manage XMPP presence.""" |
89 | 93 |
121 x = presence.getTag('x', namespace='http://jabber.org/protocol/muc#user') | 125 x = presence.getTag('x', namespace='http://jabber.org/protocol/muc#user') |
122 if x and x.getTag('status', attrs={'code': '303'}): | 126 if x and x.getTag('status', attrs={'code': '303'}): |
123 # participant changed its nickname | 127 # participant changed its nickname |
124 item = x.getTag('item') | 128 item = x.getTag('item') |
125 if not item: | 129 if not item: |
126 self.error('Debug: bad stanza, no item element', debug=True) | 130 self.error('=> Debug: bad stanza, no item element', debug=True) |
127 return | 131 return |
128 new_nick = item.getAttr('nick') | 132 new_nick = item.getAttr('nick') |
129 if not new_nick: | 133 if not new_nick: |
130 self.error('Debug: bad stanza, new nick is not given', debug=True) | 134 self.error('=> Debug: bad stanza, new nick is not given', debug=True) |
131 return | 135 return |
132 p.changeNickname(new_nick, 'irc') | 136 p.changeNickname(new_nick, 'irc') |
133 | 137 |
134 else: | 138 else: |
135 # participant left | 139 # participant left |
325 return | 329 return |
326 | 330 |
327 if event.eventtype() == 'namreply': | 331 if event.eventtype() == 'namreply': |
328 # TODO: lock self.bridges for thread safety | 332 # TODO: lock self.bridges for thread safety |
329 for bridge in self.getBridges(irc_room=event.arguments()[1], irc_server=connection.server): | 333 for bridge in self.getBridges(irc_room=event.arguments()[1], irc_server=connection.server): |
330 for nickname in re.split('(?:^[@\+]?|(?: [@\+]?)*)', event.arguments()[2].strip()): | 334 for nickname in re.split('(?:^[&@\+]?|(?: [&@\+]?)*)', event.arguments()[2].strip()): |
331 if nickname == '' or nickname == self.nickname: | 335 if nickname == '' or nickname == self.nickname: |
332 continue | 336 continue |
333 bridge.addParticipant('irc', nickname) | 337 bridge.addParticipant('irc', nickname) |
334 return | 338 return |
335 elif event.eventtype() == 'join': | 339 elif event.eventtype() == 'join': |