aboutsummaryrefslogtreecommitdiffstats
path: root/matrix-roommembers.c
blob: a437796515a6e96876fbfad6f5bd35353358d5b0 (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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * 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 *userid;

    /* the displayname we gave to purple */
    gchar *current_displayname;

    /* the current room membership */
    int membership;

    /* the displayname from the state table */
    const gchar *state_displayname;
} MatrixRoomMember;


/**
 * calculate the displayname for the given member
 *
 * @returns a string, which should be freed
 */
static gchar *_calculate_displayname_for_member(const MatrixRoomMember *member)
{
    if(member->state_displayname != NULL) {
        return g_strdup(member->state_displayname);
    } else {
        return g_strdup(member->userid);
    }
}


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->userid = g_strdup(userid);
    return mem;
}

static void _free_member(MatrixRoomMember *member)
{
    g_assert(member != NULL);
    g_free(member->userid);
    member->userid = NULL;
    g_free(member->current_displayname);
    member->current_displayname = NULL;
    g_free(member);
}


/******************************************************************************
 *
 * 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);
}


static MatrixRoomMember *_lookup_member(MatrixRoomMemberTable *table,
        const gchar *userid)
{
    return g_hash_table_lookup(table->hash_table, userid);
}


#if 0
static void _on_member_changed_displayname(PurpleConversation *conv,
        const gchar *member_user_id, MatrixRoomMember *member)
{
    PurpleConvChat *chat = PURPLE_CONV_CHAT(conv);
    gchar *old_displayname, *new_displayname;

    old_displayname = member->current_displayname;
    g_assert(old_displayname != NULL);
    new_displayname = _calculate_displayname_for_member(member_user_id, member);

    purple_conv_chat_rename_user(chat, old_displayname, new_displayname);
    g_free(old_displayname);
    member->current_displayname = new_displayname;
}


static void _on_member_left(PurpleConversation *conv,
        const gchar *member_user_id, MatrixRoomMember *member)
{
    PurpleConvChat *chat = PURPLE_CONV_CHAT(conv);
    gchar *old_displayname;

    old_displayname = member->current_displayname;
    g_assert(old_displayname != NULL);
    purple_conv_chat_remove_user(chat, old_displayname, NULL);
    g_free(old_displayname);
    member->current_displayname = NULL;
}
#endif


const gchar *matrix_roommembers_get_displayname_for_member(
        MatrixRoomMemberTable *table, const gchar *user_id)
{
    MatrixRoomMember *member = _lookup_member(table, user_id);
    gchar *displayname;

    if(member == NULL)
        return user_id;

    displayname = member -> current_displayname;

    if (displayname != NULL)
        return displayname;

    member -> current_displayname = displayname;
    return displayname;
}


void matrix_roommembers_get_new_members(MatrixRoomMemberTable *table,
        GList **display_names, GList **flags)
{
    while(table->new_members != NULL) {
        MatrixRoomMember *member = table->new_members->data;
        gchar *displayname;
        GSList *tmp;

        g_assert(member->current_displayname == NULL);
        displayname = _calculate_displayname_for_member(member);
        member->current_displayname = displayname;
        *display_names = g_list_prepend(*display_names, displayname);
        *flags = g_list_prepend(*flags, GINT_TO_POINTER(0));

        tmp = table->new_members;
        table->new_members = tmp->next;
        g_slist_free_1(tmp);
    }
}

void matrix_roommembers_get_renamed_members(MatrixRoomMemberTable *table,
        GList **old_names, GList **new_names)
{
    while(table->renamed_members != NULL) {
        MatrixRoomMember *member = table->renamed_members->data;
        gchar *displayname;
        GSList *tmp;

        g_assert(member->current_displayname != NULL);
        displayname = _calculate_displayname_for_member(member);
        *old_names = g_list_prepend(*old_names, member->current_displayname);
        *new_names = g_list_prepend(*new_names, displayname);
        member->current_displayname = displayname;

        tmp = table->renamed_members;
        table->renamed_members = tmp->next;
        g_slist_free_1(tmp);
    }
}

void matrix_roommembers_get_left_members(MatrixRoomMemberTable *table,
        GList **names)
{
    while(table->left_members != NULL) {
        MatrixRoomMember *member = table->left_members->data;
        GSList *tmp;

        g_assert(member->current_displayname != NULL);
        *names = g_list_prepend(*names, member->current_displayname);
        member->current_displayname = NULL;

        tmp = table->left_members;
        table->left_members = tmp->next;
        g_slist_free_1(tmp);
    }
}


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 = _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) {
            table->new_members = g_slist_append(
                    table->new_members, member);
        } else if(g_strcmp0(old_displayname, new_displayname) != 0) {
            table->renamed_members = g_slist_append(
                    table->renamed_members, member);
        }
    } else {
        if(old_membership_val == MATRIX_ROOM_MEMBERSHIP_JOIN) {
            table->left_members = g_slist_append(
                    table->left_members, member);
        }
    }
}


/**
 * Returns a list of user ids. Free the list, but not the string pointers.
 */
GList *matrix_roommembers_get_active_members(
        MatrixRoomMemberTable *member_table)
{
    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)) {
        const gchar *user_id = key;
        MatrixRoomMember *member = value;

        if(member->membership == MATRIX_ROOM_MEMBERSHIP_JOIN)
            members = g_list_prepend(members, (gpointer)user_id);
    }
    return members;
}