gnunet_chat_message.c (3351B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2021--2024 GNUnet e.V. 4 5 GNUnet is free software: you can redistribute it and/or modify it 6 under the terms of the GNU Affero General Public License as published 7 by the Free Software Foundation, either version 3 of the License, 8 or (at your option) any later version. 9 10 GNUnet is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Affero General Public License for more details. 14 15 You should have received a copy of the GNU Affero General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 SPDX-License-Identifier: AGPL3.0-or-later 19 */ 20 /* 21 * @author Tobias Frisch 22 * @file gnunet_chat_message.c 23 */ 24 25 #include "gnunet_chat_message.h" 26 #include "gnunet_chat_context.h" 27 28 #include <gnunet/gnunet_messenger_service.h> 29 30 struct GNUNET_CHAT_Message* 31 message_create_from_msg (struct GNUNET_CHAT_Context *context, 32 const struct GNUNET_HashCode *hash, 33 enum GNUNET_MESSENGER_MessageFlags flags, 34 const struct GNUNET_MESSENGER_Message *msg) 35 { 36 GNUNET_assert((context) && (hash) && (msg)); 37 38 struct GNUNET_CHAT_Message *message = GNUNET_new(struct GNUNET_CHAT_Message); 39 40 message->account = NULL; 41 message->context = context; 42 message->task = NULL; 43 44 GNUNET_memcpy(&(message->hash), hash, sizeof(message->hash)); 45 message->flags = flags; 46 message->flag = GNUNET_CHAT_FLAG_NONE; 47 48 message->msg = msg; 49 message->user_pointer = NULL; 50 51 return message; 52 } 53 54 struct GNUNET_CHAT_Message* 55 message_create_internally (struct GNUNET_CHAT_Account *account, 56 struct GNUNET_CHAT_Context *context, 57 enum GNUNET_CHAT_MessageFlag flag, 58 const char *warning) 59 { 60 struct GNUNET_CHAT_Message *message = GNUNET_new(struct GNUNET_CHAT_Message); 61 62 message->account = account; 63 message->context = context; 64 message->task = NULL; 65 66 memset(&(message->hash), 0, sizeof(message->hash)); 67 message->flags = GNUNET_MESSENGER_FLAG_PRIVATE; 68 message->flag = flag; 69 70 message->warning = warning; 71 message->user_pointer = NULL; 72 73 return message; 74 } 75 76 enum GNUNET_GenericReturnValue 77 message_has_msg (const struct GNUNET_CHAT_Message* message) 78 { 79 GNUNET_assert(message); 80 81 if (message->flag != GNUNET_CHAT_FLAG_NONE) 82 return GNUNET_NO; 83 84 if (message->msg) 85 return GNUNET_YES; 86 else 87 return GNUNET_NO; 88 } 89 90 void 91 message_update_msg (struct GNUNET_CHAT_Message* message, 92 enum GNUNET_MESSENGER_MessageFlags flags, 93 const struct GNUNET_MESSENGER_Message *msg) 94 { 95 GNUNET_assert((message) && (msg)); 96 97 if ((GNUNET_YES != message_has_msg(message)) || 98 (message->flags & GNUNET_MESSENGER_FLAG_DELETE)) 99 return; 100 101 if (flags & GNUNET_MESSENGER_FLAG_UPDATE) 102 message->msg = msg; 103 else if (flags & GNUNET_MESSENGER_FLAG_DELETE) 104 context_delete_message(message->context, message); 105 else 106 return; 107 108 message->flags = flags | GNUNET_MESSENGER_FLAG_UPDATE; 109 } 110 111 void 112 message_destroy (struct GNUNET_CHAT_Message* message) 113 { 114 GNUNET_assert(message); 115 116 if (message->task) 117 GNUNET_SCHEDULER_cancel(message->task); 118 119 GNUNET_free(message); 120 }