Move channel tracking data/functions to src/IRCChannel.Channels

This commit is contained in:
jesopo 2018-11-11 12:34:22 +00:00
parent 57aa62cc9c
commit 2816fe67bb
5 changed files with 76 additions and 50 deletions

View file

@ -22,8 +22,8 @@ class Module(ModuleManager.BaseModule):
@utils.hook("timer.unban") @utils.hook("timer.unban")
def _timer_unban(self, event): def _timer_unban(self, event):
server = self.bot.get_server(event["server_id"]) server = self.bot.get_server(event["server_id"])
if server.has_channel(event["channel_name"]): if event["channel_name"] in server.channels:
channel = server.get_channel(event["channel_name"]) channel = server.channels.get(event["channel_name"])
channel.send_unban(event["hostmask"]) channel.send_unban(event["hostmask"])
def _kick(self, server, channel, nickname, reason): def _kick(self, server, channel, nickname, reason):

View file

@ -54,7 +54,7 @@ class Module(ModuleManager.BaseModule):
for server_id, channel_name, _ in hooks: for server_id, channel_name, _ in hooks:
for output in outputs: for output in outputs:
server = self.bot.get_server(server_id) server = self.bot.get_server(server_id)
channel = server.get_channel(channel_name) channel = server.channels.get(channel_name)
trigger = self._make_trigger(channel, server, output) trigger = self._make_trigger(channel, server, output)
self.bot.trigger(trigger) self.bot.trigger(trigger)

View file

@ -118,7 +118,7 @@ class Module(ModuleManager.BaseModule):
# on-join channel topic line # on-join channel topic line
@utils.hook("raw.332") @utils.hook("raw.332")
def handle_332(self, event): def handle_332(self, event):
channel = event["server"].get_channel(event["args"][1]) channel = event["server"].channels.get(event["args"][1])
topic = event["args"].get(2) topic = event["args"].get(2)
channel.set_topic(topic) channel.set_topic(topic)
self.events.on("received.numeric.332").call(channel=channel, self.events.on("received.numeric.332").call(channel=channel,
@ -128,7 +128,7 @@ class Module(ModuleManager.BaseModule):
@utils.hook("raw.topic") @utils.hook("raw.topic")
def topic(self, event): def topic(self, event):
user = event["server"].get_user(event["prefix"].nickname) user = event["server"].get_user(event["prefix"].nickname)
channel = event["server"].get_channel(event["args"][0]) channel = event["server"].channels.get(event["args"][0])
topic = event["args"].get(1) topic = event["args"].get(1)
channel.set_topic(topic) channel.set_topic(topic)
self.events.on("received.topic").call(channel=channel, self.events.on("received.topic").call(channel=channel,
@ -137,7 +137,7 @@ class Module(ModuleManager.BaseModule):
# on-join channel topic set by/at # on-join channel topic set by/at
@utils.hook("raw.333") @utils.hook("raw.333")
def handle_333(self, event): def handle_333(self, event):
channel = event["server"].get_channel(event["args"][1]) channel = event["server"].channels.getl(event["args"][1])
topic_setter_hostmask = event["args"][2] topic_setter_hostmask = event["args"][2]
topic_setter = utils.irc.seperate_hostmask(topic_setter_hostmask) topic_setter = utils.irc.seperate_hostmask(topic_setter_hostmask)
@ -154,7 +154,7 @@ class Module(ModuleManager.BaseModule):
# /names response, also on-join user list # /names response, also on-join user list
@utils.hook("raw.353", default_event=True) @utils.hook("raw.353", default_event=True)
def handle_353(self, event): def handle_353(self, event):
channel = event["server"].get_channel(event["args"][2]) channel = event["server"].channels.get(event["args"][2])
nicknames = event["args"].get(3).split() nicknames = event["args"].get(3).split()
for nickname in nicknames: for nickname in nicknames:
modes = set([]) modes = set([])
@ -187,13 +187,12 @@ class Module(ModuleManager.BaseModule):
def join(self, event): def join(self, event):
account = None account = None
realname = None realname = None
channel = event["server"].channels.get(event["args"][0])
if len(event["args"]) == 2: if len(event["args"]) == 2:
channel = event["server"].get_channel(event["args"][0])
if not event["args"][1] == "*": if not event["args"][1] == "*":
account = event["args"][1] account = event["args"][1]
realname = event["args"][2] realname = event["args"][2]
else:
channel = event["server"].get_channel(event["args"][0])
if not event["server"].is_own_nickname(event["prefix"].nickname): if not event["server"].is_own_nickname(event["prefix"].nickname):
user = event["server"].get_user(event["prefix"].nickname) user = event["server"].get_user(event["prefix"].nickname)
@ -223,7 +222,7 @@ class Module(ModuleManager.BaseModule):
# on user parting channel # on user parting channel
@utils.hook("raw.part") @utils.hook("raw.part")
def part(self, event): def part(self, event):
channel = event["server"].get_channel(event["args"][0]) channel = event["server"].channels.get(event["args"][0])
reason = event["args"].get(1) reason = event["args"].get(1)
if not event["server"].is_own_nickname(event["prefix"].nickname): if not event["server"].is_own_nickname(event["prefix"].nickname):
@ -237,7 +236,7 @@ class Module(ModuleManager.BaseModule):
else: else:
self.events.on("self.part").call(channel=channel, self.events.on("self.part").call(channel=channel,
reason=reason, server=event["server"]) reason=reason, server=event["server"])
event["server"].remove_channel(channel) event["server"].channels.remove(channel)
# unknown command sent by us, oops! # unknown command sent by us, oops!
@utils.hook("raw.421", default_event=True) @utils.hook("raw.421", default_event=True)
@ -336,7 +335,7 @@ class Module(ModuleManager.BaseModule):
target = event["args"][0] target = event["args"][0]
is_channel = target[0] in event["server"].channel_types is_channel = target[0] in event["server"].channel_types
if is_channel: if is_channel:
channel = event["server"].get_channel(target) channel = event["server"].channels.get(target)
remove = False remove = False
args = event["args"][2:] args = event["args"][2:]
_args = args[:] _args = args[:]
@ -395,7 +394,7 @@ class Module(ModuleManager.BaseModule):
"action": action} "action": action}
if target[0] in event["server"].channel_types: if target[0] in event["server"].channel_types:
channel = event["server"].get_channel(event["args"][0]) channel = event["server"].channels.get(event["args"][0])
self.events.on("received.message.channel").call( self.events.on("received.message.channel").call(
user=user, channel=channel, **kwargs) user=user, channel=channel, **kwargs)
channel.buffer.add_message(user.nickname, message, action, channel.buffer.add_message(user.nickname, message, action,
@ -425,7 +424,7 @@ class Module(ModuleManager.BaseModule):
user = event["server"].get_user(event["prefix"].nickname) user = event["server"].get_user(event["prefix"].nickname)
if target[0] in event["server"].channel_types: if target[0] in event["server"].channel_types:
channel = event["server"].get_channel(target) channel = event["server"].channels.get(target)
self.events.on("received.notice.channel").call( self.events.on("received.notice.channel").call(
message=message, message_split=message_split, user=user, message=message, message_split=message_split, user=user,
server=event["server"], channel=channel, server=event["server"], channel=channel,
@ -442,7 +441,7 @@ class Module(ModuleManager.BaseModule):
target = event["args"][0] target = event["args"][0]
if target[0] in event["server"].channel_types: if target[0] in event["server"].channel_types:
channel = event["server"].get_channel(target) channel = event["server"].channels.get(target)
self.events.on("received.tagmsg.channel").call(channel=channel, self.events.on("received.tagmsg.channel").call(channel=channel,
user=user, tags=event["tags"], server=event["server"]) user=user, tags=event["tags"], server=event["server"])
elif event["server"].is_own_nickname(target): elif event["server"].is_own_nickname(target):
@ -530,7 +529,7 @@ class Module(ModuleManager.BaseModule):
# response to an empty mode command # response to an empty mode command
@utils.hook("raw.324", default_event=True) @utils.hook("raw.324", default_event=True)
def handle_324(self, event): def handle_324(self, event):
channel = event["server"].get_channel(event["args"][1]) channel = event["server"].channels.get(event["args"][1])
modes = event["args"][2] modes = event["args"][2]
if modes[0] == "+" and modes[1:]: if modes[0] == "+" and modes[1:]:
for mode in modes[1:]: for mode in modes[1:]:
@ -540,7 +539,7 @@ class Module(ModuleManager.BaseModule):
# channel creation unix timestamp # channel creation unix timestamp
@utils.hook("raw.329", default_event=True) @utils.hook("raw.329", default_event=True)
def handle_329(self, event): def handle_329(self, event):
channel = event["server"].get_channel(event["args"][1]) channel = event["server"].channels.get(event["args"][1])
channel.creation_timestamp = int(event["args"][2]) channel.creation_timestamp = int(event["args"][2])
# nickname already in use # nickname already in use
@ -563,7 +562,7 @@ class Module(ModuleManager.BaseModule):
def kick(self, event): def kick(self, event):
user = event["server"].get_user(event["prefix"].nickname) user = event["server"].get_user(event["prefix"].nickname)
target = event["args"][1] target = event["args"][1]
channel = event["server"].get_channel(event["args"][0]) channel = event["server"].channels.get(event["args"][0])
reason = event["args"].get(2) reason = event["args"].get(2)
if not event["server"].is_own_nickname(target): if not event["server"].is_own_nickname(target):
@ -580,9 +579,9 @@ class Module(ModuleManager.BaseModule):
def rename(self, event): def rename(self, event):
old_name = event["args"][0] old_name = event["args"][0]
new_name = event["args"][1] new_name = event["args"][1]
channel = event["server"].get_channel(old_name) channel = event["server"].channels.get(old_name)
event["server"].rename_channel(old_name, new_name) event["server"].channels.rename(old_name, new_name)
self.events.on("received.rename").call(channel=channel, self.events.on("received.rename").call(channel=channel,
old_name=old_name, new_name=new_name, old_name=old_name, new_name=new_name,
reason=event["args"].get(2), server=event["server"]) reason=event["args"].get(2), server=event["server"])

View file

@ -154,3 +154,54 @@ class Channel(IRCObject.Object):
def get_user_status(self, user: IRCUser.User) -> typing.Set: def get_user_status(self, user: IRCUser.User) -> typing.Set:
return self.user_modes.get(user, []) return self.user_modes.get(user, [])
class Channels(object):
def __init__(self, server: "IRCServer.Server", bot: "IRCBot.Bot",
events: EventManager.EventHook):
self._server = server
self._bot = bot
self._events = events
self._channels = {} # type: typing.Dict[str, Channel]
def __iter__(self) -> Iterable[Channel]:
return (channel for channel in self._channels.values())
def __contains__(self, name: str) -> bool
return self.contains(name)
def _get_id(self, channel_name: str) -> int:
self.bot.database.channels.add(self.id, channel_name)
return self.bot.database.channels.get_id(self.id, channel_name)
def _name_lower(self, channel_name: str) -> str:
return utils.irc.lower(self._server.case_mapping, channel_name)
def contains(self, name: str) -> bool:
lower = self._name_lower(name)
return name[0] in self._server.channel_types and lower in self._channels
def add(self, name: str) -> Channel:
id = self.get_channel_id(name)
lower = self._name_lower(name)
new_channel = Channel(lower, id, self._server, self._bot)
self._channels[lower] = new_channel
self._events.on("new.channel").call(channel=new_channel, server=self)
return new_channel
def remove(self, channel: Channel):
lower = self._name_lower(channel.name)
del self._channels[lower]
def get(self, name: str):
if not self.contains(name):
return self._add(name)
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)

View file

@ -38,7 +38,7 @@ class Server(IRCObject.Object):
self.users = {} # type: typing.Dict[str, IRCUser.User] self.users = {} # type: typing.Dict[str, IRCUser.User]
self.new_users = set([]) #type: typing.Set[IRCUser.User] self.new_users = set([]) #type: typing.Set[IRCUser.User]
self.channels = {} # type: typing.Dict[str, IRCChannel.Channel] self.channels = IRCChannel.Channels(self, self.bot, self.events)
self.own_modes = {} # type: typing.Dict[str, typing.Optional[str]] self.own_modes = {} # type: typing.Dict[str, typing.Optional[str]]
self.isupport = {} # type: typing.Dict[str, typing.Optional[str]] self.isupport = {} # type: typing.Dict[str, typing.Optional[str]]
@ -194,30 +194,6 @@ class Server(IRCObject.Object):
user = self.users.pop(utils.irc.lower(self.case_mapping, old_nickname)) user = self.users.pop(utils.irc.lower(self.case_mapping, old_nickname))
user._id = self.get_user_id(new_nickname) user._id = self.get_user_id(new_nickname)
self.users[utils.irc.lower(self.case_mapping, new_nickname)] = user self.users[utils.irc.lower(self.case_mapping, new_nickname)] = user
def has_channel(self, channel_name: str):
return channel_name[0] in self.channel_types and utils.irc.lower(
self.case_mapping, channel_name) in self.channels
def get_channel(self, channel_name: str):
if not self.has_channel(channel_name):
channel_id = self.get_channel_id(channel_name)
new_channel = IRCChannel.Channel(channel_name, channel_id,
self, self.bot)
self.events.on("new.channel").call(channel=new_channel,
server=self)
self.channels[new_channel.name] = new_channel
return self.channels[utils.irc.lower(self.case_mapping, channel_name)]
def get_channel_id(self, channel_name: str):
self.bot.database.channels.add(self.id, channel_name)
return self.bot.database.channels.get_id(self.id, channel_name)
def remove_channel(self, channel: IRCChannel.Channel):
for user in channel.users:
user.part_channel(channel)
del self.channels[channel.name]
def rename_channel(self, old_name, new_name):
channel = self.channels.pop(old_name.lower())
channel.name = new_name.lower()
self.channels[channel.name] = channel
self.bot.database.channels.rename(channel.id, new_name)
def parse_data(self, line: str): def parse_data(self, line: str):
if not line: if not line:
@ -411,8 +387,8 @@ class Server(IRCObject.Object):
message = full_message.split("\01ACTION ", 1)[1][:-1] message = full_message.split("\01ACTION ", 1)[1][:-1]
full_message_split = full_message.split() full_message_split = full_message.split()
if self.has_channel(target): if target in self.channels:
channel = self.get_channel(target) channel = self.channels.get(target)
channel.buffer.add_message(None, message, action, tags, True) channel.buffer.add_message(None, message, action, tags, True)
self.events.on("self.message.channel").call( self.events.on("self.message.channel").call(
message=full_message, message_split=full_message_split, message=full_message, message_split=full_message_split,
@ -429,8 +405,8 @@ class Server(IRCObject.Object):
full_message = message if not prefix else prefix+message full_message = message if not prefix else prefix+message
self.send("%sNOTICE %s :%s" % (self._tag_str(tags), target, self.send("%sNOTICE %s :%s" % (self._tag_str(tags), target,
full_message)) full_message))
if self.has_channel(target): if target in self.channels:
self.get_channel(target).buffer.add_notice(None, message, tags, self.channels.get(target).buffer.add_notice(None, message, tags,
True) True)
else: else:
self.get_user(target).buffer.add_notice(None, message, tags, self.get_user(target).buffer.add_notice(None, message, tags,