Mercurial > xib
comparison commands.py @ 160:266a6afb6b79
changed the commands that modify bridges
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Tue, 19 Jan 2010 09:28:11 +0100 |
parents | ce0eb3e969a7 |
children | d3b619cd60fb |
comparison
equal
deleted
inserted
replaced
159:da2f2dc61e74 | 160:266a6afb6b79 |
---|---|
24 | 24 |
25 from bridge import Bridge | 25 from bridge import Bridge |
26 | 26 |
27 | 27 |
28 commands = ['xmpp-participants', 'irc-participants', 'xmpp-connections', 'irc-connections', 'connections', 'bridges'] | 28 commands = ['xmpp-participants', 'irc-participants', 'xmpp-connections', 'irc-connections', 'connections', 'bridges'] |
29 admin_commands = ['add-bridge', 'add-xmpp-admin', 'change-bridge-mode', 'debug', 'halt', 'remove-bridge', 'restart-bot', 'restart-bridge', 'stop-bot', 'stop-bridge'] | 29 admin_commands = ['add-bridge', 'add-xmpp-admin', 'change-bridges-mode', 'debug', 'halt', 'remove-bridges', 'restart-bot', 'restart-bridges', 'stop-bot', 'stop-bridges'] |
30 | 30 |
31 def execute(bot, command_line, bot_admin, bridge): | 31 def execute(bot, command_line, bot_admin, bridge): |
32 ret = '' | |
33 command = shlex.split(command_line) | 32 command = shlex.split(command_line) |
34 if len(command) > 1: | 33 if len(command) > 1: |
35 args_array = command[1:] | 34 args_array = command[1:] |
36 else: | 35 else: |
37 args_array = [] | 36 args_array = [] |
45 else: | 44 else: |
46 return ret | 45 return ret |
47 elif command in admin_commands and not bot_admin: | 46 elif command in admin_commands and not bot_admin: |
48 return 'You have to be a bot admin to use this command.' | 47 return 'You have to be a bot admin to use this command.' |
49 | 48 |
50 b = bridge | 49 return globals()[command_func](bot, command, args_array, bridge) |
51 | 50 |
52 if command in ['change-bridge-mode', 'remove-bridge', 'restart-bridge', 'stop-bridge']: | 51 |
53 # we need to know which bridge the command is for | 52 def _find_bridges(bot, args_array): |
54 if len(args_array) == 0: | 53 ret = '' |
55 if bridge: | 54 b = [] |
56 b = bridge | 55 for arg in args_array: |
56 try: | |
57 bn = int(arg) | |
58 if bn < 1: | |
59 raise IndexError | |
60 b.append(bot.bridges[bn-1]) | |
61 except IndexError: | |
62 ret += '\nInvalid bridge number "'+str(bn)+'".' | |
63 except ValueError: | |
64 found_bridges = bot.findBridges(arg) | |
65 if len(found_bridges) == 0: | |
66 ret += '\nNo bridge found matching "'+arg+'".' | |
57 else: | 67 else: |
58 return 'You must specify a bridge. '+bridges(bot, 'bridges', [], bot_admin, None) | 68 b.extend(found_bridges) |
59 else: | 69 |
60 try: | 70 if ret != '' or len(b) == 0: |
61 bn = int(args_array[0]) | 71 if ret != '': |
62 if bn < 1: | 72 ret += '\n\n' |
63 raise IndexError | 73 ret += bridges(bot, 'bridges', [], None)+'\n\n' |
64 b = bot.bridges[bn-1] | 74 |
65 except IndexError: | 75 return (b, ret) |
66 return 'Invalid bridge number "'+str(bn)+'". '+bridges(bot, 'bridges', [], bot_admin, None) | 76 |
67 except ValueError: | 77 |
68 bridges = bot.findBridges(args_array[0]) | 78 def add_bridge(bot, command, args_array, bridge): |
69 if len(bridges) == 0: | |
70 return 'No bridge found matching "'+args_array[0]+'". '+bridges(bot, 'bridges', [], bot_admin, None) | |
71 elif len(bridges) == 1: | |
72 b = bridges[0] | |
73 elif len(bridges) > 1: | |
74 return 'More than one bridge matches "'+args_array[0]+'", please be more specific. '+bridges(bot, 'bridges', [], bot_admin, None) | |
75 | |
76 | |
77 return globals()[command_func](bot, command, args_array, bot_admin, b) | |
78 | |
79 | |
80 def add_bridge(bot, command, args_array, bot_admin, bridge): | |
81 parser = ArgumentParser(prog=command) | 79 parser = ArgumentParser(prog=command) |
82 parser.add_argument('xmpp_room_jid', type=str) | 80 parser.add_argument('xmpp_room_jid', type=str) |
83 parser.add_argument('irc_chan', type=str) | 81 parser.add_argument('irc_chan', type=str) |
84 parser.add_argument('irc_server', type=str) | 82 parser.add_argument('irc_server', type=str) |
85 parser.add_argument('--mode', choices=Bridge._modes, default='normal') | 83 parser.add_argument('--mode', choices=Bridge._modes, default='normal') |
93 bot.new_bridge(args.xmpp_room_jid, args.irc_chan, args.irc_server, args.mode, args.say_level, irc_port=args.irc_port) | 91 bot.new_bridge(args.xmpp_room_jid, args.irc_chan, args.irc_server, args.mode, args.say_level, irc_port=args.irc_port) |
94 | 92 |
95 return 'Bridge added.' | 93 return 'Bridge added.' |
96 | 94 |
97 | 95 |
98 def add_xmpp_admin(bot, command, args_array, bot_admin, bridge): | 96 def add_xmpp_admin(bot, command, args_array, bridge): |
99 parser = ArgumentParser(prog=command) | 97 parser = ArgumentParser(prog=command) |
100 parser.add_argument('jid', type=str) | 98 parser.add_argument('jid', type=str) |
101 try: | 99 try: |
102 args = parser.parse_args(args_array) | 100 args = parser.parse_args(args_array) |
103 except ArgumentParser.ParseException as e: | 101 except ArgumentParser.ParseException as e: |
109 p.bot_admin = True | 107 p.bot_admin = True |
110 | 108 |
111 return 'XMPP admin added.' | 109 return 'XMPP admin added.' |
112 | 110 |
113 | 111 |
114 def bridges(bot, command, args_array, bot_admin, bridge): | 112 def bridges(bot, command, args_array, bridge): |
115 parser = ArgumentParser(prog=command) | 113 parser = ArgumentParser(prog=command) |
116 parser.add_argument('--show-mode', default=False, action='store_true') | 114 parser.add_argument('--show-mode', default=False, action='store_true') |
117 parser.add_argument('--show-say-level', default=False, action='store_true') | 115 parser.add_argument('--show-say-level', default=False, action='store_true') |
118 parser.add_argument('--show-participants', default=False, action='store_true') | 116 parser.add_argument('--show-participants', default=False, action='store_true') |
119 try: | 117 try: |
131 xmpp_participants_nicknames = b.get_participants_nicknames_list(protocols=['xmpp']) | 129 xmpp_participants_nicknames = b.get_participants_nicknames_list(protocols=['xmpp']) |
132 ret += '\nparticipants on XMPP ('+str(len(xmpp_participants_nicknames))+'): '+' '.join(xmpp_participants_nicknames) | 130 ret += '\nparticipants on XMPP ('+str(len(xmpp_participants_nicknames))+'): '+' '.join(xmpp_participants_nicknames) |
133 irc_participants_nicknames = b.get_participants_nicknames_list(protocols=['irc']) | 131 irc_participants_nicknames = b.get_participants_nicknames_list(protocols=['irc']) |
134 ret += '\nparticipants on IRC ('+str(len(irc_participants_nicknames))+'): '+' '.join(irc_participants_nicknames) | 132 ret += '\nparticipants on IRC ('+str(len(irc_participants_nicknames))+'): '+' '.join(irc_participants_nicknames) |
135 if b.irc_connection == None: | 133 if b.irc_connection == None: |
136 ret += ' - this bridge is stopped, use "restart-bridge '+str(i+1)+'" to restart it' | 134 ret += ' - this bridge is stopped, use "restart-bridges '+str(i+1)+'" to restart it' |
137 return ret | 135 return ret |
138 | 136 |
139 | 137 |
140 def change_bridge_mode(bot, command, args_array, bot_admin, bridge): | 138 def change_bridges_mode(bot, command, args_array, bridge): |
141 new_mode = args_array[1] | 139 parser = ArgumentParser(prog=command) |
142 if not new_mode in Bridge._modes: | 140 parser.add_argument('bridge_id', nargs='+') |
143 return '"'+new_mode+'" is not a valid mode, list of modes: '+' '.join(Bridge._modes) | 141 parser.add_argument('new_mode', choices=Bridge._modes) |
144 r = bridge.changeMode(new_mode) | 142 try: |
145 if r: | 143 args = parser.parse_args(args_array) |
146 return r | 144 except ArgumentParser.ParseException as e: |
147 return 'Mode changed.' | 145 return '\n'+e.args[1] |
148 | 146 |
149 | 147 found_bridges, ret = _find_bridges(bot, args.bridge_id) |
150 def connections(bot, command, args_array, bot_admin, bridge): | 148 for found_bridge in found_bridges: |
149 r = found_bridge.changeMode(args.new_mode) | |
150 if r: | |
151 ret += r | |
152 | |
153 if ret: | |
154 return ret | |
155 return 'Modes changed.' | |
156 | |
157 | |
158 def connections(bot, command, args_array, bridge): | |
151 parser = ArgumentParser(prog=command) | 159 parser = ArgumentParser(prog=command) |
152 parser.add_argument('--verbose', '-v', default=False, action='store_true') | 160 parser.add_argument('--verbose', '-v', default=False, action='store_true') |
153 try: | 161 try: |
154 args = parser.parse_args(args_array) | 162 args = parser.parse_args(args_array) |
155 except ArgumentParser.ParseException as e: | 163 except ArgumentParser.ParseException as e: |
156 return '\n'+e.args[1] | 164 return '\n'+e.args[1] |
157 return irc_connections(bot, 'irc-connections', args_array, bot_admin, bridge)+'\n'+xmpp_connections(bot, 'xmpp-connections', args_array, bot_admin, bridge) | 165 return irc_connections(bot, 'irc-connections', args_array, bridge)+'\n'+xmpp_connections(bot, 'xmpp-connections', args_array, bridge) |
158 | 166 |
159 | 167 |
160 def debug(bot, command, args_array, bot_admin, bridge): | 168 def debug(bot, command, args_array, bridge): |
161 parser = ArgumentParser(prog=command) | 169 parser = ArgumentParser(prog=command) |
162 parser.add_argument('mode', choices=['on', 'off']) | 170 parser.add_argument('mode', choices=['on', 'off']) |
163 try: | 171 try: |
164 args = parser.parse_args(args_array) | 172 args = parser.parse_args(args_array) |
165 except ArgumentParser.ParseException as e: | 173 except ArgumentParser.ParseException as e: |
177 else: | 185 else: |
178 bot.debug = False | 186 bot.debug = False |
179 return 'Debugging is now off' | 187 return 'Debugging is now off' |
180 | 188 |
181 | 189 |
182 def halt(bot, command, args_array, bot_admin, bridge): | 190 def halt(bot, command, args_array, bridge): |
183 bot.__del__() | 191 bot.__del__() |
184 return | 192 return |
185 | 193 |
186 | 194 |
187 def irc_connections(bot, command, args_array, bot_admin, bridge): | 195 def irc_connections(bot, command, args_array, bridge): |
188 parser = ArgumentParser(prog=command) | 196 parser = ArgumentParser(prog=command) |
189 parser.add_argument('--verbose', '-v', default=False, action='store_true') | 197 parser.add_argument('--verbose', '-v', default=False, action='store_true') |
190 try: | 198 try: |
191 args = parser.parse_args(args_array) | 199 args = parser.parse_args(args_array) |
192 except ArgumentParser.ParseException as e: | 200 except ArgumentParser.ParseException as e: |
199 else: | 207 else: |
200 ret = 'Number of IRC connections: '+str(n) | 208 ret = 'Number of IRC connections: '+str(n) |
201 return ret | 209 return ret |
202 | 210 |
203 | 211 |
204 def irc_participants(bot, command, args_array, bot_admin, bridge): | 212 def irc_participants(bot, command, args_array, bridge): |
205 ret = '' | 213 ret = '' |
206 if not bridge: | 214 if not bridge: |
207 for b in bot.bridges: | 215 for b in bot.bridges: |
208 irc_participants_nicknames = b.get_participants_nicknames_list(protocols=['irc']) | 216 irc_participants_nicknames = b.get_participants_nicknames_list(protocols=['irc']) |
209 ret += '\nparticipants on '+b.irc_room+' at '+b.irc_server+' ('+str(len(irc_participants_nicknames))+'): '+' '.join(irc_participants_nicknames) | 217 ret += '\nparticipants on '+b.irc_room+' at '+b.irc_server+' ('+str(len(irc_participants_nicknames))+'): '+' '.join(irc_participants_nicknames) |
211 else: | 219 else: |
212 irc_participants_nicknames = bridge.get_participants_nicknames_list(protocols=['irc']) | 220 irc_participants_nicknames = bridge.get_participants_nicknames_list(protocols=['irc']) |
213 return '\nparticipants on '+bridge.irc_room+' at '+bridge.irc_server+' ('+str(len(irc_participants_nicknames))+'): '+' '.join(irc_participants_nicknames) | 221 return '\nparticipants on '+bridge.irc_room+' at '+bridge.irc_server+' ('+str(len(irc_participants_nicknames))+'): '+' '.join(irc_participants_nicknames) |
214 | 222 |
215 | 223 |
216 def remove_bridge(bot, command, args_array, bot_admin, bridge): | 224 def remove_bridges(bot, command, args_array, bridge): |
217 bot.removeBridge(bridge) | 225 parser = ArgumentParser(prog=command) |
218 return 'Bridge removed.' | 226 parser.add_argument('bridge_id', nargs='+') |
219 | 227 try: |
220 | 228 args = parser.parse_args(args_array) |
221 def restart_bot(bot, command, args_array, bot_admin, bridge): | 229 except ArgumentParser.ParseException as e: |
230 return '\n'+e.args[1] | |
231 | |
232 found_bridges, ret = _find_bridges(bot, args.bridge_id) | |
233 | |
234 for found_bridge in found_bridges: | |
235 bot.removeBridge(found_bridge) | |
236 | |
237 return ret+'Bridges removed.' | |
238 | |
239 | |
240 def restart_bot(bot, command, args_array, bridge): | |
222 bot.restart() | 241 bot.restart() |
223 return | 242 return |
224 | 243 |
225 def restart_bridge(bot, command, args_array, bot_admin, bridge): | 244 def restart_bridges(bot, command, args_array, bridge): |
226 bridge.restart() | 245 parser = ArgumentParser(prog=command) |
227 return 'Bridge restarted.' | 246 parser.add_argument('bridge_id', nargs='+') |
228 | 247 try: |
229 | 248 args = parser.parse_args(args_array) |
230 def stop_bot(bot, command, args_array, bot_admin, bridge): | 249 except ArgumentParser.ParseException as e: |
250 return '\n'+e.args[1] | |
251 | |
252 found_bridges, ret = _find_bridges(bot, args.bridge_id) | |
253 for found_bridge in found_bridges: | |
254 found_bridge.restart() | |
255 | |
256 return ret+'Bridges restarted.' | |
257 | |
258 | |
259 def stop_bot(bot, command, args_array, bridge): | |
231 bot.stop() | 260 bot.stop() |
232 return 'Bot stopped.' | 261 return 'Bot stopped.' |
233 | 262 |
234 | 263 |
235 def stop_bridge(bot, command, args_array, bot_admin, bridge): | 264 def stop_bridges(bot, command, args_array, bridge): |
236 bridge.stop() | 265 parser = ArgumentParser(prog=command) |
237 return 'Bridge stopped.' | 266 parser.add_argument('bridge_id', nargs='+') |
238 | 267 try: |
239 | 268 args = parser.parse_args(args_array) |
240 def xmpp_connections(bot, command, args_array, bot_admin, bridge): | 269 except ArgumentParser.ParseException as e: |
270 return '\n'+e.args[1] | |
271 | |
272 found_bridges, ret = _find_bridges(bot, args.bridge_id) | |
273 for found_bridge in found_bridges: | |
274 found_bridge.stop() | |
275 | |
276 return ret+'Bridges stopped.' | |
277 | |
278 | |
279 def xmpp_connections(bot, command, args_array, bridge): | |
241 parser = ArgumentParser(prog=command) | 280 parser = ArgumentParser(prog=command) |
242 parser.add_argument('--verbose', '-v', default=False, action='store_true') | 281 parser.add_argument('--verbose', '-v', default=False, action='store_true') |
243 try: | 282 try: |
244 args = parser.parse_args(args_array) | 283 args = parser.parse_args(args_array) |
245 except ArgumentParser.ParseException as e: | 284 except ArgumentParser.ParseException as e: |
252 else: | 291 else: |
253 ret = 'Number of XMPP connections: '+str(n) | 292 ret = 'Number of XMPP connections: '+str(n) |
254 return ret | 293 return ret |
255 | 294 |
256 | 295 |
257 def xmpp_participants(bot, command, args_array, bot_admin, bridge): | 296 def xmpp_participants(bot, command, args_array, bridge): |
258 ret = '' | 297 ret = '' |
259 if not bridge: | 298 if not bridge: |
260 for b in bot.bridges: | 299 for b in bot.bridges: |
261 xmpp_participants_nicknames = b.get_participants_nicknames_list(protocols=['xmpp']) | 300 xmpp_participants_nicknames = b.get_participants_nicknames_list(protocols=['xmpp']) |
262 ret += '\nparticipants on '+b.xmpp_room_jid+' ('+str(len(xmpp_participants_nicknames))+'): '+' '.join(xmpp_participants_nicknames) | 301 ret += '\nparticipants on '+b.xmpp_room_jid+' ('+str(len(xmpp_participants_nicknames))+'): '+' '.join(xmpp_participants_nicknames) |