forked from matrix-org/purple-matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix-roommembers.c
282 lines (229 loc) · 7.8 KB
/
matrix-roommembers.c
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* matrix-roommember.c
*
*
* Copyright (c) Openmarket UK Ltd 2015
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "matrix-roommembers.h"
#include <stdlib.h>
#include <string.h>
#include "debug.h"
#include "matrix-json.h"
/******************************************************************************
*
* Individual members
*/
typedef struct _MatrixRoomMember {
gchar *user_id;
/* the current room membership */
int membership;
/* the displayname from the state table (this is a pointer to the actual
* string in the state table, so should not be freed here) */
const gchar *state_displayname;
/* data attached to this member (matrix-room.c uses it to track the
* name we told libpurple this member had)
*/
gpointer opaque_data;
/* callback to delete the opaque_data. Called with a pointer to the member.
*/
DestroyMemberNotify on_delete;
} MatrixRoomMember;
static int _parse_membership(const gchar *membership)
{
if(membership == NULL)
return MATRIX_ROOM_MEMBERSHIP_NONE;
if(strcmp(membership, "join") == 0)
return MATRIX_ROOM_MEMBERSHIP_JOIN;
if(strcmp(membership, "leave") == 0)
return MATRIX_ROOM_MEMBERSHIP_LEAVE;
if(strcmp(membership, "invite") == 0)
return MATRIX_ROOM_MEMBERSHIP_INVITE;
return MATRIX_ROOM_MEMBERSHIP_NONE;
}
static MatrixRoomMember *_new_member(const gchar *userid)
{
MatrixRoomMember *mem = g_new0(MatrixRoomMember, 1);
mem->user_id = g_strdup(userid);
return mem;
}
static void _free_member(MatrixRoomMember *member)
{
g_assert(member != NULL);
if(member->on_delete)
member->on_delete(member);
g_free(member->user_id);
member->user_id = NULL;
g_free(member);
}
/**
* Get the user_id for the given member
*
* @returns a string, which should *not* be freed
*/
const gchar *matrix_roommember_get_user_id(const MatrixRoomMember *member)
{
return member->user_id;
}
/**
* Get the displayname for the given member
*
* @returns a string, which should *not* be freed
*/
const gchar *matrix_roommember_get_displayname(const MatrixRoomMember *member)
{
if(member->state_displayname != NULL) {
/* TODO: if there is more than one member with this displayname, we
* should return a deduplicated name
*/
return member->state_displayname;
} else {
return member->user_id;
}
}
/**
* Get the opaque data associated with the given member
*/
gpointer matrix_roommember_get_opaque_data(const MatrixRoomMember *member)
{
return member->opaque_data;
}
/**
* Set the opaque data associated with the given member
*/
void matrix_roommember_set_opaque_data(MatrixRoomMember *member,
gpointer data, DestroyMemberNotify on_delete)
{
member->opaque_data = data;
member->on_delete = on_delete;
}
/******************************************************************************
*
* member table
*/
struct _MatrixRoomMemberTable {
GHashTable *hash_table;
GSList *new_members;
GSList *left_members;
GSList *renamed_members;
};
MatrixRoomMemberTable *matrix_roommembers_new_table()
{
MatrixRoomMemberTable *table;
table = g_new0(MatrixRoomMemberTable, 1);
table -> hash_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
(GDestroyNotify) _free_member);
return table;
}
void matrix_roommembers_free_table(MatrixRoomMemberTable *table)
{
g_hash_table_destroy(table->hash_table);
table->hash_table = NULL;
g_free(table);
}
MatrixRoomMember *matrix_roommembers_lookup_member(MatrixRoomMemberTable *table,
const gchar *member_user_id)
{
return g_hash_table_lookup(table->hash_table, member_user_id);
}
void matrix_roommembers_update_member(MatrixRoomMemberTable *table,
const gchar *member_user_id, JsonObject *new_state)
{
const gchar *old_displayname = NULL;
MatrixRoomMember *member;
int old_membership_val = MATRIX_ROOM_MEMBERSHIP_NONE,
new_membership_val;
const gchar *new_displayname, *new_membership;
new_displayname = matrix_json_object_get_string_member(
new_state, "displayname");
new_membership = matrix_json_object_get_string_member(
new_state, "membership");
new_membership_val = _parse_membership(new_membership);
member = matrix_roommembers_lookup_member(table, member_user_id);
if(member != NULL) {
old_displayname = member -> state_displayname;
old_membership_val = member -> membership;
}
if(!member) {
member = _new_member(member_user_id);
g_hash_table_insert(table->hash_table, g_strdup(member_user_id),
member);
}
member->membership = new_membership_val;
member->state_displayname = new_displayname;
purple_debug_info("matrixprpl", "member %s change %i->%i, "
"%s->%s\n", member_user_id,
old_membership_val, new_membership_val,
old_displayname, new_displayname);
if(new_membership_val == MATRIX_ROOM_MEMBERSHIP_JOIN) {
if(old_membership_val != MATRIX_ROOM_MEMBERSHIP_JOIN) {
purple_debug_info("matrixprpl", "%s (%s) joins\n",
member_user_id, new_displayname);
table->new_members = g_slist_append(
table->new_members, member);
} else if(g_strcmp0(old_displayname, new_displayname) != 0) {
purple_debug_info("matrixprpl", "%s (%s) changed name (was %s)\n",
member_user_id, new_displayname, old_displayname);
table->renamed_members = g_slist_append(
table->renamed_members, member);
}
} else {
if(old_membership_val == MATRIX_ROOM_MEMBERSHIP_JOIN) {
purple_debug_info("matrixprpl", "%s (%s) leaves\n",
member_user_id, old_displayname);
table->left_members = g_slist_append(
table->left_members, member);
}
}
}
/**
* Returns a list of MatrixRoomMember *s. Free the list, but not the pointers.
*/
GList *matrix_roommembers_get_active_members(
MatrixRoomMemberTable *member_table, gboolean include_invited)
{
GHashTableIter iter;
gpointer key, value;
GList *members = NULL;
g_hash_table_iter_init (&iter, member_table->hash_table);
while (g_hash_table_iter_next (&iter, &key, &value)) {
MatrixRoomMember *member = value;
if(member->membership == MATRIX_ROOM_MEMBERSHIP_JOIN ||
(include_invited &&
member->membership == MATRIX_ROOM_MEMBERSHIP_INVITE)) {
members = g_list_prepend(members, value);
}
}
return members;
}
GSList *matrix_roommembers_get_new_members(MatrixRoomMemberTable *table)
{
GSList *members = table->new_members;
table->new_members = NULL;
return members;
}
GSList *matrix_roommembers_get_renamed_members(MatrixRoomMemberTable *table)
{
GSList *members = table->renamed_members;
table->renamed_members = NULL;
return members;
}
GSList *matrix_roommembers_get_left_members(MatrixRoomMemberTable *table)
{
GSList *members = table->left_members;
table->left_members = NULL;
return members;
}