From 02be9328a99cf32876d609738dd1d2f4db5a2dd5 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Mon, 25 Oct 2021 21:08:20 +0800 Subject: [PATCH] basic support blackList --- blackList.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ database.py | 36 ++++++++++++++++++----- readset.py | 1 + rssbot.py | 22 ++++++++++++++- 4 files changed, 131 insertions(+), 8 deletions(-) create mode 100644 blackList.py diff --git a/blackList.py b/blackList.py new file mode 100644 index 0000000..4565f3c --- /dev/null +++ b/blackList.py @@ -0,0 +1,80 @@ +# (C) 2021 lifegpc +# This file is part of rssbot. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +from re import search +from typing import List + + +class BlackInfo: + uid: int = None + op_uid: int = None + from_config: bool = False + add_time: int = None + reason: str = None + + def __init__(self, uid: int, op_uid: int = None, add_time: int = None, reason: str = None): + self.uid = uid + self.op_uid = op_uid + self.from_config = True if self.op_uid is None else False + self.add_time = add_time + self.reason = reason + + +class ConfigBlackList: + def __init__(self, m, s: str = None): + from rssbot import main + self._main: main = m + self.__list = [] + if s is not None: + l = s.split(',') + for i in l: + if search(r'^[\+-]?[0-9]+$', i) is not None: + self.__list.append(int(i)) + + def getBlackList(self) -> List[BlackInfo]: + r = [] + for i in self.__list: + r.append(BlackInfo(i)) + return r + + def isInBlackList(self, chatId: int) -> bool: + return chatId in self.__list + + +class BlackList: + def __init__(self, m): + from rssbot import main + self._main: main = m + self._configBlackList = ConfigBlackList(m, self._main._setting._blackList) + + def getBlackList(self) -> List[BlackInfo]: + m = self._configBlackList.getBlackList() + t = [] + r = [] + for i in m: + if i.uid not in t: + t.append(i.uid) + r.append(i) + return r + + def isInBlackList(self, chatId: int) -> bool: + if self._configBlackList.isInBlackList(chatId): + return True + return False + + def checkRSSList(self): + li = self.getBlackList() + for i in li: + self._main._db.removeChatInChatList(i.uid) diff --git a/database.py b/database.py index 5c370a2..5a042b8 100644 --- a/database.py +++ b/database.py @@ -53,6 +53,19 @@ hash TEXT, time INT, PRIMARY KEY (hash) )''' +USERSTATUS_TABLE = '''CREATE TABLE userStatus ( +userId INT, +status INT, +hashd TEXT, +PRIMARY KEY (userId) +)''' +USERBLACKLIST_TABLE = '''CREATE TABLE userBlackList ( +userId INT, +op_uid INT, +add_time INT, +reason TEXT, +PRIMARY KEY (userId) +)''' @unique @@ -118,6 +131,9 @@ class database: 'INSERT INTO hashList VALUES (?, ?, ?);', tuple(l)) self._db.execute('DROP TABLE hashList_old;') self._db.commit() + if v < [1, 0, 0, 6]: + self._db.execute(USERBLACKLIST_TABLE) + self._db.commit() self._db.execute('VACUUM;') self.__updateExistsTable() self.__write_version() @@ -132,18 +148,15 @@ class database: if 'chatList' not in self._exist_tables: self._db.execute(CHATLIST_TABLE) if 'userStatus' not in self._exist_tables: - self._db.execute('''CREATE TABLE userStatus ( -userId INT, -status INT, -hashd TEXT, -PRIMARY KEY (userId) -)''') + self._db.execute(USERSTATUS_TABLE) if 'hashList' not in self._exist_tables: self._db.execute(HASHLIST_TABLE) + if 'userBlackList' not in self._exist_tables: + self._db.execute(USERBLACKLIST_TABLE) self._db.commit() def __init__(self, m, loc: str): - self._version = [1, 0, 0, 5] + self._version = [1, 0, 0, 6] self._value_lock = Lock() self._db = sqlite3.connect(loc, check_same_thread=False) self._db.execute('VACUUM;') @@ -305,6 +318,15 @@ PRIMARY KEY (userId) pass return userStatus.normalStatus, '' + def removeChatInChatList(self, chatId: int): + with self._value_lock: + try: + self._db.execute("DELETE FROM chatList WHERE chatId=?;", (chatId,)) + self._db.commit() + return True + except: + return False + def removeItemInChatList(self, chatId: int, id: int): with self._value_lock: try: diff --git a/readset.py b/readset.py index e10b5b9..b75cf29 100644 --- a/readset.py +++ b/readset.py @@ -58,6 +58,7 @@ class settings: self._miraiApiHTTPAuthKey = d['miraiApiHTTPAuthKey'] if 'miraiApiHTTPAuthKey' in d and d['miraiApiHTTPAuthKey'] != '' else None self._miraiApiQQ = int(d['miraiApiQQ']) if 'miraiApiQQ' in d and d['miraiApiQQ'].isnumeric() else None self._miraiApiHTTPVer = d['miraiApiHTTPVer'] if 'miraiApiHTTPVer' in d and d['miraiApiHTTPVer'] != '' else None + self._blackList = d['blackList'] if 'blackList' in d and d['blackList'] != '' else None @property def token(self) -> str: diff --git a/rssbot.py b/rssbot.py index 429888d..3d712ac 100644 --- a/rssbot.py +++ b/rssbot.py @@ -39,6 +39,7 @@ from rssbotlib import loadRSSBotLib, AddVideoInfoResult from time import sleep from miraiDatabase import MiraiDatabase from mirai import Mirai +from blackList import BlackList MAX_ITEM_IN_MEDIA_GROUP = 10 @@ -559,7 +560,6 @@ class main: if self._upi is not None: d['offset'] = self._upi ud = self._request('getUpdates', 'post', json=d) - print(ud) if ud is not None and 'ok' in ud and ud['ok']: for i in ud['result']: for key in ['message', 'edited_message', 'channel_post', 'edited_channel_post']: @@ -606,6 +606,8 @@ class main: print('无法读取机器人信息') self._me = self._me['result'] self._rssbotLib = loadRSSBotLib(self._setting.rssbotLib, self) + self._blackList = BlackList(self) + self._blackList.checkRSSList() self._upi = None self._updateThread = updateThread(self) self._updateThread.start() @@ -700,6 +702,17 @@ class messageHandle(Thread): if self._chatId is None: print('未知的chat id') return -1 + if self._main._blackList.isInBlackList(self._chatId): + c = self.__getChatType() + if c == 'private': + c = '您' + elif c in ['supergroup', 'group']: + c = '该群组' + elif c == 'channel': + c = '该频道' + di = {'chat_id': self._chatId, 'text': f'{c}已被封禁。'} + self._main._request("sendMessage", 'post', json=di) + return self._botCommand = None if 'text' in self._data: if 'entities' in self._data: @@ -709,6 +722,10 @@ class messageHandle(Thread): di = {'chat_id': self._chatId} if self.__getChatType() in ['supergroup', 'group'] and self._fromUserId is not None: di['reply_to_message_id'] = self._messageId + if self._main._blackList.isInBlackList(self._fromUserId): + di['text'] = '您已被封禁。' + self._main._request("sendMessage", 'post', json=di) + return self._userStatus, self._hashd = self._main._db.getUserStatus( self._fromUserId) if self._userStatus == userStatus.normalStatus: @@ -948,6 +965,9 @@ class callbackQueryHandle(Thread): def run(self): self._callbackQueryId = self._data['id'] self._fromUserId = self._data['from']['id'] + if self._main._blackList.isInBlackList(self._fromUserId): + self.answer('您已被封禁。') + return l = self._data['data'].split(',') if len(l) < 3: self.answer('错误的按钮数据。')