diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2020-03-20 09:27:16 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2020-03-21 13:00:42 +0100 |
commit | 9d20b09153d7fb89301716c54edf0f055cdfb711 (patch) | |
tree | d1ccb048b5ac3f96f74bd29a168bc9b2acca689a /wee_slack.py | |
parent | a43b09c27ca62a8fc3f078f7b9b2b7fe04ee43c6 (diff) | |
download | wee-slack-9d20b09153d7fb89301716c54edf0f055cdfb711.tar.gz |
Allow using /slack register with an existing token
Diffstat (limited to 'wee_slack.py')
-rw-r--r-- | wee_slack.py | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/wee_slack.py b/wee_slack.py index 05c4a4d..82d9f6d 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -3772,8 +3772,10 @@ def me_command_cb(data, current_buffer, args): @utf8_decode def command_register(data, current_buffer, args): """ - /slack register [code] - Register a Slack team in wee-slack. + /slack register [code/token] + Register a Slack team in wee-slack. Call this without any arguments and + follow the instructions to register a new team. If you already have a token + for a team, you can call this with that token to add it. """ CLIENT_ID = "2468770254.51917335286" CLIENT_SECRET = "dcb7fe380a000cba0cca3169a5fe8d70" # Not really a secret. @@ -3789,6 +3791,9 @@ def command_register(data, current_buffer, args): """).strip().format(CLIENT_ID, REDIRECT_URI) w.prnt("", message) return w.WEECHAT_RC_OK_EAT + elif args.startswith('xox'): + add_token(args) + return w.WEECHAT_RC_OK_EAT uri = ( "https://slack.com/api/oauth.access?" @@ -3818,18 +3823,27 @@ def register_callback(data, command, return_code, out, err): "ERROR: Couldn't get Slack OAuth token: {}".format(d['error'])) return w.WEECHAT_RC_OK_EAT + add_token(d['access_token'], d['team_name']) + return w.WEECHAT_RC_OK_EAT + + +def add_token(token, team_name=None): if config.is_default('slack_api_token'): - w.config_set_plugin('slack_api_token', d['access_token']) + w.config_set_plugin('slack_api_token', token) else: # Add new token to existing set, joined by comma. - tok = config.get_string('slack_api_token') - w.config_set_plugin('slack_api_token', - ','.join([tok, d['access_token']])) + existing_tokens = config.get_string('slack_api_token') + if token in existing_tokens: + print_error('This token is already registered') + return + w.config_set_plugin('slack_api_token', ','.join([existing_tokens, token])) - w.prnt("", "Success! Added team \"%s\"" % (d['team_name'],)) + if team_name: + w.prnt("", "Success! Added team \"{}\"".format(team_name)) + else: + w.prnt("", "Success! Added token") w.prnt("", "Please reload wee-slack with: /python reload slack") w.prnt("", "If you want to add another team you can repeat this process from step 1 before reloading wee-slack.") - return w.WEECHAT_RC_OK_EAT @slack_buffer_or_ignore |