forked from seveas/hacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhilight.py
65 lines (59 loc) · 2.5 KB
/
hilight.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
# Sends highlights as a /query to yourself so they are all available in one tab
# (c)2010 Dennis Kaarsemaker <[email protected]>
# Licence: MIT
import fnmatch
import string
import xchat
__module_name__ = "hilight"
__module_version__ = "0.2"
__module_description__ = "Log highlighting messages to a special tab"
rfc2812_tolower = string.maketrans('[]\\~','{}|^')
irc_lower = lambda txt: txt.lower().translate(rfc2812_tolower)
hilight_query_name = u'Hilights'
def on_msg(word, word_eol, userdata):
sender=word[0][1:]
recipient=word[2]
message=word_eol[3][1:]
if not is_highlight(sender, recipient, message):
return xchat.EAT_NONE
ctx = xchat.find_context(server=xchat.get_info('server'),channel=hilight_query_name)
if not ctx:
# Open a query if it isn't there yet
xchat.command('query -nofocus %s' % hilight_query_name)
ctx = xchat.find_context(server=xchat.get_info('server'),channel=hilight_query_name)
if message[0] == message[-1] and message[0] == '\x01':
# CTCP. Only honor CTCP action aka /me
if message[1:7].lower() != 'action':
return xchat.EAT_NONE
ctx.emit_print('Channel Action Hilight', '%s/%s' % (sender[:sender.find('!')], recipient), message[8:-1], '')
else:
ctx.emit_print('Channel Msg Hilight', '%s/%s' % (sender[:sender.find('!')], recipient), message, '')
return xchat.EAT_NONE
xchat.hook_server("PRIVMSG", on_msg)
def is_highlight(sender, recipient, message):
"""Are we being highlighted?"""
message = irc_lower(message)
sender = irc_lower(sender)
# Only highlight channels
if not recipient[0] in '#&@':
return False
# Nicks to never highlight
nnh = irc_lower(xchat.get_prefs('irc_no_hilight') or '')
if match_word(sender[:sender.find('!')], nnh.split(',')):
return False
# Nicks to always highlight
nth = irc_lower(xchat.get_prefs('irc_nick_hilight') or '')
if match_word(sender[:sender.find('!')], nth.split(',')):
return True
# Words to highlight on, masks allowed
wth = [irc_lower(xchat.get_prefs('irc_nick%d' % x) or '') for x in (1,2,3)] + [irc_lower(xchat.get_info('nick'))]
wth += irc_lower(xchat.get_prefs('irc_extra_hilight') or '').split(', ')
for w in wth:
w = w.strip()
if w and w in message:
return True
return False
def match_word(needle, haystack):
# Evil. Use fnmatch.
haystack = [x.strip() for x in haystack if x.strip()]
return bool(fnmatch.filter(haystack, needle))