Script: topicdiff_alt.py

Display topic with changes highlighted.
Author: Juerd — Version: 1.01 — License: Public-domain
For WeeChat ≥ 0.3.0, requires: python-diff-match-patch.
Tags: irc, topic, py2, py3
Added: 2018-09-21

Download GitHub Repository

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import weechat
import diff_match_patch
import re

weechat.register('topicdiff_alt', 'Juerd <#####@juerd.nl>', '1.01', 'PD', "Announce topic with changes highlighted", '', '')

def topic(data, tags, msg):
    server = tags.split(",")[0]

    match = re.search(r':(\S+)\s+TOPIC\s+(\S+)\s+:(.*)', msg)

    if not match:
        return weechat.WEECHAT_RC_ERROR

    usermask, channel, newtopic = match.groups()
    nick, host = usermask.split("!", 1)

    buffer = weechat.buffer_search("irc", server + "." + channel)
    weechat.prnt("", server + "." + channel)

    if not buffer:
        return weechat.WEECHAT_RC_ERROR

    oldtopic = weechat.buffer_get_string(buffer, "title")
    if oldtopic == None:
        oldtopic = ""

    dmp = diff_match_patch.diff_match_patch()
    diff = dmp.diff_main(oldtopic, newtopic)
    dmp.diff_cleanupEfficiency(diff)

    topic = ""

    color_reset = weechat.color("reset")
    color_ins = weechat.color(weechat.config_get_plugin("color_ins"))
    color_del = weechat.color(weechat.config_get_plugin("color_del"))

    for chunk in diff:
        changed, text = chunk

        topic += "%s%s%s" % (
            # 0 (unchanged), 1 (added), -1 (removed)
            ["", color_ins, color_del][changed],
            text,
            ["", color_reset, color_reset][changed]
        )

    weechat.prnt_date_tags(buffer, 0, "irc_topicdiff",
        "%s%s%s%s has changed topic for %s%s%s: %s" % (
        weechat.prefix("network"),
        weechat.color(weechat.info_get("irc_nick_color", nick)) \
            if weechat.config_boolean("irc.look.color_nicks_in_server_messages") \
            else weechat.color("chat_nick"),
        nick,
        color_reset,
        weechat.color("chat_channel"),
        channel,
        color_reset,
        topic
    ))

    return weechat.WEECHAT_RC_OK

weechat.hook_signal("*,irc_in_topic", "topic", "")

if not weechat.config_is_set_plugin("color_ins"):
    weechat.config_set_plugin("color_ins", "lightcyan")

if not weechat.config_is_set_plugin("color_del"):
    weechat.config_set_plugin("color_del", "darkgray")