]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/WSGuestControl.py
new file: modules/CommandFSOpen.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 AbstractPublicWS import AbstractPublicWS
13 from AbstractWSHandler import AbstractWSHandler
14 class WSGuestControl(AbstractWSHandler, AbstractPublicWS):
15     TRIGGER=NAME = "guestcontrol"
16
17
18     def __init__(self, email_rex_str, guest_teams_and_channels, primary_teamid):
19         super().__init__()
20
21         self.email_rex = re.compile(email_rex_str)
22         self.guest_teams_and_channels = guest_teams_and_channels
23         self.primary_teamid = primary_teamid
24
25
26     def on_WS_EVENT(self, data):
27         user =  self.bot.api.get_user(data["data"]["user"]["id"])
28
29         # promote guest with correct email
30         if self.email_rex.match(user["email"]) and "system_guest" in user["roles"]:
31             print("WSGuestControl: Promoting guest.")
32             self.bot.api.promote_a_guest(user["id"])
33
34             chan = self.bot.api.create_dm_channel_with(user["id"])
35             self.bot.api.create_post(chan["id"], cleandoc("""
36                 ### ``Your account has been converted to a FULL-USER account!``
37                 You are now able to use all the features mattermost and this instance has to offer!
38                 # :thumbsup:
39                 """))
40
41             return True
42
43         # demote user with incorrect email (email no longer changable to invalid - delete this?)
44         elif not self.email_rex.match(user["email"]) and not "system_guest" in user["roles"]:
45             print("WSGuestControl: Demoting user.")
46             pprint.pprint(user)
47             self.bot.command_stats_inc("WSGuestControl: demote user -- should not happen?")
48             self.bot.api.demote_a_user(user["id"])
49
50             for team_id, chan_ids in self.guest_teams_and_channels.items():
51                 for chan_id in chan_ids:
52                     self.bot.api.add_user_to_team(team_id, user["id"], exc=False)
53                     self.bot.api.add_user_to_channel(chan_id, user["id"], exc=False)
54
55             chan = self.bot.api.create_dm_channel_with(user["id"])
56             self.bot.api.create_post(chan["id"], cleandoc("""
57                 ## ``Confirm your student email to get full access to Mattermost``
58                 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:
59                 * Beratung (Consultation)
60                 * Guest-Talk
61
62                 Guest accounts are deleted after 30 days of inactivity.
63
64                 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.
65                 """))
66
67         else:
68             print("WSGuestControl: Profile change we dont care about.")
69
70
71     def on_public_POST(self, request, data):
72         import pprint
73         pprint.pprint(data)
74
75         # todo: test if email seems valid.
76
77         channels = [ch_id for tid,ch_id_set in self.guest_teams_and_channels.items() for ch_id in ch_id_set]
78         message = "Welcome to FSInf-Mattermost. Guest accounts are very limited due to privacy, copyright and licencing reasons and are deleted after 30 das of inactivity. Set your student's eMail address (eXXXXXXXX@student.tuwien.ac.at) as soon as possible."
79         request.respond_public(200, self.bot.api.invite_guests_to_team_by_email(self.primary_teamid, data["email"], channels, message))