From 1bfa0e1ee33abab707762a7c6cc896f355a25074 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Sun, 21 Jan 2001 04:09:48 +0000 Subject: [PATCH] Nickmatch cache. A couple of helper functions to check if channel nicks belong to something. Used for checking nickmasks in highlighting and ignores (well, ignore isn't done yet). git-svn-id: http://svn.irssi.org/repos/irssi/trunk@1138 dbcabf3a-b0e7-0310-adc4-f8d773084564 --- src/core/Makefile.am | 2 + src/core/core.c | 3 + src/core/nickmatch-cache.c | 120 +++++++++++++++++++++++++++++++++++++ src/core/nickmatch-cache.h | 26 ++++++++ 4 files changed, 151 insertions(+) create mode 100644 src/core/nickmatch-cache.c create mode 100644 src/core/nickmatch-cache.h diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 1c206581..c1e12337 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -36,6 +36,7 @@ libcore_a_SOURCES = \ net-sendbuffer.c \ network.c \ nicklist.c \ + nickmatch-cache.c \ pidwait.c \ queries.c \ rawlog.c \ @@ -81,6 +82,7 @@ noinst_HEADERS = \ network.h \ nick-rec.h \ nicklist.h \ + nickmatch-cache.h \ pidwait.h \ queries.h \ rawlog.h \ diff --git a/src/core/core.c b/src/core/core.c index 804c6ba7..55307e31 100644 --- a/src/core/core.c +++ b/src/core/core.c @@ -39,6 +39,7 @@ #include "channels.h" #include "queries.h" #include "nicklist.h" +#include "nickmatch-cache.h" void chat_commands_init(void); void chat_commands_deinit(void); @@ -69,6 +70,7 @@ void core_init(void) channels_init(); queries_init(); nicklist_init(); + nickmatch_cache_init(); chat_commands_init(); settings_check(); @@ -78,6 +80,7 @@ void core_deinit(void) { chat_commands_deinit(); + nickmatch_cache_deinit(); nicklist_deinit(); queries_deinit(); channels_deinit(); diff --git a/src/core/nickmatch-cache.c b/src/core/nickmatch-cache.c new file mode 100644 index 00000000..6605a2f4 --- /dev/null +++ b/src/core/nickmatch-cache.c @@ -0,0 +1,120 @@ +/* + nickmatch-cache.c : irssi + + Copyright (C) 2001 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 "signals.h" + +#include "channels.h" +#include "nicklist.h" + +#include "nickmatch-cache.h" + +static GSList *lists; + +NICKMATCH_REC *nickmatch_init(NICKMATCH_REBUILD_FUNC func) +{ + NICKMATCH_REC *rec; + + rec = g_new0(NICKMATCH_REC, 1); + rec->func = func; + + lists = g_slist_append(lists, rec); + return rec; +} + +void nickmatch_deinit(NICKMATCH_REC *rec) +{ + lists = g_slist_remove(lists, rec); + + g_hash_table_destroy(rec->nicks); + g_free(rec); +} + +static void nickmatch_check_channel(CHANNEL_REC *channel, NICKMATCH_REC *rec) +{ + GSList *nicks, *tmp; + + nicks = nicklist_getnicks(channel); + for (tmp = nicks; tmp != NULL; tmp = tmp->next) { + NICK_REC *nick = tmp->data; + + rec->func(rec->nicks, channel, nick); + } + g_slist_free(nicks); +} + +void nickmatch_rebuild(NICKMATCH_REC *rec) +{ + if (rec->nicks != NULL) + g_hash_table_destroy(rec->nicks); + + rec->nicks = g_hash_table_new((GHashFunc) g_direct_hash, + (GCompareFunc) g_direct_equal); + + g_slist_foreach(channels, (GFunc) nickmatch_check_channel, rec); +} + +static void sig_nick_new(CHANNEL_REC *channel, NICK_REC *nick) +{ + GSList *tmp; + + g_return_if_fail(channel != NULL); + g_return_if_fail(nick != NULL); + + for (tmp = lists; tmp != NULL; tmp = tmp->next) { + NICKMATCH_REC *rec = tmp->data; + + rec->func(rec->nicks, channel, nick); + } +} + +static void sig_nick_remove(CHANNEL_REC *channel, NICK_REC *nick) +{ + GSList *tmp; + + g_return_if_fail(channel != NULL); + g_return_if_fail(nick != NULL); + + for (tmp = lists; tmp != NULL; tmp = tmp->next) { + NICKMATCH_REC *rec = tmp->data; + + g_hash_table_remove(rec->nicks, nick); + } +} + +void nickmatch_cache_init(void) +{ + lists = NULL; + signal_add("nicklist new", (SIGNAL_FUNC) sig_nick_new); + signal_add("nicklist changed", (SIGNAL_FUNC) sig_nick_new); + signal_add("nicklist host changed", (SIGNAL_FUNC) sig_nick_new); + signal_add("nicklist remove", (SIGNAL_FUNC) sig_nick_remove); +} + +void nickmatch_cache_deinit(void) +{ + g_slist_foreach(lists, (GFunc) nickmatch_deinit, NULL); + g_slist_free(lists); + + signal_remove("nicklist new", (SIGNAL_FUNC) sig_nick_new); + signal_remove("nicklist changed", (SIGNAL_FUNC) sig_nick_new); + signal_remove("nicklist host changed", (SIGNAL_FUNC) sig_nick_new); + signal_remove("nicklist remove", (SIGNAL_FUNC) sig_nick_remove); +} diff --git a/src/core/nickmatch-cache.h b/src/core/nickmatch-cache.h new file mode 100644 index 00000000..c4140a49 --- /dev/null +++ b/src/core/nickmatch-cache.h @@ -0,0 +1,26 @@ +#ifndef __NICKMATCH_CACHE_H +#define __NICKMATCH_CACHE_H + +typedef void (*NICKMATCH_REBUILD_FUNC) (GHashTable *list, + CHANNEL_REC *channel, NICK_REC *nick); + +typedef struct { + GHashTable *nicks; + NICKMATCH_REBUILD_FUNC func; +} NICKMATCH_REC; + +NICKMATCH_REC *nickmatch_init(NICKMATCH_REBUILD_FUNC func); +void nickmatch_deinit(NICKMATCH_REC *rec); + +/* Calls rebuild function for all nicks in all channels. + This must be called soon after nickmatch_init(), before any nicklist + signals get sent. */ +void nickmatch_rebuild(NICKMATCH_REC *rec); + +#define nickmatch_find(rec, nick) \ + g_hash_table_lookup((rec)->nicks, nick) + +void nickmatch_cache_init(void); +void nickmatch_cache_deinit(void); + +#endif