diff --git a/commands/sudo.py b/commands/sudo.py index 9d01e691..b96e1db8 100644 --- a/commands/sudo.py +++ b/commands/sudo.py @@ -1,10 +1,103 @@ -from command import CommandRegistry +import sqlite3 + +from command import CommandRegistry, split_args from commands import core +from little_shit import get_default_db_path, get_target __registry__ = cr = CommandRegistry() +_create_table_sql = """CREATE TABLE IF NOT EXISTS blocked_target_list ( + target TEXT NOT NULL +)""" + + +def _open_db_conn(): + conn = sqlite3.connect(get_default_db_path()) + conn.execute(_create_table_sql) + conn.commit() + return conn + @cr.register('test') @cr.restrict(full_command_only=True, superuser_only=True) def test(_, ctx_msg): core.echo('Your are the superuser!', ctx_msg) + + +@cr.register('block') +@cr.restrict(full_command_only=True, superuser_only=True) +@split_args(maxsplit=2) +def block(args, ctx_msg): + def _send_error_msg(): + core.echo('参数不正确。\n\n正确使用方法:\nsudo.block wx|qq ', ctx_msg) + + if len(args) != 2: + _send_error_msg() + return + + via, account = args + # Get a target using a fake context message + target = get_target({ + 'via': via, + 'type': 'friend_message', + 'sender_uid': account, + 'sender_account': account + }) + + if not target: + _send_error_msg() + return + + conn = _open_db_conn() + conn.execute('INSERT INTO blocked_target_list (target) VALUES (?)', (target,)) + conn.commit() + conn.close() + core.echo('成功屏蔽用户 ' + account, ctx_msg) + + +@cr.register('block_list', 'block-list') +@cr.restrict(full_command_only=True, superuser_only=True) +def block_list(_, ctx_msg, internal=False): + conn = _open_db_conn() + cursor = conn.execute('SELECT target FROM blocked_target_list') + blocked_targets = list(set([x[0] for x in list(cursor)])) # Get targets and remove duplications + conn.close() + if internal: + return blocked_targets + if blocked_targets: + # `t[1:]` to reply user account, without target prefix 'p'. + # This is a shit code, and should be changed later sometime. + core.echo('已屏蔽的用户:\n' + ', '.join([t[1:] for t in blocked_targets]), ctx_msg) + else: + core.echo('还没有屏蔽过用户') + + +@cr.register('unblock') +@cr.restrict(full_command_only=True, superuser_only=True) +@split_args(maxsplit=2) +def unblock(args, ctx_msg): + def _send_error_msg(): + core.echo('参数不正确。\n\n正确使用方法:\nsudo.unblock wx|qq ', ctx_msg) + + if len(args) != 2: + _send_error_msg() + return + + via, account = args + # Get a target using a fake context message + target = get_target({ + 'via': via, + 'type': 'friend_message', + 'sender_uid': account, + 'sender_account': account + }) + + if not target: + _send_error_msg() + return + + conn = _open_db_conn() + conn.execute('DELETE FROM blocked_target_list WHERE target = ?', (target,)) + conn.commit() + conn.close() + core.echo('成功取消屏蔽用户 ' + account, ctx_msg) diff --git a/filters/intercept_blocked_target_100.py b/filters/intercept_blocked_target_100.py new file mode 100644 index 00000000..387759f8 --- /dev/null +++ b/filters/intercept_blocked_target_100.py @@ -0,0 +1,18 @@ +""" +This filter intercepts messages from blocked targets (blocked using sudo.block command). +""" + +from commands import sudo +from filter import as_filter +from little_shit import get_target + + +@as_filter(priority=100) +def _filter(ctx_msg): + target = get_target(ctx_msg) + if not target: + return True + + if target in sudo.block_list('', ctx_msg, internal=True): + return False + return True