summaryrefslogtreecommitdiffstats
path: root/src/core/chat-protocols.c
blob: c53b01b3d0c54661ef9d5048013a46483b81ec10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 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.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "module.h"
#include <irssi/src/core/modules.h>
#include <irssi/src/core/signals.h>
#include <irssi/src/core/chat-protocols.h>

#include <irssi/src/core/chatnets.h>
#include <irssi/src/core/servers.h>
#include <irssi/src/core/servers-setup.h>
#include <irssi/src/core/channels-setup.h>

GSList *chat_protocols;

static CHAT_PROTOCOL_REC *default_proto;

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

/* Return the ID for the specified chat protocol. */
int chat_protocol_lookup(const char *name)
{
	CHAT_PROTOCOL_REC *rec;

	g_return_val_if_fail(name != NULL, -1);

	rec = chat_protocol_find(name);
	return rec == NULL ? -1 : rec->id;
}

CHAT_PROTOCOL_REC *chat_protocol_find(const char *name)
{
	GSList *tmp;

	g_return_val_if_fail(name != NULL, NULL);

	for (tmp = chat_protocols; tmp != NULL; tmp = tmp->next) {
		CHAT_PROTOCOL_REC *rec = tmp->data;

		if (g_ascii_strcasecmp(rec->name, name) == 0)
			return rec;
	}

	return NULL;
}

CHAT_PROTOCOL_REC *chat_protocol_find_id(int id)
{
	GSList *tmp;

	g_return_val_if_fail(id > 0, NULL);

	for (tmp = chat_protocols; tmp != NULL; tmp = tmp->next) {
		CHAT_PROTOCOL_REC *rec = tmp->data;

		if (rec->id == id)
			return rec;
	}

	return NULL;
}

CHAT_PROTOCOL_REC *chat_protocol_find_net(GHashTable *optlist)
{
	GSList *tmp;

	g_return_val_if_fail(optlist != NULL, NULL);

	for (tmp = chat_protocols; tmp != NULL; tmp = tmp->next) {
		CHAT_PROTOCOL_REC *rec = tmp->data;

		if (rec->chatnet != NULL &&
		    g_hash_table_lookup(optlist, rec->chatnet) != NULL)
                        return rec;
	}

	return NULL;
}

/* Register new chat protocol. */
CHAT_PROTOCOL_REC *chat_protocol_register(CHAT_PROTOCOL_REC *rec)
{
	CHAT_PROTOCOL_REC *newrec;
        int created;

	g_return_val_if_fail(rec != NULL, NULL);

	newrec = chat_protocol_find(rec->name);
        created = newrec == NULL;
	if (newrec == NULL) {
		newrec = g_new0(CHAT_PROTOCOL_REC, 1);
		chat_protocols = g_slist_append(chat_protocols, newrec);
	} else {
		/* updating existing protocol */
                g_free(newrec->name);
	}

	memcpy(newrec, rec, sizeof(CHAT_PROTOCOL_REC));
	newrec->id = module_get_uniq_id_str("PROTOCOL", rec->name);
	newrec->name = g_strdup(rec->name);

	if (default_proto == NULL)
                chat_protocol_set_default(newrec);

        if (created)
		signal_emit("chat protocol created", 1, newrec);
        else
		signal_emit("chat protocol updated", 1, newrec);
        return newrec;
}

static void chat_protocol_destroy(CHAT_PROTOCOL_REC *rec)
{
	g_return_if_fail(rec != NULL);

	chat_protocols = g_slist_remove(chat_protocols, rec);

	if (default_proto == rec) {
		chat_protocol_set_default(chat_protocols == NULL ? NULL :
					  chat_protocols->data);
	}

	signal_emit("chat protocol destroyed", 1, rec);

	g_free(rec->name);
	g_free(rec);
}

/* Unregister chat protocol. */
void chat_protocol_unregister(const char *name)
{
	CHAT_PROTOCOL_REC *rec;

	g_return_if_fail(name != NULL);

	rec = chat_protocol_find(name);
	if (rec != NULL) {
		chat_protocol_destroy(rec);

		/* there might still be references to this chat protocol -
		   recreate it as a dummy protocol */
		chat_protocol_get_unknown(name);
	}
}

/* Default chat protocol to use */
void chat_protocol_set_default(CHAT_PROTOCOL_REC *rec)
{
        default_proto = rec;
}

CHAT_PROTOCOL_REC *chat_protocol_get_default(void)
{
        return default_proto;
}

static CHATNET_REC *create_chatnet(void)
{
        return g_new0(CHATNET_REC, 1);
}

static SERVER_SETUP_REC *create_server_setup(void)
{
        return g_new0(SERVER_SETUP_REC, 1);
}

static CHANNEL_SETUP_REC *create_channel_setup(void)
{
        return g_new0(CHANNEL_SETUP_REC, 1);
}

static SERVER_CONNECT_REC *create_server_connect(void)
{
        return g_new0(SERVER_CONNECT_REC, 1);
}

static void destroy_server_connect(SERVER_CONNECT_REC *conn)
{
}

/* Return "unknown chat protocol" record. Used when protocol name is
   specified but it isn't registered yet. */
CHAT_PROTOCOL_REC *chat_protocol_get_unknown(const char *name)
{
	CHAT_PROTOCOL_REC *rec, *newrec;

	g_return_val_if_fail(name != NULL, NULL);

        rec = chat_protocol_find(name);
	if (rec != NULL)
                return rec;

	rec = g_new0(CHAT_PROTOCOL_REC, 1);
        rec->not_initialized = TRUE;
	rec->name = (char *) name;
	rec->create_chatnet = create_chatnet;
        rec->create_server_setup = create_server_setup;
        rec->create_channel_setup = create_channel_setup;
	rec->create_server_connect = create_server_connect;
	rec->destroy_server_connect = destroy_server_connect;

	newrec = chat_protocol_register(rec);
	g_free(rec);
        return newrec;
}

void chat_protocols_init(void)
{
	default_proto = NULL;
	chat_protocols = NULL;
}

void chat_protocols_deinit(void)
{
	while (chat_protocols != NULL)
                chat_protocol_destroy(chat_protocols->data);
}