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 "silc-nicklist.h"
41 #include "window-item-def.h"
43 #include "fe-common/core/printtext.h"
44 #include "fe-common/core/fe-channels.h"
45 #include "fe-common/core/keyboard.h"
46 #include "fe-common/silc/module-formats.h"
48 #include "silc-commands.h"
50 void silc_servers_reconnect_init(void);
51 void silc_servers_reconnect_deinit(void);
53 static void silc_send_channel(SILC_SERVER_REC *server,
54 char *channel, char *msg)
56 SILC_CHANNEL_REC *rec;
58 rec = silc_channel_find(server, channel);
59 if (rec == NULL || rec->entry == NULL) {
60 cmd_return_error(CMDERR_NOT_JOINED);
64 silc_client_send_channel_message(silc_client, server->conn, rec->entry,
65 NULL, SILC_MESSAGE_FLAG_UTF8,
66 msg, strlen(msg), TRUE);
73 SilcMessageFlags flags;
74 SILC_SERVER_REC *server;
77 /* Callback function that sends the private message if the client was
78 resolved from the server. */
80 static void silc_send_msg_clients(SilcClient client,
81 SilcClientConnection conn,
82 SilcClientEntry *clients,
83 SilcUInt32 clients_count,
86 PRIVMSG_REC *rec = context;
87 SILC_SERVER_REC *server = rec->server;
88 SilcClientEntry target;
89 char *nickname = NULL;
92 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
93 "%s: There is no such client", rec->nick);
95 if (clients_count > 1) {
96 silc_parse_userfqdn(rec->nick, &nickname, NULL);
98 /* Find the correct one. The rec->nick might be a formatted nick
99 so this will find the correct one. */
100 clients = silc_client_get_clients_local(silc_client, server->conn,
104 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
105 "%s: There is no such client", rec->nick);
114 /* Still check for exact math for nickname, this compares the
115 real (formatted) nickname and the nick (maybe formatted) that
116 use gave. This is to assure that `nick' does not match
118 if (strcasecmp(rec->nick, clients[0]->nickname)) {
119 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
120 "%s: There is no such client", rec->nick);
124 /* Send the private message */
125 silc_client_send_private_message(client, conn, target,
137 static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg,
138 int msg_len, SilcMessageFlags flags)
141 SilcClientEntry *clients;
142 SilcUInt32 clients_count;
143 char *nickname = NULL;
145 if (!silc_parse_userfqdn(nick, &nickname, NULL)) {
146 printformat_module("fe-common/silc", server, NULL,
147 MSGLEVEL_CRAP, SILCTXT_BAD_NICK, nick);
151 /* Find client entry */
152 clients = silc_client_get_clients_local(silc_client, server->conn,
153 nickname, nick, &clients_count);
155 rec = g_new0(PRIVMSG_REC, 1);
156 rec->nick = g_strdup(nick);
157 rec->msg = g_strdup(msg);
158 rec->server = server;
162 /* Could not find client with that nick, resolve it from server. */
163 silc_client_get_clients(silc_client, server->conn,
164 nickname, NULL, silc_send_msg_clients, rec);
169 /* Send the private message directly */
171 silc_client_send_private_message(silc_client, server->conn,
176 void silc_send_mime(SILC_SERVER_REC *server, WI_ITEM_REC *to,
177 const char *data, int data_len,
178 const char *enc, const char *type)
180 SILC_CHANNEL_REC *channel;
185 if (!(IS_SILC_SERVER(server)) || (data == NULL) || (to == NULL) ||
186 (enc == NULL) || (type == NULL))
189 #define SILC_MIME_HEADER "MIME-Version: 1.0\r\nContent-Type: %s\r\nContent-Transfer-Encoding: %s\r\n\r\n"
191 mime_data_len = data_len + strlen(SILC_MIME_HEADER) - 4
192 + strlen(enc) + strlen(type);
193 if (mime_data_len >= SILC_PACKET_MAX_LEN)
196 /* we risk to large packets here... */
197 mime_data = silc_calloc(mime_data_len, sizeof(*mime_data));
198 snprintf(mime_data, mime_data_len, SILC_MIME_HEADER, type, enc);
199 memmove(mime_data + strlen(SILC_MIME_HEADER) - 4 + strlen(enc) + strlen(type),
202 #undef SILC_MIME_HEADER
204 if (IS_SILC_CHANNEL(to)) {
205 channel = SILC_CHANNEL(to);
206 silc_client_send_channel_message(silc_client, server->conn, channel->entry,
207 NULL, SILC_MESSAGE_FLAG_DATA,
208 mime_data, mime_data_len, TRUE);
209 } else if (IS_SILC_QUERY(to)) {
210 query = SILC_QUERY(to);
211 silc_send_msg(server, query->name, mime_data, mime_data_len,
212 SILC_MESSAGE_FLAG_DATA);
216 silc_free(mime_data);
219 static int isnickflag_func(char flag)
221 return flag == '@' || flag == '+';
224 static int ischannel_func(SERVER_REC *server, const char *data)
229 const char *get_nick_flags(void)
234 static void send_message(SILC_SERVER_REC *server, char *target,
235 char *msg, int target_type)
237 char *message = NULL;
240 g_return_if_fail(server != NULL);
241 g_return_if_fail(target != NULL);
242 g_return_if_fail(msg != NULL);
244 if (!silc_term_utf8()) {
245 len = silc_utf8_encoded_len(msg, strlen(msg), SILC_STRING_LANGUAGE);
246 message = silc_calloc(len + 1, sizeof(*message));
247 g_return_if_fail(message != NULL);
248 silc_utf8_encode(msg, strlen(msg), SILC_STRING_LANGUAGE, message, len);
251 if (target_type == SEND_TARGET_CHANNEL)
252 silc_send_channel(server, target, message ? message : msg);
254 silc_send_msg(server, target, message ? message : msg,
255 message ? strlen(message) : strlen(msg),
256 SILC_MESSAGE_FLAG_UTF8);
261 static void sig_connected(SILC_SERVER_REC *server)
263 SilcClientConnection conn;
264 SilcClientConnectionParams params;
268 if (!IS_SILC_SERVER(server))
271 /* Try to read detached session data and use it if found. */
272 memset(¶ms, 0, sizeof(params));
273 memset(file, 0, sizeof(file));
274 snprintf(file, sizeof(file) - 1, "%s/session", get_irssi_dir());
275 params.detach_data = silc_file_readfile(file, ¶ms.detach_data_len);
276 if (params.detach_data)
277 params.detach_data[params.detach_data_len] = 0;
279 /* Add connection to the client library */
280 conn = silc_client_add_connection(silc_client, ¶ms,
281 server->connrec->address,
282 server->connrec->port,
286 if (params.detach_data)
287 keyboard_entry_redirect(NULL,
288 "-- Resuming old session, may take a while ...",
289 ENTRY_REDIRECT_FLAG_HIDDEN, server);
291 silc_free(params.detach_data);
294 fd = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle));
296 /* Start key exchange with the server */
297 silc_client_start_key_exchange(silc_client, conn, fd);
299 /* Put default attributes */
300 silc_query_attributes_default(silc_client, conn);
302 server->ftp_sessions = silc_dlist_init();
303 server->isnickflag = isnickflag_func;
304 server->ischannel = ischannel_func;
305 server->get_nick_flags = get_nick_flags;
306 server->send_message = (void *) send_message;
309 static void sig_disconnected(SILC_SERVER_REC *server)
311 if (!IS_SILC_SERVER(server))
314 silc_dlist_uninit(server->ftp_sessions);
316 if (server->conn && server->conn->sock != NULL) {
317 silc_client_close_connection(silc_client, server->conn);
319 /* SILC closes the handle */
320 g_io_channel_unref(net_sendbuffer_handle(server->handle));
321 net_sendbuffer_destroy(server->handle, FALSE);
322 server->handle = NULL;
326 SERVER_REC *silc_server_init_connect(SERVER_CONNECT_REC *conn)
328 SILC_SERVER_REC *server;
330 g_return_val_if_fail(IS_SILC_SERVER_CONNECT(conn), NULL);
331 if (conn->address == NULL || *conn->address == '\0')
333 if (conn->nick == NULL || *conn->nick == '\0') {
334 silc_say_error("Cannot connect: nickname is not set");
338 server = g_new0(SILC_SERVER_REC, 1);
339 server->chat_type = SILC_PROTOCOL;
340 server->connrec = (SILC_SERVER_CONNECT_REC *)conn;
341 server_connect_ref(conn);
343 if (server->connrec->port <= 0)
344 server->connrec->port = 706;
346 server_connect_init((SERVER_REC *)server);
347 return (SERVER_REC *)server;
350 void silc_server_connect(SERVER_REC *server)
352 if (!server_start_connect(server)) {
353 server_connect_unref(server->connrec);
359 /* Return a string of all channels in server in server->channels_join()
362 char *silc_server_get_channels(SILC_SERVER_REC *server)
368 g_return_val_if_fail(server != NULL, FALSE);
370 chans = g_string_new(NULL);
371 for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
372 CHANNEL_REC *channel = tmp->data;
374 g_string_sprintfa(chans, "%s,", channel->name);
378 g_string_truncate(chans, chans->len-1);
381 g_string_free(chans, FALSE);
386 /* Syntaxes of all SILC commands for HELP files (the help file generation
387 will snoop these from here). */
389 /* SYNTAX: BAN <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
390 /* SYNTAX: CMODE <channel> +|-<modes> [{ <arguments>}] */
391 /* SYNTAX: CUMODE <channel> +|-<modes> <nickname>[@<hostname>] */
392 /* SYNTAX: GETKEY <nickname or server name> */
393 /* SYNTAX: INVITE <channel> [<nickname>[@hostname>] */
394 /* SYNTAX: INVITE <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
395 /* SYNTAX: KEY MSG <nickname> set|unset|list|agreement|negotiate [<arguments>] */
396 /* SYNTAX: KEY CHANNEL <channel> set|unset|list|change [<arguments>] */
397 /* SYNTAX: KICK <channel> <nickname>[@<hostname>] [<comment>] */
398 /* SYNTAX: KILL <nickname>[@<hostname>] [<comment>] */
399 /* SYNTAX: OPER <username> [-pubkey] */
400 /* SYNTAX: SILCOPER <username> [-pubkey] */
401 /* SYNTAX: TOPIC <channel> [<topic>] */
402 /* SYNTAX: UMODE +|-<modes> */
403 /* SYNTAX: WHOIS <nickname>[@<hostname>] [-details] [<count>] */
404 /* SYNTAX: WHOWAS <nickname>[@<hostname>] [<count>] */
405 /* SYNTAX: CLOSE <server> [<port>] */
406 /* SYNTAX: SHUTDOWN */
407 /* SYNTAX: MOTD [<server>] */
408 /* SYNTAX: LIST [<channel>] */
409 /* SYNTAX: ME <message> */
410 /* SYNTAX: ACTION <channel> <message> */
411 /* SYNTAX: AWAY [<message>] */
412 /* SYNTAX: INFO [<server>] */
413 /* SYNTAX: NICK <nickname> */
414 /* SYNTAX: NOTICE <message> */
415 /* SYNTAX: PART [<channel>] */
417 /* SYNTAX: SCONNECT <server> [<port>] */
418 /* SYNTAX: USERS <channel> */
419 /* SYNTAX: FILE SEND <filepath> <nickname> [<local IP> [<local port>]] */
420 /* SYNTAX: FILE RECEIVE [<nickname>] */
421 /* SYNTAX: FILE CLOSE [<nickname>] */
423 /* SYNTAX: JOIN <channel> [<passphrase>] [-cipher <cipher>] [-hmac <hmac>] [-founder] */
425 /* SYNTAX: WATCH [<-add | -del> <nickname>] */
427 /* SYNTAX: ATTR [<-del> <option> [{ <value>}]] */
429 void silc_command_exec(SILC_SERVER_REC *server,
430 const char *command, const char *args)
433 unsigned char **argv;
434 SilcUInt32 *argv_lens, *argv_types;
436 SilcClientCommand cmd;
437 SilcClientCommandContext ctx;
439 g_return_if_fail(server != NULL);
441 tmpcmd = g_strdup(command);
443 cmd = silc_client_command_find(silc_client, tmpcmd);
448 /* Now parse all arguments */
449 data = g_strconcat(command, " ", args, NULL);
450 silc_parse_command_line(data, &argv, &argv_lens,
451 &argv_types, &argc, cmd->max_args);
454 /* Allocate command context. This and its internals must be free'd
455 by the command routine receiving it. */
456 ctx = silc_client_command_alloc();
457 ctx->client = silc_client;
458 ctx->conn = server->conn;
462 ctx->argv_lens = argv_lens;
463 ctx->argv_types = argv_types;
465 /* Execute command */
466 silc_client_command_call(cmd, ctx);
469 /* Generic command function to call any SILC command directly. */
471 static void command_self(const char *data, SILC_SERVER_REC *server,
474 CMD_SILC_SERVER(server);
476 if (!IS_SILC_SERVER(server) || !server->connected) {
477 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
481 if (IS_SILC_CHANNEL(item)) {
482 SILC_CHANNEL_REC *chanrec;
483 chanrec = silc_channel_find(server, item->visible_name);
485 server->conn->current_channel = chanrec->entry;
488 silc_command_exec(server, current_command, data);
492 /* SCONNECT command. Calls actually SILC's CONNECT command since Irssi
493 has CONNECT command for other purposes. */
495 static void command_sconnect(const char *data, SILC_SERVER_REC *server)
497 CMD_SILC_SERVER(server);
498 if (!IS_SILC_SERVER(server) || !server->connected) {
499 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
503 silc_command_exec(server, "CONNECT", data);
509 SILC_TASK_CALLBACK(silc_client_file_close_later)
511 FtpSession ftp = (FtpSession)context;
513 SILC_LOG_DEBUG(("Start"));
515 silc_client_file_close(silc_client, ftp->conn, ftp->session_id);
516 silc_free(ftp->filepath);
520 static void silc_client_file_monitor(SilcClient client,
521 SilcClientConnection conn,
522 SilcClientMonitorStatus status,
523 SilcClientFileError error,
526 SilcClientEntry client_entry,
527 SilcUInt32 session_id,
528 const char *filepath,
531 SILC_SERVER_REC *server = (SILC_SERVER_REC *)context;
535 snprintf(fsize, sizeof(fsize) - 1, "%llu", ((filesize + 1023) / 1024));
537 silc_dlist_start(server->ftp_sessions);
538 while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
539 if (ftp->session_id == session_id) {
540 if (!ftp->filepath && filepath)
541 ftp->filepath = strdup(filepath);
546 if (ftp == SILC_LIST_END)
549 if (status == SILC_CLIENT_FILE_MONITOR_ERROR) {
550 if (error == SILC_CLIENT_FILE_NO_SUCH_FILE)
551 printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
552 SILCTXT_FILE_ERROR_NO_SUCH_FILE,
553 client_entry->nickname,
554 filepath ? filepath : "[N/A]");
555 else if (error == SILC_CLIENT_FILE_PERMISSION_DENIED)
556 printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
557 SILCTXT_FILE_ERROR_PERMISSION_DENIED,
558 client_entry->nickname);
560 printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
561 SILCTXT_FILE_ERROR, client_entry->nickname);
562 silc_schedule_task_add(silc_client->schedule, 0,
563 silc_client_file_close_later, ftp,
564 1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
565 if (ftp == server->current_session)
566 server->current_session = NULL;
567 silc_dlist_del(server->ftp_sessions, ftp);
570 if (status == SILC_CLIENT_FILE_MONITOR_KEY_AGREEMENT) {
571 printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
572 SILCTXT_FILE_KEY_EXCHANGE, client_entry->nickname);
575 /* Save some transmission data */
576 if (offset && filesize) {
577 unsigned long delta = time(NULL) - ftp->starttime;
579 ftp->percent = ((double)offset / (double)filesize) * (double)100.0;
581 ftp->kps = (double)((offset / (double)delta) + 1023) / (double)1024;
583 ftp->kps = (double)(offset + 1023) / (double)1024;
584 ftp->offset = offset;
585 ftp->filesize = filesize;
588 if (status == SILC_CLIENT_FILE_MONITOR_SEND) {
590 printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
591 SILCTXT_FILE_TRANSMIT, filepath, fsize,
592 client_entry->nickname);
593 ftp->starttime = time(NULL);
595 if (offset == filesize) {
596 printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
597 SILCTXT_FILE_TRANSMITTED, filepath, fsize,
598 client_entry->nickname, ftp->kps);
599 silc_schedule_task_add(silc_client->schedule, 0,
600 silc_client_file_close_later, ftp,
601 1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
602 if (ftp == server->current_session)
603 server->current_session = NULL;
604 silc_dlist_del(server->ftp_sessions, ftp);
608 if (status == SILC_CLIENT_FILE_MONITOR_RECEIVE) {
610 printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
611 SILCTXT_FILE_RECEIVE, filepath, fsize,
612 client_entry->nickname);
613 ftp->starttime = time(NULL);
616 if (offset == filesize) {
617 printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
618 SILCTXT_FILE_RECEIVED, filepath, fsize,
619 client_entry->nickname, ftp->kps);
620 silc_schedule_task_add(silc_client->schedule, 0,
621 silc_client_file_close_later, ftp,
622 1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
623 if (ftp == server->current_session)
624 server->current_session = NULL;
625 silc_dlist_del(server->ftp_sessions, ftp);
631 SILC_SERVER_REC *server;
637 static void silc_client_command_file_get_clients(SilcClient client,
638 SilcClientConnection conn,
639 SilcClientEntry *clients,
640 SilcUInt32 clients_count,
643 FileGetClients internal = (FileGetClients)context;
646 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown nick: %s",
648 silc_free(internal->data);
649 silc_free(internal->nick);
654 signal_emit("command file", 3, internal->data, internal->server,
657 silc_free(internal->data);
658 silc_free(internal->nick);
662 static void command_file(const char *data, SILC_SERVER_REC *server,
665 SilcClientConnection conn;
666 SilcClientEntry *entrys, client_entry;
667 SilcClientFileError ret;
668 SilcUInt32 entry_count;
669 char *nickname = NULL, *tmp;
670 unsigned char **argv;
672 SilcUInt32 *argv_lens, *argv_types;
675 char *local_ip = NULL;
676 SilcUInt32 local_port = 0;
677 SilcUInt32 session_id;
679 CMD_SILC_SERVER(server);
680 if (!server || !IS_SILC_SERVER(server) || !server->connected)
681 cmd_return_error(CMDERR_NOT_CONNECTED);
685 /* Now parse all arguments */
686 tmp = g_strconcat("FILE", " ", data, NULL);
687 silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 6);
694 if (!strcasecmp(argv[1], "send"))
696 if (!strcasecmp(argv[1], "receive"))
698 if (!strcasecmp(argv[1], "close"))
703 cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
708 cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
710 /* Parse the typed nickname. */
711 if (!silc_parse_userfqdn(argv[3], &nickname, NULL)) {
712 printformat_module("fe-common/silc", server, NULL,
713 MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[3]);
717 /* Find client entry */
718 entrys = silc_client_get_clients_local(silc_client, conn, nickname,
719 argv[3], &entry_count);
721 FileGetClients inter = silc_calloc(1, sizeof(*inter));
722 inter->server = server;
723 inter->data = strdup(data);
724 inter->nick = strdup(nickname);
726 silc_client_get_clients(silc_client, conn, nickname, argv[3],
727 silc_client_command_file_get_clients, inter);
730 client_entry = entrys[0];
736 local_port = atoi(argv[5]);
739 silc_client_file_send(silc_client, conn, silc_client_file_monitor,
740 server, local_ip, local_port,
741 client_entry, argv[2], &session_id);
742 if (ret == SILC_CLIENT_FILE_OK) {
743 ftp = silc_calloc(1, sizeof(*ftp));
744 ftp->session_id = session_id;
746 printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
747 SILCTXT_FILE_SEND, client_entry->nickname,
750 ftp->client_entry = client_entry;
751 ftp->filepath = strdup(argv[2]);
754 silc_dlist_add(server->ftp_sessions, ftp);
755 server->current_session = ftp;
757 if (ret == SILC_CLIENT_FILE_ALREADY_STARTED)
758 printformat_module("fe-common/silc", server, NULL,
759 MSGLEVEL_CRAP, SILCTXT_FILE_ALREADY_STARTED,
760 client_entry->nickname);
761 if (ret == SILC_CLIENT_FILE_NO_SUCH_FILE)
762 printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
763 SILCTXT_FILE_ERROR_NO_SUCH_FILE,
764 client_entry->nickname, argv[2]);
770 /* Parse the typed nickname. */
772 if (!silc_parse_userfqdn(argv[2], &nickname, NULL)) {
773 printformat_module("fe-common/silc", server, NULL,
774 MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[2]);
778 /* Find client entry */
779 entrys = silc_client_get_clients_local(silc_client, conn, nickname,
780 argv[2], &entry_count);
782 FileGetClients inter = silc_calloc(1, sizeof(*inter));
783 inter->server = server;
784 inter->data = strdup(data);
785 inter->nick = strdup(nickname);
787 silc_client_get_clients(silc_client, conn, nickname, argv[2],
788 silc_client_command_file_get_clients, inter);
791 client_entry = entrys[0];
794 if (!server->current_session) {
795 printformat_module("fe-common/silc", server, NULL,
796 MSGLEVEL_CRAP, SILCTXT_FILE_NA);
800 ret = silc_client_file_receive(silc_client, conn,
801 silc_client_file_monitor, server, NULL,
802 server->current_session->session_id);
803 if (ret != SILC_CLIENT_FILE_OK) {
804 if (ret == SILC_CLIENT_FILE_ALREADY_STARTED)
805 printformat_module("fe-common/silc", server, NULL,
806 MSGLEVEL_CRAP, SILCTXT_FILE_ALREADY_STARTED,
807 server->current_session->client_entry->nickname);
809 printformat_module("fe-common/silc", server, NULL,
810 MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
811 server->current_session->client_entry->nickname);
817 silc_dlist_start(server->ftp_sessions);
818 while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
819 if (ftp->client_entry == client_entry && !ftp->filepath) {
820 ret = silc_client_file_receive(silc_client, conn,
821 silc_client_file_monitor, server,
822 NULL, ftp->session_id);
823 if (ret != SILC_CLIENT_FILE_OK) {
824 if (ret == SILC_CLIENT_FILE_ALREADY_STARTED)
825 printformat_module("fe-common/silc", server, NULL,
826 MSGLEVEL_CRAP, SILCTXT_FILE_ALREADY_STARTED,
827 client_entry->nickname);
829 printformat_module("fe-common/silc", server, NULL,
830 MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
831 client_entry->nickname);
837 if (ftp == SILC_LIST_END) {
838 printformat_module("fe-common/silc", server, NULL,
839 MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
840 client_entry->nickname);
846 /* Parse the typed nickname. */
848 if (!silc_parse_userfqdn(argv[2], &nickname, NULL)) {
849 printformat_module("fe-common/silc", server, NULL,
850 MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[2]);
854 /* Find client entry */
855 entrys = silc_client_get_clients_local(silc_client, conn, nickname,
856 argv[2], &entry_count);
858 FileGetClients inter = silc_calloc(1, sizeof(*inter));
859 inter->server = server;
860 inter->data = strdup(data);
861 inter->nick = strdup(nickname);
863 silc_client_get_clients(silc_client, conn, nickname, argv[2],
864 silc_client_command_file_get_clients, inter);
867 client_entry = entrys[0];
870 if (!server->current_session) {
871 printformat_module("fe-common/silc", server, NULL,
872 MSGLEVEL_CRAP, SILCTXT_FILE_NA);
876 silc_client_file_close(silc_client, conn,
877 server->current_session->session_id);
878 printformat_module("fe-common/silc", server, NULL,
879 MSGLEVEL_CRAP, SILCTXT_FILE_CLOSED,
880 server->current_session->client_entry->nickname,
881 server->current_session->filepath ?
882 server->current_session->filepath : "[N/A]");
883 silc_dlist_del(server->ftp_sessions, server->current_session);
884 silc_free(server->current_session->filepath);
885 silc_free(server->current_session);
886 server->current_session = NULL;
890 silc_dlist_start(server->ftp_sessions);
891 while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
892 if (ftp->client_entry == client_entry) {
893 silc_client_file_close(silc_client, conn, ftp->session_id);
894 printformat_module("fe-common/silc", server, NULL,
895 MSGLEVEL_CRAP, SILCTXT_FILE_CLOSED,
896 client_entry->nickname,
897 ftp->filepath ? ftp->filepath : "[N/A]");
898 if (ftp == server->current_session)
899 server->current_session = NULL;
900 silc_dlist_del(server->ftp_sessions, ftp);
901 silc_free(ftp->filepath);
907 if (ftp == SILC_LIST_END) {
908 printformat_module("fe-common/silc", server, NULL,
909 MSGLEVEL_CRAP, SILCTXT_FILE_CLIENT_NA,
910 client_entry->nickname);
917 if (!silc_dlist_count(server->ftp_sessions)) {
918 printformat_module("fe-common/silc", server, NULL,
919 MSGLEVEL_CRAP, SILCTXT_FILE_NA);
923 printformat_module("fe-common/silc", server, NULL,
924 MSGLEVEL_CRAP, SILCTXT_FILE_SHOW_HEADER);
926 silc_dlist_start(server->ftp_sessions);
927 while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
928 printformat_module("fe-common/silc", server, NULL,
929 MSGLEVEL_CRAP, SILCTXT_FILE_SHOW_LINE,
930 ftp->client_entry->nickname,
931 ftp->send ? "send" : "receive",
932 (SilcUInt32)(ftp->offset + 1023) / 1024,
933 (SilcUInt32)(ftp->filesize + 1023) / 1024,
934 ftp->percent, ftp->kps,
935 ftp->filepath ? ftp->filepath : "[N/A]");
948 void silc_server_init(void)
950 silc_servers_reconnect_init();
952 signal_add_first("server connected", (SIGNAL_FUNC) sig_connected);
953 signal_add("server disconnected", (SIGNAL_FUNC) sig_disconnected);
954 signal_add("mime-send", (SIGNAL_FUNC)silc_send_mime);
955 command_bind_silc("whois", MODULE_NAME, (SIGNAL_FUNC) command_self);
956 command_bind_silc("whowas", MODULE_NAME, (SIGNAL_FUNC) command_self);
957 command_bind_silc("nick", MODULE_NAME, (SIGNAL_FUNC) command_self);
958 command_bind_silc("topic", MODULE_NAME, (SIGNAL_FUNC) command_self);
959 command_bind_silc("cmode", MODULE_NAME, (SIGNAL_FUNC) command_self);
960 command_bind_silc("cumode", MODULE_NAME, (SIGNAL_FUNC) command_self);
961 command_bind_silc("users", MODULE_NAME, (SIGNAL_FUNC) command_self);
962 command_bind_silc("list", MODULE_NAME, (SIGNAL_FUNC) command_self);
963 command_bind_silc("ban", MODULE_NAME, (SIGNAL_FUNC) command_self);
964 command_bind_silc("oper", MODULE_NAME, (SIGNAL_FUNC) command_self);
965 command_bind_silc("silcoper", MODULE_NAME, (SIGNAL_FUNC) command_self);
966 command_bind_silc("umode", MODULE_NAME, (SIGNAL_FUNC) command_self);
967 command_bind_silc("invite", MODULE_NAME, (SIGNAL_FUNC) command_self);
968 command_bind_silc("kill", MODULE_NAME, (SIGNAL_FUNC) command_self);
969 command_bind_silc("kick", MODULE_NAME, (SIGNAL_FUNC) command_self);
970 command_bind_silc("info", MODULE_NAME, (SIGNAL_FUNC) command_self);
971 command_bind_silc("ping", MODULE_NAME, (SIGNAL_FUNC) command_self);
972 command_bind_silc("motd", MODULE_NAME, (SIGNAL_FUNC) command_self);
973 command_bind_silc("close", MODULE_NAME, (SIGNAL_FUNC) command_self);
974 command_bind_silc("shutdown", MODULE_NAME, (SIGNAL_FUNC) command_self);
975 command_bind_silc("getkey", MODULE_NAME, (SIGNAL_FUNC) command_self);
976 command_bind_silc("sconnect", MODULE_NAME, (SIGNAL_FUNC) command_sconnect);
977 command_bind_silc("file", MODULE_NAME, (SIGNAL_FUNC) command_file);
978 command_bind_silc("detach", MODULE_NAME, (SIGNAL_FUNC) command_self);
979 command_bind_silc("watch", MODULE_NAME, (SIGNAL_FUNC) command_self);
980 command_bind_silc("stats", MODULE_NAME, (SIGNAL_FUNC) command_self);
981 command_bind_silc("attr", MODULE_NAME, (SIGNAL_FUNC) command_attr);
983 command_set_options("connect", "+silcnet");
986 void silc_server_deinit(void)
988 silc_servers_reconnect_deinit();
990 signal_remove("server connected", (SIGNAL_FUNC) sig_connected);
991 signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
992 signal_remove("mime-send", (SIGNAL_FUNC)silc_send_mime);
993 command_unbind("whois", (SIGNAL_FUNC) command_self);
994 command_unbind("whowas", (SIGNAL_FUNC) command_self);
995 command_unbind("nick", (SIGNAL_FUNC) command_self);
996 command_unbind("topic", (SIGNAL_FUNC) command_self);
997 command_unbind("cmode", (SIGNAL_FUNC) command_self);
998 command_unbind("cumode", (SIGNAL_FUNC) command_self);
999 command_unbind("users", (SIGNAL_FUNC) command_self);
1000 command_unbind("list", (SIGNAL_FUNC) command_self);
1001 command_unbind("oper", (SIGNAL_FUNC) command_self);
1002 command_unbind("silcoper", (SIGNAL_FUNC) command_self);
1003 command_unbind("umode", (SIGNAL_FUNC) command_self);
1004 command_unbind("invite", (SIGNAL_FUNC) command_self);
1005 command_unbind("kill", (SIGNAL_FUNC) command_self);
1006 command_unbind("kick", (SIGNAL_FUNC) command_self);
1007 command_unbind("info", (SIGNAL_FUNC) command_self);
1008 command_unbind("ping", (SIGNAL_FUNC) command_self);
1009 command_unbind("motd", (SIGNAL_FUNC) command_self);
1010 command_unbind("ban", (SIGNAL_FUNC) command_self);
1011 command_unbind("close", (SIGNAL_FUNC) command_self);
1012 command_unbind("shutdown", (SIGNAL_FUNC) command_self);
1013 command_unbind("getkey", (SIGNAL_FUNC) command_self);
1014 command_unbind("sconnect", (SIGNAL_FUNC) command_sconnect);
1015 command_unbind("file", (SIGNAL_FUNC) command_file);
1016 command_unbind("detach", (SIGNAL_FUNC) command_self);
1017 command_unbind("watch", (SIGNAL_FUNC) command_self);
1018 command_unbind("stats", (SIGNAL_FUNC) command_self);
1019 command_unbind("attr", (SIGNAL_FUNC) command_attr);
1022 void silc_server_free_ftp(SILC_SERVER_REC *server,
1023 SilcClientEntry client_entry)
1027 silc_dlist_start(server->ftp_sessions);
1028 while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
1029 if (ftp->client_entry == client_entry) {
1030 silc_dlist_del(server->ftp_sessions, ftp);
1031 silc_free(ftp->filepath);
1037 bool silc_term_utf8(void)
1040 str = settings_get_str("term_type");
1042 if (g_strcasecmp(str, "utf-8") == 0)