diff --git a/ircrobots/interface.py b/ircrobots/interface.py new file mode 100644 index 0000000..121f6e1 --- /dev/null +++ b/ircrobots/interface.py @@ -0,0 +1,52 @@ +from typing import Optional +from enum import IntEnum +from dataclasses import dataclass + +from ircstates import Server +from irctokens import Line + +from .ircv3 import Capability + +@dataclass +class ConnectionParams(object): + nickname: str + host: str + port: int + ssl: bool + + username: Optional[str] = None + realname: Optional[str] = None + bindhost: Optional[str] = None + +class SendPriority(IntEnum): + HIGH = 0 + MEDIUM = 10 + LOW = 20 + DEFAULT = MEDIUM + +class PriorityLine(object): + def __init__(self, priority: int, line: Line): + self.priority = priority + self.line = line + def __lt__(self, other: "PriorityLine") -> bool: + return self.priority < other.priority + +class IServer(Server): + params: ConnectionParams + + async def send_raw(self, line: str, priority=SendPriority.DEFAULT): + pass + async def send(self, line: Line, priority=SendPriority.DEFAULT): + pass + + def set_throttle(self, rate: int, time: float): + pass + + async def connect(self, params: ConnectionParams): + pass + + async def queue_capability(self, cap: Capability): + pass + + async def line_written(self, line: Line): + pass diff --git a/ircrobots/server.py b/ircrobots/server.py index 0540fc0..6281c44 100644 --- a/ircrobots/server.py +++ b/ircrobots/server.py @@ -6,41 +6,18 @@ from enum import IntEnum from dataclasses import dataclass from asyncio_throttle import Throttler -from ircstates import Server as BaseServer from ircstates import Emit from irctokens import build, Line, tokenise -from .ircv3 import Capability, CAPS +from .ircv3 import Capability, CAPS +from .interface import ConnectionParams, IServer, PriorityLine, SendPriority + sc = ssl.create_default_context(ssl.Purpose.SERVER_AUTH) THROTTLE_RATE = 4 # lines THROTTLE_TIME = 2 # seconds -@dataclass -class ConnectionParams(object): - nickname: str - host: str - port: int - ssl: bool - - username: Optional[str] = None - realname: Optional[str] = None - bindhost: Optional[str] = None - -class SendPriority(IntEnum): - HIGH = 0 - MEDIUM = 10 - LOW = 20 - DEFAULT = MEDIUM - -class PriorityLine(object): - def __init__(self, priority: int, line: Line): - self.priority = priority - self.line = line - def __lt__(self, other: "PriorityLine") -> bool: - return self.priority < other.priority - -class Server(BaseServer): +class Server(IServer): _reader: asyncio.StreamReader _writer: asyncio.StreamWriter params: ConnectionParams @@ -93,9 +70,8 @@ class Server(BaseServer): if matches: self._requested_caps = matches await self.send(build("CAP", ["REQ", " ".join(matches)])) - async def _cap_ack(self, line: Line): - caps = line.params[2].split(" ") - for cap in caps: + async def _cap_ack(self, emit: Emit): + for cap in (emit.tokens or []): if cap in self._requested_caps: self._requested_caps.remove(cap) if not self._requested_caps: @@ -106,7 +82,7 @@ class Server(BaseServer): if emit.subcommand == "LS" and emit.finished: await self._cap_ls_done() elif emit.subcommand in ["ACK", "NAK"]: - await self._cap_ack(line) + await self._cap_ack(emit) async def _on_read_line(self, line: Line): pass diff --git a/requirements.txt b/requirements.txt index 18f582f..6810584 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ -ircstates ==0.8.0 +ircstates ==0.8.1 asyncio-throttle ==1.0.1 +dataclasses ==0.6