gnunet_chat_attribute_process.c (4440B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 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_attribute_process.c 23 */ 24 25 #include "gnunet_chat_attribute_process.h" 26 27 #include "../gnunet_chat_handle.h" 28 29 #include <gnunet/gnunet_common.h> 30 #include <gnunet/gnunet_reclaim_service.h> 31 #include <string.h> 32 33 struct GNUNET_CHAT_AttributeProcess* 34 internal_attributes_create(struct GNUNET_CHAT_Handle *handle, 35 const char *name) 36 { 37 GNUNET_assert(handle); 38 39 struct GNUNET_CHAT_AttributeProcess *attributes = GNUNET_new( 40 struct GNUNET_CHAT_AttributeProcess 41 ); 42 43 if (!attributes) 44 return NULL; 45 46 memset(attributes, 0, sizeof(*attributes)); 47 48 attributes->handle = handle; 49 50 if (!name) 51 goto skip_name; 52 53 attributes->name = GNUNET_strdup(name); 54 55 if (!(attributes->name)) 56 { 57 GNUNET_free(attributes); 58 return NULL; 59 } 60 61 skip_name: 62 GNUNET_CONTAINER_DLL_insert_tail( 63 attributes->handle->attributes_head, 64 attributes->handle->attributes_tail, 65 attributes 66 ); 67 68 return attributes; 69 } 70 71 struct GNUNET_CHAT_AttributeProcess* 72 internal_attributes_create_store(struct GNUNET_CHAT_Handle *handle, 73 const char *name, 74 struct GNUNET_TIME_Relative expires) 75 { 76 GNUNET_assert((handle) && (name)); 77 78 struct GNUNET_CHAT_AttributeProcess *attributes; 79 attributes = internal_attributes_create(handle, name); 80 81 if (!attributes) 82 return NULL; 83 84 attributes->attribute = GNUNET_RECLAIM_attribute_new( 85 name, 86 NULL, 87 GNUNET_RECLAIM_ATTRIBUTE_TYPE_NONE, 88 NULL, 89 0 90 ); 91 92 if (!(attributes->attribute)) 93 { 94 internal_attributes_destroy(attributes); 95 return NULL; 96 } 97 98 attributes->expires = expires; 99 100 return attributes; 101 } 102 103 struct GNUNET_CHAT_AttributeProcess* 104 internal_attributes_create_share(struct GNUNET_CHAT_Handle *handle, 105 struct GNUNET_CHAT_Contact *contact, 106 const char *name) 107 { 108 GNUNET_assert((handle) && (contact) && (name)); 109 110 struct GNUNET_CHAT_AttributeProcess *attributes; 111 attributes = internal_attributes_create(handle, name); 112 113 if (!attributes) 114 return NULL; 115 116 attributes->contact = contact; 117 118 return attributes; 119 } 120 121 struct GNUNET_CHAT_AttributeProcess* 122 internal_attributes_create_request(struct GNUNET_CHAT_Handle *handle, 123 struct GNUNET_CHAT_Account *account) 124 { 125 GNUNET_assert((handle) && (account)); 126 127 struct GNUNET_CHAT_AttributeProcess *attributes; 128 attributes = internal_attributes_create(handle, NULL); 129 130 if (!attributes) 131 return NULL; 132 133 attributes->account = account; 134 135 return attributes; 136 } 137 138 void 139 internal_attributes_destroy(struct GNUNET_CHAT_AttributeProcess *attributes) 140 { 141 GNUNET_assert((attributes) && (attributes->handle)); 142 143 GNUNET_CONTAINER_DLL_remove( 144 attributes->handle->attributes_head, 145 attributes->handle->attributes_tail, 146 attributes 147 ); 148 149 if (attributes->attribute) 150 GNUNET_free(attributes->attribute); 151 if (attributes->name) 152 GNUNET_free(attributes->name); 153 if (attributes->data) 154 GNUNET_free(attributes->data); 155 156 if (attributes->iter) 157 GNUNET_RECLAIM_get_attributes_stop(attributes->iter); 158 if (attributes->op) 159 GNUNET_RECLAIM_cancel(attributes->op); 160 161 GNUNET_free(attributes); 162 } 163 164 void 165 internal_attributes_next_iter(struct GNUNET_CHAT_AttributeProcess *attributes) 166 { 167 GNUNET_assert((attributes) && (attributes->iter)); 168 169 GNUNET_RECLAIM_get_attributes_next(attributes->iter); 170 } 171 172 void 173 internal_attributes_stop_iter(struct GNUNET_CHAT_AttributeProcess *attributes) 174 { 175 GNUNET_assert((attributes) && (attributes->iter)); 176 177 GNUNET_RECLAIM_get_attributes_stop(attributes->iter); 178 attributes->iter = NULL; 179 }