Merge commit 'origin/silc.1.1.branch'
[silc.git] / lib / silcclient / client_prvmsg.c
index c988494299b3baad131affa8e5ca676e9a5be215..1f7eaf019d90a79dc514eee760b5bdbdf977cad4 100644 (file)
 
 /************************** Private Message Send ****************************/
 
+typedef struct {
+  SilcClient client;
+  SilcClientConnection conn;
+  SilcClientEntry client_entry;
+} *SilcClientPrvmsgContext;
+
+/* Message payload encoding callback */
+
+static void silc_client_send_private_message_final(SilcBuffer message,
+                                                  void *context)
+{
+  SilcClientPrvmsgContext p = context;
+
+  /* Send the private message packet */
+  if (message)
+    silc_packet_send_ext(p->conn->stream, SILC_PACKET_PRIVATE_MESSAGE,
+                        p->client_entry->internal.send_key ?
+                        SILC_PACKET_FLAG_PRIVMSG_KEY : 0,
+                        0, NULL, SILC_ID_CLIENT, &p->client_entry->id,
+                        silc_buffer_datalen(message), NULL, NULL);
+
+  silc_client_unref_client(p->client, p->conn, p->client_entry);
+  silc_free(p);
+}
+
 /* Sends private message to remote client. */
 
 SilcBool silc_client_send_private_message(SilcClient client,
@@ -34,14 +59,16 @@ SilcBool silc_client_send_private_message(SilcClient client,
                                          unsigned char *data,
                                          SilcUInt32 data_len)
 {
-  SilcBuffer buffer;
-  SilcBool ret;
+  SilcClientPrvmsgContext p;
   SilcID sid, rid;
 
   if (silc_unlikely(!client || !conn || !client_entry))
     return FALSE;
-  if (silc_unlikely(flags & SILC_MESSAGE_FLAG_SIGNED && !hash))
+  if (silc_unlikely(flags & SILC_MESSAGE_FLAG_SIGNED && !hash)) {
+    SILC_LOG_ERROR(("Cannot send signed message without hash, missing "
+                   "arguments"));
     return FALSE;
+  }
   if (silc_unlikely(conn->internal->disconnected))
     return FALSE;
 
@@ -52,29 +79,25 @@ SilcBool silc_client_send_private_message(SilcClient client,
   rid.type = SILC_ID_CLIENT;
   rid.u.client_id = client_entry->id;
 
-  /* Encode private message payload */
-  buffer =
-    silc_message_payload_encode(flags, data, data_len,
-                               (!client_entry->internal.send_key ? FALSE :
-                                !client_entry->internal.generated),
-                               TRUE, client_entry->internal.send_key,
-                               client_entry->internal.hmac_send,
-                               client->rng, NULL, conn->private_key,
-                               hash, &sid, &rid, NULL);
-  if (silc_unlikely(!buffer)) {
-    SILC_LOG_ERROR(("Error encoding private message"));
+  p = silc_calloc(1, sizeof(*p));
+  if (!p)
     return FALSE;
-  }
 
-  /* Send the private message packet */
-  ret = silc_packet_send_ext(conn->stream, SILC_PACKET_PRIVATE_MESSAGE,
-                            client_entry->internal.send_key ?
-                            SILC_PACKET_FLAG_PRIVMSG_KEY : 0,
-                            0, NULL, SILC_ID_CLIENT, &client_entry->id,
-                            silc_buffer_datalen(buffer), NULL, NULL);
+  p->client = client;
+  p->conn = conn;
+  p->client_entry = silc_client_ref_client(client, conn, client_entry);
 
-  silc_buffer_free(buffer);
-  return ret;
+  /* Encode private message payload */
+  silc_message_payload_encode(flags, data, data_len,
+                             (!client_entry->internal.send_key ? FALSE :
+                              !client_entry->internal.generated),
+                             TRUE, client_entry->internal.send_key,
+                             client_entry->internal.hmac_send,
+                             client->rng, NULL, conn->private_key,
+                             hash, &sid, &rid, NULL,
+                             silc_client_send_private_message_final, p);
+
+  return TRUE;
 }
 
 /************************* Private Message Receive **************************/
@@ -197,14 +220,21 @@ SILC_FSM_STATE(silc_client_private_message_error)
 /* Initialize private message waiter for the `conn' connection. */
 
 SilcBool silc_client_private_message_wait_init(SilcClient client,
-                                              SilcClientConnection conn)
+                                              SilcClientConnection conn,
+                                              SilcClientEntry client_entry)
 {
-  if (conn->internal->prv_waiter)
+  SilcID id;
+
+  if (client_entry->internal.prv_waiter)
     return TRUE;
 
-  conn->internal->prv_waiter =
-    silc_packet_wait_init(conn->stream, SILC_PACKET_PRIVATE_MESSAGE, -1);
-  if (!conn->internal->prv_waiter)
+  /* We want SILC_PACKET_PRIVATE_MESSAGE packets from this source ID. */
+  id.type = SILC_ID_CLIENT;
+  id.u.client_id = client_entry->id;
+
+  client_entry->internal.prv_waiter =
+    silc_packet_wait_init(conn->stream, &id, SILC_PACKET_PRIVATE_MESSAGE, -1);
+  if (!client_entry->internal.prv_waiter)
     return FALSE;
 
   return TRUE;
@@ -213,12 +243,13 @@ SilcBool silc_client_private_message_wait_init(SilcClient client,
 /* Uninitializes private message waiter. */
 
 void silc_client_private_message_wait_uninit(SilcClient client,
-                                            SilcClientConnection conn)
+                                            SilcClientConnection conn,
+                                            SilcClientEntry client_entry)
 {
-  if (!conn->internal->prv_waiter)
+  if (!client_entry->internal.prv_waiter)
     return;
-  silc_packet_wait_uninit(conn->internal->prv_waiter, conn->stream);
-  conn->internal->prv_waiter = NULL;
+  silc_packet_wait_uninit(client_entry->internal.prv_waiter, conn->stream);
+  client_entry->internal.prv_waiter = NULL;
 }
 
 /* Blocks the calling process or thread until private message has been
@@ -230,42 +261,15 @@ SilcBool silc_client_private_message_wait(SilcClient client,
                                          SilcMessagePayload *payload)
 {
   SilcPacket packet;
-  SilcClientID remote_id;
-  SilcFSMThread thread;
 
-  if (!conn->internal->prv_waiter)
+  if (!client_entry->internal.prv_waiter)
     return FALSE;
 
   /* Block until private message arrives */
   do {
-    if ((silc_packet_wait(conn->internal->prv_waiter, 0, &packet)) < 0)
+    if ((silc_packet_wait(client_entry->internal.prv_waiter, 0, &packet)) < 0)
       return FALSE;
 
-    /* Parse sender ID */
-    if (!silc_id_str2id(packet->src_id, packet->src_id_len,
-                       SILC_ID_CLIENT, &remote_id,
-                       sizeof(remote_id))) {
-      silc_packet_free(packet);
-      continue;
-    }
-
-    /* If the private message is not for the requested client, pass it to
-       normal private message processing. */
-    if (!SILC_ID_CLIENT_COMPARE(&remote_id, &client_entry->id)) {
-      thread = silc_fsm_thread_alloc(&conn->internal->fsm, conn,
-                                    silc_client_fsm_destructor, NULL, FALSE);
-      if (!thread) {
-       silc_packet_free(packet);
-       continue;
-      }
-
-      /* The packet will be processed in the connection thread, after this
-        FSM thread is started. */
-      silc_fsm_set_state_context(thread, packet);
-      silc_fsm_start(thread, silc_client_private_message);
-      continue;
-    }
-
     /* Parse the payload and decrypt it also if private message key is set */
     *payload =
       silc_message_payload_parse(silc_buffer_data(&packet->buffer),