aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet_chat_group.c
blob: 3f9cae0f540222b02358d83aab3f2b873d599ef1 (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
/*
 * @author Tobias Frisch
 * @file gnunet_chat_group.c
 */

#include "gnunet_chat_lib.h"
#include "gnunet_chat_group.h"
#include "gnunet_chat_handle.h"

struct GNUNET_CHAT_Group*
group_create(struct GNUNET_CHAT_Handle *handle)
{
  struct GNUNET_CHAT_Group *group = GNUNET_new(struct GNUNET_CHAT_Group);

  group->handle = handle;
  group->context = context_create(handle, NULL);
  group->name = NULL;

  if (!group->context)
  {
    group_destroy (group);
    return NULL;
  }

  const struct GNUNET_HashCode *key = context_get_key(group->context);

  const int result = GNUNET_CONTAINER_multihashmap_put(
      handle->groups,
      key,
      group,
      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
  );

  if (GNUNET_OK != result) {
      group_destroy(group);
      return NULL;
  }

  return group;
}

void
group_destroy(struct GNUNET_CHAT_Group* group)
{
  if (!group->context)
    goto skip_context;

  struct GNUNET_CHAT_Handle *handle = group->handle;
  const struct GNUNET_HashCode *key = context_get_key(group->context);

  GNUNET_CONTAINER_multihashmap_remove(
      handle->groups,
      key,
      group
  );

  context_destroy(group->context);

skip_context:
  if (group->name)
    GNUNET_free(group->name);

  GNUNET_free(group);
}

void
GNUNET_CHAT_group_leave (struct GNUNET_CHAT_Group *group)
{
  if (!group)
    return;

  if (group->context)
    GNUNET_MESSENGER_close_room(group->context->room);

  group_destroy(group);
}

void
GNUNET_CHAT_group_set_name (struct GNUNET_CHAT_Group *group,
			    const char *name)
{
  if (!group)
    return;

  if (group->name)
    GNUNET_free(group->name);

  group->name = name? GNUNET_strdup(name) : NULL;
}

const char*
GNUNET_CHAT_group_get_name (const struct GNUNET_CHAT_Group *group)
{
  if (!group)
    return NULL;

  return group->name;
}

void
GNUNET_CHAT_group_invite_contact (struct GNUNET_CHAT_Group *group,
				  struct GNUNET_CHAT_Contact *contact)
{
  if ((!group) || (!contact))
    return;

  struct GNUNET_CHAT_Context *context = GNUNET_CHAT_contact_get_context(contact);

  if (!context)
    return;

  struct GNUNET_MESSENGER_Handle *messenger = group->handle->handles.messenger;
  GNUNET_MESSENGER_open_room(messenger, context_get_key(group->context));

  struct GNUNET_MESSENGER_Message message;
  message.header.kind = GNUNET_MESSENGER_KIND_INVITE;

  int result = GNUNET_CRYPTO_get_peer_identity(
      group->handle->cfg,
      &(message.body.invite.door)
  );

  if (GNUNET_OK != result)
    return;

  GNUNET_memcpy(
      &(message.body.invite.key),
      context_get_key(group->context),
      sizeof(message.body.invite.key)
  );

  GNUNET_MESSENGER_send_message(context->room, &message, NULL);
}

struct GNUNET_CHAT_GroupIterateContacts
{
  struct GNUNET_CHAT_Group *group;
  GNUNET_CHAT_GroupContactCallback callback;
  void *cls;
};

static int
group_iterate_members (void* cls, struct GNUNET_MESSENGER_Room *room,
                       const struct GNUNET_MESSENGER_Contact *contact)
{
  struct GNUNET_CHAT_GroupIterateContacts *iterate = cls;

  return iterate->callback(iterate->cls, iterate->group, NULL); // TODO
}

int
GNUNET_CHAT_group_iterate_contacts (struct GNUNET_CHAT_Group *group,
				    GNUNET_CHAT_GroupContactCallback callback,
				    void *cls)
{
  if (!group)
    return GNUNET_SYSERR;

  if (!callback)
    return GNUNET_MESSENGER_iterate_members(group->context->room, NULL, cls);

  struct GNUNET_CHAT_GroupIterateContacts iterate;
  iterate.group = group;
  iterate.callback = callback;
  iterate.cls = cls;

  return GNUNET_MESSENGER_iterate_members(
      group->context->room, group_iterate_members, &iterate
  );
}

struct GNUNET_CHAT_Context*
GNUNET_CHAT_group_get_context (struct GNUNET_CHAT_Group *group)
{
  if (!group)
    return NULL;

  return group->context;
}