libgnunetchat

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

gnunet_chat_util.h (6099B)


      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_util.h
     23  */
     24 
     25 #ifndef GNUNET_CHAT_UTIL_H_
     26 #define GNUNET_CHAT_UTIL_H_
     27 
     28 #include <gnunet/gnunet_messenger_service.h>
     29 #include <gnunet/gnunet_util_lib.h>
     30 
     31 #include "gnunet_chat_lib.h"
     32 
     33 /**
     34  * Enum for the types of chat contexts.
     35  */
     36 enum GNUNET_CHAT_ContextType
     37 {
     38   /**
     39    * Contact context type
     40    */
     41   GNUNET_CHAT_CONTEXT_TYPE_CONTACT = 1,/**< GNUNET_CHAT_CONTEXT_TYPE_CONTACT */
     42 
     43   /**
     44    * Group context type
     45    */
     46   GNUNET_CHAT_CONTEXT_TYPE_GROUP   = 2,/**< GNUNET_CHAT_CONTEXT_TYPE_GROUP */
     47 
     48   /**
     49    * Unknown context type
     50    */
     51   GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN = 0 /**< GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN */
     52 };
     53 
     54 /**
     55  * Converts a unique messenger contact, being consistent <i>member</i>
     56  * of multiple messenger rooms via memory consistency, into a short
     57  * hash variant for map access as key.
     58  *
     59  * @param[in] member Messenger contact
     60  * @param[out] shorthash Short hash
     61  */
     62 void
     63 util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member,
     64                             struct GNUNET_ShortHashCode *shorthash);
     65 
     66 /**
     67  * Converts a discourse id into a short hash variant for map access
     68  * as key.
     69  *
     70  * @param[in] id Discourse id
     71  * @param[out] shorthash Short hash
     72  */
     73 void
     74 util_shorthash_from_discourse_id (const struct GNUNET_CHAT_DiscourseId *id,
     75                                   struct GNUNET_ShortHashCode *shorthash);
     76 
     77 /**
     78  * Converts a short hash variant into a discourse id.
     79  *
     80  * @param[in] shorthash Short hash
     81  * @param[out] id Discourse id
     82  */
     83 void
     84 util_discourse_id_from_shorthash (const struct GNUNET_ShortHashCode *shorthash,
     85                                   struct GNUNET_CHAT_DiscourseId *id);
     86 
     87 /**
     88  * Updates the stored content of a <i>field</i> with
     89  * a given <i>name</i>.
     90  *
     91  * @param[in] name Name
     92  * @param[out] field String field
     93  */
     94 void
     95 util_set_name_field (const char *name,
     96                      char **field);
     97 
     98 /**
     99  * Generates the <i>hash</i> of a file under a given
    100  * <i>filename</i>.
    101  *
    102  * @param[in] filename File name
    103  * @param[out] hash Hash of file
    104  * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR
    105  */
    106 enum GNUNET_GenericReturnValue
    107 util_hash_file (const char *filename,
    108                 struct GNUNET_HashCode *hash);
    109 
    110 /**
    111  * Append the path of a <i>directory</i> and a custom
    112  * subdirectory name to a composed <i>filename</i>.
    113  *
    114  * @param[in] directory Directory path
    115  * @param[in] subdir Subdirectory name
    116  * @param[out] filename Filename
    117  * @return Number of bytes in filename excluding 0-termination
    118  */
    119 int
    120 util_get_dirname (const char *directory,
    121                   const char *subdir,
    122                   char **filename);
    123 
    124 /**
    125  * Append the path of a <i>directory</i>, a custom
    126  * subdirectory name and a <i>hash</i> to a composed
    127  * <i>filename</i>.
    128  *
    129  * @param[in] directory Directory path
    130  * @param[in] subdir Subdirectory name
    131  * @param[in] hash Hash
    132  * @param[out] filename Filename
    133  * @return Number of bytes in filename excluding 0-termination
    134  */
    135 int
    136 util_get_filename (const char *directory,
    137                    const char *subdir,
    138                    const struct GNUNET_HashCode *hash,
    139                    char **filename);
    140 
    141 /**
    142  * Allocates a new string representing the lower case versionn
    143  * of a given <i>name</i> to work properly with the EGO naming
    144  * scheme for example.
    145  *
    146  * @param[in] name Name
    147  * @return Lower case name
    148  */
    149 char*
    150 util_get_lower(const char *name);
    151 
    152 /**
    153  * Construct a composed <i>label</i> from a given context
    154  * <i>type</i> and the <i>hash</i> of the contexts room.
    155  *
    156  * @param[in] type Chat context type
    157  * @param[in] hash Hash of room
    158  * @param[out] label Namestore label
    159  * @return Number of bytes in label excluding 0-termination
    160  */
    161 int
    162 util_get_context_label (enum GNUNET_CHAT_ContextType type,
    163                         const struct GNUNET_HashCode *hash,
    164                         char **label);
    165 
    166 /**
    167  * Extract the chat context type from a used <i>label</i> by
    168  * a given context with a certain <i>hash</i> of its room.
    169  *
    170  * @param[in] label Namestore label
    171  * @param[in] hash Hash of room
    172  * @return Chat context type or #GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN
    173  */
    174 enum GNUNET_CHAT_ContextType
    175 util_get_context_label_type (const char *label,
    176 			                       const struct GNUNET_HashCode *hash);
    177 
    178 /**
    179  * Provide a standardized <i>name</i> for a lobby using
    180  * a given <i>hash</i> of its internal room.
    181  *
    182  * @param[in] hash Hash of room
    183  * @param[out] name Name of lobby
    184  * @return Number of bytes in name excluding 0-termination
    185  */
    186 int
    187 util_lobby_name (const struct GNUNET_HashCode *hash,
    188 		             char **name);
    189 
    190 /**
    191  * Check whether an identity <i>name</i> could be a
    192  * standardized name for a lobby.
    193  * @see util_lobby_name()
    194  *
    195  * @param[in] name Identity name
    196  * @return #GNUNET_YES if it might be a lobby name,
    197  * otherwise #GNUNET_NO
    198  */
    199 enum GNUNET_GenericReturnValue
    200 util_is_lobby_name(const char *name);
    201 
    202 /**
    203  * Return the chat related kind for a messages original kind
    204  * from the service. It will return #GNUNET_CHAT_KIND_UNKNOWN
    205  * in case the message kind is not supported.
    206  *
    207  * @param[in] kind Messenger service message kind
    208  * @return Chat message kind or #GNUNET_CHAT_KIND_UNKNOWN_
    209  */
    210 enum GNUNET_CHAT_MessageKind
    211 util_message_kind_from_kind (enum GNUNET_MESSENGER_MessageKind kind);
    212 
    213 #endif /* GNUNET_CHAT_UTIL_H_ */