''' This file is part of JANINE. JANINE is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. JANINE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with JANINE. If not, see . ''' import datetime import aioxmpp def make_msg(to, body): ''' Wrapper for creating a message object to enqueue or send. ''' msg = aioxmpp.Message( type_=aioxmpp.MessageType.CHAT, to=to) msg.body[None] = body return msg def pad_time_component(c): ''' If we have 12:08, it gets turned into 12:8, so we need to pad the components with a leading zero, if there is none. ''' if len(c) != 2: return f'0{c}' return c def format_time(time_str): ''' Reformat ISO style time data to a more readable format. ''' try: date = datetime.datetime.fromisoformat(time_str) except ValueError: return time_str return f'{date.day}.{date.month}.{date.year} {pad_time_component(date.hour)}:{pad_time_component(date.minute)}' def format_warning(warning): ''' Send a warning to all the recipients ''' # Reformat the message a bit effective_time = format_time(warning.effective_from) expiry_time = format_time(warning.expires) body = f'''*{warning.headline}* ({effective_time} bis {expiry_time}) {warning.description}''' if warning.instruction: body = f'''{body} {warning.instruction}''' # Smells like script injection, but okay body = body.replace('
', '\n') body = body.replace('
', '\n') return body def find_one(func, array): ''' Utility function Return the first element in array for which func returns True. ''' for e in array: if func(e): return e return None def find_all(func, array): ''' Utility function Return all elements in array for which func returns True. ''' return [e for e in array if func(e)]