/* silc-server.c : irssi Copyright (C) 2000 - 2001 Timo Sirainen Pekka Riikonen 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 "net-nonblock.h" #include "net-sendbuffer.h" #include "signals.h" #include "servers.h" #include "commands.h" #include "levels.h" #include "modules.h" #include "rawlog.h" #include "misc.h" #include "settings.h" #include "servers-setup.h" #include "silc-servers.h" #include "silc-channels.h" #include "silc-queries.h" #include "silc-nicklist.h" #include "window-item-def.h" #include "fe-common/core/printtext.h" #include "fe-common/core/fe-channels.h" #include "fe-common/core/keyboard.h" #include "fe-common/silc/module-formats.h" #include "silc-commands.h" void silc_servers_reconnect_init(void); void silc_servers_reconnect_deinit(void); static void silc_send_channel(SILC_SERVER_REC *server, char *channel, char *msg) { SILC_CHANNEL_REC *rec; rec = silc_channel_find(server, channel); if (rec == NULL || rec->entry == NULL) { cmd_return_error(CMDERR_NOT_JOINED); return; } silc_client_send_channel_message(silc_client, server->conn, rec->entry, NULL, SILC_MESSAGE_FLAG_UTF8, msg, strlen(msg), TRUE); } typedef struct { char *nick; char *msg; int len; SilcMessageFlags flags; SILC_SERVER_REC *server; } PRIVMSG_REC; /* Callback function that sends the private message if the client was resolved from the server. */ static void silc_send_msg_clients(SilcClient client, SilcClientConnection conn, SilcClientEntry *clients, SilcUInt32 clients_count, void *context) { PRIVMSG_REC *rec = context; SILC_SERVER_REC *server = rec->server; SilcClientEntry target; char *nickname = NULL; if (!clients_count) { printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s: There is no such client", rec->nick); } else { if (clients_count > 1) { silc_parse_userfqdn(rec->nick, &nickname, NULL); /* Find the correct one. The rec->nick might be a formatted nick so this will find the correct one. */ clients = silc_client_get_clients_local(silc_client, server->conn, nickname, rec->nick, &clients_count); if (!clients) { printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s: There is no such client", rec->nick); silc_free(nickname); goto out; } silc_free(nickname); } target = clients[0]; /* Still check for exact math for nickname, this compares the real (formatted) nickname and the nick (maybe formatted) that use gave. This is to assure that `nick' does not match `nick@host'. */ if (strcasecmp(rec->nick, clients[0]->nickname)) { printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s: There is no such client", rec->nick); goto out; } /* Send the private message */ silc_client_send_private_message(client, conn, target, rec->flags, rec->msg, rec->len, TRUE); } out: g_free(rec->nick); g_free(rec->msg); g_free(rec); } static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg, int msg_len, SilcMessageFlags flags) { PRIVMSG_REC *rec; SilcClientEntry *clients; SilcUInt32 clients_count; char *nickname = NULL; if (!silc_parse_userfqdn(nick, &nickname, NULL)) { printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP, SILCTXT_BAD_NICK, nick); return; } /* Find client entry */ clients = silc_client_get_clients_local(silc_client, server->conn, nickname, nick, &clients_count); if (!clients) { rec = g_new0(PRIVMSG_REC, 1); rec->nick = g_strdup(nick); rec->msg = g_strdup(msg); rec->server = server; rec->flags = flags; rec->len = msg_len; /* Could not find client with that nick, resolve it from server. */ silc_client_get_clients(silc_client, server->conn, nickname, NULL, silc_send_msg_clients, rec); silc_free(nickname); return; } /* Send the private message directly */ silc_free(nickname); silc_client_send_private_message(silc_client, server->conn, clients[0], flags, msg, msg_len, TRUE); } void silc_send_mime(SILC_SERVER_REC *server, WI_ITEM_REC *to, const char *data, int data_len, const char *enc, const char *type) { SILC_CHANNEL_REC *channel; QUERY_REC *query; char *mime_data; int mime_data_len; if (!(IS_SILC_SERVER(server)) || (data == NULL) || (to == NULL) || (enc == NULL) || (type == NULL)) return; #define SILC_MIME_HEADER "MIME-Version: 1.0\r\nContent-Type: %s\r\nContent-Transfer-Encoding: %s\r\n\r\n" mime_data_len = data_len + strlen(SILC_MIME_HEADER) - 4 + strlen(enc) + strlen(type); if (mime_data_len >= SILC_PACKET_MAX_LEN) return; /* we risk to large packets here... */ mime_data = silc_calloc(mime_data_len, sizeof(*mime_data)); snprintf(mime_data, mime_data_len, SILC_MIME_HEADER, type, enc); memmove(mime_data + strlen(SILC_MIME_HEADER) - 4 + strlen(enc) + strlen(type), data, data_len); #undef SILC_MIME_HEADER if (IS_SILC_CHANNEL(to)) { channel = SILC_CHANNEL(to); silc_client_send_channel_message(silc_client, server->conn, channel->entry, NULL, SILC_MESSAGE_FLAG_DATA, mime_data, mime_data_len, TRUE); } else if (IS_SILC_QUERY(to)) { query = SILC_QUERY(to); silc_send_msg(server, query->name, mime_data, mime_data_len, SILC_MESSAGE_FLAG_DATA); } silc_free(mime_data); } static int isnickflag_func(char flag) { return flag == '@' || flag == '+'; } static int ischannel_func(SERVER_REC *server, const char *data) { return FALSE; } const char *get_nick_flags(void) { return "@\0\0"; } static void send_message(SILC_SERVER_REC *server, char *target, char *msg, int target_type) { char *message = NULL; int len; g_return_if_fail(server != NULL); g_return_if_fail(target != NULL); g_return_if_fail(msg != NULL); if (!silc_term_utf8()) { len = silc_utf8_encoded_len(msg, strlen(msg), SILC_STRING_LANGUAGE); message = silc_calloc(len + 1, sizeof(*message)); g_return_if_fail(message != NULL); silc_utf8_encode(msg, strlen(msg), SILC_STRING_LANGUAGE, message, len); } if (target_type == SEND_TARGET_CHANNEL) silc_send_channel(server, target, message ? message : msg); else silc_send_msg(server, target, message ? message : msg, message ? strlen(message) : strlen(msg), SILC_MESSAGE_FLAG_UTF8); silc_free(message); } void silc_send_heartbeat(SilcSocketConnection sock, void *hb_context) { SILC_SERVER_REC *server = SILC_SERVER(hb_context); if (server == NULL) return; silc_client_send_packet(silc_client, server->conn, SILC_PACKET_HEARTBEAT, NULL, 0); } static void sig_connected(SILC_SERVER_REC *server) { SilcClientConnection conn; SilcClientConnectionParams params; char file[256]; int fd; if (!IS_SILC_SERVER(server)) return; /* Try to read detached session data and use it if found. */ memset(¶ms, 0, sizeof(params)); memset(file, 0, sizeof(file)); snprintf(file, sizeof(file) - 1, "%s/session", get_irssi_dir()); params.detach_data = silc_file_readfile(file, ¶ms.detach_data_len); if (params.detach_data) params.detach_data[params.detach_data_len] = 0; /* Add connection to the client library */ conn = silc_client_add_connection(silc_client, ¶ms, server->connrec->address, server->connrec->port, server); server->conn = conn; if (params.detach_data) keyboard_entry_redirect(NULL, "-- Resuming old session, may take a while ...", ENTRY_REDIRECT_FLAG_HIDDEN, server); silc_free(params.detach_data); unlink(file); fd = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle)); /* Start key exchange with the server */ silc_client_start_key_exchange(silc_client, conn, fd); /* Put default attributes */ silc_query_attributes_default(silc_client, conn); /* initialize heartbeat sending */ if (settings_get_int("heartbeat") > 0) silc_socket_set_heartbeat(conn->sock, settings_get_int("heartbeat"), (void *)server, (SilcSocketConnectionHBCb)silc_send_heartbeat, silc_client->schedule); server->ftp_sessions = silc_dlist_init(); server->isnickflag = isnickflag_func; server->ischannel = ischannel_func; server->get_nick_flags = get_nick_flags; server->send_message = (void *) send_message; } static void sig_disconnected(SILC_SERVER_REC *server) { if (!IS_SILC_SERVER(server)) return; silc_dlist_uninit(server->ftp_sessions); if (server->conn && server->conn->sock != NULL) { silc_client_close_connection(silc_client, server->conn); /* SILC closes the handle */ g_io_channel_unref(net_sendbuffer_handle(server->handle)); net_sendbuffer_destroy(server->handle, FALSE); server->handle = NULL; } } SERVER_REC *silc_server_init_connect(SERVER_CONNECT_REC *conn) { SILC_SERVER_REC *server; g_return_val_if_fail(IS_SILC_SERVER_CONNECT(conn), NULL); if (conn->address == NULL || *conn->address == '\0') return NULL; if (conn->nick == NULL || *conn->nick == '\0') { silc_say_error("Cannot connect: nickname is not set"); return NULL; } server = g_new0(SILC_SERVER_REC, 1); server->chat_type = SILC_PROTOCOL; server->connrec = (SILC_SERVER_CONNECT_REC *)conn; server_connect_ref(conn); if (server->connrec->port <= 0) server->connrec->port = 706; server_connect_init((SERVER_REC *)server); return (SERVER_REC *)server; } void silc_server_connect(SERVER_REC *server) { if (!server_start_connect(server)) { server_connect_unref(server->connrec); g_free(server); return; } } /* Return a string of all channels in server in server->channels_join() format */ char *silc_server_get_channels(SILC_SERVER_REC *server) { GSList *tmp; GString *chans; char *ret; g_return_val_if_fail(server != NULL, FALSE); chans = g_string_new(NULL); for (tmp = server->channels; tmp != NULL; tmp = tmp->next) { CHANNEL_REC *channel = tmp->data; g_string_sprintfa(chans, "%s,", channel->name); } if (chans->len > 0) g_string_truncate(chans, chans->len-1); ret = chans->str; g_string_free(chans, FALSE); return ret; } /* Syntaxes of all SILC commands for HELP files (the help file generation will snoop these from here). */ /* SYNTAX: BAN [+|-[[@[![@hostname>]]]]] */ /* SYNTAX: CMODE +|- [{ }] */ /* SYNTAX: CUMODE +|- [@] */ /* SYNTAX: GETKEY */ /* SYNTAX: INVITE [[@hostname>] */ /* SYNTAX: INVITE [+|-[[@[![@hostname>]]]]] */ /* SYNTAX: KEY MSG set|unset|list|agreement|negotiate [] */ /* SYNTAX: KEY CHANNEL set|unset|list|change [] */ /* SYNTAX: KICK [@] [] */ /* SYNTAX: KILL [@] [] */ /* SYNTAX: OPER [-pubkey] */ /* SYNTAX: SILCOPER [-pubkey] */ /* SYNTAX: TOPIC [] */ /* SYNTAX: UMODE +|- */ /* SYNTAX: WHOIS [@] [-details] [] */ /* SYNTAX: WHOWAS [@] [] */ /* SYNTAX: CLOSE [] */ /* SYNTAX: SHUTDOWN */ /* SYNTAX: MOTD [] */ /* SYNTAX: LIST [] */ /* SYNTAX: ME */ /* SYNTAX: ACTION */ /* SYNTAX: AWAY [] */ /* SYNTAX: INFO [] */ /* SYNTAX: NICK */ /* SYNTAX: NOTICE */ /* SYNTAX: PART [] */ /* SYNTAX: PING */ /* SYNTAX: SCONNECT [] */ /* SYNTAX: USERS */ /* SYNTAX: FILE SEND [ []] */ /* SYNTAX: FILE RECEIVE [] */ /* SYNTAX: FILE CLOSE [] */ /* SYNTAX: FILE */ /* SYNTAX: JOIN [] [-cipher ] [-hmac ] [-founder] */ /* SYNTAX: DETACH */ /* SYNTAX: WATCH [<-add | -del> ] */ /* SYNTAX: STATS */ /* SYNTAX: ATTR [<-del>