diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2017-09-18 22:10:53 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2017-09-18 22:10:53 +0200 |
commit | ec838dbc03613554cdf27476cab18fd7d68344aa (patch) | |
tree | f8fb78a6482e122402891cf860b8f0ef3f015887 /wee_slack.py | |
parent | 1e040228ee639fa939f2cc391c4038a039a50d59 (diff) | |
download | wee-slack-ec838dbc03613554cdf27476cab18fd7d68344aa.tar.gz |
Return early from handle_rtmstart on failure
This makes the error case a bit clearer and easier to read.
Diffstat (limited to 'wee_slack.py')
-rw-r--r-- | wee_slack.py | 136 |
1 files changed, 68 insertions, 68 deletions
diff --git a/wee_slack.py b/wee_slack.py index b1ccb09..c53b4f5 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -2033,78 +2033,78 @@ def handle_rtmstart(login_data, eventrouter): """ metadata = pickle.loads(login_data["wee_slack_request_metadata"]) - if login_data["ok"]: - # Let's reuse a team if we have it already. - th = SlackTeam.generate_team_hash(login_data['self']['name'], login_data['team']['domain']) - if not eventrouter.teams.get(th): - - users = {} - for item in login_data["users"]: - users[item["id"]] = SlackUser(**item) - # users.append(SlackUser(**item)) - - bots = {} - for item in login_data["bots"]: - bots[item["id"]] = SlackBot(**item) - - channels = {} - for item in login_data["channels"]: - channels[item["id"]] = SlackChannel(eventrouter, **item) - - for item in login_data["ims"]: - channels[item["id"]] = SlackDMChannel(eventrouter, users, **item) - - for item in login_data["groups"]: - if item["name"].startswith('mpdm-'): - channels[item["id"]] = SlackMPDMChannel(eventrouter, **item) - else: - channels[item["id"]] = SlackGroupChannel(eventrouter, **item) - - t = SlackTeam( - eventrouter, - metadata.token, - login_data['url'], - login_data["team"]["domain"], - login_data["self"]["name"], - login_data["self"]["id"], - users, - bots, - channels, - muted_channels=login_data["self"]["prefs"]["muted_channels"], - highlight_words=login_data["self"]["prefs"]["highlight_words"], - ) - eventrouter.register_team(t) + if not login_data["ok"]: + w.prnt("", "ERROR: Failed connecting to Slack with token {}: {}" + .format(metadata.token, login_data["error"])) + return - else: - t = eventrouter.teams.get(th) - t.set_reconnect_url(login_data['url']) - t.connect() + # Let's reuse a team if we have it already. + th = SlackTeam.generate_team_hash(login_data['self']['name'], login_data['team']['domain']) + if not eventrouter.teams.get(th): - # web_socket_url = login_data['url'] - # try: - # ws = create_connection(web_socket_url, sslopt=sslopt_ca_certs) - # w.hook_fd(ws.sock._sock.fileno(), 1, 0, 0, "receive_ws_callback", t.get_team_hash()) - # #ws_hook = w.hook_fd(ws.sock._sock.fileno(), 1, 0, 0, "receive_ws_callback", pickle.dumps(t)) - # ws.sock.setblocking(0) - # t.attach_websocket(ws) - # t.set_connected() - # except Exception as e: - # dbg("websocket connection error: {}".format(e)) - # return False - - t.buffer_prnt('Connected to Slack') - t.buffer_prnt('{:<20} {}'.format("Websocket URL", login_data["url"])) - t.buffer_prnt('{:<20} {}'.format("User name", login_data["self"]["name"])) - t.buffer_prnt('{:<20} {}'.format("User ID", login_data["self"]["id"])) - t.buffer_prnt('{:<20} {}'.format("Team name", login_data["team"]["name"])) - t.buffer_prnt('{:<20} {}'.format("Team domain", login_data["team"]["domain"])) - t.buffer_prnt('{:<20} {}'.format("Team id", login_data["team"]["id"])) - - dbg("connected to {}".format(t.domain)) + users = {} + for item in login_data["users"]: + users[item["id"]] = SlackUser(**item) + # users.append(SlackUser(**item)) + + bots = {} + for item in login_data["bots"]: + bots[item["id"]] = SlackBot(**item) + + channels = {} + for item in login_data["channels"]: + channels[item["id"]] = SlackChannel(eventrouter, **item) + + for item in login_data["ims"]: + channels[item["id"]] = SlackDMChannel(eventrouter, users, **item) + + for item in login_data["groups"]: + if item["name"].startswith('mpdm-'): + channels[item["id"]] = SlackMPDMChannel(eventrouter, **item) + else: + channels[item["id"]] = SlackGroupChannel(eventrouter, **item) + + t = SlackTeam( + eventrouter, + metadata.token, + login_data['url'], + login_data["team"]["domain"], + login_data["self"]["name"], + login_data["self"]["id"], + users, + bots, + channels, + muted_channels=login_data["self"]["prefs"]["muted_channels"], + highlight_words=login_data["self"]["prefs"]["highlight_words"], + ) + eventrouter.register_team(t) else: - w.prnt("", "ERROR: Failed connecting to Slack with token {}: {}" - .format(metadata.token, login_data["error"])) + t = eventrouter.teams.get(th) + t.set_reconnect_url(login_data['url']) + t.connect() + + # web_socket_url = login_data['url'] + # try: + # ws = create_connection(web_socket_url, sslopt=sslopt_ca_certs) + # w.hook_fd(ws.sock._sock.fileno(), 1, 0, 0, "receive_ws_callback", t.get_team_hash()) + # #ws_hook = w.hook_fd(ws.sock._sock.fileno(), 1, 0, 0, "receive_ws_callback", pickle.dumps(t)) + # ws.sock.setblocking(0) + # t.attach_websocket(ws) + # t.set_connected() + # except Exception as e: + # dbg("websocket connection error: {}".format(e)) + # return False + + t.buffer_prnt('Connected to Slack') + t.buffer_prnt('{:<20} {}'.format("Websocket URL", login_data["url"])) + t.buffer_prnt('{:<20} {}'.format("User name", login_data["self"]["name"])) + t.buffer_prnt('{:<20} {}'.format("User ID", login_data["self"]["id"])) + t.buffer_prnt('{:<20} {}'.format("Team name", login_data["team"]["name"])) + t.buffer_prnt('{:<20} {}'.format("Team domain", login_data["team"]["domain"])) + t.buffer_prnt('{:<20} {}'.format("Team id", login_data["team"]["id"])) + + dbg("connected to {}".format(t.domain)) # self.identifier = self.domain |