diff options
Diffstat (limited to 'wee_slack.py')
-rw-r--r-- | wee_slack.py | 51 |
1 files changed, 43 insertions, 8 deletions
diff --git a/wee_slack.py b/wee_slack.py index 76413fa..21cf0d3 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -222,7 +222,7 @@ class SlackServer(object): self.server_buffer_name = alias self.alias = alias else: - self.server_buffer_name = self.domain + self.server_buffer_name = "slack." + login_data["team"]["domain"] self.nick = login_data["self"]["name"] self.create_local_buffer() @@ -284,7 +284,7 @@ class SlackServer(object): def create_slack_mappings(self, data): for item in data["users"]: - self.add_user(User(self, item["name"], item["id"], item["presence"], item["deleted"])) + self.add_user(User(self, item["name"], item["id"], item["presence"], item["deleted"], is_bot=item.get('is_bot', False))) for item in data["bots"]: self.add_bot(Bot(self, item["name"], item["id"], item["deleted"])) @@ -635,7 +635,10 @@ class Channel(object): if self.channel_buffer: prefix_same_nick = w.config_string(w.config_get('weechat.look.prefix_same_nick')) if user == self.last_active_user and prefix_same_nick != "": - name = prefix_same_nick + if colorize_nicks and self.server.users.find(user): + name = self.server.users.find(user).color + prefix_same_nick + else: + 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')) @@ -653,11 +656,16 @@ class Channel(object): chat_color = w.config_string(w.config_get('weechat.color.chat')) if type(message) is not unicode: message = message.decode('UTF-8', 'replace') + curr_color = w.color(chat_color) + if colorize_nicks and colorize_messages and self.server.users.find(user): + curr_color = self.server.users.find(user).color + message = curr_color + message for user in self.server.users: if user.name in message: message = user.name_regex.sub( - r'\1\2{}\3'.format(user.formatted_name() + w.color(chat_color)), + r'\1\2{}\3'.format(user.formatted_name() + curr_color), message) + message = HTMLParser.HTMLParser().unescape(message) data = u"{}\t{}".format(name, message).encode('utf-8') w.prnt_date_tags(self.channel_buffer, time_int, tags, data) @@ -796,7 +804,7 @@ class DmChannel(Channel): class User(object): - def __init__(self, server, name, identifier, presence="away", deleted=False): + def __init__(self, server, name, identifier, presence="away", deleted=False, is_bot=False): self.server = server self.name = name self.identifier = identifier @@ -806,6 +814,7 @@ class User(object): self.channel_buffer = w.info_get("irc_buffer", "{}.{}".format(domain, self.name)) self.update_color() self.name_regex = re.compile(r"([\W]|\A)(@{0,1})" + self.name + "('s|[^'\w]|\Z)") + self.is_bot = is_bot if deleted: return @@ -1523,6 +1532,25 @@ def process_user_typing(message_json): channel.set_typing(server.users.find(message_json["user"]).name) +def process_bot_enable(message_json): + process_bot_integration(message_json) + + +def process_bot_disable(message_json): + process_bot_integration(message_json) + + +def process_bot_integration(message_json): + server = servers.find(message_json["_server"]) + channel = server.channels.find(message_json["channel"]) + + time = message_json['ts'] + text = "{} {}".format(server.users.find(message_json['user']).formatted_name(), + render_message(message_json)) + bot_name = get_user(message_json, server) + bot_name = bot_name.encode('utf-8') + channel.buffer_prnt(bot_name, text, time) + # todo: does this work? def process_error(message_json): @@ -1621,7 +1649,7 @@ def render_message(message_json, force=False): def process_message(message_json, cache=True): try: # send these subtype messages elsewhere - known_subtypes = ["message_changed", 'message_deleted', 'channel_join', 'channel_leave', 'channel_topic'] + known_subtypes = ["message_changed", 'message_deleted', 'channel_join', 'channel_leave', 'channel_topic', 'bot_enable', 'bot_disable'] if "subtype" in message_json and message_json["subtype"] in known_subtypes: proc[message_json["subtype"]](message_json) @@ -1791,7 +1819,11 @@ def get_user(message_json, server): if 'bot_id' in message_json and message_json['bot_id'] is not None: name = u"{} :]".format(server.bots.find(message_json["bot_id"]).formatted_name()) elif 'user' in message_json: - name = server.users.find(message_json['user']).name + u = server.users.find(message_json['user']) + if u.is_bot: + name = u"{} :]".format(u.formatted_name()) + else: + name = u.name elif 'username' in message_json: name = u"-{}-".format(message_json["username"]) elif 'service_name' in message_json: @@ -2121,7 +2153,7 @@ def create_slack_debug_buffer(): def config_changed_cb(data, option, value): global slack_api_token, distracting_channels, colorize_nicks, colorize_private_chats, slack_debug, debug_mode, \ - unfurl_ignore_alt_text + unfurl_ignore_alt_text, colorize_messages slack_api_token = w.config_get_plugin("slack_api_token") @@ -2130,6 +2162,7 @@ def config_changed_cb(data, option, value): distracting_channels = [x.strip() for x in w.config_get_plugin("distracting_channels").split(',')] colorize_nicks = w.config_get_plugin('colorize_nicks') == "1" + colorize_messages = w.config_get_plugin("colorize_messages") == "1" debug_mode = w.config_get_plugin("debug_mode").lower() if debug_mode != '' and debug_mode != 'false': create_slack_debug_buffer() @@ -2194,6 +2227,8 @@ if __name__ == "__main__": w.config_set_plugin('debug_mode', "") if not w.config_get_plugin('colorize_nicks'): w.config_set_plugin('colorize_nicks', "1") + if not w.config_get_plugin('colorize_messages'): + w.config_set_plugin('colorize_messages', "0") if not w.config_get_plugin('colorize_private_chats'): w.config_set_plugin('colorize_private_chats', "0") if not w.config_get_plugin('trigger_value'): |