Mercurial > xib
comparison bot.py @ 86:bfa32b017fc9
First hack at an error notification system
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Sun, 06 Sep 2009 15:34:49 +0200 |
parents | 73a30fc1922b |
children | 8b071629558e |
comparison
equal
deleted
inserted
replaced
85:dfa030c141f1 | 86:bfa32b017fc9 |
---|---|
35 import traceback | 35 import traceback |
36 | 36 |
37 | 37 |
38 class bot(Thread): | 38 class bot(Thread): |
39 | 39 |
40 def __init__(self, jid, password, nickname, error_fd=sys.stderr, debug=False): | 40 def __init__(self, jid, password, nickname, admins_jid=[], error_fd=sys.stderr, debug=False): |
41 Thread.__init__(self) | 41 Thread.__init__(self) |
42 self.commands = ['!xmpp_participants', '!irc_participants'] | 42 self.commands = ['!xmpp_participants', '!irc_participants'] |
43 self.bare_jid = xmpp.protocol.JID(jid=jid) | 43 self.bare_jid = xmpp.protocol.JID(jid=jid) |
44 self.bare_jid.setResource('') | 44 self.bare_jid.setResource('') |
45 self.nickname = nickname | 45 self.nickname = nickname |
46 self.password = password | 46 self.password = password |
47 self.error_fd = error_fd | 47 self.error_fd = error_fd |
48 self.debug = debug | 48 self.debug = debug |
49 self.admins_jid = admins_jid | |
49 self.bridges = [] | 50 self.bridges = [] |
50 self.xmpp_connections = {} | 51 self.xmpp_connections = {} |
51 self.irc = irclib.IRC() | 52 self.irc = irclib.IRC() |
52 self.irc.bot = self | 53 self.irc.bot = self |
53 self.irc.add_global_handler('all_events', self._irc_event_handler) | 54 self.irc.add_global_handler('all_events', self._irc_event_handler) |
61 raise | 62 raise |
62 self.xmpp_thread = Thread(target=self._xmpp_loop) | 63 self.xmpp_thread = Thread(target=self._xmpp_loop) |
63 self.xmpp_thread.start() | 64 self.xmpp_thread.start() |
64 | 65 |
65 | 66 |
66 def error(self, s, debug=False): | 67 def error(self, s, debug=False, send_to_admins=False): |
67 """Output an error message.""" | 68 """Output an error message.""" |
69 if send_to_admins == True: | |
70 self._send_message_to_admins(s) | |
68 if not debug or debug and self.debug: | 71 if not debug or debug and self.debug: |
69 try: | 72 try: |
70 self.error_fd.write(auto_encode(s)+"\n") | 73 self.error_fd.write(auto_encode(s)+"\n") |
71 except EncodingException: | 74 except EncodingException: |
72 self.error_fd.write('Error message cannot be transcoded.\n') | 75 self.error_fd.write('Error message cannot be transcoded.\n') |
100 pass | 103 pass |
101 except (xml.parsers.expat.ExpatError, xmpp.protocol.XMLNotWellFormed): | 104 except (xml.parsers.expat.ExpatError, xmpp.protocol.XMLNotWellFormed): |
102 self.error('=> Debug: invalid stanza', debug=True) | 105 self.error('=> Debug: invalid stanza', debug=True) |
103 unlock = True | 106 unlock = True |
104 except: | 107 except: |
105 self.error('[Error] Unkonwn exception on XMPP thread:') | 108 error = '[Error] Unkonwn exception on XMPP thread:\n' |
106 traceback.print_exc() | 109 error += traceback.format_exc() |
110 self.error(error, send_to_admins=True) | |
107 unlock = True | 111 unlock = True |
108 if unlock == True: | 112 if unlock == True: |
109 c.lock.release() | 113 c.lock.release() |
110 | 114 |
111 | 115 |
501 return | 505 return |
502 | 506 |
503 | 507 |
504 # Unhandled events | 508 # Unhandled events |
505 self.error('=> Debug: event not handled', debug=True) | 509 self.error('=> Debug: event not handled', debug=True) |
510 | |
511 | |
512 def _send_message_to_admins(self, message): | |
513 """[Internal] Send XMPP Message to bot admin(s)""" | |
514 for admin_jid in self.admins_jid: | |
515 try: | |
516 self.xmpp_c.send(xmpp.protocol.Message(to=admin_jid, body=message, typ='chat')) | |
517 except: | |
518 pass | |
506 | 519 |
507 | 520 |
508 def new_bridge(self, xmpp_room, irc_room, irc_server, mode, say_level, irc_port=6667): | 521 def new_bridge(self, xmpp_room, irc_room, irc_server, mode, say_level, irc_port=6667): |
509 """Create a bridge between xmpp_room and irc_room at irc_server.""" | 522 """Create a bridge between xmpp_room and irc_room at irc_server.""" |
510 b = bridge(self, xmpp_room, irc_room, irc_server, mode, say_level, irc_port=irc_port) | 523 b = bridge(self, xmpp_room, irc_room, irc_server, mode, say_level, irc_port=irc_port) |