Protocol version 1.2 integrations
[silc.git] / apps / irssi / src / silc / core / silc-servers.c
index ae34c5c70806033c6c42adc0b82a09631fc49ae0..4d68aeb0ad0cde2bd8433f716275ea84a31e6ddf 100644 (file)
@@ -69,6 +69,8 @@ static void silc_send_channel(SILC_SERVER_REC *server,
 typedef struct {
   char *nick;
   char *msg;
+  int len;
+  SilcMessageFlags flags;
   SILC_SERVER_REC *server;
 } PRIVMSG_REC;
 
@@ -121,8 +123,8 @@ static void silc_send_msg_clients(SilcClient client,
 
     /* Send the private message */
     silc_client_send_private_message(client, conn, target, 
-                                    SILC_MESSAGE_FLAG_UTF8,
-                                    rec->msg, strlen(rec->msg),
+                                    rec->flags,
+                                    rec->msg, rec->len,
                                     TRUE);
   }
   
@@ -132,7 +134,8 @@ static void silc_send_msg_clients(SilcClient client,
   g_free(rec);
 }
 
-static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg)
+static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg,
+                       int msg_len, SilcMessageFlags flags)
 {
   PRIVMSG_REC *rec;
   SilcClientEntry *clients;
@@ -153,6 +156,8 @@ static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg)
     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,
@@ -164,8 +169,51 @@ static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg)
   /* Send the private message directly */
   silc_free(nickname);
   silc_client_send_private_message(silc_client, server->conn, 
-                                  clients[0], SILC_MESSAGE_FLAG_UTF8,
-                                  msg, strlen(msg), TRUE);
+                                  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)
@@ -194,20 +242,34 @@ static void send_message(SILC_SERVER_REC *server, char *target,
   g_return_if_fail(msg != NULL);
 
   if (!silc_term_utf8()) {
-    len = silc_utf8_encoded_len(msg, strlen(msg), SILC_STRING_ASCII);
+    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_ASCII, message, len);
+    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);
+    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;
@@ -223,6 +285,8 @@ static void sig_connected(SILC_SERVER_REC *server)
   memset(file, 0, sizeof(file));
   snprintf(file, sizeof(file) - 1, "%s/session", get_irssi_dir());
   params.detach_data = silc_file_readfile(file, &params.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, &params,
@@ -244,6 +308,16 @@ static void sig_connected(SILC_SERVER_REC *server)
   /* 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;
@@ -268,33 +342,37 @@ static void sig_disconnected(SILC_SERVER_REC *server)
   }
 }
 
-SILC_SERVER_REC *silc_server_connect(SILC_SERVER_CONNECT_REC *conn){
+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') {
-    printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, 
-             "Cannot connect: nickname is not set");
+    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 = conn;
+  server->connrec = (SILC_SERVER_CONNECT_REC *)conn;
+  server_connect_ref(conn);
+
   if (server->connrec->port <= 0) 
     server->connrec->port = 706;
 
-  server_connect_ref(SERVER_CONNECT(conn));
+  server_connect_init((SERVER_REC *)server);
+  return (SERVER_REC *)server;
+}
 
-  if (!server_start_connect((SERVER_REC *) server)) {
-    server_connect_unref(SERVER_CONNECT(conn));
+void silc_server_connect(SERVER_REC *server)
+{
+  if (!server_start_connect(server)) {
+    server_connect_unref(server->connrec);
     g_free(server);
-    return NULL;
+    return;
   }
-
-  return server;
 }
 
 /* Return a string of all channels in server in server->channels_join() 
@@ -336,12 +414,12 @@ char *silc_server_get_channels(SILC_SERVER_REC *server)
 /* SYNTAX: KEY MSG <nickname> set|unset|list|agreement|negotiate [<arguments>] */
 /* SYNTAX: KEY CHANNEL <channel> set|unset|list|change [<arguments>] */
 /* SYNTAX: KICK <channel> <nickname>[@<hostname>] [<comment>] */
-/* SYNTAX: KILL <nickname>[@<hostname>] [<comment>] */
+/* SYNTAX: KILL <nickname>[@<hostname>] [<comment>] [-pubkey] */
 /* SYNTAX: OPER <username> [-pubkey] */
 /* SYNTAX: SILCOPER <username> [-pubkey] */
 /* SYNTAX: TOPIC <channel> [<topic>] */
 /* SYNTAX: UMODE +|-<modes> */
-/* SYNTAX: WHOIS <nickname>[@<hostname>] [<count>] */
+/* SYNTAX: WHOIS <nickname>[@<hostname>] [-details] [<count>] */
 /* SYNTAX: WHOWAS <nickname>[@<hostname>] [<count>] */
 /* SYNTAX: CLOSE <server> [<port>] */
 /* SYNTAX: SHUTDOWN */
@@ -357,52 +435,26 @@ char *silc_server_get_channels(SILC_SERVER_REC *server)
 /* SYNTAX: PING */
 /* SYNTAX: SCONNECT <server> [<port>] */
 /* SYNTAX: USERS <channel> */
-/* SYNTAX: FILE SEND <filepath> <nickname> [<local IP> [<local port>]] */
-/* SYNTAX: FILE RECEIVE [<nickname>] */
+/* SYNTAX: FILE SEND <filepath> <nickname> [<local IP> [<local port>]] [-no-listener]*/
+/* SYNTAX: FILE ACCEPT [<nickname>] */
 /* SYNTAX: FILE CLOSE [<nickname>] */
 /* SYNTAX: FILE */
 /* SYNTAX: JOIN <channel> [<passphrase>] [-cipher <cipher>] [-hmac <hmac>] [-founder] */
 /* SYNTAX: DETACH */
 /* SYNTAX: WATCH [<-add | -del> <nickname>] */
+/* SYNTAX: STATS */
+/* SYNTAX: ATTR [<-del> <option> [{ <value>}]] */
 
 void silc_command_exec(SILC_SERVER_REC *server,
                       const char *command, const char *args)
 {
-  SilcUInt32 argc = 0;
-  unsigned char **argv;
-  SilcUInt32 *argv_lens, *argv_types;
-  char *data, *tmpcmd;
-  SilcClientCommand cmd;
-  SilcClientCommandContext ctx;
-
+  char *data;
   g_return_if_fail(server != NULL);
 
-  tmpcmd = g_strdup(command); 
-  g_strup(tmpcmd);
-  cmd = silc_client_command_find(silc_client, tmpcmd);
-  g_free(tmpcmd);
-  if (cmd == NULL)
-    return;
-  
-  /* Now parse all arguments */
+  /* Call the command */
   data = g_strconcat(command, " ", args, NULL);
-  silc_parse_command_line(data, &argv, &argv_lens,
-                         &argv_types, &argc, cmd->max_args);
+  silc_client_command_call(silc_client, server->conn, data);
   g_free(data);
-
-  /* Allocate command context. This and its internals must be free'd
-     by the command routine receiving it. */
-  ctx = silc_client_command_alloc();
-  ctx->client = silc_client;
-  ctx->conn = server->conn;
-  ctx->command = cmd;
-  ctx->argc = argc;
-  ctx->argv = argv;
-  ctx->argv_lens = argv_lens;
-  ctx->argv_types = argv_types;
-  
-  /* Execute command */
-  silc_client_command_call(cmd, ctx);
 }
 
 /* Generic command function to call any SILC command directly. */
@@ -419,7 +471,7 @@ static void command_self(const char *data, SILC_SERVER_REC *server,
 
   if (IS_SILC_CHANNEL(item)) {
     SILC_CHANNEL_REC *chanrec;
-    chanrec = silc_channel_find(server, item->name);
+    chanrec = silc_channel_find(server, item->visible_name);
     if (chanrec)
       server->conn->current_channel = chanrec->entry;
   }
@@ -614,6 +666,7 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
   char *local_ip = NULL;
   SilcUInt32 local_port = 0;
   SilcUInt32 session_id;
+  bool do_not_bind = FALSE;
 
   CMD_SILC_SERVER(server);
   if (!server || !IS_SILC_SERVER(server) || !server->connected)
@@ -623,7 +676,7 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
 
   /* Now parse all arguments */
   tmp = g_strconcat("FILE", " ", data, NULL);
-  silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 6);
+  silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 7);
   g_free(tmp);
 
   if (argc == 1)
@@ -632,7 +685,7 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
   if (argc >= 2) {
     if (!strcasecmp(argv[1], "send"))
       type = 1;
-    if (!strcasecmp(argv[1], "receive"))
+    if (!strcasecmp(argv[1], "accept"))
       type = 2;
     if (!strcasecmp(argv[1], "close"))
       type = 3;
@@ -669,14 +722,26 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
     client_entry = entrys[0];
     silc_free(entrys);
 
-    if (argc >= 5)
-      local_ip = argv[4];
-    if (argc >= 6)
-      local_port = atoi(argv[5]);
+    if (argc >= 5) {
+      if (!strcasecmp(argv[4], "-no-listener"))
+       do_not_bind = TRUE;
+      else
+       local_ip = argv[4];
+    }
+    if (argc >= 6) {
+      if (!strcasecmp(argv[5], "-no-listener"))
+       do_not_bind = TRUE;
+      else
+       local_port = atoi(argv[5]);
+    }
+    if (argc >= 7) {
+      if (!strcasecmp(argv[6], "-no-listener"))
+       do_not_bind = TRUE;
+    }
 
     ret = 
       silc_client_file_send(silc_client, conn, silc_client_file_monitor, 
-                           server, local_ip, local_port, 
+                           server, local_ip, local_port, do_not_bind,
                            client_entry, argv[2], &session_id);
     if (ret == SILC_CLIENT_FILE_OK) {
       ftp = silc_calloc(1, sizeof(*ftp));
@@ -890,6 +955,7 @@ void silc_server_init(void)
 
   signal_add_first("server connected", (SIGNAL_FUNC) sig_connected);
   signal_add("server disconnected", (SIGNAL_FUNC) sig_disconnected);
+  signal_add("mime-send", (SIGNAL_FUNC)silc_send_mime);
   command_bind_silc("whois", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind_silc("whowas", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind_silc("nick", MODULE_NAME, (SIGNAL_FUNC) command_self);
@@ -915,6 +981,8 @@ void silc_server_init(void)
   command_bind_silc("file", MODULE_NAME, (SIGNAL_FUNC) command_file);
   command_bind_silc("detach", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind_silc("watch", MODULE_NAME, (SIGNAL_FUNC) command_self);
+  command_bind_silc("stats", MODULE_NAME, (SIGNAL_FUNC) command_self);
+  command_bind_silc("attr", MODULE_NAME, (SIGNAL_FUNC) command_attr);
 
   command_set_options("connect", "+silcnet");
 }
@@ -925,6 +993,7 @@ void silc_server_deinit(void)
 
   signal_remove("server connected", (SIGNAL_FUNC) sig_connected);
   signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
+  signal_remove("mime-send", (SIGNAL_FUNC)silc_send_mime);
   command_unbind("whois", (SIGNAL_FUNC) command_self);
   command_unbind("whowas", (SIGNAL_FUNC) command_self);
   command_unbind("nick", (SIGNAL_FUNC) command_self);
@@ -950,6 +1019,8 @@ void silc_server_deinit(void)
   command_unbind("file", (SIGNAL_FUNC) command_file);
   command_unbind("detach", (SIGNAL_FUNC) command_self);
   command_unbind("watch", (SIGNAL_FUNC) command_self);
+  command_unbind("stats", (SIGNAL_FUNC) command_self);
+  command_unbind("attr", (SIGNAL_FUNC) command_attr);
 }
 
 void silc_server_free_ftp(SILC_SERVER_REC *server,