fix: Also send instructions when we have them

This commit is contained in:
PapaTutuWawa 2020-09-27 12:44:38 +02:00
parent f6af3dcd2a
commit 68d09bbb3a
2 changed files with 21 additions and 7 deletions

View File

@ -80,7 +80,7 @@ def to_warning(data):
sender=info.get('senderName', 'N/A'),
headline=info['headline'],
description=info['description'],
instruction=info.get('instruction', 'N/A'),
instruction=info.get('instruction', ''),
landkreise=get_landkreise(data))
def get_landkreise(data):
@ -189,6 +189,10 @@ class WarningBot:
if not self.__is_message_valid(msg):
return
# Send a deliverability receipt
receipt = aioxmpp.mdr.compose_receipt(msg)
self._client.enqueue(receipt)
cmd_parts = str(msg.body.any()).split(' ')
cmd = cmd_parts[0].lower()
@ -304,15 +308,15 @@ help - Gebe diese Hilfe aus'''
if warning.id in ids:
continue
if len(set(warning.landkreise).intersection(self._warn_clients.keys())):
# We need to use a set as a warning can apply to more than one
# Landkreis
if set(warning.landkreise).intersection(self._warn_clients.keys()):
body = format_warning(warning)
for landkreis in warning.landkreise:
for to in self._warn_clients.get(landkreis, []):
msg = aioxmpp.stanza.Message(
to=aioxmpp.JID.fromstr(to),
type_=aioxmpp.MessageType.CHAT)
msg.body[None] = body
await self._client.send(msg)
await self._client.send(make_msg(
aioxmpp.JID.fromstr(to),
body))
def _load_config(self, config_file):
# Load config
@ -332,6 +336,11 @@ help - Gebe diese Hilfe aus'''
with open(self._client_store, 'r') as clients_file:
self._warn_clients = json.loads(clients_file.read())
log.info('Clients read from disk')
else:
with open(self._client_store, 'w') as clients_file:
clients_file.write('{}')
os.chmod(self._client_store, S_IRUSR | S_IWUSR)
log.info('Clients file created with 0600')
## Warnings
self._warnings_file = os.path.join(self._data_dir, 'warnings.json')

View File

@ -54,6 +54,11 @@ def format_warning(warning):
{warning.description}'''
if warning.instruction:
body = f'''{body}
{warning.instruction}'''
# Smells like script injection, but okay
body = body.replace('<br>', '\n')
body = body.replace('<br/>', '\n')