diff options
author | Jacki <jacki@thejackimonster.de> | 2024-01-05 03:53:38 +0100 |
---|---|---|
committer | Jacki <jacki@thejackimonster.de> | 2024-01-05 03:53:38 +0100 |
commit | 0aa918a8861ad8e8260309e0e0961cfb0b1b1c26 (patch) | |
tree | a0dc5afa915227d7cf4d28fa339058c139aedb64 | |
parent | 155016406d44a5a12a123a91a8eaf8aaa62e7430 (diff) | |
download | libgnunetchat-0aa918a8861ad8e8260309e0e0961cfb0b1b1c26.tar.gz libgnunetchat-0aa918a8861ad8e8260309e0e0961cfb0b1b1c26.zip |
Implement function to list attributes of a handle
Signed-off-by: Jacki <jacki@thejackimonster.de>
-rw-r--r-- | include/gnunet/gnunet_chat_lib.h | 30 | ||||
-rw-r--r-- | src/gnunet_chat_handle.h | 3 | ||||
-rw-r--r-- | src/gnunet_chat_lib.c | 53 | ||||
-rw-r--r-- | src/gnunet_chat_lib_intern.c | 68 |
4 files changed, 153 insertions, 1 deletions
diff --git a/include/gnunet/gnunet_chat_lib.h b/include/gnunet/gnunet_chat_lib.h index 8026cf6..20473dc 100644 --- a/include/gnunet/gnunet_chat_lib.h +++ b/include/gnunet/gnunet_chat_lib.h | |||
@@ -191,6 +191,20 @@ typedef enum GNUNET_GenericReturnValue | |||
191 | struct GNUNET_CHAT_Account *account); | 191 | struct GNUNET_CHAT_Account *account); |
192 | 192 | ||
193 | /** | 193 | /** |
194 | * Method called for each attribute of a specific chat handle. | ||
195 | * | ||
196 | * @param[in,out] cls Closure from #GNUNET_CHAT_get_attributes | ||
197 | * @param[in] handle Chat handle | ||
198 | * @param[in] name Attribute name | ||
199 | * @param[in] value Attribute value | ||
200 | */ | ||
201 | typedef void | ||
202 | (*GNUNET_CHAT_AttributeCallback) (void *cls, | ||
203 | struct GNUNET_CHAT_Handle *handle, | ||
204 | const char *name, | ||
205 | const char *value); | ||
206 | |||
207 | /** | ||
194 | * Method called when a lobby is opened to share with others via a chat URI. | 208 | * Method called when a lobby is opened to share with others via a chat URI. |
195 | * | 209 | * |
196 | * @param[in,out] cls Closure from #GNUNET_CHAT_lobby_open | 210 | * @param[in,out] cls Closure from #GNUNET_CHAT_lobby_open |
@@ -515,6 +529,19 @@ GNUNET_CHAT_delete_attribute (struct GNUNET_CHAT_Handle *handle, | |||
515 | const char *name); | 529 | const char *name); |
516 | 530 | ||
517 | /** | 531 | /** |
532 | * Calls an optional <i>callback</i> for each attribute of a given chat | ||
533 | * <i>handle</i>. | ||
534 | * | ||
535 | * @param[in,out] handle Chat handle | ||
536 | * @param[in] callback Callback for attribute iteration (optional) | ||
537 | * @param[in,out] cls Closure for attribute iteration (optional) | ||
538 | */ | ||
539 | void | ||
540 | GNUNET_CHAT_get_attributes (struct GNUNET_CHAT_Handle *handle, | ||
541 | GNUNET_CHAT_AttributeCallback callback, | ||
542 | void *cls); | ||
543 | |||
544 | /** | ||
518 | * Convert an UTF-8 String to a chat URI which will be newly allocated. | 545 | * Convert an UTF-8 String to a chat URI which will be newly allocated. |
519 | * | 546 | * |
520 | * @param[in] uri UTF-8 string to parse | 547 | * @param[in] uri UTF-8 string to parse |
@@ -1363,7 +1390,8 @@ const struct GNUNET_CHAT_Contact* | |||
1363 | GNUNET_CHAT_ticket_get_contact (const struct GNUNET_CHAT_Ticket *ticket); | 1390 | GNUNET_CHAT_ticket_get_contact (const struct GNUNET_CHAT_Ticket *ticket); |
1364 | 1391 | ||
1365 | /** | 1392 | /** |
1366 | * Consumes a given chat <i>ticket</i>. | 1393 | * Consumes a given chat <i>ticket</i> and calls an optional <i>callback</i> |
1394 | * for each of its attributes. | ||
1367 | * | 1395 | * |
1368 | * @param[in,out] ticket Chat ticket | 1396 | * @param[in,out] ticket Chat ticket |
1369 | * @param[in] callback Callback for ticket consumption (optional) | 1397 | * @param[in] callback Callback for ticket consumption (optional) |
diff --git a/src/gnunet_chat_handle.h b/src/gnunet_chat_handle.h index 33d6b95..27d7906 100644 --- a/src/gnunet_chat_handle.h +++ b/src/gnunet_chat_handle.h | |||
@@ -88,6 +88,9 @@ struct GNUNET_CHAT_AttributeProcess | |||
88 | 88 | ||
89 | struct GNUNET_RECLAIM_Attribute *attribute; | 89 | struct GNUNET_RECLAIM_Attribute *attribute; |
90 | 90 | ||
91 | GNUNET_CHAT_AttributeCallback callback; | ||
92 | void *closure; | ||
93 | |||
91 | struct GNUNET_RECLAIM_AttributeIterator *iter; | 94 | struct GNUNET_RECLAIM_AttributeIterator *iter; |
92 | struct GNUNET_RECLAIM_Operation *op; | 95 | struct GNUNET_RECLAIM_Operation *op; |
93 | 96 | ||
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c index 644e444..f929efa 100644 --- a/src/gnunet_chat_lib.c +++ b/src/gnunet_chat_lib.c | |||
@@ -358,6 +358,9 @@ GNUNET_CHAT_delete_attribute (struct GNUNET_CHAT_Handle *handle, | |||
358 | struct GNUNET_CHAT_AttributeProcess | 358 | struct GNUNET_CHAT_AttributeProcess |
359 | ); | 359 | ); |
360 | 360 | ||
361 | if (!attributes) | ||
362 | return; | ||
363 | |||
361 | memset(attributes, 0, sizeof(struct GNUNET_CHAT_AttributeProcess)); | 364 | memset(attributes, 0, sizeof(struct GNUNET_CHAT_AttributeProcess)); |
362 | 365 | ||
363 | attributes->handle = handle; | 366 | attributes->handle = handle; |
@@ -391,6 +394,56 @@ GNUNET_CHAT_delete_attribute (struct GNUNET_CHAT_Handle *handle, | |||
391 | } | 394 | } |
392 | 395 | ||
393 | 396 | ||
397 | void | ||
398 | GNUNET_CHAT_get_attributes (struct GNUNET_CHAT_Handle *handle, | ||
399 | GNUNET_CHAT_AttributeCallback callback, | ||
400 | void *cls) | ||
401 | { | ||
402 | GNUNET_CHAT_VERSION_ASSERT(); | ||
403 | |||
404 | if ((!handle) || (handle->destruction)) | ||
405 | return; | ||
406 | |||
407 | const struct GNUNET_CRYPTO_PrivateKey *key = handle_get_key( | ||
408 | handle | ||
409 | ); | ||
410 | |||
411 | if (!key) | ||
412 | return; | ||
413 | |||
414 | struct GNUNET_CHAT_AttributeProcess *attributes = GNUNET_new( | ||
415 | struct GNUNET_CHAT_AttributeProcess | ||
416 | ); | ||
417 | |||
418 | if (!attributes) | ||
419 | return; | ||
420 | |||
421 | memset(attributes, 0, sizeof(struct GNUNET_CHAT_AttributeProcess)); | ||
422 | |||
423 | attributes->handle = handle; | ||
424 | |||
425 | attributes->callback = callback; | ||
426 | attributes->closure = cls; | ||
427 | |||
428 | attributes->iter = GNUNET_RECLAIM_get_attributes_start( | ||
429 | handle->reclaim, | ||
430 | key, | ||
431 | cb_task_error_iterate_attribute, | ||
432 | attributes, | ||
433 | cb_iterate_attribute, | ||
434 | attributes, | ||
435 | cb_task_finish_iterate_attribute, | ||
436 | attributes | ||
437 | ); | ||
438 | |||
439 | GNUNET_CONTAINER_DLL_insert_tail( | ||
440 | handle->attributes_head, | ||
441 | handle->attributes_tail, | ||
442 | attributes | ||
443 | ); | ||
444 | } | ||
445 | |||
446 | |||
394 | struct GNUNET_CHAT_Uri* | 447 | struct GNUNET_CHAT_Uri* |
395 | GNUNET_CHAT_uri_parse (const char *uri, | 448 | GNUNET_CHAT_uri_parse (const char *uri, |
396 | char **emsg) | 449 | char **emsg) |
diff --git a/src/gnunet_chat_lib_intern.c b/src/gnunet_chat_lib_intern.c index 32af3c8..dc4423d 100644 --- a/src/gnunet_chat_lib_intern.c +++ b/src/gnunet_chat_lib_intern.c | |||
@@ -343,3 +343,71 @@ cont_update_attribute_with_status (void *cls, | |||
343 | attributes | 343 | attributes |
344 | ); | 344 | ); |
345 | } | 345 | } |
346 | |||
347 | void | ||
348 | cb_task_finish_iterate_attribute (void *cls) | ||
349 | { | ||
350 | GNUNET_assert(cls); | ||
351 | |||
352 | struct GNUNET_CHAT_AttributeProcess *attributes = ( | ||
353 | (struct GNUNET_CHAT_AttributeProcess*) cls | ||
354 | ); | ||
355 | |||
356 | struct GNUNET_CHAT_Handle *handle = attributes->handle; | ||
357 | |||
358 | if (attributes->iter) | ||
359 | GNUNET_RECLAIM_get_attributes_stop(attributes->iter); | ||
360 | |||
361 | GNUNET_CONTAINER_DLL_remove( | ||
362 | handle->attributes_head, | ||
363 | handle->attributes_tail, | ||
364 | attributes | ||
365 | ); | ||
366 | |||
367 | GNUNET_free(attributes); | ||
368 | } | ||
369 | |||
370 | void | ||
371 | cb_task_error_iterate_attribute (void *cls) | ||
372 | { | ||
373 | GNUNET_assert(cls); | ||
374 | |||
375 | struct GNUNET_CHAT_AttributeProcess *attributes = ( | ||
376 | (struct GNUNET_CHAT_AttributeProcess*) cls | ||
377 | ); | ||
378 | |||
379 | handle_send_internal_message( | ||
380 | attributes->handle, | ||
381 | NULL, | ||
382 | GNUNET_CHAT_FLAG_WARNING, | ||
383 | "Attribute iteration failed!" | ||
384 | ); | ||
385 | |||
386 | cb_task_finish_iterate_attribute(cls); | ||
387 | } | ||
388 | |||
389 | void | ||
390 | cb_iterate_attribute (void *cls, | ||
391 | const struct GNUNET_CRYPTO_PublicKey *identity, | ||
392 | const struct GNUNET_RECLAIM_Attribute *attribute) | ||
393 | { | ||
394 | GNUNET_assert(cls); | ||
395 | |||
396 | struct GNUNET_CHAT_AttributeProcess *attributes = ( | ||
397 | (struct GNUNET_CHAT_AttributeProcess*) cls | ||
398 | ); | ||
399 | |||
400 | struct GNUNET_CHAT_Handle *handle = attributes->handle; | ||
401 | |||
402 | const char *value = GNUNET_RECLAIM_attribute_value_to_string( | ||
403 | attribute->type, | ||
404 | attribute->data, | ||
405 | attribute->data_size | ||
406 | ); | ||
407 | |||
408 | if (attributes->callback) | ||
409 | attributes->callback(attributes->closure, handle, attribute->name, value); | ||
410 | |||
411 | if (attributes->iter) | ||
412 | GNUNET_RECLAIM_get_attributes_next(attributes->iter); | ||
413 | } | ||