diff options
-rw-r--r-- | wee_slack.py | 155 |
1 files changed, 80 insertions, 75 deletions
diff --git a/wee_slack.py b/wee_slack.py index 16a401d..a7eec90 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,7 @@ class Channel(SlackThing): self.active = active self.members = set(members) self.last_read = float(last_read) + self.previous_message = "" if active: self.create_buffer() self.attach_buffer() @@ -405,7 +410,10 @@ class Channel(SlackThing): colorized_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)) + if message != self.previous_message: + dbg([message, self.previous_message]) + w.prnt_date_tags(self.channel_buffer, time, tags, "%s\t%s" % (colorized_name, message)) + self.previous_message = message if set_read_marker: self.mark_read(False) else: @@ -698,7 +706,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 +723,90 @@ 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 + dbg(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"): + message_json["text"] = message_json["message"]["text"] + if message_json["message"].has_key("fallback"): + message_json["fallback"] = message_json["message"]["fallback"] 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'): + 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"] - 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) +# if message_json.has_key("text"): +# text = unfurl_refs(message_json["text"]) + "\n" +# else: +# text = "" +# text += unwrap_message(message_json) +# text += unfurl_refs(text) + 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): +# w.prnt("", 'unwrap called') + text = "" + if message_json.has_key('attachments'): + for attachment in message_json["attachments"]: + if attachment.has_key('title'): + text += "%s " % (attachment['title']) + 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']) + return 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) + 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 |