libgnunetchat

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

commit 242d75e5ceb10403e62abfb52c9381fc4351d9b5
parent 5e3ed6ef9f9d447a4ece18f79a4993c0630047c3
Author: TheJackiMonster <thejackimonster@gmail.com>
Date:   Sun, 10 Oct 2021 16:56:55 +0200

Merged callbacks and added meta messages

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

Diffstat:
Minclude/gnunet_chat_lib.h | 45++++++++++++++++++++-------------------------
Msrc/gnunet_chat_handle.c | 7+------
Msrc/gnunet_chat_handle.h | 7+------
Msrc/gnunet_chat_handle_intern.c | 11+++++++++++
Msrc/gnunet_chat_lib.c | 16+++++++++++++---
Msrc/gnunet_chat_message.c | 19+++++++++++++++++++
Msrc/gnunet_chat_message.h | 20++++++++++++++++++--
7 files changed, 83 insertions(+), 42 deletions(-)

diff --git a/include/gnunet_chat_lib.h b/include/gnunet_chat_lib.h @@ -45,39 +45,49 @@ enum GNUNET_CHAT_MessageKind { /** + * The kind to inform that something went wrong. + */ + GNUNET_CHAT_KIND_WARNING = 1, /**< GNUNET_CHAT_KIND_WARNING */ + + /** + * The kind to inform that the application can be used. + */ + GNUNET_CHAT_KIND_LOGIN = 2, /**< GNUNET_CHAT_KIND_LOGIN */ + + /** * The kind to inform that a contact has joined a chat. */ - GNUNET_CHAT_KIND_JOIN = 1, /**< GNUNET_CHAT_KIND_JOIN */ + GNUNET_CHAT_KIND_JOIN = 3, /**< GNUNET_CHAT_KIND_JOIN */ /** * The kind to inform that a contact has left a chat. */ - GNUNET_CHAT_KIND_LEAVE = 2, /**< GNUNET_CHAT_KIND_LEAVE */ + GNUNET_CHAT_KIND_LEAVE = 4, /**< GNUNET_CHAT_KIND_LEAVE */ /** * The kind to inform that a contact has changed. */ - GNUNET_CHAT_KIND_CONTACT = 3, /**< GNUNET_CHAT_KIND_CONTACT */ + GNUNET_CHAT_KIND_CONTACT = 5, /**< GNUNET_CHAT_KIND_CONTACT */ /** * The kind to describe an invitation to a different chat. */ - GNUNET_CHAT_KIND_INVITATION = 4, /**< GNUNET_CHAT_KIND_INVITATION */ + GNUNET_CHAT_KIND_INVITATION = 6, /**< GNUNET_CHAT_KIND_INVITATION */ /** * The kind to describe a text message. */ - GNUNET_CHAT_KIND_TEXT = 5, /**< GNUNET_CHAT_KIND_TEXT */ + GNUNET_CHAT_KIND_TEXT = 7, /**< GNUNET_CHAT_KIND_TEXT */ /** * The kind to describe a shared file. */ - GNUNET_CHAT_KIND_FILE = 6, /**< GNUNET_CHAT_KIND_FILE */ + GNUNET_CHAT_KIND_FILE = 8, /**< GNUNET_CHAT_KIND_FILE */ /** * The kind to inform about a deletion of a previous message. */ - GNUNET_CHAT_KIND_DELETION = 7, /**< GNUNET_CHAT_KIND_DELETION */ + GNUNET_CHAT_KIND_DELETION = 9, /**< GNUNET_CHAT_KIND_DELETION */ /** * An unknown kind of message. @@ -121,19 +131,6 @@ struct GNUNET_CHAT_File; struct GNUNET_CHAT_Invitation; /** - * Method called whenever an issue occurs regarding a certain chat context - * of a specific chat handle. - * - * @param[in,out] cls Closure from #GNUNET_CHAT_start - * @param[in,out] handle Chat handle - * @param[in,out] context Chat context - * @param[in] reason Reason indicating the issue - */ -typedef void -(*GNUNET_CHAT_WarningCallback) (void *cls, struct GNUNET_CHAT_Handle *handle, - struct GNUNET_CHAT_Context *context, int reason); - -/** * Iterator over chat contacts of a specific chat handle. * * @param[in,out] cls Closure from #GNUNET_CHAT_iterate_contacts @@ -173,7 +170,7 @@ typedef int * Iterator over chat messages in a specific chat context. * * @param[in,out] cls Closure from #GNUNET_CHAT_context_iterate_messages - * @param[in,out] context Chat context + * @param[in,out] context Chat context or NULL * @param[in] message Chat message * @return #GNUNET_YES if we should continue to iterate, #GNUNET_NO otherwise. */ @@ -256,8 +253,6 @@ typedef void * @param[in] cfg Configuration * @param[in] directory Application directory path (optional) * @param[in] name User name (optional) - * @param[in] warn_cb Callback for warnings (optional) - * @param[in,out] warn_cls Closure for warnings (optional) * @param[in] msg_cb Callback for message events (optional) * @param[in,out] msg_cls Closure for message events (optional) * @return Chat handle @@ -266,7 +261,6 @@ struct GNUNET_CHAT_Handle* GNUNET_CHAT_start (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *directory, const char *name, - GNUNET_CHAT_WarningCallback warn_cb, void *warn_cls, GNUNET_CHAT_ContextMessageCallback msg_cb, void *msg_cls); /** @@ -675,7 +669,8 @@ GNUNET_CHAT_message_get_read_receipt (const struct GNUNET_CHAT_Message *message, /** * Returns the text of a given <i>message</i> if its kind is - * #GNUNET_CHAT_KIND_TEXT, otherwise it returns NULL. + * #GNUNET_CHAT_KIND_TEXT or #GNUNET_CHAT_KIND_WARNING, + * otherwise it returns NULL. * * @param[in] message Message * @return The text of message or NULL diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c @@ -31,9 +31,7 @@ handle_create_from_config (const struct GNUNET_CONFIGURATION_Handle* cfg, const char *directory, const char *name, GNUNET_CHAT_ContextMessageCallback msg_cb, - void *msg_cls, - GNUNET_CHAT_WarningCallback warn_cb, - void *warn_cls) + void *msg_cls) { struct GNUNET_CHAT_Handle* handle = GNUNET_new(struct GNUNET_CHAT_Handle); @@ -48,9 +46,6 @@ handle_create_from_config (const struct GNUNET_CONFIGURATION_Handle* cfg, handle->msg_cb = msg_cb; handle->msg_cls = msg_cls; - handle->warn_cb = warn_cb; - handle->warn_cls = warn_cls; - handle->files = GNUNET_CONTAINER_multihashmap_create(8, GNUNET_NO); handle->contexts = GNUNET_CONTAINER_multihashmap_create(8, GNUNET_NO); handle->contacts = GNUNET_CONTAINER_multishortmap_create(8, GNUNET_NO); diff --git a/src/gnunet_chat_handle.h b/src/gnunet_chat_handle.h @@ -45,9 +45,6 @@ struct GNUNET_CHAT_Handle GNUNET_CHAT_ContextMessageCallback msg_cb; void *msg_cls; - GNUNET_CHAT_WarningCallback warn_cb; - void *warn_cls; - struct GNUNET_CONTAINER_MultiHashMap *files; struct GNUNET_CONTAINER_MultiHashMap *contexts; struct GNUNET_CONTAINER_MultiShortmap *contacts; @@ -65,9 +62,7 @@ handle_create_from_config (const struct GNUNET_CONFIGURATION_Handle* cfg, const char *directory, const char *name, GNUNET_CHAT_ContextMessageCallback msg_cb, - void *msg_cls, - GNUNET_CHAT_WarningCallback warn_cb, - void *warn_cls); + void *msg_cls); void handle_destroy (struct GNUNET_CHAT_Handle* handle); diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c @@ -364,6 +364,17 @@ on_handle_identity(void *cls, (0 < GNUNET_CONTAINER_multishortmap_size(handle->contacts))) return; + if (!handle->msg_cb) + goto skip_login; + + struct GNUNET_CHAT_Message *msg = message_create_internally( + NULL, GNUNET_CHAT_FLAG_LOGIN, NULL + ); + + handle->msg_cb(handle->msg_cls, NULL, msg); + message_destroy(msg); + +skip_login: GNUNET_MESSENGER_find_rooms( handle->messenger, NULL, find_handle_rooms, handle ); diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c @@ -41,7 +41,6 @@ struct GNUNET_CHAT_Handle* GNUNET_CHAT_start (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *directory, const char *name, - GNUNET_CHAT_WarningCallback warn_cb, void *warn_cls, GNUNET_CHAT_ContextMessageCallback msg_cb, void *msg_cls) { if (!cfg) @@ -49,8 +48,7 @@ GNUNET_CHAT_start (const struct GNUNET_CONFIGURATION_Handle *cfg, return handle_create_from_config( cfg, directory, name, - msg_cb, msg_cls, - warn_cb, warn_cls + msg_cb, msg_cls ); } @@ -670,6 +668,16 @@ GNUNET_CHAT_message_get_kind (const struct GNUNET_CHAT_Message *message) if ((!message) || (!(message->msg))) return GNUNET_CHAT_KIND_UNKNOWN; + switch (message->flag) + { + case GNUNET_CHAT_FLAG_WARNING: + return GNUNET_CHAT_KIND_WARNING; + case GNUNET_CHAT_FLAG_LOGIN: + return GNUNET_CHAT_KIND_LOGIN; + default: + break; + } + switch (message->msg->header.kind) { case GNUNET_MESSENGER_KIND_JOIN: @@ -782,6 +790,8 @@ GNUNET_CHAT_message_get_text (const struct GNUNET_CHAT_Message *message) if ((!message) || (!(message->msg))) return NULL; + if (GNUNET_CHAT_FLAG_WARNING == message->flag) + return message->warning; if (GNUNET_MESSENGER_KIND_TEXT != message->msg->header.kind) return NULL; diff --git a/src/gnunet_chat_message.c b/src/gnunet_chat_message.c @@ -36,12 +36,31 @@ message_create_from_msg (struct GNUNET_CHAT_Context *context, GNUNET_memcpy(&(message->hash), hash, sizeof(message->hash)); message->flags = flags; + message->flag = GNUNET_CHAT_FLAG_NONE; message->msg = msg; return message; } +struct GNUNET_CHAT_Message* +message_create_internally (struct GNUNET_CHAT_Context *context, + enum GNUNET_CHAT_MessageFlag flag, + const char *warning) +{ + struct GNUNET_CHAT_Message *message = GNUNET_new(struct GNUNET_CHAT_Message); + + message->context = context; + + memset(&(message->hash), 0, sizeof(message->hash)); + message->flags = GNUNET_MESSENGER_FLAG_PRIVATE; + message->flag = flag; + + message->warning = warning; + + return message; +} + void message_destroy (struct GNUNET_CHAT_Message* message) { diff --git a/src/gnunet_chat_message.h b/src/gnunet_chat_message.h @@ -41,14 +41,25 @@ struct GNUNET_CHAT_MessageList struct GNUNET_CHAT_MessageList *next; }; +enum GNUNET_CHAT_MessageFlag +{ + GNUNET_CHAT_FLAG_NONE = 0, + GNUNET_CHAT_FLAG_WARNING = 1, + GNUNET_CHAT_FLAG_LOGIN = 2 +}; + struct GNUNET_CHAT_Message { struct GNUNET_CHAT_Context *context; + union { + const struct GNUNET_MESSENGER_Message *msg; + const char *warning; + }; + struct GNUNET_HashCode hash; enum GNUNET_MESSENGER_MessageFlags flags; - - const struct GNUNET_MESSENGER_Message *msg; + enum GNUNET_CHAT_MessageFlag flag; }; struct GNUNET_CHAT_Message* @@ -57,6 +68,11 @@ message_create_from_msg (struct GNUNET_CHAT_Context *context, enum GNUNET_MESSENGER_MessageFlags flags, const struct GNUNET_MESSENGER_Message *msg); +struct GNUNET_CHAT_Message* +message_create_internally (struct GNUNET_CHAT_Context *context, + enum GNUNET_CHAT_MessageFlag flag, + const char *warning); + void message_destroy (struct GNUNET_CHAT_Message* message);