|
1 | | -# ../auth/commands.py |
| 1 | +# TODO Add commands to add/remove permissions once command filters are added |
2 | 2 |
|
3 | | -"""Registers the "sp auth" sub-commands.""" |
| 3 | +from players.entity import PlayerEntity |
4 | 4 |
|
5 | | -# ============================================================================= |
6 | | -# >> IMPORTS |
7 | | -# ============================================================================= |
8 | | -# Python Imports |
9 | | -# Collections |
10 | | -from collections import OrderedDict |
| 5 | +from commands.client import ClientCommand |
| 6 | +from commands.server import ServerCommand |
| 7 | +from .manager import auth_manager |
11 | 8 |
|
12 | | -# Source.Python imports |
13 | | -# Auth |
14 | | -from auth import auth_logger |
15 | | -from auth import _auth_strings |
16 | | -from auth.manager import auth_manager |
| 9 | +from messages import SayText2 |
17 | 10 |
|
| 11 | +import re |
18 | 12 |
|
19 | | -# ============================================================================= |
20 | | -# >> GLOBAL VARIABLES |
21 | | -# ============================================================================= |
22 | | -# Get the sp.auth.commands logger |
23 | | -auth_commands_logger = auth_logger.commands |
24 | 13 |
|
| 14 | +argument_re = re.compile("<([^>]+)>") |
25 | 15 |
|
26 | | -# ============================================================================= |
27 | | -# >> CLASSES |
28 | | -# ============================================================================= |
29 | | -class _AuthCommands(OrderedDict): |
30 | | - """Class used for executing "sp auth" sub-command functionality.""" |
31 | 16 |
|
32 | | - manager = auth_manager |
| 17 | +class AuthCommand(object): |
| 18 | + def __init__(self, command, permission=None): |
| 19 | + self.permission = permission |
| 20 | + self.command = command |
| 21 | + self.arguments = [] |
| 22 | + for arg in command.split(" "): |
| 23 | + if argument_re.match(arg): |
| 24 | + self.arguments.append(None) |
| 25 | + else: |
| 26 | + self.arguments.append(arg) |
| 27 | + self.callback = None |
33 | 28 |
|
34 | | - def call_command(self, args): |
35 | | - """Execute the given "sp auth" sub-command.""" |
36 | | - # Was a command given? |
37 | | - if not args: |
| 29 | + def __call__(self, callback, *args, **kwargs): |
| 30 | + self.callback = callback |
| 31 | + return callback |
38 | 32 |
|
39 | | - # Get a message that a sub-command is needed |
40 | | - message = '[SP Auth] ' + _auth_strings[ |
41 | | - 'Missing Command'].get_string() + '\n' |
42 | | - |
43 | | - # Print the auth help text |
44 | | - self.print_auth_help(message) |
45 | | - |
46 | | - # No need to go further |
| 33 | + def test(self, index, command): |
| 34 | + if command.get_arg_count() - 1 != len(self.arguments): |
47 | 35 | return |
48 | | - |
49 | | - # Get the command |
50 | | - command = args[0] |
51 | | - |
52 | | - # Is the command registered? |
53 | | - if command not in self: |
54 | | - |
55 | | - # Get a message about the invalid command |
56 | | - message = '[SP Auth] ' + _auth_strings[ |
57 | | - 'Invalid Sub-Command'].get_string(command=command) + '\n' |
58 | | - |
59 | | - # Print the auth help text |
60 | | - self.print_auth_help(message) |
61 | | - |
62 | | - # No need to go further |
63 | | - return |
64 | | - |
65 | | - # Does the given command use arguments? |
66 | | - if hasattr(self[command], 'args'): |
67 | | - |
68 | | - # Execute the command with the given arguments |
69 | | - self[command](args[1:]) |
70 | | - |
71 | | - # Go no further |
72 | | - return |
73 | | - |
74 | | - # Execute the command |
75 | | - self[command]() |
76 | | - |
77 | | - def _load_auth_providers(self, providers): |
78 | | - """Load the given auth providers.""" |
79 | | - # Were any providers given? |
80 | | - if not providers: |
81 | | - |
82 | | - # Send a message about the required argument |
83 | | - auth_commands_logger.log_message( |
84 | | - '[SP Auth] ' + _auth_strings['Missing Load'].get_string()) |
85 | | - |
86 | | - # No need to go further |
87 | | - return |
88 | | - |
89 | | - # Loop through all of the given providers |
90 | | - for provider in providers: |
91 | | - |
92 | | - # Load the current provider |
93 | | - self.manager.load_auth(provider) |
94 | | - |
95 | | - _load_auth_providers.args = ['<provider>', '[provider]', '...'] |
96 | | - |
97 | | - def _unload_auth_providers(self, providers): |
98 | | - """Unload the given auth providers.""" |
99 | | - # Were any providers given? |
100 | | - if not providers: |
101 | | - |
102 | | - # Send a message about the required argument |
103 | | - auth_commands_logger.log_message( |
104 | | - '[SP Auth] ' + _auth_strings['Missing Unload'].get_string()) |
105 | | - |
106 | | - # No need to go further |
107 | | - return |
108 | | - |
109 | | - # Loop through all of the given providers |
110 | | - for provider in providers: |
111 | | - |
112 | | - # Unload the current provider |
113 | | - self.manager.unload_auth(provider) |
114 | | - |
115 | | - _unload_auth_providers.args = ['<provider>', '[provider]', '...'] |
116 | | - |
117 | | - def _reload_auth_providers(self, providers=None): |
118 | | - """Reload the given auth providers.""" |
119 | | - # Were any providers given? |
120 | | - if not providers: |
121 | | - |
122 | | - # Set providers to all currently loaded providers |
123 | | - providers = list(self.manager) |
124 | | - |
125 | | - # Loop through the providers |
126 | | - for provider in providers: |
127 | | - |
128 | | - # Reload the given provider |
129 | | - self.manager.reload_auth(provider) |
130 | | - |
131 | | - _reload_auth_providers.args = ['[provider]', '[provider]', '...'] |
132 | | - |
133 | | - def _print_auth_providers(self): |
134 | | - """List all currently loaded auth providers.""" |
135 | | - # Get header messages |
136 | | - message = '[SP Auth] ' + _auth_strings[ |
137 | | - 'Providers'].get_string() + '\n' + '=' * 61 + '\n' |
138 | | - |
139 | | - # Loop through all loaded auth providers |
140 | | - for provider in self.manager: |
141 | | - |
142 | | - # Add the current provider to the message |
143 | | - message += provider + '\n' |
144 | | - |
145 | | - # Print ending messages |
146 | | - auth_commands_logger.log_message(message + '=' * 61) |
147 | | - |
148 | | - def print_auth_help(self, message=''): |
149 | | - """Print all "sp auth" sub-commands.""" |
150 | | - # Get header messages |
151 | | - header = message + '[SP Auth] ' + _auth_strings[ |
152 | | - 'Help'].get_string() + 'sp auth <command> [arguments]\n' + '=' * 78 |
153 | | - |
154 | | - # Print all "sp auth" sub-commands |
155 | | - self.print_help(header, '=' * 78) |
156 | | - |
157 | | - def print_help(self, pretext='', posttext=''): |
158 | | - """Print all "sp auth" sub-commands.""" |
159 | | - auth_commands_logger.log_message( |
160 | | - pretext + '\n' + self.get_help_text() + '\n' + posttext) |
161 | | - |
162 | | - def get_help_text(self): |
163 | | - """Return the help text for auth commands.""" |
164 | | - # Store the base message |
165 | | - message = '' |
166 | | - |
167 | | - # Loop through all registered sub-commands |
168 | | - for item in self: |
169 | | - |
170 | | - # Add the base text |
171 | | - text = 'auth {0}'.format(item) |
172 | | - |
173 | | - # Does the current item use arguments? |
174 | | - if hasattr(self[item], 'args'): |
175 | | - |
176 | | - # Add the arguments to the text |
177 | | - text += ' ' + ' '.join(self[item].args) |
178 | | - |
179 | | - # Add the doc strings |
180 | | - message += text + self[item].__doc__.rjust(78 - len(text)) + '\n' |
181 | | - |
182 | | - # Return the message |
183 | | - return message.rstrip('\n') |
184 | | - |
185 | | - |
186 | | -# ============================================================================= |
187 | | -# >> FUNCTIONS |
188 | | -# ============================================================================= |
189 | | -# Get the _AuthCommands instance |
190 | | -_auth_commands = _AuthCommands() |
191 | | - |
192 | | -# Add all auth loading/unloading commands to the dictionary |
193 | | -_auth_commands['load'] = _auth_commands._load_auth_providers |
194 | | -_auth_commands['unload'] = _auth_commands._unload_auth_providers |
195 | | -_auth_commands['reload'] = _auth_commands._reload_auth_providers |
196 | | - |
197 | | -# Add all printing commands to the dictionary |
198 | | -_auth_commands['list'] = _auth_commands._print_auth_providers |
199 | | -_auth_commands['help'] = _auth_commands.print_auth_help |
| 36 | + if self.permission is not None: |
| 37 | + if index is not None and not PlayerEntity(index).has_permission(self.permission): |
| 38 | + return |
| 39 | + args = [] |
| 40 | + for i in range(0, len(self.arguments)): |
| 41 | + if self.arguments[i] is not None and self.arguments[i] != command[i + 1]: |
| 42 | + return |
| 43 | + elif self.arguments[i] is None: |
| 44 | + args.append(command[i + 1]) |
| 45 | + if self.callback is not None: |
| 46 | + self.callback(index, *args) |
| 47 | + |
| 48 | + |
| 49 | +class AuthCommandManager(list): |
| 50 | + def register_command(self, command, permission=None): |
| 51 | + authcommand = AuthCommand(command, permission) |
| 52 | + self.append(authcommand) |
| 53 | + return authcommand |
| 54 | + |
| 55 | + |
| 56 | +auth_commands = AuthCommandManager() |
| 57 | + |
| 58 | + |
| 59 | +@ClientCommand("auth") |
| 60 | +def auth_client_command(command, index): |
| 61 | + for authcommand in auth_commands: |
| 62 | + authcommand.test(index, command) |
| 63 | + |
| 64 | + |
| 65 | +@ServerCommand("auth") |
| 66 | +def auth_server_command(command): |
| 67 | + for auth_command in auth_commands: |
| 68 | + auth_command.test(None, command) |
| 69 | + |
| 70 | + |
| 71 | +@auth_commands.register_command("load <backend>", "sp.auth.commands.load") |
| 72 | +def load_backend_command(index, backend): |
| 73 | + if auth_manager.load_backend(backend): |
| 74 | + message = "[Auth] Loaded backend {} succesfully.".format(backend) |
| 75 | + else: |
| 76 | + message = "[Auth] Failed to load backend {}.".format(backend) |
| 77 | + if index is None: |
| 78 | + print(message) |
| 79 | + else: |
| 80 | + SayText2(message=message).send(index) |
0 commit comments