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
|
/*
* Copyright (C) 2003-2011 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/>.
*/
/*
* gui-chat.c: chat functions (used by all GUI)
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <time.h>
#include <regex.h>
#include "../core/weechat.h"
#include "../core/wee-config.h"
#include "../core/wee-hook.h"
#include "../core/wee-string.h"
#include "../core/wee-utf8.h"
#include "../plugins/plugin.h"
#include "gui-chat.h"
#include "gui-buffer.h"
#include "gui-color.h"
#include "gui-filter.h"
#include "gui-line.h"
#include "gui-main.h"
#include "gui-window.h"
char *gui_chat_prefix[GUI_CHAT_NUM_PREFIXES]; /* prefixes */
char gui_chat_prefix_empty[] = ""; /* empty prefix */
int gui_chat_time_length = 0; /* length of time for each line (in chars) */
int gui_chat_mute = GUI_CHAT_MUTE_DISABLED; /* mute mode */
struct t_gui_buffer *gui_chat_mute_buffer = NULL; /* mute buffer */
int gui_chat_display_tags = 0; /* display tags? */
/*
* gui_chat_init: init some variables for chat area
* (called before reading WeeChat config file)
*/
void
gui_chat_init ()
{
/* build empty prefixes */
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR] = strdup (gui_chat_prefix_empty);
gui_chat_prefix[GUI_CHAT_PREFIX_NETWORK] = strdup (gui_chat_prefix_empty);
gui_chat_prefix[GUI_CHAT_PREFIX_ACTION] = strdup (gui_chat_prefix_empty);
gui_chat_prefix[GUI_CHAT_PREFIX_JOIN] = strdup (gui_chat_prefix_empty);
gui_chat_prefix[GUI_CHAT_PREFIX_QUIT] = strdup (gui_chat_prefix_empty);
}
/*
* gui_chat_prefix_build: build prefix with colors
* (called after reading WeeChat config file)
*/
void
gui_chat_prefix_build ()
{
char prefix[128];
int i;
for (i = 0; i < GUI_CHAT_NUM_PREFIXES; i++)
{
if (gui_chat_prefix[i])
{
free (gui_chat_prefix[i]);
gui_chat_prefix[i] = NULL;
}
}
snprintf (prefix, sizeof (prefix), "%s%s\t",
GUI_COLOR(GUI_COLOR_CHAT_PREFIX_ERROR),
CONFIG_STRING(config_look_prefix[GUI_CHAT_PREFIX_ERROR]));
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR] = strdup (prefix);
snprintf (prefix, sizeof (prefix), "%s%s\t",
GUI_COLOR(GUI_COLOR_CHAT_PREFIX_NETWORK),
CONFIG_STRING(config_look_prefix[GUI_CHAT_PREFIX_NETWORK]));
gui_chat_prefix[GUI_CHAT_PREFIX_NETWORK] = strdup (prefix);
snprintf (prefix, sizeof (prefix), "%s%s\t",
GUI_COLOR(GUI_COLOR_CHAT_PREFIX_ACTION),
CONFIG_STRING(config_look_prefix[GUI_CHAT_PREFIX_ACTION]));
gui_chat_prefix[GUI_CHAT_PREFIX_ACTION] = strdup (prefix);
snprintf (prefix, sizeof (prefix), "%s%s\t",
GUI_COLOR(GUI_COLOR_CHAT_PREFIX_JOIN),
CONFIG_STRING(config_look_prefix[GUI_CHAT_PREFIX_JOIN]));
gui_chat_prefix[GUI_CHAT_PREFIX_JOIN] = strdup (prefix);
snprintf (prefix, sizeof (prefix), "%s%s\t",
GUI_COLOR(GUI_COLOR_CHAT_PREFIX_QUIT),
CONFIG_STRING(config_look_prefix[GUI_CHAT_PREFIX_QUIT]));
gui_chat_prefix[GUI_CHAT_PREFIX_QUIT] = strdup (prefix);
}
/*
* gui_chat_utf_char_valid: return 1 if utf char is valid for screen
* otherwise return 0
*/
int
gui_chat_utf_char_valid (const char *utf_char)
{
/* chars below 32 are not valid */
if ((unsigned char)utf_char[0] < 32)
return 0;
/* 146 or 0x7F are not valid */
if ((((unsigned char)(utf_char[0]) == 146)
|| ((unsigned char)(utf_char[0]) == 0x7F))
&& (!utf_char[1]))
return 0;
/* any other char is valid */
return 1;
}
/*
* gui_chat_strlen_screen: returns number of char needed on sreen to display a
* word special chars like color, bold, .. are ignored
*/
int
gui_chat_strlen_screen (const char *string)
{
int length, size_on_screen;
length = 0;
while (string && string[0])
{
string = gui_chat_string_next_char (NULL, (unsigned char *)string, 0);
if (string)
{
size_on_screen = (gui_chat_utf_char_valid (string)) ? utf8_char_size_screen (string) : 1;
if (size_on_screen > 0)
length += size_on_screen;
string = utf8_next_char (string);
}
}
return length;
}
/*
* gui_chat_string_add_offset: move forward N chars in a string, skipping all
* formatting chars (like colors,..)
*/
char *
gui_chat_string_add_offset (const char *string, int offset)
{
while (string && string[0] && (offset > 0))
{
string = gui_chat_string_next_char (NULL,
(unsigned char *)string,
0);
if (string)
{
string = utf8_next_char (string);
offset--;
}
}
return (char *)string;
}
/*
* gui_chat_string_real_pos: get real position in string
* (ignoring color/bold/.. chars)
*/
int
gui_chat_string_real_pos (const char *string, int pos)
{
const char *real_pos, *ptr_string;
int size_on_screen;
if (pos <= 0)
return 0;
real_pos = string;
ptr_string = string;
while (ptr_string && ptr_string[0] && (pos > 0))
{
ptr_string = gui_chat_string_next_char (NULL,
(unsigned char *)ptr_string,
0);
if (ptr_string)
{
size_on_screen = (((unsigned char)ptr_string[0]) < 32) ? 1 : utf8_char_size_screen (ptr_string);
if (size_on_screen > 0)
pos -= size_on_screen;
ptr_string = utf8_next_char (ptr_string);
real_pos = ptr_string;
}
}
return 0 + (real_pos - string);
}
/*
* gui_chat_get_word_info: returns info about next word: beginning, end, length
*/
void
gui_chat_get_word_info (struct t_gui_window *window,
const char *data,
int *word_start_offset, int *word_end_offset,
int *word_length_with_spaces, int *word_length)
{
const char *start_data;
char *next_char, *next_char2;
int leading_spaces, char_size;
*word_start_offset = 0;
*word_end_offset = 0;
*word_length_with_spaces = 0;
*word_length = 0;
start_data = data;
leading_spaces = 1;
while (data && data[0])
{
next_char = gui_chat_string_next_char (window, (unsigned char *)data,
0);
if (next_char)
{
next_char2 = utf8_next_char (next_char);
if (next_char2)
{
if (next_char[0] != ' ')
{
if (leading_spaces)
*word_start_offset = next_char - start_data;
leading_spaces = 0;
char_size = next_char2 - next_char;
*word_end_offset = next_char2 - start_data - 1;
(*word_length_with_spaces) += char_size;
(*word_length) += char_size;
}
else
{
if (leading_spaces)
(*word_length_with_spaces)++;
else
{
*word_end_offset = next_char - start_data - 1;
return;
}
}
data = next_char2;
}
}
else
{
*word_end_offset = data + strlen (data) - start_data - 1;
return;
}
}
}
/*
* gu_chat_get_time_string: get time string, for display (with colors)
*/
char *
gui_chat_get_time_string (time_t date)
{
char text_time[128], text_time2[(128*3)+16], text_time_char[2];
char *text_with_color;
int i, time_first_digit, time_last_digit, last_color;
struct tm *local_time;
if (!CONFIG_STRING(config_look_buffer_time_format)
|| !CONFIG_STRING(config_look_buffer_time_format)[0])
return NULL;
local_time = localtime (&date);
if (strftime (text_time, sizeof (text_time),
CONFIG_STRING(config_look_buffer_time_format),
local_time) == 0)
return NULL;
if (strstr (text_time, "${"))
{
text_with_color = gui_color_string_replace_colors (text_time);
if (text_with_color)
{
if (strcmp (text_time, text_with_color) != 0)
return text_with_color;
free (text_with_color);
}
}
time_first_digit = -1;
time_last_digit = -1;
i = 0;
while (text_time[i])
{
if (isdigit (text_time[i]))
{
if (time_first_digit == -1)
time_first_digit = i;
time_last_digit = i;
}
i++;
}
text_time2[0] = '\0';
text_time_char[1] = '\0';
last_color = -1;
i = 0;
while (text_time[i])
{
text_time_char[0] = text_time[i];
if (time_first_digit < 0)
{
if (last_color != GUI_COLOR_CHAT_TIME)
{
strcat (text_time2, GUI_COLOR(GUI_COLOR_CHAT_TIME));
last_color = GUI_COLOR_CHAT_TIME;
}
strcat (text_time2, text_time_char);
}
else
{
if ((i < time_first_digit) || (i > time_last_digit))
{
if (last_color != GUI_COLOR_CHAT_DELIMITERS)
{
strcat (text_time2, GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS));
last_color = GUI_COLOR_CHAT_DELIMITERS;
}
strcat (text_time2, text_time_char);
}
else
{
if (isdigit (text_time[i]))
{
if (last_color != GUI_COLOR_CHAT_TIME)
{
strcat (text_time2, GUI_COLOR(GUI_COLOR_CHAT_TIME));
last_color = GUI_COLOR_CHAT_TIME;
}
strcat (text_time2, text_time_char);
}
else
{
if (last_color != GUI_COLOR_CHAT_TIME_DELIMITERS)
{
strcat (text_time2,
GUI_COLOR(GUI_COLOR_CHAT_TIME_DELIMITERS));
last_color = GUI_COLOR_CHAT_TIME_DELIMITERS;
}
strcat (text_time2, text_time_char);
}
}
}
i++;
}
return strdup (text_time2);
}
/*
* gui_chat_get_time_length: calculates time length with a time format
* (format can include color codes with format ${name})
*/
int
gui_chat_get_time_length ()
{
time_t date;
char *text_time;
int length;
if (!CONFIG_STRING(config_look_buffer_time_format)
|| !CONFIG_STRING(config_look_buffer_time_format)[0])
return 0;
length = 0;
date = time (NULL);
text_time = gui_chat_get_time_string (date);
if (text_time)
{
length = gui_chat_strlen_screen (text_time);
free (text_time);
}
return length;
}
/*
* gui_chat_change_time_format: change time format for all lines of all buffers
*/
void
gui_chat_change_time_format ()
{
struct t_gui_buffer *ptr_buffer;
struct t_gui_line *ptr_line;
for (ptr_buffer = gui_buffers; ptr_buffer;
ptr_buffer = ptr_buffer->next_buffer)
{
for (ptr_line = ptr_buffer->lines->first_line; ptr_line;
ptr_line = ptr_line->next_line)
{
if (ptr_line->data->date != 0)
{
if (ptr_line->data->str_time)
free (ptr_line->data->str_time);
ptr_line->data->str_time = gui_chat_get_time_string (ptr_line->data->date);
}
}
}
}
/*
* gui_chat_build_string_prefix_message: build a string with prefix and message
*/
char *
gui_chat_build_string_prefix_message (struct t_gui_line *line)
{
char *string, *string_without_colors;
int length;
length = 0;
if (line->data->prefix)
length += strlen (line->data->prefix);
length++;
if (line->data->message)
length += strlen (line->data->message);
length++;
string = malloc (length);
if (string)
{
string[0] = '\0';
if (line->data->prefix)
strcat (string, line->data->prefix);
strcat (string, "\t");
if (line->data->message)
strcat (string, line->data->message);
}
if (string)
{
string_without_colors = gui_color_decode (string, NULL);
if (string_without_colors)
{
free (string);
string = string_without_colors;
}
}
return string;
}
/*
* gui_chat_build_string_prefix_message: build a string with message and tags
*/
char *
gui_chat_build_string_message_tags (struct t_gui_line *line)
{
int i, length;
char *buf;
length = 64 + 2;
if (line->data->message)
length += strlen (line->data->message);
for (i = 0; i < line->data->tags_count; i++)
{
length += strlen (line->data->tags_array[i]) + 1;
}
length += 2;
buf = malloc (length);
buf[0] = '\0';
if (line->data->message)
strcat (buf, line->data->message);
strcat (buf, GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS));
strcat (buf, " [");
strcat (buf, GUI_COLOR(GUI_COLOR_CHAT_TAGS));
for (i = 0; i < line->data->tags_count; i++)
{
strcat (buf, line->data->tags_array[i]);
if (i < line->data->tags_count - 1)
strcat (buf, ",");
}
strcat (buf, GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS));
strcat (buf, "]");
return buf;
}
/*
* gui_chat_printf_date_tags: display a message in a buffer with optional
* date and tags
* Info: this function works only with formatted
* buffers (not buffers with free content)
*/
void
gui_chat_printf_date_tags (struct t_gui_buffer *buffer, time_t date,
const char *tags, const char *message, ...)
{
va_list argptr;
time_t date_printed;
int display_time, length, at_least_one_message_printed;
char strbuf[8192], *pos, *pos_prefix, *pos_tab, *pos_end;
char *modifier_data, *new_msg, *ptr_msg;
struct t_gui_line *ptr_line;
if (!gui_buffer_valid (buffer))
return;
if (!message)
return;
if (gui_init_ok)
{
if (!buffer)
buffer = gui_buffer_search_main ();
if (!buffer)
return;
if (buffer->type != GUI_BUFFER_TYPE_FORMATTED)
buffer = gui_buffers;
if (buffer->type != GUI_BUFFER_TYPE_FORMATTED)
return;
}
/* if mute is enabled for buffer (or all buffers), then just return */
if ((gui_chat_mute == GUI_CHAT_MUTE_ALL_BUFFERS)
|| ((gui_chat_mute == GUI_CHAT_MUTE_BUFFER)
&& (gui_chat_mute_buffer == buffer)))
return;
va_start (argptr, message);
vsnprintf (strbuf, sizeof (strbuf) - 1, message, argptr);
va_end (argptr);
utf8_normalize (strbuf, '?');
date_printed = time (NULL);
if (date <= 0)
date = date_printed;
at_least_one_message_printed = 0;
pos = strbuf;
while (pos)
{
/* display until next end of line */
pos_end = strchr (pos, '\n');
if (pos_end)
pos_end[0] = '\0';
/* call modifier for message printed ("weechat_print") */
new_msg = NULL;
if (buffer)
{
length = strlen (plugin_get_name (buffer->plugin)) + 1 +
strlen (buffer->name) + 1 + ((tags) ? strlen (tags) : 0) + 1;
modifier_data = malloc (length);
if (modifier_data)
{
snprintf (modifier_data, length, "%s;%s;%s",
plugin_get_name (buffer->plugin),
buffer->name,
(tags) ? tags : "");
new_msg = hook_modifier_exec (NULL,
"weechat_print",
modifier_data,
pos);
free (modifier_data);
if (new_msg)
{
if (!new_msg[0] && pos[0])
{
/*
* modifier returned empty message, then we'll not
* print anything
*/
free (new_msg);
return;
}
if (strcmp (message, new_msg) == 0)
{
/* no changes in new message */
free (new_msg);
new_msg = NULL;
}
}
}
}
pos_prefix = NULL;
display_time = 1;
ptr_msg = (new_msg) ? new_msg : pos;
/* space followed by tab => prefix ignored */
if ((ptr_msg[0] == ' ') && (ptr_msg[1] == '\t'))
{
ptr_msg += 2;
}
else
{
/* if two first chars are tab, then do not display time */
if ((ptr_msg[0] == '\t') && (ptr_msg[1] == '\t'))
{
display_time = 0;
ptr_msg += 2;
}
else
{
/* if tab found, use prefix (before tab) */
pos_tab = strchr (ptr_msg, '\t');
if (pos_tab)
{
pos_tab[0] = '\0';
pos_prefix = ptr_msg;
ptr_msg = pos_tab + 1;
}
}
}
if (gui_init_ok)
{
ptr_line = gui_line_add (buffer, (display_time) ? date : 0,
(display_time) ? date_printed : 0,
tags, pos_prefix, ptr_msg);
if (ptr_line)
{
if (buffer && buffer->print_hooks_enabled)
hook_print_exec (buffer, ptr_line);
if (ptr_line->data->displayed)
at_least_one_message_printed = 1;
}
}
else
{
if (pos_prefix)
string_iconv_fprintf (stdout, "%s ", pos_prefix);
string_iconv_fprintf (stdout, "%s\n", ptr_msg);
}
if (new_msg)
free (new_msg);
pos = (pos_end && pos_end[1]) ? pos_end + 1 : NULL;
}
if (gui_init_ok && at_least_one_message_printed)
gui_buffer_ask_chat_refresh (buffer, 1);
}
/*
* gui_chat_printf_y: display a message on a line in a buffer with free content
* Info: this function works only with free content
* buffers (not formatted buffers)
*/
void
gui_chat_printf_y (struct t_gui_buffer *buffer, int y, const char *message, ...)
{
va_list argptr;
struct t_gui_line *ptr_line;
char strbuf[8192];
int i, num_lines_to_add;
if (gui_init_ok)
{
if (!buffer)
buffer = gui_buffer_search_main ();
if (buffer->type != GUI_BUFFER_TYPE_FREE)
buffer = gui_buffers;
if (buffer->type != GUI_BUFFER_TYPE_FREE)
return;
}
va_start (argptr, message);
vsnprintf (strbuf, sizeof (strbuf) - 1, message, argptr);
va_end (argptr);
utf8_normalize (strbuf, '?');
/* no message: delete line */
if (!strbuf[0])
{
if (gui_init_ok)
{
for (ptr_line = buffer->own_lines->first_line; ptr_line;
ptr_line = ptr_line->next_line)
{
if (ptr_line->data->y >= y)
break;
}
if (ptr_line && (ptr_line->data->y == y))
{
if (ptr_line->next_line)
gui_line_clear (ptr_line);
else
gui_line_free (buffer, ptr_line);
gui_buffer_ask_chat_refresh (buffer, 2);
}
}
}
else
{
if (gui_init_ok)
{
num_lines_to_add = 0;
if (buffer->own_lines && buffer->own_lines->last_line)
num_lines_to_add = y - buffer->own_lines->last_line->data->y - 1;
else
num_lines_to_add = y;
if (num_lines_to_add > 0)
{
/*
* add empty line(s) before asked line, to ensure there is at
* least "y" lines in buffer, and then be able to scroll
* properly buffer page by page
*/
for (i = y - num_lines_to_add; i < y; i++)
{
gui_line_add_y (buffer, i, "");
}
}
gui_line_add_y (buffer, y, strbuf);
gui_buffer_ask_chat_refresh (buffer, 1);
}
else
string_iconv_fprintf (stdout, "%s\n", strbuf);
}
}
/*
* gui_chat_end: free some variables allocated for chat area
*/
void
gui_chat_end ()
{
int i;
/* free prefixes */
for (i = 0; i < GUI_CHAT_NUM_PREFIXES; i++)
{
if (gui_chat_prefix[i])
{
free (gui_chat_prefix[i]);
gui_chat_prefix[i] = NULL;
}
}
}
|