Skip to content

Commit

Permalink
refactor(protocol): move magic values to module constants/class attri…
Browse files Browse the repository at this point in the history
…butes
  • Loading branch information
linuxdaemon committed Apr 17, 2024
1 parent 9aab105 commit 4298733
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions asyncirc/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Callable,
Coroutine,
Dict,
Final,
List,
Optional,
Sequence,
Expand Down Expand Up @@ -46,6 +47,9 @@ class SASLMechanism(IntEnum):
EXTERNAL = auto()


_CAP_LIST_MIN_PARAMS: Final = 3 # CAP <subcmd> :a b c


async def _internal_ping(conn: "IrcProtocol", message: "Message") -> None:
conn.send(f"PONG {message.parameters}")

Expand Down Expand Up @@ -130,7 +134,7 @@ async def _internal_cap_handler(
raise ValueError(msg)

caplist: List[Cap] = []
if len(message.parameters) > 2:
if len(message.parameters) > (_CAP_LIST_MIN_PARAMS - 1):
caplist = CapList.parse(message.parameters[-1])

if message.parameters[1] == "LS":
Expand Down Expand Up @@ -248,6 +252,8 @@ def __init__(
sasl_mech: Optional[SASLMechanism] = None,
logger: Optional["Logger"] = None,
loop: Optional["AbstractEventLoop"] = None,
*,
max_lag: float = 60,
) -> None:
"""Create protocol for IRC connection.
Expand Down Expand Up @@ -275,6 +281,7 @@ def __init__(
self.sasl_mech = SASLMechanism(sasl_mech or SASLMechanism.NONE)
self.logger = logger
self.loop = loop or asyncio.get_event_loop()
self.max_lag = max_lag

if self.sasl_mech == SASLMechanism.PLAIN and self.sasl_auth is None:
msg = "You must specify sasl_auth when using SASL PLAIN"
Expand Down Expand Up @@ -333,7 +340,7 @@ async def pinger(self) -> None:
msg = "Server not set in ping handler"
raise ValueError(msg)

if self.server.lag > 60:
if self.server.lag > self.max_lag:
self.loop.create_task(self.connect())
else:
self.send(f"PING :LAG{time.time()}")
Expand Down

0 comments on commit 4298733

Please sign in to comment.