Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

privileges, url: configurable minimum channel access requirement for .urlban and friends #2352

Merged
merged 2 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion sopel/modules/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import requests
from urllib3.exceptions import LocationValueError # type: ignore[import]

from sopel import plugin, tools
from sopel import plugin, privileges, tools
from sopel.config import types
from sopel.tools import web

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


def _user_can_change_excludes(bot: SopelWrapper, trigger: Trigger):
if trigger.admin:
return True

required_access = bot.config.url.exclude_required_access
channel = bot.channels[trigger.sender]
user_access = channel.privileges[trigger.nick]

if user_access >= getattr(privileges, required_access):
return True

return False


@plugin.command('urlexclude', 'urlpexclude', 'urlban', 'urlpban')
@plugin.example('.urlpexclude example\\.com/\\w+', user_help=True)
@plugin.example('.urlexclude example.com/path', user_help=True)
Expand All @@ -161,6 +181,12 @@ def url_ban(bot: SopelWrapper, trigger: Trigger):
bot.reply('This command requires a URL to exclude.')
return

if not _user_can_change_excludes(bot, trigger):
bot.reply(
'Only admins and channel members with %s access or higher may '
'modify URL excludes.' % bot.config.url.exclude_required_access)
return

if trigger.group(1) in ['urlpexclude', 'urlpban']:
# validate regex pattern
try:
Expand Down Expand Up @@ -206,6 +232,12 @@ def url_unban(bot: SopelWrapper, trigger: Trigger):
bot.reply('This command requires a URL to allow.')
return

if not _user_can_change_excludes(bot, trigger):
bot.reply(
'Only admins and channel members with %s access or higher may '
'modify URL excludes.' % bot.config.url.exclude_required_access)
return

if trigger.group(1) in ['urlpallow', 'urlpunban']:
# validate regex pattern
try:
Expand Down
10 changes: 10 additions & 0 deletions sopel/privileges.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@
from __future__ import annotations


__all__ = [
'VOICE',
'HALFOP',
'OP',
'ADMIN',
'OWNER',
'OPER',
]


VOICE = 1
"""Privilege level for the +v channel permission

Expand Down