New silcconfig library and server parser. Merged silc-newconfig-final.patch.
[runtime.git] / apps / irssi / src / fe-common / core / hilight-text.c
1 /*
2  hilight-text.c : irssi
3
4     Copyright (C) 1999-2000 Timo Sirainen
5
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.
10
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.
15
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
19 */
20
21 #include "module.h"
22 #include "module-formats.h"
23 #include "signals.h"
24 #include "commands.h"
25 #include "levels.h"
26 #include "misc.h"
27 #include "lib-config/iconfig.h"
28 #include "settings.h"
29
30 #include "servers.h"
31 #include "channels.h"
32 #include "nicklist.h"
33
34 #include "hilight-text.h"
35 #include "nickmatch-cache.h"
36 #include "printtext.h"
37 #include "formats.h"
38
39 static NICKMATCH_REC *nickmatch;
40 static int never_hilight_level, default_hilight_level;
41 GSList *hilights;
42
43 static void reset_cache(void)
44 {
45         GSList *tmp;
46
47         never_hilight_level = MSGLEVEL_ALL & ~default_hilight_level;
48         for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
49                 HILIGHT_REC *rec = tmp->data;
50
51                 if (never_hilight_level & rec->level)
52                         never_hilight_level &= ~rec->level;
53         }
54
55         nickmatch_rebuild(nickmatch);
56 }
57
58 static void hilight_add_config(HILIGHT_REC *rec)
59 {
60         CONFIG_NODE *node;
61
62         g_return_if_fail(rec != NULL);
63
64         node = iconfig_node_traverse("(hilights", TRUE);
65         node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
66
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);
77
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);
81         }
82 }
83
84 static void hilight_remove_config(HILIGHT_REC *rec)
85 {
86         CONFIG_NODE *node;
87
88         g_return_if_fail(rec != NULL);
89
90         node = iconfig_node_traverse("hilights", FALSE);
91         if (node != NULL) iconfig_node_list_remove(node, g_slist_index(hilights, rec));
92 }
93
94 static void hilight_destroy(HILIGHT_REC *rec)
95 {
96         g_return_if_fail(rec != NULL);
97
98 #ifdef HAVE_REGEX_H
99         if (rec->regexp_compiled) regfree(&rec->preg);
100 #endif
101         if (rec->channels != NULL) g_strfreev(rec->channels);
102         g_free_not_null(rec->color);
103         g_free_not_null(rec->act_color);
104         g_free(rec->text);
105         g_free(rec);
106 }
107
108 static void hilights_destroy_all(void)
109 {
110         g_slist_foreach(hilights, (GFunc) hilight_destroy, NULL);
111         g_slist_free(hilights);
112         hilights = NULL;
113 }
114
115 static void hilight_remove(HILIGHT_REC *rec)
116 {
117         g_return_if_fail(rec != NULL);
118
119         hilight_remove_config(rec);
120         hilights = g_slist_remove(hilights, rec);
121         hilight_destroy(rec);
122 }
123
124 static HILIGHT_REC *hilight_find(const char *text, char **channels)
125 {
126         GSList *tmp;
127         char **chan;
128
129         g_return_val_if_fail(text != NULL, NULL);
130
131         for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
132                 HILIGHT_REC *rec = tmp->data;
133
134                 if (g_strcasecmp(rec->text, text) != 0)
135                         continue;
136
137                 if ((channels == NULL && rec->channels == NULL))
138                         return rec; /* no channels - ok */
139
140                 if (channels != NULL && strcmp(*channels, "*") == 0)
141                         return rec; /* ignore channels */
142
143                 if (channels == NULL || rec->channels == NULL)
144                         continue; /* other doesn't have channels */
145
146                 if (strarray_length(channels) != strarray_length(rec->channels))
147                         continue; /* different amount of channels */
148
149                 /* check that channels match */
150                 for (chan = channels; *chan != NULL; chan++) {
151                         if (strarray_find(rec->channels, *chan) == -1)
152                                 break;
153                 }
154
155                 if (*chan == NULL)
156                         return rec; /* channels ok */
157         }
158
159         return NULL;
160 }
161
162 static int hilight_match_text(HILIGHT_REC *rec, const char *text,
163                               int *match_beg, int *match_end)
164 {
165         char *match;
166
167         if (rec->regexp) {
168 #ifdef HAVE_REGEX_H
169                 regmatch_t rmatch[1];
170
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;
177                         }
178                         return TRUE;
179                 }
180 #endif
181         } else {
182                 match = rec->fullword ?
183                         stristr_full(text, rec->text) :
184                         stristr(text, rec->text);
185                 if (match != NULL) {
186                         if (match_beg != NULL && match_end != NULL) {
187                                 *match_beg = (int) (match-text);
188                                 *match_end = *match_beg + strlen(rec->text);
189                         }
190                         return TRUE;
191                 }
192         }
193
194         return FALSE;
195 }
196
197 #define hilight_match_level(rec, level) \
198         (level & (((rec)->level != 0 ? rec->level : default_hilight_level)))
199
200 #define hilight_match_channel(rec, channel) \
201         ((rec)->channels == NULL || ((channel) != NULL && \
202                 strarray_find((rec)->channels, (channel)) != -1))
203
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)
208 {
209         GSList *tmp;
210         CHANNEL_REC *chanrec;
211         NICK_REC *nickrec;
212
213         g_return_val_if_fail(str != NULL, NULL);
214
215         if ((never_hilight_level & level) == level)
216                 return NULL;
217
218         if (nick != NULL) {
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) {
224                         HILIGHT_REC *rec;
225
226                         if (nickrec->host == NULL)
227                                 nicklist_set_host(chanrec, nickrec, address);
228
229                         rec = nickmatch_find(nickmatch, nickrec);
230                         if (rec != NULL && hilight_match_level(rec, level))
231                                 return rec;
232                 }
233         }
234
235         for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
236                 HILIGHT_REC *rec = tmp->data;
237
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))
241                         return rec;
242         }
243
244         return NULL;
245 }
246
247 static char *hilight_get_act_color(HILIGHT_REC *rec)
248 {
249         g_return_val_if_fail(rec != NULL, NULL);
250
251         return g_strdup(rec->act_color != NULL ? rec->act_color :
252                         rec->color != NULL ? rec->color :
253                         settings_get_str("hilight_act_color"));
254 }
255
256 static char *hilight_get_color(HILIGHT_REC *rec)
257 {
258         const char *color;
259
260         g_return_val_if_fail(rec != NULL, NULL);
261
262         color = rec->color != NULL ? rec->color :
263                 settings_get_str("hilight_color");
264
265         return format_string_expand(color, NULL);
266 }
267
268 static void hilight_update_text_dest(TEXT_DEST_REC *dest, HILIGHT_REC *rec)
269 {
270         dest->level |= MSGLEVEL_HILIGHT;
271
272         if (rec->priority > 0)
273                 dest->hilight_priority = rec->priority;
274
275         g_free_not_null(dest->hilight_color);
276         dest->hilight_color = hilight_get_act_color(rec);
277 }
278
279 static void sig_print_text(TEXT_DEST_REC *dest, const char *text,
280                            const char *stripped)
281 {
282         HILIGHT_REC *hilight;
283         char *color, *newstr;
284         int old_level, hilight_start, hilight_end, hilight_len;
285         int nick_match;
286
287         if (dest->level & MSGLEVEL_NOHILIGHT)
288                 return;
289
290         hilight_start = hilight_end = 0;
291         hilight = hilight_match(dest->server, dest->target,
292                                 NULL, NULL, dest->level, stripped,
293                                 &hilight_start,
294                                 &hilight_end);
295         if (hilight == NULL)
296                 return;
297
298         nick_match = hilight->nick && (dest->level & (MSGLEVEL_PUBLIC|MSGLEVEL_ACTIONS)) == MSGLEVEL_PUBLIC;
299
300         old_level = dest->level;
301         if (!nick_match || (dest->level & MSGLEVEL_HILIGHT)) {
302                 /* update the level / hilight info */
303                 hilight_update_text_dest(dest, hilight);
304         }
305
306         if (nick_match)
307                 return; /* fe-messages.c should have taken care of this */
308
309         if (old_level & MSGLEVEL_HILIGHT) {
310                 /* nick is highlighted, just set priority */
311                 return;
312         }
313
314         color = hilight_get_color(hilight);
315         hilight_len = hilight_end-hilight_start;
316
317         if (!hilight->word) {
318                 /* hilight whole line */
319                 char *tmp = strip_codes(text);
320                 newstr = g_strconcat(color, tmp, NULL);
321                 g_free(tmp);
322         } else {
323                 /* hilight part of the line */
324                 GString *tmp;
325                 char *middle, *lastcolor;
326                 int pos, color_pos, color_len;
327
328                 tmp = g_string_new(NULL);
329
330                 /* start of the line */
331                 pos = strip_real_length(text, hilight_start, NULL, NULL);
332                 g_string_append(tmp, text);
333                 g_string_truncate(tmp, pos);
334
335                 /* color */
336                 g_string_append(tmp, color);
337
338                 /* middle of the line, stripped */
339                 middle = strip_codes(text+pos);
340                 pos = tmp->len;
341                 g_string_append(tmp, middle);
342                 g_string_truncate(tmp, pos+hilight_len);
343                 g_free(middle);
344
345                 /* end of the line */
346                 pos = strip_real_length(text, hilight_end,
347                                         &color_pos, &color_len);
348                 if (color_pos > 0)
349                         lastcolor = g_strndup(text+color_pos, color_len);
350                 else {
351                         /* no colors in line, change back to default */
352                         lastcolor = g_malloc0(3);
353                         lastcolor[0] = 4;
354                         lastcolor[1] = FORMAT_STYLE_DEFAULTS;
355                 }
356                 g_string_append(tmp, lastcolor);
357                 g_string_append(tmp, text+pos);
358                 g_free(lastcolor);
359
360                 newstr = tmp->str;
361                 g_string_free(tmp, FALSE);
362         }
363
364         signal_emit("print text", 3, dest, newstr, stripped);
365
366         g_free(color);
367         g_free(newstr);
368
369         signal_stop();
370 }
371
372 char *hilight_match_nick(SERVER_REC *server, const char *channel,
373                          const char *nick, const char *address,
374                          int level, const char *msg)
375 {
376         HILIGHT_REC *rec;
377         char *color;
378
379         rec = hilight_match(server, channel, nick, address,
380                             level, msg, NULL, NULL);
381         color = rec == NULL || !rec->nick ? NULL :
382                 hilight_get_color(rec);
383
384         return color;
385 }
386
387 static void read_hilight_config(void)
388 {
389         CONFIG_NODE *node;
390         HILIGHT_REC *rec;
391         GSList *tmp;
392         char *text, *color;
393
394         hilights_destroy_all();
395
396         node = iconfig_node_traverse("hilights", FALSE);
397         if (node == NULL) {
398                 reset_cache();
399                 return;
400         }
401
402         tmp = config_node_first(node->value);
403         for (; tmp != NULL; tmp = config_node_next(tmp)) {
404                 node = tmp->data;
405
406                 if (node->type != NODE_TYPE_BLOCK)
407                         continue;
408
409                 text = config_node_get_str(node, "text", NULL);
410                 if (text == NULL || *text == '\0')
411                         continue;
412
413                 rec = g_new0(HILIGHT_REC, 1);
414                 hilights = g_slist_append(hilights, rec);
415
416                 rec->text = g_strdup(text);
417
418                 color = config_node_get_str(node, "color", NULL);
419                 rec->color = color == NULL || *color == '\0' ? NULL :
420                         g_strdup(color);
421
422                 color = config_node_get_str(node, "act_color", NULL);
423                 rec->act_color = color == NULL || *color == '\0' ? NULL :
424                         g_strdup(color);
425
426                 rec->level = config_node_get_int(node, "level", 0);
427                 rec->priority = config_node_get_int(node, "priority", 0);
428                 rec->nick = config_node_get_bool(node, "nick", TRUE);
429                 rec->word = config_node_get_bool(node, "word", TRUE);
430
431                 rec->nickmask = config_node_get_bool(node, "mask", FALSE);
432                 rec->fullword = config_node_get_bool(node, "fullword", FALSE);
433                 rec->regexp = config_node_get_bool(node, "regexp", FALSE);
434
435 #ifdef HAVE_REGEX_H
436                 rec->regexp_compiled = !rec->regexp ? FALSE :
437                         regcomp(&rec->preg, rec->text,
438                                 REG_EXTENDED|REG_ICASE) == 0;
439 #endif
440
441                 node = config_node_section(node, "channels", -1);
442                 if (node != NULL) rec->channels = config_node_get_list(node);
443         }
444
445         reset_cache();
446 }
447
448 static void hilight_print(int index, HILIGHT_REC *rec)
449 {
450         char *chans, *levelstr;
451
452         chans = rec->channels == NULL ? NULL :
453                 g_strjoinv(",", rec->channels);
454         levelstr = rec->level == 0 ? NULL :
455                 bits2level(rec->level);
456         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
457                     TXT_HILIGHT_LINE, index, rec->text,
458                     chans != NULL ? chans : "",
459                     levelstr != NULL ? levelstr : "",
460                     rec->nickmask ? " -mask" : "",
461                     rec->fullword ? " -full" : "",
462                     rec->regexp ? " -regexp" : "");
463         g_free_not_null(chans);
464         g_free_not_null(levelstr);
465 }
466
467 static void cmd_hilight_show(void)
468 {
469         GSList *tmp;
470         int index;
471
472         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_HILIGHT_HEADER);
473         index = 1;
474         for (tmp = hilights; tmp != NULL; tmp = tmp->next, index++) {
475                 HILIGHT_REC *rec = tmp->data;
476
477                 hilight_print(index, rec);
478         }
479         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_HILIGHT_FOOTER);
480 }
481
482 /* SYNTAX: HILIGHT [-nick | -word | -line] [-mask | -full | -regexp]
483                    [-color <color>] [-actcolor <color>] [-level <level>]
484                    [-channels <channels>] <text> */
485 static void cmd_hilight(const char *data)
486 {
487         GHashTable *optlist;
488         HILIGHT_REC *rec;
489         char *colorarg, *actcolorarg, *levelarg, *priorityarg, *chanarg, *text;
490         char **channels;
491         void *free_arg;
492
493         g_return_if_fail(data != NULL);
494
495         if (*data == '\0') {
496                 cmd_hilight_show();
497                 return;
498         }
499
500         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
501                             PARAM_FLAG_GETREST, "hilight", &optlist, &text))
502                 return;
503
504         chanarg = g_hash_table_lookup(optlist, "channels");
505         levelarg = g_hash_table_lookup(optlist, "level");
506         priorityarg = g_hash_table_lookup(optlist, "priority");
507         colorarg = g_hash_table_lookup(optlist, "color");
508         actcolorarg = g_hash_table_lookup(optlist, "actcolor");
509
510         if (*text == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
511
512         channels = (chanarg == NULL || *chanarg == '\0') ? NULL :
513                 g_strsplit(replace_chars(chanarg, ',', ' '), " ", -1);
514
515         rec = hilight_find(text, channels);
516         if (rec == NULL) {
517                 rec = g_new0(HILIGHT_REC, 1);
518
519                 /* default to nick/word hilighting */
520                 rec->nick = TRUE;
521                 rec->word = TRUE;
522
523                 rec->text = g_strdup(text);
524                 rec->channels = channels;
525         } else {
526                 g_strfreev(channels);
527
528                 hilight_remove_config(rec);
529                 hilights = g_slist_remove(hilights, rec);
530         }
531
532         rec->level = (levelarg == NULL || *levelarg == '\0') ? 0 :
533                 level2bits(replace_chars(levelarg, ',', ' '));
534         rec->priority = priorityarg == NULL ? 0 : atoi(priorityarg);
535
536         if (g_hash_table_lookup(optlist, "line") != NULL) {
537                 rec->word = FALSE;
538                 rec->nick = FALSE;
539         }
540
541         if (g_hash_table_lookup(optlist, "word") != NULL) {
542                 rec->word = TRUE;
543                 rec->nick = FALSE;
544         }
545
546         if (g_hash_table_lookup(optlist, "nick") != NULL)
547                 rec->nick = TRUE;
548
549         rec->nickmask = g_hash_table_lookup(optlist, "mask") != NULL;
550         rec->fullword = g_hash_table_lookup(optlist, "full") != NULL;
551         rec->regexp = g_hash_table_lookup(optlist, "regexp") != NULL;
552
553         if (colorarg != NULL) {
554                 g_free_and_null(rec->color);
555                 if (*colorarg != '\0')
556                         rec->color = g_strdup(colorarg);
557         }
558         if (actcolorarg != NULL) {
559                 g_free_and_null(rec->act_color);
560                 if (*actcolorarg != '\0')
561                         rec->act_color = g_strdup(actcolorarg);
562         }
563
564 #ifdef HAVE_REGEX_H
565         if (rec->regexp_compiled)
566                 regfree(&rec->preg);
567         rec->regexp_compiled = !rec->regexp ? FALSE :
568                 regcomp(&rec->preg, rec->text, REG_EXTENDED|REG_ICASE) == 0;
569 #endif
570
571         hilights = g_slist_append(hilights, rec);
572         hilight_add_config(rec);
573
574         hilight_print(g_slist_index(hilights, rec)+1, rec);
575         cmd_params_free(free_arg);
576
577         reset_cache();
578 }
579
580 /* SYNTAX: DEHILIGHT <id>|<mask> */
581 static void cmd_dehilight(const char *data)
582 {
583         HILIGHT_REC *rec;
584         GSList *tmp;
585
586         if (is_numeric(data, ' ')) {
587                 /* with index number */
588                 tmp = g_slist_nth(hilights, atoi(data)-1);
589                 rec = tmp == NULL ? NULL : tmp->data;
590         } else {
591                 /* with mask */
592                 char *chans[2] = { "*", NULL };
593                 rec = hilight_find(data, chans);
594         }
595
596         if (rec == NULL)
597                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_HILIGHT_NOT_FOUND, data);
598         else {
599                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_HILIGHT_REMOVED, rec->text);
600                 hilight_remove(rec);
601                 reset_cache();
602         }
603 }
604
605 static void hilight_nick_cache(GHashTable *list, CHANNEL_REC *channel,
606                                NICK_REC *nick)
607 {
608         GSList *tmp;
609         HILIGHT_REC *match;
610         char *nickmask;
611         int len, best_match;
612
613         if (nick->host == NULL)
614                 return; /* don't check until host is known */
615
616         nickmask = g_strconcat(nick->nick, "!", nick->host, NULL);
617
618         best_match = 0; match = NULL;
619         for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
620                 HILIGHT_REC *rec = tmp->data;
621
622                 if (rec->nickmask &&
623                     hilight_match_channel(rec, channel->name) &&
624                     match_wildcards(rec->text, nickmask)) {
625                         len = strlen(rec->text);
626                         if (best_match < len) {
627                                 best_match = len;
628                                 match = rec;
629                         }
630                 }
631         }
632         g_free_not_null(nickmask);
633
634         if (match != NULL)
635                 g_hash_table_insert(list, nick, match);
636 }
637
638 static void read_settings(void)
639 {
640         default_hilight_level = level2bits(settings_get_str("hilight_level"));
641 }
642
643 void hilight_text_init(void)
644 {
645         settings_add_str("lookandfeel", "hilight_color", "%Y");
646         settings_add_str("lookandfeel", "hilight_act_color", "%M");
647         settings_add_str("lookandfeel", "hilight_level", "PUBLIC DCCMSGS");
648
649         read_settings();
650
651         nickmatch = nickmatch_init(hilight_nick_cache);
652         read_hilight_config();
653
654         signal_add_first("print text", (SIGNAL_FUNC) sig_print_text);
655         signal_add("setup reread", (SIGNAL_FUNC) read_hilight_config);
656         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
657
658         command_bind("hilight", NULL, (SIGNAL_FUNC) cmd_hilight);
659         command_bind("dehilight", NULL, (SIGNAL_FUNC) cmd_dehilight);
660         command_set_options("hilight", "-color -actcolor -level -priority -channels nick word line mask full regexp");
661 }
662
663 void hilight_text_deinit(void)
664 {
665         hilights_destroy_all();
666         nickmatch_deinit(nickmatch);
667
668         signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
669         signal_remove("setup reread", (SIGNAL_FUNC) read_hilight_config);
670         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
671
672         command_unbind("hilight", (SIGNAL_FUNC) cmd_hilight);
673         command_unbind("dehilight", (SIGNAL_FUNC) cmd_dehilight);
674 }