commit 005133f48187c05fa5c91e0424df8017347daf25
parent a0d9fe355a8b12109d15c9a82f01a3d2b0dc1119
Author: TheJackiMonster <thejackimonster@gmail.com>
Date: Sun, 13 Feb 2022 20:49:26 +0100
Added function to create an account
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
Diffstat:
7 files changed, 99 insertions(+), 5 deletions(-)
diff --git a/include/gnunet_chat_lib.h b/include/gnunet_chat_lib.h
@@ -311,6 +311,21 @@ void
GNUNET_CHAT_stop (struct GNUNET_CHAT_Handle *handle);
/**
+ * Creates a new chat account to use with a given chat <i>handle</i> under a
+ * unique <i>name</i>.
+ *
+ * If a specific name is already in use of another chat accounts, the function
+ * will fail and return #GNUNET_NO.
+ *
+ * @param[in,out] handle Chat handle
+ * @param[in] name Account name
+ * @return #GNUNET_OK on success, #GNUNET_NO on failure and otherwise #GNUNET_SYSERR
+ */
+int
+GNUNET_CHAT_account_create (struct GNUNET_CHAT_Handle *handle,
+ const char* name);
+
+/**
* Iterates through the accounts of a given chat <i>handle</i> with a selected
* callback and custom closure.
*
@@ -448,9 +463,10 @@ GNUNET_CHAT_account_get_user_pointer (const struct GNUNET_CHAT_Account *account)
* join the chat via a private invitation.
*
* @param[in,out] handle Chat handle
+ * @param[in] topic Public topic (optional)
* @return New group chat
*/
-struct GNUNET_CHAT_Group *
+struct GNUNET_CHAT_Group*
GNUNET_CHAT_group_create (struct GNUNET_CHAT_Handle *handle,
const char* topic);
diff --git a/src/gnunet_chat_account.c b/src/gnunet_chat_account.c
@@ -23,6 +23,7 @@
*/
#include "gnunet_chat_account.h"
+#include "gnunet_chat_util.h"
struct GNUNET_CHAT_Account*
account_create_from_ego(struct GNUNET_IDENTITY_Ego *ego,
@@ -33,7 +34,9 @@ account_create_from_ego(struct GNUNET_IDENTITY_Ego *ego,
struct GNUNET_CHAT_Account *account = GNUNET_new(struct GNUNET_CHAT_Account);
account->ego = ego;
- account->name = GNUNET_strdup(name);
+ account->name = NULL;
+
+ util_set_name_field(name, &(account->name));
account->user_pointer = NULL;
diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c
@@ -57,6 +57,7 @@ handle_create_from_config (const struct GNUNET_CONFIGURATION_Handle* cfg,
handle->accounts_tail = NULL;
handle->current = NULL;
+ handle->creation_op = NULL;
handle->files = NULL;
handle->contexts = NULL;
@@ -113,6 +114,9 @@ handle_destroy (struct GNUNET_CHAT_Handle *handle)
if (handle->shutdown_hook)
GNUNET_SCHEDULER_cancel(handle->shutdown_hook);
+ if (handle->creation_op)
+ GNUNET_IDENTITY_cancel(handle->creation_op);
+
if (handle->current)
handle_disconnect(handle);
diff --git a/src/gnunet_chat_handle.h b/src/gnunet_chat_handle.h
@@ -71,6 +71,7 @@ struct GNUNET_CHAT_Handle
struct GNUNET_CHAT_InternalAccounts *accounts_tail;
const struct GNUNET_CHAT_Account *current;
+ struct GNUNET_IDENTITY_Operation *creation_op;
struct GNUNET_CONTAINER_MultiHashMap *files;
struct GNUNET_CONTAINER_MultiHashMap *contexts;
diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c
@@ -212,10 +212,31 @@ on_handle_gnunet_identity(void *cls,
return;
}
- struct GNUNET_CHAT_InternalAccounts *accounts = GNUNET_new(
- struct GNUNET_CHAT_InternalAccounts
- );
+ struct GNUNET_CHAT_InternalAccounts *accounts = handle->accounts_head;
+
+ while (accounts)
+ {
+ if (!(accounts->account))
+ goto skip_account;
+
+ if ((accounts->account->name) &&
+ (0 == strcmp(accounts->account->name, name)))
+ {
+ accounts->account->ego = ego;
+ return;
+ }
+
+ if (ego == accounts->account->ego)
+ {
+ util_set_name_field(name, &(accounts->account->name));
+ return;
+ }
+
+skip_account:
+ accounts = accounts->next;
+ }
+ accounts = GNUNET_new(struct GNUNET_CHAT_InternalAccounts);
accounts->account = account_create_from_ego(ego, name);
GNUNET_CONTAINER_DLL_insert_tail(
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c
@@ -63,6 +63,43 @@ GNUNET_CHAT_stop (struct GNUNET_CHAT_Handle *handle)
int
+GNUNET_CHAT_account_create (struct GNUNET_CHAT_Handle *handle,
+ const char* name)
+{
+ if ((!handle) || (!name))
+ return GNUNET_SYSERR;
+
+ struct GNUNET_CHAT_InternalAccounts *accounts = handle->accounts_head;
+ while (accounts)
+ {
+ if (!(accounts->account))
+ goto skip_account;
+
+ if ((accounts->account->name) &&
+ (0 == strcmp(accounts->account->name, name)))
+ return GNUNET_NO;
+
+skip_account:
+ accounts = accounts->next;
+ }
+
+ if (handle->creation_op)
+ GNUNET_IDENTITY_cancel(handle->creation_op);
+
+ handle->creation_op = GNUNET_IDENTITY_create(
+ handle->identity,
+ name,
+ NULL,
+ GNUNET_IDENTITY_TYPE_ECDSA,
+ cb_account_creation,
+ handle
+ );
+
+ return (handle->creation_op? GNUNET_OK : GNUNET_SYSERR);
+}
+
+
+int
GNUNET_CHAT_iterate_accounts (const struct GNUNET_CHAT_Handle *handle,
GNUNET_CHAT_AccountCallback callback,
void *cls)
diff --git a/src/gnunet_chat_lib_intern.c b/src/gnunet_chat_lib_intern.c
@@ -24,6 +24,18 @@
#define GNUNET_UNUSED __attribute__ ((unused))
+void
+cb_account_creation (void *cls,
+ GNUNET_UNUSED const struct GNUNET_IDENTITY_PrivateKey *pk,
+ GNUNET_UNUSED const char *emsg)
+{
+ GNUNET_assert(cls);
+
+ struct GNUNET_CHAT_Handle *handle = (struct GNUNET_CHAT_Handle*) cls;
+
+ handle->creation_op = NULL;
+}
+
struct GNUNET_CHAT_HandleIterateContacts
{
struct GNUNET_CHAT_Handle *handle;