]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/WSGuestControl.py
modules/CommandThreads.py
[pub/jan/mattermost-bot.git] / modules / WSGuestControl.py
1 # Mattermost Bot module.
2 #  Copyright (c) 2016-2021 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         # demote user with incorrect email (email no longer changable to invalid - delete this?)
39         elif not self.email_rex.match(user["email"]) and not "system_guest" in user["roles"]:
40             print("WSGuestControl: Demoting user.")
41             pprint.pprint(user)
42             self.bot.command_stats_inc("WSGuestControl: demote user -- should not happen?")
43             self.bot.api.demote_a_user(user["id"])
44
45             for team_id, chan_ids in self.guest_teams_and_channels.items():
46                 for chan_id, excluded_user_ids in chan_ids.items():
47                     if not user["id"] in excluded_user_ids:
48                         self.bot.api.add_user_to_team(team_id, user["id"], exc=False)
49                         self.bot.api.add_user_to_channel(chan_id, user["id"], exc=False)
50
51             c = self.bot.api.create_dm_channel_with(user["id"])
52             self.bot.api.create_post(c["id"], cleandoc("""
53                 ## ``Confirm your student email to get full access to Mattermost``
54                 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:
55                 * Beratung (Consultation)
56                 * Guest-Talk
57
58                 Guest accounts are deleted after 30 days of inactivity.
59
60                 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.
61                 """))
62
63         else:
64             print("WSGuestControl: Profile change we dont care about.")