diff options
-rw-r--r-- | _pytest/test_unfurl.py | 3 | ||||
-rw-r--r-- | wee_slack.py | 36 |
2 files changed, 20 insertions, 19 deletions
diff --git a/_pytest/test_unfurl.py b/_pytest/test_unfurl.py index c79c0f3..2b80eda 100644 --- a/_pytest/test_unfurl.py +++ b/_pytest/test_unfurl.py @@ -24,6 +24,9 @@ unfurl_map = [ { "input": "url: <https://example.com|example with spaces> suffix", "output": "url: https://example.com (example with spaces) suffix", }, + { "input": "<@U2147483697|@othernick> multiple unfurl <https://example.com|example with spaces>", + "output": "@othernick multiple unfurl https://example.com (example with spaces)", + }, ] diff --git a/wee_slack.py b/wee_slack.py index c8ccf5b..a8807ec 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -611,6 +611,9 @@ class Channel(object): if user == self.last_active_user and prefix_same_nick != "": name = prefix_same_nick else: + nick_prefix = w.config_string(w.config_get('weechat.look.nick_prefix')) + nick_suffix = w.config_string(w.config_get('weechat.look.nick_suffix')) + if self.server.users.find(user): name = self.server.users.find(user).formatted_name() self.last_active_user = user @@ -618,6 +621,7 @@ class Channel(object): else: name = user self.last_active_user = None + name = nick_prefix + name + nick_suffix name = name.decode('utf-8') #colorize nicks in each line chat_color = w.config_string(w.config_get('weechat.color.chat')) @@ -1286,7 +1290,7 @@ def process_reply(message_json): server = servers.find(message_json["_server"]) identifier = message_json["reply_to"] item = server.message_buffer.pop(identifier) - if type(item['text']) is not unicode: + if 'text' in item and type(item['text']) is not unicode: item['text'] = item['text'].decode('UTF-8', 'replace') if "type" in item: if item["type"] == "message" and "channel" in item.keys(): @@ -1586,9 +1590,9 @@ def process_message(message_json, cache=True): except Exception: channel = channels.find(message_json["channel"]) + dbg("cannot process message {}\n{}".format(message_json, traceback.format_exc())) if channel and ("text" in message_json) and message_json['text'] is not None: channel.buffer_prnt('unknown', message_json['text']) - dbg("cannot process message {}\n{}".format(message_json, traceback.format_exc())) def process_message_changed(message_json): @@ -1671,24 +1675,18 @@ def unfurl_ref(ref, ignore_alt_text=False): def unfurl_refs(text, ignore_alt_text=False): """ - Worst code ever written. this needs work + input : <@U096Q7CQM|someuser> has joined the channel + ouput : someuser has joined the channel """ - if text and text.find('<') > -1: - end = 0 - newtext = u"" - while text.find('<') > -1: - # Prepend prefix - newtext += text[:text.find('<')] - text = text[text.find('<'):] - end = text.find('>') - if end == -1: - newtext += text - break - # Format thingabob - newtext += unfurl_ref(text[1:end], ignore_alt_text) - text = text[end+1:] - newtext += text - return newtext + # Find all strings enclosed by <> + # - <https://example.com|example with spaces> + # - <#C2147483705|#otherchannel> + # - <@U2147483697|@othernick> + # Test patterns lives in ./_pytest/test_unfurl.py + matches = re.findall(r"(<[@#]?(?:[^<]*)>)", text) + for m in matches: + # Replace them with human readable strings + text = text.replace(m, unfurl_ref(m[1:-1], ignore_alt_text)) return text |