2020import requests
2121from urllib3 .exceptions import LocationValueError # type: ignore[import]
2222
23- from sopel import plugin , tools
23+ from sopel import plugin , privileges , tools
2424from sopel .config import types
2525from 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 :
0 commit comments