libgnunetchat

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

commit 7d953503a71668ac0402a5151632dfc6ab7d4ca9
parent 06ce708121e7e890df03c0231acae6dd2646a8c1
Author: TheJackiMonster <thejackimonster@gmail.com>
Date:   Fri, 24 Nov 2023 02:10:38 +0100

Abstract parsing of context type from label

Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>

Diffstat:
Msrc/gnunet_chat_context.c | 20+++++---------------
Msrc/gnunet_chat_util.c | 27+++++++++++++++++++++++++++
Msrc/gnunet_chat_util.h | 14+++++++++++++-
3 files changed, 45 insertions(+), 16 deletions(-)

diff --git a/src/gnunet_chat_context.c b/src/gnunet_chat_context.c @@ -32,9 +32,6 @@ static const unsigned int initial_map_size_of_room = 8; static const unsigned int initial_map_size_of_contact = 4; -static const char label_prefix_of_contact [] = "contact_"; -static const char label_prefix_of_group [] = "group_"; - struct GNUNET_CHAT_Context* context_create_from_room (struct GNUNET_CHAT_Handle *handle, struct GNUNET_MESSENGER_Room *room) @@ -250,15 +247,15 @@ context_read_records (struct GNUNET_CHAT_Context *context, if (nick) GNUNET_free(nick); + const struct GNUNET_HashCode *hash = GNUNET_MESSENGER_room_get_key( + context->room + ); + if (topic) { struct GNUNET_HashCode topic_hash; GNUNET_CRYPTO_hash(topic, strlen(topic), &topic_hash); - const struct GNUNET_HashCode *hash = GNUNET_MESSENGER_room_get_key( - context->room - ); - if (0 != GNUNET_CRYPTO_hash_cmp(&topic_hash, hash)) { GNUNET_free(topic); @@ -271,14 +268,7 @@ context_read_records (struct GNUNET_CHAT_Context *context, if (topic) GNUNET_free(topic); - if (0 == strncmp(label, label_prefix_of_group, - sizeof(label_prefix_of_group) - 1)) - context->type = GNUNET_CHAT_CONTEXT_TYPE_GROUP; - else if (0 == strncmp(label, label_prefix_of_contact, - sizeof(label_prefix_of_contact) - 1)) - context->type = GNUNET_CHAT_CONTEXT_TYPE_CONTACT; - else - context->type = GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN; + context->type = util_get_context_label_type(label, hash); } void diff --git a/src/gnunet_chat_util.c b/src/gnunet_chat_util.c @@ -24,6 +24,9 @@ #include "gnunet_chat_util.h" +static const char label_prefix_of_contact [] = "contact"; +static const char label_prefix_of_group [] = "group"; + void util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member, struct GNUNET_ShortHashCode *shorthash) @@ -350,6 +353,30 @@ util_get_context_label (enum GNUNET_CHAT_ContextType type, return result; } +enum GNUNET_CHAT_ContextType +util_get_context_label_type (const char *label, + const struct GNUNET_HashCode *hash) +{ + enum GNUNET_CHAT_ContextType type = GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN; + + char *low = util_get_lower(GNUNET_h2s(hash)); + + const char *sub = strstr(label, low); + if ((!sub) || (sub == label) || (sub[-1] != '_')) + goto cleanup; + + const size_t len = (size_t) (sub - label - 1); + + if (0 == strncmp(label, label_prefix_of_group, len)) + type = GNUNET_CHAT_CONTEXT_TYPE_GROUP; + else if (0 == strncmp(label, label_prefix_of_contact, len)) + type = GNUNET_CHAT_CONTEXT_TYPE_CONTACT; + +cleanup: + GNUNET_free(low); + return type; +} + int util_lobby_name (const struct GNUNET_HashCode *hash, char **name) diff --git a/src/gnunet_chat_util.h b/src/gnunet_chat_util.h @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - Copyright (C) 2021--2022 GNUnet e.V. + Copyright (C) 2021--2023 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 @@ -172,6 +172,18 @@ util_get_context_label (enum GNUNET_CHAT_ContextType type, char **label); /** + * Extract the chat context type from a used <i>label</i> by + * a given context with a certain <i>hash</i> of its room. + * + * @param[in] label Namestore label + * @param[in] hash Hash of room + * @return Chat context type or #GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN + */ +enum GNUNET_CHAT_ContextType +util_get_context_label_type (const char *label, + const struct GNUNET_HashCode *hash); + +/** * Provide a standardized <i>name</i> for a lobby using * a given <i>hash</i> of its internal room. *