Mercurial > xib
comparison bot.py @ 201:de574314990e
log some more things when Bot.debug is False
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Thu, 25 Feb 2010 20:48:33 +0100 |
parents | 740effa74c18 |
children | 2a1ee46f86af |
comparison
equal
deleted
inserted
replaced
200:740effa74c18 | 201:de574314990e |
---|---|
60 raise | 60 raise |
61 self.xmpp_thread = threading.Thread(target=self._xmpp_loop) | 61 self.xmpp_thread = threading.Thread(target=self._xmpp_loop) |
62 self.xmpp_thread.start() | 62 self.xmpp_thread.start() |
63 | 63 |
64 | 64 |
65 def error(self, importance, message, debug=False, send_to_admins=False): | 65 def error(self, importance, message, debug=False, no_debug_add='', send_to_admins=False): |
66 """Output an error message.""" | 66 """Output an error message.""" |
67 if not self.debug: | |
68 message += no_debug_add | |
67 if send_to_admins == True: | 69 if send_to_admins == True: |
68 self._send_message_to_admins(importance, message) | 70 self._send_message_to_admins(importance, message) |
69 if importance == -1: | 71 if importance == -1: |
70 return | 72 return |
71 if not debug: | 73 if not debug: |
72 self.error_fd.write(self.format_message(importance, message).encode('utf-8')+'\n') | 74 self.error_fd.write(self.format_message(importance, message).encode('utf-8')+'\n') |
73 if debug and self.debug: | 75 elif self.debug: |
74 self.error_fd.write('='*importance+'> '+message.encode('utf-8')+'\n') | 76 self.error_fd.write('='*importance+'> '+message.encode('utf-8')+'\n') |
75 | 77 |
76 | 78 |
77 def _xmpp_loop(self): | 79 def _xmpp_loop(self): |
78 """[Internal] XMPP infinite loop.""" | 80 """[Internal] XMPP infinite loop.""" |
128 self.error(1, 'Skipping XMPP presence not received on bot connection.', debug=True) | 130 self.error(1, 'Skipping XMPP presence not received on bot connection.', debug=True) |
129 return | 131 return |
130 | 132 |
131 self.error(2, 'Received XMPP presence.\n'+presence.__str__(fancy=1), debug=True) | 133 self.error(2, 'Received XMPP presence.\n'+presence.__str__(fancy=1), debug=True) |
132 | 134 |
135 no_debug_add = '\n'+presence.__str__(fancy=1) | |
136 | |
133 from_ = xmpp.protocol.JID(presence.getFrom()) | 137 from_ = xmpp.protocol.JID(presence.getFrom()) |
134 bare_jid = unicode(from_.getNode()+'@'+from_.getDomain()) | 138 bare_jid = unicode(from_.getNode()+'@'+from_.getDomain()) |
135 for bridge in self.bridges: | 139 for bridge in self.bridges: |
136 if bare_jid == bridge.xmpp_room_jid: | 140 if bare_jid == bridge.xmpp_room_jid: |
137 # presence comes from a muc | 141 # presence comes from a muc |
189 return | 193 return |
190 if p.protocol != 'xmpp': | 194 if p.protocol != 'xmpp': |
191 return | 195 return |
192 item = x.getTag('item') | 196 item = x.getTag('item') |
193 if not item: | 197 if not item: |
194 self.error(1, 'bad stanza, no item element', debug=True) | 198 self.error(say_levels.debug, 'bad stanza, no item element', no_debug_add=no_debug_add) |
195 return | 199 return |
196 new_nick = item.getAttr('nick') | 200 new_nick = item.getAttr('nick') |
197 if not new_nick: | 201 if not new_nick: |
198 self.error(1, 'bad stanza, new nick is not given', debug=True) | 202 self.error(say_levels.debug, 'bad stanza, new nick is not given', no_debug_add=no_debug_add) |
199 return | 203 return |
200 p.changeNickname(new_nick, 'irc') | 204 p.changeNickname(new_nick, 'irc') |
201 | 205 |
202 elif x and x.getTag('status', attrs={'code': '307'}): | 206 elif x and x.getTag('status', attrs={'code': '307'}): |
203 # participant was kicked | 207 # participant was kicked |
285 | 289 |
286 return | 290 return |
287 | 291 |
288 return | 292 return |
289 | 293 |
290 if self.debug: | 294 self.error(say_levels.debug, 'Unhandled XMPP presence', no_debug_add='\n'+presence.__str__(fancy=1)) |
291 self.error(1, 'presence was not handled', debug=True) | |
292 else: | |
293 self.error(say_levels.debug, 'Unhandled XMPP presence:\n'+message.__str__(fancy=1)) | |
294 | 295 |
295 | 296 |
296 def _xmpp_iq_handler(self, dispatcher, iq): | 297 def _xmpp_iq_handler(self, dispatcher, iq): |
297 """[Internal] Manage XMPP IQs.""" | 298 """[Internal] Manage XMPP IQs.""" |
298 | 299 |
337 self.error(2, 'Sending\n'+s.__str__(fancy=1), debug=True) | 338 self.error(2, 'Sending\n'+s.__str__(fancy=1), debug=True) |
338 xmpp_c.send(s) | 339 xmpp_c.send(s) |
339 else: | 340 else: |
340 self.error(1, 'won\'t answer.', debug=True) | 341 self.error(1, 'won\'t answer.', debug=True) |
341 return | 342 return |
342 self.error(1, 'XMPP chat message not relayed', debug=True) | 343 self.error(say_levels.debug, 'XMPP chat message not relayed', no_debug_add='\n'+message.__str__(fancy=1)) |
343 return | 344 return |
344 | 345 |
345 # message does not come from a room | 346 # message does not come from a room |
346 if xmpp_c.nickname == self.nickname: | 347 if xmpp_c.nickname == self.nickname: |
347 self.error(2, 'Received XMPP chat message.\n'+message.__str__(fancy=1), debug=True) | 348 self.error(2, 'Received XMPP chat message.\n'+message.__str__(fancy=1), debug=True) |
396 | 397 |
397 try: | 398 try: |
398 participant = bridge.getParticipant(resource) | 399 participant = bridge.getParticipant(resource) |
399 except Bridge.NoSuchParticipantException: | 400 except Bridge.NoSuchParticipantException: |
400 if resource != self.nickname: | 401 if resource != self.nickname: |
401 self.error(1, 'NoSuchParticipantException "'+resource+'" on "'+str(bridge)+'", WTF ?', debug=True) | 402 self.error(say_levels.debug, 'NoSuchParticipantException "'+resource+'" on "'+str(bridge)+'", WTF ?', no_debug_add='\n'+message.__str__(fancy=1)) |
402 return | 403 return |
403 | 404 |
404 participant.sayOnIRC(message.getBody()) | 405 participant.sayOnIRC(message.getBody()) |
405 return | 406 return |
406 | 407 |
476 self.error(1, 'ignoring '+event.eventtype(), debug=True) | 477 self.error(1, 'ignoring '+event.eventtype(), debug=True) |
477 return | 478 return |
478 | 479 |
479 | 480 |
480 # A string representation of the event | 481 # A string representation of the event |
481 event_str = 'connection='+connection.__str__()+'\neventtype='+event.eventtype()+'\nsource='+repr(event.source())+'\ntarget='+repr(event.target())+'\narguments='+repr(event.arguments()) | 482 event_str = '\nconnection='+connection.__str__()+'\neventtype='+event.eventtype()+'\nsource='+repr(event.source())+'\ntarget='+repr(event.target())+'\narguments='+repr(event.arguments()) |
482 debug_str = 'Received IRC event.\n'+event_str | 483 debug_str = 'Received IRC event.'+event_str |
483 printed_event = False | 484 printed_event = False |
484 | 485 |
485 | 486 |
486 if event.eventtype() in ['pubmsg', 'action', 'privmsg', 'quit', 'part', 'nick', 'kick']: | 487 if event.eventtype() in ['pubmsg', 'action', 'privmsg', 'quit', 'part', 'nick', 'kick']: |
487 if nickname == None: | 488 if nickname == None: |
499 else: | 500 else: |
500 self.error(2, debug_str, debug=True) | 501 self.error(2, debug_str, debug=True) |
501 printed_event = True | 502 printed_event = True |
502 | 503 |
503 if event.eventtype() == 'kick' and len(event.arguments()) < 1: | 504 if event.eventtype() == 'kick' and len(event.arguments()) < 1: |
504 self.error(1, 'at least 1 argument is needed for a '+event.eventtype()+' event', debug=True) | 505 self.error(say_levels.debug, 'at least 1 argument is needed for a '+event.eventtype()+' event', no_debug_add=event_str) |
505 return | 506 return |
506 | 507 |
507 if event.eventtype() in ['pubmsg', 'action']: | 508 if event.eventtype() in ['pubmsg', 'action']: |
508 if connection.get_nickname() != self.nickname: | 509 if connection.get_nickname() != self.nickname: |
509 self.error(1, 'ignoring IRC '+event.eventtype()+' not received on bot connection', debug=True) | 510 self.error(1, 'ignoring IRC '+event.eventtype()+' not received on bot connection', debug=True) |
557 bridge.removeParticipant('irc', kicked.nickname, 'Kicked by '+nickname+' with reason: '+event.arguments()[1]) | 558 bridge.removeParticipant('irc', kicked.nickname, 'Kicked by '+nickname+' with reason: '+event.arguments()[1]) |
558 else: | 559 else: |
559 bridge.removeParticipant('irc', kicked.nickname, 'Kicked by '+nickname+' (no reason was given)') | 560 bridge.removeParticipant('irc', kicked.nickname, 'Kicked by '+nickname+' (no reason was given)') |
560 return | 561 return |
561 except Bridge.NoSuchParticipantException: | 562 except Bridge.NoSuchParticipantException: |
562 self.error(1, 'a participant that was not here has been kicked ? WTF ?', debug=True) | 563 self.error(say_levels.debug, 'a participant that was not here has been kicked ? WTF ?', no_debug_add=event_str) |
563 return | 564 return |
564 else: | 565 else: |
565 continue | 566 continue |
566 | 567 |
567 | 568 |
707 return | 708 return |
708 | 709 |
709 | 710 |
710 # Unhandled events | 711 # Unhandled events |
711 if not printed_event: | 712 if not printed_event: |
712 self.error(say_levels.debug, 'The following IRC event was not handled:\n'+event_str+'\n', send_to_admins=True) | 713 self.error(say_levels.debug, 'The following IRC event was not handled:'+event_str+'\n', send_to_admins=True) |
713 else: | 714 else: |
714 self.error(1, 'event not handled', debug=True) | 715 self.error(1, 'event not handled', debug=True) |
715 self._send_message_to_admins(say_levels.debug, 'The following IRC event was not handled:\n'+event_str) | 716 self._send_message_to_admins(say_levels.debug, 'The following IRC event was not handled:'+event_str) |
716 | 717 |
717 | 718 |
718 def _send_message_to_admins(self, importance, message): | 719 def _send_message_to_admins(self, importance, message): |
719 """[Internal] Send XMPP Message to bot admin(s)""" | 720 """[Internal] Send XMPP Message to bot admin(s)""" |
720 for admin in self.admins: | 721 for admin in self.admins: |