diff options
author | TheJackiMonster <thejackimonster@gmail.com> | 2022-04-03 03:22:56 +0200 |
---|---|---|
committer | TheJackiMonster <thejackimonster@gmail.com> | 2022-04-03 03:22:56 +0200 |
commit | 4e7fe777d6bb03b88ae3e0ac781f366f5296b23d (patch) | |
tree | 4353a294f0306e56f0be20c7c054775cd80c15f1 | |
parent | cd4754e94da6553f93c0d499a1feb95025b44638 (diff) |
Implement relogin after renaming and add doxygen docs
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r-- | src/gnunet_chat_account.h | 20 | ||||
-rw-r--r-- | src/gnunet_chat_contact.c | 2 | ||||
-rw-r--r-- | src/gnunet_chat_contact.h | 30 | ||||
-rw-r--r-- | src/gnunet_chat_context.h | 49 | ||||
-rw-r--r-- | src/gnunet_chat_file.h | 83 | ||||
-rw-r--r-- | src/gnunet_chat_group.h | 21 | ||||
-rw-r--r-- | src/gnunet_chat_handle.c | 31 | ||||
-rw-r--r-- | src/gnunet_chat_handle.h | 102 | ||||
-rw-r--r-- | src/gnunet_chat_handle_intern.c | 20 | ||||
-rw-r--r-- | src/gnunet_chat_invitation.h | 15 | ||||
-rw-r--r-- | src/gnunet_chat_lib.c | 44 | ||||
-rw-r--r-- | src/gnunet_chat_lib_intern.c | 16 | ||||
-rw-r--r-- | src/gnunet_chat_lobby.h | 22 | ||||
-rw-r--r-- | src/gnunet_chat_message.h | 28 | ||||
-rw-r--r-- | src/gnunet_chat_uri.h | 13 | ||||
-rw-r--r-- | src/gnunet_chat_util.c | 5 | ||||
-rw-r--r-- | src/gnunet_chat_util.h | 106 | ||||
-rw-r--r-- | tests/test_gnunet_chat_handle.c | 14 |
18 files changed, 546 insertions, 75 deletions
diff --git a/src/gnunet_chat_account.h b/src/gnunet_chat_account.h index 40d8392..45ca869 100644 --- a/src/gnunet_chat_account.h +++ b/src/gnunet_chat_account.h @@ -38,14 +38,34 @@ struct GNUNET_CHAT_Account void *user_pointer; }; +/** + * Creates a chat account using a given <i>ego</i> and + * a matching <i>name</i>. + * + * @param[in] ego EGO + * @param[in] name Name + * @return New chat account + */ struct GNUNET_CHAT_Account* account_create_from_ego(struct GNUNET_IDENTITY_Ego *ego, const char *name); +/** + * Updates the stored directory path by a chat <i>account</i> + * using its current ego and key information. + * + * @param[in,out] account Chat account + * @param[in] base_directory The base directory for the accounts + */ void account_update_directory (struct GNUNET_CHAT_Account *account, const char *base_directory); +/** + * Destroys a chat <i>account</i> and frees its memory. + * + * @param[in,out] account Chat account + */ void account_destroy(struct GNUNET_CHAT_Account *account); diff --git a/src/gnunet_chat_contact.c b/src/gnunet_chat_contact.c index 1f88cad..5b6c1a5 100644 --- a/src/gnunet_chat_contact.c +++ b/src/gnunet_chat_contact.c @@ -71,7 +71,7 @@ contact_update_key (struct GNUNET_CHAT_Contact *contact) } struct GNUNET_CHAT_Context* -contact_find_context (struct GNUNET_CHAT_Contact *contact) +contact_find_context (const struct GNUNET_CHAT_Contact *contact) { GNUNET_assert(contact); diff --git a/src/gnunet_chat_contact.h b/src/gnunet_chat_contact.h index cf0dded..54918cc 100644 --- a/src/gnunet_chat_contact.h +++ b/src/gnunet_chat_contact.h @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2021 GNUnet e.V. + Copyright (C) 2021--2022 GNUnet e.V. GNUnet is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published @@ -46,16 +46,42 @@ struct GNUNET_CHAT_Contact int is_owned; }; +/** + * Creates a chat contact using a given messenger <i>contact</i> + * with a selected chat <i>handle</i>. + * + * @param[in,out] handle Chat handle + * @param[in] member Messenger contact + * @return New chat contact + */ struct GNUNET_CHAT_Contact* contact_create_from_member (struct GNUNET_CHAT_Handle *handle, const struct GNUNET_MESSENGER_Contact *member); +/** + * Updates the string representation of the public key from + * a given chat <i>contact</i>. + * + * @param[in,out] contact Chat contact + */ void contact_update_key (struct GNUNET_CHAT_Contact *contact); +/** + * Searches for a chat context containing a given chat + * <i>contact</i> and the least amount of other members. + * + * @param[in] contact Chat contact + * @return Chat context or NULL + */ struct GNUNET_CHAT_Context* -contact_find_context (struct GNUNET_CHAT_Contact *contact); +contact_find_context (const struct GNUNET_CHAT_Contact *contact); +/** + * Destroys a chat <i>contact</i> and frees its memory. + * + * @param[in,out] contact Chat contact + */ void contact_destroy (struct GNUNET_CHAT_Contact* contact); diff --git a/src/gnunet_chat_context.h b/src/gnunet_chat_context.h index 5bafe53..66e777a 100644 --- a/src/gnunet_chat_context.h +++ b/src/gnunet_chat_context.h @@ -61,31 +61,80 @@ struct GNUNET_CHAT_Context struct GNUNET_NAMESTORE_QueueEntry *query; }; +/** + * Creates a chat context from a messenger <i>room</i> + * with a selected chat <i>handle</i>. + * + * @param[in,out] handle Chat handle + * @param[in,out] room Messenger room + * @return New chat context + */ struct GNUNET_CHAT_Context* context_create_from_room (struct GNUNET_CHAT_Handle *handle, struct GNUNET_MESSENGER_Room *room); +/** + * Creates a chat context from a messenger <i>contact</i> + * with a selected chat <i>handle</i>. + * + * @param[in,out] handle Chat handle + * @param[in] contact Messenger contact + * @return New chat context + */ struct GNUNET_CHAT_Context* context_create_from_contact (struct GNUNET_CHAT_Handle *handle, const struct GNUNET_MESSENGER_Contact *contact); +/** + * Destroys a chat <i>context</i> and frees its memory. + * + * @param[in,out] context Chat context + */ void context_destroy (struct GNUNET_CHAT_Context* context); +/** + * Updates the connected messenger <i>room</i> of a + * selected chat <i>context</i>. + * + * @param[in,out] context Chat context + * @param[in,out] room Messenger room + */ void context_update_room (struct GNUNET_CHAT_Context *context, struct GNUNET_MESSENGER_Room *room); +/** + * Updates the <i>nick</i> of a selected chat <i>context</i>. + * + * @param[in,out] context Chat context + * @param[in] nick Nick name + */ void context_update_nick (struct GNUNET_CHAT_Context *context, const char *nick); +/** + * Reads the <i>data</i> of records under a given <i>label</i> + * and updates the chat <i>context</i> with it. + * + * @param[in,out] context Chat context + * @param[in] label Label + * @param[in] count Count of data + * @param[in] data Records data + */ void context_read_records (struct GNUNET_CHAT_Context *context, const char *label, unsigned int count, const struct GNUNET_GNSRECORD_Data *data); +/** + * Writes the data from a selected chat <i>context</i> into + * the namestore as private records. + * + * @param[in,out] context Chat context + */ void context_write_records (struct GNUNET_CHAT_Context *context); diff --git a/src/gnunet_chat_file.h b/src/gnunet_chat_file.h index 8aa1465..5208f18 100644 --- a/src/gnunet_chat_file.h +++ b/src/gnunet_chat_file.h @@ -103,45 +103,128 @@ struct GNUNET_CHAT_File void *user_pointer; }; +/** + * Creates a chat file handle from a file body in a + * <i>message</i> with a selected chat <i>handle</i>. + * + * @param[in,out] handle Chat handle + * @param[in] message File message body + * @return New chat file handle + */ struct GNUNET_CHAT_File* file_create_from_message (struct GNUNET_CHAT_Handle *handle, const struct GNUNET_MESSENGER_MessageFile *message); +/** + * Creates a chat file handle from a local file on disk + * under a given <i>name</i> using a <i>hash</i> and a + * selected symmetric <i>key</i> with a selected chat + * <i>handle</i>. + * + * @param[in,out] handle Chat handle + * @param[in] name File name + * @param[in] hash File hash + * @param[in] key Symmetric key + * @return New chat file handle + */ struct GNUNET_CHAT_File* file_create_from_disk (struct GNUNET_CHAT_Handle *handle, const char *name, const struct GNUNET_HashCode *hash, const struct GNUNET_CRYPTO_SymmetricSessionKey *key); +/** + * Destroys a chat <i>file</i> handle and frees its memory. + * + * @param[in,out] file Chat file handle + */ void file_destroy (struct GNUNET_CHAT_File *file); +/** + * Binds a chat <i>context</i>, a callback and a closure + * to a given chat <i>file</i> handle to be called on any + * progress uploading the regarding file. + * + * @param[in,out] file Chat file handle + * @param[in,out] context Chat context + * @param[in] cb Callback for upload progress + * @param[in,out] cls Closure + */ void file_bind_upload (struct GNUNET_CHAT_File *file, struct GNUNET_CHAT_Context *context, GNUNET_CHAT_FileUploadCallback cb, void *cls); +/** + * Binds a callback and a closure to a given chat <i>file</i> + * handle to be called on any progress downloading the + * regarding file. + * + * @param[in,out] file Chat file handle + * @param[in] cb Callback for download progress + * @param[in,out] cls Closure + */ void file_bind_downlaod (struct GNUNET_CHAT_File *file, GNUNET_CHAT_FileDownloadCallback cb, void *cls); +/** + * Binds a callback and a closure to a given chat <i>file</i> + * handle to be called on any progress unindexing the + * regarding file. + * + * @param[in,out] file Chat file handle + * @param[in] cb Callback for unindex progress + * @param[in,out] cls Closure + */ void file_bind_unindex (struct GNUNET_CHAT_File *file, GNUNET_CHAT_FileUnindexCallback cb, void *cls); +/** + * Calls the regarding events and bound callback of a given + * chat <i>file</i> handle to a file upload progress using + * the provided state of <i>completed</i> bytes and its file + * <i>size</i>. + * + * @param[in,out] file Chat file handle + * @param[in] completed Amount of uploaded bytes + * @param[in] size Full file size + */ void file_update_upload (struct GNUNET_CHAT_File *file, uint64_t completed, uint64_t size); +/** + * Calls the regarding events and bound callback of a given + * chat <i>file</i> handle to a file download progress using + * the provided state of <i>completed</i> bytes and its file + * <i>size</i>. + * + * @param[in,out] file Chat file handle + * @param[in] completed Amount of downloaded bytes + * @param[in] size Full file size + */ void file_update_download (struct GNUNET_CHAT_File *file, uint64_t completed, uint64_t size); +/** + * Calls the regarding events and bound callback of a given + * chat <i>file</i> handle to a file unindex progress using + * the provided state of <i>completed</i> bytes and its file + * <i>size</i>. + * + * @param[in,out] file Chat file handle + * @param[in] completed Amount of unindexed bytes + * @param[in] size Full file size + */ void file_update_unindex (struct GNUNET_CHAT_File *file, uint64_t completed, diff --git a/src/gnunet_chat_group.h b/src/gnunet_chat_group.h index 4c2bf9d..af576a0 100644 --- a/src/gnunet_chat_group.h +++ b/src/gnunet_chat_group.h @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2021 GNUnet e.V. + Copyright (C) 2021--2022 GNUnet e.V. GNUnet is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published @@ -47,13 +47,32 @@ struct GNUNET_CHAT_Group void *user_pointer; }; +/** + * Creates a chat group from a chat <i>context</i> + * with a selected chat <i>handle</i>. + * + * @param[in,out] handle Chat handle + * @param[in,out] context Chat context + * @return New chat group + */ struct GNUNET_CHAT_Group* group_create_from_context (struct GNUNET_CHAT_Handle *handle, struct GNUNET_CHAT_Context *context); +/** + * Destroys a chat <i>group</i> and frees its memory. + * + * @param[in,out] group Chat group + */ void group_destroy (struct GNUNET_CHAT_Group* group); +/** + * Publishes a selected chat <i>group</i> under the + * topic of its context. + * + * @param[in,out] group Chat group + */ void group_publish (struct GNUNET_CHAT_Group* group); diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c index cbbbcc7..b9b1d3e 100644 --- a/src/gnunet_chat_handle.c +++ b/src/gnunet_chat_handle.c @@ -168,39 +168,32 @@ handle_destroy (struct GNUNET_CHAT_Handle *handle) GNUNET_NAMESTORE_disconnect(handle->namestore); struct GNUNET_CHAT_InternalAccounts *accounts; - accounts = handle->accounts_head; - - while (accounts) - { - if (accounts->op) - GNUNET_IDENTITY_cancel(accounts->op); - - accounts->op = NULL; - accounts = accounts->next; - } - - if (handle->identity) - GNUNET_IDENTITY_disconnect(handle->identity); - - if (handle->arm) - GNUNET_ARM_disconnect(handle->arm); while (handle->accounts_head) { accounts = handle->accounts_head; + if (accounts->op) + GNUNET_IDENTITY_cancel(accounts->op); + if (accounts->account) account_destroy(accounts->account); GNUNET_CONTAINER_DLL_remove( - handle->accounts_head, - handle->accounts_tail, - accounts + handle->accounts_head, + handle->accounts_tail, + accounts ); GNUNET_free(accounts); } + if (handle->identity) + GNUNET_IDENTITY_disconnect(handle->identity); + + if (handle->arm) + GNUNET_ARM_disconnect(handle->arm); + if (handle->directory) GNUNET_free(handle->directory); diff --git a/src/gnunet_chat_handle.h b/src/gnunet_chat_handle.h index 2cc6349..3028186 100644 --- a/src/gnunet_chat_handle.h +++ b/src/gnunet_chat_handle.h @@ -124,52 +124,154 @@ struct GNUNET_CHAT_Handle void *user_pointer; }; +/** + * Creates a chat handle with a selected configuration, + * a custom message callback and a custom closure for + * the callback. + * + * @param[in] cfg Configuration + * @param[in] msg_cb Message callback + * @param[in,out] msg_cls Closure + * @return New chat handle + */ struct GNUNET_CHAT_Handle* handle_create_from_config (const struct GNUNET_CONFIGURATION_Handle* cfg, GNUNET_CHAT_ContextMessageCallback msg_cb, void *msg_cls); +/** + * Updates the string representation of the public key from + * a given chat <i>handle</i>. + * + * @param[in,out] handle Chat handle + */ void handle_update_key (struct GNUNET_CHAT_Handle *handle); +/** + * Destroys a chat <i>handle</i> and frees its memory. + * + * @param[in,out] handle Chat handle + */ void handle_destroy (struct GNUNET_CHAT_Handle *handle); +/** + * Connects a given chat <i>handle</i> to a selected + * chat <i>account</i> using it for further operations. + * + * @param[in,out] handle Chat handle + * @param[in] account Chat account + */ void handle_connect (struct GNUNET_CHAT_Handle *handle, const struct GNUNET_CHAT_Account *account); +/** + * Disconnects a given chat <i>handle</i> from its current + * connected chat account. + * + * @param[in,out] handle Chat handle + */ void handle_disconnect (struct GNUNET_CHAT_Handle *handle); +/** + * Returns the main directory path to store information + * of a given chat <i>handle</i>. + * + * @param[in] handle Chat handle + * @return Directory path + */ const char* handle_get_directory (const struct GNUNET_CHAT_Handle *handle); +/** + * Returns the private key from the current connected chat + * account of a given chat <i>handle</i>. + * + * @param[in] handle Chat handle + * @return EGOs private key or NULL + */ const struct GNUNET_IDENTITY_PrivateKey* handle_get_key (const struct GNUNET_CHAT_Handle *handle); +/** + * Sends an internal chat message from a given chat + * <i>handle</i> with an optional chat <i>context</i>, + * a custom <i>flag</i> and an optional <i>warning</i> text. + * + * @param[in,out] handle Chat handle + * @param[in,out] context Chat context or NULL + * @param[in] flag Chat message flag + * @param[in] warning Warning text + */ void handle_send_internal_message (struct GNUNET_CHAT_Handle *handle, struct GNUNET_CHAT_Context *context, enum GNUNET_CHAT_MessageFlag flag, const char *warning); +/** + * Sends a name message to a messenger <i>room</i> with + * a selected chat <i>handle</i>. + * + * @param[in,out] handle Chat handle + * @param[in,out] room Messenger room + */ void handle_send_room_name (struct GNUNET_CHAT_Handle *handle, struct GNUNET_MESSENGER_Room *room); +/** + * Checks a given chat <i>handle</i> for any chat context + * connected with a messenger <i>room</i>, creates it if + * necessary and manages its context type. + * + * @param[in,out] handle Chat handle + * @param[in,out] room Messenger room + * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR + */ int handle_request_context_by_room (struct GNUNET_CHAT_Handle *handle, struct GNUNET_MESSENGER_Room *room); +/** + * Returns the chat contact registered for a given messenger + * <i>contact</i> by a selected chat <i>handle</i>. + * + * @param[in] handle Chat handle + * @param[in] contact Messenger contact + * @return Chat contact or NULL + */ struct GNUNET_CHAT_Contact* handle_get_contact_from_messenger (const struct GNUNET_CHAT_Handle *handle, const struct GNUNET_MESSENGER_Contact *contact); +/** + * Returns the chat group registered for a given messenger + * <i>room</i> by a selected chat <i>handle</i>. + * + * @param[in] handle Chat handle + * @param[in] room Messenger room + * @return Chat group or NULL + */ struct GNUNET_CHAT_Group* handle_get_group_from_messenger (const struct GNUNET_CHAT_Handle *handle, const struct GNUNET_MESSENGER_Room *room); +/** + * Processes the <i>data</i> of records under a given + * <i>label</i> and creates a matching chat <i>context</i> + * with it if it does not exist already, registered by a chat + * <i>handle</i>, to be updated. + * + * @param[in,out] handle Chat handle + * @param[in] label Namestore label + * @param[in] count Count of data + * @param[in] data Records data + * @return Chat context or NULL + */ struct GNUNET_CHAT_Context* handle_process_records (struct GNUNET_CHAT_Handle *handle, const char *label, diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c index 63758a8..e278eaf 100644 --- a/src/gnunet_chat_handle_intern.c +++ b/src/gnunet_chat_handle_intern.c @@ -219,12 +219,12 @@ notify_handle_fs_progress(void* cls, void on_handle_gnunet_identity(void *cls, struct GNUNET_IDENTITY_Ego *ego, - GNUNET_UNUSED void **ctx, + void **ctx, const char *name) { struct GNUNET_CHAT_Handle* handle = cls; - if (!name) + if (!ctx) return; if (!ego) @@ -241,9 +241,22 @@ on_handle_gnunet_identity(void *cls, goto check_matching_name; if (name) + { util_set_name_field(name, &(accounts->account->name)); + + if (handle->current == accounts->account) + handle_send_internal_message( + handle, + NULL, + GNUNET_CHAT_FLAG_LOGIN, + NULL + ); + } else { + if (handle->current == accounts->account) + handle_disconnect(handle); + account_destroy(accounts->account); GNUNET_CONTAINER_DLL_remove( @@ -269,6 +282,9 @@ skip_account: accounts = accounts->next; } + if (!name) + return; + accounts = GNUNET_new(struct GNUNET_CHAT_InternalAccounts); accounts->account = account_create_from_ego(ego, name); diff --git a/src/gnunet_chat_invitation.h b/src/gnunet_chat_invitation.h index 433131e..a0d76de 100644 --- a/src/gnunet_chat_invitation.h +++ b/src/gnunet_chat_invitation.h @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2021 GNUnet e.V. + Copyright (C) 2021--2022 GNUnet e.V. GNUnet is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published @@ -42,10 +42,23 @@ struct GNUNET_CHAT_Invitation GNUNET_PEER_Id door; }; +/** + * Creates a chat invitation from a invite body in a + * <i>message</i> with a selected chat <i>context</i>. + * + * @param[in,out] context Chat context + * @param[in] message Invite message body + * @return New chat invitation + */ struct GNUNET_CHAT_Invitation* invitation_create_from_message (struct GNUNET_CHAT_Context *context, const struct GNUNET_MESSENGER_MessageInvite *message); +/** + * Destroys a chat <i>invitation</i> and frees its memory. + * + * @param[in,out] invitation Chat invitation + */ void invitation_destroy (struct GNUNET_CHAT_Invitation *invitation); diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c index 6739d15..c52ddf4 100644 --- a/src/gnunet_chat_lib.c +++ b/src/gnunet_chat_lib.c @@ -70,8 +70,11 @@ GNUNET_CHAT_stop (struct GNUNET_CHAT_Handle *handle) if ((!handle) || (handle->destruction)) return; - handle->destruction = GNUNET_SCHEDULER_add_now( - task_handle_destruction, handle + handle->destruction = GNUNET_SCHEDULER_add_at_with_priority( + GNUNET_TIME_absolute_get(), + GNUNET_SCHEDULER_PRIORITY_IDLE, + task_handle_destruction, + handle ); } @@ -82,7 +85,7 @@ GNUNET_CHAT_account_create (struct GNUNET_CHAT_Handle *handle, { GNUNET_CHAT_VERSION_ASSERT(); - if ((!handle) || (!name)) + if ((!handle) || (handle->destruction) || (!name)) return GNUNET_SYSERR; struct GNUNET_CHAT_InternalAccounts *accounts = handle->accounts_head; @@ -134,7 +137,7 @@ GNUNET_CHAT_account_delete(struct GNUNET_CHAT_Handle *handle, { GNUNET_CHAT_VERSION_ASSERT(); - if ((!handle) || (!name)) + if ((!handle) || (handle->destruction) || (!name)) return GNUNET_SYSERR; struct GNUNET_CHAT_InternalAccounts *accounts = handle->accounts_head; @@ -161,7 +164,7 @@ GNUNET_CHAT_account_delete(struct GNUNET_CHAT_Handle *handle, handle->identity, name, cb_account_deletion, - accounts + handle ); return (accounts->op? GNUNET_OK : GNUNET_SYSERR); @@ -175,7 +178,7 @@ GNUNET_CHAT_iterate_accounts (const struct GNUNET_CHAT_Handle *handle, { GNUNET_CHAT_VERSION_ASSERT(); - if (!handle) + if ((!handle) || (handle->destruction)) return GNUNET_SYSERR; int result = 0; @@ -205,7 +208,7 @@ GNUNET_CHAT_connect (struct GNUNET_CHAT_Handle *handle, { GNUNET_CHAT_VERSION_ASSERT(); - if (!handle) + if ((!handle) || (handle->destruction)) return; if (handle->current == account) @@ -226,7 +229,7 @@ GNUNET_CHAT_disconnect (struct GNUNET_CHAT_Handle *handle) { GNUNET_CHAT_VERSION_ASSERT(); - if ((!handle) || (!(handle->current))) + if ((!handle) || (handle->destruction) || (!(handle->current))) return; handle_disconnect(handle); @@ -238,7 +241,7 @@ GNUNET_CHAT_get_connected (const struct GNUNET_CHAT_Handle *handle) { GNUNET_CHAT_VERSION_ASSERT(); - if (!handle) + if ((!handle) || (handle->destruction)) return NULL; return handle->current; @@ -250,7 +253,7 @@ GNUNET_CHAT_update (struct GNUNET_CHAT_Handle *handle) { GNUNET_CHAT_VERSION_ASSERT(); - if (!handle) + if ((!handle) || (handle->destruction)) return GNUNET_SYSERR; return GNUNET_MESSENGER_update(handle->messenger); @@ -263,7 +266,7 @@ GNUNET_CHAT_set_name (struct GNUNET_CHAT_Handle *handle, { GNUNET_CHAT_VERSION_ASSERT(); - if (!handle) + if ((!handle) || (handle->destruction)) return GNUNET_SYSERR; if (!name) @@ -278,7 +281,7 @@ GNUNET_CHAT_get_name (const struct GNUNET_CHAT_Handle *handle) { GNUNET_CHAT_VERSION_ASSERT(); - if (!handle) + if ((!handle) || (handle->destruction)) return NULL; return GNUNET_MESSENGER_get_name(handle->messenger); @@ -290,7 +293,7 @@ GNUNET_CHAT_get_key (const struct GNUNET_CHAT_Handle *handle) { GNUNET_CHAT_VERSION_ASSERT(); - if (!handle) + if ((!handle) || (handle->destruction)) return NULL; return handle->public_key; @@ -390,7 +393,7 @@ GNUNET_CHAT_lobby_open (struct GNUNET_CHAT_Handle *handle, { GNUNET_CHAT_VERSION_ASSERT(); - if (!handle) + if ((!handle) || (handle->destruction)) return NULL; struct GNUNET_CHAT_InternalLobbies *lobbies = GNUNET_new( @@ -448,7 +451,7 @@ GNUNET_CHAT_lobby_join (struct GNUNET_CHAT_Handle *handle, { GNUNET_CHAT_VERSION_ASSERT(); - if ((!handle) || (!uri) || (!(handle->gns))) + if ((!handle) || (handle->destruction) || (!uri) || (!(handle->gns))) return; struct GNUNET_CHAT_UriLookups *lookups = GNUNET_new( @@ -482,7 +485,7 @@ GNUNET_CHAT_set_user_pointer (struct GNUNET_CHAT_Handle *handle, { GNUNET_CHAT_VERSION_ASSERT(); - if (!handle) + if ((!handle) || (handle->destruction)) return; handle->user_pointer = user_pointer; @@ -494,7 +497,7 @@ GNUNET_CHAT_get_user_pointer (const struct GNUNET_CHAT_Handle *handle) { GNUNET_CHAT_VERSION_ASSERT(); - if (!handle) + if ((!handle) || (handle->destruction)) return NULL; return handle->user_pointer; @@ -508,7 +511,7 @@ GNUNET_CHAT_iterate_contacts (struct GNUNET_CHAT_Handle *handle, { GNUNET_CHAT_VERSION_ASSERT(); - if ((!handle) || (!(handle->contacts))) + if ((!handle) || (handle->destruction) || (!(handle->contacts))) return GNUNET_SYSERR; struct GNUNET_CHAT_HandleIterateContacts it; @@ -565,7 +568,8 @@ GNUNET_CHAT_group_create (struct GNUNET_CHAT_Handle *handle, { GNUNET_CHAT_VERSION_ASSERT(); - if ((!handle) || (!(handle->groups)) || (!(handle->contexts))) + if ((!handle) || (handle->destruction) || + (!(handle->groups)) || (!(handle->contexts))) return NULL; struct GNUNET_HashCode key; @@ -629,7 +633,7 @@ GNUNET_CHAT_iterate_groups (struct GNUNET_CHAT_Handle *handle, { GNUNET_CHAT_VERSION_ASSERT(); - if ((!handle) || (!(handle->groups))) + if ((!handle) || (handle->destruction) || (!(handle->groups))) return GNUNET_SYSERR; struct GNUNET_CHAT_HandleIterateGroups it; diff --git a/src/gnunet_chat_lib_intern.c b/src/gnunet_chat_lib_intern.c index a430fe5..c4f570b 100644 --- a/src/gnunet_chat_lib_intern.c +++ b/src/gnunet_chat_lib_intern.c @@ -68,27 +68,13 @@ cb_account_deletion (void *cls, { GNUNET_assert(cls); - struct GNUNET_CHAT_InternalAccounts *accounts = ( - (struct GNUNET_CHAT_InternalAccounts*) cls - ); - - struct GNUNET_CHAT_Handle *handle = accounts->handle; + struct GNUNET_CHAT_Handle *handle = (struct GNUNET_CHAT_Handle*) cls; if (emsg) { handle_send_internal_message(handle, NULL, GNUNET_CHAT_FLAG_WARNING, emsg); return; } - - GNUNET_CONTAINER_DLL_remove( - handle->accounts_head, - handle->accounts_tail, - accounts - ); - - GNUNET_free(accounts); - - handle_send_internal_message(handle, NULL, GNUNET_CHAT_FLAG_REFRESH, NULL); } void diff --git a/src/gnunet_chat_lobby.h b/src/gnunet_chat_lobby.h index bc37cfc..c77fbae 100644 --- a/src/gnunet_chat_lobby.h +++ b/src/gnunet_chat_lobby.h @@ -53,12 +53,34 @@ struct GNUNET_CHAT_Lobby void *cls; }; +/** + * Creates a new chat lobby using a given chat <i>handle</i>. + * + * @param[in,out] handle Chat handle + * @return New chat lobby + */ struct GNUNET_CHAT_Lobby* lobby_create (struct GNUNET_CHAT_Handle *handle); +/** + * Destroys a chat <i>lobby</i> and frees its memory. + * + * @param[in,out] lobby Chat lobby + */ void lobby_destroy (struct GNUNET_CHAT_Lobby *lobby); +/** + * Opens a chat <i>lobby</i> and closes it automatically + * after a selected <i>delay</i>. Once the lobby is open + * a given <i>callback</i> will be called with a custom + * closure. + * + * @param[in,out] lobby Chat lobby + * @param[in] delay Delay to close down the lobby again + * @param[in] callback Lobby opening callback + * @param[in,out] cls Closure + */ void lobby_open (struct GNUNET_CHAT_Lobby *lobby, struct GNUNET_TIME_Relative delay, diff --git a/src/gnunet_chat_message.h b/src/gnunet_chat_message.h index 59737ac..e39cc9c 100644 --- a/src/gnunet_chat_message.h +++ b/src/gnunet_chat_message.h @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2021 GNUnet e.V. + Copyright (C) 2021--2022 GNUnet e.V. GNUnet is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published @@ -64,17 +64,43 @@ struct GNUNET_CHAT_Message enum GNUNET_CHAT_MessageFlag flag; }; +/** + * Creates a chat message representing an actual message + * from the messenger service in a given chat <i>context</i> + * with a valid <i>hash</i> and message <i>flags</i> + * + * @param[in,out] context Chat context + * @param[in] hash Message hash + * @param[in] flags Message flags + * @param[in] msg Messenger message + * @return New chat message + */ struct GNUNET_CHAT_Message* message_create_from_msg (struct GNUNET_CHAT_Context *context, const struct GNUNET_HashCode *hash, enum GNUNET_MESSENGER_MessageFlags flags, const struct GNUNET_MESSENGER_Message *msg); +/** + * Creates an internal chat message with an optional chat + * <i>context</i>, a custom <i>flag</i> and an optional + * <i>warning</i> text. + * + * @param[in,out] context Chat context or NULL + * @param[in] flag Chat message flag + * @param[in] warning Warning text + * @return New internal chat message + */ struct GNUNET_CHAT_Message* message_create_internally (struct GNUNET_CHAT_Context *context, enum GNUNET_CHAT_MessageFlag flag, const char *warning); +/** + * Destroys a chat <i>message</i> and frees its memory. + * + * @param[in,out] message Chat message + */ void message_destroy (struct GNUNET_CHAT_Message* message); diff --git a/src/gnunet_chat_uri.h b/src/gnunet_chat_uri.h index 0074123..a79f02a 100644 --- a/src/gnunet_chat_uri.h +++ b/src/gnunet_chat_uri.h @@ -38,10 +38,23 @@ struct GNUNET_CHAT_Uri char *label; }; +/** + * Creates a chat uri with a selected key as <i>zone</i> + * and a <i>label</i>. + * + * @param[in] zone URI zone + * @param[in] label URI label + * @return New chat uri + */ struct GNUNET_CHAT_Uri* uri_create (const struct GNUNET_IDENTITY_PublicKey *zone, const char *label); +/** + * Destroys a chat <i>uri</i> and frees its memory. + * + * @param[in,out] uri Chat uri + */ void uri_destroy (struct GNUNET_CHAT_Uri *uri); diff --git a/src/gnunet_chat_util.c b/src/gnunet_chat_util.c index ae70918..de17e0c 100644 --- a/src/gnunet_chat_util.c +++ b/src/gnunet_chat_util.c @@ -314,8 +314,9 @@ util_get_context_label (enum GNUNET_CHAT_ContextType type, ); } -int util_lobby_name (const struct GNUNET_HashCode *hash, - char **name) +int +util_lobby_name (const struct GNUNET_HashCode *hash, + char **name) { return GNUNET_asprintf ( name, diff --git a/src/gnunet_chat_util.h b/src/gnunet_chat_util.h index c94c32c..55f13de 100644 --- a/src/gnunet_chat_util.h +++ b/src/gnunet_chat_util.h @@ -32,50 +32,146 @@ #include <gnunet/gnunet_messenger_service.h> #include <gnunet/gnunet_util_lib.h> +/** + * Enum for the types of chat contexts. + */ enum GNUNET_CHAT_ContextType { - GNUNET_CHAT_CONTEXT_TYPE_CONTACT = 1, - GNUNET_CHAT_CONTEXT_TYPE_GROUP = 2, - GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN = 0 + /** + * Contact context type + */ + GNUNET_CHAT_CONTEXT_TYPE_CONTACT = 1,/**< GNUNET_CHAT_CONTEXT_TYPE_CONTACT */ + + /** + * Group context type + */ + GNUNET_CHAT_CONTEXT_TYPE_GROUP = 2,/**< GNUNET_CHAT_CONTEXT_TYPE_GROUP */ + + /** + * Unknown context type + */ + GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN = 0 /**< GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN */ }; +/** + * Converts a unique messenger contact, being consistent <i>member</i> + * of multiple messenger rooms via memory consistency, into a short + * hash variant for map access as key. + * + * @param[in] member Messenger contact + * @param[out] shorthash Short hash + */ void util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member, struct GNUNET_ShortHashCode *shorthash); +/** + * Updates the stored content of a <i>field</i> with + * a given <i>name</i>. + * + * @param[in] name Name + * @param[out] field String field + */ void util_set_name_field (const char *name, char **field); +/** + * Generates the <i>hash</i> of a file under a given + * <i>filename</i>. + * + * @param[in] filename File name + * @param[out] hash Hash of file + * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR + */ int util_hash_file (const char *filename, struct GNUNET_HashCode *hash); +/** + * Encrypts a file inplace under a given <i>filename</i> + * with a selected symmetric <i>key</i> and its <i>hash</i> + * as initialization vector. + * + * @param[in] filename File name + * @param[in] hash Hash of file + * @param[in] key Symmetric key + * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR + */ int util_encrypt_file (const char *filename, const struct GNUNET_HashCode *hash, const struct GNUNET_CRYPTO_SymmetricSessionKey *key); +/** + * Decrypts a file inplace under a given <i>filename</i> + * with a selected symmetric <i>key</i> and its <i>hash</i> + * as parameter for the initialization vector and comparison + * to verify success. + * + * @param[in] filename File name + * @param[in] hash Hash of file + * @param[in] key Symmetric key + * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR + */ int util_decrypt_file (const char *filename, const struct GNUNET_HashCode *hash, const struct GNUNET_CRYPTO_SymmetricSessionKey *key); +/** + * Append the path of a <i>directory</i> and a custom + * subdirectory name to a composed <i>filename</i>. + * + * @param[in] directory Directory path + * @param[in] subdir Subdirectory name + * @param[out] filename Filename + * @return Number of bytes in filename excluding 0-termination + */ int util_get_dirname (const char *directory, const char *subdir, char **filename); +/** + * Append the path of a <i>directory</i>, a custom + * subdirectory name and a <i>hash</i> to a composed + * <i>filename</i>. + * + * @param[in] directory Directory path + * @param[in] subdir Subdirectory name + * @param[in] hash Hash + * @param[out] filename Filename + * @return Number of bytes in filename excluding 0-termination + */ int util_get_filename (const char *directory, const char *subdir, const struct GNUNET_HashCode *hash, char **filename); +/** + * Construct a composed <i>label</i> from a given context + * <i>type</i> and the <i>hash</i> of the contexts room. + * + * @param[in] type Chat context type + * @param[in] hash Hash of room + * @param[out] label Namestore label + * @return Number of bytes in label excluding 0-termination + */ int util_get_context_label (enum GNUNET_CHAT_ContextType type, const struct GNUNET_HashCode *hash, char **label); -int util_lobby_name (const struct GNUNET_HashCode *hash, - char **name); +/** + * Provide a standardized <i>name</i> for a lobby using + * a given <i>hash</i> of its internal room. + * + * @param[in] hash Hash of room + * @param[out] name Name of lobby + * @return Number of bytes in name excluding 0-termination + */ +int +util_lobby_name (const struct GNUNET_HashCode *hash, + char **name); #endif /* GNUNET_CHAT_UTIL_H_ */ diff --git a/tests/test_gnunet_chat_handle.c b/tests/test_gnunet_chat_handle.c index 6bc6fbf..c8a9e89 100644 --- a/tests/test_gnunet_chat_handle.c +++ b/tests/test_gnunet_chat_handle.c @@ -255,7 +255,7 @@ skip_search_account: else { ck_assert_ptr_ne(dup, NULL); - // ck_assert_str_ne(key, dup); // TODO: needs to be implemented in service! + ck_assert_str_ne(key, dup); GNUNET_free(dup); @@ -303,7 +303,7 @@ on_gnunet_chat_handle_rename_it(void *cls, ck_assert_ptr_ne(name, NULL); ck_assert_ptr_eq(GNUNET_CHAT_get_connected(handle), NULL); - if (0 == strcmp(name, "gnunet_chat_handle_rename_A")) + if (0 == strcmp(name, "gnunet_chat_handle_rename_a")) { GNUNET_CHAT_connect(chat, account); return GNUNET_NO; @@ -346,6 +346,8 @@ skip_search_account: const char *name = GNUNET_CHAT_get_name(handle); ck_assert_ptr_ne(name, NULL); + printf("login: %s\n", name); + char *dup = (char*) GNUNET_CHAT_get_user_pointer(handle); if (!dup) @@ -359,10 +361,10 @@ skip_search_account: ck_assert_int_eq(GNUNET_CHAT_set_name( handle, - "gnunet_chat_handle_rename_B" + "gnunet_chat_handle_rename_b" ), GNUNET_YES); } - else + else if (0 != strcmp(name, dup)) { ck_assert_ptr_ne(dup, NULL); ck_assert_str_ne(name, dup); @@ -373,7 +375,7 @@ skip_search_account: ck_assert_int_eq(GNUNET_CHAT_account_delete( handle, - "gnunet_chat_handle_rename_B" + "gnunet_chat_handle_rename_b" ), GNUNET_OK); GNUNET_CHAT_stop(handle); @@ -391,7 +393,7 @@ call_gnunet_chat_handle_rename(const struct GNUNET_CONFIGURATION_Handle *cfg) ck_assert_ptr_ne(handle, NULL); ck_assert_int_eq(GNUNET_CHAT_account_create( handle, - "gnunet_chat_handle_rename_A" + "gnunet_chat_handle_rename_a" ), GNUNET_OK); } |