matrix-glib-sdk/src/matrix-types.c

396 lines
12 KiB
C
Raw Normal View History

/*
* This file is part of matrix-glib-sdk
*
* matrix-glib-sdk is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* matrix-glib-sdk 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with matrix-glib-sdk. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include "matrix-types.h"
2016-01-15 16:59:45 +00:00
#include "matrix-enumtypes.h"
#include "matrix-glib.h"
/**
* SECTION:matrix-types
* @title: Generic types
* @short_description: Generic types for Matrix GLib SDK calls
*
* These are the generic types used by many SDK calls for
* communication with the homeserver.
*/
/**
2016-01-25 11:17:44 +00:00
* MatrixError:
* @MATRIX_ERROR_NONE: no error
* @MATRIX_ERROR_COMMUNICATION_ERROR: there was a problem in
* communication (e.g. connection
* error)
* @MATRIX_ERROR_INCOMPLETE: the passed/generated data is incomplete
* @MATRIX_ERROR_BAD_REQUEST: the request is invalid
* @MATRIX_ERROR_BAD_RESPONSE: malformed response, or the response is
* not a JSON object
* @MATRIX_ERROR_INVALID_ROOM_ID: the provided string doesnt contain
* a valid room ID
* @MATRIX_ERROR_INVALID_TYPE: the provided type is invalid
* @MATRIX_ERROR_UNSUPPORTED: the operation is unsupported
* @MATRIX_ERROR_INVALID_FORMAT: the format of the JSON node is
* invalid (e.g. it is an array instead
* of an object)
2016-01-25 11:17:44 +00:00
* @MATRIX_ERROR_UNKNOWN_VALUE: the response from the Matrix.org
* server contains a value unknown to
* this library. These should be reported
* to the Matrix GLib SDK developers
2016-02-03 17:19:10 +00:00
* @MATRIX_ERROR_M_MISSING_TOKEN: authorization token is missing from
* the request
* @MATRIX_ERROR_M_FORBIDDEN: access was forbidden (e.g. due to a
* missing/invalid token, or using a bad
* password during login)
* @MATRIX_ERROR_M_UNKNOWN: an error unknown to the Matrix homeserver
* @MATRIX_ERROR_M_UNKNOWN_TOKEN: the token provided is not known for
* the homeserver
* @MATRIX_ERROR_M_NOT_JSON: illegal request, the content is not valid
* JSON
* @MATRIX_ERROR_M_UNRECOGNIZED: the homeserver didn't understand the
* request
* @MATRIX_ERROR_M_UNAUTHORIZED: the request is unauthorized
* @MATRIX_ERROR_M_BAD_JSON: the JSON data is not in the required
* format
* @MATRIX_ERROR_M_USER_IN_USE: the specified username is in use
* @MATRIX_ERROR_M_ROOM_IN_USE: the specified room is in use
* @MATRIX_ERROR_M_BAD_PAGINATION: invalid pagination parameters
* @MATRIX_ERROR_M_BAD_STATE: invalid state event
* @MATRIX_ERROR_M_NOT_FOUND: the requested resource is not found
* @MATRIX_ERROR_M_GUEST_ACCESS_FORBIDDEN: guest access was requested,
* but it is forbidden
* @MATRIX_ERROR_M_LIMIT_EXCEEDED: the request was rate limited
* @MATRIX_ERROR_M_CAPTCHA_NEEDED: a captcha is needed to continue
* @MATRIX_ERROR_M_CAPTCHA_INVALID: the provided captcha is invalid
* @MATRIX_ERROR_M_MISSING_PARAM: a parameter is missing from the
* request
* @MATRIX_ERROR_M_TOO_LARGE: the request data is too large
* @MATRIX_ERROR_M_EXCLUSIVE: the desired user ID is in an exclusive
* namespace claimed by an application
* server
* @MATRIX_ERROR_M_THREEPID_AUTH_FAILED: 3rd party authentication
* failed
* @MATRIX_ERROR_M_THREEPID_IN_USE: the provided 3rd party ID is
* already in use
* @MATRIX_ERROR_M_INVALID_USERNAME: the given username is invalid
2016-02-03 17:19:10 +00:00
* @MATRIX_ERROR_UNSPECIFIED: no error code was sent by the
* homeserver. If you see this error, that
* usually indicates a homeserver bug
2016-01-25 11:17:44 +00:00
* @MATRIX_ERROR_UNKNOWN_ERROR: an error unknown to this library
*
* Value mappings from Matrix.org API error codes
* (e.g. <code>M_MISSING_TOKEN</code>). They should be set
* automatically by API calls, if the response contains an error code.
*/
/**
2016-01-25 11:17:44 +00:00
* MATRIX_ERROR:
*
2016-01-25 11:17:44 +00:00
* Error domain for Matrix GLib SDK. See #GError for more information
* on error domains.
*/
/**
2016-01-25 11:17:44 +00:00
* matrix_error_quark:
*
2016-01-25 11:17:44 +00:00
* Gets the Matrix error #GQuark
*/
2016-01-25 11:17:44 +00:00
G_DEFINE_QUARK(matrix-error-quark, matrix_error);
/**
* MatrixRoomPreset:
* @MATRIX_ROOM_PRESET_NONE: no preset
* @MATRIX_ROOM_PRESET_PRIVATE: preset for private rooms
* @MATRIX_ROOM_PRESET_TRUSTED_PRIVATE: same as private rooms, but all
* users get the same power level
* as the room creator
* @MATRIX_ROOM_PRESET_PUBLIC: preset for public rooms
*
* Preset values for matrix_api_create_room() calls.
*/
/**
* MatrixRoomVisibility:
* @MATRIX_ROOM_VISIBILITY_DEFAULT: use a server-assigned value
* (usually <code>private</code>
* @MATRIX_ROOM_VISIBILITY_PUBLIC: make the room visible in the public
* room list
* @MATRIX_ROOM_VISIBILITY_PRIVATE: hide the room from the public room
* list
*
* Visibility values for room creation. Not to be confused with join
* rules.
*/
/**
* MatrixPusherKind:
* @MATRIX_PUSHER_KIND_OVERRIDE: highest priority rules
* @MATRIX_PUSHER_KIND_SENDER: for (unencrypted) messages that match
* certain patterns
* @MATRIX_PUSHER_KIND_ROOM: for all messages for a given room. The
* rule ID of a room rule is always the ID
* of the room that it affects
* @MATRIX_PUSHER_KIND_CONTENT: for messages from a specific Matrix
* user ID. The rule ID of such rules is
* always the Matrix ID of the user whose
* messages they'd apply to
* @MATRIX_PUSHER_KIND_UNDERRIDE: lowest priority rules
*
* Pusher types.
*/
/**
* MatrixReceiptType:
* @MATRIX_RECEIPT_TYPE_READ: indicate that the message has been read
*
* Receipt types of acknowledgment.
*/
2016-01-11 12:45:51 +00:00
/**
* MatrixStateEvent:
2016-01-11 12:45:51 +00:00
*
* An opaque structure to hold a state event filter.
*/
struct _MatrixStateEvent {
2016-01-11 12:45:51 +00:00
gchar *type;
gchar *state_key;
JsonNode *content;
guint refcount;
};
G_DEFINE_BOXED_TYPE(MatrixStateEvent, matrix_state_event,
(GBoxedCopyFunc)matrix_state_event_ref,
(GBoxedFreeFunc)matrix_state_event_unref);
2016-01-11 12:45:51 +00:00
/**
* matrix_state_event_new:
2016-01-11 12:45:51 +00:00
*
* Create a new #MatrixStateEvent object with reference count of 1.
2016-01-11 12:45:51 +00:00
*
* Returns: (transfer full): a new #MatrixStateEvent
2016-01-11 12:45:51 +00:00
*/
MatrixStateEvent *
matrix_state_event_new(void)
2016-01-11 12:45:51 +00:00
{
MatrixStateEvent *event;
2016-01-11 12:45:51 +00:00
event = g_new0(MatrixStateEvent, 1);
2016-01-11 12:45:51 +00:00
event->refcount = 1;
return event;
}
static void
matrix_state_event_free(MatrixStateEvent *event)
2016-01-11 12:45:51 +00:00
{
g_free(event->type);
g_free(event->state_key);
if (event->content) {
json_node_free(event->content);
}
g_free(event);
}
/**
* matrix_state_event_ref:
* @event: a #MatrixStateEvent
2016-01-11 12:45:51 +00:00
*
* Increase reference count of @event by one.
*
* Returns: (transfer none): the same #MatrixStateEvent
2016-01-11 12:45:51 +00:00
*/
MatrixStateEvent *
matrix_state_event_ref(MatrixStateEvent *event)
2016-01-11 12:45:51 +00:00
{
event->refcount++;
return event;
}
/**
* matrix_state_event_unref:
* @event: a #MatrixStateEvent
2016-01-11 12:45:51 +00:00
*
* Decrease reference count of @event by one. If reference count
* reaches zero, @event is freed.
*/
void
matrix_state_event_unref(MatrixStateEvent *event)
2016-01-11 12:45:51 +00:00
{
if (--event->refcount == 0) {
matrix_state_event_free(event);
2016-01-11 12:45:51 +00:00
}
}
/**
* matrix_state_event_set_event_type:
* @event: a #MatrixStateEvent
2016-01-11 12:45:51 +00:00
* @event_type: the type of the state event
*
* Set the type of the state event in @event.
*/
void
matrix_state_event_set_event_type(MatrixStateEvent *event,
const gchar *event_type)
2016-01-11 12:45:51 +00:00
{
g_free(event->type);
event->type = g_strdup(event_type);
}
/**
* matrix_state_event_get_event_type:
* @event: a #MatrixStateEvent
2016-01-11 12:45:51 +00:00
*
* Get the type of the state event in @event.
*
* Returns: (transfer none): the event type. The returned value is
* owned by @event and should not be freed nor modified.
*/
const gchar *
matrix_state_event_get_event_type(MatrixStateEvent *event)
2016-01-11 12:45:51 +00:00
{
return event->type;
}
/**
* matrix_state_event_set_state_key:
* @event: a #MatrixStateEvent
2016-01-11 12:45:51 +00:00
* @state_key: the key of the state event
*
* Set the state key for the state event @event.
*/
void
matrix_state_event_set_state_key(MatrixStateEvent *event,
const gchar *state_key)
2016-01-11 12:45:51 +00:00
{
g_free(event->state_key);
event->state_key = g_strdup(state_key);
}
/**
* matrix_state_event_get_state_key:
* @event: a #MatrixStateEvent
2016-01-11 12:45:51 +00:00
*
* Get the state key of @event.
*
* Returns: (transfer none): the state key. The returned value is
* owned by @event and should not be freed nor modified
*/
const gchar *
matrix_state_event_get_state_key(MatrixStateEvent *event)
2016-01-11 12:45:51 +00:00
{
return event->state_key;
}
/**
* matrix_state_event_set_content:
* @event: a #MatrixStateEvent
2016-01-11 12:45:51 +00:00
* @content: the desired content of the state event
*
* Set the content of the state event.
*/
void
matrix_state_event_set_content(MatrixStateEvent *event, const JsonNode *content)
2016-01-11 12:45:51 +00:00
{
if (event->content) {
json_node_free(event->content);
}
event->content = json_node_copy((JsonNode *)content);
}
/**
* matrix_state_event_get_content:
* @event: a #MatrixStateEvent
2016-01-11 12:45:51 +00:00
*
* Get the contents of the state event.
*
* Returns: (transfer none): the contents of the state event. The
* returned value is owned by @event and should not be freed
* nor modified
*/
const JsonNode *
matrix_state_event_get_content(MatrixStateEvent *event)
2016-01-11 12:45:51 +00:00
{
return event->content;
}
/**
* matrix_state_event_get_json_node:
* @event: a #MatrixStateEvent
2016-01-11 12:45:51 +00:00
*
* Get the JSON representation of the state event as a #JsonNode
* object.
*
* Returns: (transfer full): the JSON representation of the state
* event
*/
JsonNode *
matrix_state_event_get_json_node(MatrixStateEvent *event)
2016-01-11 12:45:51 +00:00
{
JsonBuilder *builder;
JsonNode *node;
builder = json_builder_new();
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "state_key");
json_builder_add_string_value(builder, event->state_key);
json_builder_set_member_name(builder, "type");
json_builder_add_string_value(builder, event->type);
json_builder_set_member_name(builder, "content");
json_builder_add_value(builder, event->content);
json_builder_end_object(builder);
node = json_builder_get_root(builder);
g_object_unref(builder);
return node;
}
/**
* matrix_state_event_get_json_data:
* @event: a #MatrixStateEvent
2016-01-11 12:45:51 +00:00
* @datalen: (out): storage for the the length of the JSON data or
* %NULL
*
* Get the JSON representation of the state event as a string.
*
* Returns: (transfer full): the JSON representation of the state
* event
*/
gchar *
matrix_state_event_get_json_data(MatrixStateEvent *event, gsize *datalen)
2016-01-11 12:45:51 +00:00
{
JsonGenerator *generator;
JsonNode *node = matrix_state_event_get_json_node(event);
2016-01-11 12:45:51 +00:00
gchar *data;
generator = json_generator_new();
json_generator_set_root(generator, node);
json_node_free(node);
data = json_generator_to_data(generator, datalen);
g_object_unref(generator);
return data;
}