Script: topicdiff.py

Show differences between old and new topics.
Author: daf — Version: 0.4 — License: GPL-3.0-or-later
For WeeChat ≥ 0.3.0.
Tags: irc, topic, py2, py3
Added: 2011-08-28 — Updated: 2019-09-22

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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
# Show differences between old and new topics
# Author: Dafydd Harries <daf@rhydd.org>
# License: GPL3

import re

try:
    from itertools import zip_longest
except ImportError:
    from itertools import izip_longest as zip_longest

import weechat

SCRIPT_NAME    = "topicdiff"
SCRIPT_AUTHOR  = "Dafydd Harries <daf@rhydd.org>"
SCRIPT_VERSION = "0.4"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC    = "Show differences between old and new topics."

pending_change_buffer = None
topics = {}

def topic_chunks(s):
    return re.split(r'\s+[-~|]+\s+', s)

def topic_changed(buffer, new_topic):
    if buffer in topics:
        old_chunks = topic_chunks(topics[buffer])
        new_chunks = topic_chunks(new_topic)

        for old_chunk, new_chunk in zip_longest(old_chunks, new_chunks):
            if old_chunk and old_chunk not in new_chunks:
                weechat.prnt(buffer, '%s-\t%s' % (
                    weechat.color('red'),
                    old_chunk
                ))

            if new_chunk and new_chunk not in old_chunks:
                weechat.prnt(buffer, '%s+\t%s' % (
                    weechat.color('green'),
                    new_chunk
                ))

    topics[buffer] = new_topic

def print_332(data, buffer, time, tags, displayed, highlight, prefix, message):
    #weechat.prnt('', 'print: %r' % (a,))
    global pending_change_buffer
    pending_change_buffer = buffer
    return weechat.WEECHAT_RC_OK

def print_topic(
        data, buffer, time, tags, displayed, highlight, prefix, message):
    global pending_change_buffer
    pending_change_buffer = buffer
    return weechat.WEECHAT_RC_OK

def irc_in2_332(data, tags, msg):
    global pending_change_buffer

    if pending_change_buffer is None:
        # Hmm, that's weird.
        #weechat.prnt('', 'no pending buffer :(')
        return weechat.WEECHAT_RC_OK

    match = re.match(r':\S+ 332 \S+ \S+ :(.*)', msg)

    if not match:
        #weechat.prnt('', 'no match :(')
        return weechat.WEECHAT_RC_OK

    new_topic = match.group(1)
    topic_changed(pending_change_buffer, new_topic)
    pending_change_buffer = None
    return weechat.WEECHAT_RC_OK

def irc_in2_topic(data, tags, msg):
    global pending_change_buffer

    #weechat.prnt('', '%r' % ((tags, msg),))

    if pending_change_buffer is None:
        # Hmm, that's weird.
        #weechat.prnt('', 'no pending buffer :(')
        return weechat.WEECHAT_RC_OK

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

    if not match:
        #weechat.prnt('', 'no match :(')
        return weechat.WEECHAT_RC_OK

    new_topic = match.group(1)
    topic_changed(pending_change_buffer, new_topic)
    pending_change_buffer = None
    return weechat.WEECHAT_RC_OK

def register():
    weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION,
        SCRIPT_LICENSE, SCRIPT_DESC, '', '')

    weechat.hook_print('', 'irc_332', '', 1, 'print_332', '')
    weechat.hook_print('', 'irc_topic', '', 1, 'print_topic', '')
    weechat.hook_signal('*,irc_in2_332', 'irc_in2_332', '')
    weechat.hook_signal('*,irc_in2_topic', 'irc_in2_topic', '')

if __name__ == '__main__':
    register()