Created
May 23, 2017 05:28
-
-
Save nolanlum/4f388973a9f105b2a4df65872516c460 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf8 | |
import xchat, hashlib, json, os, math | |
__module_name__ = "split nicks" | |
__module_version__ = "1.0" | |
__module_description__ = "split nick colouration" | |
__module_author__ = "nattofriends" | |
nicks = {} | |
colors = [19, 20, 22, 24, 26, 27, 28, 29]; # xchat colors, then doubled to be length = 16 | |
SELF = False | |
def get_setting(setting): | |
settings = get_settings_dict() | |
if settings is not None and setting in settings: | |
return settings[setting] | |
return None | |
def get_settings_dict(): | |
if not os.path.exists("split_color_prefs"): | |
return None | |
with open("split_color_prefs") as p: | |
try: | |
settings = json.load(p) | |
except ValueError: | |
print "[split_color] Settings file was corrupt." | |
settings = {} | |
return settings | |
def put_setting(setting, value): | |
settings = get_settings_dict() | |
if settings is None: | |
settings = {} | |
settings[setting] = value | |
with open("split_color_prefs", "w") as p: | |
try: | |
json.dump(settings, p) | |
except ValueError: | |
print "[split_color] Error writing to settings file." | |
def recolor(nick): | |
global nicks | |
rainbow = get_setting("rainbow") | |
rainbow = 2 if rainbow is None else rainbow | |
if nick in nicks: | |
hash = nicks[nick] | |
else: | |
hash = hashlib.md5(nick).digest() | |
nicks[nick] = hash | |
rv = "" | |
if rainbow < 0: | |
rainbow = -rainbow | |
hash = hash[:len(nick) / rainbow] | |
nick = gather(nick, rainbow) | |
else: | |
hash = hash[:rainbow] | |
nick = gather(nick, int(math.ceil(len(nick) * 1.0 / rainbow))) | |
hash = map(lambda el: ord(el) >> 5, hash) | |
mash = ["\x03{}{}".format(colors[color], l) for color, l in zip(hash, nick)] | |
rv = "".join(mash) + "\x03" | |
return rv | |
def gather (what, bunches): | |
rv = [] | |
while len(what) > 0: | |
these = min(bunches, len(what)) | |
rv.append(what[:bunches]) | |
what = what[bunches:] | |
return rv | |
def as_kwargs(attributes): | |
attribute_names = ['time'] | |
kwargs = {attr: getattr(attributes, attr) for attr in attribute_names if getattr(attributes, attr, None)} | |
return kwargs | |
def recolor_msg(word, word_eol, userdata, attributes): | |
global SELF | |
nick, text = word[0:2] | |
nick = xchat.strip(nick) | |
if SELF: | |
SELF = False | |
return xchat.EAT_NONE | |
colored = recolor(nick) | |
SELF = True | |
xchat.emit_print(userdata, colored, text, word[2] if len(word) > 2 else "", word[3] if len(word) > 3 else "", **as_kwargs(attributes)) | |
#xchat.command("gui color 2") | |
return xchat.EAT_XCHAT | |
def recolor_nickchange(word, word_eol, userdata, attributes): | |
global SELF | |
nicks = word | |
nicks = map(xchat.strip, nicks) | |
nicks = map(recolor, nicks) | |
if SELF: | |
SELF = False | |
return xchat.EAT_NONE | |
SELF = True | |
xchat.emit_print(userdata, *nicks, **as_kwargs(attributes)) | |
return xchat.EAT_XCHAT | |
def rainbow(word, word_eol, userdata): | |
err = False | |
if len(word) != 2: | |
# err = True | |
print "[split_color] Current preference:", get_setting("rainbow") | |
return | |
try: | |
value = int(word[1]) | |
except ValueError: | |
err = True | |
# if value not in [0, 1]: | |
# err = True | |
if err: | |
print "[split_color] usage: /tester <num>; 0 = binary split, anything else = x-long colors" | |
return | |
rv = put_setting("rainbow", value) | |
print "[split_color] Preference saved", value | |
return | |
xchat.hook_print_attrs("Channel Message", recolor_msg, userdata = "Channel Message") | |
xchat.hook_print_attrs("Private Message to Dialog", recolor_msg, userdata = "Private Message to Dialog") | |
xchat.hook_print_attrs("Change Nick", recolor_nickchange, userdata = "Change Nick") | |
xchat.hook_print_attrs("Your Nick Changing", recolor_nickchange, userdata = "Your Nick Changing") | |
xchat.hook_command("rainbow", rainbow) | |
print "\x03split_color plugin loaded\x03" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment