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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/*
* gui-nick.c - nick functions (used by all GUI)
*
* Copyright (C) 2003-2020 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WeeChat. If not, see <https://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "../core/weechat.h"
#include "../core/wee-config.h"
#include "../core/wee-hashtable.h"
#include "../core/wee-string.h"
#include "../core/wee-utf8.h"
#include "gui-nick.h"
#include "gui-color.h"
/*
* Hashes a string with a variant of djb2 hash, using 64-bit integer.
*
* Number pointed by *color_64 is updated by the function.
*/
void
gui_nick_hash_djb2_64 (const char *nickname, uint64_t *color_64)
{
while (nickname && nickname[0])
{
*color_64 ^= (*color_64 << 5) + (*color_64 >> 2) +
utf8_char_int (nickname);
nickname = utf8_next_char (nickname);
}
}
/*
* Hashes a string with a variant of djb2 hash, using 32-bit integer.
*
* Number pointed by *color_32 is updated by the function.
*/
void
gui_nick_hash_djb2_32 (const char *nickname, uint32_t *color_32)
{
while (nickname && nickname[0])
{
*color_32 ^= (*color_32 << 5) + (*color_32 >> 2) +
utf8_char_int (nickname);
nickname = utf8_next_char (nickname);
}
}
/*
* Hashes a string with sum of letters, using 64-bit integer.
*
* Number pointed by *color_64 is updated by the function.
*/
void
gui_nick_hash_sum_64 (const char *nickname, uint64_t *color_64)
{
while (nickname && nickname[0])
{
*color_64 += utf8_char_int (nickname);
nickname = utf8_next_char (nickname);
}
}
/*
* Hashes a string with sum of letters, using 32-bit integer.
*
* Number pointed by *color_32 is updated by the function.
*/
void
gui_nick_hash_sum_32 (const char *nickname, uint32_t *color_32)
{
while (nickname && nickname[0])
{
*color_32 += utf8_char_int (nickname);
nickname = utf8_next_char (nickname);
}
}
/*
* Hashes a nickname to find color.
*
* Returns a number which is the index of color in the nicks colors of option
* "weechat.color.chat_nick_colors".
*/
int
gui_nick_hash_color (const char *nickname)
{
const char *ptr_salt;
uint64_t color_64;
uint32_t color_32;
if (!nickname || !nickname[0])
return 0;
if (!config_nick_colors)
config_set_nick_colors ();
if (config_num_nick_colors == 0)
return 0;
ptr_salt = CONFIG_STRING(config_look_nick_color_hash_salt);
color_64 = 0;
switch (CONFIG_INTEGER(config_look_nick_color_hash))
{
case CONFIG_LOOK_NICK_COLOR_HASH_DJB2:
/* variant of djb2 hash */
color_64 = 5381;
gui_nick_hash_djb2_64 (ptr_salt, &color_64);
gui_nick_hash_djb2_64 (nickname, &color_64);
break;
case CONFIG_LOOK_NICK_COLOR_HASH_SUM:
/* sum of letters */
color_64 = 0;
gui_nick_hash_sum_64 (ptr_salt, &color_64);
gui_nick_hash_sum_64 (nickname, &color_64);
break;
case CONFIG_LOOK_NICK_COLOR_HASH_DJB2_32:
/* variant of djb2 hash (using 32-bit integer) */
color_32 = 5381;
gui_nick_hash_djb2_32 (ptr_salt, &color_32);
gui_nick_hash_djb2_32 (nickname, &color_32);
color_64 = color_32;
break;
case CONFIG_LOOK_NICK_COLOR_HASH_SUM_32:
/* sum of letters (using 32-bit integer) */
color_32 = 0;
gui_nick_hash_sum_32 (ptr_salt, &color_32);
gui_nick_hash_sum_32 (nickname, &color_32);
color_64 = color_32;
break;
}
return (color_64 % config_num_nick_colors);
}
/*
* Gets forced color for a nick.
*
* Returns the name of color (for example: "green"), NULL if no color is forced
* for nick.
*/
const char *
gui_nick_get_forced_color (const char *nickname)
{
const char *forced_color;
char *nick_lower;
if (!nickname || !nickname[0])
return NULL;
forced_color = hashtable_get (config_hashtable_nick_color_force, nickname);
if (forced_color)
return forced_color;
nick_lower = strdup (nickname);
if (nick_lower)
{
string_tolower (nick_lower);
forced_color = hashtable_get (config_hashtable_nick_color_force,
nick_lower);
free (nick_lower);
}
return forced_color;
}
/*
* Duplicates a nick and stops at first char in list (using option
* weechat.look.nick_color_stop_chars).
*
* Note: result must be freed after use.
*/
char *
gui_nick_strdup_for_color (const char *nickname)
{
int char_size, other_char_seen;
char *result, *pos, utf_char[16];
if (!nickname)
return NULL;
result = malloc (strlen (nickname) + 1);
pos = result;
other_char_seen = 0;
while (nickname[0])
{
char_size = utf8_char_size (nickname);
memcpy (utf_char, nickname, char_size);
utf_char[char_size] = '\0';
if (strstr (CONFIG_STRING(config_look_nick_color_stop_chars),
utf_char))
{
if (other_char_seen)
{
pos[0] = '\0';
return result;
}
}
else
{
other_char_seen = 1;
}
memcpy (pos, utf_char, char_size);
pos += char_size;
nickname += char_size;
}
pos[0] = '\0';
return result;
}
/*
* Finds a color code for a nick (according to nick letters).
*
* Returns a WeeChat color code (that can be used for display).
*/
const char *
gui_nick_find_color (const char *nickname)
{
int color;
char *nickname2;
const char *forced_color, *str_color;
if (!nickname || !nickname[0])
return gui_color_get_custom ("default");
if (!config_nick_colors)
config_set_nick_colors ();
if (config_num_nick_colors == 0)
return gui_color_get_custom ("default");
nickname2 = gui_nick_strdup_for_color (nickname);
/* look if color is forced */
forced_color = gui_nick_get_forced_color (
(nickname2) ? nickname2 : nickname);
if (forced_color)
{
forced_color = gui_color_get_custom (forced_color);
if (forced_color && forced_color[0])
{
if (nickname2)
free (nickname2);
return forced_color;
}
}
/* hash nickname to get color */
color = gui_nick_hash_color ((nickname2) ? nickname2 : nickname);
if (nickname2)
free (nickname2);
/* return color */
str_color = gui_color_get_custom (config_nick_colors[color]);
return (str_color[0]) ? str_color : gui_color_get_custom ("default");
}
/*
* Finds a color name for a nick (according to nick letters).
*
* Returns the name of a color (for example: "green").
*/
const char *
gui_nick_find_color_name (const char *nickname)
{
int color;
char *nickname2;
const char *forced_color;
static char *default_color = "default";
if (!nickname || !nickname[0])
return default_color;
if (!config_nick_colors)
config_set_nick_colors ();
if (config_num_nick_colors == 0)
return default_color;
nickname2 = gui_nick_strdup_for_color (nickname);
/* look if color is forced */
forced_color = gui_nick_get_forced_color (
(nickname2) ? nickname2 : nickname);
if (forced_color)
{
if (nickname2)
free (nickname2);
return forced_color;
}
/* hash nickname to get color */
color = gui_nick_hash_color ((nickname2) ? nickname2 : nickname);
if (nickname2)
free (nickname2);
/* return color name */
return config_nick_colors[color];
}
|