]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/WSGuestControl.py
modules/CommandCoronaPresenceLogger.py
[pub/jan/mattermost-bot.git] / modules / WSGuestControl.py
1 # Mattermost Bot module.
2 #  Copyright (c) 2016-2022 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 #  published under MIT-License
4
5 import pprint
6 import re
7
8 from inspect import cleandoc
9
10
11 from AbstractWSHandler import *
12 class WSGuestControl(AbstractWSHandler):
13     NAME = "guestcontrol"
14
15
16     def __init__(self, email_rex_str, guest_teams_and_channels):
17         super().__init__()
18
19         self.email_rex = re.compile(email_rex_str)
20         self.guest_teams_and_channels = guest_teams_and_channels
21
22
23     def on_WS_EVENT(self, data):
24         user =  self.bot.api.get_user(data["data"]["user"]["id"])
25
26         # promote guest with correct email
27         if self.email_rex.match(user["email"]) and "system_guest" in user["roles"]:
28             print("WSGuestControl: Promoting guest.")
29             self.bot.api.promote_a_guest(user["id"])
30
31             c = self.bot.api.create_dm_channel_with(user["id"])
32             self.bot.api.create_post(c["id"], cleandoc("""
33                 ### ``Your account has been converted to a FULL-USER account!``
34                 You are now able to use all the features mattermost and this instance has to offer!
35                 # :thumbsup:
36                 """))
37
38             return True
39
40         # demote user with incorrect email (email no longer changable to invalid - delete this?)
41         elif not self.email_rex.match(user["email"]) and not "system_guest" in user["roles"]:
42             print("WSGuestControl: Demoting user.")
43             pprint.pprint(user)
44             self.bot.command_stats_inc("WSGuestControl: demote user -- should not happen?")
45             self.bot.api.demote_a_user(user["id"])
46
47             for team_id, chan_ids in self.guest_teams_and_channels.items():
48                 for chan_id, excluded_user_ids in chan_ids.items():
49                     if not user["id"] in excluded_user_ids:
50                         self.bot.api.add_user_to_team(team_id, user["id"], exc=False)
51                         self.bot.api.add_user_to_channel(chan_id, user["id"], exc=False)
52
53             c = self.bot.api.create_dm_channel_with(user["id"])
54             self.bot.api.create_post(c["id"], cleandoc("""
55                 ## ``Confirm your student email to get full access to Mattermost``
56                 Your account is currently a [guest account](https://docs.mattermost.com/deployment/guest-accounts.html), which means your user expirience is limited due to privacy, copyright and licencing reasons and you are restricted to these channels:
57                 * Beratung (Consultation)
58                 * Guest-Talk
59
60                 Guest accounts are deleted after 30 days of inactivity.
61
62                 To gain full access to all channels and features you need to go to your Account Settings and change your email to ``eXXXXXXXX@student.tuwien.ac.at`` and then click on the link in the confirmation email. You will then get full access within an hour.
63                 """))
64
65         else:
66             print("WSGuestControl: Profile change we dont care about.")