File: topicdiff.py

package info (click to toggle)
weechat-scripts 20120603-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,804 kB
  • sloc: python: 23,181; perl: 16,968; ruby: 1,166; tcl: 199; sh: 8; makefile: 7
file content (99 lines) | stat: -rw-r--r-- 2,832 bytes parent folder | download | duplicates (3)
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
# Show differences between old and new topics
# Author: Dafydd Harries <daf@rhydd.org>
# License: GPL3

import re

import weechat

SCRIPT_NAME    = "topicdiff"
SCRIPT_AUTHOR  = "Dafydd Harries <daf@rhydd.org>"
SCRIPT_VERSION = "0.2"
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 = set(topic_chunks(topics[buffer]))
        new_chunks = set(topic_chunks(new_topic))
        removed_chunks = old_chunks - new_chunks
        added_chunks = new_chunks - old_chunks

        for chunk in removed_chunks:
            weechat.prnt(buffer, ' -: %s' % chunk)

        for chunk in added_chunks:
            weechat.prnt(buffer, ' +: %s' % 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()