1 # Mattermost Bot module.
2 # Copyright (c) 2016-2022 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 # Copyright (c) 2020 by Sabrina -- minor rewording of the message
4 # published under MIT-License
10 from inspect import cleandoc
12 from webdav3.client import Client
15 # pylint: disable=wrong-import-position
16 from AbstractCommand import AbstractCommand
17 class CommandCoronaPresenceLogger(AbstractCommand):
19 CONFIG = {"display_name": "somebot-command", "auto_complete": False}
20 CONFIG["description"] = AbstractCommand.ICON_PRIVATE+"Corona-presence-logging"
21 USEINFO = cleandoc("""
22 ``/cpl`` is our approach to corona related presence logging at fsinf.
23 Use ``/cpl --yes`` to get your presence at fsinf today logged.
24 Consider using this command if you stay longer than a few seconds at fsinf (grabbing a Drink for an exam).
26 This information is retained for about a month.
28 If we get informed of somebody testing positive for COVID-19, we shall inform anyone who was at fsinf on the same days.
29 If you shall test positive for COVID-19, please inform us, so we can inform anyone who was at fsinf on the same days as you.
33 def __init__(self, team_id, datadir, dav_options=None):
34 super().__init__(team_id)
35 self.datadir = datadir
36 self.dav_options = dav_options
39 def on_POST(self, request, data):
40 data['text'] = data['text'].strip()
42 if re.match(r"^(-|—|–)+meme$", data["text"]) and self.dav_options:
43 self.channel_index_th = threading.Timer(1.0, self._upload_meme, [request, data])
44 self.channel_index_th.setName("CommandCoronaPresenceLogger::upload_meme()")
45 self.channel_index_th.setDaemon(True)
46 self.channel_index_th.start()
48 request.respond_cmd_chan("``AUTODELETE-DAY`` **``CPL, OIDA!``**", props={"cpl-meme-upload-todo":"todo"})
50 elif re.match(r"^(-|—|–)+", data['text']):
51 filename = self.datadir + datetime.date.today().isoformat()+"--"+data["user_id"]
52 with open(filename, "a") as f:
53 f.write(data["user_name"]+" -- "+datetime.datetime.now().isoformat()+"\n")
54 request.respond_cmd_temp(cleandoc("""
55 ## :white_check_mark: Success! :)
56 #### ``Your presence at FSINF today has been logged.``
58 This information is retained for about a month.
60 If we get informed of somebody testing COVID-19 positive, we shall inform anyone being at fsinf on the same days.
61 If you shall test COVID-19 positive, please inform us, so we can inform anyone who was at fsinf on the same days as you.
63 self.bot.api.add_user_to_channel("m5sik9jwmib6mfpnpcmsib1h7w", data["user_id"])
64 self.bot.debug_chan("``/cpl`` presence logged: ``"+data["user_name"]+"``")
67 request.respond_err("Please use ``/cpl --yes`` to confirm your presence at fsinf. \n\n"+self.USEINFO)
70 # racecondition if called in the same channel too fast. nonissue?
71 def _upload_meme(self, request, data):
73 for p in self.bot.api.get_posts_for_channel(data["channel_id"]):
78 if "cpl-meme-upload-todo" in p["props"] and p["props"]["cpl-meme-upload-todo"] == "todo":
79 client = Client(self.dav_options)
80 file_list = client.list()
82 client.download_file(random.choice(file_list), '/tmp/cpl-meme.png')
84 self.bot.api.create_post(p["channel_id"], '``AUTODELETE-DAY`` **``CPL, OIDA!``**', props={"from_webhook":"true"}, filepaths=['/tmp/cpl-meme.png'], root_id=p["root_id"])
85 self.bot.api.delete_post(p["id"])