2016-12-02 22:24:19 +08:00
|
|
|
import functools
|
|
|
|
import re
|
|
|
|
|
2017-02-15 15:52:18 +08:00
|
|
|
from little_shit import get_command_name_separators, get_command_args_separators
|
|
|
|
from msg_src_adapter import get_adapter_by_ctx
|
2016-12-02 22:24:19 +08:00
|
|
|
|
|
|
|
_command_name_seps = get_command_name_separators()
|
2017-01-11 15:20:00 +08:00
|
|
|
_command_args_seps = get_command_args_separators()
|
2016-12-02 22:24:19 +08:00
|
|
|
|
|
|
|
|
2016-12-03 17:16:16 +08:00
|
|
|
class CommandNotExistsError(Exception):
|
2016-12-02 22:24:19 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-12-03 17:16:16 +08:00
|
|
|
class CommandPermissionError(Exception):
|
2016-12-02 22:24:19 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-12-03 17:16:16 +08:00
|
|
|
class CommandScopeError(Exception):
|
2016-12-02 22:24:19 +08:00
|
|
|
def __init__(self, msg_type):
|
|
|
|
self.msg_type = msg_type
|
|
|
|
|
|
|
|
|
|
|
|
class CommandRegistry:
|
|
|
|
"""
|
|
|
|
Represent a map of commands and functions.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, init_func=None):
|
|
|
|
self.init_func = init_func
|
|
|
|
self.command_map = {}
|
|
|
|
self.alias_map = {}
|
|
|
|
self.hidden_command_names = []
|
|
|
|
|
|
|
|
def register(self, command_name, *other_names, hidden=False):
|
|
|
|
"""
|
|
|
|
Register command names and map them to a command function.
|
|
|
|
|
|
|
|
:param command_name: command name to register
|
|
|
|
:param other_names: other names of this command
|
|
|
|
:param hidden: hide the command name or not
|
|
|
|
NOTE: This is kind of like the 'full_command_only' in restrict(),
|
|
|
|
but only controls ONE command name,
|
|
|
|
while the later controls the whole command.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
if hidden:
|
|
|
|
self.hidden_command_names.append(command_name)
|
|
|
|
if not hasattr(func, 'restricted'):
|
|
|
|
# Apply a default restriction
|
|
|
|
func = self.restrict()(func)
|
|
|
|
self.command_map[command_name] = func
|
|
|
|
for name in other_names:
|
|
|
|
self.command_map[name] = func
|
|
|
|
|
|
|
|
@functools.wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
2016-12-03 21:12:07 +08:00
|
|
|
return func(*args, **kwargs)
|
2016-12-02 22:24:19 +08:00
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
# noinspection PyMethodMayBeStatic
|
|
|
|
def restrict(self, full_command_only=False, superuser_only=False,
|
|
|
|
group_owner_only=False, group_admin_only=False,
|
2016-12-29 23:45:34 +08:00
|
|
|
allow_private=True, allow_discuss=True, allow_group=True):
|
2016-12-02 22:24:19 +08:00
|
|
|
"""
|
|
|
|
Give a command some restriction.
|
|
|
|
This decorator must be put below all register() decorators.
|
|
|
|
Example:
|
|
|
|
@cr.register('wow', hidden=True)
|
|
|
|
@cr.register('another_command_name')
|
|
|
|
@cr.restrict(full_command_only=True)
|
|
|
|
def wow(_1, _2):
|
|
|
|
pass
|
|
|
|
|
|
|
|
:param full_command_only: whether to be called with full command (including registry name)
|
|
|
|
:param superuser_only: superuser only
|
|
|
|
:param group_owner_only: group owner only when processing group message
|
|
|
|
:param group_admin_only: group admin only when processing group message
|
|
|
|
:param allow_private: allow private message
|
2016-12-29 23:45:34 +08:00
|
|
|
:param allow_discuss: allow discuss message
|
2016-12-02 22:24:19 +08:00
|
|
|
:param allow_group: allow group message
|
|
|
|
"""
|
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
func.restricted = True
|
|
|
|
# Visibility
|
|
|
|
func.full_command_only = full_command_only
|
|
|
|
# Permission
|
|
|
|
func.superuser_only = superuser_only
|
|
|
|
func.group_owner_only = group_owner_only
|
|
|
|
func.group_admin_only = group_admin_only
|
|
|
|
# Scope
|
|
|
|
func.allow_private = allow_private
|
2016-12-29 23:45:34 +08:00
|
|
|
func.allow_discuss = allow_discuss
|
2016-12-02 22:24:19 +08:00
|
|
|
func.allow_group = allow_group
|
|
|
|
return func
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
def call(self, command_name, args_text, ctx_msg, **options):
|
|
|
|
"""
|
|
|
|
Call the command matching the specified command name.
|
|
|
|
|
|
|
|
:param command_name: command name
|
|
|
|
:param args_text: arguments as a string
|
|
|
|
:param ctx_msg: context message
|
|
|
|
:param options: other possible options
|
|
|
|
:return: things returned by the command function
|
|
|
|
:raises CommandScopeError: the message scope (group or private) is not allowed
|
|
|
|
:raises CommandPermissionError: the user is not permitted to call this command
|
|
|
|
"""
|
|
|
|
if command_name in self.command_map:
|
|
|
|
func = self.command_map[command_name]
|
|
|
|
if not self._check_scope(func, ctx_msg):
|
2017-02-15 15:52:18 +08:00
|
|
|
msg_type = ctx_msg.get('msg_type')
|
|
|
|
if msg_type == 'group':
|
2016-12-29 23:45:34 +08:00
|
|
|
msg_type_str = '群组消息'
|
2017-02-15 15:52:18 +08:00
|
|
|
elif msg_type == 'discuss':
|
2016-12-29 23:45:34 +08:00
|
|
|
msg_type_str = '讨论组消息'
|
2017-02-15 15:52:18 +08:00
|
|
|
elif msg_type == 'private':
|
2016-12-29 23:45:34 +08:00
|
|
|
msg_type_str = '私聊消息'
|
2017-02-15 15:52:18 +08:00
|
|
|
else:
|
|
|
|
msg_type_str = '未知来源消息'
|
2016-12-29 23:45:34 +08:00
|
|
|
raise CommandScopeError(msg_type_str)
|
2016-12-02 22:24:19 +08:00
|
|
|
if not self._check_permission(func, ctx_msg):
|
|
|
|
raise CommandPermissionError
|
|
|
|
return func(args_text, ctx_msg, **options)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _check_scope(func, ctx_msg):
|
|
|
|
"""
|
|
|
|
Check if current message scope (group or private) is allowed.
|
|
|
|
|
|
|
|
:param func: command function to check
|
|
|
|
:param ctx_msg: context message
|
|
|
|
:return: allowed or not
|
|
|
|
"""
|
|
|
|
allowed_msg_type = set()
|
|
|
|
if func.allow_group:
|
2017-02-15 15:52:18 +08:00
|
|
|
allowed_msg_type.add('group')
|
2016-12-29 23:45:34 +08:00
|
|
|
if func.allow_discuss:
|
2017-02-15 15:52:18 +08:00
|
|
|
allowed_msg_type.add('discuss')
|
2016-12-02 22:24:19 +08:00
|
|
|
if func.allow_private:
|
2017-02-15 15:52:18 +08:00
|
|
|
allowed_msg_type.add('private')
|
2016-12-02 22:24:19 +08:00
|
|
|
|
2017-02-15 15:52:18 +08:00
|
|
|
if ctx_msg.get('msg_type') in allowed_msg_type:
|
2016-12-02 22:24:19 +08:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2017-01-11 16:51:53 +08:00
|
|
|
# noinspection PyTypeChecker
|
2016-12-02 22:24:19 +08:00
|
|
|
@staticmethod
|
|
|
|
def _check_permission(func, ctx_msg):
|
|
|
|
"""
|
|
|
|
Check if current message sender is permitted to call this command.
|
|
|
|
|
|
|
|
:param func: command function to check
|
|
|
|
:param ctx_msg: context message
|
|
|
|
:return: permitted or not
|
|
|
|
"""
|
2017-02-15 15:52:18 +08:00
|
|
|
adapter = get_adapter_by_ctx(ctx_msg)
|
|
|
|
if adapter.is_sender_superuser(ctx_msg):
|
|
|
|
return True # Superuser is the BIG BOSS
|
|
|
|
|
|
|
|
if func.superuser_only:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if ctx_msg.get('msg_type') == 'group':
|
|
|
|
# TODO: 在酷 Q 测试一下
|
|
|
|
allowed_roles = {'owner', 'admin', 'member'}
|
|
|
|
if func.group_admin_only:
|
|
|
|
allowed_roles.intersection_update({'owner', 'admin'})
|
|
|
|
if func.group_owner_only:
|
|
|
|
allowed_roles.intersection_update({'owner'})
|
|
|
|
|
|
|
|
role = adapter.get_sender_group_role(ctx_msg)
|
|
|
|
if role not in allowed_roles:
|
2016-12-04 11:58:34 +08:00
|
|
|
return False
|
2016-12-02 22:24:19 +08:00
|
|
|
|
2016-12-29 23:45:34 +08:00
|
|
|
# Still alive, let go
|
2016-12-02 22:24:19 +08:00
|
|
|
return True
|
|
|
|
|
|
|
|
def has(self, command_name):
|
|
|
|
"""
|
|
|
|
Check if this registry has the specified command name,
|
|
|
|
except command names that is hidden and full command only.
|
|
|
|
|
|
|
|
:param command_name: command name
|
|
|
|
:return: has or not
|
|
|
|
"""
|
|
|
|
return command_name in self.command_map \
|
|
|
|
and command_name not in self.hidden_command_names \
|
|
|
|
and not self.command_map.get(command_name).full_command_only
|
|
|
|
|
|
|
|
def has_include_hidden(self, command_name):
|
|
|
|
"""
|
|
|
|
Check if this registry has the specified command name,
|
|
|
|
including command names that is hidden and full command only.
|
|
|
|
|
|
|
|
:param command_name: command name
|
|
|
|
:return: has or not
|
|
|
|
"""
|
|
|
|
return command_name in self.command_map
|
|
|
|
|
|
|
|
|
|
|
|
class CommandHub:
|
|
|
|
"""
|
|
|
|
Represent series of command registries,
|
|
|
|
which means it's used as a collection of different registries
|
|
|
|
and allows same command names.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.registry_map = {}
|
|
|
|
|
|
|
|
def add_registry(self, registry_name, registry):
|
|
|
|
"""
|
|
|
|
Add a registry to the hub, running the init function of the registry.
|
|
|
|
|
|
|
|
:param registry_name: registry name
|
|
|
|
:param registry: registry object
|
|
|
|
"""
|
|
|
|
if registry.init_func:
|
|
|
|
registry.init_func()
|
|
|
|
self.registry_map[registry_name] = registry
|
|
|
|
|
|
|
|
def call(self, command_name, args_text, ctx_msg, **options):
|
|
|
|
"""
|
|
|
|
Call the commands matching the specified command name.
|
|
|
|
|
|
|
|
:param command_name: command name
|
|
|
|
:param args_text: arguments as a string
|
|
|
|
:param ctx_msg: context message
|
|
|
|
:param options: other possible options
|
|
|
|
:return: things returned by the command function
|
|
|
|
(list of things if more than one matching command)
|
|
|
|
:raises CommandNotExistsError: no command exists
|
|
|
|
:raises CommandScopeError: the message scope is disallowed by all commands
|
|
|
|
:raises CommandPermissionError: the user is baned by all commands
|
|
|
|
"""
|
|
|
|
if not command_name:
|
|
|
|
# If the command name is empty, we just return
|
|
|
|
return None
|
|
|
|
|
|
|
|
command = re.split('|'.join(_command_name_seps), command_name, 1)
|
|
|
|
if len(command) == 2 and command[0] in self.registry_map:
|
|
|
|
registry = self.registry_map.get(command[0])
|
|
|
|
if registry.has_include_hidden(command[1]):
|
|
|
|
return registry.call(command[1], args_text, ctx_msg, **options)
|
|
|
|
else:
|
|
|
|
raise CommandNotExistsError
|
|
|
|
else:
|
|
|
|
results = []
|
|
|
|
cmd_exists = False
|
|
|
|
permitted = False
|
|
|
|
for registry in self.registry_map.values():
|
|
|
|
# Trying to call all commands with the name
|
|
|
|
if registry.has(command_name):
|
|
|
|
cmd_exists = True
|
|
|
|
try:
|
|
|
|
results.append(
|
|
|
|
registry.call(command_name, args_text, ctx_msg, **options))
|
|
|
|
permitted = True # If it's permitted, this will be set
|
|
|
|
except CommandPermissionError:
|
|
|
|
pass
|
|
|
|
if not cmd_exists:
|
|
|
|
raise CommandNotExistsError
|
|
|
|
if not permitted:
|
|
|
|
# No command was permitted
|
|
|
|
raise CommandPermissionError
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
hub = CommandHub()
|
2017-01-11 15:20:00 +08:00
|
|
|
|
|
|
|
|
2017-01-13 21:03:44 +08:00
|
|
|
class CommandArgumentError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def split_arguments(maxsplit=0):
|
|
|
|
"""
|
|
|
|
To use this decorator, you should add a parameter exactly named 'argv' to the function of the command,
|
|
|
|
which will be set to the split argument list when called.
|
|
|
|
|
|
|
|
However, the first parameter, typically 'args_text', will remain to be the whole argument string, like before.
|
|
|
|
|
|
|
|
:param maxsplit: max split time
|
|
|
|
"""
|
|
|
|
|
2017-01-11 15:20:00 +08:00
|
|
|
def decorator(func):
|
2017-01-13 21:03:44 +08:00
|
|
|
@functools.wraps(func)
|
2017-01-12 21:59:54 +08:00
|
|
|
def wrapper(argument, *args, **kwargs):
|
2017-01-13 21:03:44 +08:00
|
|
|
if argument is None:
|
|
|
|
raise CommandArgumentError
|
|
|
|
if kwargs.get('argv') is not None:
|
|
|
|
argv = kwargs['argv']
|
|
|
|
del kwargs['argv']
|
|
|
|
elif isinstance(argument, (list, tuple)):
|
|
|
|
argv = list(argument)
|
2017-01-12 21:59:54 +08:00
|
|
|
else:
|
2017-01-13 21:03:44 +08:00
|
|
|
argv = list(filter(lambda arg: arg, re.split('|'.join(_command_args_seps), argument, maxsplit)))
|
|
|
|
return func(argument, argv=argv, *args, **kwargs)
|
2017-01-11 15:20:00 +08:00
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
return decorator
|