Files
LangBot/pkg/pipeline/bansess/bansess.py

52 lines
1.8 KiB
Python
Raw Normal View History

from __future__ import annotations
from .. import stage, entities
import langbot_plugin.api.entities.builtin.pipeline.query as pipeline_query
@stage.stage_class('BanSessionCheckStage')
class BanSessionCheckStage(stage.PipelineStage):
2025-07-10 11:09:33 +08:00
"""Access control processing stage
2025-07-10 11:09:33 +08:00
Only check if the group or personal number in the query is in the access control list.
"""
async def initialize(self, pipeline_config: dict):
2024-02-06 23:57:21 +08:00
pass
async def process(self, query: pipeline_query.Query, stage_inst_name: str) -> entities.StageProcessResult:
2024-02-06 21:26:03 +08:00
found = False
mode = query.pipeline_config['trigger']['access-control']['mode']
2024-02-06 21:26:03 +08:00
sess_list = query.pipeline_config['trigger']['access-control'][mode]
2024-02-06 21:26:03 +08:00
if (query.launcher_type.value == 'group' and 'group_*' in sess_list) or (
query.launcher_type.value == 'person' and 'person_*' in sess_list
):
2024-02-06 21:26:03 +08:00
found = True
else:
for sess in sess_list:
if sess == f'{query.launcher_type.value}_{query.launcher_id}':
2024-02-06 21:26:03 +08:00
found = True
break
# 使用 *_id 来表示加白/拉黑某用户的私聊和群聊场景
if sess.startswith('*_') and (sess[2:] == query.launcher_id or sess[2:] == query.sender_id):
found = True
break
ctn = False
2024-02-06 21:26:03 +08:00
if mode == 'whitelist':
ctn = found
else:
ctn = not found
return entities.StageProcessResult(
2025-05-10 18:04:58 +08:00
result_type=entities.ResultType.CONTINUE if ctn else entities.ResultType.INTERRUPT,
new_query=query,
2025-07-10 11:09:33 +08:00
console_notice=f'Ignore message according to access control: {query.launcher_type.value}_{query.launcher_id}'
if not ctn
else '',
)