4 Copyright (C) 2000 - 2001 Timo Sirainen
5 Pekka Riikonen <priikone@poseidon.pspt.fi>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "net-nonblock.h"
25 #include "net-sendbuffer.h"
35 #include "servers-setup.h"
37 #include "silc-servers.h"
38 #include "silc-channels.h"
39 #include "silc-queries.h"
40 #include "window-item-def.h"
42 #include "fe-common/core/printtext.h"
44 void silc_servers_reconnect_init(void);
45 void silc_servers_reconnect_deinit(void);
47 static void silc_send_channel(SILC_SERVER_REC *server,
48 char *channel, char *msg)
50 SILC_CHANNEL_REC *rec;
52 rec = silc_channel_find(server, channel);
56 silc_client_send_channel_message(silc_client, server->conn, rec->entry,
57 NULL, 0, msg, strlen(msg), TRUE);
65 static void silc_send_msg_clients(SilcClient client,
66 SilcClientConnection conn,
67 SilcClientEntry *clients,
71 PRIVMSG_REC *rec = context;
72 SilcClientEntry target;
74 if (clients_count == 0) {
75 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown nick: %s", rec->nick);
77 target = clients[0]; /* FIXME: not a good idea :) */
79 silc_client_send_private_message(client, conn, target, 0,
80 rec->msg, strlen(rec->msg),
89 static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg)
93 rec = g_new0(PRIVMSG_REC, 1);
94 rec->nick = g_strdup(nick);
95 rec->msg = g_strdup(msg);
97 silc_client_get_clients(silc_client, server->conn,
98 nick, "", silc_send_msg_clients, rec);
101 static int isnickflag_func(char flag)
103 return flag == '@' || flag == '+';
106 static int ischannel_func(const char *data)
111 const char *get_nick_flags(void)
116 static void send_message(SILC_SERVER_REC *server, char *target, char *msg)
118 g_return_if_fail(server != NULL);
119 g_return_if_fail(target != NULL);
120 g_return_if_fail(msg != NULL);
123 silc_send_channel(server, target, msg);
125 silc_send_msg(server, target, msg);
128 static void sig_connected(SILC_SERVER_REC *server)
130 SilcClientConnection conn;
133 if (!IS_SILC_SERVER(server))
136 conn = silc_client_add_connection(silc_client,
137 server->connrec->address,
138 server->connrec->port,
142 fd = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle));
143 if (!silc_client_start_key_exchange(silc_client, conn, fd)) {
144 /* some internal error occured */
145 server_disconnect(SERVER(server));
150 server->isnickflag = isnickflag_func;
151 server->ischannel = ischannel_func;
152 server->get_nick_flags = get_nick_flags;
153 server->send_message = (void *) send_message;
156 static void sig_disconnected(SILC_SERVER_REC *server)
158 if (!IS_SILC_SERVER(server) || server->conn == NULL)
161 if (server->conn->sock != NULL) {
162 silc_client_close_connection(silc_client, NULL, server->conn);
164 /* SILC closes the handle */
165 g_io_channel_unref(net_sendbuffer_handle(server->handle));
166 net_sendbuffer_destroy(server->handle, FALSE);
167 server->handle = NULL;
171 SILC_SERVER_REC *silc_server_connect(SILC_SERVER_CONNECT_REC *conn)
173 SILC_SERVER_REC *server;
175 g_return_val_if_fail(IS_SILC_SERVER_CONNECT(conn), NULL);
176 if (conn->address == NULL || *conn->address == '\0')
178 if (conn->nick == NULL || *conn->nick == '\0') {
179 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
180 "Cannot connect: nickname is not set");
184 server = g_new0(SILC_SERVER_REC, 1);
185 server->chat_type = SILC_PROTOCOL;
186 server->connrec = conn;
187 if (server->connrec->port <= 0)
188 server->connrec->port = 706;
190 if (!server_start_connect((SERVER_REC *) server)) {
191 server_connect_free(SERVER_CONNECT(conn));
199 /* Return a string of all channels in server in server->channels_join()
202 char *silc_server_get_channels(SILC_SERVER_REC *server)
208 g_return_val_if_fail(server != NULL, FALSE);
210 chans = g_string_new(NULL);
211 for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
212 CHANNEL_REC *channel = tmp->data;
214 g_string_sprintfa(chans, "%s,", channel->name);
218 g_string_truncate(chans, chans->len-1);
221 g_string_free(chans, FALSE);
226 void silc_command_exec(SILC_SERVER_REC *server,
227 const char *command, const char *args)
230 unsigned char **argv;
231 uint32 *argv_lens, *argv_types;
233 SilcClientCommand *cmd;
234 SilcClientCommandContext ctx;
236 g_return_if_fail(server != NULL);
238 tmpcmd = g_strdup(command);
240 cmd = silc_client_command_find(tmpcmd);
245 /* Now parse all arguments */
246 data = g_strconcat(command, " ", args, NULL);
247 silc_parse_command_line(data, &argv, &argv_lens,
248 &argv_types, &argc, cmd->max_args);
251 /* Allocate command context. This and its internals must be free'd
252 by the command routine receiving it. */
253 ctx = silc_client_command_alloc();
254 ctx->client = silc_client;
255 ctx->conn = server->conn;
259 ctx->argv_lens = argv_lens;
260 ctx->argv_types = argv_types;
262 /* Execute command */
266 static void command_users(const char *data, SILC_SERVER_REC *server,
269 signal_emit("command names", 3, data, server, item);
272 static void command_self(const char *data, SILC_SERVER_REC *server)
274 if (!IS_SILC_SERVER(server) || !server->connected) {
275 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
279 silc_command_exec(server, current_command, data);
283 static void event_text(const char *line, SILC_SERVER_REC *server,
288 g_return_if_fail(line != NULL);
290 if (!IS_SILC_ITEM(item))
293 str = g_strdup_printf("%s %s", item->name, line);
294 signal_emit("command msg", 3, str, server, item);
300 void silc_server_init(void)
302 silc_servers_reconnect_init();
304 signal_add_first("server connected", (SIGNAL_FUNC) sig_connected);
305 signal_add("server disconnected", (SIGNAL_FUNC) sig_disconnected);
306 signal_add("send text", (SIGNAL_FUNC) event_text);
307 command_bind("whois", MODULE_NAME, (SIGNAL_FUNC) command_self);
308 command_bind("whowas", MODULE_NAME, (SIGNAL_FUNC) command_self);
309 command_bind("nick", MODULE_NAME, (SIGNAL_FUNC) command_self);
310 command_bind("topic", MODULE_NAME, (SIGNAL_FUNC) command_self);
311 command_bind("cmode", MODULE_NAME, (SIGNAL_FUNC) command_self);
312 command_bind("cumode", MODULE_NAME, (SIGNAL_FUNC) command_self);
313 command_bind("users", MODULE_NAME, (SIGNAL_FUNC) command_self);
314 command_bind("list", MODULE_NAME, (SIGNAL_FUNC) command_self);
315 command_bind("ban", MODULE_NAME, (SIGNAL_FUNC) command_self);
316 command_bind("silcoper", MODULE_NAME, (SIGNAL_FUNC) command_self);
317 command_bind("umode", MODULE_NAME, (SIGNAL_FUNC) command_self);
318 command_bind("invite", MODULE_NAME, (SIGNAL_FUNC) command_self);
319 command_bind("kill", MODULE_NAME, (SIGNAL_FUNC) command_self);
320 command_bind("kick", MODULE_NAME, (SIGNAL_FUNC) command_self);
321 command_bind("info", MODULE_NAME, (SIGNAL_FUNC) command_self);
322 command_bind("connect", MODULE_NAME, (SIGNAL_FUNC) command_self);
323 command_bind("ping", MODULE_NAME, (SIGNAL_FUNC) command_self);
324 command_bind("motd", MODULE_NAME, (SIGNAL_FUNC) command_self);
325 command_bind("close", MODULE_NAME, (SIGNAL_FUNC) command_self);
326 command_bind("shutdown", MODULE_NAME, (SIGNAL_FUNC) command_self);
327 command_bind("getkey", MODULE_NAME, (SIGNAL_FUNC) command_self);
329 command_set_options("connect", "+silcnet");
332 void silc_server_deinit(void)
334 silc_servers_reconnect_deinit();
336 signal_remove("server connected", (SIGNAL_FUNC) sig_connected);
337 signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
338 signal_remove("send text", (SIGNAL_FUNC) event_text);
339 command_unbind("whois", (SIGNAL_FUNC) command_self);
340 command_unbind("whowas", (SIGNAL_FUNC) command_self);
341 command_unbind("nick", (SIGNAL_FUNC) command_self);
342 command_unbind("topic", (SIGNAL_FUNC) command_self);
343 command_unbind("cmode", (SIGNAL_FUNC) command_self);
344 command_unbind("cumode", (SIGNAL_FUNC) command_self);
345 command_unbind("users", (SIGNAL_FUNC) command_self);
346 command_unbind("list", (SIGNAL_FUNC) command_self);
347 command_unbind("silcoper", (SIGNAL_FUNC) command_self);
348 command_unbind("umode", (SIGNAL_FUNC) command_self);
349 command_unbind("invite", (SIGNAL_FUNC) command_self);
350 command_unbind("kill", (SIGNAL_FUNC) command_self);
351 command_unbind("kick", (SIGNAL_FUNC) command_self);
352 command_unbind("info", (SIGNAL_FUNC) command_self);
353 command_unbind("connect", (SIGNAL_FUNC) command_self);
354 command_unbind("ping", (SIGNAL_FUNC) command_self);
355 command_unbind("motd", (SIGNAL_FUNC) command_self);
356 command_unbind("ban", (SIGNAL_FUNC) command_self);
357 command_unbind("close", (SIGNAL_FUNC) command_self);
358 command_unbind("shutdown", (SIGNAL_FUNC) command_self);
359 command_unbind("getkey", (SIGNAL_FUNC) command_self);