diff options
Diffstat (limited to 'wee_slack.py')
-rw-r--r-- | wee_slack.py | 64 |
1 files changed, 57 insertions, 7 deletions
diff --git a/wee_slack.py b/wee_slack.py index fb7bc6b..46643ec 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -13,6 +13,8 @@ import HTMLParser import sys import traceback import collections +import ssl + from websocket import create_connection,WebSocketConnectionClosedException # hack to make tests possible.. better way? @@ -58,6 +60,12 @@ SLACK_API_TRANSLATOR = { NICK_GROUP_HERE = "0|Here" NICK_GROUP_AWAY = "1|Away" +sslopt_ca_certs = {} +if hasattr(ssl, "get_default_verify_paths") and callable(ssl.get_default_verify_paths): + ssl_defaults = ssl.get_default_verify_paths() + if ssl_defaults.cafile is not None: + sslopt_ca_certs = {'ca_certs': ssl_defaults.cafile} + def dbg(message, fout=False, main_buffer=False): """ send debug output to the slack-debug buffer and optionally write to a file. @@ -258,12 +266,14 @@ class SlackServer(object): def create_local_buffer(self): if not w.buffer_search("", self.server_buffer_name): self.buffer = w.buffer_new(self.server_buffer_name, "buffer_input_cb", "", "", "") + if w.config_string(w.config_get('irc.look.server_buffer')) == 'merge_with_core': + w.buffer_merge(self.buffer, w.buffer_search_main()) w.buffer_set(self.buffer, "nicklist", "1") def create_slack_websocket(self, data): web_socket_url = data['url'] try: - self.ws = create_connection(web_socket_url) + self.ws = create_connection(web_socket_url, sslopt=sslopt_ca_certs) self.ws_hook = w.hook_fd(self.ws.sock._sock.fileno(), 1, 0, 0, "slack_websocket_cb", self.identifier) self.ws.sock.setblocking(0) return True @@ -976,6 +986,23 @@ def slack_buffer_required(f): @slack_buffer_required +def msg_command_cb(data, current_buffer, args): + dbg("msg_command_cb") + aargs = args.split(None, 2) + who = aargs[1] + + command_talk(current_buffer, who) + + if len(aargs) > 2: + message = aargs[2] + server = servers.find(current_domain_name()) + if server: + channel = server.channels.find(who) + channel.send_message(message) + return w.WEECHAT_RC_OK_EAT + + +@slack_buffer_required def command_upload(current_buffer, args): """ Uploads a file to the current buffer @@ -1004,7 +1031,7 @@ def command_talk(current_buffer, args): server = servers.find(current_domain_name()) if server: channel = server.channels.find(args) - if channel: + if not channel: channel.open() else: user = server.users.find(args) @@ -1644,14 +1671,36 @@ def unwrap_attachments(message_json, text_before): attachment_text = '' if "attachments" in message_json: if text_before: - attachment_text = u' --- ' + attachment_text = u'\n' for attachment in message_json["attachments"]: + # Attachments should be rendered roughly like: + # + # $pretext + # $title ($title_link) OR $from_url + # $text + # $fields t = [] - if "from_url" in attachment and text_before is False: - t.append(attachment['from_url']) - if "fallback" in attachment: + if 'pretext' in attachment: + t.append(attachment['pretext']) + if "title" in attachment: + if 'title_link' in attachment: + t.append('%s (%s)' % (attachment["title"], attachment["title_link"],)) + else: + t.append(attachment["title"]) + elif "from_url" in attachment: + t.append(attachment["from_url"]) + if "text" in attachment: + tx = re.sub(r' *\n[\n ]+', '\n', attachment["text"]) + t.append(tx) + if 'fields' in attachment: + for f in attachment['fields']: + if f['title'] != '': + t.append('%s %s' % (f['title'], f['value'],)) + else: + t.append(f['value']) + if t == [] and "fallback" in attachment: t.append(attachment["fallback"]) - attachment_text += ": ".join(t) + attachment_text += "\n".join([x.strip() for x in t if x]) return attachment_text @@ -2188,6 +2237,7 @@ if __name__ == "__main__": w.hook_command_run('/part', 'part_command_cb', '') w.hook_command_run('/leave', 'part_command_cb', '') w.hook_command_run('/topic', 'topic_command_cb', '') + w.hook_command_run('/msg', 'msg_command_cb', '') w.hook_command_run("/input complete_next", "complete_next_cb", "") w.hook_completion("nicks", "complete @-nicks for slack", "nick_completion_cb", "") |