gnunet_chat_account.c (3159B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2022--2025 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_account.c 23 */ 24 25 #include "gnunet_chat_account.h" 26 #include "gnunet_chat_handle.h" 27 #include "gnunet_chat_util.h" 28 29 #include <gnunet/gnunet_common.h> 30 #include <gnunet/gnunet_identity_service.h> 31 #include <gnunet/gnunet_messenger_service.h> 32 33 struct GNUNET_CHAT_Account* 34 account_create (const char *name) 35 { 36 GNUNET_assert(name); 37 38 struct GNUNET_CHAT_Account *account = GNUNET_new(struct GNUNET_CHAT_Account); 39 40 account->ego = NULL; 41 account->created = GNUNET_NO; 42 43 account->name = NULL; 44 45 util_set_name_field(name, &(account->name)); 46 47 account->user_pointer = NULL; 48 49 return account; 50 } 51 52 struct GNUNET_CHAT_Account* 53 account_create_from_ego (struct GNUNET_IDENTITY_Ego *ego, 54 const char *name) 55 { 56 GNUNET_assert((ego) && (name)); 57 58 struct GNUNET_CHAT_Account *account = account_create(name); 59 60 account->ego = ego; 61 account->created = GNUNET_YES; 62 63 return account; 64 } 65 66 const struct GNUNET_CRYPTO_BlindablePrivateKey* 67 account_get_key (const struct GNUNET_CHAT_Account *account) 68 { 69 GNUNET_assert(account); 70 71 if (!(account->ego)) 72 return NULL; 73 74 return GNUNET_IDENTITY_ego_get_private_key( 75 account->ego 76 ); 77 } 78 79 const char* 80 account_get_name (const struct GNUNET_CHAT_Account *account) 81 { 82 GNUNET_assert(account); 83 84 return account->name; 85 } 86 87 void 88 account_update_ego (struct GNUNET_CHAT_Account *account, 89 struct GNUNET_CHAT_Handle *handle, 90 struct GNUNET_IDENTITY_Ego *ego) 91 { 92 GNUNET_assert((account) && (handle)); 93 94 enum GNUNET_CHAT_MessageFlag flag; 95 if (GNUNET_NO == account->created) 96 flag = GNUNET_CHAT_FLAG_CREATE_ACCOUNT; 97 else 98 flag = GNUNET_CHAT_FLAG_UPDATE_ACCOUNT; 99 100 account->ego = ego; 101 102 if (!(account->ego)) 103 return; 104 105 if ((handle->current == account) && (handle->messenger)) 106 { 107 GNUNET_MESSENGER_set_key( 108 handle->messenger, 109 GNUNET_IDENTITY_ego_get_private_key(account->ego) 110 ); 111 112 handle_update_key(handle); 113 } 114 115 handle_send_internal_message( 116 handle, 117 account, 118 NULL, 119 flag, 120 NULL, 121 GNUNET_YES 122 ); 123 } 124 125 void 126 account_delete (struct GNUNET_CHAT_Account *account) 127 { 128 GNUNET_assert(account); 129 130 // TODO: clear namestore entries 131 } 132 133 void 134 account_destroy (struct GNUNET_CHAT_Account *account) 135 { 136 GNUNET_assert(account); 137 138 if (account->name) 139 GNUNET_free(account->name); 140 141 GNUNET_free(account); 142 }