diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2018-01-28 20:25:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-28 20:25:07 +0100 |
commit | f44d5bd98ae657868a54ef2f0655273de4d6b657 (patch) | |
tree | a43b48734f45ffbc389833c290779a7c996aad48 /wee_slack.py | |
parent | 01a3032162e48bbb0e3a9a974f9561aaacc7f814 (diff) | |
parent | 073858642dc8ae33721f764d1bef4754603de20b (diff) | |
download | wee-slack-f44d5bd98ae657868a54ef2f0655273de4d6b657.tar.gz |
Merge pull request #487 from ajdavis/restore-slack-oauth
Restore OAuth support
Diffstat (limited to 'wee_slack.py')
-rw-r--r-- | wee_slack.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/wee_slack.py b/wee_slack.py index c452b1b..651e995 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -6,6 +6,7 @@ from collections import OrderedDict from functools import wraps from itertools import islice +import textwrap import time import json import pickle @@ -3004,6 +3005,52 @@ def me_command_cb(data, current_buffer, args): return w.WEECHAT_RC_OK_EAT +def command_register(data, current_buffer, args): + CLIENT_ID = "2468770254.51917335286" + CLIENT_SECRET = "dcb7fe380a000cba0cca3169a5fe8d70" # Not really a secret. + if args == 'register': + message = textwrap.dedent(""" + #### Retrieving a Slack token via OAUTH #### + + 1) Paste this into a browser: https://slack.com/oauth/authorize?client_id=2468770254.51917335286&scope=client + 2) Select the team you wish to access from wee-slack in your browser. + 3) Click "Authorize" in the browser **IMPORTANT: the redirect will fail, this is expected** + 4) Copy the "code" portion of the URL to your clipboard + 5) Return to weechat and run `/slack register [code]` + """) + w.prnt("", message) + return + + try: + _, oauth_code = args.split() + except ValueError: + w.prnt("", + "ERROR: wrong number of arguments given for register command") + return + + uri = ( + "https://slack.com/api/oauth.access?" + "client_id={}&client_secret={}&code={}" + ).format(CLIENT_ID, CLIENT_SECRET, oauth_code) + ret = urllib.urlopen(uri).read() + d = json.loads(ret) + if not d["ok"]: + w.prnt("", + "ERROR: Couldn't get Slack OAuth token: {}".format(d['error'])) + return + + if config.is_default('slack_api_token'): + w.config_set_plugin('slack_api_token', d['access_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']])) + + w.prnt("", "Success! Added team \"%s\"" % (d['team_name'],)) + w.prnt("", "Please reload wee-slack with: /script reload slack") + + @slack_buffer_or_ignore @utf8_decode def msg_command_cb(data, current_buffer, args): @@ -3651,6 +3698,10 @@ class PluginConfig(object): def get_int(self, key): return int(w.config_get_plugin(key)) + def is_default(self, key): + default = self.default_settings.get(key).default + return w.config_get_plugin(key) == default + get_debug_level = get_int get_group_name_prefix = get_string get_map_underline_to = get_string |