]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/WSGuestControl.py
core/AbstractCommand.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 # pylint: disable=wrong-import-position
12 from AbstractWSHandler import AbstractWSHandler
13 class WSGuestControl(AbstractWSHandler):
14     NAME = "guestcontrol"
15
16
17     def __init__(self, email_rex_str, guest_teams_and_channels):
18         super().__init__()
19
20         self.email_rex = re.compile(email_rex_str)
21         self.guest_teams_and_channels = guest_teams_and_channels
22
23
24     def on_WS_EVENT(self, data):
25         user =  self.bot.api.get_user(data["data"]["user"]["id"])
26
27         # promote guest with correct email
28         if self.email_rex.match(user["email"]) and "system_guest" in user["roles"]:
29             print("WSGuestControl: Promoting guest.")
30             self.bot.api.promote_a_guest(user["id"])
31
32             chan = self.bot.api.create_dm_channel_with(user["id"])
33             self.bot.api.create_post(chan["id"], cleandoc("""
34                 ### ``Your account has been converted to a FULL-USER account!``
35                 You are now able to use all the features mattermost and this instance has to offer!
36                 # :thumbsup:
37                 """))
38
39             return True
40
41         # demote user with incorrect email (email no longer changable to invalid - delete this?)
42         elif not self.email_rex.match(user["email"]) and not "system_guest" in user["roles"]:
43             print("WSGuestControl: Demoting user.")
44             pprint.pprint(user)
45             self.bot.command_stats_inc("WSGuestControl: demote user -- should not happen?")
46             self.bot.api.demote_a_user(user["id"])
47
48             for team_id, chan_ids in self.guest_teams_and_channels.items():
49                 for chan_id, excluded_user_ids in chan_ids.items():
50                     if not user["id"] in excluded_user_ids:
51                         self.bot.api.add_user_to_team(team_id, user["id"], exc=False)
52                         self.bot.api.add_user_to_channel(chan_id, user["id"], exc=False)
53
54             chan = self.bot.api.create_dm_channel_with(user["id"])
55             self.bot.api.create_post(chan["id"], cleandoc("""
56                 ## ``Confirm your student email to get full access to Mattermost``
57                 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:
58                 * Beratung (Consultation)
59                 * Guest-Talk
60
61                 Guest accounts are deleted after 30 days of inactivity.
62
63                 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.
64                 """))
65
66         else:
67             print("WSGuestControl: Profile change we dont care about.")