Heavy improvements. Version 1.2

This commit is contained in:
Ben 2025-06-30 09:26:59 -07:00
parent 24f6f25506
commit a4ebbaca1d
Signed by: webmaster
GPG key ID: A5FCBAF34E6E8B50
5 changed files with 67 additions and 39 deletions

View file

@ -30,39 +30,44 @@ class ConsumerNTFY(Plugin):
return self.config["command_prefix"]
# Checks if a sender if allowed to send for a particular region
def validateSender(self, region: str, sender: str):
# Check that config value exists
if not self.config["allowed_regions"]: return False
# Check that region is allowed
if not self.config["allowed_regions"][region]: return False
# All senders allowed for this region
if len(self.config["allowed_regions"][region]) == 0: return True
# Check that sender is allowed for region
if not sender in self.config["allowed_regions"][region]: return False
return True
def validate_sender(self, region: str, sender: str):
# Mautrix isn't documented, like at all, so I'm just gonna catch the
# error because IDK how to see if a map is inside a map.
try: allowed_list = self.config["allowed_regions"][region]
except: return False
if len(allowed_list) == 0: return True
if sender in allowed_list: return True
# Sender not allowed in region config
return False
# Does the necesary config checks for the given event
# Returns list of regions to process (strings)
# Currently just the specified region and "__global__"
def validateReport(self, evt: MessageEvent, message: str):
# Does the necessary config checks for the given event
# Returns list of recursive configs to process
def validate_report(self, evt: MessageEvent, message: str):
# Split command (minus !command_name) into tokens
tokens = message.split()
region = tokens[0].lower()
# Each command must have a state/territory designation and a message
if len(tokens) < 2: return None
if len(tokens) < 2: return []
# And we must have self.config["region_configs"]
try: trashvariable = self.config["allowed_regions"][region]
except: return []
self.log.debug(region)
configs = [] # To be returned
# This is a list of regions to process for this specific message
# This is only used to consider __global__
regions_to_process = []
if (self.validateSender("__global__", evt.sender)):
regions_to_process.append("__global__")
if (self.validateSender(region, evt.sender)):
regions_to_process.append(region)
allowed = False
if region != "__global__" and (self.validate_sender(region, evt.sender)):
allowed = True # Also append __global__ conf automatically
try: configs.append(self.config["region_configs"][region])
except: trashvariable = None
return regions_to_process
if allowed or (self.validate_sender("__global__", evt.sender)):
try: configs.append(self.config["region_configs"]["__global__"])
except: trashvariable = None
return configs
# What gets called when !command_name message is sent
@command.new(name=get_command_name, help="Report Something")
@ -73,12 +78,11 @@ class ConsumerNTFY(Plugin):
# Iterate through each endpoint that the message should be pushed to
# (if any)
for region in self.validateReport(evt, message):
for region_config in self.validate_report(evt, message):
# Detect no regions in reaction
if ntfy_posts_passed is None: ntfy_posts_passed = True
# Grab region-specific conrfig
region_config = self.config["region_configs"][region]
self.log.debug(region_config)
# Create notification text
split_message = message.split()
@ -88,12 +92,12 @@ class ConsumerNTFY(Plugin):
url = region_config["server_url"] + "/" + region_config["server_topic"]
# Consider authentication
authentication = None
if region_config["server_use_authentication"]:
authentication = aiohttp.BasicAuth(region_config["server_username"], region_config["server_password"])
auth = None
if "username" in region_config and "password" in region_config:
auth = aiohttp.BasicAuth(region_config["username"], region_config["password"])
# Send notification
async with self.http.post(url, data=text, auth=authentication) as response:
async with self.http.post(url, data=text, auth=auth) as response:
if not response.status == 200:
ntfy_posts_passed = False