2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
hilight-text.c : irssi
|
|
|
|
|
|
|
|
Copyright (C) 1999-2000 Timo Sirainen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "module-formats.h"
|
|
|
|
#include "signals.h"
|
|
|
|
#include "commands.h"
|
2001-01-20 23:14:24 -05:00
|
|
|
#include "levels.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "misc.h"
|
|
|
|
#include "lib-config/iconfig.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
#include "servers.h"
|
2001-01-20 23:14:24 -05:00
|
|
|
#include "channels.h"
|
|
|
|
#include "nicklist.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
#include "hilight-text.h"
|
2001-01-20 23:14:24 -05:00
|
|
|
#include "nickmatch-cache.h"
|
2000-10-28 16:14:19 -04:00
|
|
|
#include "printtext.h"
|
2000-11-12 15:20:05 -05:00
|
|
|
#include "formats.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
static NICKMATCH_REC *nickmatch;
|
|
|
|
static HILIGHT_REC *next_nick_hilight, *next_line_hilight;
|
|
|
|
static int next_hilight_start, next_hilight_end;
|
|
|
|
static int never_hilight_level, default_hilight_level;
|
2000-04-26 04:03:38 -04:00
|
|
|
GSList *hilights;
|
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
static void reset_cache(void)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
never_hilight_level = MSGLEVEL_ALL & ~default_hilight_level;
|
|
|
|
for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
|
|
|
|
HILIGHT_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (never_hilight_level & rec->level)
|
|
|
|
never_hilight_level &= ~rec->level;
|
|
|
|
}
|
|
|
|
|
|
|
|
nickmatch_rebuild(nickmatch);
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
static void hilight_add_config(HILIGHT_REC *rec)
|
|
|
|
{
|
|
|
|
CONFIG_NODE *node;
|
|
|
|
|
2000-05-09 09:14:07 -04:00
|
|
|
g_return_if_fail(rec != NULL);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
node = iconfig_node_traverse("(hilights", TRUE);
|
|
|
|
node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
|
|
|
|
|
2000-05-10 09:57:42 -04:00
|
|
|
iconfig_node_set_str(node, "text", rec->text);
|
2000-11-26 05:24:30 -05:00
|
|
|
if (rec->level > 0) iconfig_node_set_int(node, "level", rec->level);
|
2000-05-10 09:57:42 -04:00
|
|
|
if (rec->color) iconfig_node_set_str(node, "color", rec->color);
|
2001-01-20 23:14:24 -05:00
|
|
|
if (rec->act_color) iconfig_node_set_str(node, "act_color", rec->act_color);
|
|
|
|
if (rec->priority > 0) iconfig_node_set_int(node, "priority", rec->priority);
|
|
|
|
iconfig_node_set_bool(node, "nick", rec->nick);
|
|
|
|
iconfig_node_set_bool(node, "word", rec->word);
|
2000-11-26 05:24:30 -05:00
|
|
|
if (rec->nickmask) iconfig_node_set_bool(node, "mask", TRUE);
|
|
|
|
if (rec->fullword) iconfig_node_set_bool(node, "fullword", TRUE);
|
|
|
|
if (rec->regexp) iconfig_node_set_bool(node, "regexp", TRUE);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
if (rec->channels != NULL && *rec->channels != NULL) {
|
|
|
|
node = config_node_section(node, "channels", NODE_TYPE_LIST);
|
2000-11-26 05:24:30 -05:00
|
|
|
iconfig_node_add_list(node, rec->channels);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hilight_remove_config(HILIGHT_REC *rec)
|
|
|
|
{
|
|
|
|
CONFIG_NODE *node;
|
|
|
|
|
2000-05-09 09:14:07 -04:00
|
|
|
g_return_if_fail(rec != NULL);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
node = iconfig_node_traverse("hilights", FALSE);
|
2000-05-10 09:57:42 -04:00
|
|
|
if (node != NULL) iconfig_node_list_remove(node, g_slist_index(hilights, rec));
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void hilight_destroy(HILIGHT_REC *rec)
|
|
|
|
{
|
2000-05-09 09:14:07 -04:00
|
|
|
g_return_if_fail(rec != NULL);
|
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
#ifdef HAVE_REGEX_H
|
|
|
|
if (rec->regexp_compiled) regfree(&rec->preg);
|
|
|
|
#endif
|
|
|
|
if (rec->channels != NULL) g_strfreev(rec->channels);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free_not_null(rec->color);
|
2001-01-20 23:14:24 -05:00
|
|
|
g_free_not_null(rec->act_color);
|
|
|
|
g_free(rec->text);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free(rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hilights_destroy_all(void)
|
|
|
|
{
|
|
|
|
g_slist_foreach(hilights, (GFunc) hilight_destroy, NULL);
|
|
|
|
g_slist_free(hilights);
|
|
|
|
hilights = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hilight_remove(HILIGHT_REC *rec)
|
|
|
|
{
|
2000-05-09 09:14:07 -04:00
|
|
|
g_return_if_fail(rec != NULL);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
hilight_remove_config(rec);
|
|
|
|
hilights = g_slist_remove(hilights, rec);
|
|
|
|
hilight_destroy(rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HILIGHT_REC *hilight_find(const char *text, char **channels)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
char **chan;
|
|
|
|
|
|
|
|
g_return_val_if_fail(text != NULL, NULL);
|
|
|
|
|
|
|
|
for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
|
|
|
|
HILIGHT_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (g_strcasecmp(rec->text, text) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((channels == NULL && rec->channels == NULL))
|
|
|
|
return rec; /* no channels - ok */
|
|
|
|
|
|
|
|
if (channels != NULL && strcmp(*channels, "*") == 0)
|
|
|
|
return rec; /* ignore channels */
|
|
|
|
|
|
|
|
if (channels == NULL || rec->channels == NULL)
|
|
|
|
continue; /* other doesn't have channels */
|
|
|
|
|
|
|
|
if (strarray_length(channels) != strarray_length(rec->channels))
|
|
|
|
continue; /* different amount of channels */
|
|
|
|
|
|
|
|
/* check that channels match */
|
|
|
|
for (chan = channels; *chan != NULL; chan++) {
|
|
|
|
if (strarray_find(rec->channels, *chan) == -1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*chan == NULL)
|
|
|
|
return rec; /* channels ok */
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-06-30 15:50:56 -04:00
|
|
|
/* color name -> mirc color number */
|
|
|
|
static int mirc_color_name(const char *name)
|
|
|
|
{
|
|
|
|
static const char *names[] = {
|
|
|
|
"bla dbla", /* black */
|
|
|
|
"blu dblu", /* blue */
|
|
|
|
"gree dgree", /* green */
|
|
|
|
"r dr br lr", /* red .. um.. only one of them. */
|
|
|
|
"br dbr dy", /* brown / dark yello */
|
|
|
|
"m p dm dp", /* magenta / purple */
|
|
|
|
"o", /* orange */
|
|
|
|
"y by", /* yellow */
|
|
|
|
"bg lg", /* bright green */
|
|
|
|
"c dc", /* cyan */
|
|
|
|
"bc lc", /* bright cyan */
|
|
|
|
"bb lb", /* bright blue */
|
|
|
|
"bm bp lm lp", /* bright magenta/purple */
|
|
|
|
"dgray dgrey", /* dark grey */
|
|
|
|
"grey gray", /* grey */
|
|
|
|
"w", /* white */
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *p, *pname;
|
|
|
|
int n, ok;
|
|
|
|
|
|
|
|
for (n = 0; names[n] != NULL; n++) {
|
|
|
|
pname = name; ok = TRUE;
|
|
|
|
for (p = names[n]; ; p++) {
|
|
|
|
if (*p == ' ' || *p == '\0') {
|
|
|
|
if (ok) return n+1;
|
|
|
|
if (*p == '\0') break;
|
|
|
|
|
|
|
|
ok = TRUE;
|
|
|
|
pname = name;
|
|
|
|
} else if (toupper((int) *p) == toupper((int) *pname))
|
|
|
|
pname++;
|
|
|
|
else
|
|
|
|
ok = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
static int hilight_match_text(HILIGHT_REC *rec, const char *text,
|
|
|
|
int *match_beg, int *match_end)
|
|
|
|
{
|
|
|
|
char *match;
|
|
|
|
|
|
|
|
if (rec->regexp) {
|
|
|
|
#ifdef HAVE_REGEX_H
|
|
|
|
regmatch_t rmatch[1];
|
|
|
|
|
|
|
|
if (rec->regexp_compiled &&
|
|
|
|
regexec(&rec->preg, text, 1, rmatch, 0) == 0) {
|
|
|
|
if (rmatch[0].rm_so > 0 &&
|
|
|
|
match_beg != NULL && match_end != NULL) {
|
|
|
|
*match_beg = rmatch[0].rm_so;
|
|
|
|
*match_end = rmatch[0].rm_eo;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
match = rec->fullword ?
|
|
|
|
stristr_full(text, rec->text) :
|
|
|
|
stristr(text, rec->text);
|
|
|
|
if (match != NULL) {
|
|
|
|
if (match_beg != NULL && match_end != NULL) {
|
|
|
|
*match_beg = (int) (match-text);
|
|
|
|
*match_end = *match_beg + strlen(rec->text);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define hilight_match_level(rec, level) \
|
|
|
|
(level & (((rec)->level != 0 ? rec->level : default_hilight_level)))
|
|
|
|
|
|
|
|
#define hilight_match_channel(rec, channel) \
|
|
|
|
((rec)->channels == NULL || ((channel) != NULL && \
|
|
|
|
strarray_find((rec)->channels, (channel)) != -1))
|
|
|
|
|
|
|
|
HILIGHT_REC *hilight_match(SERVER_REC *server, const char *channel,
|
|
|
|
const char *nick, const char *address,
|
|
|
|
int level, const char *str,
|
|
|
|
int *match_beg, int *match_end)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
GSList *tmp;
|
2001-01-20 23:14:24 -05:00
|
|
|
CHANNEL_REC *chanrec;
|
|
|
|
NICK_REC *nickrec;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-06-28 17:47:13 -04:00
|
|
|
g_return_val_if_fail(str != NULL, NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
if ((never_hilight_level & level) == level)
|
|
|
|
return NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
if (nick != NULL) {
|
|
|
|
/* check nick mask hilights */
|
|
|
|
chanrec = channel_find(server, channel);
|
|
|
|
nickrec = chanrec == NULL ? NULL :
|
|
|
|
nicklist_find(chanrec, nick);
|
|
|
|
if (nickrec != NULL) {
|
|
|
|
HILIGHT_REC *rec;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
if (nickrec->host == NULL)
|
|
|
|
nicklist_set_host(chanrec, nickrec, address);
|
|
|
|
|
|
|
|
rec = nickmatch_find(nickmatch, nickrec);
|
|
|
|
if (rec != NULL && hilight_match_level(rec, level))
|
|
|
|
return rec;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
|
|
|
|
HILIGHT_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (!rec->nickmask && hilight_match_level(rec, level) &&
|
|
|
|
hilight_match_channel(rec, channel) &&
|
|
|
|
hilight_match_text(rec, str, match_beg, match_end))
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2001-01-09 16:06:45 -05:00
|
|
|
}
|
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
char *hilight_get_color(HILIGHT_REC *rec, int activity)
|
2001-01-09 16:06:45 -05:00
|
|
|
{
|
2001-01-20 23:14:24 -05:00
|
|
|
const char *color;
|
2001-01-09 16:06:45 -05:00
|
|
|
char number[MAX_INT_STRLEN];
|
|
|
|
int colornum;
|
|
|
|
|
|
|
|
g_return_val_if_fail(rec != NULL, NULL);
|
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
color = activity && rec->act_color != NULL ?
|
|
|
|
rec->act_color : rec->color;
|
2001-01-09 16:06:45 -05:00
|
|
|
if (color == NULL)
|
|
|
|
color = settings_get_str("hilight_color");
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-06-30 15:50:56 -04:00
|
|
|
if (isalpha((int) *color)) {
|
|
|
|
/* color was specified with it's name - try to convert it */
|
|
|
|
colornum = mirc_color_name(color);
|
|
|
|
if (colornum <= 0) colornum = 16;
|
|
|
|
|
|
|
|
ltoa(number, colornum);
|
|
|
|
color = number;
|
|
|
|
}
|
2001-01-09 16:06:45 -05:00
|
|
|
|
2000-06-28 17:47:13 -04:00
|
|
|
return g_strconcat(isdigit(*color) ? "\003" : "", color, NULL);
|
|
|
|
}
|
2000-06-27 15:11:25 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
static void hilight_update_text_dest(TEXT_DEST_REC *dest, HILIGHT_REC *rec)
|
|
|
|
{
|
|
|
|
char *color;
|
|
|
|
|
|
|
|
dest->level |= MSGLEVEL_HILIGHT;
|
|
|
|
|
|
|
|
if (rec->priority > 0)
|
|
|
|
dest->hilight_priority = rec->priority;
|
|
|
|
|
|
|
|
color = hilight_get_color(rec, TRUE);
|
|
|
|
if (*color == 3)
|
|
|
|
dest->hilight_color = atoi(color+1);
|
|
|
|
g_free(color);
|
|
|
|
}
|
|
|
|
|
2000-11-12 15:20:05 -05:00
|
|
|
static void sig_print_text_stripped(TEXT_DEST_REC *dest, const char *str)
|
2000-06-28 17:47:13 -04:00
|
|
|
{
|
2001-01-09 16:06:45 -05:00
|
|
|
HILIGHT_REC *hilight;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-06-28 17:47:13 -04:00
|
|
|
g_return_if_fail(str != NULL);
|
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
if (next_nick_hilight != NULL) {
|
|
|
|
if (!next_nick_hilight->nick) {
|
|
|
|
/* non-nick hilight wanted */
|
|
|
|
hilight = next_nick_hilight;
|
|
|
|
next_nick_hilight = NULL;
|
|
|
|
if (!hilight_match_text(hilight, str,
|
|
|
|
&next_hilight_start,
|
|
|
|
&next_hilight_end)) {
|
|
|
|
next_hilight_start = 0;
|
|
|
|
next_hilight_end = strlen(str);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* nick is highlighted, just set priority */
|
|
|
|
hilight_update_text_dest(dest, next_nick_hilight);
|
|
|
|
next_nick_hilight = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (dest->level & (MSGLEVEL_NOHILIGHT|MSGLEVEL_HILIGHT))
|
|
|
|
return;
|
|
|
|
|
|
|
|
hilight = hilight_match(dest->server, dest->target,
|
|
|
|
NULL, NULL, dest->level, str,
|
|
|
|
&next_hilight_start,
|
|
|
|
&next_hilight_end);
|
2001-01-09 16:06:45 -05:00
|
|
|
}
|
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
if (hilight != NULL) {
|
|
|
|
/* update the level / hilight info */
|
|
|
|
hilight_update_text_dest(dest, hilight);
|
2000-06-28 17:47:13 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
next_line_hilight = hilight;
|
|
|
|
}
|
|
|
|
}
|
2001-01-09 16:06:45 -05:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
static void sig_print_text(TEXT_DEST_REC *dest, const char *str)
|
|
|
|
{
|
|
|
|
char *color, *newstr;
|
|
|
|
int next_hilight_len;
|
2000-06-30 15:50:56 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
if (next_line_hilight == NULL)
|
|
|
|
return;
|
2000-06-28 17:47:13 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
color = hilight_get_color(next_line_hilight, FALSE);
|
|
|
|
next_hilight_len = next_hilight_end-next_hilight_start;
|
2000-06-28 17:47:13 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
if (!next_line_hilight->word) {
|
|
|
|
/* hilight whole line */
|
|
|
|
char *tmp = strip_codes(str);
|
|
|
|
newstr = g_strconcat(color, tmp, NULL);
|
|
|
|
g_free(tmp);
|
|
|
|
} else {
|
|
|
|
/* hilight part of the line */
|
|
|
|
GString *tmp;
|
|
|
|
char *middle, *lastcolor;
|
|
|
|
int pos, color_pos, color_len;
|
|
|
|
|
|
|
|
tmp = g_string_new(NULL);
|
|
|
|
|
|
|
|
/* start of the line */
|
|
|
|
pos = strip_real_length(str, next_hilight_start, NULL, NULL);
|
|
|
|
g_string_append(tmp, str);
|
|
|
|
g_string_truncate(tmp, pos);
|
|
|
|
|
|
|
|
/* color */
|
|
|
|
g_string_append(tmp, color);
|
|
|
|
|
|
|
|
/* middle of the line, stripped */
|
|
|
|
middle = strip_codes(str+pos);
|
|
|
|
pos = tmp->len;
|
|
|
|
g_string_append(tmp, middle);
|
|
|
|
g_string_truncate(tmp, pos+next_hilight_len);
|
|
|
|
g_free(middle);
|
|
|
|
|
|
|
|
/* end of the line */
|
|
|
|
pos = strip_real_length(str, next_hilight_end,
|
|
|
|
&color_pos, &color_len);
|
|
|
|
lastcolor = g_strndup(str+color_pos, color_len);
|
|
|
|
g_string_append(tmp, lastcolor);
|
|
|
|
g_string_append(tmp, str+pos);
|
|
|
|
g_free(lastcolor);
|
|
|
|
|
|
|
|
newstr = tmp->str;
|
|
|
|
g_string_free(tmp, FALSE);
|
|
|
|
}
|
2000-06-28 17:47:13 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
next_line_hilight = NULL;
|
2000-11-12 15:20:05 -05:00
|
|
|
signal_emit("print text", 2, dest, newstr);
|
2000-06-28 17:47:13 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
g_free(color);
|
|
|
|
g_free(newstr);
|
2000-06-30 15:50:56 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
signal_stop();
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
char *hilight_match_nick(SERVER_REC *server, const char *channel,
|
|
|
|
const char *nick, const char *address,
|
|
|
|
int level, const char *msg)
|
2001-01-09 16:06:45 -05:00
|
|
|
{
|
|
|
|
HILIGHT_REC *rec;
|
2001-01-20 23:14:24 -05:00
|
|
|
char *color;
|
2000-09-30 18:49:48 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
rec = hilight_match(server, channel, nick, address,
|
|
|
|
level, msg, NULL, NULL);
|
|
|
|
color = rec == NULL || !rec->nick ? NULL :
|
|
|
|
hilight_get_color(rec, FALSE);
|
2000-09-30 18:49:48 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
next_nick_hilight = rec;
|
2001-01-09 16:06:45 -05:00
|
|
|
return color;
|
2000-09-30 18:49:48 -04:00
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
static void read_hilight_config(void)
|
|
|
|
{
|
|
|
|
CONFIG_NODE *node;
|
|
|
|
HILIGHT_REC *rec;
|
|
|
|
GSList *tmp;
|
|
|
|
char *text, *color;
|
|
|
|
|
|
|
|
hilights_destroy_all();
|
|
|
|
|
|
|
|
node = iconfig_node_traverse("hilights", FALSE);
|
2001-01-26 13:52:01 -05:00
|
|
|
if (node == NULL) {
|
|
|
|
reset_cache();
|
|
|
|
return;
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
|
|
|
|
node = tmp->data;
|
|
|
|
|
|
|
|
if (node->type != NODE_TYPE_BLOCK)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
text = config_node_get_str(node, "text", NULL);
|
|
|
|
if (text == NULL || *text == '\0')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
rec = g_new0(HILIGHT_REC, 1);
|
|
|
|
hilights = g_slist_append(hilights, rec);
|
|
|
|
|
|
|
|
rec->text = g_strdup(text);
|
2001-01-20 23:14:24 -05:00
|
|
|
|
|
|
|
color = config_node_get_str(node, "color", NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
rec->color = color == NULL || *color == '\0' ? NULL :
|
|
|
|
g_strdup(color);
|
2001-01-20 23:14:24 -05:00
|
|
|
|
|
|
|
color = config_node_get_str(node, "act_color", NULL);
|
|
|
|
rec->act_color = color == NULL || *color == '\0' ? NULL :
|
|
|
|
g_strdup(color);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
rec->level = config_node_get_int(node, "level", 0);
|
2001-01-09 16:06:45 -05:00
|
|
|
rec->priority = config_node_get_int(node, "priority", 0);
|
2000-06-30 15:50:56 -04:00
|
|
|
rec->nick = config_node_get_bool(node, "nick", TRUE);
|
2001-01-20 23:14:24 -05:00
|
|
|
rec->word = config_node_get_bool(node, "word", TRUE);
|
|
|
|
|
2000-06-30 15:50:56 -04:00
|
|
|
rec->nickmask = config_node_get_bool(node, "mask", FALSE);
|
2000-04-26 04:03:38 -04:00
|
|
|
rec->fullword = config_node_get_bool(node, "fullword", FALSE);
|
|
|
|
rec->regexp = config_node_get_bool(node, "regexp", FALSE);
|
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
#ifdef HAVE_REGEX_H
|
|
|
|
rec->regexp_compiled = !rec->regexp ? FALSE :
|
|
|
|
regcomp(&rec->preg, rec->text,
|
|
|
|
REG_EXTENDED|REG_ICASE) == 0;
|
|
|
|
#endif
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
node = config_node_section(node, "channels", -1);
|
|
|
|
if (node != NULL) rec->channels = config_node_get_list(node);
|
|
|
|
}
|
2001-01-20 23:14:24 -05:00
|
|
|
|
|
|
|
reset_cache();
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void hilight_print(int index, HILIGHT_REC *rec)
|
|
|
|
{
|
|
|
|
char *chans, *levelstr;
|
|
|
|
|
|
|
|
chans = rec->channels == NULL ? NULL :
|
|
|
|
g_strjoinv(",", rec->channels);
|
|
|
|
levelstr = rec->level == 0 ? NULL :
|
|
|
|
bits2level(rec->level);
|
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
|
2001-01-07 14:42:59 -05:00
|
|
|
TXT_HILIGHT_LINE, index, rec->text,
|
2000-04-26 04:03:38 -04:00
|
|
|
chans != NULL ? chans : "",
|
|
|
|
levelstr != NULL ? levelstr : "",
|
2001-01-20 23:14:24 -05:00
|
|
|
rec->nickmask ? " -mask" : "",
|
2000-04-26 04:03:38 -04:00
|
|
|
rec->fullword ? " -word" : "",
|
|
|
|
rec->regexp ? " -regexp" : "");
|
|
|
|
g_free_not_null(chans);
|
|
|
|
g_free_not_null(levelstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_hilight_show(void)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
int index;
|
|
|
|
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_HILIGHT_HEADER);
|
2000-04-26 04:03:38 -04:00
|
|
|
index = 1;
|
|
|
|
for (tmp = hilights; tmp != NULL; tmp = tmp->next, index++) {
|
|
|
|
HILIGHT_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
hilight_print(index, rec);
|
|
|
|
}
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_HILIGHT_FOOTER);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
/* SYNTAX: HILIGHT [-nick | -word | -line] [-mask | -full | -regexp]
|
|
|
|
[-color <color>] [-actcolor <color>] [-level <level>]
|
2000-07-23 19:19:22 -04:00
|
|
|
[-channels <channels>] <text> */
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_hilight(const char *data)
|
|
|
|
{
|
2000-06-17 21:18:12 -04:00
|
|
|
GHashTable *optlist;
|
2000-04-26 04:03:38 -04:00
|
|
|
HILIGHT_REC *rec;
|
2001-01-20 23:14:24 -05:00
|
|
|
char *colorarg, *actcolorarg, *levelarg, *priorityarg, *chanarg, *text;
|
2000-06-17 21:18:12 -04:00
|
|
|
char **channels;
|
|
|
|
void *free_arg;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
g_return_if_fail(data != NULL);
|
|
|
|
|
|
|
|
if (*data == '\0') {
|
|
|
|
cmd_hilight_show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-06-17 21:18:12 -04:00
|
|
|
if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
|
|
|
|
PARAM_FLAG_GETREST, "hilight", &optlist, &text))
|
|
|
|
return;
|
|
|
|
|
|
|
|
chanarg = g_hash_table_lookup(optlist, "channels");
|
|
|
|
levelarg = g_hash_table_lookup(optlist, "level");
|
2001-01-09 16:06:45 -05:00
|
|
|
priorityarg = g_hash_table_lookup(optlist, "priority");
|
2000-06-17 21:18:12 -04:00
|
|
|
colorarg = g_hash_table_lookup(optlist, "color");
|
2001-01-20 23:14:24 -05:00
|
|
|
actcolorarg = g_hash_table_lookup(optlist, "actcolor");
|
2000-06-17 21:18:12 -04:00
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
if (*text == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
|
|
|
|
|
2000-06-17 21:18:12 -04:00
|
|
|
channels = (chanarg == NULL || *chanarg == '\0') ? NULL :
|
2000-04-26 04:03:38 -04:00
|
|
|
g_strsplit(replace_chars(chanarg, ',', ' '), " ", -1);
|
|
|
|
|
|
|
|
rec = hilight_find(text, channels);
|
|
|
|
if (rec == NULL) {
|
|
|
|
rec = g_new0(HILIGHT_REC, 1);
|
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
/* default to nick/word hilighting */
|
|
|
|
rec->nick = TRUE;
|
|
|
|
rec->word = TRUE;
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
rec->text = g_strdup(text);
|
|
|
|
rec->channels = channels;
|
|
|
|
} else {
|
|
|
|
g_strfreev(channels);
|
|
|
|
|
|
|
|
hilight_remove_config(rec);
|
|
|
|
hilights = g_slist_remove(hilights, rec);
|
|
|
|
}
|
|
|
|
|
2000-07-09 10:25:03 -04:00
|
|
|
rec->level = (levelarg == NULL || *levelarg == '\0') ? 0 :
|
|
|
|
level2bits(replace_chars(levelarg, ',', ' '));
|
2001-01-09 16:06:45 -05:00
|
|
|
rec->priority = priorityarg == NULL ? 0 : atoi(priorityarg);
|
2001-01-20 23:14:24 -05:00
|
|
|
|
|
|
|
if (g_hash_table_lookup(optlist, "line") != NULL) {
|
|
|
|
rec->word = FALSE;
|
|
|
|
rec->nick = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_hash_table_lookup(optlist, "word") != NULL) {
|
|
|
|
rec->word = TRUE;
|
|
|
|
rec->nick = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_hash_table_lookup(optlist, "nick") != NULL)
|
|
|
|
rec->nick = TRUE;
|
|
|
|
|
2000-06-28 17:47:13 -04:00
|
|
|
rec->nickmask = g_hash_table_lookup(optlist, "mask") != NULL;
|
2000-06-17 21:18:12 -04:00
|
|
|
rec->fullword = g_hash_table_lookup(optlist, "word") != NULL;
|
|
|
|
rec->regexp = g_hash_table_lookup(optlist, "regexp") != NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
if (colorarg != NULL) {
|
|
|
|
if (*colorarg != '\0')
|
|
|
|
rec->color = g_strdup(colorarg);
|
|
|
|
else
|
|
|
|
g_free_and_null(rec->color);
|
|
|
|
}
|
|
|
|
if (actcolorarg != NULL) {
|
|
|
|
if (*actcolorarg != '\0')
|
|
|
|
rec->act_color = g_strdup(actcolorarg);
|
|
|
|
else
|
|
|
|
g_free_and_null(rec->act_color);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_REGEX_H
|
|
|
|
if (rec->regexp_compiled)
|
|
|
|
regfree(&rec->preg);
|
|
|
|
rec->regexp_compiled = !rec->regexp ? FALSE :
|
|
|
|
regcomp(&rec->preg, rec->text, REG_EXTENDED|REG_ICASE) == 0;
|
|
|
|
#endif
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-09 10:25:03 -04:00
|
|
|
hilights = g_slist_append(hilights, rec);
|
2000-04-26 04:03:38 -04:00
|
|
|
hilight_add_config(rec);
|
2000-07-09 10:25:03 -04:00
|
|
|
|
|
|
|
hilight_print(g_slist_index(hilights, rec)+1, rec);
|
2000-06-17 21:18:12 -04:00
|
|
|
cmd_params_free(free_arg);
|
2001-01-20 23:14:24 -05:00
|
|
|
|
|
|
|
reset_cache();
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: DEHILIGHT <id>|<mask> */
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_dehilight(const char *data)
|
|
|
|
{
|
|
|
|
HILIGHT_REC *rec;
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
if (is_numeric(data, ' ')) {
|
|
|
|
/* with index number */
|
2000-05-04 06:32:42 -04:00
|
|
|
tmp = g_slist_nth(hilights, atoi(data)-1);
|
2000-04-26 04:03:38 -04:00
|
|
|
rec = tmp == NULL ? NULL : tmp->data;
|
|
|
|
} else {
|
|
|
|
/* with mask */
|
|
|
|
char *chans[2] = { "*", NULL };
|
|
|
|
rec = hilight_find(data, chans);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rec == NULL)
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_HILIGHT_NOT_FOUND, data);
|
2000-05-09 09:14:07 -04:00
|
|
|
else {
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_HILIGHT_REMOVED, rec->text);
|
2000-05-09 09:14:07 -04:00
|
|
|
hilight_remove(rec);
|
2001-01-20 23:14:24 -05:00
|
|
|
reset_cache();
|
2000-05-09 09:14:07 -04:00
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
static void hilight_nick_cache(GHashTable *list, CHANNEL_REC *channel,
|
|
|
|
NICK_REC *nick)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
HILIGHT_REC *match;
|
|
|
|
char *nickmask;
|
|
|
|
int len, best_match;
|
|
|
|
|
|
|
|
if (nick->host == NULL)
|
|
|
|
return; /* don't check until host is known */
|
|
|
|
|
|
|
|
nickmask = g_strconcat(nick->nick, "!", nick->host, NULL);
|
|
|
|
|
|
|
|
best_match = 0; match = NULL;
|
|
|
|
for (tmp = hilights; tmp != NULL; tmp = tmp->next) {
|
|
|
|
HILIGHT_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (rec->nickmask &&
|
|
|
|
hilight_match_channel(rec, channel->name) &&
|
|
|
|
match_wildcards(rec->text, nickmask)) {
|
|
|
|
len = strlen(rec->text);
|
|
|
|
if (best_match < len) {
|
|
|
|
best_match = len;
|
|
|
|
match = rec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free_not_null(nickmask);
|
|
|
|
|
|
|
|
if (match != NULL)
|
|
|
|
g_hash_table_insert(list, nick, match);
|
|
|
|
}
|
|
|
|
|
2001-01-13 09:04:11 -05:00
|
|
|
static void read_settings(void)
|
|
|
|
{
|
2001-01-28 14:11:34 -05:00
|
|
|
default_hilight_level = level2bits(settings_get_str("hilight_level"));
|
2001-01-13 09:04:11 -05:00
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
void hilight_text_init(void)
|
|
|
|
{
|
2001-01-13 09:04:11 -05:00
|
|
|
settings_add_str("lookandfeel", "hilight_color", "8");
|
2001-01-28 14:11:34 -05:00
|
|
|
settings_add_str("lookandfeel", "hilight_level", "PUBLIC DCCMSGS");
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-01-20 23:14:24 -05:00
|
|
|
next_nick_hilight = NULL;
|
|
|
|
next_line_hilight = NULL;
|
|
|
|
|
|
|
|
read_settings();
|
|
|
|
|
|
|
|
nickmatch = nickmatch_init(hilight_nick_cache);
|
|
|
|
read_hilight_config();
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_add_first("print text stripped", (SIGNAL_FUNC) sig_print_text_stripped);
|
2001-01-09 16:06:45 -05:00
|
|
|
signal_add_first("print text", (SIGNAL_FUNC) sig_print_text);
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_add("setup reread", (SIGNAL_FUNC) read_hilight_config);
|
2001-01-13 09:04:11 -05:00
|
|
|
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
|
2001-01-09 16:06:45 -05:00
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
command_bind("hilight", NULL, (SIGNAL_FUNC) cmd_hilight);
|
|
|
|
command_bind("dehilight", NULL, (SIGNAL_FUNC) cmd_dehilight);
|
2001-01-20 23:14:24 -05:00
|
|
|
command_set_options("hilight", "-color -actcolor -level -priority -channels nick word line mask full regexp");
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void hilight_text_deinit(void)
|
|
|
|
{
|
|
|
|
hilights_destroy_all();
|
2001-01-20 23:14:24 -05:00
|
|
|
nickmatch_deinit(nickmatch);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
signal_remove("print text stripped", (SIGNAL_FUNC) sig_print_text_stripped);
|
2001-01-09 16:06:45 -05:00
|
|
|
signal_remove("print text", (SIGNAL_FUNC) sig_print_text);
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_remove("setup reread", (SIGNAL_FUNC) read_hilight_config);
|
2001-01-13 09:04:11 -05:00
|
|
|
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
|
2001-01-09 16:06:45 -05:00
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
command_unbind("hilight", (SIGNAL_FUNC) cmd_hilight);
|
|
|
|
command_unbind("dehilight", (SIGNAL_FUNC) cmd_dehilight);
|
|
|
|
}
|