2018-11-11 16:11:45 +00:00
|
|
|
import typing
|
|
|
|
from src import EventManager, IRCBot, IRCChannel, IRCServer, utils
|
2018-11-11 14:53:16 +00:00
|
|
|
|
|
|
|
class Channels(object):
|
|
|
|
def __init__(self, server: "IRCServer.Server", bot: "IRCBot.Bot",
|
|
|
|
events: EventManager.EventHook):
|
|
|
|
self._server = server
|
|
|
|
self._bot = bot
|
|
|
|
self._events = events
|
2018-11-11 16:11:45 +00:00
|
|
|
self._channels = {} # type: typing.Dict[str, IRCChannel.Channel]
|
2018-11-11 14:53:16 +00:00
|
|
|
|
2018-11-11 16:11:45 +00:00
|
|
|
def __iter__(self) -> typing.Iterable[IRCChannel.Channel]:
|
2018-11-11 14:53:16 +00:00
|
|
|
return (channel for channel in self._channels.values())
|
|
|
|
def __contains__(self, name: str) -> bool:
|
|
|
|
return self.contains(name)
|
|
|
|
def __len__(self) -> int:
|
|
|
|
return len(self._channels)
|
|
|
|
def __getitem__(self, name: str):
|
|
|
|
return self.get(name)
|
|
|
|
|
2018-11-13 11:36:44 +00:00
|
|
|
def keys(self):
|
|
|
|
return self._channels.keys()
|
|
|
|
def values(self):
|
|
|
|
return self._channels.values()
|
|
|
|
def items(self):
|
|
|
|
return self._channels.items()
|
|
|
|
|
2019-04-07 12:59:53 +00:00
|
|
|
def get_id(self, channel_name: str) -> int:
|
2018-11-11 14:53:16 +00:00
|
|
|
self._bot.database.channels.add(self._server.id, channel_name)
|
|
|
|
return self._bot.database.channels.get_id(self._server.id, channel_name)
|
|
|
|
|
|
|
|
def _name_lower(self, channel_name: str) -> str:
|
2019-01-24 17:12:50 +00:00
|
|
|
return self._server.irc_lower(channel_name)
|
2018-11-11 14:53:16 +00:00
|
|
|
|
|
|
|
def contains(self, name: str) -> bool:
|
|
|
|
lower = self._name_lower(name)
|
|
|
|
return name[0] in self._server.channel_types and lower in self._channels
|
|
|
|
|
2018-11-11 16:11:45 +00:00
|
|
|
def add(self, name: str) -> IRCChannel.Channel:
|
2019-04-07 12:59:53 +00:00
|
|
|
id = self.get_id(name)
|
2018-11-11 14:53:16 +00:00
|
|
|
lower = self._name_lower(name)
|
2018-11-11 16:11:45 +00:00
|
|
|
new_channel = IRCChannel.Channel(lower, id, self._server, self._bot)
|
2018-11-11 14:53:16 +00:00
|
|
|
self._channels[lower] = new_channel
|
|
|
|
self._events.on("new.channel").call(channel=new_channel, server=self)
|
|
|
|
return new_channel
|
|
|
|
|
2018-11-11 16:11:45 +00:00
|
|
|
def remove(self, channel: IRCChannel.Channel):
|
2018-11-11 14:53:16 +00:00
|
|
|
lower = self._name_lower(channel.name)
|
|
|
|
del self._channels[lower]
|
|
|
|
|
|
|
|
def get(self, name: str):
|
|
|
|
return self._channels[self._name_lower(name)]
|
|
|
|
|
|
|
|
def rename(self, old_name, new_name):
|
|
|
|
old_lower = self._name_lower(old_name)
|
|
|
|
new_lower = self._name_lower(new_name)
|
|
|
|
|
|
|
|
channel = self.channels.pop(old_lower)
|
|
|
|
channel.name = new_name
|
|
|
|
self._channels[new_name] = channel
|
|
|
|
|
|
|
|
self._bot.database.channels.rename(channel.id, new_lower)
|