diff options
author | Timo Sirainen <cras@irssi.org> | 2000-09-01 00:26:46 +0000 |
---|---|---|
committer | cras <cras@dbcabf3a-b0e7-0310-adc4-f8d773084564> | 2000-09-01 00:26:46 +0000 |
commit | 28a7908e7372fe0e81dd439b6559bc2abe3ea6e8 (patch) | |
tree | f911b89cd7d5a5f5e9fb206c380cd5ddebe1b987 /src/core/chat-protocols.c | |
parent | 03091413eeb0935adfad1bee7b02a0161f403835 (diff) | |
download | irssi-28a7908e7372fe0e81dd439b6559bc2abe3ea6e8.zip |
Added chat protocol register. Changed all chat_type fields to use it.
git-svn-id: http://svn.irssi.org/repos/irssi/trunk@640 dbcabf3a-b0e7-0310-adc4-f8d773084564
Diffstat (limited to 'src/core/chat-protocols.c')
-rw-r--r-- | src/core/chat-protocols.c | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/src/core/chat-protocols.c b/src/core/chat-protocols.c new file mode 100644 index 00000000..a5537f88 --- /dev/null +++ b/src/core/chat-protocols.c @@ -0,0 +1,172 @@ +/* + chat-protocol.c : irssi + + Copyright (C) 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 "chat-protocols.h" + +typedef struct { + int id; + char *name; + char *fullname; + char *chatnet; +} PROTOCOL_REC; + +static int id_counter; +static GSList *protocols; + +void *chat_protocol_check_cast(void *object, int type_pos, const char *id) +{ + return object == NULL || + chat_protocol_lookup(id) != + G_STRUCT_MEMBER(int, object, type_pos) ? NULL : object; +} + +static PROTOCOL_REC *chat_protocol_find(const char *name) +{ + GSList *tmp; + + g_return_val_if_fail(name != NULL, NULL); + + for (tmp = protocols; tmp != NULL; tmp = tmp->next) { + PROTOCOL_REC *rec = tmp->data; + + if (g_strcasecmp(rec->name, name) == 0) + return rec; + } + + return NULL; +} + +static PROTOCOL_REC *chat_protocol_find_id(int id) +{ + GSList *tmp; + + g_return_val_if_fail(id > 0, NULL); + + for (tmp = protocols; tmp != NULL; tmp = tmp->next) { + PROTOCOL_REC *rec = tmp->data; + + if (rec->id == id) + return rec; + } + + return NULL; +} + +/* Register new chat protocol. */ +void chat_protocol_register(const char *name, + const char *fullname, + const char *chatnet) +{ + PROTOCOL_REC *rec; + + g_return_if_fail(name != NULL); + g_return_if_fail(fullname != NULL); + g_return_if_fail(chatnet != NULL); + + if (chat_protocol_find(name) != NULL) + return; + + rec = g_new0(PROTOCOL_REC, 1); + rec->id = ++id_counter; + rec->name = g_strdup(name); + rec->fullname = g_strdup(fullname); + rec->chatnet = g_strdup(chatnet); + protocols = g_slist_append(protocols, rec); +} + +static void chat_protocol_destroy(PROTOCOL_REC *rec) +{ + g_return_if_fail(rec != NULL); + + protocols = g_slist_remove(protocols, rec); + + g_free(rec->name); + g_free(rec->fullname); + g_free(rec->chatnet); + g_free(rec); +} + +/* Unregister chat protocol. */ +void chat_protocol_unregister(const char *name) +{ + PROTOCOL_REC *rec; + + g_return_if_fail(name != NULL); + + rec = chat_protocol_find(name); + if (rec != NULL) chat_protocol_destroy(rec); +} + +/* Return the ID for the specified chat protocol. */ +int chat_protocol_lookup(const char *name) +{ + PROTOCOL_REC *rec; + + g_return_val_if_fail(name != NULL, -1); + + rec = chat_protocol_find(name); + return rec == NULL ? -1 : rec->id; +} + +/* Return the name for the specified chat protocol ID. */ +const char *chat_protocol_get_name(int id) +{ + PROTOCOL_REC *rec; + + g_return_val_if_fail(id > 0, NULL); + + rec = chat_protocol_find_id(id); + return rec == NULL ? NULL : rec->name; +} + +/* Return the full name for the specified chat protocol ID. */ +const char *chat_protocol_get_fullname(int id) +{ + PROTOCOL_REC *rec; + + g_return_val_if_fail(id > 0, NULL); + + rec = chat_protocol_find_id(id); + return rec == NULL ? NULL : rec->fullname; +} + +/* Return the chatnet identifier name for the specified chat protocol ID. */ +const char *chat_protocol_get_chatnet(int id) +{ + PROTOCOL_REC *rec; + + g_return_val_if_fail(id > 0, NULL); + + rec = chat_protocol_find_id(id); + return rec == NULL ? NULL : rec->chatnet; +} + +void chat_protocols_init(void) +{ + id_counter = 0; + protocols = NULL; +} + +void chat_protocols_deinit(void) +{ + while (protocols != NULL) + chat_protocol_destroy(protocols->data); +} |