libgnunetchat

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

gnunet_chat_accounts.c (2827B)


      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_accounts.c
     23  */
     24 
     25 #include "gnunet_chat_accounts.h"
     26 
     27 #include "../gnunet_chat_handle.h"
     28 
     29 #include <gnunet/gnunet_common.h>
     30 
     31 struct GNUNET_CHAT_InternalAccounts*
     32 internal_accounts_create(struct GNUNET_CHAT_Handle *handle,
     33                          struct GNUNET_CHAT_Account *account)
     34 {
     35   GNUNET_assert(handle);
     36 
     37   struct GNUNET_CHAT_InternalAccounts *accounts = GNUNET_new(
     38     struct GNUNET_CHAT_InternalAccounts
     39   );
     40 
     41   if (!accounts)
     42     return NULL;
     43 
     44   accounts->handle = handle;
     45   accounts->account = account;
     46 
     47   accounts->identifier = NULL;
     48   accounts->op = NULL;
     49   accounts->method = GNUNET_CHAT_ACCOUNT_NONE;
     50 
     51   GNUNET_CONTAINER_DLL_insert(
     52     accounts->handle->accounts_head,
     53     accounts->handle->accounts_tail,
     54     accounts
     55   );
     56 
     57   return accounts;
     58 }
     59 
     60 void
     61 internal_accounts_destroy(struct GNUNET_CHAT_InternalAccounts *accounts)
     62 {
     63   GNUNET_assert((accounts) && (accounts->handle));
     64 
     65   GNUNET_CONTAINER_DLL_remove(
     66     accounts->handle->accounts_head,
     67     accounts->handle->accounts_tail,
     68     accounts
     69   );
     70 
     71   if (accounts->identifier)
     72     GNUNET_free(accounts->identifier);
     73 
     74   if (accounts->op)
     75     GNUNET_IDENTITY_cancel(accounts->op);
     76 
     77   GNUNET_free(accounts);
     78 }
     79 
     80 void
     81 internal_accounts_start_method(struct GNUNET_CHAT_InternalAccounts *accounts,
     82                                enum GNUNET_CHAT_AccountMethod method,
     83                                const char *identifier)
     84 {
     85   GNUNET_assert(
     86     (accounts) && 
     87     (GNUNET_CHAT_ACCOUNT_NONE == accounts->method) &&
     88     (!(accounts->identifier)) &&
     89     (!(accounts->op))
     90   );
     91 
     92   accounts->identifier = identifier ? GNUNET_strdup(identifier) : NULL;
     93   accounts->method = method;
     94 }
     95 
     96 void
     97 internal_accounts_stop_method(struct GNUNET_CHAT_InternalAccounts *accounts)
     98 {
     99   GNUNET_assert(accounts);
    100 
    101   if (accounts->identifier)
    102   {
    103     GNUNET_free(accounts->identifier);
    104     accounts->identifier = NULL;
    105   }
    106 
    107   if (accounts->op)
    108   {
    109     GNUNET_IDENTITY_cancel(accounts->op);
    110     accounts->op = NULL;
    111   }
    112 
    113   accounts->method = GNUNET_CHAT_ACCOUNT_NONE;
    114 }