From b9e5d885f44f7a8ed5c6cc1dd2d5aafe94458cb1 Mon Sep 17 00:00:00 2001 From: LoaD Accumulator Date: Wed, 6 Sep 2023 07:31:45 +0200 Subject: [PATCH] [ADD] WIP: User directory from #70 + .gitignore. Still as WIP as the original however! --- .gitignore | 6 + src/Routes.c | 1 + src/Routes/RouteUserDirectory.c | 209 +++++ src/Schema/ClientEvent.c | 299 ++++++ src/Schema/Filter.c | 1193 ++++++++++++++++++++++++ src/Schema/PduV1.c | 477 ++++++++++ src/Schema/PduV3.c | 500 ++++++++++ src/Schema/RoomCreateRequest.c | 712 ++++++++++++++ src/include/Routes.h | 1 + src/include/Schema/ClientEvent.h | 39 + src/include/Schema/Filter.h | 80 ++ src/include/Schema/PduV1.h | 51 + src/include/Schema/PduV3.h | 50 + src/include/Schema/RoomCreateRequest.h | 82 ++ 14 files changed, 3700 insertions(+) create mode 100644 src/Routes/RouteUserDirectory.c create mode 100644 src/Schema/ClientEvent.c create mode 100644 src/Schema/Filter.c create mode 100644 src/Schema/PduV1.c create mode 100644 src/Schema/PduV3.c create mode 100644 src/Schema/RoomCreateRequest.c create mode 100644 src/include/Schema/ClientEvent.h create mode 100644 src/include/Schema/Filter.h create mode 100644 src/include/Schema/PduV1.h create mode 100644 src/include/Schema/PduV3.h create mode 100644 src/include/Schema/RoomCreateRequest.h diff --git a/.gitignore b/.gitignore index 7d63ee8..d325fae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +<<<<<<< HEAD build data .env @@ -12,3 +13,8 @@ contrib/.vagrant src/Schema src/include/Schema man/mandoc.db +======= +# Telodendria .gitignore +build/* +Cytoplasm/build/* +>>>>>>> b099a3e ([ADD] WIP: User directory from #70 + .gitignore.) diff --git a/src/Routes.c b/src/Routes.c index ca7d7bd..03f757a 100644 --- a/src/Routes.c +++ b/src/Routes.c @@ -71,6 +71,7 @@ RouterBuild(void) R("/_matrix/client/v3/profile/(.*)", RouteUserProfile); R("/_matrix/client/v3/profile/(.*)/(avatar_url|displayname)", RouteUserProfile); + R("/_matrix/client/v3/user_directory/search", RouteUserDirectory); R("/_matrix/client/v3/user/(.*)/filter", RouteFilter); R("/_matrix/client/v3/user/(.*)/filter/(.*)", RouteFilter); diff --git a/src/Routes/RouteUserDirectory.c b/src/Routes/RouteUserDirectory.c new file mode 100644 index 0000000..c1040ff --- /dev/null +++ b/src/Routes/RouteUserDirectory.c @@ -0,0 +1,209 @@ +/* + * Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net> + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include + +#include +#include +#include +#include +#include +#include +#include + +ROUTE_IMPL(RouteUserDirectory, path, argp) +{ + RouteArgs *args = argp; + HashMap *response = NULL; + HashMap *request = NULL; + + Array *users = NULL; + Array *results = NULL; + + Db *db = args->matrixArgs->db; + + Config *config = NULL; + + User *user = NULL; + + JsonValue *val = NULL; + + char *token = NULL; + char *searchTerm = NULL; + char *requesterName = NULL; + + size_t limit = 10; + + size_t i, included; + + (void) path; + + + if (HttpRequestMethodGet(args->context) != HTTP_POST) + { + HttpResponseStatus(args->context, HTTP_BAD_REQUEST); + response = MatrixErrorCreate(M_UNRECOGNIZED); + goto finish; + } + + request = JsonDecode(HttpServerStream(args->context)); + if (!request) + { + HttpResponseStatus(args->context, HTTP_BAD_REQUEST); + response = MatrixErrorCreate(M_NOT_JSON); + goto finish; + } + + response = MatrixGetAccessToken(args->context, &token); + if (response) + { + return response; + } + + /* TODO: Actually use information related to the user. */ + user = UserAuthenticate(db, token); + if (!user) + { + HttpResponseStatus(args->context, HTTP_BAD_REQUEST); + response = MatrixErrorCreate(M_UNKNOWN_TOKEN); + goto finish; + } + requesterName = UserGetName(user); + + /* Parse limit and search_term */ + if (!(val = JsonGet(request, 1, "search_term")) || + JsonValueType(val) != JSON_STRING) + { + /* The Spec requires search_term to be set to a string. */ + HttpResponseStatus(args->context, HTTP_BAD_REQUEST); + response = MatrixErrorCreate(M_BAD_JSON); + goto finish; + } + searchTerm = StrLower(JsonValueAsString(val)); + + if ((val = JsonGet(request, 1, "limit")) && + JsonValueType(val) == JSON_INTEGER) + { + /* It is however far more leinent on limit, with 10 by default. */ + limit = JsonValueAsInteger(val); + } + + response = HashMapCreate(); + results = ArrayCreate(); + + /* TODO: Check for users matching search term and users outside our + * local server. */ + users = DbList(db, 1, "users"); + + + /* Offending line? */ + config = ConfigLock(db); + if (!config) + { + Log(LOG_ERR, "Directory endpoint failed to lock configuration."); + HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR); + response = MatrixErrorCreate(M_UNKNOWN); + + goto finish; + } + + for (i = 0, included = 0; i < ArraySize(users) && included < limit; i++) + { + HashMap *obj; + User *currentUser; + char *name = ArrayGet(users, i); + char *displayName; + char *lowerDisplayName; + char *avatarUrl; + + if (!StrEquals(name, requesterName)) + { + currentUser = UserLock(db, name); + } + else + { + currentUser = user; + } + + displayName = UserGetProfile(currentUser, "displayname"); + lowerDisplayName = StrLower(displayName); + avatarUrl = UserGetProfile(currentUser, "avatar_url"); + + /* Check for the user ID and display name. */ + if (strstr(name, searchTerm) || + (lowerDisplayName && strstr(lowerDisplayName, searchTerm))) + { + included++; + + obj = HashMapCreate(); + if (displayName) + { + JsonSet(obj, JsonValueString(displayName), 1, "display_name"); + } + if (avatarUrl) + { + JsonSet(obj, JsonValueString(displayName), 1, "avatar_url"); + } + if (name) + { + char *userID = StrConcat(4, "@", name, ":", config->serverName); + JsonSet(obj, JsonValueString(userID), 1, "user_id"); + Free(userID); + } + ArrayAdd(results, JsonValueObject(obj)); + } + if (lowerDisplayName) + { + Free(lowerDisplayName); + } + if (!StrEquals(name, requesterName)) + { + UserUnlock(currentUser); + } + } + JsonSet(response, JsonValueArray(results), 1, "results"); + JsonSet(response, JsonValueBoolean(included == limit), 1, "limited"); + +finish: + if (user) + { + UserUnlock(user); + } + if (request) + { + JsonFree(request); + } + if (searchTerm) + { + Free(searchTerm); + } + if (users) + { + DbListFree(users); + } + if (config) + { + ConfigUnlock(config); + } + return response; +} diff --git a/src/Schema/ClientEvent.c b/src/Schema/ClientEvent.c new file mode 100644 index 0000000..f6295af --- /dev/null +++ b/src/Schema/ClientEvent.c @@ -0,0 +1,299 @@ +/* Generated by j2s */ + +#include + +#include +#include +#include + +int +ClientEventUnsignedDataFromJson(HashMap *json, ClientEventUnsignedData *out, char **errp) +{ + JsonValue *val; + int enumParseRes; + + (void) enumParseRes; + + if (!json | !out) + { + *errp = "Invalid pointers passed to ClientEventUnsignedDataFromJson()"; + return 0; + } + + val = HashMapGet(json, "redacted_because"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "ClientEventUnsignedData.redacted_because must be of type object."; + return 0; + } + + out->redacted_because = JsonValueAsObject(JsonValueDuplicate(val)); + } + + val = HashMapGet(json, "transaction_id"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "ClientEventUnsignedData.transaction_id must be of type string."; + return 0; + } + + out->transaction_id = StrDuplicate(JsonValueAsString(val)); + } + + val = HashMapGet(json, "prev_content"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "ClientEventUnsignedData.prev_content must be of type object."; + return 0; + } + + out->prev_content = JsonValueAsObject(JsonValueDuplicate(val)); + } + + val = HashMapGet(json, "age"); + if (val) + { + if (JsonValueType(val) != JSON_INTEGER) + { + *errp = "ClientEventUnsignedData.age must be of type integer."; + return 0; + } + + out->age = JsonValueAsInteger(val); + } + + return 1; +} + +HashMap * +ClientEventUnsignedDataToJson(ClientEventUnsignedData *val) +{ + HashMap *json; + + if (!val) + { + return NULL; + } + + json = HashMapCreate(); + if (!json) + { + return NULL; + } + + HashMapSet(json, "redacted_because", JsonValueObject(JsonDuplicate(val->redacted_because))); + HashMapSet(json, "transaction_id", JsonValueString(val->transaction_id)); + HashMapSet(json, "prev_content", JsonValueObject(JsonDuplicate(val->prev_content))); + HashMapSet(json, "age", JsonValueInteger(val->age)); + return json; +} + +void +ClientEventUnsignedDataFree(ClientEventUnsignedData *val) +{ + if (!val) + { + return; + } + + JsonFree(val->redacted_because); + Free(val->transaction_id); + JsonFree(val->prev_content); +} + +int +ClientEventFromJson(HashMap *json, ClientEvent *out, char **errp) +{ + JsonValue *val; + int enumParseRes; + + (void) enumParseRes; + + if (!json | !out) + { + *errp = "Invalid pointers passed to ClientEventFromJson()"; + return 0; + } + + val = HashMapGet(json, "origin_server_ts"); + if (val) + { + if (JsonValueType(val) != JSON_INTEGER) + { + *errp = "ClientEvent.origin_server_ts must be of type integer."; + return 0; + } + + out->origin_server_ts = JsonValueAsInteger(val); + } + else + { + *errp = "ClientEvent.origin_server_ts is required."; + return 0; + } + + val = HashMapGet(json, "content"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "ClientEvent.content must be of type object."; + return 0; + } + + out->content = JsonValueAsObject(JsonValueDuplicate(val)); + } + else + { + *errp = "ClientEvent.content is required."; + return 0; + } + + val = HashMapGet(json, "room_id"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "ClientEvent.room_id must be of type string."; + return 0; + } + + out->room_id = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "ClientEvent.room_id is required."; + return 0; + } + + val = HashMapGet(json, "sender"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "ClientEvent.sender must be of type string."; + return 0; + } + + out->sender = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "ClientEvent.sender is required."; + return 0; + } + + val = HashMapGet(json, "state_key"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "ClientEvent.state_key must be of type string."; + return 0; + } + + out->state_key = StrDuplicate(JsonValueAsString(val)); + } + + val = HashMapGet(json, "event_id"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "ClientEvent.event_id must be of type string."; + return 0; + } + + out->event_id = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "ClientEvent.event_id is required."; + return 0; + } + + val = HashMapGet(json, "type"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "ClientEvent.type must be of type string."; + return 0; + } + + out->type = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "ClientEvent.type is required."; + return 0; + } + + val = HashMapGet(json, "unsigned"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "ClientEvent.unsigned must be of type ClientEventUnsignedData."; + return 0; + } + + if (!ClientEventUnsignedDataFromJson(JsonValueAsObject(val), &out->_unsigned, errp)) + { + return 0; + } + } + + return 1; +} + +HashMap * +ClientEventToJson(ClientEvent *val) +{ + HashMap *json; + + if (!val) + { + return NULL; + } + + json = HashMapCreate(); + if (!json) + { + return NULL; + } + + HashMapSet(json, "origin_server_ts", JsonValueInteger(val->origin_server_ts)); + HashMapSet(json, "content", JsonValueObject(JsonDuplicate(val->content))); + HashMapSet(json, "room_id", JsonValueString(val->room_id)); + HashMapSet(json, "sender", JsonValueString(val->sender)); + HashMapSet(json, "state_key", JsonValueString(val->state_key)); + HashMapSet(json, "event_id", JsonValueString(val->event_id)); + HashMapSet(json, "type", JsonValueString(val->type)); + HashMapSet(json, "unsigned", JsonValueObject(ClientEventUnsignedDataToJson(&val->_unsigned))); + return json; +} + +void +ClientEventFree(ClientEvent *val) +{ + if (!val) + { + return; + } + + JsonFree(val->content); + Free(val->room_id); + Free(val->sender); + Free(val->state_key); + Free(val->event_id); + Free(val->type); + ClientEventUnsignedDataFree(&val->_unsigned); +} + diff --git a/src/Schema/Filter.c b/src/Schema/Filter.c new file mode 100644 index 0000000..d6db515 --- /dev/null +++ b/src/Schema/Filter.c @@ -0,0 +1,1193 @@ +/* Generated by j2s */ + +#include + +#include +#include +#include + +int +FilterRoomFromJson(HashMap *json, FilterRoom *out, char **errp) +{ + JsonValue *val; + int enumParseRes; + + (void) enumParseRes; + + if (!json | !out) + { + *errp = "Invalid pointers passed to FilterRoomFromJson()"; + return 0; + } + + val = HashMapGet(json, "not_rooms"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "FilterRoom.not_rooms must be of type [string]."; + return 0; + } + + out->not_rooms = ArrayCreate(); + if (!out->not_rooms) + { + *errp = "Failed to allocate memory for FilterRoom.not_rooms."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i not_rooms, StrDuplicate(JsonValueAsString(v))); + } + } + } + + val = HashMapGet(json, "state"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "FilterRoom.state must be of type FilterRoomEvent."; + return 0; + } + + if (!FilterRoomEventFromJson(JsonValueAsObject(val), &out->state, errp)) + { + return 0; + } + } + + val = HashMapGet(json, "include_leave"); + if (val) + { + if (JsonValueType(val) != JSON_BOOLEAN) + { + *errp = "FilterRoom.include_leave must be of type boolean."; + return 0; + } + + out->include_leave = JsonValueAsBoolean(val); + } + + val = HashMapGet(json, "timeline"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "FilterRoom.timeline must be of type FilterRoomEvent."; + return 0; + } + + if (!FilterRoomEventFromJson(JsonValueAsObject(val), &out->timeline, errp)) + { + return 0; + } + } + + val = HashMapGet(json, "account_data"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "FilterRoom.account_data must be of type FilterRoomEvent."; + return 0; + } + + if (!FilterRoomEventFromJson(JsonValueAsObject(val), &out->account_data, errp)) + { + return 0; + } + } + + val = HashMapGet(json, "rooms"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "FilterRoom.rooms must be of type [string]."; + return 0; + } + + out->rooms = ArrayCreate(); + if (!out->rooms) + { + *errp = "Failed to allocate memory for FilterRoom.rooms."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i rooms, StrDuplicate(JsonValueAsString(v))); + } + } + } + + val = HashMapGet(json, "ephemeral"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "FilterRoom.ephemeral must be of type FilterRoomEvent."; + return 0; + } + + if (!FilterRoomEventFromJson(JsonValueAsObject(val), &out->ephemeral, errp)) + { + return 0; + } + } + + return 1; +} + +HashMap * +FilterRoomToJson(FilterRoom *val) +{ + HashMap *json; + + if (!val) + { + return NULL; + } + + json = HashMapCreate(); + if (!json) + { + return NULL; + } + + if (val->not_rooms) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->not_rooms); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->not_rooms, i))); + } + HashMapSet(json, "not_rooms", JsonValueArray(jsonArr)); + } + HashMapSet(json, "state", JsonValueObject(FilterRoomEventToJson(&val->state))); + HashMapSet(json, "include_leave", JsonValueBoolean(val->include_leave)); + HashMapSet(json, "timeline", JsonValueObject(FilterRoomEventToJson(&val->timeline))); + HashMapSet(json, "account_data", JsonValueObject(FilterRoomEventToJson(&val->account_data))); + if (val->rooms) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->rooms); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->rooms, i))); + } + HashMapSet(json, "rooms", JsonValueArray(jsonArr)); + } + HashMapSet(json, "ephemeral", JsonValueObject(FilterRoomEventToJson(&val->ephemeral))); + return json; +} + +void +FilterRoomFree(FilterRoom *val) +{ + if (!val) + { + return; + } + + if (val->not_rooms) + { + size_t i; + for (i = 0; i < ArraySize(val->not_rooms); i++) + { + Free(ArrayGet(val->not_rooms, i)); + } + ArrayFree(val->not_rooms); + } + FilterRoomEventFree(&val->state); + FilterRoomEventFree(&val->timeline); + FilterRoomEventFree(&val->account_data); + if (val->rooms) + { + size_t i; + for (i = 0; i < ArraySize(val->rooms); i++) + { + Free(ArrayGet(val->rooms, i)); + } + ArrayFree(val->rooms); + } + FilterRoomEventFree(&val->ephemeral); +} + +int +FilterEventFormatFromStr(char *str) +{ + if (StrEquals(str, "federation")) + { + return FILTER_FORMAT_FEDERATION; + } + else if (StrEquals(str, "client")) + { + return FILTER_FORMANT_CLIENT; + } + else + { + return -1; + } +} + +char * +FilterEventFormatToStr(FilterEventFormat val) +{ + switch (val) + { + case FILTER_FORMAT_FEDERATION: + return "federation"; + case FILTER_FORMANT_CLIENT: + return "client"; + default: + return NULL; + } +} + +int +FilterEventFromJson(HashMap *json, FilterEvent *out, char **errp) +{ + JsonValue *val; + int enumParseRes; + + (void) enumParseRes; + + if (!json | !out) + { + *errp = "Invalid pointers passed to FilterEventFromJson()"; + return 0; + } + + val = HashMapGet(json, "not_senders"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "FilterEvent.not_senders must be of type [string]."; + return 0; + } + + out->not_senders = ArrayCreate(); + if (!out->not_senders) + { + *errp = "Failed to allocate memory for FilterEvent.not_senders."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i not_senders, StrDuplicate(JsonValueAsString(v))); + } + } + } + + val = HashMapGet(json, "limit"); + if (val) + { + if (JsonValueType(val) != JSON_INTEGER) + { + *errp = "FilterEvent.limit must be of type integer."; + return 0; + } + + out->limit = JsonValueAsInteger(val); + } + + val = HashMapGet(json, "senders"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "FilterEvent.senders must be of type [string]."; + return 0; + } + + out->senders = ArrayCreate(); + if (!out->senders) + { + *errp = "Failed to allocate memory for FilterEvent.senders."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i senders, StrDuplicate(JsonValueAsString(v))); + } + } + } + + val = HashMapGet(json, "types"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "FilterEvent.types must be of type [string]."; + return 0; + } + + out->types = ArrayCreate(); + if (!out->types) + { + *errp = "Failed to allocate memory for FilterEvent.types."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i types, StrDuplicate(JsonValueAsString(v))); + } + } + } + + val = HashMapGet(json, "not_types"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "FilterEvent.not_types must be of type [string]."; + return 0; + } + + out->not_types = ArrayCreate(); + if (!out->not_types) + { + *errp = "Failed to allocate memory for FilterEvent.not_types."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i not_types, StrDuplicate(JsonValueAsString(v))); + } + } + } + + return 1; +} + +HashMap * +FilterEventToJson(FilterEvent *val) +{ + HashMap *json; + + if (!val) + { + return NULL; + } + + json = HashMapCreate(); + if (!json) + { + return NULL; + } + + if (val->not_senders) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->not_senders); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->not_senders, i))); + } + HashMapSet(json, "not_senders", JsonValueArray(jsonArr)); + } + HashMapSet(json, "limit", JsonValueInteger(val->limit)); + if (val->senders) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->senders); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->senders, i))); + } + HashMapSet(json, "senders", JsonValueArray(jsonArr)); + } + if (val->types) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->types); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->types, i))); + } + HashMapSet(json, "types", JsonValueArray(jsonArr)); + } + if (val->not_types) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->not_types); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->not_types, i))); + } + HashMapSet(json, "not_types", JsonValueArray(jsonArr)); + } + return json; +} + +void +FilterEventFree(FilterEvent *val) +{ + if (!val) + { + return; + } + + if (val->not_senders) + { + size_t i; + for (i = 0; i < ArraySize(val->not_senders); i++) + { + Free(ArrayGet(val->not_senders, i)); + } + ArrayFree(val->not_senders); + } + if (val->senders) + { + size_t i; + for (i = 0; i < ArraySize(val->senders); i++) + { + Free(ArrayGet(val->senders, i)); + } + ArrayFree(val->senders); + } + if (val->types) + { + size_t i; + for (i = 0; i < ArraySize(val->types); i++) + { + Free(ArrayGet(val->types, i)); + } + ArrayFree(val->types); + } + if (val->not_types) + { + size_t i; + for (i = 0; i < ArraySize(val->not_types); i++) + { + Free(ArrayGet(val->not_types, i)); + } + ArrayFree(val->not_types); + } +} + +int +FilterFromJson(HashMap *json, Filter *out, char **errp) +{ + JsonValue *val; + int enumParseRes; + + (void) enumParseRes; + + if (!json | !out) + { + *errp = "Invalid pointers passed to FilterFromJson()"; + return 0; + } + + val = HashMapGet(json, "event_format"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "Filter.event_format must be of type FilterEventFormat."; + return 0; + } + + enumParseRes = FilterEventFormatFromStr(JsonValueAsString(val)); + if (enumParseRes == -1) + { + *errp = "Invalid value for Filter.event_format."; + return 0; + } + out->event_format = enumParseRes; + } + + val = HashMapGet(json, "presence"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "Filter.presence must be of type FilterEvent."; + return 0; + } + + if (!FilterEventFromJson(JsonValueAsObject(val), &out->presence, errp)) + { + return 0; + } + } + + val = HashMapGet(json, "account_data"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "Filter.account_data must be of type FilterEvent."; + return 0; + } + + if (!FilterEventFromJson(JsonValueAsObject(val), &out->account_data, errp)) + { + return 0; + } + } + + val = HashMapGet(json, "room"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "Filter.room must be of type FilterRoom."; + return 0; + } + + if (!FilterRoomFromJson(JsonValueAsObject(val), &out->room, errp)) + { + return 0; + } + } + + val = HashMapGet(json, "event_fields"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "Filter.event_fields must be of type [string]."; + return 0; + } + + out->event_fields = ArrayCreate(); + if (!out->event_fields) + { + *errp = "Failed to allocate memory for Filter.event_fields."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i event_fields, StrDuplicate(JsonValueAsString(v))); + } + } + } + + return 1; +} + +HashMap * +FilterToJson(Filter *val) +{ + HashMap *json; + + if (!val) + { + return NULL; + } + + json = HashMapCreate(); + if (!json) + { + return NULL; + } + + HashMapSet(json, "event_format", JsonValueString(FilterEventFormatToStr(val->event_format))); + HashMapSet(json, "presence", JsonValueObject(FilterEventToJson(&val->presence))); + HashMapSet(json, "account_data", JsonValueObject(FilterEventToJson(&val->account_data))); + HashMapSet(json, "room", JsonValueObject(FilterRoomToJson(&val->room))); + if (val->event_fields) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->event_fields); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->event_fields, i))); + } + HashMapSet(json, "event_fields", JsonValueArray(jsonArr)); + } + return json; +} + +void +FilterFree(Filter *val) +{ + if (!val) + { + return; + } + + FilterEventFree(&val->presence); + FilterEventFree(&val->account_data); + FilterRoomFree(&val->room); + if (val->event_fields) + { + size_t i; + for (i = 0; i < ArraySize(val->event_fields); i++) + { + Free(ArrayGet(val->event_fields, i)); + } + ArrayFree(val->event_fields); + } +} + +int +FilterRoomEventFromJson(HashMap *json, FilterRoomEvent *out, char **errp) +{ + JsonValue *val; + int enumParseRes; + + (void) enumParseRes; + + if (!json | !out) + { + *errp = "Invalid pointers passed to FilterRoomEventFromJson()"; + return 0; + } + + val = HashMapGet(json, "not_rooms"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "FilterRoomEvent.not_rooms must be of type [string]."; + return 0; + } + + out->not_rooms = ArrayCreate(); + if (!out->not_rooms) + { + *errp = "Failed to allocate memory for FilterRoomEvent.not_rooms."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i not_rooms, StrDuplicate(JsonValueAsString(v))); + } + } + } + + val = HashMapGet(json, "not_senders"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "FilterRoomEvent.not_senders must be of type [string]."; + return 0; + } + + out->not_senders = ArrayCreate(); + if (!out->not_senders) + { + *errp = "Failed to allocate memory for FilterRoomEvent.not_senders."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i not_senders, StrDuplicate(JsonValueAsString(v))); + } + } + } + + val = HashMapGet(json, "limit"); + if (val) + { + if (JsonValueType(val) != JSON_INTEGER) + { + *errp = "FilterRoomEvent.limit must be of type integer."; + return 0; + } + + out->limit = JsonValueAsInteger(val); + } + + val = HashMapGet(json, "senders"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "FilterRoomEvent.senders must be of type [string]."; + return 0; + } + + out->senders = ArrayCreate(); + if (!out->senders) + { + *errp = "Failed to allocate memory for FilterRoomEvent.senders."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i senders, StrDuplicate(JsonValueAsString(v))); + } + } + } + + val = HashMapGet(json, "include_redundant_members"); + if (val) + { + if (JsonValueType(val) != JSON_BOOLEAN) + { + *errp = "FilterRoomEvent.include_redundant_members must be of type boolean."; + return 0; + } + + out->include_redundant_members = JsonValueAsBoolean(val); + } + + val = HashMapGet(json, "types"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "FilterRoomEvent.types must be of type [string]."; + return 0; + } + + out->types = ArrayCreate(); + if (!out->types) + { + *errp = "Failed to allocate memory for FilterRoomEvent.types."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i types, StrDuplicate(JsonValueAsString(v))); + } + } + } + + val = HashMapGet(json, "rooms"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "FilterRoomEvent.rooms must be of type [string]."; + return 0; + } + + out->rooms = ArrayCreate(); + if (!out->rooms) + { + *errp = "Failed to allocate memory for FilterRoomEvent.rooms."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i rooms, StrDuplicate(JsonValueAsString(v))); + } + } + } + + val = HashMapGet(json, "lazy_load_members"); + if (val) + { + if (JsonValueType(val) != JSON_BOOLEAN) + { + *errp = "FilterRoomEvent.lazy_load_members must be of type boolean."; + return 0; + } + + out->lazy_load_members = JsonValueAsBoolean(val); + } + + val = HashMapGet(json, "not_types"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "FilterRoomEvent.not_types must be of type [string]."; + return 0; + } + + out->not_types = ArrayCreate(); + if (!out->not_types) + { + *errp = "Failed to allocate memory for FilterRoomEvent.not_types."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i not_types, StrDuplicate(JsonValueAsString(v))); + } + } + } + + val = HashMapGet(json, "contains_url"); + if (val) + { + if (JsonValueType(val) != JSON_BOOLEAN) + { + *errp = "FilterRoomEvent.contains_url must be of type boolean."; + return 0; + } + + out->contains_url = JsonValueAsBoolean(val); + } + + val = HashMapGet(json, "unread_thread_notifications"); + if (val) + { + if (JsonValueType(val) != JSON_BOOLEAN) + { + *errp = "FilterRoomEvent.unread_thread_notifications must be of type boolean."; + return 0; + } + + out->unread_thread_notifications = JsonValueAsBoolean(val); + } + + return 1; +} + +HashMap * +FilterRoomEventToJson(FilterRoomEvent *val) +{ + HashMap *json; + + if (!val) + { + return NULL; + } + + json = HashMapCreate(); + if (!json) + { + return NULL; + } + + if (val->not_rooms) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->not_rooms); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->not_rooms, i))); + } + HashMapSet(json, "not_rooms", JsonValueArray(jsonArr)); + } + if (val->not_senders) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->not_senders); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->not_senders, i))); + } + HashMapSet(json, "not_senders", JsonValueArray(jsonArr)); + } + HashMapSet(json, "limit", JsonValueInteger(val->limit)); + if (val->senders) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->senders); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->senders, i))); + } + HashMapSet(json, "senders", JsonValueArray(jsonArr)); + } + HashMapSet(json, "include_redundant_members", JsonValueBoolean(val->include_redundant_members)); + if (val->types) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->types); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->types, i))); + } + HashMapSet(json, "types", JsonValueArray(jsonArr)); + } + if (val->rooms) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->rooms); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->rooms, i))); + } + HashMapSet(json, "rooms", JsonValueArray(jsonArr)); + } + HashMapSet(json, "lazy_load_members", JsonValueBoolean(val->lazy_load_members)); + if (val->not_types) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->not_types); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->not_types, i))); + } + HashMapSet(json, "not_types", JsonValueArray(jsonArr)); + } + HashMapSet(json, "contains_url", JsonValueBoolean(val->contains_url)); + HashMapSet(json, "unread_thread_notifications", JsonValueBoolean(val->unread_thread_notifications)); + return json; +} + +void +FilterRoomEventFree(FilterRoomEvent *val) +{ + if (!val) + { + return; + } + + if (val->not_rooms) + { + size_t i; + for (i = 0; i < ArraySize(val->not_rooms); i++) + { + Free(ArrayGet(val->not_rooms, i)); + } + ArrayFree(val->not_rooms); + } + if (val->not_senders) + { + size_t i; + for (i = 0; i < ArraySize(val->not_senders); i++) + { + Free(ArrayGet(val->not_senders, i)); + } + ArrayFree(val->not_senders); + } + if (val->senders) + { + size_t i; + for (i = 0; i < ArraySize(val->senders); i++) + { + Free(ArrayGet(val->senders, i)); + } + ArrayFree(val->senders); + } + if (val->types) + { + size_t i; + for (i = 0; i < ArraySize(val->types); i++) + { + Free(ArrayGet(val->types, i)); + } + ArrayFree(val->types); + } + if (val->rooms) + { + size_t i; + for (i = 0; i < ArraySize(val->rooms); i++) + { + Free(ArrayGet(val->rooms, i)); + } + ArrayFree(val->rooms); + } + if (val->not_types) + { + size_t i; + for (i = 0; i < ArraySize(val->not_types); i++) + { + Free(ArrayGet(val->not_types, i)); + } + ArrayFree(val->not_types); + } +} + diff --git a/src/Schema/PduV1.c b/src/Schema/PduV1.c new file mode 100644 index 0000000..ceb2997 --- /dev/null +++ b/src/Schema/PduV1.c @@ -0,0 +1,477 @@ +/* Generated by j2s */ + +#include + +#include +#include +#include + +int +PduV1EventHashFromJson(HashMap *json, PduV1EventHash *out, char **errp) +{ + JsonValue *val; + int enumParseRes; + + (void) enumParseRes; + + if (!json | !out) + { + *errp = "Invalid pointers passed to PduV1EventHashFromJson()"; + return 0; + } + + val = HashMapGet(json, "sha256"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "PduV1EventHash.sha256 must be of type string."; + return 0; + } + + out->sha256 = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "PduV1EventHash.sha256 is required."; + return 0; + } + + return 1; +} + +HashMap * +PduV1EventHashToJson(PduV1EventHash *val) +{ + HashMap *json; + + if (!val) + { + return NULL; + } + + json = HashMapCreate(); + if (!json) + { + return NULL; + } + + HashMapSet(json, "sha256", JsonValueString(val->sha256)); + return json; +} + +void +PduV1EventHashFree(PduV1EventHash *val) +{ + if (!val) + { + return; + } + + Free(val->sha256); +} + +int +PduV1FromJson(HashMap *json, PduV1 *out, char **errp) +{ + JsonValue *val; + int enumParseRes; + + (void) enumParseRes; + + if (!json | !out) + { + *errp = "Invalid pointers passed to PduV1FromJson()"; + return 0; + } + + val = HashMapGet(json, "origin_server_ts"); + if (val) + { + if (JsonValueType(val) != JSON_INTEGER) + { + *errp = "PduV1.origin_server_ts must be of type integer."; + return 0; + } + + out->origin_server_ts = JsonValueAsInteger(val); + } + else + { + *errp = "PduV1.origin_server_ts is required."; + return 0; + } + + val = HashMapGet(json, "content"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "PduV1.content must be of type object."; + return 0; + } + + out->content = JsonValueAsObject(JsonValueDuplicate(val)); + } + else + { + *errp = "PduV1.content is required."; + return 0; + } + + val = HashMapGet(json, "redacts"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "PduV1.redacts must be of type string."; + return 0; + } + + out->redacts = StrDuplicate(JsonValueAsString(val)); + } + + val = HashMapGet(json, "sender"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "PduV1.sender must be of type string."; + return 0; + } + + out->sender = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "PduV1.sender is required."; + return 0; + } + + val = HashMapGet(json, "depth"); + if (val) + { + if (JsonValueType(val) != JSON_INTEGER) + { + *errp = "PduV1.depth must be of type integer."; + return 0; + } + + out->depth = JsonValueAsInteger(val); + } + else + { + *errp = "PduV1.depth is required."; + return 0; + } + + val = HashMapGet(json, "prev_events"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "PduV1.prev_events must be of type array."; + return 0; + } + + out->prev_events = JsonValueAsArray(JsonValueDuplicate(val)); + } + else + { + *errp = "PduV1.prev_events is required."; + return 0; + } + + val = HashMapGet(json, "type"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "PduV1.type must be of type string."; + return 0; + } + + out->type = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "PduV1.type is required."; + return 0; + } + + val = HashMapGet(json, "unsigned"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "PduV1.unsigned must be of type PduV1UnsignedData."; + return 0; + } + + if (!PduV1UnsignedDataFromJson(JsonValueAsObject(val), &out->_unsigned, errp)) + { + return 0; + } + } + + val = HashMapGet(json, "auth_events"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "PduV1.auth_events must be of type array."; + return 0; + } + + out->auth_events = JsonValueAsArray(JsonValueDuplicate(val)); + } + else + { + *errp = "PduV1.auth_events is required."; + return 0; + } + + val = HashMapGet(json, "room_id"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "PduV1.room_id must be of type string."; + return 0; + } + + out->room_id = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "PduV1.room_id is required."; + return 0; + } + + val = HashMapGet(json, "state_key"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "PduV1.state_key must be of type string."; + return 0; + } + + out->state_key = StrDuplicate(JsonValueAsString(val)); + } + + val = HashMapGet(json, "signatures"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "PduV1.signatures must be of type object."; + return 0; + } + + out->signatures = JsonValueAsObject(JsonValueDuplicate(val)); + } + else + { + *errp = "PduV1.signatures is required."; + return 0; + } + + val = HashMapGet(json, "event_id"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "PduV1.event_id must be of type string."; + return 0; + } + + out->event_id = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "PduV1.event_id is required."; + return 0; + } + + val = HashMapGet(json, "hashes"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "PduV1.hashes must be of type PduV1EventHash."; + return 0; + } + + if (!PduV1EventHashFromJson(JsonValueAsObject(val), &out->hashes, errp)) + { + return 0; + } + } + else + { + *errp = "PduV1.hashes is required."; + return 0; + } + + return 1; +} + +HashMap * +PduV1ToJson(PduV1 *val) +{ + HashMap *json; + + if (!val) + { + return NULL; + } + + json = HashMapCreate(); + if (!json) + { + return NULL; + } + + HashMapSet(json, "origin_server_ts", JsonValueInteger(val->origin_server_ts)); + HashMapSet(json, "content", JsonValueObject(JsonDuplicate(val->content))); + HashMapSet(json, "redacts", JsonValueString(val->redacts)); + HashMapSet(json, "sender", JsonValueString(val->sender)); + HashMapSet(json, "depth", JsonValueInteger(val->depth)); + if (val->prev_events) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->prev_events); i++) + { + ArrayAdd(jsonArr, JsonValueDuplicate(ArrayGet(val->prev_events, i))); + } + HashMapSet(json, "prev_events", JsonValueArray(jsonArr)); + } + HashMapSet(json, "type", JsonValueString(val->type)); + HashMapSet(json, "unsigned", JsonValueObject(PduV1UnsignedDataToJson(&val->_unsigned))); + if (val->auth_events) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->auth_events); i++) + { + ArrayAdd(jsonArr, JsonValueDuplicate(ArrayGet(val->auth_events, i))); + } + HashMapSet(json, "auth_events", JsonValueArray(jsonArr)); + } + HashMapSet(json, "room_id", JsonValueString(val->room_id)); + HashMapSet(json, "state_key", JsonValueString(val->state_key)); + HashMapSet(json, "signatures", JsonValueObject(JsonDuplicate(val->signatures))); + HashMapSet(json, "event_id", JsonValueString(val->event_id)); + HashMapSet(json, "hashes", JsonValueObject(PduV1EventHashToJson(&val->hashes))); + return json; +} + +void +PduV1Free(PduV1 *val) +{ + if (!val) + { + return; + } + + JsonFree(val->content); + Free(val->redacts); + Free(val->sender); + if (val->prev_events) + { + size_t i; + for (i = 0; i < ArraySize(val->prev_events); i++) + { + JsonValueFree(ArrayGet(val->prev_events, i)); + } + ArrayFree(val->prev_events); + } + Free(val->type); + PduV1UnsignedDataFree(&val->_unsigned); + if (val->auth_events) + { + size_t i; + for (i = 0; i < ArraySize(val->auth_events); i++) + { + JsonValueFree(ArrayGet(val->auth_events, i)); + } + ArrayFree(val->auth_events); + } + Free(val->room_id); + Free(val->state_key); + JsonFree(val->signatures); + Free(val->event_id); + PduV1EventHashFree(&val->hashes); +} + +int +PduV1UnsignedDataFromJson(HashMap *json, PduV1UnsignedData *out, char **errp) +{ + JsonValue *val; + int enumParseRes; + + (void) enumParseRes; + + if (!json | !out) + { + *errp = "Invalid pointers passed to PduV1UnsignedDataFromJson()"; + return 0; + } + + val = HashMapGet(json, "age"); + if (val) + { + if (JsonValueType(val) != JSON_INTEGER) + { + *errp = "PduV1UnsignedData.age must be of type integer."; + return 0; + } + + out->age = JsonValueAsInteger(val); + } + + return 1; +} + +HashMap * +PduV1UnsignedDataToJson(PduV1UnsignedData *val) +{ + HashMap *json; + + if (!val) + { + return NULL; + } + + json = HashMapCreate(); + if (!json) + { + return NULL; + } + + HashMapSet(json, "age", JsonValueInteger(val->age)); + return json; +} + +void +PduV1UnsignedDataFree(PduV1UnsignedData *val) +{ + if (!val) + { + return; + } + +} + diff --git a/src/Schema/PduV3.c b/src/Schema/PduV3.c new file mode 100644 index 0000000..ef2bc8b --- /dev/null +++ b/src/Schema/PduV3.c @@ -0,0 +1,500 @@ +/* Generated by j2s */ + +#include + +#include +#include +#include + +int +PduV3EventHashFromJson(HashMap *json, PduV3EventHash *out, char **errp) +{ + JsonValue *val; + int enumParseRes; + + (void) enumParseRes; + + if (!json | !out) + { + *errp = "Invalid pointers passed to PduV3EventHashFromJson()"; + return 0; + } + + val = HashMapGet(json, "sha256"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "PduV3EventHash.sha256 must be of type string."; + return 0; + } + + out->sha256 = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "PduV3EventHash.sha256 is required."; + return 0; + } + + return 1; +} + +HashMap * +PduV3EventHashToJson(PduV3EventHash *val) +{ + HashMap *json; + + if (!val) + { + return NULL; + } + + json = HashMapCreate(); + if (!json) + { + return NULL; + } + + HashMapSet(json, "sha256", JsonValueString(val->sha256)); + return json; +} + +void +PduV3EventHashFree(PduV3EventHash *val) +{ + if (!val) + { + return; + } + + Free(val->sha256); +} + +int +PduV3FromJson(HashMap *json, PduV3 *out, char **errp) +{ + JsonValue *val; + int enumParseRes; + + (void) enumParseRes; + + if (!json | !out) + { + *errp = "Invalid pointers passed to PduV3FromJson()"; + return 0; + } + + val = HashMapGet(json, "origin_server_ts"); + if (val) + { + if (JsonValueType(val) != JSON_INTEGER) + { + *errp = "PduV3.origin_server_ts must be of type integer."; + return 0; + } + + out->origin_server_ts = JsonValueAsInteger(val); + } + else + { + *errp = "PduV3.origin_server_ts is required."; + return 0; + } + + val = HashMapGet(json, "content"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "PduV3.content must be of type object."; + return 0; + } + + out->content = JsonValueAsObject(JsonValueDuplicate(val)); + } + else + { + *errp = "PduV3.content is required."; + return 0; + } + + val = HashMapGet(json, "redacts"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "PduV3.redacts must be of type string."; + return 0; + } + + out->redacts = StrDuplicate(JsonValueAsString(val)); + } + + val = HashMapGet(json, "sender"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "PduV3.sender must be of type string."; + return 0; + } + + out->sender = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "PduV3.sender is required."; + return 0; + } + + val = HashMapGet(json, "depth"); + if (val) + { + if (JsonValueType(val) != JSON_INTEGER) + { + *errp = "PduV3.depth must be of type integer."; + return 0; + } + + out->depth = JsonValueAsInteger(val); + } + else + { + *errp = "PduV3.depth is required."; + return 0; + } + + val = HashMapGet(json, "prev_events"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "PduV3.prev_events must be of type [string]."; + return 0; + } + + out->prev_events = ArrayCreate(); + if (!out->prev_events) + { + *errp = "Failed to allocate memory for PduV3.prev_events."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i prev_events, StrDuplicate(JsonValueAsString(v))); + } + } + } + else + { + *errp = "PduV3.prev_events is required."; + return 0; + } + + val = HashMapGet(json, "type"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "PduV3.type must be of type string."; + return 0; + } + + out->type = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "PduV3.type is required."; + return 0; + } + + val = HashMapGet(json, "unsigned"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "PduV3.unsigned must be of type PduV3UnsignedData."; + return 0; + } + + if (!PduV3UnsignedDataFromJson(JsonValueAsObject(val), &out->_unsigned, errp)) + { + return 0; + } + } + + val = HashMapGet(json, "auth_events"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "PduV3.auth_events must be of type [string]."; + return 0; + } + + out->auth_events = ArrayCreate(); + if (!out->auth_events) + { + *errp = "Failed to allocate memory for PduV3.auth_events."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i auth_events, StrDuplicate(JsonValueAsString(v))); + } + } + } + else + { + *errp = "PduV3.auth_events is required."; + return 0; + } + + val = HashMapGet(json, "room_id"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "PduV3.room_id must be of type string."; + return 0; + } + + out->room_id = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "PduV3.room_id is required."; + return 0; + } + + val = HashMapGet(json, "state_key"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "PduV3.state_key must be of type string."; + return 0; + } + + out->state_key = StrDuplicate(JsonValueAsString(val)); + } + + val = HashMapGet(json, "signatures"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "PduV3.signatures must be of type object."; + return 0; + } + + out->signatures = JsonValueAsObject(JsonValueDuplicate(val)); + } + else + { + *errp = "PduV3.signatures is required."; + return 0; + } + + val = HashMapGet(json, "hashes"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "PduV3.hashes must be of type PduV3EventHash."; + return 0; + } + + if (!PduV3EventHashFromJson(JsonValueAsObject(val), &out->hashes, errp)) + { + return 0; + } + } + else + { + *errp = "PduV3.hashes is required."; + return 0; + } + + return 1; +} + +HashMap * +PduV3ToJson(PduV3 *val) +{ + HashMap *json; + + if (!val) + { + return NULL; + } + + json = HashMapCreate(); + if (!json) + { + return NULL; + } + + HashMapSet(json, "origin_server_ts", JsonValueInteger(val->origin_server_ts)); + HashMapSet(json, "content", JsonValueObject(JsonDuplicate(val->content))); + HashMapSet(json, "redacts", JsonValueString(val->redacts)); + HashMapSet(json, "sender", JsonValueString(val->sender)); + HashMapSet(json, "depth", JsonValueInteger(val->depth)); + if (val->prev_events) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->prev_events); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->prev_events, i))); + } + HashMapSet(json, "prev_events", JsonValueArray(jsonArr)); + } + HashMapSet(json, "type", JsonValueString(val->type)); + HashMapSet(json, "unsigned", JsonValueObject(PduV3UnsignedDataToJson(&val->_unsigned))); + if (val->auth_events) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->auth_events); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->auth_events, i))); + } + HashMapSet(json, "auth_events", JsonValueArray(jsonArr)); + } + HashMapSet(json, "room_id", JsonValueString(val->room_id)); + HashMapSet(json, "state_key", JsonValueString(val->state_key)); + HashMapSet(json, "signatures", JsonValueObject(JsonDuplicate(val->signatures))); + HashMapSet(json, "hashes", JsonValueObject(PduV3EventHashToJson(&val->hashes))); + return json; +} + +void +PduV3Free(PduV3 *val) +{ + if (!val) + { + return; + } + + JsonFree(val->content); + Free(val->redacts); + Free(val->sender); + if (val->prev_events) + { + size_t i; + for (i = 0; i < ArraySize(val->prev_events); i++) + { + Free(ArrayGet(val->prev_events, i)); + } + ArrayFree(val->prev_events); + } + Free(val->type); + PduV3UnsignedDataFree(&val->_unsigned); + if (val->auth_events) + { + size_t i; + for (i = 0; i < ArraySize(val->auth_events); i++) + { + Free(ArrayGet(val->auth_events, i)); + } + ArrayFree(val->auth_events); + } + Free(val->room_id); + Free(val->state_key); + JsonFree(val->signatures); + PduV3EventHashFree(&val->hashes); +} + +int +PduV3UnsignedDataFromJson(HashMap *json, PduV3UnsignedData *out, char **errp) +{ + JsonValue *val; + int enumParseRes; + + (void) enumParseRes; + + if (!json | !out) + { + *errp = "Invalid pointers passed to PduV3UnsignedDataFromJson()"; + return 0; + } + + val = HashMapGet(json, "age"); + if (val) + { + if (JsonValueType(val) != JSON_INTEGER) + { + *errp = "PduV3UnsignedData.age must be of type integer."; + return 0; + } + + out->age = JsonValueAsInteger(val); + } + + return 1; +} + +HashMap * +PduV3UnsignedDataToJson(PduV3UnsignedData *val) +{ + HashMap *json; + + if (!val) + { + return NULL; + } + + json = HashMapCreate(); + if (!json) + { + return NULL; + } + + HashMapSet(json, "age", JsonValueInteger(val->age)); + return json; +} + +void +PduV3UnsignedDataFree(PduV3UnsignedData *val) +{ + if (!val) + { + return; + } + +} + diff --git a/src/Schema/RoomCreateRequest.c b/src/Schema/RoomCreateRequest.c new file mode 100644 index 0000000..6e38dd6 --- /dev/null +++ b/src/Schema/RoomCreateRequest.c @@ -0,0 +1,712 @@ +/* Generated by j2s */ + +#include + +#include +#include +#include + +int +RoomVisibilityFromStr(char *str) +{ + if (StrEquals(str, "public")) + { + return ROOM_PUBLIC; + } + else if (StrEquals(str, "private")) + { + return ROOM_PRIVATE; + } + else + { + return -1; + } +} + +char * +RoomVisibilityToStr(RoomVisibility val) +{ + switch (val) + { + case ROOM_PUBLIC: + return "public"; + case ROOM_PRIVATE: + return "private"; + default: + return NULL; + } +} + +int +RoomCreateRequestFromJson(HashMap *json, RoomCreateRequest *out, char **errp) +{ + JsonValue *val; + int enumParseRes; + + (void) enumParseRes; + + if (!json | !out) + { + *errp = "Invalid pointers passed to RoomCreateRequestFromJson()"; + return 0; + } + + val = HashMapGet(json, "invite"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "RoomCreateRequest.invite must be of type [string]."; + return 0; + } + + out->invite = ArrayCreate(); + if (!out->invite) + { + *errp = "Failed to allocate memory for RoomCreateRequest.invite."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i invite, StrDuplicate(JsonValueAsString(v))); + } + } + } + + val = HashMapGet(json, "room_version"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "RoomCreateRequest.room_version must be of type string."; + return 0; + } + + out->room_version = StrDuplicate(JsonValueAsString(val)); + } + + val = HashMapGet(json, "invite_3pid"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "RoomCreateRequest.invite_3pid must be of type [RoomInvite3Pid]."; + return 0; + } + + out->invite_3pid = ArrayCreate(); + if (!out->invite_3pid) + { + *errp = "Failed to allocate memory for RoomCreateRequest.invite_3pid."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i invite_3pid, parsed); + } + } + } + + val = HashMapGet(json, "topic"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "RoomCreateRequest.topic must be of type string."; + return 0; + } + + out->topic = StrDuplicate(JsonValueAsString(val)); + } + + val = HashMapGet(json, "visibility"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "RoomCreateRequest.visibility must be of type RoomVisibility."; + return 0; + } + + enumParseRes = RoomVisibilityFromStr(JsonValueAsString(val)); + if (enumParseRes == -1) + { + *errp = "Invalid value for RoomCreateRequest.visibility."; + return 0; + } + out->visibility = enumParseRes; + } + + val = HashMapGet(json, "creation_content"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "RoomCreateRequest.creation_content must be of type object."; + return 0; + } + + out->creation_content = JsonValueAsObject(JsonValueDuplicate(val)); + } + + val = HashMapGet(json, "is_direct"); + if (val) + { + if (JsonValueType(val) != JSON_BOOLEAN) + { + *errp = "RoomCreateRequest.is_direct must be of type boolean."; + return 0; + } + + out->is_direct = JsonValueAsBoolean(val); + } + + val = HashMapGet(json, "name"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "RoomCreateRequest.name must be of type string."; + return 0; + } + + out->name = StrDuplicate(JsonValueAsString(val)); + } + + val = HashMapGet(json, "initial_state"); + if (val) + { + if (JsonValueType(val) != JSON_ARRAY) + { + *errp = "RoomCreateRequest.initial_state must be of type [RoomStateEvent]."; + return 0; + } + + out->initial_state = ArrayCreate(); + if (!out->initial_state) + { + *errp = "Failed to allocate memory for RoomCreateRequest.initial_state."; + return 0; + } + else + { + Array *arr = JsonValueAsArray(val); + size_t i; + + for (i = 0; i initial_state, parsed); + } + } + } + + val = HashMapGet(json, "power_level_content_override"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "RoomCreateRequest.power_level_content_override must be of type object."; + return 0; + } + + out->power_level_content_override = JsonValueAsObject(JsonValueDuplicate(val)); + } + + val = HashMapGet(json, "room_alias_name"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "RoomCreateRequest.room_alias_name must be of type string."; + return 0; + } + + out->room_alias_name = StrDuplicate(JsonValueAsString(val)); + } + + val = HashMapGet(json, "preset"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "RoomCreateRequest.preset must be of type RoomCreatePreset."; + return 0; + } + + enumParseRes = RoomCreatePresetFromStr(JsonValueAsString(val)); + if (enumParseRes == -1) + { + *errp = "Invalid value for RoomCreateRequest.preset."; + return 0; + } + out->preset = enumParseRes; + } + + return 1; +} + +HashMap * +RoomCreateRequestToJson(RoomCreateRequest *val) +{ + HashMap *json; + + if (!val) + { + return NULL; + } + + json = HashMapCreate(); + if (!json) + { + return NULL; + } + + if (val->invite) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->invite); i++) + { + ArrayAdd(jsonArr, JsonValueString(ArrayGet(val->invite, i))); + } + HashMapSet(json, "invite", JsonValueArray(jsonArr)); + } + HashMapSet(json, "room_version", JsonValueString(val->room_version)); + if (val->invite_3pid) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->invite_3pid); i++) + { + ArrayAdd(jsonArr, JsonValueObject(RoomInvite3PidToJson(ArrayGet(val->invite_3pid, i)))); + } + HashMapSet(json, "invite_3pid", JsonValueArray(jsonArr)); + } + HashMapSet(json, "topic", JsonValueString(val->topic)); + HashMapSet(json, "visibility", JsonValueString(RoomVisibilityToStr(val->visibility))); + HashMapSet(json, "creation_content", JsonValueObject(JsonDuplicate(val->creation_content))); + HashMapSet(json, "is_direct", JsonValueBoolean(val->is_direct)); + HashMapSet(json, "name", JsonValueString(val->name)); + if (val->initial_state) + { + size_t i; + Array *jsonArr = ArrayCreate(); + if (!jsonArr) + { + JsonFree(json); + return NULL; + } + for (i = 0; i < ArraySize(val->initial_state); i++) + { + ArrayAdd(jsonArr, JsonValueObject(RoomStateEventToJson(ArrayGet(val->initial_state, i)))); + } + HashMapSet(json, "initial_state", JsonValueArray(jsonArr)); + } + HashMapSet(json, "power_level_content_override", JsonValueObject(JsonDuplicate(val->power_level_content_override))); + HashMapSet(json, "room_alias_name", JsonValueString(val->room_alias_name)); + HashMapSet(json, "preset", JsonValueString(RoomCreatePresetToStr(val->preset))); + return json; +} + +void +RoomCreateRequestFree(RoomCreateRequest *val) +{ + if (!val) + { + return; + } + + if (val->invite) + { + size_t i; + for (i = 0; i < ArraySize(val->invite); i++) + { + Free(ArrayGet(val->invite, i)); + } + ArrayFree(val->invite); + } + Free(val->room_version); + if (val->invite_3pid) + { + size_t i; + for (i = 0; i < ArraySize(val->invite_3pid); i++) + { + RoomInvite3PidFree(ArrayGet(val->invite_3pid, i)); + Free(ArrayGet(val->invite_3pid, i)); + } + ArrayFree(val->invite_3pid); + } + Free(val->topic); + JsonFree(val->creation_content); + Free(val->name); + if (val->initial_state) + { + size_t i; + for (i = 0; i < ArraySize(val->initial_state); i++) + { + RoomStateEventFree(ArrayGet(val->initial_state, i)); + Free(ArrayGet(val->initial_state, i)); + } + ArrayFree(val->initial_state); + } + JsonFree(val->power_level_content_override); + Free(val->room_alias_name); +} + +int +RoomInvite3PidFromJson(HashMap *json, RoomInvite3Pid *out, char **errp) +{ + JsonValue *val; + int enumParseRes; + + (void) enumParseRes; + + if (!json | !out) + { + *errp = "Invalid pointers passed to RoomInvite3PidFromJson()"; + return 0; + } + + val = HashMapGet(json, "id_access_token"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "RoomInvite3Pid.id_access_token must be of type string."; + return 0; + } + + out->id_access_token = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "RoomInvite3Pid.id_access_token is required."; + return 0; + } + + val = HashMapGet(json, "address"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "RoomInvite3Pid.address must be of type string."; + return 0; + } + + out->address = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "RoomInvite3Pid.address is required."; + return 0; + } + + val = HashMapGet(json, "medium"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "RoomInvite3Pid.medium must be of type Room3PidMedium."; + return 0; + } + + enumParseRes = Room3PidMediumFromStr(JsonValueAsString(val)); + if (enumParseRes == -1) + { + *errp = "Invalid value for RoomInvite3Pid.medium."; + return 0; + } + out->medium = enumParseRes; + } + else + { + *errp = "RoomInvite3Pid.medium is required."; + return 0; + } + + val = HashMapGet(json, "id_server"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "RoomInvite3Pid.id_server must be of type string."; + return 0; + } + + out->id_server = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "RoomInvite3Pid.id_server is required."; + return 0; + } + + return 1; +} + +HashMap * +RoomInvite3PidToJson(RoomInvite3Pid *val) +{ + HashMap *json; + + if (!val) + { + return NULL; + } + + json = HashMapCreate(); + if (!json) + { + return NULL; + } + + HashMapSet(json, "id_access_token", JsonValueString(val->id_access_token)); + HashMapSet(json, "address", JsonValueString(val->address)); + HashMapSet(json, "medium", JsonValueString(Room3PidMediumToStr(val->medium))); + HashMapSet(json, "id_server", JsonValueString(val->id_server)); + return json; +} + +void +RoomInvite3PidFree(RoomInvite3Pid *val) +{ + if (!val) + { + return; + } + + Free(val->id_access_token); + Free(val->address); + Free(val->id_server); +} + +int +Room3PidMediumFromStr(char *str) +{ + if (StrEquals(str, "msisdn")) + { + return ROOM_3PID_MSISDN; + } + else if (StrEquals(str, "email")) + { + return ROOM_3PID_EMAIL; + } + else + { + return -1; + } +} + +char * +Room3PidMediumToStr(Room3PidMedium val) +{ + switch (val) + { + case ROOM_3PID_MSISDN: + return "msisdn"; + case ROOM_3PID_EMAIL: + return "email"; + default: + return NULL; + } +} + +int +RoomCreatePresetFromStr(char *str) +{ + if (StrEquals(str, "public_chat")) + { + return ROOM_CREATE_PUBLIC; + } + else if (StrEquals(str, "trusted_private_chat")) + { + return ROOM_CREATE_TRUSTED; + } + else if (StrEquals(str, "private_chat")) + { + return ROOM_CREATE_PRIVATE; + } + else + { + return -1; + } +} + +char * +RoomCreatePresetToStr(RoomCreatePreset val) +{ + switch (val) + { + case ROOM_CREATE_PUBLIC: + return "public_chat"; + case ROOM_CREATE_TRUSTED: + return "trusted_private_chat"; + case ROOM_CREATE_PRIVATE: + return "private_chat"; + default: + return NULL; + } +} + +int +RoomStateEventFromJson(HashMap *json, RoomStateEvent *out, char **errp) +{ + JsonValue *val; + int enumParseRes; + + (void) enumParseRes; + + if (!json | !out) + { + *errp = "Invalid pointers passed to RoomStateEventFromJson()"; + return 0; + } + + val = HashMapGet(json, "content"); + if (val) + { + if (JsonValueType(val) != JSON_OBJECT) + { + *errp = "RoomStateEvent.content must be of type object."; + return 0; + } + + out->content = JsonValueAsObject(JsonValueDuplicate(val)); + } + else + { + *errp = "RoomStateEvent.content is required."; + return 0; + } + + val = HashMapGet(json, "state_key"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "RoomStateEvent.state_key must be of type string."; + return 0; + } + + out->state_key = StrDuplicate(JsonValueAsString(val)); + } + + val = HashMapGet(json, "type"); + if (val) + { + if (JsonValueType(val) != JSON_STRING) + { + *errp = "RoomStateEvent.type must be of type string."; + return 0; + } + + out->type = StrDuplicate(JsonValueAsString(val)); + } + else + { + *errp = "RoomStateEvent.type is required."; + return 0; + } + + return 1; +} + +HashMap * +RoomStateEventToJson(RoomStateEvent *val) +{ + HashMap *json; + + if (!val) + { + return NULL; + } + + json = HashMapCreate(); + if (!json) + { + return NULL; + } + + HashMapSet(json, "content", JsonValueObject(JsonDuplicate(val->content))); + HashMapSet(json, "state_key", JsonValueString(val->state_key)); + HashMapSet(json, "type", JsonValueString(val->type)); + return json; +} + +void +RoomStateEventFree(RoomStateEvent *val) +{ + if (!val) + { + return; + } + + JsonFree(val->content); + Free(val->state_key); + Free(val->type); +} + diff --git a/src/include/Routes.h b/src/include/Routes.h index 3c0598a..a9fd135 100644 --- a/src/include/Routes.h +++ b/src/include/Routes.h @@ -85,6 +85,7 @@ ROUTE(RouteChangePwd); ROUTE(RouteDeactivate); ROUTE(RouteTokenValid); ROUTE(RouteUserProfile); +ROUTE(RouteUserDirectory); ROUTE(RouteRequestToken); ROUTE(RouteUiaFallback); diff --git a/src/include/Schema/ClientEvent.h b/src/include/Schema/ClientEvent.h new file mode 100644 index 0000000..e8bbdb0 --- /dev/null +++ b/src/include/Schema/ClientEvent.h @@ -0,0 +1,39 @@ +/* Generated by j2s */ + +#ifndef TELODENDRIA_SCHEMA_CLIENTEVENT_H +#define TELODENDRIA_SCHEMA_CLIENTEVENT_H + +#include +#include +#include + + +typedef struct ClientEventUnsignedData +{ + HashMap * redacted_because; + char * transaction_id; + HashMap * prev_content; + Int64 age; +} ClientEventUnsignedData; + +typedef struct ClientEvent +{ + Int64 origin_server_ts; + HashMap * content; + char * room_id; + char * sender; + char * state_key; + char * event_id; + char * type; + ClientEventUnsignedData _unsigned; +} ClientEvent; + +extern int ClientEventUnsignedDataFromJson(HashMap *, ClientEventUnsignedData *, char **); +extern HashMap * ClientEventUnsignedDataToJson(ClientEventUnsignedData *); +extern void ClientEventUnsignedDataFree(ClientEventUnsignedData *); + +extern int ClientEventFromJson(HashMap *, ClientEvent *, char **); +extern HashMap * ClientEventToJson(ClientEvent *); +extern void ClientEventFree(ClientEvent *); + +#endif /* TELODENDRIA_SCHEMA_CLIENTEVENT_H */ diff --git a/src/include/Schema/Filter.h b/src/include/Schema/Filter.h new file mode 100644 index 0000000..676579c --- /dev/null +++ b/src/include/Schema/Filter.h @@ -0,0 +1,80 @@ +/* Generated by j2s */ + +#ifndef TELODENDRIA_SCHEMA_FILTER_H +#define TELODENDRIA_SCHEMA_FILTER_H + +#include +#include +#include + + +typedef struct FilterEvent +{ + Array * not_senders; + Int64 limit; + Array * senders; + Array * types; + Array * not_types; +} FilterEvent; + +typedef enum FilterEventFormat +{ + FILTER_FORMAT_FEDERATION, + FILTER_FORMANT_CLIENT +} FilterEventFormat; + +typedef struct FilterRoomEvent +{ + Array * not_rooms; + Array * not_senders; + Int64 limit; + Array * senders; + int include_redundant_members; + Array * types; + Array * rooms; + int lazy_load_members; + Array * not_types; + int contains_url; + int unread_thread_notifications; +} FilterRoomEvent; + +typedef struct FilterRoom +{ + Array * not_rooms; + FilterRoomEvent state; + int include_leave; + FilterRoomEvent timeline; + FilterRoomEvent account_data; + Array * rooms; + FilterRoomEvent ephemeral; +} FilterRoom; + +typedef struct Filter +{ + FilterEventFormat event_format; + FilterEvent presence; + FilterEvent account_data; + FilterRoom room; + Array * event_fields; +} Filter; + +extern int FilterRoomFromJson(HashMap *, FilterRoom *, char **); +extern HashMap * FilterRoomToJson(FilterRoom *); +extern void FilterRoomFree(FilterRoom *); + +extern int FilterEventFormatFromStr(char *); +extern char * FilterEventFormatToStr(FilterEventFormat); + +extern int FilterEventFromJson(HashMap *, FilterEvent *, char **); +extern HashMap * FilterEventToJson(FilterEvent *); +extern void FilterEventFree(FilterEvent *); + +extern int FilterFromJson(HashMap *, Filter *, char **); +extern HashMap * FilterToJson(Filter *); +extern void FilterFree(Filter *); + +extern int FilterRoomEventFromJson(HashMap *, FilterRoomEvent *, char **); +extern HashMap * FilterRoomEventToJson(FilterRoomEvent *); +extern void FilterRoomEventFree(FilterRoomEvent *); + +#endif /* TELODENDRIA_SCHEMA_FILTER_H */ diff --git a/src/include/Schema/PduV1.h b/src/include/Schema/PduV1.h new file mode 100644 index 0000000..5094e82 --- /dev/null +++ b/src/include/Schema/PduV1.h @@ -0,0 +1,51 @@ +/* Generated by j2s */ + +#ifndef TELODENDRIA_SCHEMA_PDUV1_H +#define TELODENDRIA_SCHEMA_PDUV1_H + +#include +#include +#include + + +typedef struct PduV1UnsignedData +{ + Int64 age; +} PduV1UnsignedData; + +typedef struct PduV1EventHash +{ + char * sha256; +} PduV1EventHash; + +typedef struct PduV1 +{ + Int64 origin_server_ts; + HashMap * content; + char * redacts; + char * sender; + Int64 depth; + Array * prev_events; + char * type; + PduV1UnsignedData _unsigned; + Array * auth_events; + char * room_id; + char * state_key; + HashMap * signatures; + char * event_id; + PduV1EventHash hashes; +} PduV1; + +extern int PduV1EventHashFromJson(HashMap *, PduV1EventHash *, char **); +extern HashMap * PduV1EventHashToJson(PduV1EventHash *); +extern void PduV1EventHashFree(PduV1EventHash *); + +extern int PduV1FromJson(HashMap *, PduV1 *, char **); +extern HashMap * PduV1ToJson(PduV1 *); +extern void PduV1Free(PduV1 *); + +extern int PduV1UnsignedDataFromJson(HashMap *, PduV1UnsignedData *, char **); +extern HashMap * PduV1UnsignedDataToJson(PduV1UnsignedData *); +extern void PduV1UnsignedDataFree(PduV1UnsignedData *); + +#endif /* TELODENDRIA_SCHEMA_PDUV1_H */ diff --git a/src/include/Schema/PduV3.h b/src/include/Schema/PduV3.h new file mode 100644 index 0000000..e853343 --- /dev/null +++ b/src/include/Schema/PduV3.h @@ -0,0 +1,50 @@ +/* Generated by j2s */ + +#ifndef TELODENDRIA_SCHEMA_PDUV3_H +#define TELODENDRIA_SCHEMA_PDUV3_H + +#include +#include +#include + + +typedef struct PduV3UnsignedData +{ + Int64 age; +} PduV3UnsignedData; + +typedef struct PduV3EventHash +{ + char * sha256; +} PduV3EventHash; + +typedef struct PduV3 +{ + Int64 origin_server_ts; + HashMap * content; + char * redacts; + char * sender; + Int64 depth; + Array * prev_events; + char * type; + PduV3UnsignedData _unsigned; + Array * auth_events; + char * room_id; + char * state_key; + HashMap * signatures; + PduV3EventHash hashes; +} PduV3; + +extern int PduV3EventHashFromJson(HashMap *, PduV3EventHash *, char **); +extern HashMap * PduV3EventHashToJson(PduV3EventHash *); +extern void PduV3EventHashFree(PduV3EventHash *); + +extern int PduV3FromJson(HashMap *, PduV3 *, char **); +extern HashMap * PduV3ToJson(PduV3 *); +extern void PduV3Free(PduV3 *); + +extern int PduV3UnsignedDataFromJson(HashMap *, PduV3UnsignedData *, char **); +extern HashMap * PduV3UnsignedDataToJson(PduV3UnsignedData *); +extern void PduV3UnsignedDataFree(PduV3UnsignedData *); + +#endif /* TELODENDRIA_SCHEMA_PDUV3_H */ diff --git a/src/include/Schema/RoomCreateRequest.h b/src/include/Schema/RoomCreateRequest.h new file mode 100644 index 0000000..52f8d38 --- /dev/null +++ b/src/include/Schema/RoomCreateRequest.h @@ -0,0 +1,82 @@ +/* Generated by j2s */ + +#ifndef TELODENDRIA_SCHEMA_ROOMCREATE_H +#define TELODENDRIA_SCHEMA_ROOMCREATE_H + +#include +#include +#include + + +typedef enum Room3PidMedium +{ + ROOM_3PID_MSISDN, + ROOM_3PID_EMAIL +} Room3PidMedium; + +typedef enum RoomCreatePreset +{ + ROOM_CREATE_PUBLIC, + ROOM_CREATE_TRUSTED, + ROOM_CREATE_PRIVATE +} RoomCreatePreset; + +typedef struct RoomStateEvent +{ + HashMap * content; + char * state_key; + char * type; +} RoomStateEvent; + +typedef struct RoomInvite3Pid +{ + char * id_access_token; + char * address; + Room3PidMedium medium; + char * id_server; +} RoomInvite3Pid; + +typedef enum RoomVisibility +{ + ROOM_PUBLIC, + ROOM_PRIVATE +} RoomVisibility; + +typedef struct RoomCreateRequest +{ + Array * invite; + char * room_version; + Array * invite_3pid; + char * topic; + RoomVisibility visibility; + HashMap * creation_content; + int is_direct; + char * name; + Array * initial_state; + HashMap * power_level_content_override; + char * room_alias_name; + RoomCreatePreset preset; +} RoomCreateRequest; + +extern int RoomVisibilityFromStr(char *); +extern char * RoomVisibilityToStr(RoomVisibility); + +extern int RoomCreateRequestFromJson(HashMap *, RoomCreateRequest *, char **); +extern HashMap * RoomCreateRequestToJson(RoomCreateRequest *); +extern void RoomCreateRequestFree(RoomCreateRequest *); + +extern int RoomInvite3PidFromJson(HashMap *, RoomInvite3Pid *, char **); +extern HashMap * RoomInvite3PidToJson(RoomInvite3Pid *); +extern void RoomInvite3PidFree(RoomInvite3Pid *); + +extern int Room3PidMediumFromStr(char *); +extern char * Room3PidMediumToStr(Room3PidMedium); + +extern int RoomCreatePresetFromStr(char *); +extern char * RoomCreatePresetToStr(RoomCreatePreset); + +extern int RoomStateEventFromJson(HashMap *, RoomStateEvent *, char **); +extern HashMap * RoomStateEventToJson(RoomStateEvent *); +extern void RoomStateEventFree(RoomStateEvent *); + +#endif /* TELODENDRIA_SCHEMA_ROOMCREATE_H */