]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandCoronaPresenceLogger.py
modules/CommandDSACheck.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"] = "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.cmd_respond_text_chan("``AUTODELETE-DAY`` **``CPL, OIDA!``**", props={"cpl-meme-upload-todo":"todo"})
49
50         # accept all.
51         #elif re.match(r"^(-|—|–)+y(e|a+)s$", data['text']):
52         if re.match(r"^(-|—|–)+", data['text']):
53             filename = self.datadir + datetime.date.today().isoformat()+"--"+data["user_id"]
54             with open(filename, "a") as f:
55                 f.write(data["user_name"]+" -- "+datetime.datetime.now().isoformat()+"\n")
56                 request.cmd_respond_text_temp(cleandoc("""
57                     ## :white_check_mark: ``Your presence at FSINF today has been logged.`` :white_check_mark:
58                     This information is retained for about a month.
59
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.
62                     """))
63                 self.bot.api.add_user_to_channel("m5sik9jwmib6mfpnpcmsib1h7w", data["user_id"])
64                 self.bot.debug_chan("``/cpl`` presence logged: ``"+data["user_name"]+"``")
65
66         else:
67             request.cmd_respond_text_temp("## :warning: Please use ``/cpl --yes`` to confirm your presence at fsinf. :warning: \n\n"+self.USEINFO)
68
69
70     # racecondition if called in the same channel too fast. nonissue?
71     def _upload_meme(self, request, data):
72             post_cnt = 0
73             for p in self.bot.api.get_posts_for_channel(data["channel_id"]):
74                 post_cnt += 1
75                 if post_cnt > 20:
76                     break
77
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()
81                     del file_list[0]
82                     client.download_file(random.choice(file_list), '/tmp/cpl-meme.png')
83
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"])