libgnunetchat

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

commit 5c93dccc66d2044eccc98acecbeca8fb97ba407e
parent 5e91c20728b8c8fbad9df2f56338acce1325aa3f
Author: Jacki <jacki@thejackimonster.de>
Date:   Sun, 13 Oct 2024 20:21:00 +0200

Adjust code for consistency and reusage

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

Diffstat:
Msrc/gnunet_chat_handle.c | 81++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------
Msrc/gnunet_chat_handle.h | 31++++++++++++++++++++++---------
Msrc/gnunet_chat_lib.c | 50+++++++++++++++++++-------------------------------
3 files changed, 103 insertions(+), 59 deletions(-)

diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c @@ -201,6 +201,9 @@ handle_destroy (struct GNUNET_CHAT_Handle *handle) handle->files, it_destroy_handle_files, NULL ); + while (handle->attributes_head) + internal_attributes_destroy(handle->attributes_head); + if (handle->reclaim) GNUNET_RECLAIM_disconnect(handle->reclaim); @@ -496,8 +499,9 @@ handle_disconnect (struct GNUNET_CHAT_Handle *handle) } static struct GNUNET_CHAT_InternalAccounts* -find_accounts_by_name (struct GNUNET_CHAT_Handle *handle, - const char *name) +find_accounts_by_name (const struct GNUNET_CHAT_Handle *handle, + const char *name, + enum GNUNET_GenericReturnValue skip_op) { GNUNET_assert((handle) && (name)); @@ -506,7 +510,8 @@ find_accounts_by_name (struct GNUNET_CHAT_Handle *handle, while (accounts) { - if (!(accounts->account)) + if ((!(accounts->account)) || ((GNUNET_YES == skip_op) && + (accounts->op))) goto skip_account; account_name = account_get_name( @@ -523,6 +528,22 @@ find_accounts_by_name (struct GNUNET_CHAT_Handle *handle, return accounts; } +struct GNUNET_CHAT_Account* +handle_get_account_by_name (const struct GNUNET_CHAT_Handle *handle, + const char *name, + enum GNUNET_GenericReturnValue skip_op) +{ + GNUNET_assert((handle) && (name)); + + struct GNUNET_CHAT_InternalAccounts *accounts; + accounts = find_accounts_by_name(handle, name, skip_op); + + if (!accounts) + return NULL; + + return accounts->account; +} + static enum GNUNET_GenericReturnValue update_accounts_operation (struct GNUNET_CHAT_InternalAccounts **out_accounts, struct GNUNET_CHAT_Handle *handle, @@ -557,7 +578,7 @@ handle_create_account (struct GNUNET_CHAT_Handle *handle, GNUNET_assert((handle) && (name)); struct GNUNET_CHAT_InternalAccounts *accounts; - accounts = find_accounts_by_name(handle, name); + accounts = find_accounts_by_name(handle, name, GNUNET_NO); if (accounts) return GNUNET_SYSERR; @@ -590,12 +611,18 @@ handle_create_account (struct GNUNET_CHAT_Handle *handle, enum GNUNET_GenericReturnValue handle_delete_account (struct GNUNET_CHAT_Handle *handle, - const char *name) + const struct GNUNET_CHAT_Account *account) { - GNUNET_assert((handle) && (name)); + GNUNET_assert((handle) && (account)); struct GNUNET_CHAT_InternalAccounts *accounts; - accounts = find_accounts_by_name(handle, name); + accounts = handle->accounts_head; + + while (accounts) + if (account == accounts->account) + break; + else + accounts = accounts->next; if (!accounts) return GNUNET_SYSERR; @@ -611,6 +638,8 @@ handle_delete_account (struct GNUNET_CHAT_Handle *handle, if (GNUNET_OK != result) return result; + const char *name = account_get_name(account); + accounts->op = GNUNET_IDENTITY_delete( handle->identity, name, @@ -626,20 +655,31 @@ handle_delete_account (struct GNUNET_CHAT_Handle *handle, enum GNUNET_GenericReturnValue handle_rename_account (struct GNUNET_CHAT_Handle *handle, - const char *old_name, + const struct GNUNET_CHAT_Account *account, const char *new_name) { - GNUNET_assert((handle) && (old_name) && (new_name)); - - if (0 == strcmp(old_name, new_name)) - return GNUNET_OK; + GNUNET_assert((handle) && (account) && (new_name)); struct GNUNET_CHAT_InternalAccounts *accounts; - accounts = find_accounts_by_name(handle, old_name); + accounts = handle->accounts_head; + + while (accounts) + if (account == accounts->account) + break; + else + accounts = accounts->next; if (!accounts) return GNUNET_SYSERR; + if (find_accounts_by_name(handle, new_name, GNUNET_NO)) + return GNUNET_SYSERR; + + const char *old_name = account_get_name(account); + + if (0 == strcmp(old_name, new_name)) + return GNUNET_OK; + enum GNUNET_GenericReturnValue result; result = update_accounts_operation( &accounts, @@ -743,17 +783,20 @@ handle_update (struct GNUNET_CHAT_Handle *handle) { GNUNET_assert((handle) && (handle->current)); - const char *name = handle->current->name; - - if (!name) - return GNUNET_SYSERR; - struct GNUNET_CHAT_InternalAccounts *accounts; - accounts = find_accounts_by_name(handle, name); + accounts = handle->accounts_head; + + while (accounts) + if (handle->current == accounts->account) + break; + else + accounts = accounts->next; if (!accounts) return GNUNET_SYSERR; + const char *name = account_get_name(handle->current); + enum GNUNET_GenericReturnValue result; result = update_accounts_operation( &accounts, diff --git a/src/gnunet_chat_handle.h b/src/gnunet_chat_handle.h @@ -194,6 +194,20 @@ void handle_disconnect (struct GNUNET_CHAT_Handle *handle); /** + * Searches for an existing chat account by <i>name</i> as + * identifier for a given chat <i>handle</i>. + * + * @param[in] handle Chat handle + * @param[in] name Chat account name + * @param[in] skip_op Whether to skip accounts with active operation + * @return Chat account + */ +struct GNUNET_CHAT_Account* +handle_get_account_by_name (const struct GNUNET_CHAT_Handle *handle, + const char *name, + enum GNUNET_GenericReturnValue skip_op); + +/** * Enqueues a creation for a chat account with a specific * <i>name</i> as identifier for a given chat <i>handle</i>. * @@ -206,30 +220,29 @@ handle_create_account (struct GNUNET_CHAT_Handle *handle, const char *name); /** - * Enqueues a deletion for a chat account with a specific - * <i>name</i> as identifier for a given chat <i>handle</i>. + * Enqueues a deletion for a chat <i>account</i> of a + * given chat <i>handle</i>. * * @param[in,out] handle Chat handle - * @param[in] name Chat account name + * @param[in] account Chat account * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR */ enum GNUNET_GenericReturnValue handle_delete_account (struct GNUNET_CHAT_Handle *handle, - const char *name); + const struct GNUNET_CHAT_Account *account); /** - * Renames a chat account with a specific <i>old_name</i> - * as identifier for a given chat <i>handle</i> to another - * specific <i>new_name</i>. + * Renames a chat <i>account</i> of a given chat + * <i>handle</i> to another specific <i>new_name</i>. * * @param[in,out] handle Chat handle - * @param[in] old_name Old chat account name + * @param[in] account Chat account * @param[in] new_name New chat account name * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR */ enum GNUNET_GenericReturnValue handle_rename_account (struct GNUNET_CHAT_Handle *handle, - const char *old_name, + const struct GNUNET_CHAT_Account *account, const char *new_name); /** diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c @@ -111,7 +111,8 @@ GNUNET_CHAT_account_create (struct GNUNET_CHAT_Handle *handle, char *low = util_get_lower(name); - int result = handle_create_account(handle, low); + enum GNUNET_GenericReturnValue result; + result = handle_create_account(handle, low); GNUNET_free(low); return result; @@ -120,14 +121,20 @@ GNUNET_CHAT_account_create (struct GNUNET_CHAT_Handle *handle, enum GNUNET_GenericReturnValue GNUNET_CHAT_account_delete(struct GNUNET_CHAT_Handle *handle, - const char* name) + const char *name) { GNUNET_CHAT_VERSION_ASSERT(); if ((!handle) || (handle->destruction) || (!name)) return GNUNET_SYSERR; - return handle_delete_account(handle, name); + const struct GNUNET_CHAT_Account *account; + account = handle_get_account_by_name(handle, name, GNUNET_NO); + + if (!account) + return GNUNET_SYSERR; + + return handle_delete_account(handle, account); } @@ -141,7 +148,7 @@ GNUNET_CHAT_iterate_accounts (struct GNUNET_CHAT_Handle *handle, if ((!handle) || (handle->destruction)) return GNUNET_SYSERR; - int result = 0; + int iterations = 0; struct GNUNET_CHAT_InternalAccounts *accounts = handle->accounts_head; while (accounts) @@ -149,7 +156,7 @@ GNUNET_CHAT_iterate_accounts (struct GNUNET_CHAT_Handle *handle, if ((!(accounts->account)) || (accounts->op)) goto skip_account; - result++; + iterations++; if ((callback) && (GNUNET_YES != callback(cls, handle, accounts->account))) break; @@ -158,7 +165,7 @@ GNUNET_CHAT_iterate_accounts (struct GNUNET_CHAT_Handle *handle, accounts = accounts->next; } - return result; + return iterations; } @@ -171,26 +178,7 @@ GNUNET_CHAT_find_account (const struct GNUNET_CHAT_Handle *handle, if ((!handle) || (handle->destruction)) return NULL; - struct GNUNET_CHAT_InternalAccounts *accounts = handle->accounts_head; - const char *account_name; - - while (accounts) - { - if ((!(accounts->account)) || (accounts->op)) - goto skip_account; - - account_name = account_get_name( - accounts->account - ); - - if (0 == strcmp(account_name, name)) - return accounts->account; - - skip_account: - accounts = accounts->next; - } - - return NULL; + return handle_get_account_by_name(handle, name, GNUNET_YES); } @@ -291,10 +279,10 @@ GNUNET_CHAT_set_name (struct GNUNET_CHAT_Handle *handle, return GNUNET_NO; char *low = util_get_lower(name); - int result; + enum GNUNET_GenericReturnValue result; if (handle->current) - result = handle_rename_account(handle, handle->current->name, low); + result = handle_rename_account(handle, handle->current, low); else result = GNUNET_OK; @@ -3144,7 +3132,7 @@ GNUNET_CHAT_discourse_iterate_contacts (struct GNUNET_CHAT_Discourse *discourse, if (! discourse) return GNUNET_SYSERR; - int result = 0; + int iterations = 0; struct GNUNET_CHAT_DiscourseSubscription *sub; for (sub = discourse->head; sub; sub = sub->next) @@ -3155,8 +3143,8 @@ GNUNET_CHAT_discourse_iterate_contacts (struct GNUNET_CHAT_Discourse *discourse, if (callback) callback(cls, discourse, sub->contact); - result++; + iterations++; } - return result; + return iterations; }