libgnunetchat

library for GNUnet Messenger
Log | Files | Refs | README | LICENSE

commit 8d4dda12bf6cd39e4d6b655bf3c4bcd9c369e41e
parent 7dcf5b56a5d5315a94e2f4c325be1f89d86e7729
Author: Jacki <jacki@thejackimonster.de>
Date:   Wed, 24 Apr 2024 14:11:50 +0200

Implement function to iterate attributes from disconnected accounts

Signed-off-by: Jacki <jacki@thejackimonster.de>

Diffstat:
Minclude/gnunet/gnunet_chat_lib.h | 30++++++++++++++++++++++++++++++
Msrc/gnunet_chat_account.c | 13+++++++++++++
Msrc/gnunet_chat_account.h | 10++++++++++
Msrc/gnunet_chat_handle.c | 11++++-------
Msrc/gnunet_chat_handle.h | 4++++
Msrc/gnunet_chat_lib.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/gnunet_chat_lib_intern.c | 17++++++++++++++---
7 files changed, 127 insertions(+), 10 deletions(-)

diff --git a/include/gnunet/gnunet_chat_lib.h b/include/gnunet/gnunet_chat_lib.h @@ -223,6 +223,21 @@ typedef enum GNUNET_GenericReturnValue struct GNUNET_CHAT_Account *account); /** + * Iterator over attributes of a specific chat account. + * + * @param[in,out] cls Closure from #GNUNET_CHAT_get_attributes + * @param[in] account Chat account + * @param[in] name Attribute name + * @param[in] value Attribute value + * @return #GNUNET_YES if we should continue to iterate, #GNUNET_NO otherwise. + */ +typedef enum GNUNET_GenericReturnValue +(*GNUNET_CHAT_AccountAttributeCallback) (void *cls, + const struct GNUNET_CHAT_Account *account, + const char *name, + const char *value); + +/** * Iterator over attributes of a specific chat handle. * * @param[in,out] cls Closure from #GNUNET_CHAT_get_attributes @@ -785,6 +800,21 @@ const char* GNUNET_CHAT_account_get_name (const struct GNUNET_CHAT_Account *account); /** + * Calls an optional <i>callback</i> for each attribute of a given chat + * <i>account</i> using a chat <i>handle</i>. + * + * @param[in,out] handle Chat handle + * @param[in] account Chat account + * @param[in] callback Callback for attribute iteration (optional) + * @param[in,out] cls Closure for attribute iteration (optional) + */ +void +GNUNET_CHAT_account_get_attributes (struct GNUNET_CHAT_Handle *handle, + const struct GNUNET_CHAT_Account *account, + GNUNET_CHAT_AccountAttributeCallback callback, + void *cls); + +/** * Sets a custom <i>user pointer</i> to a given chat <i>account</i> so it can * be accessed in account related callbacks. * diff --git a/src/gnunet_chat_account.c b/src/gnunet_chat_account.c @@ -68,6 +68,19 @@ account_update_directory (struct GNUNET_CHAT_Account *account, GNUNET_free(key_string); } +const struct GNUNET_CRYPTO_PrivateKey* +account_get_key (const struct GNUNET_CHAT_Account *account) +{ + GNUNET_assert(account); + + if (!(account->ego)) + return NULL; + + return GNUNET_IDENTITY_ego_get_private_key( + account->ego + ); +} + void account_destroy(struct GNUNET_CHAT_Account *account) { diff --git a/src/gnunet_chat_account.h b/src/gnunet_chat_account.h @@ -61,6 +61,16 @@ account_update_directory (struct GNUNET_CHAT_Account *account, const char *base_directory); /** + * Returns the private key from a given chat + * <i>account</i>. + * + * @param[in] account Chat account + * @return EGOs private key or NULL + */ +const struct GNUNET_CRYPTO_PrivateKey* +account_get_key (const struct GNUNET_CHAT_Account *account); + +/** * Destroys a chat <i>account</i> and frees its memory. * * @param[in,out] account Chat account diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c @@ -349,9 +349,8 @@ handle_connect (struct GNUNET_CHAT_Handle *handle, handle->gns = GNUNET_GNS_connect(handle->cfg); - const struct GNUNET_CRYPTO_PrivateKey *key = NULL; - if (account->ego) - key = GNUNET_IDENTITY_ego_get_private_key(account->ego); + const struct GNUNET_CRYPTO_PrivateKey *key; + key = account_get_key(account); handle->reclaim = GNUNET_RECLAIM_connect( handle->cfg @@ -783,12 +782,10 @@ handle_get_key (const struct GNUNET_CHAT_Handle *handle) { GNUNET_assert(handle); - if ((!(handle->current)) || (!(handle->current->ego))) + if (!(handle->current)) return NULL; - return GNUNET_IDENTITY_ego_get_private_key( - handle->current->ego - ); + return account_get_key(handle->current); } void diff --git a/src/gnunet_chat_handle.h b/src/gnunet_chat_handle.h @@ -94,6 +94,8 @@ struct GNUNET_CHAT_UriLookups struct GNUNET_CHAT_AttributeProcess { + const struct GNUNET_CHAT_Account *account; + struct GNUNET_CHAT_Handle *handle; struct GNUNET_CHAT_Contact *contact; @@ -103,6 +105,8 @@ struct GNUNET_CHAT_AttributeProcess void *data; GNUNET_CHAT_AttributeCallback callback; + GNUNET_CHAT_AccountAttributeCallback account_callback; + void *closure; struct GNUNET_RECLAIM_AttributeIterator *iter; diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c @@ -987,6 +987,58 @@ GNUNET_CHAT_account_get_name (const struct GNUNET_CHAT_Account *account) void +GNUNET_CHAT_account_get_attributes (struct GNUNET_CHAT_Handle *handle, + const struct GNUNET_CHAT_Account *account, + GNUNET_CHAT_AccountAttributeCallback callback, + void *cls) +{ + GNUNET_CHAT_VERSION_ASSERT(); + + if ((!handle) || (handle->destruction) || (!account)) + return; + + const struct GNUNET_CRYPTO_PrivateKey *key = account_get_key( + account + ); + + if (!key) + return; + + struct GNUNET_CHAT_AttributeProcess *attributes = GNUNET_new( + struct GNUNET_CHAT_AttributeProcess + ); + + if (!attributes) + return; + + memset(attributes, 0, sizeof(struct GNUNET_CHAT_AttributeProcess)); + + attributes->handle = handle; + attributes->account = account; + + attributes->account_callback = callback; + attributes->closure = cls; + + attributes->iter = GNUNET_RECLAIM_get_attributes_start( + handle->reclaim, + key, + cb_task_error_iterate_attribute, + attributes, + cb_iterate_attribute, + attributes, + cb_task_finish_iterate_attribute, + attributes + ); + + GNUNET_CONTAINER_DLL_insert_tail( + handle->attributes_head, + handle->attributes_tail, + attributes + ); +} + + +void GNUNET_CHAT_account_set_user_pointer (struct GNUNET_CHAT_Account *account, void *user_pointer) { diff --git a/src/gnunet_chat_lib_intern.c b/src/gnunet_chat_lib_intern.c @@ -22,6 +22,7 @@ * @file gnunet_chat_lib_intern.c */ +#include "gnunet_chat_account.h" #include "gnunet_chat_contact.h" #include "gnunet_chat_handle.h" @@ -487,9 +488,12 @@ cb_task_finish_iterate_attribute (void *cls) struct GNUNET_CHAT_Handle *handle = attributes->handle; - const struct GNUNET_CRYPTO_PrivateKey *key = handle_get_key( - handle - ); + const struct GNUNET_CRYPTO_PrivateKey *key; + + if (attributes->account) + key = account_get_key(attributes->account); + else + key = handle_get_key(handle); attributes->iter = NULL; @@ -682,6 +686,13 @@ cb_iterate_attribute (void *cls, if (attributes->callback) result = attributes->callback(attributes->closure, handle, attribute->name, value); + else if (attributes->account_callback) + result = attributes->account_callback( + attributes->closure, + attributes->account, + attribute->name, + value + ); if (value) GNUNET_free (value);