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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 int never_hilight_level, default_hilight_level;
43 static void reset_level_cache(void)
47 never_hilight_level = MSGLEVEL_ALL & ~default_hilight_level;
48 for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
49 HILIGHT_REC *rec = tmp->data;
51 if (never_hilight_level & rec->level)
52 never_hilight_level &= ~rec->level;
56 static void reset_cache(void)
59 nickmatch_rebuild(nickmatch);
62 static void hilight_add_config(HILIGHT_REC *rec)
66 g_return_if_fail(rec != NULL);
68 node = iconfig_node_traverse("(hilights", TRUE);
69 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
71 iconfig_node_set_str(node, "text", rec->text);
72 if (rec->level > 0) iconfig_node_set_int(node, "level", rec->level);
73 if (rec->color) iconfig_node_set_str(node, "color", rec->color);
74 if (rec->act_color) iconfig_node_set_str(node, "act_color", rec->act_color);
75 if (rec->priority > 0) iconfig_node_set_int(node, "priority", rec->priority);
76 iconfig_node_set_bool(node, "nick", rec->nick);
77 iconfig_node_set_bool(node, "word", rec->word);
78 if (rec->nickmask) iconfig_node_set_bool(node, "mask", TRUE);
79 if (rec->fullword) iconfig_node_set_bool(node, "fullword", TRUE);
80 if (rec->regexp) iconfig_node_set_bool(node, "regexp", TRUE);
82 if (rec->channels != NULL && *rec->channels != NULL) {
83 node = config_node_section(node, "channels", NODE_TYPE_LIST);
84 iconfig_node_add_list(node, rec->channels);
88 static void hilight_remove_config(HILIGHT_REC *rec)
92 g_return_if_fail(rec != NULL);
94 node = iconfig_node_traverse("hilights", FALSE);
95 if (node != NULL) iconfig_node_list_remove(node, g_slist_index(hilights, rec));
98 static void hilight_destroy(HILIGHT_REC *rec)
100 g_return_if_fail(rec != NULL);
103 if (rec->regexp_compiled) regfree(&rec->preg);
105 if (rec->channels != NULL) g_strfreev(rec->channels);
106 g_free_not_null(rec->color);
107 g_free_not_null(rec->act_color);
112 static void hilights_destroy_all(void)
114 g_slist_foreach(hilights, (GFunc) hilight_destroy, NULL);
115 g_slist_free(hilights);
119 static void hilight_init_rec(HILIGHT_REC *rec)
122 if (rec->regexp_compiled) regfree(&rec->preg);
123 rec->regexp_compiled = !rec->regexp ? FALSE :
124 regcomp(&rec->preg, rec->text, REG_EXTENDED|REG_ICASE) == 0;
128 void hilight_create(HILIGHT_REC *rec)
130 if (g_slist_find(hilights, rec) != NULL) {
131 hilight_remove_config(rec);
132 hilights = g_slist_remove(hilights, rec);
135 hilights = g_slist_append(hilights, rec);
136 hilight_add_config(rec);
138 hilight_init_rec(rec);
140 signal_emit("hilight created", 1, rec);
143 void hilight_remove(HILIGHT_REC *rec)
145 g_return_if_fail(rec != NULL);
147 hilight_remove_config(rec);
148 hilights = g_slist_remove(hilights, rec);
150 signal_emit("hilight destroyed", 1, rec);
151 hilight_destroy(rec);
154 static HILIGHT_REC *hilight_find(const char *text, char **channels)
159 g_return_val_if_fail(text != NULL, NULL);
161 for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
162 HILIGHT_REC *rec = tmp->data;
164 if (g_strcasecmp(rec->text, text) != 0)
167 if ((channels == NULL && rec->channels == NULL))
168 return rec; /* no channels - ok */
170 if (channels != NULL && strcmp(*channels, "*") == 0)
171 return rec; /* ignore channels */
173 if (channels == NULL || rec->channels == NULL)
174 continue; /* other doesn't have channels */
176 if (strarray_length(channels) != strarray_length(rec->channels))
177 continue; /* different amount of channels */
179 /* check that channels match */
180 for (chan = channels; *chan != NULL; chan++) {
181 if (strarray_find(rec->channels, *chan) == -1)
186 return rec; /* channels ok */
192 static int hilight_match_text(HILIGHT_REC *rec, const char *text,
193 int *match_beg, int *match_end)
199 regmatch_t rmatch[1];
201 if (rec->regexp_compiled &&
202 regexec(&rec->preg, text, 1, rmatch, 0) == 0) {
203 if (rmatch[0].rm_so > 0 &&
204 match_beg != NULL && match_end != NULL) {
205 *match_beg = rmatch[0].rm_so;
206 *match_end = rmatch[0].rm_eo;
212 match = rec->fullword ?
213 stristr_full(text, rec->text) :
214 stristr(text, rec->text);
216 if (match_beg != NULL && match_end != NULL) {
217 *match_beg = (int) (match-text);
218 *match_end = *match_beg + strlen(rec->text);
227 #define hilight_match_level(rec, level) \
228 (level & (((rec)->level != 0 ? rec->level : default_hilight_level)))
230 #define hilight_match_channel(rec, channel) \
231 ((rec)->channels == NULL || ((channel) != NULL && \
232 strarray_find((rec)->channels, (channel)) != -1))
234 HILIGHT_REC *hilight_match(SERVER_REC *server, const char *channel,
235 const char *nick, const char *address,
236 int level, const char *str,
237 int *match_beg, int *match_end)
240 CHANNEL_REC *chanrec;
243 g_return_val_if_fail(str != NULL, NULL);
245 if ((never_hilight_level & level) == level)
249 /* check nick mask hilights */
250 chanrec = channel_find(server, channel);
251 nickrec = chanrec == NULL ? NULL :
252 nicklist_find(chanrec, nick);
253 if (nickrec != NULL) {
256 if (nickrec->host == NULL)
257 nicklist_set_host(chanrec, nickrec, address);
259 rec = nickmatch_find(nickmatch, nickrec);
260 if (rec != NULL && hilight_match_level(rec, level))
265 for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
266 HILIGHT_REC *rec = tmp->data;
268 if (!rec->nickmask && hilight_match_level(rec, level) &&
269 hilight_match_channel(rec, channel) &&
270 hilight_match_text(rec, str, match_beg, match_end))
277 static char *hilight_get_act_color(HILIGHT_REC *rec)
279 g_return_val_if_fail(rec != NULL, NULL);
281 return g_strdup(rec->act_color != NULL ? rec->act_color :
282 rec->color != NULL ? rec->color :
283 settings_get_str("hilight_act_color"));
286 char *hilight_get_color(HILIGHT_REC *rec)
290 g_return_val_if_fail(rec != NULL, NULL);
292 color = rec->color != NULL ? rec->color :
293 settings_get_str("hilight_color");
295 return format_string_expand(color, NULL);
298 void hilight_update_text_dest(TEXT_DEST_REC *dest, HILIGHT_REC *rec)
300 dest->level |= MSGLEVEL_HILIGHT;
302 if (rec->priority > 0)
303 dest->hilight_priority = rec->priority;
305 g_free_and_null(dest->hilight_color);
306 if (rec->act_color != NULL && strcmp(rec->act_color, "%n") == 0)
307 dest->level |= MSGLEVEL_NO_ACT;
309 dest->hilight_color = hilight_get_act_color(rec);
312 static void hilight_print(int index, HILIGHT_REC *rec);
314 static void sig_print_text(TEXT_DEST_REC *dest, const char *text,
315 const char *stripped)
317 HILIGHT_REC *hilight;
318 char *color, *newstr;
319 int old_level, hilight_start, hilight_end, hilight_len;
322 if (dest->level & MSGLEVEL_NOHILIGHT)
325 hilight_start = hilight_end = 0;
326 hilight = hilight_match(dest->server, dest->target,
327 NULL, NULL, dest->level, stripped,
333 nick_match = hilight->nick && (dest->level & (MSGLEVEL_PUBLIC|MSGLEVEL_ACTIONS)) == MSGLEVEL_PUBLIC;
335 old_level = dest->level;
336 if (!nick_match || (dest->level & MSGLEVEL_HILIGHT)) {
337 /* update the level / hilight info */
338 hilight_update_text_dest(dest, hilight);
342 return; /* fe-messages.c should have taken care of this */
344 if (old_level & MSGLEVEL_HILIGHT) {
345 /* nick is highlighted, just set priority */
349 color = hilight_get_color(hilight);
350 hilight_len = hilight_end-hilight_start;
352 if (!hilight->word) {
353 /* hilight whole line */
354 char *tmp = strip_codes(text);
355 newstr = g_strconcat(color, tmp, NULL);
358 /* hilight part of the line */
361 int pos, color_pos, color_len;
363 tmp = g_string_new(NULL);
365 /* start of the line */
366 pos = strip_real_length(text, hilight_start, NULL, NULL);
367 g_string_append(tmp, text);
368 g_string_truncate(tmp, pos);
371 g_string_append(tmp, color);
373 /* middle of the line, stripped */
374 middle = strip_codes(text+pos);
376 g_string_append(tmp, middle);
377 g_string_truncate(tmp, pos+hilight_len);
380 /* end of the line */
381 pos = strip_real_length(text, hilight_end,
382 &color_pos, &color_len);
384 g_string_append_len(tmp, text+color_pos, color_len);
386 /* no colors in line, change back to default */
387 g_string_append_c(tmp, 4);
388 g_string_append_c(tmp, FORMAT_STYLE_DEFAULTS);
390 g_string_append(tmp, text+pos);
393 g_string_free(tmp, FALSE);
396 signal_emit("print text", 3, dest, newstr, stripped);
404 HILIGHT_REC *hilight_match_nick(SERVER_REC *server, const char *channel,
405 const char *nick, const char *address,
406 int level, const char *msg)
410 rec = hilight_match(server, channel, nick, address,
411 level, msg, NULL, NULL);
412 return (rec == NULL || !rec->nick) ? NULL : rec;
415 static void read_hilight_config(void)
422 hilights_destroy_all();
424 node = iconfig_node_traverse("hilights", FALSE);
430 tmp = config_node_first(node->value);
431 for (; tmp != NULL; tmp = config_node_next(tmp)) {
434 if (node->type != NODE_TYPE_BLOCK)
437 text = config_node_get_str(node, "text", NULL);
438 if (text == NULL || *text == '\0')
441 rec = g_new0(HILIGHT_REC, 1);
442 hilights = g_slist_append(hilights, rec);
444 rec->text = g_strdup(text);
446 color = config_node_get_str(node, "color", NULL);
447 rec->color = color == NULL || *color == '\0' ? NULL :
450 color = config_node_get_str(node, "act_color", NULL);
451 rec->act_color = color == NULL || *color == '\0' ? NULL :
454 rec->level = config_node_get_int(node, "level", 0);
455 rec->priority = config_node_get_int(node, "priority", 0);
456 rec->nick = config_node_get_bool(node, "nick", TRUE);
457 rec->word = config_node_get_bool(node, "word", TRUE);
459 rec->nickmask = config_node_get_bool(node, "mask", FALSE);
460 rec->fullword = config_node_get_bool(node, "fullword", FALSE);
461 rec->regexp = config_node_get_bool(node, "regexp", FALSE);
463 hilight_init_rec(rec);
465 node = config_node_section(node, "channels", -1);
466 if (node != NULL) rec->channels = config_node_get_list(node);
472 static void hilight_print(int index, HILIGHT_REC *rec)
474 char *chans, *levelstr;
477 options = g_string_new(NULL);
478 if (!rec->nick || !rec->word) {
479 if (rec->nick) g_string_append(options, "-nick ");
480 if (rec->word) g_string_append(options, "-word ");
483 if (rec->nickmask) g_string_append(options, "-mask ");
484 if (rec->fullword) g_string_append(options, "-full ");
486 g_string_append(options, "-regexp ");
488 if (!rec->regexp_compiled)
489 g_string_append(options, "[INVALID!] ");
493 if (rec->priority != 0)
494 g_string_append_printf(options, "-priority %d ", rec->priority);
495 if (rec->color != NULL)
496 g_string_append_printf(options, "-color %s ", rec->color);
497 if (rec->act_color != NULL)
498 g_string_append_printf(options, "-actcolor %s ", rec->act_color);
500 chans = rec->channels == NULL ? NULL :
501 g_strjoinv(",", rec->channels);
502 levelstr = rec->level == 0 ? NULL :
503 bits2level(rec->level);
504 if (levelstr != NULL)
505 levelstr = g_strconcat(levelstr, " ", NULL);
506 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
507 TXT_HILIGHT_LINE, index, rec->text,
508 chans != NULL ? chans : "",
509 levelstr != NULL ? levelstr : "",
511 g_free_not_null(chans);
512 g_free_not_null(levelstr);
513 g_string_free(options, TRUE);
516 static void cmd_hilight_show(void)
521 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_HILIGHT_HEADER);
523 for (tmp = hilights; tmp != NULL; tmp = tmp->next, index++) {
524 HILIGHT_REC *rec = tmp->data;
526 hilight_print(index, rec);
528 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_HILIGHT_FOOTER);
531 /* SYNTAX: HILIGHT [-nick | -word | -line] [-mask | -full | -regexp]
532 [-color <color>] [-actcolor <color>] [-level <level>]
533 [-channels <channels>] <text> */
534 static void cmd_hilight(const char *data)
538 char *colorarg, *actcolorarg, *levelarg, *priorityarg, *chanarg, *text;
542 g_return_if_fail(data != NULL);
549 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
550 PARAM_FLAG_GETREST, "hilight", &optlist, &text))
553 chanarg = g_hash_table_lookup(optlist, "channels");
554 levelarg = g_hash_table_lookup(optlist, "level");
555 priorityarg = g_hash_table_lookup(optlist, "priority");
556 colorarg = g_hash_table_lookup(optlist, "color");
557 actcolorarg = g_hash_table_lookup(optlist, "actcolor");
559 if (*text == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
561 channels = (chanarg == NULL || *chanarg == '\0') ? NULL :
562 g_strsplit(chanarg, ",", -1);
564 rec = hilight_find(text, channels);
566 rec = g_new0(HILIGHT_REC, 1);
568 /* default to nick/word hilighting */
572 rec->text = g_strdup(text);
573 rec->channels = channels;
575 g_strfreev(channels);
578 rec->level = (levelarg == NULL || *levelarg == '\0') ? 0 :
579 level2bits(replace_chars(levelarg, ',', ' '), NULL);
580 rec->priority = priorityarg == NULL ? 0 : atoi(priorityarg);
582 if (g_hash_table_lookup(optlist, "line") != NULL) {
587 if (g_hash_table_lookup(optlist, "word") != NULL) {
592 if (g_hash_table_lookup(optlist, "nick") != NULL)
595 rec->nickmask = g_hash_table_lookup(optlist, "mask") != NULL;
596 rec->fullword = g_hash_table_lookup(optlist, "full") != NULL;
597 rec->regexp = g_hash_table_lookup(optlist, "regexp") != NULL;
599 if (colorarg != NULL) {
600 g_free_and_null(rec->color);
601 if (*colorarg != '\0')
602 rec->color = g_strdup(colorarg);
604 if (actcolorarg != NULL) {
605 g_free_and_null(rec->act_color);
606 if (*actcolorarg != '\0')
607 rec->act_color = g_strdup(actcolorarg);
612 hilight_print(g_slist_index(hilights, rec)+1, rec);
613 cmd_params_free(free_arg);
618 /* SYNTAX: DEHILIGHT <id>|<mask> */
619 static void cmd_dehilight(const char *data)
624 if (is_numeric(data, ' ')) {
625 /* with index number */
626 tmp = g_slist_nth(hilights, atoi(data)-1);
627 rec = tmp == NULL ? NULL : tmp->data;
630 char *chans[2] = { "*", NULL };
631 rec = hilight_find(data, chans);
635 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_HILIGHT_NOT_FOUND, data);
637 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_HILIGHT_REMOVED, rec->text);
643 static void hilight_nick_cache(GHashTable *list, CHANNEL_REC *channel,
651 if (nick->host == NULL)
652 return; /* don't check until host is known */
654 nickmask = g_strconcat(nick->nick, "!", nick->host, NULL);
656 best_match = 0; match = NULL;
657 for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
658 HILIGHT_REC *rec = tmp->data;
661 hilight_match_channel(rec, channel->name) &&
662 match_wildcards(rec->text, nickmask)) {
663 len = strlen(rec->text);
664 if (best_match < len) {
670 g_free_not_null(nickmask);
673 g_hash_table_insert(list, nick, match);
676 static void read_settings(void)
678 default_hilight_level = settings_get_level("hilight_level");
682 void hilight_text_init(void)
684 settings_add_str("lookandfeel", "hilight_color", "%Y");
685 settings_add_str("lookandfeel", "hilight_act_color", "%M");
686 settings_add_level("lookandfeel", "hilight_level", "PUBLIC DCCMSGS");
690 nickmatch = nickmatch_init(hilight_nick_cache);
691 read_hilight_config();
693 signal_add_first("print text", (SIGNAL_FUNC) sig_print_text);
694 signal_add("setup reread", (SIGNAL_FUNC) read_hilight_config);
695 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
697 command_bind("hilight", NULL, (SIGNAL_FUNC) cmd_hilight);
698 command_bind("dehilight", NULL, (SIGNAL_FUNC) cmd_dehilight);
699 command_set_options("hilight", "-color -actcolor -level -priority -channels nick word line mask full regexp");
702 void hilight_text_deinit(void)
704 hilights_destroy_all();
705 nickmatch_deinit(nickmatch);
707 signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
708 signal_remove("setup reread", (SIGNAL_FUNC) read_hilight_config);
709 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
711 command_unbind("hilight", (SIGNAL_FUNC) cmd_hilight);
712 command_unbind("dehilight", (SIGNAL_FUNC) cmd_dehilight);