52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import logging
|
||
|
|
|
||
|
|
from yuxi.channel.extensions.wechat_ilink.types import ILinkAccount, ILinkDMStrategy, ILinkGroupStrategy
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
class ILinkSecurity:
|
||
|
|
def __init__(self, account: ILinkAccount, pairing=None):
|
||
|
|
self._account = account
|
||
|
|
self._pairing = pairing
|
||
|
|
|
||
|
|
def check_dm_access(self, sender_id: str) -> bool:
|
||
|
|
policy = self._account.dm_policy
|
||
|
|
|
||
|
|
if policy == ILinkDMStrategy.DISABLED:
|
||
|
|
return False
|
||
|
|
|
||
|
|
if policy == ILinkDMStrategy.OPEN:
|
||
|
|
if not self._account.allow_from or "*" in self._account.allow_from:
|
||
|
|
return True
|
||
|
|
return sender_id in self._account.allow_from
|
||
|
|
|
||
|
|
if policy == ILinkDMStrategy.ALLOWLIST:
|
||
|
|
return sender_id in self._account.allow_from
|
||
|
|
|
||
|
|
if policy == ILinkDMStrategy.PAIRING:
|
||
|
|
return self._check_pairing(sender_id)
|
||
|
|
|
||
|
|
return True
|
||
|
|
|
||
|
|
def check_group_access(self, group_id: str, sender_id: str) -> bool:
|
||
|
|
policy = self._account.group_policy
|
||
|
|
|
||
|
|
if policy == ILinkGroupStrategy.DISABLED:
|
||
|
|
return False
|
||
|
|
|
||
|
|
if policy == ILinkGroupStrategy.OPEN:
|
||
|
|
return True
|
||
|
|
|
||
|
|
if policy == ILinkGroupStrategy.ALLOWLIST:
|
||
|
|
return group_id in self._account.allow_from
|
||
|
|
|
||
|
|
return True
|
||
|
|
|
||
|
|
def _check_pairing(self, sender_id: str) -> bool:
|
||
|
|
if self._pairing is None:
|
||
|
|
return False
|
||
|
|
return self._pairing.is_paired(sender_id)
|