-
Notifications
You must be signed in to change notification settings - Fork 2
/
colorize.py
147 lines (110 loc) · 4.91 KB
/
colorize.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
Colorize - nick and URL coloring for XChat/Hexchat
------------------------------------------------------------------------------
Copyright (c) 2013 Shaun Duncan and Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from __future__ import print_function
import re
import xchat
__module_name__ = 'colorize'
__module_author__ = 'Shaun Duncan'
__module_version__ = '0.5'
__module_description__ = 'Colorizes nicks and URLs in messages'
# Taken from source: src/common/text.c - rcolors
colors = [19, 20, 22, 24, 25, 26, 27, 28, 29]
url_pat = re.compile(r'(https?://[-A-Za-z0-9+&@#/%?=~_()|!:,.;\003\017]*[-A-Za-z0-9+&@#/%=~_|\003\017])', re.I)
nick_pat = re.compile(r'([a-zA-Z0-9\[\]\{\}\^\\\-_]+)')
def is_colorized(string):
"""
Checks to see if a string is already colorized, i.e. if 0x3 appears in the content
"""
return '\003' in string
def color_enabled():
"""
Simple wrapper to check pref setting for automatic nick coloring
"""
pref = xchat.get_prefs('text_color_nicks')
return pref is not None and pref == 1
def colorize_string(string, underline=False):
"""
Colorizes a string along the same lines as the source of xchat does.
It's a very simple approach: take the sum of char values of the input
string, mod the number of colors to use. Returns the original string
wrapped with escape sequences to colorize.
Should the user pass an empty string, the empty string is still returned,
but wrapped with no color
"""
if string == '':
ret = '\00300\017'
else:
idx = sum(ord(c) for c in string)
ret = '\003%s%s\017' % (colors[idx % len(colors)], string)
return '%s%s' % ('\037' if underline else '', ret)
# This isn't really necessary, but it looks nice
_color_mod_name = ''.join([colorize_string(c) for c in __module_name__])
def on_unload(data):
"""
Callback for module unload
"""
print('{0} plugin v{1} unloaded'.format(_color_mod_name, __module_version__))
def colorize_message(word, word_eol, userdata):
"""
Callback for message printing events. Scans contents of a message for both
URLs and nicks and colorizes them. This colorized output is sent along to the
client
"""
if len(word) < 2:
return xchat.EAT_NONE
message = word[1]
# This prevents recursive printing
if is_colorized(message):
return xchat.EAT_NONE
current_channel = xchat.get_info('channel')
# Private messages - the channel is the nick
if not current_channel.startswith('#'):
nicks = [current_channel, xchat.get_info('nick')]
else:
nicks = [u.nick for u in xchat.get_list('users')]
# Only perform nick coloring if the setting is enabled
if color_enabled():
output = []
# Split up the message delimited by a possible nick
for token in nick_pat.split(message):
if token in nicks:
token = colorize_string(token)
output.append(token)
output = ''.join(output)
else:
output = message
# Colorize URLs
for url in url_pat.findall(output):
output = output.replace(url, '\037\00328%s\017' % xchat.strip(url, len(url), 3))
# This is a bit of a hack where we tag the message with a non-printing
# color so we don't recurse forever
if not is_colorized(output):
output = '%s%s' % (output, colorize_string(''))
xchat.command('gui color 2')
xchat.emit_print(userdata, word[0], output)
return xchat.EAT_ALL
# Hook anything that prints a relevant message
xchat.hook_unload(on_unload)
xchat.hook_print('Channel Message', colorize_message, userdata='Channel Message')
xchat.hook_print('Your Message', colorize_message, userdata='Your Message')
xchat.hook_print('Private Message', colorize_message, userdata='Private Message')
xchat.hook_print('Private Message to Dialog', colorize_message, userdata='Private Message to Dialog')
print('{0} plugin v{1} loaded'.format(_color_mod_name, __module_version__))