]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandCoronaPresenceLogger.py
modules/CommandJoinAll.py
[pub/jan/mattermost-bot.git] / modules / CommandCoronaPresenceLogger.py
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
5
6 import datetime
7 import random
8 import re
9 import threading
10 from inspect import cleandoc
11
12 from webdav3.client import Client
13
14
15 # pylint: disable=wrong-import-position
16 from AbstractCommand import AbstractCommand
17 class CommandCoronaPresenceLogger(AbstractCommand):
18     TRIGGER = "cpl"
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).
25
26         This information is retained for about a month.
27
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.
30         """)
31
32
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
37
38
39     def on_POST(self, request, data):
40         data['text'] = data['text'].strip()
41
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()
47
48             request.respond_cmd_chan("``AUTODELETE-DAY`` **``CPL, OIDA!``**", props={"cpl-meme-upload-todo":"todo"})
49
50         elif re.match(r"^(-|—|–)+", data['text']):
51             filename = self.datadir + datetime.date.today().isoformat()+"__"+data["user_id"]
52             with open(filename, "a") as file:
53                 file.write(data["user_id"]+" -- "+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.``
57                     This information is retained for about a month.
58
59                     #### :point_right: If you turn out to be COVID-positive please inform @someone to issue a warning to other possibly exposed people. :point_left:
60                     """))
61                 self.bot.api.add_user_to_channel("m5sik9jwmib6mfpnpcmsib1h7w", data["user_id"])
62                 self.bot.debug_chan("``/cpl`` presence logged: ``"+data["user_name"]+"``")
63
64         else:
65             request.respond_cmd_err("Please use ``/cpl --yes`` to confirm your presence at fsinf. \n\n"+self.USEINFO)
66
67
68     # racecondition if called in the same channel too fast. nonissue?
69     def _upload_meme(self, request, data):
70         post_cnt = 0
71         for post in self.bot.api.get_posts_for_channel(data["channel_id"]):
72             post_cnt += 1
73             if post_cnt > 20:
74                 break
75
76             if "cpl-meme-upload-todo" in post["props"] and post["props"]["cpl-meme-upload-todo"] == "todo":
77                 client = Client(self.dav_options)
78                 file_list = client.list()
79                 del file_list[0]
80                 client.download_file(random.choice(file_list), '/tmp/cpl-meme.png')
81
82                 self.bot.api.create_post(post["channel_id"], '``AUTODELETE-DAY`` **``CPL, OIDA!``**', props={"from_webhook":"true"}, filepaths=['/tmp/cpl-meme.png'], root_id=post["root_id"])
83                 self.bot.api.delete_post(post["id"])