From 53e14fa2548d7ce79be70c02c02542c885af1066 Mon Sep 17 00:00:00 2001 From: David Sangrey Date: Mon, 30 May 2022 19:30:16 -0400 Subject: [PATCH] [#166] First Pass Implementation A basic lifting of the HalpyBOT List Handler (from @Rik079) to Pydle --- pydle/features/rfc1459/client.py | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/pydle/features/rfc1459/client.py b/pydle/features/rfc1459/client.py index 48581d1..362dedb 100644 --- a/pydle/features/rfc1459/client.py +++ b/pydle/features/rfc1459/client.py @@ -7,12 +7,34 @@ from pydle.client import BasicClient, NotInChannel, AlreadyInChannel from . import parsing, protocol +from typing import List, Optional +import asyncio class RFC1459Support(BasicClient): """ Basic RFC1459 client. """ DEFAULT_QUIT_MESSAGE = 'Quitting' + def __init__( + self, + nickname: str = "HalpyLISTener", + fallback: Optional[List] = None, + username=None, + realname=None, + eventloop=None, + **kwargs, + ): + super().__init__( + nickname, + fallback if fallback is not None else [], + username, + realname, + eventloop, + **kwargs, + ) + self._pending_query = asyncio.Queue() + self._channellist = set() + ## Internals. def _reset_attributes(self): @@ -869,6 +891,31 @@ async def on_raw_319(self, message): if nickname in self._pending['whois']: self._whois_info[nickname].update(info) + async def all_channels(self) -> List[str]: + await self.rawmsg("LIST") + # Create future + future = asyncio.get_event_loop().create_future() + await self._pending_query.put(future) + try: + channels = await future + return list(channels) + except asyncio.CancelledError: + # We don't care if it's cancelled as this should only happen on shutdown + pass + + async def on_raw_321(self, *args): + # Results incoming, empty channel list + self._channellist.clear() + + async def on_raw_322(self, message): + # Called by Pydle when we get a new channel, add to list + self._channellist.add(message.params[1]) + + async def on_raw_323(self, *args): + # Set results + future = await self._pending_query.get() + future.set_result(self._channellist) + async def on_raw_324(self, message): """ Channel mode. """ target, channel = message.params[:2]