updates.
[silc.git] / apps / irssi / src / silc / core / silc-servers.c
index 50233995048e60bd1ed2302fcf3d7876f6fcceae..210740a1b863ae196f6bea2c3665cdfb4536205c 100644 (file)
 #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/silc/module-formats.h"
 
 void silc_servers_reconnect_init(void);
 void silc_servers_reconnect_deinit(void);
@@ -50,7 +52,7 @@ static void silc_send_channel(SILC_SERVER_REC *server,
   SILC_CHANNEL_REC *rec;
   
   rec = silc_channel_find(server, channel);
-  if (rec == NULL)
+  if (rec == NULL || rec->entry == NULL)
     return;
   
   silc_client_send_channel_message(silc_client, server->conn, rec->entry, 
@@ -60,6 +62,7 @@ static void silc_send_channel(SILC_SERVER_REC *server,
 typedef struct {
   char *nick;
   char *msg;
+  SILC_SERVER_REC *server;
 } PRIVMSG_REC;
 
 /* Callback function that sends the private message if the client was
@@ -72,18 +75,39 @@ static void silc_send_msg_clients(SilcClient client,
                                  void *context)
 {
   PRIVMSG_REC *rec = context;
+  SILC_SERVER_REC *server = rec->server;
   SilcClientEntry target;
-  
-  if (clients_count == 0) {
+  char *nickname = NULL;
+
+  if (!clients_count) {
     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown nick: %s", rec->nick);
   } else {
-    target = clients[0]; /* FIXME: not a good idea :) */
-    
+    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, "Unknown nick: %s", 
+                 rec->nick);
+       silc_free(nickname);
+       goto out;
+      }
+      silc_free(nickname);
+    }
+
+    target = clients[0];
+
+    /* Send the private message */
     silc_client_send_private_message(client, conn, target, 0,
                                     rec->msg, strlen(rec->msg),
                                     TRUE);
   }
   
+ out:
   g_free(rec->nick);
   g_free(rec->msg);
   g_free(rec);
@@ -92,32 +116,36 @@ static void silc_send_msg_clients(SilcClient client,
 static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg)
 {
   PRIVMSG_REC *rec;
-  SilcClientEntry client_entry;
-  uint32 num = 0;
-  char *nickname = NULL, *serv = NULL;
-  
-  if (!silc_parse_nickname(nick, &nickname, &serv, &num)) {
-    printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Bad nickname: %s", nick);
+  SilcClientEntry *clients;
+  uint32 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 */
-  client_entry = silc_idlist_get_client(silc_client, server->conn, 
-                                       nickname, serv, num, FALSE);
-  if (!client_entry) {
+  clients = silc_client_get_clients_local(silc_client, server->conn, 
+                                         nickname, nick, &clients_count);
+  silc_free(nickname);
+
+  if (!clients) {
     rec = g_new0(PRIVMSG_REC, 1);
     rec->nick = g_strdup(nick);
     rec->msg = g_strdup(msg);
+    rec->server = server;
 
     /* Could not find client with that nick, resolve it from server. */
     silc_client_get_clients(silc_client, server->conn,
-                           nickname, serv, silc_send_msg_clients, rec);
+                           nickname, NULL, silc_send_msg_clients, rec);
     return;
   }
 
   /* Send the private message directly */
-  silc_client_send_private_message(silc_client, server->conn, client_entry, 0,
-                                  msg, strlen(msg), TRUE);
+  silc_client_send_private_message(silc_client, server->conn, 
+                                  clients[0], 0, msg, strlen(msg), TRUE);
 }
 
 static int isnickflag_func(char flag)
@@ -177,10 +205,10 @@ static void sig_connected(SILC_SERVER_REC *server)
 
 static void sig_disconnected(SILC_SERVER_REC *server)
 {
-  if (!IS_SILC_SERVER(server) || server->conn == NULL)
+  if (!IS_SILC_SERVER(server))
     return;
   
-  if (server->conn->sock != NULL) {
+  if (server->conn && server->conn->sock != NULL) {
     silc_client_close_connection(silc_client, NULL, server->conn);
     
     /* SILC closes the handle */
@@ -245,6 +273,40 @@ char *silc_server_get_channels(SILC_SERVER_REC *server)
   return ret;
 }
 
+/* Syntaxes of all SILC commands for HELP files (the help file generation
+   will snoop these from here). */
+
+/* SYNTAX: BAN <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
+/* SYNTAX: CMODE <channel> +|-<modes> [{ <arguments>}] */
+/* SYNTAX: CUMODE <channel> +|-<modes> <nickname>[@<hostname>] [-pubkey|<passwd>] */
+/* SYNTAX: GETKEY <nickname or server name> */
+/* SYNTAX: INVITE <channel> [<nickname>[@hostname>] */
+/* SYNTAX: INVITE <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
+/* SYNTAX: KEY MSG <nickname> set|unset|list|agreement|negotiate [<arguments>] */
+/* SYNTAX: KEY CHANNEL <channel> set|unset|list|agreement|negotiate [<arguments>] */
+/* SYNTAX: KICK <channel> <nickname>[@<hostname>] [<comment>] */
+/* SYNTAX: KILL <nickname>[@<hostname>] [<comment>] */
+/* SYNTAX: OPER <username> [-pubkey] */
+/* SYNTAX: SILCOPER <username> [-pubkey] */
+/* SYNTAX: TOPIC <channel> [<topic>] */
+/* SYNTAX: UMODE +|-<modes> */
+/* SYNTAX: WHOIS <nickname>[@<hostname>] [<count>] */
+/* SYNTAX: WHOWAS <nickname>[@<hostname>] [<count>] */
+/* SYNTAX: CLOSE <server> [<port>] */
+/* SYNTAX: SHUTDOWN */
+/* SYNTAX: MOTD [<server>] */
+/* SYNTAX: LIST [<channel>] */
+/* SYNTAX: ME <message> */
+/* SYNTAX: ACTION <channel> <message> */
+/* SYNTAX: AWAY [<message>] */
+/* SYNTAX: INFO [<server>] */
+/* SYNTAX: NICK <nickname> */
+/* SYNTAX: NOTICE <message> */
+/* SYNTAX: PART [<channel>] */
+/* SYNTAX: PING */
+/* SYNTAX: SCONNECT <server> [<port>] */
+/* SYNTAX: USERS <channel> */
+
 void silc_command_exec(SILC_SERVER_REC *server,
                       const char *command, const char *args)
 {
@@ -282,20 +344,44 @@ void silc_command_exec(SILC_SERVER_REC *server,
   ctx->argv_types = argv_types;
   
   /* Execute command */
-  (*cmd->cb)(ctx);
+  (*cmd->cb)(ctx, NULL);
 }
 
-static void command_self(const char *data, SILC_SERVER_REC *server)
+/* Generic command function to call any SILC command directly. */
+
+static void command_self(const char *data, SILC_SERVER_REC *server,
+                        WI_ITEM_REC *item)
 {
   if (!IS_SILC_SERVER(server) || !server->connected) {
     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
     return;
   }
 
+  if (IS_SILC_CHANNEL(item)) {
+    SILC_CHANNEL_REC *chanrec;
+    chanrec = silc_channel_find(server, item->name);
+    if (chanrec)
+      server->conn->current_channel = chanrec->entry;
+  }
+
   silc_command_exec(server, current_command, data);
   signal_stop();
 }
 
+/* SCONNECT command.  Calls actually SILC's CONNECT command since Irssi
+   has CONNECT command for other purposes. */
+
+static void command_sconnect(const char *data, SILC_SERVER_REC *server)
+{
+  if (!IS_SILC_SERVER(server) || !server->connected) {
+    printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
+    return;
+  }
+
+  silc_command_exec(server, "CONNECT", data);
+  signal_stop();
+}
+
 static void event_text(const char *line, SILC_SERVER_REC *server,
                       WI_ITEM_REC *item)
 {
@@ -313,6 +399,90 @@ static void event_text(const char *line, SILC_SERVER_REC *server,
   signal_stop();
 }
 
+typedef struct {
+  SILC_SERVER_REC *server;
+  char *data;
+  WI_ITEM_REC *item;
+} *FileGetClients;
+
+SILC_CLIENT_CMD_FUNC(file_get_clients)
+{
+  FileGetClients internal = (FileGetClients)context;
+  signal_emit("command file", 3, internal->data, internal->server,
+             internal->item);
+  silc_free(internal->data);
+  silc_free(internal);
+}
+
+static void command_file(const char *data, SILC_SERVER_REC *server,
+                        WI_ITEM_REC *item)
+{
+  SilcClientConnection conn;
+  SilcClientEntry client_entry;
+  char *nickname, *tmp;
+  unsigned char **argv;
+  uint32 argc;
+  uint32 *argv_lens, *argv_types;
+  int type;
+
+  if (!server || !IS_SILC_SERVER(server) || !server->connected)
+    cmd_return_error(CMDERR_NOT_CONNECTED);
+
+  conn = server->conn;
+
+  /* Now parse all arguments */
+  tmp = g_strconcat("KEY", " ", data, NULL);
+  silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 4);
+  g_free(tmp);
+
+  type = 0;
+  if (!strcasecmp(argv[1], "send"))
+    type = 1;
+  if (!strcasecmp(argv[1], "receive"))
+    type = 2;
+  
+  /* Parse the typed nickname. */
+  if (!silc_parse_userfqdn(argv[3], &nickname, NULL)) {
+    printformat_module("fe-common/silc", server, NULL,
+                      MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[2]);
+    return;
+  }
+
+  /* Find client entry */
+  client_entry = silc_idlist_get_client(silc_client, conn, nickname, 
+                                       argv[3], TRUE);
+  if (!client_entry) {
+    FileGetClients inter = silc_calloc(1, sizeof(*inter));
+    inter->server = server;
+    inter->data = strdup(data);
+    inter->item = item;
+    
+    /* Client entry not found, it was requested thus mark this to be
+       pending command. */
+    silc_client_command_pending(conn, SILC_COMMAND_IDENTIFY, 
+                               conn->cmd_ident, 
+                               NULL, silc_client_command_file_get_clients, 
+                               inter);
+    goto out;
+  }
+
+  switch (type) {
+  case 1:
+    silc_client_file_send(silc_client, conn, NULL, NULL, client_entry,
+                         argv[2]);
+    break;
+
+  case 2:
+    break;
+
+  default:
+    break;
+  }
+
+ out:
+  silc_free(nickname);
+}
+
 void silc_server_init(void)
 {
   silc_servers_reconnect_init();
@@ -329,18 +499,20 @@ void silc_server_init(void)
   command_bind("users", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind("list", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind("ban", MODULE_NAME, (SIGNAL_FUNC) command_self);
+  command_bind("oper", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind("silcoper", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind("umode", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind("invite", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind("kill", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind("kick", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind("info", MODULE_NAME, (SIGNAL_FUNC) command_self);
-  command_bind("connect", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind("ping", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind("motd", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind("close", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind("shutdown", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind("getkey", MODULE_NAME, (SIGNAL_FUNC) command_self);
+  command_bind("sconnect", MODULE_NAME, (SIGNAL_FUNC) command_sconnect);
+  command_bind("file", MODULE_NAME, (SIGNAL_FUNC) command_file);
 
   command_set_options("connect", "+silcnet");
 }
@@ -360,17 +532,19 @@ void silc_server_deinit(void)
   command_unbind("cumode", (SIGNAL_FUNC) command_self);
   command_unbind("users", (SIGNAL_FUNC) command_self);
   command_unbind("list", (SIGNAL_FUNC) command_self);
+  command_unbind("oper", (SIGNAL_FUNC) command_self);
   command_unbind("silcoper", (SIGNAL_FUNC) command_self);
   command_unbind("umode", (SIGNAL_FUNC) command_self);
   command_unbind("invite", (SIGNAL_FUNC) command_self);
   command_unbind("kill", (SIGNAL_FUNC) command_self);
   command_unbind("kick", (SIGNAL_FUNC) command_self);
   command_unbind("info", (SIGNAL_FUNC) command_self);
-  command_unbind("connect", (SIGNAL_FUNC) command_self);
   command_unbind("ping", (SIGNAL_FUNC) command_self);
   command_unbind("motd", (SIGNAL_FUNC) command_self);
   command_unbind("ban", (SIGNAL_FUNC) command_self);
   command_unbind("close", (SIGNAL_FUNC) command_self);
   command_unbind("shutdown", (SIGNAL_FUNC) command_self);
   command_unbind("getkey", (SIGNAL_FUNC) command_self);
+  command_unbind("sconnect", (SIGNAL_FUNC) command_sconnect);
+  command_unbind("file", (SIGNAL_FUNC) command_file);
 }