diff options
Diffstat (limited to 'wee_slack.py')
-rw-r--r-- | wee_slack.py | 178 |
1 files changed, 102 insertions, 76 deletions
diff --git a/wee_slack.py b/wee_slack.py index 16a401d..f5dc156 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -9,7 +9,11 @@ import urllib import urlparse from websocket import create_connection -import weechat as w +#hack to make tests possible.. better way? +try: + import weechat as w +except: + pass SCRIPT_NAME = "slack_extension" SCRIPT_AUTHOR = "Ryan Huber <rhuber@gmail.com>" @@ -274,6 +278,8 @@ class Channel(SlackThing): self.active = active self.members = set(members) self.last_read = float(last_read) + self.previous_prnt_name = "" + self.previous_prnt_message = "" if active: self.create_buffer() self.attach_buffer() @@ -402,10 +408,19 @@ class Channel(SlackThing): time = int(float(time)) if self.channel_buffer: if self.server.users.find(user) and user != self.server.nick: - colorized_name = self.server.users.find(user).colorized_name() + name = self.server.users.find(user).colorized_name() else: - colorized_name = user - w.prnt_date_tags(self.channel_buffer, time, tags, "%s\t%s" % (colorized_name, message)) + name = user + if message != self.previous_prnt_message: + #dbg([message, self.previous_prnt_message]) + w.prnt_date_tags(self.channel_buffer, time, tags, "%s\t%s" % (name, message)) + #eventually maybe - doesn't reprint name if next message is same user + #if name != self.previous_prnt_name: + # w.prnt_date_tags(self.channel_buffer, time, tags, "%s\t%s" % (name, message)) + # self.previous_prnt_name = name + #else: + # w.prnt_date_tags(self.channel_buffer, time, tags, "%s\t%s" % ("", message)) + self.previous_prnt_message = message if set_read_marker: self.mark_read(False) else: @@ -698,7 +713,6 @@ def process_im_marked(message_json): def process_im_created(message_json): server = servers.find(message_json["myserver"]) - dbg(message_json) item = message_json["channel"] channel_name = server.users.find(item["user"]).name if server.channels.find(channel_name): @@ -716,92 +730,104 @@ def process_user_typing(message_json): def process_error(message_json): connected = False +#def process_message_changed(message_json): +# process_message(message_json) def process_message(message_json): mark_silly_channels_read(message_json["channel"]) -# try: - known_subtypes = ['channel_join', 'channel_leave', 'message_changed'] + + #send these messages elsewhere + known_subtypes = ['channel_join', 'channel_leave'] if message_json.has_key("subtype") and message_json["subtype"] in known_subtypes: proc[message_json["subtype"]](message_json) + else: + #move message properties down to root of json object + message_json = unwrap_message(message_json) + server = servers.find(message_json["myserver"]) + channel = channels.find(message_json["channel"]) time = message_json['ts'] - channel = message_json["channel"] - - if message_json.has_key('text'): - text = message_json["text"] - elif message_json.has_key('fallback'): + if message_json.has_key("fallback"): text = message_json["fallback"] - elif message_json.has_key('title'): - text += message_json["title"] + elif message_json.has_key("text"): + text = message_json["text"] else: - dbg(message_json) text = "" - text = text.encode('ascii', 'ignore') - - if message_json.has_key('attachments'): - for attachment in message_json["attachments"]: - dbg(attachment) - if attachment.has_key('title'): - text += "%s" % (attachment['title']) -# if attachment.has_key('fallback'): -# text += "%s\n" % (attachment['fallback']) - if attachment.has_key('author_link'): - text += "%s: " % (attachment['author_link']) -# if attachment.has_key('author_name'): -# text += "%s: " % (attachment['author_name']) - if attachment.has_key('text'): - text += "%s" % (attachment['text']) - - #clean up tweets - text = re.sub("<.*?\\|(.*?)>", "\\1", text) - - #clean up references - if text.find('<@') > -1: - dbg('found reference!') - newtext = [] - text = text.split() - dbg(text) - for item in text: - if item.startswith('<@U'): - dbg('found user reference!') - item = item[2:-1] - if users.find_first(item): - item = "@" + users.find_first(item).name - newtext.append(item) - dbg(text) - text = " ".join(newtext) - - - text = text.replace('\t', ' ') - - #first figure out the name - if message_json.has_key('user'): - name = server.users.find(message_json['user']).name - elif message_json.has_key('username'): - name = "-%s-" % message_json["username"] - elif message_json.has_key('service_name'): - name = "-%s-" % message_json["service_name"] - elif message_json.has_key('bot_id'): - name = "-%s-" % message_json["bot_id"] - else: - name = "unknown name" - #color = w.info_get('irc_nick_color', name) - #def_color = w.color('default') - #name = "%s%s%s" % (color, name, def_color) + text = unfurl_refs(text) + if message_json.has_key("attachments"): + text += "\n%s" % (unwrap_attachments(message_json)) + text = text.lstrip() + text = text.replace("\t", " ") + name = get_user(message_json, server) server.channels.find(channel).buffer_prnt(name, text, time) -def process_message_changed(message_json): - if message_json["type"] != "message": - #dbg("message changed: " + str(message_json)) - server = servers.find(message_json["myserver"]) - channel = server.channels.find(message_json["channel"]) - user = server.users.find(message_json["message"]["user"]) - text = "Edited: " + message_json["message"]["text"] - time = message_json["ts"] - channel.buffer_prnt(user.name, text, time) +def unwrap_message(message_json): + if message_json.has_key("message"): + if message_json["message"].has_key("attachments"): + message_json["attachments"] = message_json["message"]["attachments"] + if message_json["message"].has_key("text"): + if message_json.has_key("text"): + message_json["text"] += message_json["message"]["text"] + dbg("added text!") + else: + message_json["text"] = message_json["message"]["text"] + if message_json["message"].has_key("fallback"): + if message_json.has_key("fallback"): + message_json["fallback"] += message_json["message"]["fallback"] + else: + message_json["fallback"] = message_json["message"]["fallback"] + return message_json + +def unwrap_attachments(message_json): + attachment_text = '' + for attachment in message_json["attachments"]: + if attachment.has_key("fallback"): + attachment_text += attachment["fallback"] + attachment_text = attachment_text.encode('ascii', 'ignore') + return attachment_text + +def unfurl_refs(text): + if text.find('<') > -1: + newtext = [] + text = text.split(" ") + for item in text: + #dbg(item) + if item.startswith('<') and item.endswith('>'): + item = item[1:-1] + dbg('found ref: %s' % item) + if item.find('|') > -1: + item = item.split('|')[0] + dbg('made ref: %s' % item) + if item.startswith('@U'): + if users.find(item[1:]): + try: + item = "@%s" % users.find(item[1:]).name + except: + dbg("NAME: " + str(item)) + if item.startswith('#c'): + if channels.find(item[1:]): + item = "#%s" % channels.find(item[1:]).name + newtext.append(item) + text = " ".join(newtext) + return text + else: + return text + +def get_user(message_json, server): + if message_json.has_key('user'): + name = server.users.find(message_json['user']).name + elif message_json.has_key('username'): + name = "-%s-" % message_json["username"] + elif message_json.has_key('service_name'): + name = "-%s-" % message_json["service_name"] + elif message_json.has_key('bot_id'): + name = "-%s-" % message_json["bot_id"] + else: + name = "" + return name ### END Websocket handling methods |