4 Copyright (C) 1999-2000 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "module-formats.h"
27 #include "lib-config/iconfig.h"
34 #include "hilight-text.h"
35 #include "nickmatch-cache.h"
36 #include "printtext.h"
39 static NICKMATCH_REC *nickmatch;
40 static HILIGHT_REC *next_nick_hilight, *next_line_hilight;
41 static int next_hilight_start, next_hilight_end;
42 static int never_hilight_level, default_hilight_level;
45 static void reset_cache(void)
49 never_hilight_level = MSGLEVEL_ALL & ~default_hilight_level;
50 for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
51 HILIGHT_REC *rec = tmp->data;
53 if (never_hilight_level & rec->level)
54 never_hilight_level &= ~rec->level;
57 nickmatch_rebuild(nickmatch);
60 static void hilight_add_config(HILIGHT_REC *rec)
64 g_return_if_fail(rec != NULL);
66 node = iconfig_node_traverse("(hilights", TRUE);
67 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
69 iconfig_node_set_str(node, "text", rec->text);
70 if (rec->level > 0) iconfig_node_set_int(node, "level", rec->level);
71 if (rec->color) iconfig_node_set_str(node, "color", rec->color);
72 if (rec->act_color) iconfig_node_set_str(node, "act_color", rec->act_color);
73 if (rec->priority > 0) iconfig_node_set_int(node, "priority", rec->priority);
74 iconfig_node_set_bool(node, "nick", rec->nick);
75 iconfig_node_set_bool(node, "word", rec->word);
76 if (rec->nickmask) iconfig_node_set_bool(node, "mask", TRUE);
77 if (rec->fullword) iconfig_node_set_bool(node, "fullword", TRUE);
78 if (rec->regexp) iconfig_node_set_bool(node, "regexp", TRUE);
80 if (rec->channels != NULL && *rec->channels != NULL) {
81 node = config_node_section(node, "channels", NODE_TYPE_LIST);
82 iconfig_node_add_list(node, rec->channels);
86 static void hilight_remove_config(HILIGHT_REC *rec)
90 g_return_if_fail(rec != NULL);
92 node = iconfig_node_traverse("hilights", FALSE);
93 if (node != NULL) iconfig_node_list_remove(node, g_slist_index(hilights, rec));
96 static void hilight_destroy(HILIGHT_REC *rec)
98 g_return_if_fail(rec != NULL);
101 if (rec->regexp_compiled) regfree(&rec->preg);
103 if (rec->channels != NULL) g_strfreev(rec->channels);
104 g_free_not_null(rec->color);
105 g_free_not_null(rec->act_color);
110 static void hilights_destroy_all(void)
112 g_slist_foreach(hilights, (GFunc) hilight_destroy, NULL);
113 g_slist_free(hilights);
117 static void hilight_remove(HILIGHT_REC *rec)
119 g_return_if_fail(rec != NULL);
121 hilight_remove_config(rec);
122 hilights = g_slist_remove(hilights, rec);
123 hilight_destroy(rec);
126 static HILIGHT_REC *hilight_find(const char *text, char **channels)
131 g_return_val_if_fail(text != NULL, NULL);
133 for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
134 HILIGHT_REC *rec = tmp->data;
136 if (g_strcasecmp(rec->text, text) != 0)
139 if ((channels == NULL && rec->channels == NULL))
140 return rec; /* no channels - ok */
142 if (channels != NULL && strcmp(*channels, "*") == 0)
143 return rec; /* ignore channels */
145 if (channels == NULL || rec->channels == NULL)
146 continue; /* other doesn't have channels */
148 if (strarray_length(channels) != strarray_length(rec->channels))
149 continue; /* different amount of channels */
151 /* check that channels match */
152 for (chan = channels; *chan != NULL; chan++) {
153 if (strarray_find(rec->channels, *chan) == -1)
158 return rec; /* channels ok */
164 static int hilight_match_text(HILIGHT_REC *rec, const char *text,
165 int *match_beg, int *match_end)
171 regmatch_t rmatch[1];
173 if (rec->regexp_compiled &&
174 regexec(&rec->preg, text, 1, rmatch, 0) == 0) {
175 if (rmatch[0].rm_so > 0 &&
176 match_beg != NULL && match_end != NULL) {
177 *match_beg = rmatch[0].rm_so;
178 *match_end = rmatch[0].rm_eo;
184 match = rec->fullword ?
185 stristr_full(text, rec->text) :
186 stristr(text, rec->text);
188 if (match_beg != NULL && match_end != NULL) {
189 *match_beg = (int) (match-text);
190 *match_end = *match_beg + strlen(rec->text);
199 #define hilight_match_level(rec, level) \
200 (level & (((rec)->level != 0 ? rec->level : default_hilight_level)))
202 #define hilight_match_channel(rec, channel) \
203 ((rec)->channels == NULL || ((channel) != NULL && \
204 strarray_find((rec)->channels, (channel)) != -1))
206 HILIGHT_REC *hilight_match(SERVER_REC *server, const char *channel,
207 const char *nick, const char *address,
208 int level, const char *str,
209 int *match_beg, int *match_end)
212 CHANNEL_REC *chanrec;
215 g_return_val_if_fail(str != NULL, NULL);
217 if ((never_hilight_level & level) == level)
221 /* check nick mask hilights */
222 chanrec = channel_find(server, channel);
223 nickrec = chanrec == NULL ? NULL :
224 nicklist_find(chanrec, nick);
225 if (nickrec != NULL) {
228 if (nickrec->host == NULL)
229 nicklist_set_host(chanrec, nickrec, address);
231 rec = nickmatch_find(nickmatch, nickrec);
232 if (rec != NULL && hilight_match_level(rec, level))
237 for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
238 HILIGHT_REC *rec = tmp->data;
240 if (!rec->nickmask && hilight_match_level(rec, level) &&
241 hilight_match_channel(rec, channel) &&
242 hilight_match_text(rec, str, match_beg, match_end))
249 static char *hilight_get_act_color(HILIGHT_REC *rec)
251 g_return_val_if_fail(rec != NULL, NULL);
253 return g_strdup(rec->act_color != NULL ? rec->act_color :
254 rec->color != NULL ? rec->color :
255 settings_get_str("hilight_act_color"));
258 static char *hilight_get_color(HILIGHT_REC *rec)
262 g_return_val_if_fail(rec != NULL, NULL);
264 color = rec->color != NULL ? rec->color :
265 settings_get_str("hilight_color");
267 return format_string_expand(color);
270 static void hilight_update_text_dest(TEXT_DEST_REC *dest, HILIGHT_REC *rec)
272 dest->level |= MSGLEVEL_HILIGHT;
274 if (rec->priority > 0)
275 dest->hilight_priority = rec->priority;
277 dest->hilight_color = hilight_get_act_color(rec);
280 static void sig_print_text_stripped(TEXT_DEST_REC *dest, const char *str)
282 HILIGHT_REC *hilight;
284 g_return_if_fail(str != NULL);
286 if (next_nick_hilight != NULL) {
287 if (!next_nick_hilight->nick) {
288 /* non-nick hilight wanted */
289 hilight = next_nick_hilight;
290 next_nick_hilight = NULL;
291 if (!hilight_match_text(hilight, str,
293 &next_hilight_end)) {
294 next_hilight_start = 0;
295 next_hilight_end = strlen(str);
298 /* nick is highlighted, just set priority */
299 hilight_update_text_dest(dest, next_nick_hilight);
300 next_nick_hilight = NULL;
304 if (dest->level & (MSGLEVEL_NOHILIGHT|MSGLEVEL_HILIGHT))
307 hilight = hilight_match(dest->server, dest->target,
308 NULL, NULL, dest->level, str,
313 if (hilight != NULL) {
314 /* update the level / hilight info */
315 hilight_update_text_dest(dest, hilight);
317 next_line_hilight = hilight;
321 static void sig_print_text(TEXT_DEST_REC *dest, const char *str)
323 char *color, *newstr;
324 int next_hilight_len;
326 if (next_line_hilight == NULL)
329 color = hilight_get_color(next_line_hilight);
330 next_hilight_len = next_hilight_end-next_hilight_start;
332 if (!next_line_hilight->word) {
333 /* hilight whole line */
334 char *tmp = strip_codes(str);
335 newstr = g_strconcat(color, tmp, NULL);
338 /* hilight part of the line */
340 char *middle, *lastcolor;
341 int pos, color_pos, color_len;
343 tmp = g_string_new(NULL);
345 /* start of the line */
346 pos = strip_real_length(str, next_hilight_start, NULL, NULL);
347 g_string_append(tmp, str);
348 g_string_truncate(tmp, pos);
351 g_string_append(tmp, color);
353 /* middle of the line, stripped */
354 middle = strip_codes(str+pos);
356 g_string_append(tmp, middle);
357 g_string_truncate(tmp, pos+next_hilight_len);
360 /* end of the line */
361 pos = strip_real_length(str, next_hilight_end,
362 &color_pos, &color_len);
364 lastcolor = g_strndup(str+color_pos, color_len);
366 /* no colors in line, change back to default */
367 lastcolor = g_malloc0(3);
369 lastcolor[1] = FORMAT_STYLE_DEFAULTS;
371 g_string_append(tmp, lastcolor);
372 g_string_append(tmp, str+pos);
376 g_string_free(tmp, FALSE);
379 next_line_hilight = NULL;
380 signal_emit("print text", 2, dest, newstr);
388 char *hilight_match_nick(SERVER_REC *server, const char *channel,
389 const char *nick, const char *address,
390 int level, const char *msg)
395 rec = hilight_match(server, channel, nick, address,
396 level, msg, NULL, NULL);
397 color = rec == NULL || !rec->nick ? NULL :
398 hilight_get_color(rec);
400 next_nick_hilight = rec;
404 static void read_hilight_config(void)
411 hilights_destroy_all();
413 node = iconfig_node_traverse("hilights", FALSE);
419 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
422 if (node->type != NODE_TYPE_BLOCK)
425 text = config_node_get_str(node, "text", NULL);
426 if (text == NULL || *text == '\0')
429 rec = g_new0(HILIGHT_REC, 1);
430 hilights = g_slist_append(hilights, rec);
432 rec->text = g_strdup(text);
434 color = config_node_get_str(node, "color", NULL);
435 rec->color = color == NULL || *color == '\0' ? NULL :
438 color = config_node_get_str(node, "act_color", NULL);
439 rec->act_color = color == NULL || *color == '\0' ? NULL :
442 rec->level = config_node_get_int(node, "level", 0);
443 rec->priority = config_node_get_int(node, "priority", 0);
444 rec->nick = config_node_get_bool(node, "nick", TRUE);
445 rec->word = config_node_get_bool(node, "word", TRUE);
447 rec->nickmask = config_node_get_bool(node, "mask", FALSE);
448 rec->fullword = config_node_get_bool(node, "fullword", FALSE);
449 rec->regexp = config_node_get_bool(node, "regexp", FALSE);
452 rec->regexp_compiled = !rec->regexp ? FALSE :
453 regcomp(&rec->preg, rec->text,
454 REG_EXTENDED|REG_ICASE) == 0;
457 node = config_node_section(node, "channels", -1);
458 if (node != NULL) rec->channels = config_node_get_list(node);
464 static void hilight_print(int index, HILIGHT_REC *rec)
466 char *chans, *levelstr;
468 chans = rec->channels == NULL ? NULL :
469 g_strjoinv(",", rec->channels);
470 levelstr = rec->level == 0 ? NULL :
471 bits2level(rec->level);
472 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
473 TXT_HILIGHT_LINE, index, rec->text,
474 chans != NULL ? chans : "",
475 levelstr != NULL ? levelstr : "",
476 rec->nickmask ? " -mask" : "",
477 rec->fullword ? " -full" : "",
478 rec->regexp ? " -regexp" : "");
479 g_free_not_null(chans);
480 g_free_not_null(levelstr);
483 static void cmd_hilight_show(void)
488 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_HILIGHT_HEADER);
490 for (tmp = hilights; tmp != NULL; tmp = tmp->next, index++) {
491 HILIGHT_REC *rec = tmp->data;
493 hilight_print(index, rec);
495 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_HILIGHT_FOOTER);
498 /* SYNTAX: HILIGHT [-nick | -word | -line] [-mask | -full | -regexp]
499 [-color <color>] [-actcolor <color>] [-level <level>]
500 [-channels <channels>] <text> */
501 static void cmd_hilight(const char *data)
505 char *colorarg, *actcolorarg, *levelarg, *priorityarg, *chanarg, *text;
509 g_return_if_fail(data != NULL);
516 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
517 PARAM_FLAG_GETREST, "hilight", &optlist, &text))
520 chanarg = g_hash_table_lookup(optlist, "channels");
521 levelarg = g_hash_table_lookup(optlist, "level");
522 priorityarg = g_hash_table_lookup(optlist, "priority");
523 colorarg = g_hash_table_lookup(optlist, "color");
524 actcolorarg = g_hash_table_lookup(optlist, "actcolor");
526 if (*text == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
528 channels = (chanarg == NULL || *chanarg == '\0') ? NULL :
529 g_strsplit(replace_chars(chanarg, ',', ' '), " ", -1);
531 rec = hilight_find(text, channels);
533 rec = g_new0(HILIGHT_REC, 1);
535 /* default to nick/word hilighting */
539 rec->text = g_strdup(text);
540 rec->channels = channels;
542 g_strfreev(channels);
544 hilight_remove_config(rec);
545 hilights = g_slist_remove(hilights, rec);
548 rec->level = (levelarg == NULL || *levelarg == '\0') ? 0 :
549 level2bits(replace_chars(levelarg, ',', ' '));
550 rec->priority = priorityarg == NULL ? 0 : atoi(priorityarg);
552 if (g_hash_table_lookup(optlist, "line") != NULL) {
557 if (g_hash_table_lookup(optlist, "word") != NULL) {
562 if (g_hash_table_lookup(optlist, "nick") != NULL)
565 rec->nickmask = g_hash_table_lookup(optlist, "mask") != NULL;
566 rec->fullword = g_hash_table_lookup(optlist, "full") != NULL;
567 rec->regexp = g_hash_table_lookup(optlist, "regexp") != NULL;
569 if (colorarg != NULL) {
570 if (*colorarg != '\0')
571 rec->color = g_strdup(colorarg);
573 g_free_and_null(rec->color);
575 if (actcolorarg != NULL) {
576 if (*actcolorarg != '\0')
577 rec->act_color = g_strdup(actcolorarg);
579 g_free_and_null(rec->act_color);
583 if (rec->regexp_compiled)
585 rec->regexp_compiled = !rec->regexp ? FALSE :
586 regcomp(&rec->preg, rec->text, REG_EXTENDED|REG_ICASE) == 0;
589 hilights = g_slist_append(hilights, rec);
590 hilight_add_config(rec);
592 hilight_print(g_slist_index(hilights, rec)+1, rec);
593 cmd_params_free(free_arg);
598 /* SYNTAX: DEHILIGHT <id>|<mask> */
599 static void cmd_dehilight(const char *data)
604 if (is_numeric(data, ' ')) {
605 /* with index number */
606 tmp = g_slist_nth(hilights, atoi(data)-1);
607 rec = tmp == NULL ? NULL : tmp->data;
610 char *chans[2] = { "*", NULL };
611 rec = hilight_find(data, chans);
615 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_HILIGHT_NOT_FOUND, data);
617 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_HILIGHT_REMOVED, rec->text);
623 static void hilight_nick_cache(GHashTable *list, CHANNEL_REC *channel,
631 if (nick->host == NULL)
632 return; /* don't check until host is known */
634 nickmask = g_strconcat(nick->nick, "!", nick->host, NULL);
636 best_match = 0; match = NULL;
637 for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
638 HILIGHT_REC *rec = tmp->data;
641 hilight_match_channel(rec, channel->name) &&
642 match_wildcards(rec->text, nickmask)) {
643 len = strlen(rec->text);
644 if (best_match < len) {
650 g_free_not_null(nickmask);
653 g_hash_table_insert(list, nick, match);
656 static void read_settings(void)
658 default_hilight_level = level2bits(settings_get_str("hilight_level"));
661 void hilight_text_init(void)
663 settings_add_str("lookandfeel", "hilight_color", "%Y");
664 settings_add_str("lookandfeel", "hilight_act_color", "%M");
665 settings_add_str("lookandfeel", "hilight_level", "PUBLIC DCCMSGS");
667 next_nick_hilight = NULL;
668 next_line_hilight = NULL;
672 nickmatch = nickmatch_init(hilight_nick_cache);
673 read_hilight_config();
675 signal_add_first("print text stripped", (SIGNAL_FUNC) sig_print_text_stripped);
676 signal_add_first("print text", (SIGNAL_FUNC) sig_print_text);
677 signal_add("setup reread", (SIGNAL_FUNC) read_hilight_config);
678 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
680 command_bind("hilight", NULL, (SIGNAL_FUNC) cmd_hilight);
681 command_bind("dehilight", NULL, (SIGNAL_FUNC) cmd_dehilight);
682 command_set_options("hilight", "-color -actcolor -level -priority -channels nick word line mask full regexp");
685 void hilight_text_deinit(void)
687 hilights_destroy_all();
688 nickmatch_deinit(nickmatch);
690 signal_remove("print text stripped", (SIGNAL_FUNC) sig_print_text_stripped);
691 signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
692 signal_remove("setup reread", (SIGNAL_FUNC) read_hilight_config);
693 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
695 command_unbind("hilight", (SIGNAL_FUNC) cmd_hilight);
696 command_unbind("dehilight", (SIGNAL_FUNC) cmd_dehilight);