Mercurial > xib
annotate start_bots_from_xml_config.py @ 204:6388579c701d
auto-reconnect in irclib, restart if bot's IRC connection was lost
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Fri, 26 Feb 2010 20:59:20 +0100 |
parents | 102f895347ff |
children |
rev | line source |
---|---|
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
3 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
4 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
5 # *** LICENSE *** |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
7 # it under the terms of the GNU General Public License as published by |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
9 # (at your option) any later version. |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
10 # |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
14 # GNU General Public License for more details. |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
15 # |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU General Public License |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
18 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
19 |
124 | 20 from xml.dom.minidom import parse |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
21 from time import sleep |
13
a9077fa9cac9
Added optional parameter to start_bots_from_xml_config.py
Charly COSTE <changaco@changaco.net>
parents:
0
diff
changeset
|
22 import sys |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
13
diff
changeset
|
23 import traceback |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
24 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
25 from admin import Admin |
124 | 26 from bot import Bot |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
27 import say_levels |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
28 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
29 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
30 try: |
13
a9077fa9cac9
Added optional parameter to start_bots_from_xml_config.py
Charly COSTE <changaco@changaco.net>
parents:
0
diff
changeset
|
31 if len(sys.argv) > 1: |
a9077fa9cac9
Added optional parameter to start_bots_from_xml_config.py
Charly COSTE <changaco@changaco.net>
parents:
0
diff
changeset
|
32 config = parse(sys.argv[1]) |
a9077fa9cac9
Added optional parameter to start_bots_from_xml_config.py
Charly COSTE <changaco@changaco.net>
parents:
0
diff
changeset
|
33 else: |
a9077fa9cac9
Added optional parameter to start_bots_from_xml_config.py
Charly COSTE <changaco@changaco.net>
parents:
0
diff
changeset
|
34 config = parse('config.xml') |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
35 except IOError: |
19
c1b84196c100
Changed format of non-debug error messages, fixed IRC namreply handling, prevented crash when receiving bad XMPP stanza.
Charly COSTE <changaco@changaco.net>
parents:
17
diff
changeset
|
36 print '[Error] configuration file is missing or cannot be read' |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
37 quit(1) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
38 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
39 bots_jids = [] |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
40 for bot_el in config.getElementsByTagName('bot'): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
41 if bot_el.getAttribute('jid') in bots_jids: |
19
c1b84196c100
Changed format of non-debug error messages, fixed IRC namreply handling, prevented crash when receiving bad XMPP stanza.
Charly COSTE <changaco@changaco.net>
parents:
17
diff
changeset
|
42 print '[Error] you cannot have two bots using the same JID' |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
43 quit(2) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
44 bots_jids.append(bot_el.getAttribute('jid')) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
45 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
46 |
171
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
47 |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
48 bots = [] |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
49 for bot_el in config.getElementsByTagName('bot'): |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
50 debug = False |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
51 if bot_el.hasAttribute('debug'): |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
52 if bot_el.getAttribute('debug') == 'true': |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
53 debug = True |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
54 |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
55 admins = [] |
171
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
56 for admin_el in bot_el.getElementsByTagName('admin'): |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
57 if admin_el.hasAttribute('jid'): |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
58 admin = Admin() |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
59 admin.jid = admin_el.getAttribute('jid') |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
60 if admin_el.hasAttribute('say_level'): |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
61 admin.say_level = say_levels.get(admin_el.getAttribute('say_level')) |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
62 else: |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
63 admin.say_level = say_levels.warning |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
64 admins.append(admin) |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
65 |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
66 bot = Bot(bot_el.getAttribute('jid'), bot_el.getAttribute('password'), bot_el.getAttribute('nickname'), admins=admins, debug=debug) |
171
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
67 bots.append(bot) |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
68 for bridge_el in bot_el.getElementsByTagName('bridge'): |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
69 xmpp_room = bridge_el.getElementsByTagName('xmpp-room')[0] |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
70 irc = bridge_el.getElementsByTagName('irc')[0] |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
71 |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
72 irc_connection_interval = 1 |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
73 if irc.hasAttribute('connection_interval'): |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
74 try: |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
75 irc_connection_interval = float(irc.getAttribute('connection_interval')) |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
76 except ValueError: |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
77 print '[Error] the value of connection_interval must be a number' |
172
64a0e9636ae6
removed encoding.py, charsets handling is now in irclib and custom charsets can be set per server in the configuration file
Charly COSTE <changaco@changaco.net>
parents:
171
diff
changeset
|
78 if irc.hasAttribute('charsets'): |
64a0e9636ae6
removed encoding.py, charsets handling is now in irclib and custom charsets can be set per server in the configuration file
Charly COSTE <changaco@changaco.net>
parents:
171
diff
changeset
|
79 irc_charsets = irc.getAttribute('charsets').split() |
64a0e9636ae6
removed encoding.py, charsets handling is now in irclib and custom charsets can be set per server in the configuration file
Charly COSTE <changaco@changaco.net>
parents:
171
diff
changeset
|
80 else: |
64a0e9636ae6
removed encoding.py, charsets handling is now in irclib and custom charsets can be set per server in the configuration file
Charly COSTE <changaco@changaco.net>
parents:
171
diff
changeset
|
81 irc_charsets = None |
171
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
82 |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
83 if bridge_el.hasAttribute('say_level'): |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
84 say_level = say_levels.get(bridge_el.getAttribute('say_level')) |
171
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
85 else: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
86 say_level = say_levels.nothing |
171
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
87 |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
88 if bridge_el.hasAttribute('mode'): |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
89 mode = bridge_el.getAttribute('mode') |
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
90 else: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
91 mode = 'bypass' |
171
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
92 |
172
64a0e9636ae6
removed encoding.py, charsets handling is now in irclib and custom charsets can be set per server in the configuration file
Charly COSTE <changaco@changaco.net>
parents:
171
diff
changeset
|
93 bot.new_bridge(xmpp_room.getAttribute('jid'), irc.getAttribute('chan'), irc.getAttribute('server'), mode, say_level, irc_connection_interval=irc_connection_interval, irc_charsets=irc_charsets) |
171
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
161
diff
changeset
|
94 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
95 try: |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
96 if len(bots) == 0: |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
97 print 'No bots in the configuration file, exiting ...' |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
98 exit(0) |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
99 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
100 while True: |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
101 for bot in bots: |
161
c021656e4c54
minor fix in start_bots_from_xml_config.py
Charly COSTE <changaco@changaco.net>
parents:
155
diff
changeset
|
102 if bot.halt and len(bot.xmpp_connections) == 0: |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
103 bots.remove(bot) |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
104 if len(bots) == 0: |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
105 raise Exception() |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
106 sleep(10) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
107 except: |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
108 if len(bots) == 0: |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
109 print 'All bots have been shut down, exiting ...' |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
110 exit(0) |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
111 |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
13
diff
changeset
|
112 for bot in bots: |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
113 bots.remove(bot) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
13
diff
changeset
|
114 del bot |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
13
diff
changeset
|
115 traceback.print_exc() |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
116 quit(3) |