From 2452706b909364178655c6c918a0348fb4298fb2 Mon Sep 17 00:00:00 2001 From: mathieui Date: Sat, 20 Dec 2014 23:13:26 +0100 Subject: Add a deterministic_nick_colors option (default: true) --- src/user.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/user.py') diff --git a/src/user.py b/src/user.py index 0d29569f..8b4ad94b 100644 --- a/src/user.py +++ b/src/user.py @@ -12,6 +12,7 @@ An user is a MUC participant, not a roster contact (see contact.py) from random import choice from datetime import timedelta, datetime +from hashlib import md5 from theming import get_theme @@ -27,14 +28,23 @@ class User(object): """ keep trace of an user in a Room """ - def __init__(self, nick, affiliation, show, status, role, jid): + def __init__(self, nick, affiliation, show, status, role, jid, deterministic=True): self.last_talked = datetime(1, 1, 1) # The oldest possible time self.update(affiliation, show, status, role) self.change_nick(nick) - self.color = choice(get_theme().LIST_COLOR_NICKNAMES) + if deterministic: + self.set_deterministic_color() + else: + self.color = choice(get_theme().LIST_COLOR_NICKNAMES) self.jid = jid self.chatstate = None + def set_deterministic_color(self): + theme = get_theme() + mod = len(theme.LIST_COLOR_NICKNAMES) + nick_pos = int(md5(self.nick.encode('utf-8')).hexdigest(), 16) % mod + self.color = theme.LIST_COLOR_NICKNAMES[nick_pos] + def update(self, affiliation, show, status, role): self.affiliation = affiliation self.show = show -- cgit v1.2.3