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
This commit is contained in:
Timo Sirainen 2000-09-01 00:26:46 +00:00 committed by cras
commit 28a7908e73
26 changed files with 259 additions and 62 deletions

View file

@ -18,6 +18,7 @@ libcore_a_SOURCES = \
channels.c \
channels-setup.c \
commands.c \
chat-protocols.c \
chatnets.c \
core.c \
levels.c \
@ -57,6 +58,7 @@ noinst_HEADERS = \
channels.h \
channels-setup.h \
commands.h \
chat-protocols.h \
chatnets.h \
core.h \
levels.h \

View file

@ -62,6 +62,7 @@ void channel_destroy(CHANNEL_REC *channel)
MODULE_DATA_DEINIT(channel);
g_free_not_null(channel->topic);
g_free_not_null(channel->key);
g_free(channel->mode);
g_free(channel->name);
g_free(channel);
}
@ -84,7 +85,7 @@ static CHANNEL_REC *channel_find_server(SERVER_REC *server,
for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
CHANNEL_REC *rec = tmp->data;
if (rec->chat_type == server->channel_type &&
if (rec->chat_type == server->chat_type &&
g_strcasecmp(name, rec->name) == 0)
return rec;
}

172
src/core/chat-protocols.c Normal file
View file

@ -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);
}

29
src/core/chat-protocols.h Normal file
View file

@ -0,0 +1,29 @@
#ifndef __CHAT_PROTOCOLS_H
#define __CHAT_PROTOCOLS_H
#define PROTO_CHECK_CAST(object, cast, type_field, id) \
((cast *) chat_protocol_check_cast(object, \
offsetof(cast, type_field), id))
void *chat_protocol_check_cast(void *object, int type_pos, const char *id);
/* Register new chat protocol. */
void chat_protocol_register(const char *name,
const char *fullname,
const char *chatnet);
/* Unregister chat protocol. */
void chat_protocol_unregister(const char *name);
/* Return the ID for the specified chat protocol. */
int chat_protocol_lookup(const char *name);
/* Return the name for the specified chat protocol ID. */
const char *chat_protocol_get_name(int id);
/* Return the full name for the specified chat protocol ID. */
const char *chat_protocol_get_fullname(int id);
/* Return the chatnet identifier name for the specified chat protocol ID. */
const char *chat_protocol_get_chatnet(int id);
void chat_protocols_init(void);
void chat_protocols_deinit(void);
#endif

View file

@ -1,5 +1,5 @@
int type; /* should always be "CHATNET" */
int chat_type;
int type; /* module_get_uniq_id("CHATNET", 0) */
int chat_type; /* chat_protocol_lookup(xx) */
char *name;

View file

@ -27,6 +27,7 @@
#include "signals.h"
#include "settings.h"
#include "chat-protocols.h"
#include "servers.h"
#include "chatnets.h"
#include "commands.h"
@ -51,6 +52,7 @@ void core_init(void)
settings_init();
commands_init();
chat_protocols_init();
chatnets_init();
servers_init();
log_init();
@ -73,6 +75,7 @@ void core_deinit(void)
log_deinit();
servers_deinit();
chatnets_deinit();
chat_protocols_deinit();
commands_deinit();
settings_deinit();

View file

@ -84,7 +84,7 @@ static QUERY_REC *query_find_server(SERVER_REC *server, const char *nick)
for (tmp = server->queries; tmp != NULL; tmp = tmp->next) {
QUERY_REC *rec = tmp->data;
if (rec->chat_type == server->query_type &&
if (rec->chat_type == server->chat_type &&
g_strcasecmp(nick, rec->name) == 0)
return rec;
}

View file

@ -1,7 +1,7 @@
/* SERVER_CONNECT_REC definition, used for inheritance */
int type;
int chat_type;
int type; /* module_get_uniq_id("SERVER CONNECT", 0) */
int chat_type; /* chat_protocol_lookup(xx) */
/* if we're connecting via proxy, or just NULLs */
char *proxy;

View file

@ -1,7 +1,7 @@
/* SERVER_REC definition, used for inheritance */
int type; /* should always be "SERVER" */
int chat_type;
int type; /* module_get_uniq_id("SERVER", 0) */
int chat_type; /* chat_protocol_lookup(xx) */
STRUCT_SERVER_CONNECT_REC *connrec;
time_t connect_time; /* connection time */
@ -42,9 +42,6 @@ GSList *queries;
/* support for multiple server types */
void *channel_find_func;
void *query_find_func;
int channel_type;
int query_type;
void *mask_match_func;
#undef STRUCT_SERVER_CONNECT_REC

View file

@ -1,5 +1,5 @@
int type;
int chat_type;
int type; /* module_get_uniq_id("SERVER SETUP", 0) */
int chat_type; /* chat_protocol_lookup(xx) */
char *chatnet;

View file

@ -1,7 +1,7 @@
/* WI_ITEM_REC definition, used for inheritance */
int type; /* window item type - channel/query/.. */
int chat_type; /* chat server type - irc/silc/.. */
int type; /* module_get_uniq_id("CHANNEL/QUERY/xxx", 0) */
int chat_type; /* chat_protocol_lookup(xx) */
GHashTable *module_data;
STRUCT_SERVER_REC *server;