python ircd using asyncio
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
781 B

  1. from asyncio import Queue
  2. from dataclasses import dataclass, field
  3. from typing import Dict
  4. from paircd.client import Client
  5. @dataclass
  6. class Channel:
  7. name: str
  8. clients_by_nick: Dict[str, Client] = field(default_factory=dict)
  9. msg_queue: Queue = field(default_factory=Queue)
  10. def add_client(self, client: Client) -> None:
  11. self.clients_by_nick[client.nickname] = client
  12. async def process(self) -> None:
  13. while True:
  14. msg = await self.msg_queue.get()
  15. for client in self.clients_by_nick.values():
  16. # Don't broadcast client's messages back to themselves
  17. if msg.startswith(f":{client.id()} PRIVMSG".encode("utf-8")):
  18. continue
  19. client.msg_queue.put_nowait(msg)