]> git.somenet.org - pub/jan/mattermost.git/blob - somebot/modules/CommandMateBindAccount.py
[somebot] /randomfood
[pub/jan/mattermost.git] / somebot / modules / CommandMateBindAccount.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 import MCAPI
6
7
8 from AbstractCommand import *
9 class CommandMateBindAccount(AbstractCommand):
10     TRIGGER = "mate-bind-account"
11     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
12         "auto_complete_hint": "<mate-terminal-username> <mate-terminal-password>",
13     }
14     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Bind your mate-terminal-account to your mattermost username."
15
16
17     def on_POST(self, request, data):
18         data['text'] = data['text'].strip()
19         mc_credentials = data['text'].split(" ")
20
21         if data["text"] == "" or data["text"].lower() == "help" or len(mc_credentials) != 2:
22             request.cmd_respond_text_temp("This command takes exactly 2 parameters: ``<mate-terminal-username> <mate-terminal-password``.\n"
23                 "Once you passed the correct mate-terminal credentials your Mattermost account will be bound to that account.\n"
24                 "The binding can be undone at the mate-terminal @ fsinf or via debug weg-front-end.\n"
25                 "Once sucessfully bound you will be able to use ``/mate <mate-terminal-input-string>`` to pay for your drink.")
26
27         try:
28             handle = MCAPI.API("KEYBOARD", mc_credentials[0], mc_credentials[1])
29             result = handle.manage_token('add', 'MM_'+data["user_id"])
30         except MCAPI.APIException as e:
31             if 'CERR_AUTH_FAIL_ACCPWDRATE' == e.message:
32                 request.cmd_respond_text_temp(":stop_sign: Failed to bind Mattermost account to mate-terminal account: Invalid mate-terminal credentials :(")
33                 return
34             elif 'CERR_FAILED_TO_ADD_TOKEN_EXISTING' == e.message:
35                 request.cmd_respond_text_temp(":warning: Failed to bind Mattermost account to mate-terminal account: Mattermost username is already bound to an account.")
36                 return
37
38         request.cmd_respond_text_temp(":white_check_mark: Sucessfully bound Mattermost account and mate-terminal account :)")
39         self.bot.debug_chan("``/mate-bind-account success: @"+data["user_name"]+'``')