From f407a9417dfff9e6bf3363573e42e85bf2191790 Mon Sep 17 00:00:00 2001 From: TheJackiMonster Date: Mon, 8 Nov 2021 00:25:45 +0100 Subject: Added functions to get contact or group of a context Signed-off-by: TheJackiMonster --- include/gnunet_chat_lib.h | 56 ++++++++++++++++++++++++++++++++++---------- src/gnunet_chat_lib.c | 55 +++++++++++++++++++++++++++++++++++++++++++ src/gnunet_chat_lib_intern.c | 17 ++++++++++++++ 3 files changed, 115 insertions(+), 13 deletions(-) diff --git a/include/gnunet_chat_lib.h b/include/gnunet_chat_lib.h index 06b7478..2023323 100644 --- a/include/gnunet_chat_lib.h +++ b/include/gnunet_chat_lib.h @@ -139,7 +139,8 @@ struct GNUNET_CHAT_Invitation; * @return #GNUNET_YES if we should continue to iterate, #GNUNET_NO otherwise. */ typedef int -(*GNUNET_CHAT_ContactCallback) (void *cls, struct GNUNET_CHAT_Handle *handle, +(*GNUNET_CHAT_ContactCallback) (void *cls, + struct GNUNET_CHAT_Handle *handle, struct GNUNET_CHAT_Contact *contact); /** @@ -151,7 +152,8 @@ typedef int * @return #GNUNET_YES if we should continue to iterate, #GNUNET_NO otherwise. */ typedef int -(*GNUNET_CHAT_GroupCallback) (void *cls, struct GNUNET_CHAT_Handle *handle, +(*GNUNET_CHAT_GroupCallback) (void *cls, + struct GNUNET_CHAT_Handle *handle, struct GNUNET_CHAT_Group *group); /** @@ -163,8 +165,9 @@ typedef int * @return #GNUNET_YES if we should continue to iterate, #GNUNET_NO otherwise. */ typedef int -(*GNUNET_CHAT_GroupContactCallback) (void *cls, struct GNUNET_CHAT_Group *group, - struct GNUNET_CHAT_Contact *contact); +(*GNUNET_CHAT_GroupContactCallback) (void *cls, + struct GNUNET_CHAT_Group *group, + struct GNUNET_CHAT_Contact *contact); /** * Iterator over chat messages in a specific chat context. @@ -175,7 +178,8 @@ typedef int * @return #GNUNET_YES if we should continue to iterate, #GNUNET_NO otherwise. */ typedef int -(*GNUNET_CHAT_ContextMessageCallback) (void *cls, struct GNUNET_CHAT_Context *context, +(*GNUNET_CHAT_ContextMessageCallback) (void *cls, + struct GNUNET_CHAT_Context *context, const struct GNUNET_CHAT_Message *message); /** @@ -187,7 +191,8 @@ typedef int * @return #GNUNET_YES if we should continue to iterate, #GNUNET_NO otherwise. */ typedef int -(*GNUNET_CHAT_ContextFileCallback) (void *cls, struct GNUNET_CHAT_Context *context, +(*GNUNET_CHAT_ContextFileCallback) (void *cls, + struct GNUNET_CHAT_Context *context, struct GNUNET_CHAT_File *file); /** @@ -202,7 +207,8 @@ typedef int * @return #GNUNET_YES if we should continue to iterate, #GNUNET_NO otherwise. */ typedef int -(*GNUNET_CHAT_MessageReadReceiptCallback) (void *cls, const struct GNUNET_CHAT_Message *message, +(*GNUNET_CHAT_MessageReadReceiptCallback) (void *cls, + const struct GNUNET_CHAT_Message *message, const struct GNUNET_CHAT_Contact *contact, int read_receipt); @@ -215,8 +221,10 @@ typedef int * @param[in] size Full size of the uploading file (in bytes) */ typedef void -(*GNUNET_CHAT_FileUploadCallback) (void *cls, const struct GNUNET_CHAT_File *file, - uint64_t completed, uint64_t size); +(*GNUNET_CHAT_FileUploadCallback) (void *cls, + const struct GNUNET_CHAT_File *file, + uint64_t completed, + uint64_t size); /** * Method called during a download of a specific file in a chat which was shared. @@ -227,8 +235,10 @@ typedef void * @param[in] size Full size of the downloading file (in bytes) */ typedef void -(*GNUNET_CHAT_FileDownloadCallback) (void *cls, const struct GNUNET_CHAT_File *file, - uint64_t completed, uint64_t size); +(*GNUNET_CHAT_FileDownloadCallback) (void *cls, + const struct GNUNET_CHAT_File *file, + uint64_t completed, + uint64_t size); /** * Method called during an unindexing of a specific file in a chat which was @@ -240,8 +250,10 @@ typedef void * @param[in] size Full size of the unindexing file (in bytes) */ typedef void -(*GNUNET_CHAT_FileUnindexCallback) (void *cls, struct GNUNET_CHAT_File *file, - uint64_t completed, uint64_t size); +(*GNUNET_CHAT_FileUnindexCallback) (void *cls, + struct GNUNET_CHAT_File *file, + uint64_t completed, + uint64_t size); /** * Start a chat handle with a certain configuration, an application directory @@ -515,6 +527,24 @@ GNUNET_CHAT_group_iterate_contacts (struct GNUNET_CHAT_Group *group, struct GNUNET_CHAT_Context* GNUNET_CHAT_group_get_context (struct GNUNET_CHAT_Group *group); +/** + * Returns the chat contact which uses a given context. + * + * @param[in] context Context + * @return Chat contact + */ +const struct GNUNET_CHAT_Contact* +GNUNET_CHAT_context_get_contact (const struct GNUNET_CHAT_Context *context); + +/** + * Returns the chat group which uses a given context. + * + * @param[in] context Context + * @return Chat group + */ +const struct GNUNET_CHAT_Group* +GNUNET_CHAT_context_get_group (const struct GNUNET_CHAT_Context *context); + /** * Sets a custom user pointer to a given chat context so it can * be accessed in chat context related callbacks. diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c index e002b89..57744fe 100644 --- a/src/gnunet_chat_lib.c +++ b/src/gnunet_chat_lib.c @@ -486,6 +486,61 @@ GNUNET_CHAT_group_get_context (struct GNUNET_CHAT_Group *group) } +const struct GNUNET_CHAT_Contact* +GNUNET_CHAT_context_get_contact (const struct GNUNET_CHAT_Context *context) +{ + if ((!context) || (GNUNET_CHAT_CONTEXT_TYPE_CONTACT != context->type)) + return NULL; + + struct GNUNET_MESSENGER_Room *room = context->room; + struct GNUNET_CHAT_RoomFindContact find; + + find.contact = NULL; + + GNUNET_MESSENGER_iterate_members( + room, + it_room_find_contact, + &find + ); + + if (!find.contact) + return NULL; + + struct GNUNET_ShortHashCode shorthash; + util_shorthash_from_member(find.contact, &shorthash); + + const struct GNUNET_CHAT_Contact *contact; + contact = GNUNET_CONTAINER_multishortmap_get( + context->handle->contacts, &shorthash + ); + + GNUNET_assert((contact == NULL) || (contact->context == context)); + return contact; +} + + +const struct GNUNET_CHAT_Group* +GNUNET_CHAT_context_get_group (const struct GNUNET_CHAT_Context *context) +{ + if ((!context) || (GNUNET_CHAT_CONTEXT_TYPE_GROUP != context->type)) + return NULL; + + const struct GNUNET_MESSENGER_Room *room = context->room; + const struct GNUNET_HashCode *key = GNUNET_MESSENGER_room_get_key(room); + + if (!key) + return NULL; + + const struct GNUNET_CHAT_Group *group; + group = GNUNET_CONTAINER_multihashmap_get( + context->handle->groups, key + ); + + GNUNET_assert((group == NULL) || (group->context == context)); + return group; +} + + void GNUNET_CHAT_context_set_user_pointer (struct GNUNET_CHAT_Context *context, void *user_pointer) diff --git a/src/gnunet_chat_lib_intern.c b/src/gnunet_chat_lib_intern.c index 6d72cbf..e87176d 100644 --- a/src/gnunet_chat_lib_intern.c +++ b/src/gnunet_chat_lib_intern.c @@ -89,6 +89,23 @@ it_contact_find_room (void *cls, return GNUNET_NO; } +struct GNUNET_CHAT_RoomFindContact +{ + const struct GNUNET_MESSENGER_Contact *contact; +}; + +int +it_room_find_contact (void *cls, + GNUNET_UNUSED struct GNUNET_MESSENGER_Room *room, + const struct GNUNET_MESSENGER_Contact *member) +{ + GNUNET_assert((cls) && (member)); + + struct GNUNET_CHAT_RoomFindContact *find = cls; + find->contact = member; + return GNUNET_NO; +} + struct GNUNET_CHAT_GroupIterateContacts { struct GNUNET_CHAT_Group *group; -- cgit v1.2.3