/************************** 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,
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;
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 **************************/
/* 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;
/* 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
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),
silc_free(keys);
}
+/* Return private message key from the client entry. */
+
+SilcBool
+silc_client_private_message_key_is_set(SilcClient client,
+ SilcClientConnection conn,
+ SilcClientEntry client_entry)
+{
+ return client_entry->internal.send_key != NULL;
+}
+
/* Sets away `message'. The away message may be set when the client's
mode is changed to SILC_UMODE_GONE and the client whishes to reply
to anyone who sends private message. The `message' will be sent