2016-11-12 11:03:36 +00:00
|
|
|
import uuid
|
2016-03-29 11:56:58 +00:00
|
|
|
import IRCLog
|
|
|
|
|
|
|
|
class Channel(object):
|
|
|
|
def __init__(self, name, server, bot):
|
2016-11-12 11:03:36 +00:00
|
|
|
self.id = uuid.uuid1().hex
|
2016-03-29 11:56:58 +00:00
|
|
|
self.name = name.lower()
|
|
|
|
self.server = server
|
|
|
|
self.bot = bot
|
|
|
|
self.topic = ""
|
|
|
|
self.topic_setter_nickname = None
|
|
|
|
self.topic_setter_username = None
|
|
|
|
self.topic_setter_hostname = None
|
|
|
|
self.topic_time = 0
|
|
|
|
self.users = set([])
|
|
|
|
self.modes = {}
|
|
|
|
self.created_timestamp = None
|
|
|
|
self.log = IRCLog.Log(bot)
|
2018-07-15 12:39:15 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def set_topic(self, topic):
|
|
|
|
self.topic = topic
|
|
|
|
def set_topic_setter(self, nickname, username=None, hostname=None):
|
|
|
|
self.topic_setter_nickname = nickname
|
|
|
|
self.topic_setter_username = username
|
|
|
|
self.topic_setter_hostname = hostname
|
|
|
|
def set_topic_time(self, unix_timestamp):
|
|
|
|
self.topic_time = unix_timestamp
|
2018-07-15 12:39:15 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def add_user(self, user):
|
|
|
|
self.users.add(user)
|
|
|
|
def remove_user(self, user):
|
|
|
|
self.users.remove(user)
|
2018-07-02 15:34:29 +00:00
|
|
|
for mode in list(self.modes.keys()):
|
|
|
|
if mode in self.server.mode_prefixes.values(
|
2018-07-02 15:19:22 +00:00
|
|
|
) 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]
|
2016-04-18 15:50:19 +00:00
|
|
|
def has_user(self, user):
|
|
|
|
return user in self.users
|
2018-07-15 12:39:15 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def add_mode(self, mode, args=None):
|
|
|
|
if not mode in self.modes:
|
|
|
|
self.modes[mode] = set([])
|
|
|
|
if args:
|
2018-07-02 15:19:22 +00:00
|
|
|
if mode in self.server.mode_prefixes.values():
|
2018-07-02 15:15:26 +00:00
|
|
|
user = self.server.get_user(args)
|
|
|
|
if user:
|
|
|
|
self.modes[mode].add(user)
|
|
|
|
else:
|
|
|
|
self.modes[mode].add(args.lower())
|
2016-04-03 12:20:05 +00:00
|
|
|
self.bot.events.on("mode").on("channel").call(
|
|
|
|
channel=self, mode=mode, args=args, remove=False)
|
2016-03-29 11:56:58 +00:00
|
|
|
def remove_mode(self, mode, args=None):
|
|
|
|
if not args:
|
|
|
|
del self.modes[mode]
|
|
|
|
else:
|
2018-07-02 15:19:22 +00:00
|
|
|
if mode in self.server.mode_prefixes.values():
|
2018-07-02 15:15:26 +00:00
|
|
|
user = self.server.get_user(args)
|
|
|
|
if user:
|
|
|
|
self.modes[mode].discard(user)
|
|
|
|
else:
|
|
|
|
self.modes[mode].discard(args.lower())
|
2016-03-29 11:56:58 +00:00
|
|
|
if not len(self.modes[mode]):
|
|
|
|
del self.modes[mode]
|
2016-04-03 12:20:05 +00:00
|
|
|
self.bot.events.on("mode").on("channel").call(
|
|
|
|
channel=self, mode=mode, args=args, remove=True)
|
2018-07-15 12:39:15 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def set_setting(self, setting, value):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.bot.database.channel_settings.set(self.server.id,
|
2016-03-29 11:56:58 +00:00
|
|
|
self.name, setting, value)
|
|
|
|
def get_setting(self, setting, default=None):
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.bot.database.channel_settings.get(
|
2016-03-29 11:56:58 +00:00
|
|
|
self.server.id, self.name, setting, default)
|
|
|
|
def find_settings(self, pattern, default=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.bot.database.channel_settings.find(
|
2016-03-29 11:56:58 +00:00
|
|
|
self.server.id, self.name, pattern, default)
|
2018-08-03 12:43:45 +00:00
|
|
|
def find_settings_prefix(self, prefix, default=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.bot.database.channel_settings.find_prefix(
|
2018-08-03 12:43:45 +00:00
|
|
|
self.server.id, self.name, prefix, default)
|
2016-03-29 11:56:58 +00:00
|
|
|
def del_setting(self, setting):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.bot.database.channel_settings.delete(self.server.id,
|
2016-03-29 11:56:58 +00:00
|
|
|
self.name, setting)
|
2018-08-05 11:52:29 +00:00
|
|
|
|
|
|
|
def set_user_setting(self, nickname, setting, value):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.bot.database.user_channel_settings.set(self.server.id,
|
2018-08-05 11:52:29 +00:00
|
|
|
self.name, nickname, setting, value)
|
|
|
|
def get_user_setting(self, nickname, setting, default=None):
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.bot.database.user_channel_settings.get(
|
2018-08-05 11:52:29 +00:00
|
|
|
self.server.id, self.name, nickname, setting, default)
|
|
|
|
def find_user_settings(self, nickname, pattern, default=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.bot.database.user_channel_settings.find(
|
2018-08-05 11:52:29 +00:00
|
|
|
self.server.id, self.name, nickname, pattern, default)
|
|
|
|
def find_user_settings_prefix(self, nickname, prefix, default=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.bot.database.user_channel_settings.find_prefix(
|
2018-08-05 11:52:29 +00:00
|
|
|
self.server.id, self.name, nickname, prefix, default)
|
|
|
|
def del_user_setting(self, nickname, setting):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.bot.database.user_channel_settings.delete(self.server.id,
|
2018-08-05 11:52:29 +00:00
|
|
|
self.name, nickname, setting)
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-07-14 08:50:12 +00:00
|
|
|
def send_message(self, text, prefix=None):
|
|
|
|
self.server.send_message(self.name, text, prefix=prefix)
|
2016-03-29 11:56:58 +00:00
|
|
|
def send_mode(self, mode=None, target=None):
|
|
|
|
self.server.send_mode(self.name, mode, target)
|
2016-04-18 15:50:19 +00:00
|
|
|
def send_kick(self, target, reason=None):
|
|
|
|
self.server.send_kick(self.name, target, reason)
|
|
|
|
def send_ban(self, hostmask):
|
|
|
|
self.server.send_mode(self.name, "+b", hostmask)
|
2018-07-19 13:39:10 +00:00
|
|
|
def send_unban(self, hostmask):
|
|
|
|
self.server.send_mode(self.name, "-b", hostmask)
|
2016-04-18 15:50:19 +00:00
|
|
|
|
2018-07-02 15:15:26 +00:00
|
|
|
def mode_or_above(self, user, mode):
|
2016-03-29 11:56:58 +00:00
|
|
|
mode_orders = list(self.server.mode_prefixes.values())
|
|
|
|
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-07-02 15:15:26 +00:00
|
|
|
def get_user_status(self, user):
|
2017-01-27 21:39:51 +00:00
|
|
|
modes = ""
|
|
|
|
for mode in self.server.mode_prefixes.values():
|
2018-07-02 15:15:26 +00:00
|
|
|
if user in self.modes.get(mode, []):
|
2017-01-27 21:39:51 +00:00
|
|
|
modes += mode
|
|
|
|
return modes
|