summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--data/default_config.cfg3
-rw-r--r--doc/source/configuration.rst9
-rw-r--r--src/config.py1
-rw-r--r--src/tabs/muctab.py19
-rw-r--r--src/user.py14
5 files changed, 42 insertions, 4 deletions
diff --git a/data/default_config.cfg b/data/default_config.cfg
index 36ca7858..95ae1333 100644
--- a/data/default_config.cfg
+++ b/data/default_config.cfg
@@ -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
diff --git a/doc/source/configuration.rst b/doc/source/configuration.rst
index b7099020..d3d4fb43 100644
--- a/doc/source/configuration.rst
+++ b/doc/source/configuration.rst
@@ -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``
diff --git a/src/config.py b/src/config.py
index 86aae8ef..339e8a85 100644
--- a/src/config.py
+++ b/src/config.py
@@ -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,
diff --git a/src/tabs/muctab.py b/src/tabs/muctab.py
index 369ec3e5..72478bd4 100644
--- a/src/tabs/muctab.py
+++ b/src/tabs/muctab.py
@@ -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)
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