addition of silc.css
[crypto.git] / apps / irssi / src / core / chat-protocols.c
1 /*
2  chat-protocol.c : irssi
3
4     Copyright (C) 2000 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "modules.h"
23 #include "signals.h"
24 #include "chat-protocols.h"
25
26 #include "chatnets.h"
27 #include "servers.h"
28 #include "servers-setup.h"
29 #include "channels-setup.h"
30
31 GSList *chat_protocols;
32
33 static CHAT_PROTOCOL_REC *default_proto;
34
35 void *chat_protocol_check_cast(void *object, int type_pos, const char *id)
36 {
37         return object == NULL ||
38                 chat_protocol_lookup(id) !=
39                 G_STRUCT_MEMBER(int, object, type_pos) ? NULL : object;
40 }
41
42 /* Return the ID for the specified chat protocol. */
43 int chat_protocol_lookup(const char *name)
44 {
45         CHAT_PROTOCOL_REC *rec;
46
47         g_return_val_if_fail(name != NULL, -1);
48
49         rec = chat_protocol_find(name);
50         return rec == NULL ? -1 : rec->id;
51 }
52
53 CHAT_PROTOCOL_REC *chat_protocol_find(const char *name)
54 {
55         GSList *tmp;
56
57         g_return_val_if_fail(name != NULL, NULL);
58
59         for (tmp = chat_protocols; tmp != NULL; tmp = tmp->next) {
60                 CHAT_PROTOCOL_REC *rec = tmp->data;
61
62                 if (g_strcasecmp(rec->name, name) == 0)
63                         return rec;
64         }
65
66         return NULL;
67 }
68
69 CHAT_PROTOCOL_REC *chat_protocol_find_id(int id)
70 {
71         GSList *tmp;
72
73         g_return_val_if_fail(id > 0, NULL);
74
75         for (tmp = chat_protocols; tmp != NULL; tmp = tmp->next) {
76                 CHAT_PROTOCOL_REC *rec = tmp->data;
77
78                 if (rec->id == id)
79                         return rec;
80         }
81
82         return NULL;
83 }
84
85 CHAT_PROTOCOL_REC *chat_protocol_find_net(GHashTable *optlist)
86 {
87         GSList *tmp;
88
89         g_return_val_if_fail(optlist != NULL, NULL);
90
91         for (tmp = chat_protocols; tmp != NULL; tmp = tmp->next) {
92                 CHAT_PROTOCOL_REC *rec = tmp->data;
93
94                 if (rec->chatnet != NULL &&
95                     g_hash_table_lookup(optlist, rec->chatnet) != NULL)
96                         return rec;
97         }
98
99         return NULL;
100 }
101
102 /* Register new chat protocol. */
103 CHAT_PROTOCOL_REC *chat_protocol_register(CHAT_PROTOCOL_REC *rec)
104 {
105         CHAT_PROTOCOL_REC *newrec;
106         int created;
107
108         g_return_val_if_fail(rec != NULL, NULL);
109
110         newrec = chat_protocol_find(rec->name);
111         created = newrec == NULL;
112         if (newrec == NULL) {
113                 newrec = g_new0(CHAT_PROTOCOL_REC, 1);
114                 chat_protocols = g_slist_append(chat_protocols, newrec);
115         } else {
116                 /* updating existing protocol */
117                 g_free(newrec->name);
118         }
119
120         memcpy(newrec, rec, sizeof(CHAT_PROTOCOL_REC));
121         newrec->id = module_get_uniq_id_str("PROTOCOL", rec->name);
122         newrec->name = g_strdup(rec->name);
123
124         if (default_proto == NULL)
125                 chat_protocol_set_default(newrec);
126
127         if (created)
128                 signal_emit("chat protocol created", 1, newrec);
129         else
130                 signal_emit("chat protocol updated", 1, newrec);
131         return newrec;
132 }
133
134 static void chat_protocol_destroy(CHAT_PROTOCOL_REC *rec)
135 {
136         g_return_if_fail(rec != NULL);
137
138         chat_protocols = g_slist_remove(chat_protocols, rec);
139
140         if (default_proto == rec) {
141                 chat_protocol_set_default(chat_protocols == NULL ? NULL :
142                                           chat_protocols->data);
143         }
144
145         signal_emit("chat protocol destroyed", 1, rec);
146
147         g_free(rec->name);
148         g_free(rec);
149 }
150
151 /* Unregister chat protocol. */
152 void chat_protocol_unregister(const char *name)
153 {
154         CHAT_PROTOCOL_REC *rec;
155
156         g_return_if_fail(name != NULL);
157
158         rec = chat_protocol_find(name);
159         if (rec != NULL) chat_protocol_destroy(rec);
160 }
161
162 /* Default chat protocol to use */
163 void chat_protocol_set_default(CHAT_PROTOCOL_REC *rec)
164 {
165         default_proto = rec;
166 }
167
168 CHAT_PROTOCOL_REC *chat_protocol_get_default(void)
169 {
170         return default_proto;
171 }
172
173 static CHATNET_REC *create_chatnet(void)
174 {
175         return g_new0(CHATNET_REC, 1);
176 }
177
178 static SERVER_SETUP_REC *create_server_setup(void)
179 {
180         return g_new0(SERVER_SETUP_REC, 1);
181 }
182
183 static CHANNEL_SETUP_REC *create_channel_setup(void)
184 {
185         return g_new0(CHANNEL_SETUP_REC, 1);
186 }
187
188 static SERVER_CONNECT_REC *create_server_connect(void)
189 {
190         return g_new0(SERVER_CONNECT_REC, 1);
191 }
192
193 /* Return "unknown chat protocol" record. Used when protocol name is
194    specified but it isn't registered yet. */
195 CHAT_PROTOCOL_REC *chat_protocol_get_unknown(const char *name)
196 {
197         CHAT_PROTOCOL_REC *rec, *newrec;
198
199         g_return_val_if_fail(name != NULL, NULL);
200
201         rec = chat_protocol_find(name);
202         if (rec != NULL)
203                 return rec;
204
205         rec = g_new0(CHAT_PROTOCOL_REC, 1);
206         rec->not_initialized = TRUE;
207         rec->name = (char *) name;
208         rec->create_chatnet = create_chatnet;
209         rec->create_server_setup = create_server_setup;
210         rec->create_channel_setup = create_channel_setup;
211         rec->create_server_connect = create_server_connect;
212
213         newrec = chat_protocol_register(rec);
214         g_free(rec);
215         return newrec;
216 }
217
218 void chat_protocols_init(void)
219 {
220         default_proto = NULL;
221         chat_protocols = NULL;
222 }
223
224 void chat_protocols_deinit(void)
225 {
226         while (chat_protocols != NULL)
227                 chat_protocol_destroy(chat_protocols->data);
228 }