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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
/*
* irc-color.c - IRC color decoding/encoding in messages
*
* Copyright (C) 2003-2013 Sebastien 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 <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "../weechat-plugin.h"
#include "irc.h"
#include "irc-color.h"
#include "irc-config.h"
char *irc_color_to_weechat[IRC_NUM_COLORS] =
{ /* 0 */ "white",
/* 1 */ "black",
/* 2 */ "blue",
/* 3 */ "green",
/* 4 */ "lightred",
/* 5 */ "red",
/* 6 */ "magenta",
/* 7 */ "brown",
/* 8 */ "yellow",
/* 9 */ "lightgreen",
/* 10 */ "cyan",
/* 11 */ "lightcyan",
/* 12 */ "lightblue",
/* 13 */ "lightmagenta",
/* 14 */ "gray",
/* 15 */ "white"
};
/*
* Replaces IRC colors by WeeChat colors.
*
* If keep_colors == 0: removes any color/style in message otherwise keeps
* colors.
*
* Note: result must be freed after use.
*/
char *
irc_color_decode (const char *string, int keep_colors)
{
unsigned char *out, *out2, *ptr_string;
int out_length, length, out_pos, length_to_add;
char str_fg[3], str_bg[3], str_color[128], str_key[128], str_to_add[128];
const char *remapped_color;
int fg, bg, bold, reverse, italic, underline, rc;
/*
* create output string with size of length*2 (with min 128 bytes),
* this string will be realloc() later with a larger size if needed
*/
out_length = (strlen (string) * 2) + 1;
if (out_length < 128)
out_length = 128;
out = malloc (out_length);
if (!out)
return NULL;
/* initialize attributes */
bold = 0;
reverse = 0;
italic = 0;
underline = 0;
ptr_string = (unsigned char *)string;
out[0] = '\0';
out_pos = 0;
while (ptr_string && ptr_string[0])
{
str_to_add[0] = '\0';
switch (ptr_string[0])
{
case IRC_COLOR_BOLD_CHAR:
if (keep_colors)
{
snprintf (str_to_add, sizeof (str_to_add), "%s",
weechat_color ((bold) ? "-bold" : "bold"));
}
bold ^= 1;
ptr_string++;
break;
case IRC_COLOR_RESET_CHAR:
if (keep_colors)
{
snprintf (str_to_add, sizeof (str_to_add), "%s",
weechat_color ("reset"));
}
bold = 0;
reverse = 0;
italic = 0;
underline = 0;
ptr_string++;
break;
case IRC_COLOR_FIXED_CHAR:
ptr_string++;
break;
case IRC_COLOR_REVERSE_CHAR:
case IRC_COLOR_REVERSE2_CHAR:
if (keep_colors)
{
snprintf (str_to_add, sizeof (str_to_add), "%s",
weechat_color ((reverse) ? "-reverse" : "reverse"));
}
reverse ^= 1;
ptr_string++;
break;
case IRC_COLOR_ITALIC_CHAR:
if (keep_colors)
{
snprintf (str_to_add, sizeof (str_to_add), "%s",
weechat_color ((italic) ? "-italic" : "italic"));
}
italic ^= 1;
ptr_string++;
break;
case IRC_COLOR_UNDERLINE_CHAR:
if (keep_colors)
{
snprintf (str_to_add, sizeof (str_to_add), "%s",
weechat_color ((underline) ? "-underline" : "underline"));
}
underline ^= 1;
ptr_string++;
break;
case IRC_COLOR_COLOR_CHAR:
ptr_string++;
str_fg[0] = '\0';
str_bg[0] = '\0';
if (isdigit (ptr_string[0]))
{
str_fg[0] = ptr_string[0];
str_fg[1] = '\0';
ptr_string++;
if (isdigit (ptr_string[0]))
{
str_fg[1] = ptr_string[0];
str_fg[2] = '\0';
ptr_string++;
}
}
if ((ptr_string[0] == ',') && (isdigit (ptr_string[1])))
{
ptr_string++;
str_bg[0] = ptr_string[0];
str_bg[1] = '\0';
ptr_string++;
if (isdigit (ptr_string[0]))
{
str_bg[1] = ptr_string[0];
str_bg[2] = '\0';
ptr_string++;
}
}
if (keep_colors)
{
if (str_fg[0] || str_bg[0])
{
fg = -1;
bg = -1;
if (str_fg[0])
{
rc = sscanf (str_fg, "%d", &fg);
if ((rc != EOF) && (rc >= 1))
{
fg %= IRC_NUM_COLORS;
}
}
if (str_bg[0])
{
rc = sscanf (str_bg, "%d", &bg);
if ((rc != EOF) && (rc >= 1))
{
bg %= IRC_NUM_COLORS;
}
}
/* search "fg,bg" in hashtable of remapped colors */
snprintf (str_key, sizeof (str_key), "%d,%d", fg, bg);
remapped_color = weechat_hashtable_get (
irc_config_hashtable_color_mirc_remap,
str_key);
if (remapped_color)
{
snprintf (str_color, sizeof (str_color),
"|%s", remapped_color);
}
else
{
snprintf (str_color, sizeof (str_color),
"|%s%s%s",
(fg >= 0) ? irc_color_to_weechat[fg] : "",
(bg >= 0) ? "," : "",
(bg >= 0) ? irc_color_to_weechat[bg] : "");
}
snprintf (str_to_add, sizeof (str_to_add), "%s",
weechat_color (str_color));
}
else
{
snprintf (str_to_add, sizeof (str_to_add), "%s",
weechat_color ("resetcolor"));
}
}
break;
default:
/*
* we are not on an IRC color code, just copy the UTF-8 char
* into "str_to_add"
*/
length = weechat_utf8_char_size ((char *)ptr_string);
if (length == 0)
length = 1;
memcpy (str_to_add, ptr_string, length);
str_to_add[length] = '\0';
ptr_string += length;
break;
}
/* add "str_to_add" (if not empty) to "out" */
if (str_to_add[0])
{
/* if "out" is too small for adding "str_to_add", do a realloc() */
length_to_add = strlen (str_to_add);
if (out_pos + length_to_add + 1 > out_length)
{
/* try to double the size of "out" */
out_length *= 2;
out2 = realloc (out, out_length);
if (!out2)
return (char *)out;
out = out2;
}
/* add "str_to_add" to "out" */
memcpy (out + out_pos, str_to_add, length_to_add + 1);
out_pos += length_to_add;
}
}
return (char *)out;
}
/*
* Replaces IRC color codes by codes for command line.
*
* Note: result must be freed after use.
*/
char *
irc_color_decode_for_user_entry (const char *string)
{
unsigned char *out, *ptr_string;
int out_length, out_pos, length;
out_length = (strlen (string) * 2) + 1;
out = malloc (out_length);
if (!out)
return NULL;
ptr_string = (unsigned char *)string;
out_pos = 0;
while (ptr_string && ptr_string[0] && (out_pos < out_length - 1))
{
switch (ptr_string[0])
{
case IRC_COLOR_BOLD_CHAR:
out[out_pos++] = 0x02;
ptr_string++;
break;
case IRC_COLOR_FIXED_CHAR:
ptr_string++;
break;
case IRC_COLOR_RESET_CHAR:
out[out_pos++] = 0x0F;
ptr_string++;
break;
case IRC_COLOR_REVERSE_CHAR:
case IRC_COLOR_REVERSE2_CHAR:
out[out_pos++] = 0x12;
ptr_string++;
break;
case IRC_COLOR_ITALIC_CHAR:
out[out_pos++] = 0x1D;
ptr_string++;
break;
case IRC_COLOR_UNDERLINE_CHAR:
out[out_pos++] = 0x15;
ptr_string++;
break;
case IRC_COLOR_COLOR_CHAR:
out[out_pos++] = 0x03;
ptr_string++;
break;
default:
length = weechat_utf8_char_size ((char *)ptr_string);
if (length == 0)
length = 1;
memcpy (out + out_pos, ptr_string, length);
out_pos += length;
ptr_string += length;
}
}
out[out_pos] = '\0';
return (char *)out;
}
/*
* Replaces color codes in command line by IRC color codes.
*
* If keep_colors == 0, remove any color/style in message, otherwise keeps
* colors.
*
* Note: result must be freed after use.
*/
char *
irc_color_encode (const char *string, int keep_colors)
{
unsigned char *out, *ptr_string;
int out_length, out_pos, length;
out_length = (strlen (string) * 2) + 1;
out = malloc (out_length);
if (!out)
return NULL;
ptr_string = (unsigned char *)string;
out_pos = 0;
while (ptr_string && ptr_string[0] && (out_pos < out_length - 1))
{
switch (ptr_string[0])
{
case 0x02: /* ^B */
if (keep_colors)
out[out_pos++] = IRC_COLOR_BOLD_CHAR;
ptr_string++;
break;
case 0x03: /* ^C */
if (keep_colors)
out[out_pos++] = IRC_COLOR_COLOR_CHAR;
ptr_string++;
if (isdigit (ptr_string[0]))
{
if (keep_colors)
out[out_pos++] = ptr_string[0];
ptr_string++;
if (isdigit (ptr_string[0]))
{
if (keep_colors)
out[out_pos++] = ptr_string[0];
ptr_string++;
}
}
if (ptr_string[0] == ',')
{
if (keep_colors)
out[out_pos++] = ',';
ptr_string++;
if (isdigit (ptr_string[0]))
{
if (keep_colors)
out[out_pos++] = ptr_string[0];
ptr_string++;
if (isdigit (ptr_string[0]))
{
if (keep_colors)
out[out_pos++] = ptr_string[0];
ptr_string++;
}
}
}
break;
case 0x0F: /* ^O */
if (keep_colors)
out[out_pos++] = IRC_COLOR_RESET_CHAR;
ptr_string++;
break;
case 0x12: /* ^R */
if (keep_colors)
out[out_pos++] = IRC_COLOR_REVERSE_CHAR;
ptr_string++;
break;
case 0x15: /* ^U */
if (keep_colors)
out[out_pos++] = IRC_COLOR_UNDERLINE_CHAR;
ptr_string++;
break;
default:
length = weechat_utf8_char_size ((char *)ptr_string);
if (length == 0)
length = 1;
memcpy (out + out_pos, ptr_string, length);
out_pos += length;
ptr_string += length;
}
}
out[out_pos] = '\0';
return (char *)out;
}
/*
* Callback for modifiers "irc_color_decode" and "irc_color_encode".
*
* This modifier can be used by other plugins to decode/encode IRC colors in
* messages.
*/
char *
irc_color_modifier_cb (void *data, const char *modifier,
const char *modifier_data, const char *string)
{
int keep_colors;
/* make C compiler happy */
(void) data;
keep_colors = (modifier_data && (strcmp (modifier_data, "1") == 0)) ? 1 : 0;
if (strcmp (modifier, "irc_color_decode") == 0)
return irc_color_decode (string, keep_colors);
if (strcmp (modifier, "irc_color_encode") == 0)
return irc_color_encode (string, keep_colors);
/* unknown modifier */
return NULL;
}
/*
* Returns color name for tags (replace "," by ":").
*
* Note: result must be freed after use.
*/
char *
irc_color_for_tags (const char *color)
{
if (!color)
return NULL;
return weechat_string_replace (color, ",", ":");
}
|