diff options
author | Christian Grothoff <christian@grothoff.org> | 2011-02-06 23:27:01 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2011-02-06 23:27:01 +0000 |
commit | a0b3e179abb44828f76843739487b2686b14cc8e (patch) | |
tree | 52b2cfb63a66b13ce39bd1bf8bed04ff74ae102a /src/chat/chat.c | |
parent | 7b3d9e8f57db9db2053dd7e25c9dbd92514819e0 (diff) | |
download | gnunet-a0b3e179abb44828f76843739487b2686b14cc8e.tar.gz gnunet-a0b3e179abb44828f76843739487b2686b14cc8e.zip |
more chat code from Mantis #1657
Diffstat (limited to 'src/chat/chat.c')
-rw-r--r-- | src/chat/chat.c | 96 |
1 files changed, 80 insertions, 16 deletions
diff --git a/src/chat/chat.c b/src/chat/chat.c index da1237d84..f3e3470a0 100644 --- a/src/chat/chat.c +++ b/src/chat/chat.c | |||
@@ -32,12 +32,12 @@ | |||
32 | #include "gnunet_signatures.h" | 32 | #include "gnunet_signatures.h" |
33 | #include "chat.h" | 33 | #include "chat.h" |
34 | 34 | ||
35 | #define DEBUG_CHAT GNUNET_YES | 35 | #define DEBUG_CHAT GNUNET_NO |
36 | #define NICK_IDENTITY_PREFIX ".chat_identity_" | 36 | #define NICK_IDENTITY_PREFIX ".chat_identity_" |
37 | 37 | ||
38 | 38 | ||
39 | /** | 39 | /** |
40 | * Handle for a (joined) chat room. | 40 | * Handle for a chat room. |
41 | */ | 41 | */ |
42 | struct GNUNET_CHAT_Room | 42 | struct GNUNET_CHAT_Room |
43 | { | 43 | { |
@@ -53,6 +53,12 @@ struct GNUNET_CHAT_Room | |||
53 | 53 | ||
54 | struct MemberList *members; | 54 | struct MemberList *members; |
55 | 55 | ||
56 | int is_joined; | ||
57 | |||
58 | GNUNET_CHAT_JoinCallback join_callback; | ||
59 | |||
60 | void *join_callback_cls; | ||
61 | |||
56 | GNUNET_CHAT_MessageCallback message_callback; | 62 | GNUNET_CHAT_MessageCallback message_callback; |
57 | 63 | ||
58 | void *message_callback_cls; | 64 | void *message_callback_cls; |
@@ -221,7 +227,9 @@ process_result (struct GNUNET_CHAT_Room *room, | |||
221 | struct JoinNotificationMessage *join_msg; | 227 | struct JoinNotificationMessage *join_msg; |
222 | struct ReceiveNotificationMessage *received_msg; | 228 | struct ReceiveNotificationMessage *received_msg; |
223 | struct ConfirmationReceiptMessage *receipt; | 229 | struct ConfirmationReceiptMessage *receipt; |
230 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey; | ||
224 | GNUNET_HashCode id; | 231 | GNUNET_HashCode id; |
232 | const GNUNET_HashCode *sender; | ||
225 | struct GNUNET_CONTAINER_MetaData *meta; | 233 | struct GNUNET_CONTAINER_MetaData *meta; |
226 | struct GNUNET_CHAT_SendReceiptContext *src; | 234 | struct GNUNET_CHAT_SendReceiptContext *src; |
227 | struct MemberList *pos; | 235 | struct MemberList *pos; |
@@ -261,11 +269,30 @@ process_result (struct GNUNET_CHAT_Room *room, | |||
261 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), | 269 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), |
262 | &pos->id); | 270 | &pos->id); |
263 | GNUNET_PSEUDONYM_add (room->cfg, &pos->id, meta); | 271 | GNUNET_PSEUDONYM_add (room->cfg, &pos->id, meta); |
272 | pos->next = room->members; | ||
273 | room->members = pos; | ||
274 | if (GNUNET_NO == room->is_joined) | ||
275 | { | ||
276 | GNUNET_CRYPTO_rsa_key_get_public (room->my_private_key, &pkey); | ||
277 | if (0 == memcmp (&join_msg->public_key, | ||
278 | &pkey, | ||
279 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))) | ||
280 | { | ||
281 | room->join_callback (room->join_callback_cls); | ||
282 | room->is_joined = GNUNET_YES; | ||
283 | } | ||
284 | else | ||
285 | { | ||
286 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
287 | _("The current user must be the the first one joined\n")); | ||
288 | GNUNET_break (0); | ||
289 | return; | ||
290 | } | ||
291 | } | ||
292 | else | ||
264 | room->member_list_callback (room->member_list_callback_cls, | 293 | room->member_list_callback (room->member_list_callback_cls, |
265 | meta, &join_msg->public_key, | 294 | meta, &join_msg->public_key, |
266 | ntohl (join_msg->msg_options)); | 295 | ntohl (join_msg->msg_options)); |
267 | pos->next = room->members; | ||
268 | room->members = pos; | ||
269 | break; | 296 | break; |
270 | case GNUNET_MESSAGE_TYPE_CHAT_LEAVE_NOTIFICATION: | 297 | case GNUNET_MESSAGE_TYPE_CHAT_LEAVE_NOTIFICATION: |
271 | #if DEBUG_CHAT | 298 | #if DEBUG_CHAT |
@@ -347,6 +374,13 @@ process_result (struct GNUNET_CHAT_Room *room, | |||
347 | memcpy (message_content, &received_msg[1], msg_len); | 374 | memcpy (message_content, &received_msg[1], msg_len); |
348 | } | 375 | } |
349 | message_content[msg_len] = '\0'; | 376 | message_content[msg_len] = '\0'; |
377 | if (0 != (ntohl (received_msg->msg_options) & GNUNET_CHAT_MSG_ANONYMOUS)) | ||
378 | { | ||
379 | sender = NULL; | ||
380 | meta = NULL; | ||
381 | } | ||
382 | else | ||
383 | { | ||
350 | pos = room->members; | 384 | pos = room->members; |
351 | while ((NULL != pos) && | 385 | while ((NULL != pos) && |
352 | (0 != memcmp (&pos->id, | 386 | (0 != memcmp (&pos->id, |
@@ -354,11 +388,15 @@ process_result (struct GNUNET_CHAT_Room *room, | |||
354 | sizeof (GNUNET_HashCode)))) | 388 | sizeof (GNUNET_HashCode)))) |
355 | pos = pos->next; | 389 | pos = pos->next; |
356 | GNUNET_assert (NULL != pos); | 390 | GNUNET_assert (NULL != pos); |
391 | sender = &received_msg->sender; | ||
392 | meta = pos->meta; | ||
393 | } | ||
357 | room->message_callback (room->message_callback_cls, | 394 | room->message_callback (room->message_callback_cls, |
358 | room, | 395 | room, |
359 | &received_msg->sender, | 396 | sender, |
360 | pos->meta, | 397 | meta, |
361 | message_content, | 398 | message_content, |
399 | GNUNET_TIME_absolute_ntoh (received_msg->timestamp), | ||
362 | ntohl (received_msg->msg_options)); | 400 | ntohl (received_msg->msg_options)); |
363 | if (message_content != decrypted_msg) | 401 | if (message_content != decrypted_msg) |
364 | GNUNET_free (message_content); | 402 | GNUNET_free (message_content); |
@@ -378,9 +416,7 @@ process_result (struct GNUNET_CHAT_Room *room, | |||
378 | room, | 416 | room, |
379 | ntohl (receipt->sequence_number), | 417 | ntohl (receipt->sequence_number), |
380 | GNUNET_TIME_absolute_ntoh (receipt->timestamp), | 418 | GNUNET_TIME_absolute_ntoh (receipt->timestamp), |
381 | &receipt->target, | 419 | &receipt->target); |
382 | &receipt->content, | ||
383 | &receipt->signature); | ||
384 | break; | 420 | break; |
385 | default: | 421 | default: |
386 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 422 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
@@ -510,8 +546,9 @@ transmit_join_request (void *cls, | |||
510 | { | 546 | { |
511 | #if DEBUG_CHAT | 547 | #if DEBUG_CHAT |
512 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 548 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
513 | "Could not transmit join request\n"); | 549 | "Could not transmit join request, retrying...\n"); |
514 | #endif | 550 | #endif |
551 | GNUNET_CHAT_rejoin_room (chat_room); | ||
515 | return 0; | 552 | return 0; |
516 | } | 553 | } |
517 | #if DEBUG_CHAT | 554 | #if DEBUG_CHAT |
@@ -542,6 +579,10 @@ transmit_join_request (void *cls, | |||
542 | _("Could not serialize metadata\n")); | 579 | _("Could not serialize metadata\n")); |
543 | return 0; | 580 | return 0; |
544 | } | 581 | } |
582 | GNUNET_CLIENT_receive (chat_room->client, | ||
583 | &receive_results, | ||
584 | chat_room, | ||
585 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
545 | return size_of_join; | 586 | return size_of_join; |
546 | } | 587 | } |
547 | 588 | ||
@@ -623,6 +664,8 @@ GNUNET_CHAT_join_room (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
623 | struct GNUNET_CONTAINER_MetaData *member_info, | 664 | struct GNUNET_CONTAINER_MetaData *member_info, |
624 | const char *room_name, | 665 | const char *room_name, |
625 | enum GNUNET_CHAT_MsgOptions msg_options, | 666 | enum GNUNET_CHAT_MsgOptions msg_options, |
667 | GNUNET_CHAT_JoinCallback joinCallback, | ||
668 | void *join_cls, | ||
626 | GNUNET_CHAT_MessageCallback messageCallback, | 669 | GNUNET_CHAT_MessageCallback messageCallback, |
627 | void *message_cls, | 670 | void *message_cls, |
628 | GNUNET_CHAT_MemberListCallback memberCallback, | 671 | GNUNET_CHAT_MemberListCallback memberCallback, |
@@ -654,11 +697,32 @@ GNUNET_CHAT_join_room (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
654 | _("Failed to connect to the chat service\n")); | 697 | _("Failed to connect to the chat service\n")); |
655 | return NULL; | 698 | return NULL; |
656 | } | 699 | } |
700 | if (NULL == joinCallback) | ||
701 | { | ||
702 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
703 | _("Undefined mandatory parameter: joinCallback\n")); | ||
704 | return NULL; | ||
705 | } | ||
706 | if (NULL == messageCallback) | ||
707 | { | ||
708 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
709 | _("Undefined mandatory parameter: messageCallback\n")); | ||
710 | return NULL; | ||
711 | } | ||
712 | if (NULL == memberCallback) | ||
713 | { | ||
714 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
715 | _("Undefined mandatory parameter: memberCallback\n")); | ||
716 | return NULL; | ||
717 | } | ||
657 | chat_room = GNUNET_malloc (sizeof (struct GNUNET_CHAT_Room)); | 718 | chat_room = GNUNET_malloc (sizeof (struct GNUNET_CHAT_Room)); |
658 | chat_room->msg_options = msg_options; | 719 | chat_room->msg_options = msg_options; |
659 | chat_room->room_name = GNUNET_strdup (room_name); | 720 | chat_room->room_name = GNUNET_strdup (room_name); |
660 | chat_room->member_info = GNUNET_CONTAINER_meta_data_duplicate (member_info); | 721 | chat_room->member_info = GNUNET_CONTAINER_meta_data_duplicate (member_info); |
661 | chat_room->my_private_key = priv_key; | 722 | chat_room->my_private_key = priv_key; |
723 | chat_room->is_joined = GNUNET_NO; | ||
724 | chat_room->join_callback = joinCallback; | ||
725 | chat_room->join_callback_cls = join_cls; | ||
662 | chat_room->message_callback = messageCallback; | 726 | chat_room->message_callback = messageCallback; |
663 | chat_room->message_callback_cls = message_cls; | 727 | chat_room->message_callback_cls = message_cls; |
664 | chat_room->member_list_callback = memberCallback; | 728 | chat_room->member_list_callback = memberCallback; |
@@ -668,10 +732,6 @@ GNUNET_CHAT_join_room (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
668 | chat_room->cfg = cfg; | 732 | chat_room->cfg = cfg; |
669 | chat_room->client = client; | 733 | chat_room->client = client; |
670 | chat_room->members = NULL; | 734 | chat_room->members = NULL; |
671 | GNUNET_CLIENT_receive (client, | ||
672 | &receive_results, | ||
673 | chat_room, | ||
674 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
675 | if (GNUNET_SYSERR == GNUNET_CHAT_rejoin_room (chat_room)) | 735 | if (GNUNET_SYSERR == GNUNET_CHAT_rejoin_room (chat_room)) |
676 | { | 736 | { |
677 | GNUNET_CHAT_leave_room (chat_room); | 737 | GNUNET_CHAT_leave_room (chat_room); |
@@ -717,6 +777,8 @@ transmit_send_request (void *cls, | |||
717 | msg_to_send->header.type = htons (GNUNET_MESSAGE_TYPE_CHAT_TRANSMIT_REQUEST); | 777 | msg_to_send->header.type = htons (GNUNET_MESSAGE_TYPE_CHAT_TRANSMIT_REQUEST); |
718 | msg_to_send->msg_options = htonl (smc->options); | 778 | msg_to_send->msg_options = htonl (smc->options); |
719 | msg_to_send->sequence_number = htonl (smc->sequence_number); | 779 | msg_to_send->sequence_number = htonl (smc->sequence_number); |
780 | msg_to_send->timestamp = | ||
781 | GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ()); | ||
720 | msg_to_send->reserved = htonl (0); | 782 | msg_to_send->reserved = htonl (0); |
721 | if (NULL == smc->receiver) | 783 | if (NULL == smc->receiver) |
722 | memset (&msg_to_send->target, 0, sizeof (GNUNET_HashCode)); | 784 | memset (&msg_to_send->target, 0, sizeof (GNUNET_HashCode)); |
@@ -770,13 +832,15 @@ GNUNET_CHAT_send_message (struct GNUNET_CHAT_Room *room, | |||
770 | #if DEBUG_CHAT | 832 | #if DEBUG_CHAT |
771 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending a message\n"); | 833 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending a message\n"); |
772 | #endif | 834 | #endif |
773 | *sequence_number = ++room->sequence_number; | 835 | room->sequence_number++; |
836 | if (NULL != sequence_number) | ||
837 | *sequence_number = room->sequence_number; | ||
774 | smc = GNUNET_malloc (sizeof (struct GNUNET_CHAT_SendMessageContext)); | 838 | smc = GNUNET_malloc (sizeof (struct GNUNET_CHAT_SendMessageContext)); |
775 | smc->chat_room = room; | 839 | smc->chat_room = room; |
776 | smc->message = GNUNET_strdup (message); | 840 | smc->message = GNUNET_strdup (message); |
777 | smc->options = options; | 841 | smc->options = options; |
778 | smc->receiver = receiver; | 842 | smc->receiver = receiver; |
779 | smc->sequence_number = *sequence_number; | 843 | smc->sequence_number = room->sequence_number; |
780 | msg_size = strlen (message) + sizeof (struct TransmitRequestMessage); | 844 | msg_size = strlen (message) + sizeof (struct TransmitRequestMessage); |
781 | GNUNET_CLIENT_notify_transmit_ready (room->client, | 845 | GNUNET_CLIENT_notify_transmit_ready (room->client, |
782 | msg_size, | 846 | msg_size, |