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 int never_hilight_level, default_hilight_level;
43 static void reset_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;
55 nickmatch_rebuild(nickmatch);
58 static void hilight_add_config(HILIGHT_REC *rec)
62 g_return_if_fail(rec != NULL);
64 node = iconfig_node_traverse("(hilights", TRUE);
65 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
67 iconfig_node_set_str(node, "text", rec->text);
68 if (rec->level > 0) iconfig_node_set_int(node, "level", rec->level);
69 if (rec->color) iconfig_node_set_str(node, "color", rec->color);
70 if (rec->act_color) iconfig_node_set_str(node, "act_color", rec->act_color);
71 if (rec->priority > 0) iconfig_node_set_int(node, "priority", rec->priority);
72 iconfig_node_set_bool(node, "nick", rec->nick);
73 iconfig_node_set_bool(node, "word", rec->word);
74 if (rec->nickmask) iconfig_node_set_bool(node, "mask", TRUE);
75 if (rec->fullword) iconfig_node_set_bool(node, "fullword", TRUE);
76 if (rec->regexp) iconfig_node_set_bool(node, "regexp", TRUE);
78 if (rec->channels != NULL && *rec->channels != NULL) {
79 node = config_node_section(node, "channels", NODE_TYPE_LIST);
80 iconfig_node_add_list(node, rec->channels);
84 static void hilight_remove_config(HILIGHT_REC *rec)
88 g_return_if_fail(rec != NULL);
90 node = iconfig_node_traverse("hilights", FALSE);
91 if (node != NULL) iconfig_node_list_remove(node, g_slist_index(hilights, rec));
94 static void hilight_destroy(HILIGHT_REC *rec)
96 g_return_if_fail(rec != NULL);
99 if (rec->regexp_compiled) regfree(&rec->preg);
101 if (rec->channels != NULL) g_strfreev(rec->channels);
102 g_free_not_null(rec->color);
103 g_free_not_null(rec->act_color);
108 static void hilights_destroy_all(void)
110 g_slist_foreach(hilights, (GFunc) hilight_destroy, NULL);
111 g_slist_free(hilights);
115 static void hilight_remove(HILIGHT_REC *rec)
117 g_return_if_fail(rec != NULL);
119 hilight_remove_config(rec);
120 hilights = g_slist_remove(hilights, rec);
121 hilight_destroy(rec);
124 static HILIGHT_REC *hilight_find(const char *text, char **channels)
129 g_return_val_if_fail(text != NULL, NULL);
131 for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
132 HILIGHT_REC *rec = tmp->data;
134 if (g_strcasecmp(rec->text, text) != 0)
137 if ((channels == NULL && rec->channels == NULL))
138 return rec; /* no channels - ok */
140 if (channels != NULL && strcmp(*channels, "*") == 0)
141 return rec; /* ignore channels */
143 if (channels == NULL || rec->channels == NULL)
144 continue; /* other doesn't have channels */
146 if (strarray_length(channels) != strarray_length(rec->channels))
147 continue; /* different amount of channels */
149 /* check that channels match */
150 for (chan = channels; *chan != NULL; chan++) {
151 if (strarray_find(rec->channels, *chan) == -1)
156 return rec; /* channels ok */
162 static int hilight_match_text(HILIGHT_REC *rec, const char *text,
163 int *match_beg, int *match_end)
169 regmatch_t rmatch[1];
171 if (rec->regexp_compiled &&
172 regexec(&rec->preg, text, 1, rmatch, 0) == 0) {
173 if (rmatch[0].rm_so > 0 &&
174 match_beg != NULL && match_end != NULL) {
175 *match_beg = rmatch[0].rm_so;
176 *match_end = rmatch[0].rm_eo;
182 match = rec->fullword ?
183 stristr_full(text, rec->text) :
184 stristr(text, rec->text);
186 if (match_beg != NULL && match_end != NULL) {
187 *match_beg = (int) (match-text);
188 *match_end = *match_beg + strlen(rec->text);
197 #define hilight_match_level(rec, level) \
198 (level & (((rec)->level != 0 ? rec->level : default_hilight_level)))
200 #define hilight_match_channel(rec, channel) \
201 ((rec)->channels == NULL || ((channel) != NULL && \
202 strarray_find((rec)->channels, (channel)) != -1))
204 HILIGHT_REC *hilight_match(SERVER_REC *server, const char *channel,
205 const char *nick, const char *address,
206 int level, const char *str,
207 int *match_beg, int *match_end)
210 CHANNEL_REC *chanrec;
213 g_return_val_if_fail(str != NULL, NULL);
215 if ((never_hilight_level & level) == level)
219 /* check nick mask hilights */
220 chanrec = channel_find(server, channel);
221 nickrec = chanrec == NULL ? NULL :
222 nicklist_find(chanrec, nick);
223 if (nickrec != NULL) {
226 if (nickrec->host == NULL)
227 nicklist_set_host(chanrec, nickrec, address);
229 rec = nickmatch_find(nickmatch, nickrec);
230 if (rec != NULL && hilight_match_level(rec, level))
235 for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
236 HILIGHT_REC *rec = tmp->data;
238 if (!rec->nickmask && hilight_match_level(rec, level) &&
239 hilight_match_channel(rec, channel) &&
240 hilight_match_text(rec, str, match_beg, match_end))
247 static char *hilight_get_act_color(HILIGHT_REC *rec)
249 g_return_val_if_fail(rec != NULL, NULL);
251 return g_strdup(rec->act_color != NULL ? rec->act_color :
252 rec->color != NULL ? rec->color :
253 settings_get_str("hilight_act_color"));
256 static char *hilight_get_color(HILIGHT_REC *rec)
260 g_return_val_if_fail(rec != NULL, NULL);
262 color = rec->color != NULL ? rec->color :
263 settings_get_str("hilight_color");
265 return format_string_expand(color, NULL);
268 static void hilight_update_text_dest(TEXT_DEST_REC *dest, HILIGHT_REC *rec)
270 dest->level |= MSGLEVEL_HILIGHT;
272 if (rec->priority > 0)
273 dest->hilight_priority = rec->priority;
275 g_free_and_null(dest->hilight_color);
276 if (rec->act_color != NULL && strcmp(rec->act_color, "%n") == 0)
277 dest->level |= MSGLEVEL_NO_ACT;
279 dest->hilight_color = hilight_get_act_color(rec);
282 static void sig_print_text(TEXT_DEST_REC *dest, const char *text,
283 const char *stripped)
285 HILIGHT_REC *hilight;
286 char *color, *newstr;
287 int old_level, hilight_start, hilight_end, hilight_len;
290 if (dest->level & MSGLEVEL_NOHILIGHT)
293 hilight_start = hilight_end = 0;
294 hilight = hilight_match(dest->server, dest->target,
295 NULL, NULL, dest->level, stripped,
301 nick_match = hilight->nick && (dest->level & (MSGLEVEL_PUBLIC|MSGLEVEL_ACTIONS)) == MSGLEVEL_PUBLIC;
303 old_level = dest->level;
304 if (!nick_match || (dest->level & MSGLEVEL_HILIGHT)) {
305 /* update the level / hilight info */
306 hilight_update_text_dest(dest, hilight);
310 return; /* fe-messages.c should have taken care of this */
312 if (old_level & MSGLEVEL_HILIGHT) {
313 /* nick is highlighted, just set priority */
317 color = hilight_get_color(hilight);
318 hilight_len = hilight_end-hilight_start;
320 if (!hilight->word) {
321 /* hilight whole line */
322 char *tmp = strip_codes(text);
323 newstr = g_strconcat(color, tmp, NULL);
326 /* hilight part of the line */
328 char *middle, *lastcolor;
329 int pos, color_pos, color_len;
331 tmp = g_string_new(NULL);
333 /* start of the line */
334 pos = strip_real_length(text, hilight_start, NULL, NULL);
335 g_string_append(tmp, text);
336 g_string_truncate(tmp, pos);
339 g_string_append(tmp, color);
341 /* middle of the line, stripped */
342 middle = strip_codes(text+pos);
344 g_string_append(tmp, middle);
345 g_string_truncate(tmp, pos+hilight_len);
348 /* end of the line */
349 pos = strip_real_length(text, hilight_end,
350 &color_pos, &color_len);
352 lastcolor = g_strndup(text+color_pos, color_len);
354 /* no colors in line, change back to default */
355 lastcolor = g_malloc0(3);
357 lastcolor[1] = FORMAT_STYLE_DEFAULTS;
359 g_string_append(tmp, lastcolor);
360 g_string_append(tmp, text+pos);
364 g_string_free(tmp, FALSE);
367 signal_emit("print text", 3, dest, newstr, stripped);
375 char *hilight_match_nick(SERVER_REC *server, const char *channel,
376 const char *nick, const char *address,
377 int level, const char *msg)
382 rec = hilight_match(server, channel, nick, address,
383 level, msg, NULL, NULL);
384 color = rec == NULL || !rec->nick ? NULL :
385 hilight_get_color(rec);
390 static void read_hilight_config(void)
397 hilights_destroy_all();
399 node = iconfig_node_traverse("hilights", FALSE);
405 tmp = config_node_first(node->value);
406 for (; tmp != NULL; tmp = config_node_next(tmp)) {
409 if (node->type != NODE_TYPE_BLOCK)
412 text = config_node_get_str(node, "text", NULL);
413 if (text == NULL || *text == '\0')
416 rec = g_new0(HILIGHT_REC, 1);
417 hilights = g_slist_append(hilights, rec);
419 rec->text = g_strdup(text);
421 color = config_node_get_str(node, "color", NULL);
422 rec->color = color == NULL || *color == '\0' ? NULL :
425 color = config_node_get_str(node, "act_color", NULL);
426 rec->act_color = color == NULL || *color == '\0' ? NULL :
429 rec->level = config_node_get_int(node, "level", 0);
430 rec->priority = config_node_get_int(node, "priority", 0);
431 rec->nick = config_node_get_bool(node, "nick", TRUE);
432 rec->word = config_node_get_bool(node, "word", TRUE);
434 rec->nickmask = config_node_get_bool(node, "mask", FALSE);
435 rec->fullword = config_node_get_bool(node, "fullword", FALSE);
436 rec->regexp = config_node_get_bool(node, "regexp", FALSE);
439 rec->regexp_compiled = !rec->regexp ? FALSE :
440 regcomp(&rec->preg, rec->text,
441 REG_EXTENDED|REG_ICASE) == 0;
444 node = config_node_section(node, "channels", -1);
445 if (node != NULL) rec->channels = config_node_get_list(node);
451 static void hilight_print(int index, HILIGHT_REC *rec)
453 char *chans, *levelstr;
455 chans = rec->channels == NULL ? NULL :
456 g_strjoinv(",", rec->channels);
457 levelstr = rec->level == 0 ? NULL :
458 bits2level(rec->level);
459 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
460 TXT_HILIGHT_LINE, index, rec->text,
461 chans != NULL ? chans : "",
462 levelstr != NULL ? levelstr : "",
463 rec->nickmask ? " -mask" : "",
464 rec->fullword ? " -full" : "",
465 rec->regexp ? " -regexp" : "");
466 g_free_not_null(chans);
467 g_free_not_null(levelstr);
470 static void cmd_hilight_show(void)
475 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_HILIGHT_HEADER);
477 for (tmp = hilights; tmp != NULL; tmp = tmp->next, index++) {
478 HILIGHT_REC *rec = tmp->data;
480 hilight_print(index, rec);
482 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_HILIGHT_FOOTER);
485 /* SYNTAX: HILIGHT [-nick | -word | -line] [-mask | -full | -regexp]
486 [-color <color>] [-actcolor <color>] [-level <level>]
487 [-channels <channels>] <text> */
488 static void cmd_hilight(const char *data)
492 char *colorarg, *actcolorarg, *levelarg, *priorityarg, *chanarg, *text;
496 g_return_if_fail(data != NULL);
503 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
504 PARAM_FLAG_GETREST, "hilight", &optlist, &text))
507 chanarg = g_hash_table_lookup(optlist, "channels");
508 levelarg = g_hash_table_lookup(optlist, "level");
509 priorityarg = g_hash_table_lookup(optlist, "priority");
510 colorarg = g_hash_table_lookup(optlist, "color");
511 actcolorarg = g_hash_table_lookup(optlist, "actcolor");
513 if (*text == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
515 channels = (chanarg == NULL || *chanarg == '\0') ? NULL :
516 g_strsplit(replace_chars(chanarg, ',', ' '), " ", -1);
518 rec = hilight_find(text, channels);
520 rec = g_new0(HILIGHT_REC, 1);
522 /* default to nick/word hilighting */
526 rec->text = g_strdup(text);
527 rec->channels = channels;
529 g_strfreev(channels);
531 hilight_remove_config(rec);
532 hilights = g_slist_remove(hilights, rec);
535 rec->level = (levelarg == NULL || *levelarg == '\0') ? 0 :
536 level2bits(replace_chars(levelarg, ',', ' '));
537 rec->priority = priorityarg == NULL ? 0 : atoi(priorityarg);
539 if (g_hash_table_lookup(optlist, "line") != NULL) {
544 if (g_hash_table_lookup(optlist, "word") != NULL) {
549 if (g_hash_table_lookup(optlist, "nick") != NULL)
552 rec->nickmask = g_hash_table_lookup(optlist, "mask") != NULL;
553 rec->fullword = g_hash_table_lookup(optlist, "full") != NULL;
554 rec->regexp = g_hash_table_lookup(optlist, "regexp") != NULL;
556 if (colorarg != NULL) {
557 g_free_and_null(rec->color);
558 if (*colorarg != '\0')
559 rec->color = g_strdup(colorarg);
561 if (actcolorarg != NULL) {
562 g_free_and_null(rec->act_color);
563 if (*actcolorarg != '\0')
564 rec->act_color = g_strdup(actcolorarg);
568 if (rec->regexp_compiled)
570 rec->regexp_compiled = !rec->regexp ? FALSE :
571 regcomp(&rec->preg, rec->text, REG_EXTENDED|REG_ICASE) == 0;
574 hilights = g_slist_append(hilights, rec);
575 hilight_add_config(rec);
577 hilight_print(g_slist_index(hilights, rec)+1, rec);
578 cmd_params_free(free_arg);
583 /* SYNTAX: DEHILIGHT <id>|<mask> */
584 static void cmd_dehilight(const char *data)
589 if (is_numeric(data, ' ')) {
590 /* with index number */
591 tmp = g_slist_nth(hilights, atoi(data)-1);
592 rec = tmp == NULL ? NULL : tmp->data;
595 char *chans[2] = { "*", NULL };
596 rec = hilight_find(data, chans);
600 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_HILIGHT_NOT_FOUND, data);
602 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_HILIGHT_REMOVED, rec->text);
608 static void hilight_nick_cache(GHashTable *list, CHANNEL_REC *channel,
616 if (nick->host == NULL)
617 return; /* don't check until host is known */
619 nickmask = g_strconcat(nick->nick, "!", nick->host, NULL);
621 best_match = 0; match = NULL;
622 for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
623 HILIGHT_REC *rec = tmp->data;
626 hilight_match_channel(rec, channel->name) &&
627 match_wildcards(rec->text, nickmask)) {
628 len = strlen(rec->text);
629 if (best_match < len) {
635 g_free_not_null(nickmask);
638 g_hash_table_insert(list, nick, match);
641 static void read_settings(void)
643 default_hilight_level = level2bits(settings_get_str("hilight_level"));
646 void hilight_text_init(void)
648 settings_add_str("lookandfeel", "hilight_color", "%Y");
649 settings_add_str("lookandfeel", "hilight_act_color", "%M");
650 settings_add_str("lookandfeel", "hilight_level", "PUBLIC DCCMSGS");
654 nickmatch = nickmatch_init(hilight_nick_cache);
655 read_hilight_config();
657 signal_add_first("print text", (SIGNAL_FUNC) sig_print_text);
658 signal_add("setup reread", (SIGNAL_FUNC) read_hilight_config);
659 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
661 command_bind("hilight", NULL, (SIGNAL_FUNC) cmd_hilight);
662 command_bind("dehilight", NULL, (SIGNAL_FUNC) cmd_dehilight);
663 command_set_options("hilight", "-color -actcolor -level -priority -channels nick word line mask full regexp");
666 void hilight_text_deinit(void)
668 hilights_destroy_all();
669 nickmatch_deinit(nickmatch);
671 signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
672 signal_remove("setup reread", (SIGNAL_FUNC) read_hilight_config);
673 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
675 command_unbind("hilight", (SIGNAL_FUNC) cmd_hilight);
676 command_unbind("dehilight", (SIGNAL_FUNC) cmd_dehilight);