aboutsummaryrefslogtreecommitdiffstats
path: root/matrix-statetable.c
blob: ebc5e114b786cae1af232fbbd54ab6749e370bfe (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
/*
 * matrix-statetable.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-statetable.h"

#include "debug.h"

#include "matrix-event.h"
#include "matrix-json.h"


/**
 * create a new, empty, state table
 */
MatrixRoomStateEventTable *matrix_statetable_new()
{
    return g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
            (GDestroyNotify) g_hash_table_destroy);
}


void matrix_statetable_destroy(MatrixRoomStateEventTable *table)
{
    g_hash_table_destroy(table);
}


/**
 * look up a particular bit of state
 *
 * @returns null if this key ies not known
 */
MatrixRoomEvent *matrix_statetable_get_event(
        MatrixRoomStateEventTable *state_table, const gchar *event_type,
        const gchar *state_key)
{
    GHashTable *tmp;

    tmp = (GHashTable *) g_hash_table_lookup(state_table, event_type);
    if(tmp == NULL)
        return NULL;

    return (MatrixRoomEvent *)g_hash_table_lookup(tmp, state_key);
}


/**
 * Update the state table on a room
 */
void matrix_statetable_update(MatrixRoomStateEventTable *state_table,
        JsonObject *json_event_obj,
        MatrixStateUpdateCallback callback, gpointer user_data)
{
    const gchar *event_type, *state_key, *sender;
    JsonObject *json_content_obj;
    MatrixRoomEvent *event, *old_event;
    GHashTable *state_table_entry;

    event_type = matrix_json_object_get_string_member(
            json_event_obj, "type");
    state_key = matrix_json_object_get_string_member(
            json_event_obj, "state_key");
    sender = matrix_json_object_get_string_member(
            json_event_obj, "sender");
    json_content_obj = matrix_json_object_get_object_member(
            json_event_obj, "content");

    if(event_type == NULL || state_key == NULL || sender == NULL ||
            json_content_obj == NULL) {
        purple_debug_warning("matrixprpl", "event missing fields");
        return;
    }

    event = matrix_event_new(event_type, json_content_obj);
    event -> sender = g_strdup(sender);

    state_table_entry = g_hash_table_lookup(state_table, event_type);
    if(state_table_entry == NULL) {
        state_table_entry = g_hash_table_new_full(g_str_hash, g_str_equal,
                g_free, (GDestroyNotify)matrix_event_free);
        g_hash_table_insert(state_table, g_strdup(event_type),
                state_table_entry);
        old_event = NULL;
    } else {
        old_event = g_hash_table_lookup(state_table_entry,
                state_key);
    }

    if(callback) {
        callback(event_type, state_key, old_event, event, user_data);
    }

    g_hash_table_insert(state_table_entry, g_strdup(state_key), event);
}


/**
 * If the room has an official name, or an alias, return it
 *
 * @returns a string which should be freed
 */
gchar *matrix_statetable_get_room_alias(MatrixRoomStateEventTable *state_table)
{
    GHashTable *tmp;
    MatrixRoomEvent *event;
    const gchar *tmpname = NULL;

    /* start by looking for the official room name */
    event = matrix_statetable_get_event(state_table, "m.room.name", "");
    if(event != NULL) {
        tmpname = matrix_json_object_get_string_member(
                event->content, "name");
        if(tmpname != NULL) {
            return g_strdup(tmpname);
        }
    }

    /* look for a canonical alias */
    event = matrix_statetable_get_event(state_table, "m.room.canonical_alias",
            "");
    if(event != NULL) {
        tmpname = matrix_json_object_get_string_member(
                event->content, "alias");
        if(tmpname != NULL) {
            return g_strdup(tmpname);
        }
    }

    /* look for an alias */
    tmp = (GHashTable *) g_hash_table_lookup(state_table, "m.room.aliases");
    if(tmp != NULL) {
        GHashTableIter iter;
        gpointer key, value;

        g_hash_table_iter_init(&iter, tmp);
        while(g_hash_table_iter_next(&iter, &key, &value)) {
            MatrixRoomEvent *event = value;
            JsonArray *array = matrix_json_object_get_array_member(
                    event->content, "aliases");
            if(array != NULL && json_array_get_length(array) > 0) {
                tmpname = matrix_json_array_get_string_element(array, 0);
                if(tmpname != NULL) {
                    return g_strdup(tmpname);
                }
            }
        }
    }

    return NULL;
}