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.

27 lines
877 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. def remove_client_by_nick(self, nick: str) -> None:
  13. del self.clients_by_nick[nick]
  14. async def process(self) -> None:
  15. while True:
  16. msg = await self.msg_queue.get()
  17. for client in self.clients_by_nick.values():
  18. # Don't broadcast client's messages back to themselves
  19. if msg.startswith(f":{client.id()} PRIVMSG".encode("utf-8")):
  20. continue
  21. client.msg_queue.put_nowait(msg)