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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
|
/*
* irc-color.c - IRC color decoding/encoding in messages
*
* Copyright (C) 2003-2022 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/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <regex.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 */ "darkgray",
/* 15 */ "gray",
/* 16-23 */ "52", "94", "100", "58", "22", "29", "23", "24",
/* 24-31 */ "17", "54", "53", "89", "88", "130", "142", "64",
/* 32-39 */ "28", "35", "30", "25", "18", "91", "90", "125",
/* 40-47 */ "124", "166", "184", "106", "34", "49", "37", "33",
/* 48-55 */ "19", "129", "127", "161", "196", "208", "226", "154",
/* 56-63 */ "46", "86", "51", "75", "21", "171", "201", "198",
/* 64-71 */ "203", "215", "227", "191", "83", "122", "87", "111",
/* 72-79 */ "63", "177", "207", "205", "217", "223", "229", "193",
/* 80-87 */ "157", "158", "159", "153", "147", "183", "219", "212",
/* 88-95 */ "16", "233", "235", "237", "239", "241", "244", "247",
/* 96-98 */ "250", "254", "231",
/* 99 */ "default",
};
char irc_color_term2irc[IRC_COLOR_TERM2IRC_NUM_COLORS] =
{ /* term > IRC */
1, /* 0 1 (black) */
5, /* 1 5 (red) */
3, /* 2 3 (green) */
7, /* 3 7 (brown) */
2, /* 4 2 (blue) */
6, /* 5 6 (magenta) */
10, /* 6 10 (cyan) */
15, /* 7 15 (gray) */
14, /* 8 14 (darkgray) */
4, /* 9 4 (lightred) */
9, /* 10 9 (lightgreen) */
8, /* 11 8 (yellow) */
12, /* 12 12 (lightblue) */
13, /* 13 13 (lightmagenta) */
11, /* 14 11 (lightcyan) */
0, /* 15 0 (white) */
};
regex_t *irc_color_regex_ansi = NULL;
/*
* 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)
{
char **out;
char str_fg[3], str_bg[3], str_color[128], str_key[128], str_to_add[128];
const char *remapped_color;
unsigned char *ptr_string;
int length, fg, bg, bold, reverse, italic, underline, rc;
if (!string)
return NULL;
length = strlen (string);
out = weechat_string_dyn_alloc (length + (length / 2) + 1);
if (!out)
return NULL;
/* initialize attributes */
bold = 0;
reverse = 0;
italic = 0;
underline = 0;
ptr_string = (unsigned char *)string;
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:
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]))
{
/* foreground */
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])))
{
/* background */
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])
weechat_string_dyn_concat (out, str_to_add, -1);
}
return weechat_string_dyn_free (out, 0);
}
/*
* 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)
{
char **out;
unsigned char *ptr_string;
int length;
if (!string)
return NULL;
length = strlen (string);
out = weechat_string_dyn_alloc (length + (length / 2) + 1);
if (!out)
return NULL;
ptr_string = (unsigned char *)string;
while (ptr_string && ptr_string[0])
{
switch (ptr_string[0])
{
case 0x02: /* ^B */
if (keep_colors)
weechat_string_dyn_concat (out, IRC_COLOR_BOLD_STR, -1);
ptr_string++;
break;
case 0x03: /* ^C */
if (keep_colors)
weechat_string_dyn_concat (out, IRC_COLOR_COLOR_STR, -1);
ptr_string++;
if (isdigit (ptr_string[0]))
{
/* foreground */
if (keep_colors)
{
weechat_string_dyn_concat (out,
(const char *)ptr_string,
1);
}
ptr_string++;
if (isdigit (ptr_string[0]))
{
if (keep_colors)
{
weechat_string_dyn_concat (
out,
(const char *)ptr_string,
1);
}
ptr_string++;
}
}
if (ptr_string[0] == ',')
{
/* background */
if (keep_colors)
weechat_string_dyn_concat (out, ",", -1);
ptr_string++;
if (isdigit (ptr_string[0]))
{
if (keep_colors)
{
weechat_string_dyn_concat (
out,
(const char *)ptr_string,
1);
}
ptr_string++;
if (isdigit (ptr_string[0]))
{
if (keep_colors)
{
weechat_string_dyn_concat (
out,
(const char *)ptr_string,
1);
}
ptr_string++;
}
}
}
break;
case 0x0F: /* ^O */
if (keep_colors)
weechat_string_dyn_concat (out, IRC_COLOR_RESET_STR, -1);
ptr_string++;
break;
case 0x16: /* ^V */
if (keep_colors)
weechat_string_dyn_concat (out, IRC_COLOR_REVERSE_STR, -1);
ptr_string++;
break;
case 0x1D: /* ^] */
if (keep_colors)
weechat_string_dyn_concat (out, IRC_COLOR_ITALIC_STR, -1);
ptr_string++;
break;
case 0x1F: /* ^_ */
if (keep_colors)
weechat_string_dyn_concat (out, IRC_COLOR_UNDERLINE_STR, -1);
ptr_string++;
break;
default:
length = weechat_utf8_char_size ((char *)ptr_string);
if (length == 0)
length = 1;
weechat_string_dyn_concat (out,
(const char *)ptr_string,
length);
ptr_string += length;
}
}
return weechat_string_dyn_free (out, 0);
}
/*
* Converts a RGB color to IRC color.
*
* Returns a IRC color number (between 0 and 15), -1 if error.
*/
int
irc_color_convert_rgb2irc (int rgb)
{
char str_color[64], *error, *info_color;
long number;
snprintf (str_color, sizeof (str_color),
"%d,%d",
rgb,
IRC_COLOR_TERM2IRC_NUM_COLORS);
info_color = weechat_info_get ("color_rgb2term", str_color);
if (!info_color || !info_color[0])
{
if (info_color)
free (info_color);
return -1;
}
error = NULL;
number = strtol (info_color, &error, 10);
if (!error || error[0]
|| (number < 0) || (number >= IRC_COLOR_TERM2IRC_NUM_COLORS))
{
free (info_color);
return -1;
}
free (info_color);
return irc_color_term2irc[number];
}
/*
* Converts a terminal color to IRC color.
*
* Returns a IRC color number (between 0 and 15), -1 if error.
*/
int
irc_color_convert_term2irc (int color)
{
char str_color[64], *error, *info_color;
long number;
snprintf (str_color, sizeof (str_color), "%d", color);
info_color = weechat_info_get ("color_term2rgb", str_color);
if (!info_color || !info_color[0])
{
if (info_color)
free (info_color);
return -1;
}
error = NULL;
number = strtol (info_color, &error, 10);
if (!error || error[0] || (number < 0) || (number > 0xFFFFFF))
{
free (info_color);
return -1;
}
free (info_color);
return irc_color_convert_rgb2irc (number);
}
/*
* Replaces ANSI colors by IRC colors (or removes them).
*
* This callback is called by irc_color_decode_ansi, it must not be called
* directly.
*/
char *
irc_color_decode_ansi_cb (void *data, const char *text)
{
struct t_irc_color_ansi_state *ansi_state;
char *text2, **items, *output, str_color[128];
int i, length, num_items, value, value2, color;
ansi_state = (struct t_irc_color_ansi_state *)data;
/* if we don't keep colors or if text is empty, just return empty string */
if (!ansi_state->keep_colors || !text || !text[0])
return strdup ("");
/* only sequences ending with 'm' are used, the others are discarded */
length = strlen (text);
if (text[length - 1] != 'm')
return strdup ("");
/* sequence "\33[m" resets color */
if (length < 4)
return strdup (IRC_COLOR_RESET_STR);
text2 = NULL;
items = NULL;
output = NULL;
/* extract text between "\33[" and "m" */
text2 = weechat_strndup (text + 2, length - 3);
if (!text2)
goto end;
items = weechat_string_split (text2, ";", NULL,
WEECHAT_STRING_SPLIT_STRIP_LEFT
| WEECHAT_STRING_SPLIT_STRIP_RIGHT
| WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
0, &num_items);
if (!items)
goto end;
output = malloc ((32 * num_items) + 1);
if (!output)
goto end;
output[0] = '\0';
for (i = 0; i < num_items; i++)
{
value = atoi (items[i]);
switch (value)
{
case 0: /* reset */
strcat (output, IRC_COLOR_RESET_STR);
ansi_state->bold = 0;
ansi_state->underline = 0;
ansi_state->italic = 0;
break;
case 1: /* bold */
if (!ansi_state->bold)
{
strcat (output, IRC_COLOR_BOLD_STR);
ansi_state->bold = 1;
}
break;
case 2: /* remove bold */
case 21:
case 22:
if (ansi_state->bold)
{
strcat (output, IRC_COLOR_BOLD_STR);
ansi_state->bold = 0;
}
break;
case 3: /* italic */
if (!ansi_state->italic)
{
strcat (output, IRC_COLOR_ITALIC_STR);
ansi_state->italic = 1;
}
break;
case 4: /* underline */
if (!ansi_state->underline)
{
strcat (output, IRC_COLOR_UNDERLINE_STR);
ansi_state->underline = 1;
}
break;
case 23: /* remove italic */
if (ansi_state->italic)
{
strcat (output, IRC_COLOR_ITALIC_STR);
ansi_state->italic = 0;
}
break;
case 24: /* remove underline */
if (ansi_state->underline)
{
strcat (output, IRC_COLOR_UNDERLINE_STR);
ansi_state->underline = 0;
}
break;
case 30: /* text color */
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
snprintf (str_color, sizeof (str_color),
"%c%02d",
IRC_COLOR_COLOR_CHAR,
irc_color_term2irc[value - 30]);
strcat (output, str_color);
break;
case 38: /* text color */
if (i + 1 < num_items)
{
value2 = atoi (items[i + 1]);
if (value2 == 2)
{
/* RGB color */
if (i + 4 < num_items)
{
color = irc_color_convert_rgb2irc (
(atoi (items[i + 2]) << 16) |
(atoi (items[i + 3]) << 8) |
atoi (items[i + 4]));
if (color >= 0)
{
snprintf (str_color, sizeof (str_color),
"%c%02d",
IRC_COLOR_COLOR_CHAR,
color);
strcat (output, str_color);
}
i += 4;
}
}
else if (value2 == 5)
{
/* terminal color (0-255) */
if (i + 2 < num_items)
{
color = irc_color_convert_term2irc (atoi (items[i + 2]));
if (color >= 0)
{
snprintf (str_color, sizeof (str_color),
"%c%02d",
IRC_COLOR_COLOR_CHAR,
color);
strcat (output, str_color);
}
i += 2;
}
}
}
break;
case 39: /* default text color */
snprintf (str_color, sizeof (str_color),
"%c15",
IRC_COLOR_COLOR_CHAR);
strcat (output, str_color);
break;
case 40: /* background color */
case 41:
case 42:
case 43:
case 44:
case 45:
case 46:
case 47:
snprintf (str_color, sizeof (str_color),
"%c,%02d",
IRC_COLOR_COLOR_CHAR,
irc_color_term2irc[value - 40]);
strcat (output, str_color);
break;
case 48: /* background color */
if (i + 1 < num_items)
{
value2 = atoi (items[i + 1]);
if (value2 == 2)
{
/* RGB color */
if (i + 4 < num_items)
{
color = irc_color_convert_rgb2irc (
(atoi (items[i + 2]) << 16) |
(atoi (items[i + 3]) << 8) |
atoi (items[i + 4]));
if (color >= 0)
{
snprintf (str_color, sizeof (str_color),
"%c,%02d",
IRC_COLOR_COLOR_CHAR,
color);
strcat (output, str_color);
}
i += 4;
}
}
else if (value2 == 5)
{
/* terminal color (0-255) */
if (i + 2 < num_items)
{
color = irc_color_convert_term2irc (atoi (items[i + 2]));
if (color >= 0)
{
snprintf (str_color, sizeof (str_color),
"%c,%02d",
IRC_COLOR_COLOR_CHAR,
color);
strcat (output, str_color);
}
i += 2;
}
}
}
break;
case 49: /* default background color */
snprintf (str_color, sizeof (str_color),
"%c,01",
IRC_COLOR_COLOR_CHAR);
strcat (output, str_color);
break;
case 90: /* text color (bright) */
case 91:
case 92:
case 93:
case 94:
case 95:
case 96:
case 97:
snprintf (str_color, sizeof (str_color),
"%c%02d",
IRC_COLOR_COLOR_CHAR,
irc_color_term2irc[value - 90 + 8]);
strcat (output, str_color);
break;
case 100: /* background color (bright) */
case 101:
case 102:
case 103:
case 104:
case 105:
case 106:
case 107:
snprintf (str_color, sizeof (str_color),
"%c,%02d",
IRC_COLOR_COLOR_CHAR,
irc_color_term2irc[value - 100 + 8]);
strcat (output, str_color);
break;
}
}
end:
if (items)
weechat_string_free_split (items);
if (text2)
free (text2);
return (output) ? output : strdup ("");
}
/*
* Replaces ANSI colors by IRC 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_ansi (const char *string, int keep_colors)
{
struct t_irc_color_ansi_state ansi_state;
char *ansi_regex;
/* allocate/compile regex if needed (first call) */
if (!irc_color_regex_ansi)
{
irc_color_regex_ansi = malloc (sizeof (*irc_color_regex_ansi));
if (!irc_color_regex_ansi)
return NULL;
ansi_regex = weechat_info_get ("color_ansi_regex", NULL);
if (weechat_string_regcomp (irc_color_regex_ansi,
ansi_regex,
REG_EXTENDED) != 0)
{
if (ansi_regex)
free (ansi_regex);
free (irc_color_regex_ansi);
irc_color_regex_ansi = NULL;
return NULL;
}
if (ansi_regex)
free (ansi_regex);
}
ansi_state.keep_colors = keep_colors;
ansi_state.bold = 0;
ansi_state.underline = 0;
ansi_state.italic = 0;
return weechat_string_replace_regex (string, irc_color_regex_ansi,
"$0", '$',
&irc_color_decode_ansi_cb,
&ansi_state);
}
/*
* Callback for modifiers "irc_color_decode", "irc_color_encode" and
* "irc_color_decode_ansi".
*
* These modifiers can be used by other plugins to decode/encode IRC/ANSI
* colors in messages.
*/
char *
irc_color_modifier_cb (const void *pointer, void *data,
const char *modifier, const char *modifier_data,
const char *string)
{
int keep_colors;
/* make C compiler happy */
(void) pointer;
(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);
if (strcmp (modifier, "irc_color_decode_ansi") == 0)
return irc_color_decode_ansi (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, ",", ":");
}
/*
* Adds mapping between IRC color codes and WeeChat color names in an infolist.
*
* Returns:
* 1: OK
* 0: error
*/
int
irc_color_weechat_add_to_infolist (struct t_infolist *infolist)
{
struct t_infolist_item *ptr_item;
char str_color_irc[32];
int i;
if (!infolist)
return 0;
for (i = 0; i < IRC_NUM_COLORS; i++)
{
ptr_item = weechat_infolist_new_item (infolist);
if (!ptr_item)
return 0;
snprintf (str_color_irc, sizeof (str_color_irc), "%02d", i);
if (!weechat_infolist_new_var_string (ptr_item, "color_irc", str_color_irc))
return 0;
if (!weechat_infolist_new_var_string (ptr_item, "color_weechat", irc_color_to_weechat[i]))
return 0;
}
return 1;
}
/*
* Ends IRC colors.
*/
void
irc_color_end ()
{
if (irc_color_regex_ansi)
{
regfree (irc_color_regex_ansi);
free (irc_color_regex_ansi);
irc_color_regex_ansi = NULL;
}
}
|