Added STATS command. Patch by Ville Räsänen.
[silc.git] / apps / irssi / src / silc / core / silc-servers.c
index 089242715af34577d97b104ab85617306431be38..29cdca77bb13f449f2a765c6899dabfd4dca116d 100644 (file)
@@ -62,12 +62,15 @@ static void silc_send_channel(SILC_SERVER_REC *server,
   }
 
   silc_client_send_channel_message(silc_client, server->conn, rec->entry, 
-                                  NULL, 0, msg, strlen(msg), TRUE);
+                                  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;
 
@@ -119,8 +122,9 @@ static void silc_send_msg_clients(SilcClient client,
     }
 
     /* Send the private message */
-    silc_client_send_private_message(client, conn, target, 0,
-                                    rec->msg, strlen(rec->msg),
+    silc_client_send_private_message(client, conn, target, 
+                                    rec->flags,
+                                    rec->msg, rec->len,
                                     TRUE);
   }
   
@@ -130,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;
@@ -151,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,
@@ -162,7 +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], 0, 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)
@@ -183,14 +234,28 @@ const char *get_nick_flags(void)
 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, msg);
+    silc_send_channel(server, target, message ? message : msg);
   else
-    silc_send_msg(server, target, msg);
+    silc_send_msg(server, target, message ? message : msg,
+                 message ? strlen(message) : strlen(msg),
+                 SILC_MESSAGE_FLAG_UTF8);
+
+  silc_free(message);
 }
 
 static void sig_connected(SILC_SERVER_REC *server)
@@ -253,8 +318,7 @@ static void sig_disconnected(SILC_SERVER_REC *server)
   }
 }
 
-SILC_SERVER_REC *silc_server_connect(SILC_SERVER_CONNECT_REC *conn)
-{
+SILC_SERVER_REC *silc_server_connect(SILC_SERVER_CONNECT_REC *conn){
   SILC_SERVER_REC *server;
 
   g_return_val_if_fail(IS_SILC_SERVER_CONNECT(conn), NULL);
@@ -350,6 +414,7 @@ char *silc_server_get_channels(SILC_SERVER_REC *server)
 /* SYNTAX: JOIN <channel> [<passphrase>] [-cipher <cipher>] [-hmac <hmac>] [-founder] */
 /* SYNTAX: DETACH */
 /* SYNTAX: WATCH [<-add | -del> <nickname>] */
+/* SYNTAX: STATS */
 
 void silc_command_exec(SILC_SERVER_REC *server,
                       const char *command, const char *args)
@@ -876,6 +941,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);
@@ -901,6 +967,7 @@ 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_set_options("connect", "+silcnet");
 }
@@ -911,6 +978,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);
@@ -936,6 +1004,7 @@ 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);
 }
 
 void silc_server_free_ftp(SILC_SERVER_REC *server,
@@ -952,3 +1021,13 @@ void silc_server_free_ftp(SILC_SERVER_REC *server,
     }
   }
 }
+
+bool silc_term_utf8(void)
+{
+  const char *str;
+  str = settings_get_str("term_type");
+  if (str)
+    if (g_strcasecmp(str, "utf-8") == 0)
+      return TRUE;
+  return FALSE;
+}