libgnunetchat

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

gnunet_chat_group.c (2627B)


      1 /*
      2    This file is part of GNUnet.
      3    Copyright (C) 2021--2024, 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_group.c
     23  */
     24 
     25 #include "gnunet_chat_group.h"
     26 
     27 #include "gnunet_chat_group_intern.c"
     28 #include <gnunet/gnunet_scheduler_lib.h>
     29 
     30 static const unsigned int initial_map_size_of_context = 8;
     31 static const uint16_t group_regex_compression = 6;
     32 
     33 struct GNUNET_CHAT_Group*
     34 group_create_from_context (struct GNUNET_CHAT_Handle *handle,
     35 			                     struct GNUNET_CHAT_Context *context)
     36 {
     37   GNUNET_assert((handle) && (context));
     38 
     39   struct GNUNET_CHAT_Group* group = GNUNET_new(struct GNUNET_CHAT_Group);
     40 
     41   group->handle = handle;
     42   group->context = context;
     43 
     44   group->destruction = NULL;
     45 
     46   group->announcement = NULL;
     47   group->search = NULL;
     48 
     49   group->registry = GNUNET_CONTAINER_multipeermap_create(
     50     initial_map_size_of_context, GNUNET_NO);
     51 
     52   group->user_pointer = NULL;
     53 
     54   return group;
     55 }
     56 
     57 void
     58 group_destroy (struct GNUNET_CHAT_Group* group)
     59 {
     60   GNUNET_assert(group);
     61 
     62   if (group->destruction)
     63     GNUNET_SCHEDULER_cancel(group->destruction);
     64 
     65   if (group->registry)
     66     GNUNET_CONTAINER_multipeermap_destroy(group->registry);
     67 
     68   if (group->search)
     69     GNUNET_REGEX_search_cancel(group->search);
     70 
     71   if (group->announcement)
     72     GNUNET_REGEX_announce_cancel(group->announcement);
     73 
     74   GNUNET_free(group);
     75 }
     76 
     77 void
     78 group_publish (struct GNUNET_CHAT_Group* group)
     79 {
     80   GNUNET_assert(
     81     (group) &&
     82 		(group->context) &&
     83 		(group->context->topic) &&
     84 		(group->handle) &&
     85 		(group->handle->cfg)
     86   );
     87 
     88   char* topic = NULL;
     89   GNUNET_asprintf (
     90     &topic,
     91     "GNUNET_CHAT_%s",
     92     group->context->topic
     93   );
     94 
     95   group->announcement = GNUNET_REGEX_announce(
     96     group->handle->cfg, topic,
     97     GNUNET_TIME_relative_get_minute_(),
     98     group_regex_compression
     99   );
    100 
    101   group->search = GNUNET_REGEX_search(
    102     group->handle->cfg, topic,
    103     search_group_by_topic, group
    104   );
    105 
    106   GNUNET_free(topic);
    107 }