comparison commands.py @ 134:931a5edc7971

moved commands to a separate file Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Fri, 15 Jan 2010 23:11:25 +0100
parents
children 8c6184411ec7
comparison
equal deleted inserted replaced
133:e662ff6ecf50 134:931a5edc7971
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 import shlex
19
20 from argparse_modified import ArgumentParser
21 import muc
22 xmpp = muc.xmpp
23 del muc
24
25 from bridge import Bridge
26
27
28 commands = ['xmpp-participants', 'irc-participants', 'bridges']
29 admin_commands = ['add-bridge', 'add-xmpp-admin', 'change-bridge-mode', 'halt', 'remove-bridge', 'restart-bot', 'restart-bridge', 'stop-bridge']
30
31 def execute(bot, command_line, bot_admin, bridge):
32 ret = ''
33 command = shlex.split(command_line)
34 if len(command) > 1:
35 args_array = command[1:]
36 else:
37 args_array = []
38 command = command[0]
39 command_func = command.replace('-', '_')
40
41 if not globals().has_key(command_func):
42 ret = 'Error: "'+command+'" is not a valid command.\ncommands: '+' '.join(commands)
43 if bot_admin:
44 return ret+'\n'+'admin commands: '+' '.join(admin_commands)
45 else:
46 return ret
47 elif command in admin_commands and not bot_admin:
48 return 'You have to be a bot admin to use this command.'
49
50 b = bridge
51
52 if command in ['change-bridge-mode', 'remove-bridge', 'restart-bridge', 'stop-bridge']:
53 # we need to know which bridge the command is for
54 if len(args_array) == 0:
55 if bridge:
56 b = bridge
57 else:
58 return 'You must specify a bridge. '+bridges(bot, 'bridges', [], bot_admin, None)
59 else:
60 try:
61 bn = int(args_array[0])
62 if bn < 1:
63 raise IndexError
64 b = bot.bridges[bn-1]
65 except IndexError:
66 return 'Invalid bridge number "'+str(bn)+'". '+bridges(bot, 'bridges', [], bot_admin, None)
67 except ValueError:
68 bridges = bot.findBridges(args_array[0])
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)
82 parser.add_argument('xmpp_room_jid', type=str)
83 parser.add_argument('irc_chan', type=str)
84 parser.add_argument('irc_server', type=str)
85 parser.add_argument('--mode', choices=Bridge._modes, default='normal')
86 parser.add_argument('--say-level', choices=Bridge._say_levels, default='all')
87 parser.add_argument('--irc-port', type=int, default=6667)
88 try:
89 args = parser.parse_args(args_array)
90 except ArgumentParser.ParseException as e:
91 return '\n'+e.args[1]
92
93 bot.new_bridge(args.xmpp_room_jid, args.irc_chan, args.irc_server, args.mode, args.say_level, irc_port=args.irc_port)
94
95 return 'Bridge added.'
96
97
98 def add_xmpp_admin(bot, command, args_array, bot_admin, bridge):
99 parser = ArgumentParser(prog=command)
100 parser.add_argument('jid', type=str)
101 try:
102 args = parser.parse_args(args_array)
103 except ArgumentParser.ParseException as e:
104 return '\n'+e.args[1]
105 bot.admins_jid.append(args.jid)
106 for b in bot.bridges:
107 for p in b.participants:
108 if p.real_jid != None and xmpp.protocol.JID(args.jid).bareMatch(p.real_jid):
109 p.bot_admin = True
110
111 return 'XMPP admin added.'
112
113
114 def bridges(bot, command, args_array, bot_admin, bridge):
115 parser = ArgumentParser(prog=command)
116 parser.add_argument('--show-mode', default=False, action='store_true')
117 parser.add_argument('--show-say-level', default=False, action='store_true')
118 parser.add_argument('--show-participants', default=False, action='store_true')
119 try:
120 args = parser.parse_args(args_array)
121 except ArgumentParser.ParseException as e:
122 return '\n'+e.args[1]
123 ret = 'List of bridges:'
124 for i, b in enumerate(bot.bridges):
125 ret += '\n'+str(i+1)+' - '+str(b)
126 if args.show_mode:
127 ret += ' - mode='+b.mode
128 if args.show_say_level:
129 ret += ' - say_level='+Bridge._say_levels[b.say_level]
130 if args.show_participants:
131 xmpp_participants_nicknames = b.get_participants_nicknames_list(protocols=['xmpp'])
132 ret += '\nparticipants on XMPP ('+str(len(xmpp_participants_nicknames))+'): '+' '.join(xmpp_participants_nicknames)
133 irc_participants_nicknames = b.get_participants_nicknames_list(protocols=['irc'])
134 ret += '\nparticipants on IRC ('+str(len(irc_participants_nicknames))+'): '+' '.join(irc_participants_nicknames)
135 if b.irc_connection == None:
136 ret += ' - this bridge is stopped, use "restart-bridge '+str(i+1)+'" to restart it'
137 return ret
138
139
140 def change_bridge_mode(bot, command, args_array, bot_admin, bridge):
141 new_mode = args_array[1]
142 if not new_mode in Bridge._modes:
143 return '"'+new_mode+'" is not a valid mode, list of modes: '+' '.join(Bridge._modes)
144 r = bridge.changeMode(new_mode)
145 if r:
146 return r
147 return 'Mode changed.'
148
149
150 def halt(bot, command, args_array, bot_admin, bridge):
151 bot.stop()
152 return
153
154
155 def irc_participants(bot, command, args_array, bot_admin, bridge):
156 if not bridge:
157 for b in bot.bridges:
158 irc_participants_nicknames = b.get_participants_nicknames_list(protocols=['irc'])
159 ret += '\nparticipants on '+b.irc_room+' at '+b.irc_server+' ('+str(len(irc_participants_nicknames))+'): '+' '.join(irc_participants_nicknames)
160 return ret
161 else:
162 irc_participants_nicknames = bridge.get_participants_nicknames_list(protocols=['irc'])
163 return '\nparticipants on '+bridge.irc_room+' at '+bridge.irc_server+' ('+str(len(irc_participants_nicknames))+'): '+' '.join(irc_participants_nicknames)
164
165
166 def remove_bridge(bot, command, args_array, bot_admin, bridge):
167 bot.removeBridge(bridge)
168 return 'Bridge removed.'
169
170
171 def restart_bot(bot, command, args_array, bot_admin, bridge):
172 bot.restart()
173 return
174
175 def restart_bridge(bot, command, args_array, bot_admin, bridge):
176 bridge.restart()
177 return 'Bridge restarted.'
178
179
180 def stop_bridge(bot, command, args_array, bot_admin, bridge):
181 bridge.stop()
182 return 'Bridge stopped.'
183
184
185 def xmpp_participants(bot, command, args_array, bot_admin, bridge):
186 if not bridge:
187 for b in bot.bridges:
188 xmpp_participants_nicknames = b.get_participants_nicknames_list(protocols=['xmpp'])
189 ret += '\nparticipants on '+b.xmpp_room_jid+' ('+str(len(xmpp_participants_nicknames))+'): '+' '.join(xmpp_participants_nicknames)
190 return ret
191 else:
192 xmpp_participants_nicknames = bridge.get_participants_nicknames_list(protocols=['xmpp'])
193 return '\nparticipants on '+bridge.xmpp_room_jid+' ('+str(len(xmpp_participants_nicknames))+'): '+' '.join(xmpp_participants_nicknames)