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_init_rec(HILIGHT_REC *rec)
118 if (rec->regexp_compiled) regfree(&rec->preg);
119 rec->regexp_compiled = !rec->regexp ? FALSE :
120 regcomp(&rec->preg, rec->text, REG_EXTENDED|REG_ICASE) == 0;
124 void hilight_create(HILIGHT_REC *rec)
126 if (g_slist_find(hilights, rec) != NULL) {
127 hilight_remove_config(rec);
128 hilights = g_slist_remove(hilights, rec);
131 hilights = g_slist_append(hilights, rec);
132 hilight_add_config(rec);
134 hilight_init_rec(rec);
136 signal_emit("hilight created", 1, rec);
139 void hilight_remove(HILIGHT_REC *rec)
141 g_return_if_fail(rec != NULL);
143 hilight_remove_config(rec);
144 hilights = g_slist_remove(hilights, rec);
146 signal_emit("hilight destroyed", 1, rec);
147 hilight_destroy(rec);
150 static HILIGHT_REC *hilight_find(const char *text, char **channels)
155 g_return_val_if_fail(text != NULL, NULL);
157 for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
158 HILIGHT_REC *rec = tmp->data;
160 if (g_strcasecmp(rec->text, text) != 0)
163 if ((channels == NULL && rec->channels == NULL))
164 return rec; /* no channels - ok */
166 if (channels != NULL && strcmp(*channels, "*") == 0)
167 return rec; /* ignore channels */
169 if (channels == NULL || rec->channels == NULL)
170 continue; /* other doesn't have channels */
172 if (strarray_length(channels) != strarray_length(rec->channels))
173 continue; /* different amount of channels */
175 /* check that channels match */
176 for (chan = channels; *chan != NULL; chan++) {
177 if (strarray_find(rec->channels, *chan) == -1)
182 return rec; /* channels ok */
188 static int hilight_match_text(HILIGHT_REC *rec, const char *text,
189 int *match_beg, int *match_end)
195 regmatch_t rmatch[1];
197 if (rec->regexp_compiled &&
198 regexec(&rec->preg, text, 1, rmatch, 0) == 0) {
199 if (rmatch[0].rm_so > 0 &&
200 match_beg != NULL && match_end != NULL) {
201 *match_beg = rmatch[0].rm_so;
202 *match_end = rmatch[0].rm_eo;
208 match = rec->fullword ?
209 stristr_full(text, rec->text) :
210 stristr(text, rec->text);
212 if (match_beg != NULL && match_end != NULL) {
213 *match_beg = (int) (match-text);
214 *match_end = *match_beg + strlen(rec->text);
223 #define hilight_match_level(rec, level) \
224 (level & (((rec)->level != 0 ? rec->level : default_hilight_level)))
226 #define hilight_match_channel(rec, channel) \
227 ((rec)->channels == NULL || ((channel) != NULL && \
228 strarray_find((rec)->channels, (channel)) != -1))
230 HILIGHT_REC *hilight_match(SERVER_REC *server, const char *channel,
231 const char *nick, const char *address,
232 int level, const char *str,
233 int *match_beg, int *match_end)
236 CHANNEL_REC *chanrec;
239 g_return_val_if_fail(str != NULL, NULL);
241 if ((never_hilight_level & level) == level)
245 /* check nick mask hilights */
246 chanrec = channel_find(server, channel);
247 nickrec = chanrec == NULL ? NULL :
248 nicklist_find(chanrec, nick);
249 if (nickrec != NULL) {
252 if (nickrec->host == NULL)
253 nicklist_set_host(chanrec, nickrec, address);
255 rec = nickmatch_find(nickmatch, nickrec);
256 if (rec != NULL && hilight_match_level(rec, level))
261 for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
262 HILIGHT_REC *rec = tmp->data;
264 if (!rec->nickmask && hilight_match_level(rec, level) &&
265 hilight_match_channel(rec, channel) &&
266 hilight_match_text(rec, str, match_beg, match_end))
273 static char *hilight_get_act_color(HILIGHT_REC *rec)
275 g_return_val_if_fail(rec != NULL, NULL);
277 return g_strdup(rec->act_color != NULL ? rec->act_color :
278 rec->color != NULL ? rec->color :
279 settings_get_str("hilight_act_color"));
282 char *hilight_get_color(HILIGHT_REC *rec)
286 g_return_val_if_fail(rec != NULL, NULL);
288 color = rec->color != NULL ? rec->color :
289 settings_get_str("hilight_color");
291 return format_string_expand(color, NULL);
294 void hilight_update_text_dest(TEXT_DEST_REC *dest, HILIGHT_REC *rec)
296 dest->level |= MSGLEVEL_HILIGHT;
298 if (rec->priority > 0)
299 dest->hilight_priority = rec->priority;
301 g_free_and_null(dest->hilight_color);
302 if (rec->act_color != NULL && strcmp(rec->act_color, "%n") == 0)
303 dest->level |= MSGLEVEL_NO_ACT;
305 dest->hilight_color = hilight_get_act_color(rec);
308 static void hilight_print(int index, HILIGHT_REC *rec);
310 static void sig_print_text(TEXT_DEST_REC *dest, const char *text,
311 const char *stripped)
313 HILIGHT_REC *hilight;
314 char *color, *newstr;
315 int old_level, hilight_start, hilight_end, hilight_len;
318 if (dest->level & MSGLEVEL_NOHILIGHT)
321 hilight_start = hilight_end = 0;
322 hilight = hilight_match(dest->server, dest->target,
323 NULL, NULL, dest->level, stripped,
329 nick_match = hilight->nick && (dest->level & (MSGLEVEL_PUBLIC|MSGLEVEL_ACTIONS)) == MSGLEVEL_PUBLIC;
331 old_level = dest->level;
332 if (!nick_match || (dest->level & MSGLEVEL_HILIGHT)) {
333 /* update the level / hilight info */
334 hilight_update_text_dest(dest, hilight);
338 return; /* fe-messages.c should have taken care of this */
340 if (old_level & MSGLEVEL_HILIGHT) {
341 /* nick is highlighted, just set priority */
345 color = hilight_get_color(hilight);
346 hilight_len = hilight_end-hilight_start;
348 if (!hilight->word) {
349 /* hilight whole line */
350 char *tmp = strip_codes(text);
351 newstr = g_strconcat(color, tmp, NULL);
354 /* hilight part of the line */
356 char *middle, *lastcolor;
357 int pos, color_pos, color_len;
359 tmp = g_string_new(NULL);
361 /* start of the line */
362 pos = strip_real_length(text, hilight_start, NULL, NULL);
363 g_string_append(tmp, text);
364 g_string_truncate(tmp, pos);
367 g_string_append(tmp, color);
369 /* middle of the line, stripped */
370 middle = strip_codes(text+pos);
372 g_string_append(tmp, middle);
373 g_string_truncate(tmp, pos+hilight_len);
376 /* end of the line */
377 pos = strip_real_length(text, hilight_end,
378 &color_pos, &color_len);
380 lastcolor = g_strndup(text+color_pos, color_len);
382 /* no colors in line, change back to default */
383 lastcolor = g_malloc0(3);
385 lastcolor[1] = FORMAT_STYLE_DEFAULTS;
387 g_string_append(tmp, lastcolor);
388 g_string_append(tmp, text+pos);
392 g_string_free(tmp, FALSE);
395 signal_emit("print text", 3, dest, newstr, stripped);
403 HILIGHT_REC *hilight_match_nick(SERVER_REC *server, const char *channel,
404 const char *nick, const char *address,
405 int level, const char *msg)
409 rec = hilight_match(server, channel, nick, address,
410 level, msg, NULL, NULL);
411 return (rec == NULL || !rec->nick) ? NULL : rec;
414 static void read_hilight_config(void)
421 hilights_destroy_all();
423 node = iconfig_node_traverse("hilights", FALSE);
429 tmp = config_node_first(node->value);
430 for (; tmp != NULL; tmp = config_node_next(tmp)) {
433 if (node->type != NODE_TYPE_BLOCK)
436 text = config_node_get_str(node, "text", NULL);
437 if (text == NULL || *text == '\0')
440 rec = g_new0(HILIGHT_REC, 1);
441 hilights = g_slist_append(hilights, rec);
443 rec->text = g_strdup(text);
445 color = config_node_get_str(node, "color", NULL);
446 rec->color = color == NULL || *color == '\0' ? NULL :
449 color = config_node_get_str(node, "act_color", NULL);
450 rec->act_color = color == NULL || *color == '\0' ? NULL :
453 rec->level = config_node_get_int(node, "level", 0);
454 rec->priority = config_node_get_int(node, "priority", 0);
455 rec->nick = config_node_get_bool(node, "nick", TRUE);
456 rec->word = config_node_get_bool(node, "word", TRUE);
458 rec->nickmask = config_node_get_bool(node, "mask", FALSE);
459 rec->fullword = config_node_get_bool(node, "fullword", FALSE);
460 rec->regexp = config_node_get_bool(node, "regexp", FALSE);
462 hilight_init_rec(rec);
464 node = config_node_section(node, "channels", -1);
465 if (node != NULL) rec->channels = config_node_get_list(node);
471 static void hilight_print(int index, HILIGHT_REC *rec)
473 char *chans, *levelstr;
476 options = g_string_new(NULL);
477 if (!rec->nick || !rec->word) {
478 if (rec->nick) g_string_append(options, "-nick ");
479 if (rec->word) g_string_append(options, "-word ");
482 if (rec->nickmask) g_string_append(options, "-nickmask ");
483 if (rec->fullword) g_string_append(options, "-fullword ");
485 g_string_append(options, "-regexp ");
487 if (!rec->regexp_compiled)
488 g_string_append(options, "[INVALID!] ");
492 if (rec->priority != 0)
493 g_string_sprintfa(options, "-priority %d ", rec->priority);
494 if (rec->color != NULL)
495 g_string_sprintfa(options, "-color %s ", rec->color);
496 if (rec->act_color != NULL)
497 g_string_sprintfa(options, "-actcolor %s ", rec->act_color);
499 chans = rec->channels == NULL ? NULL :
500 g_strjoinv(",", rec->channels);
501 levelstr = rec->level == 0 ? NULL :
502 bits2level(rec->level);
503 if (levelstr != NULL)
504 levelstr = g_strconcat(levelstr, " ", NULL);
505 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
506 TXT_HILIGHT_LINE, index, rec->text,
507 chans != NULL ? chans : "",
508 levelstr != NULL ? levelstr : "",
510 g_free_not_null(chans);
511 g_free_not_null(levelstr);
512 g_string_free(options, TRUE);
515 static void cmd_hilight_show(void)
520 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_HILIGHT_HEADER);
522 for (tmp = hilights; tmp != NULL; tmp = tmp->next, index++) {
523 HILIGHT_REC *rec = tmp->data;
525 hilight_print(index, rec);
527 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_HILIGHT_FOOTER);
530 /* SYNTAX: HILIGHT [-nick | -word | -line] [-mask | -full | -regexp]
531 [-color <color>] [-actcolor <color>] [-level <level>]
532 [-channels <channels>] <text> */
533 static void cmd_hilight(const char *data)
537 char *colorarg, *actcolorarg, *levelarg, *priorityarg, *chanarg, *text;
541 g_return_if_fail(data != NULL);
548 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
549 PARAM_FLAG_GETREST, "hilight", &optlist, &text))
552 chanarg = g_hash_table_lookup(optlist, "channels");
553 levelarg = g_hash_table_lookup(optlist, "level");
554 priorityarg = g_hash_table_lookup(optlist, "priority");
555 colorarg = g_hash_table_lookup(optlist, "color");
556 actcolorarg = g_hash_table_lookup(optlist, "actcolor");
558 if (*text == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
560 channels = (chanarg == NULL || *chanarg == '\0') ? NULL :
561 g_strsplit(replace_chars(chanarg, ',', ' '), " ", -1);
563 rec = hilight_find(text, channels);
565 rec = g_new0(HILIGHT_REC, 1);
567 /* default to nick/word hilighting */
571 rec->text = g_strdup(text);
572 rec->channels = channels;
574 g_strfreev(channels);
577 rec->level = (levelarg == NULL || *levelarg == '\0') ? 0 :
578 level2bits(replace_chars(levelarg, ',', ' '));
579 rec->priority = priorityarg == NULL ? 0 : atoi(priorityarg);
581 if (g_hash_table_lookup(optlist, "line") != NULL) {
586 if (g_hash_table_lookup(optlist, "word") != NULL) {
591 if (g_hash_table_lookup(optlist, "nick") != NULL)
594 rec->nickmask = g_hash_table_lookup(optlist, "mask") != NULL;
595 rec->fullword = g_hash_table_lookup(optlist, "full") != NULL;
596 rec->regexp = g_hash_table_lookup(optlist, "regexp") != NULL;
598 if (colorarg != NULL) {
599 g_free_and_null(rec->color);
600 if (*colorarg != '\0')
601 rec->color = g_strdup(colorarg);
603 if (actcolorarg != NULL) {
604 g_free_and_null(rec->act_color);
605 if (*actcolorarg != '\0')
606 rec->act_color = g_strdup(actcolorarg);
611 hilight_print(g_slist_index(hilights, rec)+1, rec);
612 cmd_params_free(free_arg);
617 /* SYNTAX: DEHILIGHT <id>|<mask> */
618 static void cmd_dehilight(const char *data)
623 if (is_numeric(data, ' ')) {
624 /* with index number */
625 tmp = g_slist_nth(hilights, atoi(data)-1);
626 rec = tmp == NULL ? NULL : tmp->data;
629 char *chans[2] = { "*", NULL };
630 rec = hilight_find(data, chans);
634 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_HILIGHT_NOT_FOUND, data);
636 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_HILIGHT_REMOVED, rec->text);
642 static void hilight_nick_cache(GHashTable *list, CHANNEL_REC *channel,
650 if (nick->host == NULL)
651 return; /* don't check until host is known */
653 nickmask = g_strconcat(nick->nick, "!", nick->host, NULL);
655 best_match = 0; match = NULL;
656 for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
657 HILIGHT_REC *rec = tmp->data;
660 hilight_match_channel(rec, channel->name) &&
661 match_wildcards(rec->text, nickmask)) {
662 len = strlen(rec->text);
663 if (best_match < len) {
669 g_free_not_null(nickmask);
672 g_hash_table_insert(list, nick, match);
675 static void read_settings(void)
677 default_hilight_level = settings_get_level("hilight_level");
680 void hilight_text_init(void)
682 settings_add_str("lookandfeel", "hilight_color", "%Y");
683 settings_add_str("lookandfeel", "hilight_act_color", "%M");
684 settings_add_level("lookandfeel", "hilight_level", "PUBLIC DCCMSGS");
688 nickmatch = nickmatch_init(hilight_nick_cache);
689 read_hilight_config();
691 signal_add_first("print text", (SIGNAL_FUNC) sig_print_text);
692 signal_add("setup reread", (SIGNAL_FUNC) read_hilight_config);
693 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
695 command_bind("hilight", NULL, (SIGNAL_FUNC) cmd_hilight);
696 command_bind("dehilight", NULL, (SIGNAL_FUNC) cmd_dehilight);
697 command_set_options("hilight", "-color -actcolor -level -priority -channels nick word line mask full regexp");
700 void hilight_text_deinit(void)
702 hilights_destroy_all();
703 nickmatch_deinit(nickmatch);
705 signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
706 signal_remove("setup reread", (SIGNAL_FUNC) read_hilight_config);
707 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
709 command_unbind("hilight", (SIGNAL_FUNC) cmd_hilight);
710 command_unbind("dehilight", (SIGNAL_FUNC) cmd_dehilight);