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'), sender=info.get('senderName', 'N/A'),
headline=info['headline'], headline=info['headline'],
description=info['description'], description=info['description'],
instruction=info.get('instruction', 'N/A'), instruction=info.get('instruction', ''),
landkreise=get_landkreise(data)) landkreise=get_landkreise(data))
def get_landkreise(data): def get_landkreise(data):
@ -189,6 +189,10 @@ class WarningBot:
if not self.__is_message_valid(msg): if not self.__is_message_valid(msg):
return return
# Send a deliverability receipt
receipt = aioxmpp.mdr.compose_receipt(msg)
self._client.enqueue(receipt)
cmd_parts = str(msg.body.any()).split(' ') cmd_parts = str(msg.body.any()).split(' ')
cmd = cmd_parts[0].lower() cmd = cmd_parts[0].lower()
@ -304,15 +308,15 @@ help - Gebe diese Hilfe aus'''
if warning.id in ids: if warning.id in ids:
continue 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) body = format_warning(warning)
for landkreis in warning.landkreise: for landkreis in warning.landkreise:
for to in self._warn_clients.get(landkreis, []): for to in self._warn_clients.get(landkreis, []):
msg = aioxmpp.stanza.Message( await self._client.send(make_msg(
to=aioxmpp.JID.fromstr(to), aioxmpp.JID.fromstr(to),
type_=aioxmpp.MessageType.CHAT) body))
msg.body[None] = body
await self._client.send(msg)
def _load_config(self, config_file): def _load_config(self, config_file):
# Load config # Load config
@ -332,6 +336,11 @@ help - Gebe diese Hilfe aus'''
with open(self._client_store, 'r') as clients_file: with open(self._client_store, 'r') as clients_file:
self._warn_clients = json.loads(clients_file.read()) self._warn_clients = json.loads(clients_file.read())
log.info('Clients read from disk') 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 ## Warnings
self._warnings_file = os.path.join(self._data_dir, 'warnings.json') self._warnings_file = os.path.join(self._data_dir, 'warnings.json')

View File

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