]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandCoronaPresenceLogger.py
modules/CACommandModDelNowNotifyAuthor.py
[pub/jan/mattermost-bot.git] / modules / CommandCoronaPresenceLogger.py
1 # Mattermost Bot module.
2 #  Copyright (c) 2016-2021 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 from AbstractCommand import AbstractCommand
16 class CommandCoronaPresenceLogger(AbstractCommand):
17     TRIGGER = "cpl"
18     CONFIG = {"display_name": "somebot-command", "auto_complete": False}
19     CONFIG["description"] = "Corona-presence-logging"
20     USEINFO = cleandoc("""
21         ``/cpl`` is our approach to corona related presence logging at fsinf.
22         Use ``/cpl --yes`` to get your presence at fsinf today logged.
23         Consider using this command if you stay longer than a few seconds at fsinf (grabbing a Drink for an exam).
24
25         This information is retained for about a month.
26
27         If we get informed of somebody testing positive for COVID-19, we shall inform anyone who was at fsinf on the same days.
28         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.
29         """)
30
31
32     def __init__(self, team_id, datadir, dav_options=None):
33         super().__init__(team_id)
34         self.datadir = datadir
35         self.dav_options = dav_options
36
37
38     def on_POST(self, request, data):
39         data['text'] = data['text'].strip()
40
41         if re.match(r"^(-|—|–)+meme$", data["text"]) and self.dav_options:
42             self.channel_index_th = threading.Timer(1.0, self._upload_meme, [request, data])
43             self.channel_index_th.setName("CommandCoronaPresenceLogger::upload_meme()")
44             self.channel_index_th.setDaemon(True)
45             self.channel_index_th.start()
46
47             request.cmd_respond_text_chan("``AUTODELETE-DAY`` **``CPL, OIDA!``**", props={"cpl-meme-upload-todo":"todo"})
48
49         # accept all.
50         #elif re.match(r"^(-|—|–)+y(e|a+)s$", data['text']):
51         if re.match(r"^(-|—|–)+", data['text']):
52             filename = self.datadir + datetime.date.today().isoformat()+"--"+data["user_id"]
53             with open(filename, "a") as f:
54                 f.write(data["user_name"]+" -- "+datetime.datetime.now().isoformat()+"\n")
55                 request.cmd_respond_text_temp(cleandoc("""
56                     ## :white_check_mark: ``Your presence at FSINF today has been logged.`` :white_check_mark:
57                     This information is retained for about a month.
58
59                     If we get informed of somebody testing COVID-19 positive, we shall inform anyone being at fsinf on the same days.
60                     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.
61                     """))
62                 self.bot.api.add_user_to_channel("m5sik9jwmib6mfpnpcmsib1h7w", data["user_id"])
63                 self.bot.debug_chan("``/cpl`` presence logged: ``"+data["user_name"]+"``")
64
65         else:
66             request.cmd_respond_text_temp("## :warning: Please use ``/cpl --yes`` to confirm your presence at fsinf. :warning: \n\n"+self.USEINFO)
67
68
69     # racecondition if called in the same channel too fast. nonissue?
70     def _upload_meme(self, request, data):
71             post_cnt = 0
72             for p in self.bot.api.get_posts_for_channel(data["channel_id"]):
73                 post_cnt += 1
74                 if post_cnt > 20:
75                     break
76
77                 if "cpl-meme-upload-todo" in p["props"] and p["props"]["cpl-meme-upload-todo"] == "todo":
78                     client = Client(self.dav_options)
79                     file_list = client.list()
80                     del file_list[0]
81                     client.download_file(random.choice(file_list), '/tmp/cpl-meme.png')
82
83                     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"])
84                     self.bot.api.delete_post(p["id"])