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