Verified Commit 2452706b authored by mathieui's avatar mathieui

Add a deterministic_nick_colors option (default: true)

parent 030b4d7b
......@@ -371,6 +371,9 @@ vertical_tab_list_sort = desc
# possible values: desc, asc
user_list_sort = desc
# If the MUC nicks should receive a fixed color based on their text or not
deterministic_nick_colors = true
# The nick of people who join, part, change their status, etc. in a MUC will
# be displayed using their nick color if true.
display_user_color_in_join_part = true
......
......@@ -519,6 +519,15 @@ or the way messages are displayed.
If set to ``desc``, the MUC users will be displayed from top to bottom in the list,
if set to ``asc``, they will be displayed from bottom to top.
deterministic_nick_colors
**Default value:** ``true``
Use a deterministic algorithm to choose the user colors in chatrooms if
set to ``true``. Otherwise the colors will be picked randomly.
The value of this option affects the behavior of :term:`/recolor`.
vertical_tab_list_size
**Default value:** ``20``
......
......@@ -42,6 +42,7 @@ DEFAULT_CONFIG = {
'custom_host': '',
'custom_port': '',
'default_nick': '',
'deterministic_nick_colors': True,
'display_activity_notifications': False,
'display_gaming_notifications': False,
'display_mood_notifications': False,
......
......@@ -401,6 +401,19 @@ class MucTab(ChatTab):
/recolor [random]
Re-assign color to the participants of the room
"""
deterministic = config.get_by_tabname('deterministic_nick_colors', self.name)
if deterministic:
for user in self.users:
if user.nick == self.own_nick:
continue
user.set_deterministic_color()
if args[0] == 'random':
self.core.information(_('"random" was provided, but poezio is '
'configured to use deterministic colors'),
'Warning')
self.user_win.refresh(self.users)
self.input.refresh()
return
compare_users = lambda x: x.last_talked
users = list(self.users)
sorted_users = sorted(users, key=compare_users, reverse=True)
......@@ -995,12 +1008,13 @@ class MucTab(ChatTab):
role = presence['muc']['role']
jid = presence['muc']['jid']
typ = presence['type']
deterministic = config.get_by_tabname('deterministic_nick_colors', self.name)
if not self.joined: # user in the room BEFORE us.
# ignore redondant presence message, see bug #1509
if (from_nick not in [user.nick for user in self.users]
and typ != "unavailable"):
new_user = User(from_nick, affiliation, show,
status, role, jid)
status, role, jid, deterministic)
self.users.append(new_user)
self.core.events.trigger('muc_join', presence, self)
if '110' in status_codes or self.own_nick == from_nick:
......@@ -1136,8 +1150,9 @@ class MucTab(ChatTab):
"""
When a new user joins the groupchat
"""
deterministic = config.get_by_tabname('deterministic_nick_colors', self.name)
user = User(from_nick, affiliation,
show, status, role, jid)
show, status, role, jid, deterministic)
self.users.append(user)
hide_exit_join = config.get_by_tabname('hide_exit_join',
self.general_jid)
......
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment