aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheJackiMonster <thejackimonster@gmail.com>2022-02-13 20:49:26 +0100
committerTheJackiMonster <thejackimonster@gmail.com>2022-02-13 20:49:26 +0100
commit005133f48187c05fa5c91e0424df8017347daf25 (patch)
treedc8d638670986be863ebc3ea64c5fa07c0885e7f
parenta0d9fe355a8b12109d15c9a82f01a3d2b0dc1119 (diff)
downloadlibgnunetchat-005133f48187c05fa5c91e0424df8017347daf25.tar.gz
libgnunetchat-005133f48187c05fa5c91e0424df8017347daf25.zip
Added function to create an account
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r--include/gnunet_chat_lib.h18
-rw-r--r--src/gnunet_chat_account.c5
-rw-r--r--src/gnunet_chat_handle.c4
-rw-r--r--src/gnunet_chat_handle.h1
-rw-r--r--src/gnunet_chat_handle_intern.c27
-rw-r--r--src/gnunet_chat_lib.c37
-rw-r--r--src/gnunet_chat_lib_intern.c12
7 files changed, 99 insertions, 5 deletions
diff --git a/include/gnunet_chat_lib.h b/include/gnunet_chat_lib.h
index a0d57d9..6eb5c70 100644
--- a/include/gnunet_chat_lib.h
+++ b/include/gnunet_chat_lib.h
@@ -311,6 +311,21 @@ void
311GNUNET_CHAT_stop (struct GNUNET_CHAT_Handle *handle); 311GNUNET_CHAT_stop (struct GNUNET_CHAT_Handle *handle);
312 312
313/** 313/**
314 * Creates a new chat account to use with a given chat <i>handle</i> under a
315 * unique <i>name</i>.
316 *
317 * If a specific name is already in use of another chat accounts, the function
318 * will fail and return #GNUNET_NO.
319 *
320 * @param[in,out] handle Chat handle
321 * @param[in] name Account name
322 * @return #GNUNET_OK on success, #GNUNET_NO on failure and otherwise #GNUNET_SYSERR
323 */
324int
325GNUNET_CHAT_account_create (struct GNUNET_CHAT_Handle *handle,
326 const char* name);
327
328/**
314 * Iterates through the accounts of a given chat <i>handle</i> with a selected 329 * Iterates through the accounts of a given chat <i>handle</i> with a selected
315 * callback and custom closure. 330 * callback and custom closure.
316 * 331 *
@@ -448,9 +463,10 @@ GNUNET_CHAT_account_get_user_pointer (const struct GNUNET_CHAT_Account *account)
448 * join the chat via a private invitation. 463 * join the chat via a private invitation.
449 * 464 *
450 * @param[in,out] handle Chat handle 465 * @param[in,out] handle Chat handle
466 * @param[in] topic Public topic (optional)
451 * @return New group chat 467 * @return New group chat
452 */ 468 */
453struct GNUNET_CHAT_Group * 469struct GNUNET_CHAT_Group*
454GNUNET_CHAT_group_create (struct GNUNET_CHAT_Handle *handle, 470GNUNET_CHAT_group_create (struct GNUNET_CHAT_Handle *handle,
455 const char* topic); 471 const char* topic);
456 472
diff --git a/src/gnunet_chat_account.c b/src/gnunet_chat_account.c
index 09e48f8..53163ae 100644
--- a/src/gnunet_chat_account.c
+++ b/src/gnunet_chat_account.c
@@ -23,6 +23,7 @@
23 */ 23 */
24 24
25#include "gnunet_chat_account.h" 25#include "gnunet_chat_account.h"
26#include "gnunet_chat_util.h"
26 27
27struct GNUNET_CHAT_Account* 28struct GNUNET_CHAT_Account*
28account_create_from_ego(struct GNUNET_IDENTITY_Ego *ego, 29account_create_from_ego(struct GNUNET_IDENTITY_Ego *ego,
@@ -33,7 +34,9 @@ account_create_from_ego(struct GNUNET_IDENTITY_Ego *ego,
33 struct GNUNET_CHAT_Account *account = GNUNET_new(struct GNUNET_CHAT_Account); 34 struct GNUNET_CHAT_Account *account = GNUNET_new(struct GNUNET_CHAT_Account);
34 35
35 account->ego = ego; 36 account->ego = ego;
36 account->name = GNUNET_strdup(name); 37 account->name = NULL;
38
39 util_set_name_field(name, &(account->name));
37 40
38 account->user_pointer = NULL; 41 account->user_pointer = NULL;
39 42
diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c
index d3c9394..a8f24e7 100644
--- 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,
57 handle->accounts_tail = NULL; 57 handle->accounts_tail = NULL;
58 58
59 handle->current = NULL; 59 handle->current = NULL;
60 handle->creation_op = NULL;
60 61
61 handle->files = NULL; 62 handle->files = NULL;
62 handle->contexts = NULL; 63 handle->contexts = NULL;
@@ -113,6 +114,9 @@ handle_destroy (struct GNUNET_CHAT_Handle *handle)
113 if (handle->shutdown_hook) 114 if (handle->shutdown_hook)
114 GNUNET_SCHEDULER_cancel(handle->shutdown_hook); 115 GNUNET_SCHEDULER_cancel(handle->shutdown_hook);
115 116
117 if (handle->creation_op)
118 GNUNET_IDENTITY_cancel(handle->creation_op);
119
116 if (handle->current) 120 if (handle->current)
117 handle_disconnect(handle); 121 handle_disconnect(handle);
118 122
diff --git a/src/gnunet_chat_handle.h b/src/gnunet_chat_handle.h
index 569a62f..0c0440e 100644
--- a/src/gnunet_chat_handle.h
+++ b/src/gnunet_chat_handle.h
@@ -71,6 +71,7 @@ struct GNUNET_CHAT_Handle
71 struct GNUNET_CHAT_InternalAccounts *accounts_tail; 71 struct GNUNET_CHAT_InternalAccounts *accounts_tail;
72 72
73 const struct GNUNET_CHAT_Account *current; 73 const struct GNUNET_CHAT_Account *current;
74 struct GNUNET_IDENTITY_Operation *creation_op;
74 75
75 struct GNUNET_CONTAINER_MultiHashMap *files; 76 struct GNUNET_CONTAINER_MultiHashMap *files;
76 struct GNUNET_CONTAINER_MultiHashMap *contexts; 77 struct GNUNET_CONTAINER_MultiHashMap *contexts;
diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c
index bb4c107..e3f660d 100644
--- a/src/gnunet_chat_handle_intern.c
+++ b/src/gnunet_chat_handle_intern.c
@@ -212,10 +212,31 @@ on_handle_gnunet_identity(void *cls,
212 return; 212 return;
213 } 213 }
214 214
215 struct GNUNET_CHAT_InternalAccounts *accounts = GNUNET_new( 215 struct GNUNET_CHAT_InternalAccounts *accounts = handle->accounts_head;
216 struct GNUNET_CHAT_InternalAccounts 216
217 ); 217 while (accounts)
218 {
219 if (!(accounts->account))
220 goto skip_account;
221
222 if ((accounts->account->name) &&
223 (0 == strcmp(accounts->account->name, name)))
224 {
225 accounts->account->ego = ego;
226 return;
227 }
228
229 if (ego == accounts->account->ego)
230 {
231 util_set_name_field(name, &(accounts->account->name));
232 return;
233 }
234
235skip_account:
236 accounts = accounts->next;
237 }
218 238
239 accounts = GNUNET_new(struct GNUNET_CHAT_InternalAccounts);
219 accounts->account = account_create_from_ego(ego, name); 240 accounts->account = account_create_from_ego(ego, name);
220 241
221 GNUNET_CONTAINER_DLL_insert_tail( 242 GNUNET_CONTAINER_DLL_insert_tail(
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c
index 0b36765..b273441 100644
--- a/src/gnunet_chat_lib.c
+++ b/src/gnunet_chat_lib.c
@@ -63,6 +63,43 @@ GNUNET_CHAT_stop (struct GNUNET_CHAT_Handle *handle)
63 63
64 64
65int 65int
66GNUNET_CHAT_account_create (struct GNUNET_CHAT_Handle *handle,
67 const char* name)
68{
69 if ((!handle) || (!name))
70 return GNUNET_SYSERR;
71
72 struct GNUNET_CHAT_InternalAccounts *accounts = handle->accounts_head;
73 while (accounts)
74 {
75 if (!(accounts->account))
76 goto skip_account;
77
78 if ((accounts->account->name) &&
79 (0 == strcmp(accounts->account->name, name)))
80 return GNUNET_NO;
81
82skip_account:
83 accounts = accounts->next;
84 }
85
86 if (handle->creation_op)
87 GNUNET_IDENTITY_cancel(handle->creation_op);
88
89 handle->creation_op = GNUNET_IDENTITY_create(
90 handle->identity,
91 name,
92 NULL,
93 GNUNET_IDENTITY_TYPE_ECDSA,
94 cb_account_creation,
95 handle
96 );
97
98 return (handle->creation_op? GNUNET_OK : GNUNET_SYSERR);
99}
100
101
102int
66GNUNET_CHAT_iterate_accounts (const struct GNUNET_CHAT_Handle *handle, 103GNUNET_CHAT_iterate_accounts (const struct GNUNET_CHAT_Handle *handle,
67 GNUNET_CHAT_AccountCallback callback, 104 GNUNET_CHAT_AccountCallback callback,
68 void *cls) 105 void *cls)
diff --git a/src/gnunet_chat_lib_intern.c b/src/gnunet_chat_lib_intern.c
index 3ad5d4f..154de1b 100644
--- a/src/gnunet_chat_lib_intern.c
+++ b/src/gnunet_chat_lib_intern.c
@@ -24,6 +24,18 @@
24 24
25#define GNUNET_UNUSED __attribute__ ((unused)) 25#define GNUNET_UNUSED __attribute__ ((unused))
26 26
27void
28cb_account_creation (void *cls,
29 GNUNET_UNUSED const struct GNUNET_IDENTITY_PrivateKey *pk,
30 GNUNET_UNUSED const char *emsg)
31{
32 GNUNET_assert(cls);
33
34 struct GNUNET_CHAT_Handle *handle = (struct GNUNET_CHAT_Handle*) cls;
35
36 handle->creation_op = NULL;
37}
38
27struct GNUNET_CHAT_HandleIterateContacts 39struct GNUNET_CHAT_HandleIterateContacts
28{ 40{
29 struct GNUNET_CHAT_Handle *handle; 41 struct GNUNET_CHAT_Handle *handle;