Skip to content

Commit e5be43c

Browse files
authored
Merge pull request #2352 from sopel-irc/url-exclude-perms
privileges, url: configurable minimum channel access requirement for `.urlban` and friends
2 parents 9014653 + 98c0afa commit e5be43c

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

sopel/modules/url.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import requests
2121
from urllib3.exceptions import LocationValueError # type: ignore[import]
2222

23-
from sopel import plugin, tools
23+
from sopel import plugin, privileges, tools
2424
from sopel.config import types
2525
from sopel.tools import web
2626

@@ -59,6 +59,12 @@ class UrlSection(types.StaticSection):
5959
# TODO some validation rules maybe?
6060
exclude = types.ListAttribute('exclude')
6161
"""A list of regular expressions to match URLs for which the title should not be shown."""
62+
exclude_required_access = types.ChoiceAttribute(
63+
'exclude_required_access',
64+
choices=privileges.__all__,
65+
default='OP',
66+
)
67+
"""Minimum channel access level required to edit ``exclude`` list using chat commands."""
6268
exclusion_char = types.ValidatedAttribute('exclusion_char', default='!')
6369
"""A character (or string) which, when immediately preceding a URL, will stop that URL's title from being shown."""
6470
shorten_url_length = types.ValidatedAttribute(
@@ -146,6 +152,20 @@ def shutdown(bot: Sopel):
146152
pass
147153

148154

155+
def _user_can_change_excludes(bot: SopelWrapper, trigger: Trigger):
156+
if trigger.admin:
157+
return True
158+
159+
required_access = bot.config.url.exclude_required_access
160+
channel = bot.channels[trigger.sender]
161+
user_access = channel.privileges[trigger.nick]
162+
163+
if user_access >= getattr(privileges, required_access):
164+
return True
165+
166+
return False
167+
168+
149169
@plugin.command('urlexclude', 'urlpexclude', 'urlban', 'urlpban')
150170
@plugin.example('.urlpexclude example\\.com/\\w+', user_help=True)
151171
@plugin.example('.urlexclude example.com/path', user_help=True)
@@ -161,6 +181,12 @@ def url_ban(bot: SopelWrapper, trigger: Trigger):
161181
bot.reply('This command requires a URL to exclude.')
162182
return
163183

184+
if not _user_can_change_excludes(bot, trigger):
185+
bot.reply(
186+
'Only admins and channel members with %s access or higher may '
187+
'modify URL excludes.' % bot.config.url.exclude_required_access)
188+
return
189+
164190
if trigger.group(1) in ['urlpexclude', 'urlpban']:
165191
# validate regex pattern
166192
try:
@@ -206,6 +232,12 @@ def url_unban(bot: SopelWrapper, trigger: Trigger):
206232
bot.reply('This command requires a URL to allow.')
207233
return
208234

235+
if not _user_can_change_excludes(bot, trigger):
236+
bot.reply(
237+
'Only admins and channel members with %s access or higher may '
238+
'modify URL excludes.' % bot.config.url.exclude_required_access)
239+
return
240+
209241
if trigger.group(1) in ['urlpallow', 'urlpunban']:
210242
# validate regex pattern
211243
try:

sopel/privileges.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@
5656
from __future__ import annotations
5757

5858

59+
__all__ = [
60+
'VOICE',
61+
'HALFOP',
62+
'OP',
63+
'ADMIN',
64+
'OWNER',
65+
'OPER',
66+
]
67+
68+
5969
VOICE = 1
6070
"""Privilege level for the +v channel permission
6171

0 commit comments

Comments
 (0)