]> git.somenet.org - pub/jan/mattermost.git/blob - somebot/core/AbstractCommand.py
[somebot] /mate*
[pub/jan/mattermost.git] / somebot / core / AbstractCommand.py
1 # Mattermost Bot.
2 #  Copyright (c) 2016-2020 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 #  published under MIT-License
4
5
6 class RequireException(Exception):
7         """Required precondition not met-exception. Mostly used for permission checks. Message will be forwarded to command-caller/user"""
8
9
10 class AbstractCommand():
11     URL = None
12     TEAM_ID = None
13     TRIGGER = None
14     CONFIG = {"display_name": "somebot-command", "auto_complete": True}
15     USEINFO = None
16
17     bot = None
18     mm_secret_token = None
19
20
21     def __init__(self, team_id):
22         self.TEAM_ID = team_id
23
24
25 #    def __str__(self):
26 #        return str(self.__class__)+" for team_id: "+str(self.TEAM_ID)
27 #    def __repr__(self):
28 #        return self.__str__()
29
30
31     # can/should be overridden by the user
32     def on_register(self):
33         """Consider to override. Handles the post-command-registration logic at bot startup."""
34         self._create_slash_command()
35
36     # can/should be overridden by the user
37     def on_shutdown(self):
38         """Consider to override. Handles the shutdown-procedure."""
39         return
40
41     # should be overridden by the user
42     def on_POST(self, request, data):
43         """Override. Handles the post-command logic."""
44         return
45
46     # should be overridden by the user
47     # manual command authentication needed!
48     def on_POST_interactive(self, request, data):
49         """Consider to override. Handles the interactive-message logic."""
50         return
51
52     # should be overridden by the user
53     # manual command authentication needed!
54     def on_POST_dialog(self, request, data):
55         """Consider to override. Handles the dialog logic."""
56         return
57
58
59     def _on_register(self, bot):
60         self.bot = bot
61         self.URL = self.bot.local_websrv_url+"/"+self.TEAM_ID+"/"+self.TRIGGER
62         if self.USEINFO:
63             self.bot.USETOPICS.setdefault("/"+self.TRIGGER, self.USEINFO)
64         self.on_register()
65
66
67     def _on_shutdown(self):
68         self.on_shutdown()
69
70
71     def _on_POST(self, request, data):
72         try:
73             self.on_POST(request, data)
74         except RequireException as ex:
75             request.cmd_respond_text_temp(str(ex))
76
77
78     def _on_POST_interactive(self, request, data):
79         try:
80             self.on_POST_interactive(request, data)
81         except RequireException as ex:
82             request.cmd_respond_text_temp(str(ex))
83
84
85     def _on_POST_dialog(self, request, data):
86         try:
87             self.on_POST_dialog(request, data)
88         except RequireException as ex:
89             request.cmd_respond_text_temp(str(ex))
90
91
92     def _create_slash_command(self):
93         # (possibly) delete old version of command
94         for command in self.bot.api.list_custom_slash_commands_per_team(self.TEAM_ID):
95             if command["url"] == self.URL or command["trigger"].lower() == self.TRIGGER.lower():
96                 self.bot.api.delete_slash_command(command["id"])
97
98         # create slash command
99         res = self.bot.api.create_slash_command(self.TEAM_ID, self.TRIGGER.lower(), self.URL+"/command")
100         res.update(self.CONFIG)
101         self.bot.api.update_slash_command(res)
102         self.mm_secret_token = res["token"]
103
104
105     def _require_bot_admin(self, data):
106         """
107         Require exactly bot admin priviledges.
108         Throws RequireException if not priviledged.
109         """
110         if not data["user_id"] in self.bot.admin_ids:
111             raise RequireException("### Leave me alone. You are not by real dad.")
112
113
114     def _require_team_admin(self, data):
115         """
116         Require at least team admin priviledges.
117         Throws RequireException if not priviledged.
118         """
119         team_member = self.bot.api.get_team_member(data["team_id"], data["user_id"])
120         if "team_admin" not in team_member["roles"]:
121             raise RequireException("### You are not TEAM_ADMIN. :(")
122
123
124     def _require_channel_admin(self, data):
125         """
126         Require at least channel admin priviledges.
127         Throws RequireException if not priviledged.
128         """
129         raise RequireException("### Priviledge-check for CHANNEL_ADMIN not implemented/TODO, but required. yeah. nope.")