Showing with 35 additions and 39 deletions.
  1. +35 −39 sopel_channelmgnt/channelmgnt/__init__.py
@@ -8,7 +8,7 @@
from sopel import formatting
from sopel.config.types import StaticSection, ValidatedAttribute
from sopel.module import (
OP, commands, example, priority, require_admin, require_chanmsg
OP, commands, example, priority, require_admin, require_chanmsg,
)
from sopel.tools import Identifier
from sopel.tools import SopelMemory
@@ -32,8 +32,8 @@ class ChannelmgntSection(StaticSection):
def setup(bot):
"""Set up config and bot memory for the plugin."""
bot.config.define_section('channelmgnt', ChannelmgntSection)
bot.memory["channelmgnt"] = SopelMemory()
bot.memory["channelmgnt"]["jdcache"] = jp.createdict(bot.settings.channelmgnt.datafile)
bot.memory['channelmgnt'] = SopelMemory()
bot.memory['channelmgnt']['jdcache'] = jp.createdict(bot.settings.channelmgnt.datafile)


def configure(config):
@@ -50,18 +50,18 @@ def default_mask(trigger):
topic_ = formatting.bold('Topic:')
topic_ = formatting.color('| ' + topic_, formatting.colors.PURPLE)
arg = formatting.color('{}', formatting.colors.GREEN)
return '{} {} {} {}'.format(welcome, chan, topic_, arg)
return f'{welcome} {chan} {topic_} {arg}'


def chanopget(channeldata, chanopsjson):
"""Get chanop data for the given channel."""
chanops = []
if 'inherits-from' in channeldata.keys():
for x in channeldata["inherits-from"]:
for x in channeldata['inherits-from']:
y = channelparse(channel=x, cachedjson=chanopsjson)
chanops = chanops + y[0]["chanops"]
chanops = chanops + y[0]['chanops']
if 'chanops' in channeldata.keys():
chanops = chanops + (channeldata["chanops"])
chanops = chanops + (channeldata['chanops'])
if chanops == []:
return False
else:
@@ -81,10 +81,8 @@ def get_chanops(channel, cachedjson):
"""Get chanop data for the provided channel."""
channeldata = channelparse(channel=channel, cachedjson=cachedjson)
if not channeldata:
chanops = False
else:
chanops = chanopget(channeldata[0], channeldata[1])
return chanops
return False
return chanopget(channeldata[0], channeldata[1])


def deopbot(chan, bot):
@@ -94,7 +92,7 @@ def deopbot(chan, bot):

def makemodechange(bot, trigger, mode, isusermode=False, isbqmode=False, selfsafe=False):
"""Change the channel mode."""
chanops = get_chanops(str(trigger.sender), bot.memory["channelmgnt"]["jdcache"])
chanops = get_chanops(str(trigger.sender), bot.memory['channelmgnt']['jdcache'])
dodeop = False
if chanops:
if bot.channels[trigger.sender].privileges[bot.nick] < OP and trigger.account in chanops:
@@ -123,7 +121,7 @@ def makemodechange(bot, trigger, mode, isusermode=False, isbqmode=False, selfsaf
bot.reply('Access Denied. If in error, please contact the channel founder.')

else:
bot.reply('No ChanOps Found. Please ask for assistance in {}'.format(bot.settings.channelmgnt.support_channel))
bot.reply(f'No ChanOps Found. Please ask for assistance in {bot.settings.channelmgnt.support_channel}')


@require_chanmsg
@@ -172,7 +170,7 @@ def devoice(bot, trigger):
@example('.kick Zppix')
def kick(bot, trigger):
"""Kick a user from the channel."""
chanops = get_chanops(str(trigger.sender), bot.memory["channelmgnt"]["jdcache"])
chanops = get_chanops(str(trigger.sender), bot.memory['channelmgnt']['jdcache'])
dodeop = False
if chanops:
if bot.channels[trigger.sender].privileges[bot.nick] < OP and trigger.account in chanops:
@@ -202,7 +200,7 @@ def kick(bot, trigger):
else:
bot.reply('Access Denied. If in error, please contact the channel founder.')
else:
bot.reply('No ChanOps Found. Please ask for assistance in {}'.format(bot.settings.channelmgnt.support_channel))
bot.reply(f'No ChanOps Found. Please ask for assistance in {bot.settings.channelmgnt.support_channel}')


def parse_host_mask(text):
@@ -219,21 +217,21 @@ def parse_host_mask(text):
if mask == '*!*@*':
return mask
if re.match('^[^.@!/]+$', mask) is not None:
return '%s!*@*' % mask
return f'{mask}!*@*'
if re.match('^[^@!]+$', mask) is not None:
return '*!*@%s' % mask
return f'*!*@{mask}'

m = re.match('^([^!@]+)@$', mask)
if m is not None:
return '*!%s@*' % m.group(1)
return f'*!{m.group(1)}@*'

m = re.match('^([^!@]+)@([^@!]+)$', mask)
if m is not None:
return '*!%s@%s' % (m.group(1), m.group(2))
return f'*!{m.group(1)}@{m.group(2)}'

m = re.match('^([^!@]+)!(^[!@]+)@?$', mask)
if m is not None:
return '%s!%s@*' % (m.group(1), m.group(2))
return f'{m.group(1)}!{m.group(2)}@*'
return ''


@@ -276,7 +274,7 @@ def unquiet(bot, trigger):
@priority('high')
def kickban(bot, trigger):
"""Kick and ban a user from the channel. The bot must be a channel operator for this command to work."""
chanops = get_chanops(str(trigger.sender), bot.memory["channelmgnt"]["jdcache"])
chanops = get_chanops(str(trigger.sender), bot.memory['channelmgnt']['jdcache'])
dodeop = False
if chanops:
if bot.channels[trigger.sender].privileges[bot.nick] < OP and trigger.account in chanops:
@@ -291,7 +289,7 @@ def kickban(bot, trigger):
return
opt = Identifier(text[1])
nick = opt
mask = text[2] if any([s in text[2] for s in "!@*"]) else ''
mask = text[2] if any([s in text[2] for s in '!@*']) else ''
channel = trigger.sender
reasonidx = 3 if mask != '' else 2
if not opt.is_nick():
@@ -300,7 +298,7 @@ def kickban(bot, trigger):
return
channel = opt
nick = text[2]
mask = text[3] if any([s in text[3] for s in "!@*"]) else ''
mask = text[3] if any([s in text[3] for s in '!@*']) else ''
reasonidx = 4 if mask != '' else 3
reason = ' '.join(text[reasonidx:])
mask = parse_host_mask(trigger.group().split())
@@ -314,7 +312,7 @@ def kickban(bot, trigger):
else:
bot.reply('Access Denied. If in error, please contact the channel founder.')
else:
bot.reply('No ChanOps Found. Please ask for assistance in {}'.format(bot.settings.channelmgnt.support_channel))
bot.reply(f'No ChanOps Found. Please ask for assistance in {bot.settings.channelmgnt.support_channel}')


def get_mask(bot, channel, default):
@@ -327,7 +325,7 @@ def get_mask(bot, channel, default):
@example('.topic Your Great New Topic')
def topic(bot, trigger):
"""Change the channel topic. The bot must be a channel operator for this command to work."""
chanops = get_chanops(str(trigger.sender), bot.memory["channelmgnt"]["jdcache"])
chanops = get_chanops(str(trigger.sender), bot.memory['channelmgnt']['jdcache'])
dodeop = False
if chanops:
if bot.channels[trigger.sender].privileges[bot.nick] < OP and trigger.account in chanops:
@@ -356,21 +354,20 @@ def topic(bot, trigger):
if dodeop:
deopbot(trigger.sender, bot)
else:
bot.reply('Access Denied. If in error, please contact the channel founder.')
else:
bot.reply('No ChanOps Found. Please ask for assistance in {}'.format(bot.settings.channelmgnt.support_channel))
return bot.reply('Access Denied. If in error, please contact the channel founder.')
return bot.reply('No ChanOps Found. Please ask for assistance in {}'.format(bot.settings.channelmgnt.support_channel))


@require_chanmsg
@commands('tmask')
@example('.tmask Welcome to My Channel | Info: {}')
def set_mask(bot, trigger):
"""Set the topic mask to use for the current channel. Within the topic mask, {} is used to allow substituting in chunks of text. This mask is used when running the 'topic' command."""
chanops = get_chanops(str(trigger.sender), bot.memory["channelmgnt"]["jdcache"])
chanops = get_chanops(str(trigger.sender), bot.memory['channelmgnt']['jdcache'])
if chanops:
if trigger.account in chanops:
bot.db.set_channel_value(trigger.sender, 'topic_mask', trigger.group(2))
bot.say("Gotcha, " + trigger.account)
bot.say(f'Gotcha, {trigger.account}')
else:
bot.reply('Access Denied. If in error, please contact the channel founder.')
else:
@@ -391,7 +388,7 @@ def show_mask(bot, trigger):
@commands('invite')
def invite_user(bot, trigger):
"""Command to invite users to a room."""
chanops = get_chanops(str(trigger.sender), bot.memory["channelmgnt"]["jdcache"])
chanops = get_chanops(str(trigger.sender), bot.memory['channelmgnt']['jdcache'])
channel = trigger.sender
dodeop = False
if chanops:
@@ -410,24 +407,23 @@ def invite_user(bot, trigger):
else:
bot.reply('Access Denied. If in error, please contact the channel founder.')
else:
bot.reply('No ChanOps Found. Please ask for assistance in {}'.format(bot.settings.channelmgnt.support_channel))
bot.reply(f'No ChanOps Found. Please ask for assistance in {bot.settings.channelmgnt.support_channel}')


@require_admin(message="Only admins may purge cache.")
@commands('resetchanopcache')
def reset_chanop_cache(bot, trigger): # noqa: U100
"""Reset the cache of the channel management data file."""
bot.reply("Refreshing Cache...")
bot.memory["channelmgnt"]["jdcache"] = jp.createdict(bot.settings.channelmgnt.datafile)
bot.reply("Cache refreshed")
bot.reply('Refreshing Cache...')
bot.memory['channelmgnt']['jdcache'] = jp.createdict(bot.settings.channelmgnt.datafile)
bot.reply('Cache refreshed')


@require_admin(message="Only admins may check cache")
@commands('checkchanopcache')
def check_chanop_cache(bot, trigger): # noqa: U100
"""Validate the cache matches the copy on disk."""
result = jp.validatecache(bot.settings.channelmgnt.datafile, bot.memory["channelmgnt"]["jdcache"])
result = jp.validatecache(bot.settings.channelmgnt.datafile, bot.memory['channelmgnt']['jdcache'])
if result:
bot.reply("Cache is correct.")
else:
bot.reply("Cache does not match on-disk copy")
return bot.reply('Cache is correct.')
return bot.reply('Cache does not match on-disk copy')