diff --git a/ircrobots/matching.py b/ircrobots/matching.py index 47d6acb..6e69c55 100644 --- a/ircrobots/matching.py +++ b/ircrobots/matching.py @@ -1,43 +1,55 @@ from typing import List, Optional from irctokens import Line -from .numerics import NUMERIC_NAMES +from ircstates import NUMERIC_NAMES from .interface import IServer, IMatchResponse, IMatchResponseParam -class Response(IMatchResponse): +class Responses(IMatchResponse): def __init__(self, - command: str, - params: List[IMatchResponseParam]): - self._command = command - self._params = params + commands: List[str], + params: List[IMatchResponseParam]=[]): + self._commands = commands + self._params = params def __repr__(self) -> str: - return f"Response({self._command}: {self._params!r})" + return f"Responses({self._commands!r}: {self._params!r})" def match(self, server: IServer, line: Line) -> bool: - if line.command == self._command: - for i, param in enumerate(self._params): - if (i >= len(line.params) or - not param.match(server, line.params[i])): - return False - else: - return True + for command in self._commands: + if line.command == command: + for i, param in enumerate(self._params): + if (i >= len(line.params) or + not param.match(server, line.params[i])): + continue + else: + return True else: return False +class Response(Responses): + def __init__(self, + command: str, + params: List[IMatchResponseParam]=[]): + super().__init__([command], params) + + def __repr__(self) -> str: + return f"Response({self._commands[0]}: {self._params!r})" + + class Numeric(Response): def __init__(self, name: str, params: List[IMatchResponseParam]=[]): super().__init__(NUMERIC_NAMES.get(name, name), params) -class Numerics(IMatchResponse): +class Numerics(Responses): def __init__(self, - numerics: List[str]): - self._numerics = [NUMERIC_NAMES.get(n, n) for n in numerics] - def __repr__(self) -> str: - return f"Numerics({self._numerics!r})" + numerics: List[str], + params: List[IMatchResponseParam]=[]): + self._numerics = numerics + numerics = [NUMERIC_NAMES.get(n, n) for n in numerics] + super().__init__(numerics, params) - def match(self, server: IServer, line: Line): - return line.command in self._numerics + def __repr__(self) -> str: + return f"Numerics({self._numerics!r}: {self._params!r})" class ResponseOr(IMatchResponse): def __init__(self, *responses: IMatchResponse): diff --git a/ircrobots/numerics.py b/ircrobots/numerics.py deleted file mode 100644 index 29ea54d..0000000 --- a/ircrobots/numerics.py +++ /dev/null @@ -1,16 +0,0 @@ -NUMERIC_NUMBERS = {} -NUMERIC_NAMES = {} - -def _numeric(number: str, name: str): - NUMERIC_NUMBERS[number] = name - NUMERIC_NAMES[name] = number - -_numeric("001", "RPL_WELCOME") -_numeric("005", "RPL_ISUPPORT") - -_numeric("903", "RPL_SASLSUCCESS") -_numeric("904", "ERR_SASLFAIL") -_numeric("905", "ERR_SASLTOOLONG") -_numeric("906", "ERR_SASLABORTED") -_numeric("907", "ERR_SASLALREADY") -_numeric("908", "RPL_SASLMECHS") diff --git a/ircrobots/server.py b/ircrobots/server.py index 68eb39d..01e8c4c 100644 --- a/ircrobots/server.py +++ b/ircrobots/server.py @@ -3,13 +3,14 @@ from typing import Awaitable, Deque, Dict, List, Optional, Set, Tuple from collections import deque from asyncio_throttle import Throttler -from ircstates import Emit, Channel +from ircstates import Emit, Channel, NUMERIC_NAMES from irctokens import build, Line, tokenise from .ircv3 import CAPContext, CAP_SASL from .sasl import SASLContext, SASLResult -from .matching import Numeric, ParamAny, ParamFolded +from .matching import ResponseOr, Numerics, Numeric, ParamAny, ParamFolded from .asyncs import MaybeAwait +from .struct import Whois from .interface import (ConnectionParams, ICapability, IServer, SentLine, SendPriority, SASLParams, IMatchResponse) diff --git a/requirements.txt b/requirements.txt index 85bf5ae..4961e9c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ anyio ==1.3.0 asyncio-throttle ==1.0.1 dataclasses ==0.6 -ircstates ==0.8.3 +ircstates ==0.8.7