aboutsummaryrefslogtreecommitdiff
path: root/src/messenger/messenger_api_room.c
blob: 5fedf1a788a61f14385fd1fabe46e11af2705af2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
   This file is part of GNUnet.
   Copyright (C) 2020 GNUnet e.V.

   GNUnet is free software: you can redistribute it and/or modify it
   under the terms of the GNU Affero General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   GNUnet is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   SPDX-License-Identifier: AGPL3.0-or-later
 */
/**
 * @author Tobias Frisch
 * @file src/messenger/messenger_api_room.c
 * @brief messenger api: client implementation of GNUnet MESSENGER service
 */

#include "messenger_api_room.h"

#include "messenger_api_handle.h"

struct GNUNET_MESSENGER_Room*
create_room (struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_HashCode *key)
{
  struct GNUNET_MESSENGER_Room *room = GNUNET_new(struct GNUNET_MESSENGER_Room);

  room->handle = handle;
  GNUNET_memcpy(&(room->key), key, sizeof(*key));

  room->opened = GNUNET_NO;
  room->contact_id = NULL;

  room->members = GNUNET_CONTAINER_multishortmap_create (8, GNUNET_NO);

  init_list_tunnels (&(room->entries));

  room->messages = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);

  return room;
}

static int
iterate_destroy_message (void *cls, const struct GNUNET_HashCode *key, void *value)
{
  struct GNUNET_MESSENGER_Message *message = value;

  destroy_message (message);

  return GNUNET_YES;
}

void
destroy_room (struct GNUNET_MESSENGER_Room *room)
{
  if (room->members)
    GNUNET_CONTAINER_multishortmap_destroy (room->members);

  clear_list_tunnels (&(room->entries));

  if (room->messages)
  {
    GNUNET_CONTAINER_multihashmap_iterate (room->messages, iterate_destroy_message, NULL);

    GNUNET_CONTAINER_multihashmap_destroy (room->messages);
  }

  if (room->contact_id)
    GNUNET_free(room->contact_id);

  GNUNET_free(room);
}

const struct GNUNET_MESSENGER_Message*
get_room_message (const struct GNUNET_MESSENGER_Room *room, const struct GNUNET_HashCode *hash)
{
  return GNUNET_CONTAINER_multihashmap_get (room->messages, hash);
}

static void
handle_join_message (struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message,
                     const struct GNUNET_HashCode *hash)
{
  struct GNUNET_MESSENGER_Contact *contact = get_handle_contact_by_pubkey (room->handle, &(message->body.join.key));

  if (contact)
    GNUNET_CONTAINER_multishortmap_put (room->members, &(message->header.sender_id), contact,
                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
}

static void
handle_leave_message (struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message,
                      const struct GNUNET_HashCode *hash)
{
  GNUNET_CONTAINER_multishortmap_remove_all (room->members, &(message->header.sender_id));
}

static void
handle_name_message (struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message,
                     const struct GNUNET_HashCode *hash)
{
  struct GNUNET_MESSENGER_Contact *contact = GNUNET_CONTAINER_multishortmap_get (room->members,
                                                                                 &(message->header.sender_id));

  if (contact)
    set_contact_name (contact, message->body.name.name);
}

static void
handle_key_message (struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message,
                    const struct GNUNET_HashCode *hash)
{
  struct GNUNET_MESSENGER_Contact *contact = GNUNET_CONTAINER_multishortmap_get (room->members,
                                                                                 &(message->header.sender_id));

  if (contact)
    swap_handle_contact_by_pubkey (room->handle, contact, &(message->body.key.key));
}

static void
handle_id_message (struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message,
                   const struct GNUNET_HashCode *hash)
{
  struct GNUNET_MESSENGER_Contact *contact = GNUNET_CONTAINER_multishortmap_get (room->members,
                                                                                 &(message->header.sender_id));

  if ((contact) && (GNUNET_OK
      == GNUNET_CONTAINER_multishortmap_put (room->members, &(message->body.id.id), contact,
                                             GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)))
    GNUNET_CONTAINER_multishortmap_remove (room->members, &(message->header.sender_id), contact);
}

static void
handle_miss_message (struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message,
                     const struct GNUNET_HashCode *hash)
{
  if ((room->contact_id) && (0 == GNUNET_memcmp(&(message->header.sender_id), room->contact_id)))
  {
    struct GNUNET_MESSENGER_ListTunnel *match = find_list_tunnels (&(room->entries), &(message->body.miss.peer), NULL);

    if (match)
      remove_from_list_tunnels (&(room->entries), match);
  }
}

void
handle_room_message (struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message,
                     const struct GNUNET_HashCode *hash)
{
  if (GNUNET_NO != GNUNET_CONTAINER_multihashmap_contains (room->messages, hash))
    return;

  switch (message->header.kind)
  {
  case GNUNET_MESSENGER_KIND_JOIN:
    handle_join_message (room, message, hash);
    break;
  case GNUNET_MESSENGER_KIND_LEAVE:
    handle_leave_message (room, message, hash);
    break;
  case GNUNET_MESSENGER_KIND_NAME:
    handle_name_message (room, message, hash);
    break;
  case GNUNET_MESSENGER_KIND_KEY:
    handle_key_message (room, message, hash);
    break;
  case GNUNET_MESSENGER_KIND_ID:
    handle_id_message (room, message, hash);
    break;
  case GNUNET_MESSENGER_KIND_MISS:
    handle_miss_message (room, message, hash);
    break;
  default:
    break;
  }

  struct GNUNET_MESSENGER_Message *clone = copy_message (message);

  if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (room->messages, hash, clone,
                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
    destroy_message (clone);
}