2018-10-30 14:58:48 +00:00
|
|
|
import typing, uuid
|
2018-11-11 12:42:20 +00:00
|
|
|
from src import EventManager, IRCBot, IRCBuffer, IRCObject, IRCServer, IRCUser
|
|
|
|
from src import utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-10-01 12:48:55 +00:00
|
|
|
class Channel(IRCObject.Object):
|
2018-12-02 09:56:57 +00:00
|
|
|
name = ""
|
2018-10-30 14:58:48 +00:00
|
|
|
def __init__(self, name: str, id, server: "IRCServer.Server",
|
|
|
|
bot: "IRCBot.Bot"):
|
2019-01-24 17:12:50 +00:00
|
|
|
self.name = server.irc_lower(name)
|
2018-08-18 23:19:53 +00:00
|
|
|
self.id = id
|
2016-03-29 11:56:58 +00:00
|
|
|
self.server = server
|
|
|
|
self.bot = bot
|
|
|
|
self.topic = ""
|
2018-10-30 17:49:35 +00:00
|
|
|
self.topic_setter_nickname = None # type: typing.Optional[str]
|
|
|
|
self.topic_setter_username = None # type: typing.Optional[str]
|
|
|
|
self.topic_setter_hostname = None # type: typing.Optional[str]
|
2016-03-29 11:56:58 +00:00
|
|
|
self.topic_time = 0
|
2018-11-20 14:08:36 +00:00
|
|
|
self.users = set([]) # type: typing.Set[IRCUser.User]
|
|
|
|
self.modes = {} # type: typing.Dict[str, typing.Set]
|
|
|
|
self.user_modes = {} # type: typing.Dict[IRCUser.User, typing.Set]
|
2016-03-29 11:56:58 +00:00
|
|
|
self.created_timestamp = None
|
2018-09-11 09:15:16 +00:00
|
|
|
self.buffer = IRCBuffer.Buffer(bot, server)
|
2018-08-28 11:23:57 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def __repr__(self) -> str:
|
2018-08-28 11:23:57 +00:00
|
|
|
return "IRCChannel.Channel(%s|%s)" % (self.server.name, self.name)
|
2018-10-30 14:58:48 +00:00
|
|
|
def __str__(self) -> str:
|
2018-10-01 12:48:55 +00:00
|
|
|
return self.name
|
2018-07-15 12:39:15 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def set_topic(self, topic: str):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.topic = topic
|
2018-10-30 14:58:48 +00:00
|
|
|
def set_topic_setter(self, nickname: str, username: str=None,
|
|
|
|
hostname: str=None):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.topic_setter_nickname = nickname
|
|
|
|
self.topic_setter_username = username
|
|
|
|
self.topic_setter_hostname = hostname
|
2018-10-30 14:58:48 +00:00
|
|
|
def set_topic_time(self, unix_timestamp: int):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.topic_time = unix_timestamp
|
2018-07-15 12:39:15 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def add_user(self, user: IRCUser.User):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.users.add(user)
|
2018-10-30 14:58:48 +00:00
|
|
|
def remove_user(self, user: IRCUser.User):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.users.remove(user)
|
2018-07-02 15:34:29 +00:00
|
|
|
for mode in list(self.modes.keys()):
|
2018-10-03 15:44:00 +00:00
|
|
|
if mode in self.server.prefix_modes and user in self.modes[mode]:
|
2018-07-02 15:15:26 +00:00
|
|
|
self.modes[mode].discard(user)
|
|
|
|
if not len(self.modes[mode]):
|
|
|
|
del self.modes[mode]
|
2018-10-03 15:44:44 +00:00
|
|
|
if user in self.user_modes:
|
2018-10-04 03:41:28 +00:00
|
|
|
del self.user_modes[user]
|
2018-10-30 14:58:48 +00:00
|
|
|
def has_user(self, user: IRCUser.User) -> bool:
|
2016-04-18 15:50:19 +00:00
|
|
|
return user in self.users
|
2018-07-15 12:39:15 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def add_mode(self, mode: str, arg: str=None):
|
2016-03-29 11:56:58 +00:00
|
|
|
if not mode in self.modes:
|
|
|
|
self.modes[mode] = set([])
|
2018-08-30 10:12:48 +00:00
|
|
|
if arg:
|
2018-10-03 15:44:00 +00:00
|
|
|
if mode in self.server.prefix_modes:
|
2018-08-30 10:12:48 +00:00
|
|
|
user = self.server.get_user(arg)
|
2018-07-02 15:15:26 +00:00
|
|
|
if user:
|
|
|
|
self.modes[mode].add(user)
|
2018-10-03 15:44:44 +00:00
|
|
|
if not user in self.user_modes:
|
|
|
|
self.user_modes[user] = set([])
|
|
|
|
self.user_modes[user].add(mode)
|
2018-07-02 15:15:26 +00:00
|
|
|
else:
|
2018-08-30 10:12:48 +00:00
|
|
|
self.modes[mode].add(arg.lower())
|
2018-10-30 14:58:48 +00:00
|
|
|
def remove_mode(self, mode: str, arg: str=None):
|
2019-01-16 11:59:36 +00:00
|
|
|
if not arg:
|
|
|
|
if mode in self.modes:
|
|
|
|
del self.modes[mode]
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
2018-10-03 15:44:00 +00:00
|
|
|
if mode in self.server.prefix_modes:
|
2018-08-30 10:12:48 +00:00
|
|
|
user = self.server.get_user(arg)
|
2018-07-02 15:15:26 +00:00
|
|
|
if user:
|
2018-10-18 20:07:08 +00:00
|
|
|
if mode in self.modes:
|
|
|
|
self.modes[mode].discard(user)
|
|
|
|
if user in self.user_modes:
|
|
|
|
self.user_modes[user].discard(mode)
|
|
|
|
if not self.user_modes[user]:
|
|
|
|
del self.user_modes[user]
|
2018-07-02 15:15:26 +00:00
|
|
|
else:
|
2018-08-30 10:12:48 +00:00
|
|
|
self.modes[mode].discard(arg.lower())
|
2018-11-26 13:39:26 +00:00
|
|
|
if mode in self.modes and not len(self.modes[mode]):
|
2016-03-29 11:56:58 +00:00
|
|
|
del self.modes[mode]
|
2018-10-30 14:58:48 +00:00
|
|
|
def change_mode(self, remove: bool, mode: str, arg: str=None):
|
2018-08-30 10:12:48 +00:00
|
|
|
if remove:
|
|
|
|
self.remove_mode(mode, arg)
|
|
|
|
else:
|
|
|
|
self.add_mode(mode, arg)
|
2018-07-15 12:39:15 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def set_setting(self, setting: str, value: typing.Any):
|
2018-08-18 23:19:53 +00:00
|
|
|
self.bot.database.channel_settings.set(self.id, setting, value)
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_setting(self, setting: str, default: typing.Any=None
|
|
|
|
) -> typing.Any:
|
2018-08-18 23:19:53 +00:00
|
|
|
return self.bot.database.channel_settings.get(self.id, setting,
|
|
|
|
default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_settings(self, pattern: str, default: typing.Any=[]
|
|
|
|
) -> typing.List[typing.Any]:
|
2018-08-18 23:19:53 +00:00
|
|
|
return self.bot.database.channel_settings.find(self.id, pattern,
|
|
|
|
default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_settings_prefix(self, prefix: str, default: typing.Any=[]
|
|
|
|
) -> typing.List[typing.Any]:
|
2018-08-18 23:19:53 +00:00
|
|
|
return self.bot.database.channel_settings.find_prefix(self.id,
|
|
|
|
prefix, default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def del_setting(self, setting: str):
|
2018-08-18 23:19:53 +00:00
|
|
|
self.bot.database.channel_settings.delete(self.id, setting)
|
2018-08-05 11:52:29 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def set_user_setting(self, user_id: int, setting: str, value: typing.Any):
|
2018-08-18 23:19:53 +00:00
|
|
|
self.bot.database.user_channel_settings.set(user_id, self.id,
|
|
|
|
setting, value)
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_user_setting(self, user_id: int, setting: str,
|
|
|
|
default: typing.Any=None) -> typing.Any:
|
2018-08-18 23:19:53 +00:00
|
|
|
return self.bot.database.user_channel_settings.get(user_id,
|
|
|
|
self.id, setting, default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_user_settings(self, user_id: int, pattern: str,
|
|
|
|
default: typing.Any=[]) -> typing.List[typing.Any]:
|
2018-08-18 23:19:53 +00:00
|
|
|
return self.bot.database.user_channel_settings.find(user_id,
|
|
|
|
self.id, pattern, default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_user_settings_prefix(self, user_id: int, prefix: str,
|
|
|
|
default: typing.Any=[]) -> typing.List[typing.Any]:
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.bot.database.user_channel_settings.find_prefix(
|
2018-08-18 23:19:53 +00:00
|
|
|
user_id, self.id, prefix, default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def del_user_setting(self, user_id: int, setting: str):
|
2018-08-18 23:19:53 +00:00
|
|
|
self.bot.database.user_channel_settings.delete(user_id, self.id,
|
|
|
|
setting)
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_all_by_setting(self, setting: str, default: typing.Any=[]
|
|
|
|
) -> typing.List[typing.Any]:
|
2018-08-31 14:13:56 +00:00
|
|
|
return self.bot.database.user_channel_settings.find_all_by_setting(
|
|
|
|
self.id, setting, default)
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2019-02-18 14:56:25 +00:00
|
|
|
def send_message(self, text: str, tags: dict={}):
|
|
|
|
self.server.send_message(self.name, text, tags=tags)
|
|
|
|
def send_notice(self, text: str, tags: dict={}):
|
|
|
|
self.server.send_notice(self.name, text, tags=tags)
|
2019-02-22 22:34:54 +00:00
|
|
|
def send_tagmsg(self, tags: dict):
|
|
|
|
self.server.send_tagmsg(self.name, tags)
|
|
|
|
|
2019-02-18 14:56:25 +00:00
|
|
|
def send_mode(self, mode: str=None, target: typing.List[str]=None):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.server.send_mode(self.name, mode, target)
|
2018-10-30 14:58:48 +00:00
|
|
|
def send_kick(self, target: str, reason: str=None):
|
2016-04-18 15:50:19 +00:00
|
|
|
self.server.send_kick(self.name, target, reason)
|
2018-10-30 14:58:48 +00:00
|
|
|
def send_ban(self, hostmask: str):
|
2019-02-18 14:56:25 +00:00
|
|
|
self.server.send_mode(self.name, "+b", [hostmask])
|
2018-10-30 14:58:48 +00:00
|
|
|
def send_unban(self, hostmask: str):
|
2019-02-18 14:56:25 +00:00
|
|
|
self.server.send_mode(self.name, "-b", [hostmask])
|
2018-10-30 14:58:48 +00:00
|
|
|
def send_topic(self, topic: str):
|
2018-09-06 16:25:38 +00:00
|
|
|
self.server.send_topic(self.name, topic)
|
2018-10-30 14:58:48 +00:00
|
|
|
def send_part(self, reason: str=None):
|
2018-10-02 13:55:12 +00:00
|
|
|
self.server.send_part(self.name, reason)
|
2016-04-18 15:50:19 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def mode_or_above(self, user: IRCUser.User, mode: str) -> bool:
|
2018-10-03 15:44:00 +00:00
|
|
|
mode_orders = list(self.server.prefix_modes)
|
2016-03-29 11:56:58 +00:00
|
|
|
mode_index = mode_orders.index(mode)
|
|
|
|
for mode in mode_orders[:mode_index+1]:
|
2018-07-02 15:15:26 +00:00
|
|
|
if user in self.modes.get(mode, []):
|
2016-03-29 11:56:58 +00:00
|
|
|
return True
|
|
|
|
return False
|
2017-01-27 21:39:51 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def has_mode(self, user: IRCUser.User, mode: str) -> bool:
|
2018-10-11 10:20:53 +00:00
|
|
|
return user in self.modes.get(mode, [])
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_user_status(self, user: IRCUser.User) -> typing.Set:
|
2018-11-20 14:08:36 +00:00
|
|
|
return self.user_modes.get(user, set([]))
|