matrix-glib-sdk/src/matrix-http-api.c

2153 lines
63 KiB
C
Raw Normal View History

2015-12-11 13:15:42 +00:00
/*
* 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 "config.h"
2015-12-14 15:07:35 +00:00
#include "matrix-http-api.h"
2016-01-11 15:28:58 +00:00
#include "matrix-enumtypes.h"
2015-12-11 13:15:42 +00:00
#include <string.h>
#include <libsoup/soup.h>
/**
* SECTION:matrix-http-api
* @short_description: Low level API calls to communicate with a
* Matrix.org server via HTTP
* @title: MatrixHTTPAPI
* @stability: Unstable
* @include: matrix-glib/matrix.h
*
* This is a class for low level communication with a Matrix.org
* server via HTTP.
*/
/**
* MatrixHTTPAPI:
*
* The MatrixHTTPAPI objects instance definition.
*/
/**
* MatrixHTTPAPIClass:
* @parent_class: the parent class structure (#GObjectClass)
*
* The MatrixHTTPAPI objects class definition.
*/
2015-12-15 16:37:06 +00:00
#define API_ENDPOINT "/_matrix/client/api/v1/"
2016-01-15 12:13:23 +00:00
#define MEDIA_ENDPOINT "/_matrix/media/v1/"
2015-12-11 13:15:42 +00:00
typedef struct _MatrixHTTPAPIPrivate {
SoupSession *soup_session;
2015-12-15 16:37:06 +00:00
SoupURI *uri;
2016-01-15 12:13:23 +00:00
SoupURI *media_uri;
2015-12-11 13:15:42 +00:00
gchar *token;
2016-01-04 16:37:50 +00:00
gchar *refresh_token;
2016-01-04 16:39:48 +00:00
gchar *user_id;
2016-01-06 13:10:35 +00:00
gchar *homeserver;
2015-12-11 13:15:42 +00:00
gboolean validate_certificate;
} MatrixHTTPAPIPrivate;
enum {
PROP_VALIDATE_CERTIFICATE = 1,
PROP_BASE_URL,
2015-12-14 15:07:35 +00:00
PROP_TOKEN,
2016-01-04 16:37:50 +00:00
PROP_REFRESH_TOKEN,
2016-01-04 16:39:48 +00:00
PROP_USER_ID,
2016-01-06 13:10:35 +00:00
PROP_HOMESERVER,
2015-12-11 13:15:42 +00:00
N_PROPERTIES
};
2016-01-15 12:13:23 +00:00
typedef enum {
CALL_API,
CALL_MEDIA
} CallType;
2015-12-14 15:27:47 +00:00
typedef struct {
MatrixHTTPAPI *api;
JsonNode *request_content;
2016-01-15 12:33:27 +00:00
GByteArray *raw_content;
2015-12-14 15:27:47 +00:00
MatrixAPICallback callback;
gpointer callback_data;
2016-01-15 12:06:23 +00:00
gboolean accept_non_json;
2016-01-15 12:13:23 +00:00
CallType call_type;
2015-12-14 15:27:47 +00:00
} MatrixHTTPAPIRequest;
2015-12-11 13:15:42 +00:00
GParamSpec *obj_properties[N_PROPERTIES] = {NULL,};
static void matrix_http_api_matrix_api_init(MatrixAPIInterface *iface);
static void i_set_token(MatrixAPI *api, const gchar *token);
static const gchar *i_get_token(MatrixAPI *api);
2016-01-04 16:37:50 +00:00
static void i_set_refresh_token(MatrixAPI *api, const gchar *refresh_token);
static const gchar *i_get_refresh_token(MatrixAPI *api);
2016-01-04 16:39:48 +00:00
static const gchar *i_get_user_id(MatrixAPI *api);
2016-01-06 13:10:35 +00:00
static const gchar *i_get_homeserver(MatrixAPI *api);
2015-12-11 13:15:42 +00:00
G_DEFINE_TYPE_WITH_CODE(MatrixHTTPAPI, matrix_http_api, G_TYPE_OBJECT,
G_ADD_PRIVATE(MatrixHTTPAPI)
G_IMPLEMENT_INTERFACE(MATRIX_TYPE_API,
matrix_http_api_matrix_api_init));
2015-12-11 13:15:42 +00:00
static void
matrix_http_api_finalize(GObject *gobject)
{
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(
MATRIX_HTTP_API(gobject));
g_free(priv->token);
2016-01-04 16:37:50 +00:00
g_free(priv->refresh_token);
2016-01-04 16:39:48 +00:00
g_free(priv->user_id);
2016-01-06 13:10:35 +00:00
g_free(priv->homeserver);
2015-12-11 13:15:42 +00:00
g_signal_handlers_destroy(gobject);
G_OBJECT_CLASS(matrix_http_api_parent_class)->finalize(gobject);
}
2016-01-15 11:38:37 +00:00
static void
_set_url(SoupURI **uri, const gchar *base, const gchar *endpoint)
{
gchar *url;
SoupURI *new_uri;
if (base[strlen(base) - 1] == '/') {
url = g_strdup_printf("%s%s", base, endpoint + 1);
} else {
url = g_strdup_printf("%s%s", base, endpoint);
}
new_uri = soup_uri_new(url);
if (new_uri && SOUP_URI_VALID_FOR_HTTP(new_uri)) {
*uri = new_uri;
} else {
if (new_uri) {
soup_uri_free(new_uri);
}
*uri = NULL;
}
}
2015-12-11 13:15:42 +00:00
static void
matrix_http_api_set_property(GObject *gobject,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
2015-12-11 13:15:42 +00:00
{
MatrixHTTPAPI *api = MATRIX_HTTP_API(gobject);
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(api);
switch (prop_id) {
case PROP_VALIDATE_CERTIFICATE:
matrix_http_api_set_validate_certificate(api, g_value_get_boolean(value));
2015-12-11 13:15:42 +00:00
break;
case PROP_BASE_URL:
2015-12-11 13:15:42 +00:00
{
const gchar *base_url;
gchar *last_occurence;
2016-01-15 12:13:23 +00:00
SoupURI *api_uri, *media_uri;
2015-12-11 13:15:42 +00:00
base_url = g_value_get_string(value);
if (!g_str_is_ascii(base_url)) {
g_warning("URL specified (%s) is not ASCII", base_url);
return;
}
/* Check if the provided URL already ends with the API endpoint */
2016-01-15 11:38:37 +00:00
if ((last_occurence = g_strrstr(base_url, API_ENDPOINT)) != NULL) {
g_warning("Provided URL (%s) already contains the API endpoint. Please use an URL without it!", base_url);
2016-01-15 11:38:37 +00:00
return;
2015-12-11 13:15:42 +00:00
}
2016-01-15 11:38:37 +00:00
_set_url(&api_uri, base_url, API_ENDPOINT);
2016-01-15 12:13:23 +00:00
_set_url(&media_uri, base_url, MEDIA_ENDPOINT);
2016-01-15 11:38:37 +00:00
2016-01-15 12:13:23 +00:00
if (api_uri && media_uri) {
gchar *api_url, *media_url;
if (priv->uri) {
soup_uri_free(priv->uri);
}
2016-01-15 12:13:23 +00:00
if (priv->media_uri) {
soup_uri_free(priv->media_uri);
}
2016-01-15 11:38:37 +00:00
priv->uri = api_uri;
2016-01-15 12:13:23 +00:00
priv->media_uri = media_uri;
2016-01-15 11:38:37 +00:00
// Free all tokens and IDs, as they wont be valid for
// the new server
g_free(priv->token);
priv->token = NULL;
g_free(priv->refresh_token);
priv->refresh_token = NULL;
g_free(priv->homeserver);
priv->homeserver = NULL;
g_free(priv->user_id);
priv->user_id = NULL;
2016-01-15 11:38:37 +00:00
api_url = soup_uri_to_string(api_uri, FALSE);
2016-01-15 12:13:23 +00:00
media_url = soup_uri_to_string(media_uri, FALSE);
2016-01-15 11:38:37 +00:00
g_debug("API URL: %s", api_url);
2016-01-15 12:13:23 +00:00
g_debug("Media URL: %s", media_url);
2016-01-15 11:38:37 +00:00
g_free(api_url);
2016-01-15 12:13:23 +00:00
g_free(media_url);
} else {
2016-01-15 11:38:37 +00:00
if (api_uri) {
soup_uri_free(api_uri);
}
2016-01-15 12:13:23 +00:00
if (media_uri) {
soup_uri_free(media_uri);
}
2016-01-15 11:38:37 +00:00
g_warning("Invalid URL: %s", base_url);
}
2015-12-15 16:37:06 +00:00
2015-12-11 13:15:42 +00:00
break;
}
2015-12-14 15:07:35 +00:00
case PROP_TOKEN:
i_set_token(MATRIX_API(api), g_value_get_string(value));
2015-12-14 15:07:35 +00:00
break;
2016-01-04 16:37:50 +00:00
case PROP_REFRESH_TOKEN:
i_set_refresh_token(MATRIX_API(api),
g_value_get_string(value));
break;
2015-12-11 13:15:42 +00:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
}
}
static void
matrix_http_api_get_property(GObject *gobject,
guint prop_id,
GValue *value,
GParamSpec *pspec)
2015-12-11 13:15:42 +00:00
{
MatrixHTTPAPI *api = MATRIX_HTTP_API(gobject);
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(api);
switch (prop_id) {
case PROP_VALIDATE_CERTIFICATE:
g_value_set_boolean(value, priv->validate_certificate);
break;
case PROP_BASE_URL:
2015-12-15 16:37:06 +00:00
g_value_take_string(value, soup_uri_to_string(priv->uri, FALSE));
2015-12-11 13:15:42 +00:00
break;
2015-12-14 15:07:35 +00:00
case PROP_TOKEN:
g_value_set_string(value,
i_get_token(MATRIX_API(api)));
2015-12-14 15:07:35 +00:00
break;
2016-01-04 16:37:50 +00:00
case PROP_REFRESH_TOKEN:
g_value_set_string(value, i_get_refresh_token(MATRIX_API(api)));
break;
2016-01-04 16:39:48 +00:00
case PROP_USER_ID:
g_value_set_string(value, i_get_user_id(MATRIX_API(api)));
break;
2016-01-06 13:10:35 +00:00
case PROP_HOMESERVER:
g_value_set_string(value, i_get_homeserver(MATRIX_API(api)));
break;
2015-12-11 13:15:42 +00:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
}
}
static void
matrix_http_api_class_init(MatrixHTTPAPIClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
gobject_class->set_property = matrix_http_api_set_property;
gobject_class->get_property = matrix_http_api_get_property;
gobject_class->finalize = matrix_http_api_finalize;
/**
* MatrixHTTPAPI:validate-certificate:
*
* Set to %FALSE if you dont want the SSL/TLS certificates to be
* validated.
*/
obj_properties[PROP_VALIDATE_CERTIFICATE] = g_param_spec_boolean(
"validate-certificate",
"Validate certificate",
"TRUE if server certificates should be validated",
TRUE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property(gobject_class,
PROP_VALIDATE_CERTIFICATE,
obj_properties[PROP_VALIDATE_CERTIFICATE]);
2015-12-11 13:15:42 +00:00
/**
* MatrixHTTPAPI:base-url:
2015-12-11 13:15:42 +00:00
*
* The base URL to use for communication with the Matrix.org
* server. If the URL doesnt end with the correct API endpoint
* (/_matrix/client/api/v1), it gets appended automatically.
*
* Changing the base URL automatically clears all authorization
* tokens.
2015-12-11 13:15:42 +00:00
*/
obj_properties[PROP_BASE_URL] = g_param_spec_string(
"base-url", "Server's base URL",
2015-12-11 13:15:42 +00:00
"Matrix.org home server to connect to.",
NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property(gobject_class,
PROP_BASE_URL,
obj_properties[PROP_BASE_URL]);
2015-12-11 13:15:42 +00:00
g_object_class_override_property(gobject_class, PROP_TOKEN, "token");
2016-01-04 16:37:50 +00:00
g_object_class_override_property(gobject_class,
PROP_REFRESH_TOKEN,
"refresh-token");
2016-01-04 16:39:48 +00:00
g_object_class_override_property(gobject_class, PROP_USER_ID, "user-id");
2016-01-06 13:10:35 +00:00
g_object_class_override_property(gobject_class,
PROP_HOMESERVER,
"homeserver");
2015-12-11 13:15:42 +00:00
}
static void
matrix_http_api_init(MatrixHTTPAPI *api)
{
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(api);
2015-12-15 16:37:06 +00:00
priv->uri = NULL;
2015-12-11 13:15:42 +00:00
priv->token = NULL;
2016-01-04 16:37:50 +00:00
priv->refresh_token = NULL;
2016-01-04 16:39:48 +00:00
priv->user_id = NULL;
2016-01-06 13:10:35 +00:00
priv->homeserver = NULL;
2015-12-11 13:15:42 +00:00
priv->validate_certificate = TRUE;
2015-12-14 15:27:47 +00:00
priv->soup_session = soup_session_new();
2015-12-11 13:15:42 +00:00
}
/**
* matrix_http_api_new:
* @base_url: the URL to use as the API endpoint
* @token: (allow-none): an authorization token to use. If %NULL,
* requests that need authentication will fail
*
* Create a new #MatrixHTTPAPI object with the specified base URL, and
* an optional authorization token
*/
2015-12-11 13:15:42 +00:00
MatrixHTTPAPI *
matrix_http_api_new(const gchar *base_url, const gchar *token)
{
return g_object_new(MATRIX_TYPE_HTTP_API,
"base-url", base_url,
"token", token,
NULL);
}
static void
i_set_token(MatrixAPI *api, const gchar *token)
{
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(
MATRIX_HTTP_API(api));
g_free(priv->token);
priv->token = g_strdup(token);
}
static const gchar *
i_get_token(MatrixAPI *api)
{
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(
MATRIX_HTTP_API(api));
return priv->token;
}
2016-01-04 16:37:50 +00:00
static void
i_set_refresh_token(MatrixAPI *api, const gchar *refresh_token)
{
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(
MATRIX_HTTP_API(api));
g_free(priv->refresh_token);
priv->refresh_token = g_strdup(refresh_token);
2016-01-04 16:37:50 +00:00
}
static const gchar *
i_get_refresh_token(MatrixAPI *api)
{
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(
MATRIX_HTTP_API(api));
return priv->refresh_token;
}
2016-01-04 16:39:48 +00:00
static const gchar *
i_get_user_id(MatrixAPI *api)
{
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(
MATRIX_HTTP_API(api));
return priv->user_id;
}
2016-01-06 13:10:35 +00:00
static const gchar *
i_get_homeserver(MatrixAPI *api)
{
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(
MATRIX_HTTP_API(api));
return priv->homeserver;
}
2015-12-11 13:15:42 +00:00
/**
* matrix_http_api_set_validate_certificate:
* @api: a #MatrixHTTPAPI implementation
* @validate_certificate: %TRUE if server certificates should be
* validated
*
* Sets if server certificates should be validated.
*/
void
matrix_http_api_set_validate_certificate(MatrixHTTPAPI *api,
gboolean validate_certificate)
{
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(api);
priv->validate_certificate = validate_certificate;
g_object_set(priv->soup_session, "ssl-strict", validate_certificate, NULL);
2015-12-11 13:15:42 +00:00
}
/**
* matrix_http_api_get_validate_certificate:
* @api: a #MatrixHTTPAPI implementation
*
* Gets the value set by matrix_http_api_set_validate_certificate()
*
* Returns: %TRUE if the server certificates should be validated
*/
gboolean
matrix_http_api_get_validate_certificate(MatrixHTTPAPI *api)
{
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(api);
return priv->validate_certificate;
}
2015-12-14 15:23:00 +00:00
static gchar *
enum_to_string(GType enum_type, gint value, gboolean convert_dashes)
{
GEnumClass *enum_class = g_type_class_ref(enum_type);
GEnumValue *enum_value = g_enum_get_value(enum_class, value);
gchar *nick = NULL;
if (value) {
nick = g_strdup(enum_value->value_nick);
if (convert_dashes) {
gchar *a;
for (a = nick; *a; a++) {
if (*a == '-') {
*a = '_';
}
}
}
}
g_type_class_unref(enum_class);
return nick;
}
2015-12-14 15:27:47 +00:00
static void
_response_callback(SoupSession *session,
SoupMessage *msg,
MatrixHTTPAPIRequest *request)
2015-12-14 15:27:47 +00:00
{
MatrixHTTPAPI *api = request->api;
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(api);
GError *err = NULL;
JsonNode *content = NULL;
2016-01-15 12:06:23 +00:00
GByteArray *raw_content = NULL;
2015-12-14 15:27:47 +00:00
if (msg->status_code < SOUP_STATUS_CONTINUE) {
err = g_error_new(MATRIX_API_ERROR,
MATRIX_API_ERROR_COMMUNICATION_ERROR,
"%s %d: %s",
(msg->status_code < 100) ? "Network error" : "HTTP",
msg->status_code, msg->reason_phrase);
} else { // No error
2015-12-14 15:27:47 +00:00
SoupBuffer *buffer;
const guint8 *data;
gsize datalen;
JsonParser *parser;
2016-01-15 12:06:23 +00:00
SoupURI *request_uri = soup_message_get_uri(msg);
2016-01-15 12:13:23 +00:00
const gchar *request_url;
switch (request->call_type) {
case CALL_API:
request_url = soup_uri_get_path(request_uri)
+ strlen(API_ENDPOINT);
break;
case CALL_MEDIA:
request_url = soup_uri_get_path(request_uri)
+ strlen(MEDIA_ENDPOINT);
break;
}
2015-12-14 15:27:47 +00:00
buffer = soup_message_body_flatten(msg->response_body);
soup_buffer_get_data(buffer, &data, &datalen);
parser = json_parser_new();
if (json_parser_load_from_data(parser,
(const gchar *)data, datalen,
&err)) {
2016-01-15 12:06:23 +00:00
g_debug("Data (%s): %s", request_url, data);
2015-12-14 15:27:47 +00:00
content = json_parser_get_root(parser);
if (JSON_NODE_HOLDS_OBJECT(content)) {
JsonObject *root_object;
JsonNode *node;
GError *err = NULL;
2015-12-14 15:27:47 +00:00
root_object = json_node_get_object(content);
/* Check if the response holds an access token; if it
* does, set it as our new token */
if ((node = json_object_get_member(
root_object, "access_token")) != NULL) {
const gchar *access_token;
if ((access_token = json_node_get_string(node)) != NULL) {
g_debug("Got new access token: %s", access_token);
i_set_token(MATRIX_API(api), access_token);
2015-12-14 15:27:47 +00:00
}
}
2016-01-04 16:37:50 +00:00
/* Check if the response holds a refresh token; if it
* does, set it as our new refresh token */
if ((node = json_object_get_member(
root_object, "refresh_token")) != NULL) {
const gchar *refresh_token;
if ((refresh_token = json_node_get_string(node)) != NULL) {
g_debug("Got new refresh token: %s", refresh_token);
i_set_refresh_token(MATRIX_API(api), refresh_token);
2016-01-04 16:37:50 +00:00
}
}
2015-12-14 15:27:47 +00:00
/* Check if the response holds a homeserver name */
if ((node = json_object_get_member(
root_object, "home_server")) != NULL) {
const gchar *homeserver = json_node_get_string(node);
2016-01-06 13:10:35 +00:00
g_free(priv->homeserver);
priv->user_id = g_strdup(homeserver);
2015-12-14 15:27:47 +00:00
g_debug("Our home server calls itself %s", homeserver);
}
2016-01-04 16:39:48 +00:00
/* Check if the response holds a user ID; if it does,
* set this as our user ID */
if ((node = json_object_get_member(
root_object, "user_id")) != NULL) {
const gchar *user_id = json_node_get_string(node);
g_free(priv->user_id);
priv->user_id = g_strdup(user_id);
g_debug("We are reported to be logged in as %s", user_id);
}
{ // Check if the response holds an error
JsonNode *errcode_node = json_object_get_member(root_object,
"errcode");
JsonNode *error_node = json_object_get_member(root_object,
"error");
if (errcode_node || error_node) {
gchar *message;
const gchar *errcode = NULL;
const gchar *error = NULL;
MatrixAPIError error_code = MATRIX_API_ERROR_UNKNOWN_ERROR;
if (errcode_node) {
GEnumClass *error_class;
GEnumValue *value;
errcode = json_node_get_string(errcode_node);
if (strncmp("M_", errcode, 2) == 0) {
gchar *matrix_error_code = g_strdup_printf(
"MATRIX_API_ERROR_%s", errcode + 2);
error_class = g_type_class_ref(
MATRIX_TYPE_API_ERROR);
value = g_enum_get_value_by_name(
error_class, matrix_error_code);
g_free(matrix_error_code);
g_type_class_unref(error_class);
if (value) {
error_code = value->value;
} else {
g_info("An unknown error code '%s' was sent by the homeserver. You may want to report it to the %s developers", errcode, PACKAGE_NAME);
}
}
} else {
g_info("An error was sent by the homeserver, but no error code was specified. You may want to report this to the homeserver administrators.");
error_code = MATRIX_API_ERROR_UNSPECIFIED;
}
if (error_node) {
error = json_node_get_string(error_node);
}
if (errcode_node && error_node) {
message = g_strdup_printf("%s: %s", errcode, error);
} else if (errcode_node) {
message = g_strdup(errcode);
} else {
message = g_strdup_printf(
"(No errcode given) %s", error);
}
err = g_error_new_literal(MATRIX_API_ERROR,
error_code,
message);
}
2015-12-14 15:27:47 +00:00
}
2016-01-15 09:37:10 +00:00
} else if (!JSON_NODE_HOLDS_ARRAY(content)) {
// Not a JSON object, neither an array
err = g_error_new(MATRIX_API_ERROR,
MATRIX_API_ERROR_BAD_RESPONSE,
"Bad response (not a JSON object)");
g_debug("Bad response: %s", data);
2015-12-14 15:27:47 +00:00
}
} else { // Invalid JSON
2016-01-15 12:06:23 +00:00
if (request->accept_non_json) {
raw_content = g_byte_array_sized_new(datalen);
g_byte_array_append(raw_content, data, datalen);
g_debug("Binary data (%s): %" G_GSIZE_FORMAT " bytes",
request_url,
datalen);
} else {
err = g_error_new(MATRIX_API_ERROR,
MATRIX_API_ERROR_BAD_RESPONSE,
"Malformed response (invalid JSON)");
g_debug("Malformed response (%s): %s", request_url, data);
}
2015-12-14 15:27:47 +00:00
}
}
/* Call the assigned function, if any */
if (request->callback) {
request->callback(
MATRIX_API(api),
2016-01-15 12:06:23 +00:00
soup_message_headers_get_content_type(
msg->response_headers,
NULL),
content,
2016-01-15 12:06:23 +00:00
raw_content,
request->callback_data,
err);
}
g_clear_error(&err);
2015-12-14 15:27:47 +00:00
}
/**
* matrix_http_api_get_base_url:
* @api: a #MatrixHTTPAPI implementation
*
* Get the base URL set for @api.
*
* Returns: (transfer full): the base URL set for @api
*/
const gchar *
matrix_http_api_get_base_url(MatrixHTTPAPI *api)
{
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(api);
return soup_uri_to_string(priv->uri, FALSE);
}
2016-01-12 13:09:25 +00:00
#define create_query_params() (g_hash_table_new_full(g_str_hash, \
(GEqualFunc)g_strcmp0, \
NULL, \
g_free))
2015-12-14 15:27:47 +00:00
static void
_send(MatrixHTTPAPI *api,
MatrixAPICallback callback,
gpointer user_data,
2016-01-15 12:13:23 +00:00
CallType call_type,
const gchar *method,
const gchar *path,
2016-01-12 13:09:25 +00:00
GHashTable *params,
2016-01-15 12:33:27 +00:00
const gchar *content_type,
JsonNode *json_content,
GByteArray *raw_content,
2016-01-15 12:06:23 +00:00
gboolean accept_non_json,
GError **error)
2015-12-14 15:27:47 +00:00
{
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(api);
SoupURI *request_path;
SoupMessage *message;
2016-01-12 13:09:25 +00:00
gchar *data, *url;
gsize datalen;
MatrixHTTPAPIRequest *request;
if (!priv->uri) {
g_set_error(error,
MATRIX_API_ERROR, MATRIX_API_ERROR_COMMUNICATION_ERROR,
"No valid base URL");
return;
}
2016-01-15 12:33:27 +00:00
if (json_content && raw_content) {
g_critical("Too many parameters for MatrixHTTPAPI._send. This is a bug");
}
if (raw_content && !content_type) {
g_critical("Raw content needs content_type to be set. This is a bug");
}
2015-12-14 15:27:47 +00:00
if (!g_str_is_ascii(method)) {
g_warning("Method must be ASCII encoded!");
return;
}
if ((g_ascii_strcasecmp("GET", method) != 0)
&& (g_ascii_strcasecmp("POST", method) != 0)
2015-12-14 15:27:47 +00:00
&& (g_ascii_strcasecmp("PUT", method) != 0)
&& (g_ascii_strcasecmp("DELETE", method) != 0)) {
g_warning("Invalid method name '%s'", method);
return;
}
2016-01-15 12:13:23 +00:00
switch (call_type) {
case CALL_API:
request_path = soup_uri_new_with_base(priv->uri, path);
break;
case CALL_MEDIA:
request_path = soup_uri_new_with_base(priv->media_uri, path);
break;
}
2016-01-12 13:09:25 +00:00
if (!params) {
params = create_query_params();
}
if (priv->token) {
g_debug("Adding access token '%s'", priv->token);
g_hash_table_replace(params, "access_token", g_strdup(priv->token));
}
soup_uri_set_query_from_form(request_path, params);
g_hash_table_unref(params);
message = soup_message_new_from_uri(method, request_path);
2016-01-12 13:09:25 +00:00
url = soup_uri_to_string(request_path, FALSE);
soup_uri_free(request_path);
2016-01-15 12:33:27 +00:00
if (json_content) {
JsonGenerator *generator;
generator = json_generator_new();
2016-01-15 12:33:27 +00:00
json_generator_set_root(generator, (JsonNode *)json_content);
data = json_generator_to_data(generator, &datalen);
2016-01-15 12:33:27 +00:00
} else if (raw_content) {
data = raw_content->data;
datalen = raw_content->len;
} else {
data = g_strdup("{}");
datalen = 2;
}
2016-01-12 13:09:46 +00:00
g_debug("Sending (%s %s): %s", method, url, data);
soup_message_set_flags(message, SOUP_MESSAGE_NO_REDIRECT);
soup_message_set_request(message,
2016-01-15 12:33:27 +00:00
(content_type == NULL)
? "application/json"
: content_type,
raw_content ? SOUP_MEMORY_COPY : SOUP_MEMORY_TAKE,
data, datalen);
g_object_ref(message);
request = g_new0(MatrixHTTPAPIRequest, 1);
2016-01-15 12:33:27 +00:00
request->request_content = json_content;
request->raw_content = raw_content;
request->api = api;
request->callback = callback;
request->callback_data = user_data;
2016-01-15 12:06:23 +00:00
request->accept_non_json = accept_non_json;
2016-01-15 12:13:23 +00:00
request->call_type = call_type;
soup_session_queue_message(priv->soup_session,
message,
(SoupSessionCallback)_response_callback,
request);
2015-12-14 15:27:47 +00:00
}
2016-01-11 15:28:58 +00:00
static void
i_login(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *type,
const JsonNode *content,
GError **error)
{
JsonNode *body;
JsonObject *root_object;
2015-12-15 10:09:19 +00:00
body = json_node_copy((JsonNode *)content);
root_object = json_node_get_object(body);
json_object_set_string_member(root_object, "type", type);
2015-12-15 10:09:19 +00:00
_send(MATRIX_HTTP_API(api),
callback, user_data,
2016-01-15 12:13:23 +00:00
CALL_API,
2016-01-15 12:33:27 +00:00
"POST", "login", NULL, NULL, body, NULL,
2016-01-15 12:06:23 +00:00
FALSE, error);
}
2016-01-11 15:28:58 +00:00
static void
add_state_event(MatrixAPIStateEvent *event, JsonBuilder *builder)
{
JsonNode *node = matrix_api_state_event_get_json_node(event);
json_builder_add_value(builder, node);
json_node_free(node);
}
static void
add_string(gchar *str, JsonBuilder *builder)
{
json_builder_add_string_value(builder, str);
}
static void
i_create_room(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
MatrixAPIRoomPreset preset,
const gchar *room_name,
const gchar *room_alias,
const gchar *topic,
MatrixAPIRoomVisibility visibility,
JsonNode *creation_content,
GList *initial_state,
GList *invitees,
GError **error)
{
JsonNode *body;
JsonObject *root_object;
JsonBuilder *builder;
builder = json_builder_new();
json_builder_begin_object(builder);
if (creation_content) {
json_builder_set_member_name(builder, "creation_content");
json_builder_add_value(builder, creation_content);
}
if (initial_state) {
json_builder_set_member_name(builder, "initial_state");
json_builder_begin_array(builder);
g_list_foreach(initial_state, (GFunc)add_state_event, builder);
json_builder_end_array(builder);
}
if (invitees) {
json_builder_set_member_name(builder, "invite");
json_builder_begin_array(builder);
g_list_foreach(invitees, (GFunc)add_string, builder);
json_builder_end_array(builder);
}
if (room_name) {
json_builder_set_member_name(builder, "name");
json_builder_add_string_value(builder, room_name);
}
if (preset != MATRIX_API_ROOM_PRESET_NONE) {
gchar *preset_string = enum_to_string(
MATRIX_TYPE_API_ROOM_PRESET, preset, TRUE);
2016-01-11 15:28:58 +00:00
if (preset_string) {
2016-01-11 15:28:58 +00:00
json_builder_set_member_name(builder, "preset");
json_builder_add_string_value(builder, preset_string);
g_free(preset_string);
2016-01-11 15:28:58 +00:00
} else {
g_debug("Invalid room preset type");
}
}
if (room_alias) {
json_builder_set_member_name(builder, "room_alias_name");
json_builder_add_string_value(builder, room_alias);
}
if (topic) {
json_builder_set_member_name(builder, "topic");
json_builder_add_string_value(builder, topic);
}
if (visibility != MATRIX_API_ROOM_VISIBILITY_DEFAULT) {
gchar *visibility_string = enum_to_string(
MATRIX_TYPE_API_ROOM_VISIBILITY, visibility, TRUE);
2016-01-11 15:28:58 +00:00
if (visibility_string) {
2016-01-11 15:28:58 +00:00
json_builder_set_member_name(builder, "visibility");
json_builder_add_string_value(builder, visibility_string);
g_free(visibility_string);
2016-01-11 15:28:58 +00:00
} else {
g_debug("Invalid room visibility type");
}
}
json_builder_end_object(builder);
body = json_builder_get_root(builder);
g_object_unref(builder);
_send(MATRIX_HTTP_API(api),
callback, user_data,
2016-01-15 12:13:23 +00:00
CALL_API,
2016-01-15 12:33:27 +00:00
"POST", "createRoom", NULL, NULL, body, NULL,
2016-01-15 12:06:23 +00:00
FALSE, error);
2016-01-11 15:28:58 +00:00
}
2016-01-12 15:39:31 +00:00
static void
i_initial_sync(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
guint limit,
gboolean archived,
GError **err)
{
GHashTable *params;
params = create_query_params();
if (limit != 0) {
g_hash_table_replace(params, "limit", g_strdup_printf("%d", limit));
}
if (archived) {
g_hash_table_replace(params, "archived", g_strdup("true"));
}
_send(MATRIX_HTTP_API(api),
callback, user_data,
2016-01-15 12:13:23 +00:00
CALL_API,
2016-01-15 12:33:27 +00:00
"GET", "initialSync", params, NULL, NULL, NULL,
2016-01-15 12:06:23 +00:00
FALSE, err);
2016-01-12 15:39:31 +00:00
}
2016-01-12 16:10:02 +00:00
static void
i_event_stream(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *from_token,
gulong timeout,
GError **err)
{
GHashTable *params;
params = create_query_params();
if (from_token) {
g_hash_table_replace(params, "from", g_strdup(from_token));
}
if (timeout != 0) {
g_hash_table_replace(params,
"timeout", g_strdup_printf("%lu", timeout));
2016-01-12 16:10:02 +00:00
}
_send(MATRIX_HTTP_API(api),
callback, user_data,
2016-01-15 12:13:23 +00:00
CALL_API,
2016-01-15 12:33:27 +00:00
"GET", "events", params, NULL, NULL, NULL,
2016-01-15 12:06:23 +00:00
FALSE, err);
2016-01-12 16:10:02 +00:00
}
2016-01-13 08:55:33 +00:00
static void
i_leave_room(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_id,
GError **error)
{
gchar *encoded_room_id, *path;
encoded_room_id = soup_uri_encode(room_id, NULL);
path = g_strdup_printf("rooms/%s/leave", encoded_room_id);
g_free(encoded_room_id);
_send(MATRIX_HTTP_API(api),
callback, user_data,
2016-01-15 12:13:23 +00:00
CALL_API,
2016-01-15 12:33:27 +00:00
"POST", path, NULL, NULL, NULL, NULL,
2016-01-15 12:06:23 +00:00
FALSE, error);
2016-01-13 08:55:33 +00:00
g_free(path);
}
2016-01-13 09:35:59 +00:00
static void
i_list_public_rooms(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
GError **error)
{
_send(MATRIX_HTTP_API(api),
callback, user_data,
2016-01-15 12:13:23 +00:00
CALL_API,
2016-01-15 12:33:27 +00:00
"GET", "publicRooms", NULL, NULL, NULL, NULL,
2016-01-15 12:06:23 +00:00
FALSE, error);
2016-01-13 09:35:59 +00:00
}
2016-01-13 09:02:00 +00:00
static void
i_join_room(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_id,
GError **error)
{
gchar *encoded_room_id, *path;
// TODO: a more thorough check should be used here
if (*room_id != '!') {
2016-01-15 09:36:47 +00:00
g_set_error(error,
2016-01-13 09:02:00 +00:00
MATRIX_API_ERROR, MATRIX_API_ERROR_INVALID_ROOM_ID,
"Invalid room ID");
return;
}
2016-01-15 09:36:47 +00:00
encoded_room_id = soup_uri_encode(room_id, NULL);
2016-01-13 09:02:00 +00:00
path = g_strdup_printf("rooms/%s/join", encoded_room_id);
g_free(encoded_room_id);
_send(MATRIX_HTTP_API(api),
callback, user_data,
2016-01-15 12:13:23 +00:00
CALL_API,
2016-01-15 12:33:27 +00:00
"POST", path, NULL, NULL, NULL, NULL,
2016-01-15 12:06:23 +00:00
FALSE, error);
2016-01-13 09:02:00 +00:00
g_free(path);
}
2016-01-15 09:37:10 +00:00
static void
i_get_presence_list(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *user_id,
GError **error)
{
gchar *encoded_user_id;
gchar *path;
encoded_user_id = soup_uri_encode(user_id, NULL);
path = g_strdup_printf("presence/list/%s", encoded_user_id);
g_free(encoded_user_id);
_send(MATRIX_HTTP_API(api),
callback, user_data,
2016-01-15 12:13:23 +00:00
CALL_API,
2016-01-15 12:33:27 +00:00
"GET", path, NULL, NULL, NULL, NULL,
2016-01-15 12:06:23 +00:00
FALSE, error);
2016-01-15 09:37:10 +00:00
g_free(path);
}
2016-01-15 11:45:32 +00:00
static void
i_get_user_presence(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *user_id,
GError **error)
{
gchar *encoded_user_id;
gchar *path;
encoded_user_id = soup_uri_encode(user_id, NULL);
path = g_strdup_printf("presence/%s/status", encoded_user_id);
g_free(encoded_user_id);
_send(MATRIX_HTTP_API(api),
callback, user_data,
2016-01-15 12:13:38 +00:00
CALL_API,
2016-01-15 12:33:27 +00:00
"GET", path, NULL, NULL, NULL, NULL,
2016-01-15 12:06:23 +00:00
FALSE, error);
2016-01-15 11:45:32 +00:00
g_free(path);
}
2016-01-15 12:13:38 +00:00
static void
i_media_download(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *server_name,
const gchar *media_id,
GError **error)
{
gchar *encoded_server_name, *encoded_media_id, *path;
encoded_server_name = soup_uri_encode(server_name, NULL);
encoded_media_id = soup_uri_encode(media_id, NULL);
path = g_strdup_printf("download/%s/%s",
encoded_server_name,
encoded_media_id);
g_free(encoded_server_name);
g_free(encoded_media_id);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_MEDIA,
2016-01-15 12:33:27 +00:00
"GET", path, NULL, NULL, NULL, NULL,
2016-01-15 12:13:38 +00:00
TRUE, error);
g_free(path);
}
2016-01-15 12:26:59 +00:00
static void
i_media_thumbnail(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *server_name,
const gchar *media_id,
guint width,
guint height,
MatrixAPIResizeMethod method,
GError **error)
{
gchar *encoded_server_name,
*encoded_media_id,
*path;
GHashTable *params;
encoded_server_name = soup_uri_encode(server_name, NULL);
encoded_media_id = soup_uri_encode(media_id, NULL);
path = g_strdup_printf("download/%s/%s",
encoded_server_name,
encoded_media_id);
g_free(encoded_server_name);
g_free(encoded_media_id);
params = create_query_params();
if (width > 0) {
g_hash_table_replace(params, "width", g_strdup_printf("%u", width));
}
if (height > 0) {
g_hash_table_replace(params, "height", g_strdup_printf("%u", height));
}
if (method != MATRIX_API_RESIZE_METHOD_DEFAULT) {
switch (method) {
case MATRIX_API_RESIZE_METHOD_CROP:
g_hash_table_replace(params, "method", g_strdup("crop"));
break;
case MATRIX_API_RESIZE_METHOD_SCALE:
g_hash_table_replace(params, "method", g_strdup("scale"));
break;
}
}
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_MEDIA,
2016-01-15 12:33:27 +00:00
"GET", path, params, NULL, NULL, NULL,
2016-01-15 12:26:59 +00:00
TRUE, error);
g_free(path);
}
2016-01-15 12:57:53 +00:00
static void
i_media_upload(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *content_type,
GByteArray *content,
GError **error)
{
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_MEDIA,
"POST", "upload", NULL, content_type, NULL, content,
FALSE, error);
}
2016-01-15 13:04:50 +00:00
static void
i_update_presence_list(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *user_id,
GList *drop_ids,
GList *invite_ids,
GError **error)
{
gchar *encoded_user_id;
gchar *path;
JsonBuilder *builder;
JsonNode *body;
encoded_user_id = soup_uri_encode(user_id, NULL);
path = g_strdup_printf("presence/%s/status", encoded_user_id);
g_free(encoded_user_id);
builder = json_builder_new();
json_builder_begin_object(builder);
if (drop_ids) {
json_builder_set_member_name(builder, "drop");
json_builder_begin_array(builder);
g_list_foreach(drop_ids, (GFunc)add_string, builder);
json_builder_end_array(builder);
}
if (invite_ids) {
json_builder_set_member_name(builder, "invide");
json_builder_begin_array(builder);
g_list_foreach(invite_ids, (GFunc)add_string, builder);
json_builder_end_array(builder);
}
json_builder_end_object(builder);
body = json_builder_get_root(builder);
g_object_unref(builder);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"POST", path, NULL, NULL, body, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 13:31:45 +00:00
static void
i_set_user_presence(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *user_id,
MatrixAPIPresence presence,
const gchar *status_message,
GError **error)
{
gchar *encoded_user_id;
gchar *path, *presence_string, *a;
JsonBuilder *builder;
JsonNode *body;
GEnumClass *presence_class;
GEnumValue *value;
encoded_user_id = soup_uri_encode(user_id, NULL);
path = g_strdup_printf("presence/%s/status", encoded_user_id);
g_free(encoded_user_id);
builder = json_builder_new();
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "presence");
presence_string = enum_to_string(MATRIX_TYPE_API_PRESENCE, presence, TRUE);
2016-01-15 13:31:45 +00:00
json_builder_add_string_value(builder, presence_string);
g_free(presence_string);
if (status_message) {
json_builder_set_member_name(builder, "status_msg");
json_builder_add_string_value(builder, status_message);
}
json_builder_end_object(builder);
body = json_builder_get_root(builder);
g_object_unref(builder);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"POST", path, NULL, NULL, body, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 13:43:58 +00:00
static void
i_update_pusher(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
MatrixAPIPusher *pusher,
GError **error)
{
JsonNode *pusher_node;
if ((pusher_node = matrix_api_pusher_get_json_node(
pusher, error)) == NULL) {
return;
}
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"POST", "pushers/set", NULL, NULL, pusher_node, NULL,
FALSE, error);
}
2016-01-15 13:49:39 +00:00
static void
i_get_pushers(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
GError **error)
{
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"GET", "pushrules/", NULL, NULL, NULL, NULL,
FALSE, error);
}
2016-01-15 14:10:11 +00:00
static void
i_delete_pusher(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *scope,
MatrixAPIPusherKind kind,
const gchar *rule_id,
GError **error)
{
gchar *encoded_scope, *encoded_rule_id, *kind_string, *path;
encoded_scope = soup_uri_encode(scope, NULL);
encoded_rule_id = soup_uri_encode(rule_id, NULL);
kind_string = enum_to_string(MATRIX_TYPE_API_PUSHER_KIND, kind, TRUE);
path = g_strdup_printf("pushrules/%s/%s/%s",
encoded_scope,
kind_string,
encoded_rule_id);
g_free(encoded_scope);
g_free(encoded_rule_id);
g_free(kind_string);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"DELETE", path, NULL, NULL, NULL, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 14:12:31 +00:00
static void
i_get_pusher(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *scope,
MatrixAPIPusherKind kind,
const gchar *rule_id,
GError **error)
{
gchar *encoded_scope, *encoded_rule_id, *kind_string, *path;
encoded_scope = soup_uri_encode(scope, NULL);
encoded_rule_id = soup_uri_encode(rule_id, NULL);
kind_string = enum_to_string(MATRIX_TYPE_API_PUSHER_KIND, kind, TRUE);
path = g_strdup_printf("pushrules/%s/%s/%s",
encoded_scope,
kind_string,
encoded_rule_id);
g_free(encoded_scope);
g_free(encoded_rule_id);
g_free(kind_string);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"GET", path, NULL, NULL, NULL, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 14:29:57 +00:00
static void
add_condition_kind_object(MatrixAPIPusherConditionKind kind,
JsonBuilder *builder)
{
gchar *kind_string = enum_to_string(
MATRIX_TYPE_API_PUSHER_CONDITION_KIND, kind, TRUE);
if (!kind_string) {
g_warning("Invalid condition kind");
return;
}
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "kind");
json_builder_add_string_value(builder, kind_string);
json_builder_end_object(builder);
g_free(kind_string);
}
static void i_add_pusher(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *scope,
MatrixAPIPusherKind kind,
const gchar *rule_id,
const gchar *before,
const gchar *after,
GList *actions,
GList *conditions,
GError **error)
{
gchar *encoded_scope, *encoded_rule_id, *kind_string, *path;
GHashTable *params;
JsonBuilder *builder;
JsonNode *body;
encoded_scope = soup_uri_encode(scope, NULL);
encoded_rule_id = soup_uri_encode(rule_id, NULL);
kind_string = enum_to_string(MATRIX_TYPE_API_PUSHER_KIND, kind, TRUE);
path = g_strdup_printf("pushrules/%s/%s/%s",
encoded_scope,
kind_string,
encoded_rule_id);
g_free(encoded_scope);
g_free(encoded_rule_id);
g_free(kind_string);
params = create_query_params();
if (before) {
g_hash_table_replace(params, "before", g_strdup(before));
}
if (after) {
g_hash_table_replace(params, "after", g_strdup(after));
}
builder = json_builder_new();
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "actions");
json_builder_begin_array(builder);
g_list_foreach(actions, (GFunc)add_string, builder);
json_builder_end_array(builder);
if (conditions) {
json_builder_set_member_name(builder, "conditions");
json_builder_begin_array(builder);
g_list_foreach(conditions, (GFunc)add_condition_kind_object, builder);
json_builder_end_array(builder);
}
json_builder_end_object(builder);
body = json_builder_get_root(builder);
g_object_unref(builder);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"GET", path, params, NULL, body, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 14:33:50 +00:00
static void
i_toggle_pusher(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *scope,
MatrixAPIPusherKind kind,
const gchar *rule_id,
gboolean enabled,
GError **error)
{
gchar *encoded_scope, *encoded_rule_id, *kind_string, *path;
JsonBuilder *builder;
JsonNode *body;
encoded_scope = soup_uri_encode(scope, NULL);
encoded_rule_id = soup_uri_encode(rule_id, NULL);
kind_string = enum_to_string(MATRIX_TYPE_API_PUSHER_KIND, kind, TRUE);
path = g_strdup_printf("pushrules/%s/%s/%s",
encoded_scope,
kind_string,
encoded_rule_id);
g_free(encoded_scope);
g_free(encoded_rule_id);
g_free(kind_string);
builder = json_builder_new();
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "enabled");
json_builder_add_boolean_value(builder, enabled);
json_builder_end_object(builder);
body = json_builder_get_root(builder);
g_object_unref(builder);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"GET", path, NULL, NULL, body, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 14:37:36 +00:00
static void
i_delete_room_alias(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_alias,
GError **error)
{
gchar *encoded_room_alias, *path;
encoded_room_alias = soup_uri_encode(room_alias, NULL);
path = g_strdup_printf("room/%s", encoded_room_alias);
g_free(encoded_room_alias);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"DELETE", path, NULL, NULL, NULL, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 14:40:33 +00:00
static void
i_get_room_id(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_alias,
GError **error)
{
gchar *encoded_room_alias, *path;
encoded_room_alias = soup_uri_encode(room_alias, NULL);
path = g_strdup_printf("room/%s", encoded_room_alias);
g_free(encoded_room_alias);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"GET", path, NULL, NULL, NULL, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 14:45:41 +00:00
static void
i_create_room_alias(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_id,
const gchar *room_alias,
GError **error)
{
gchar *encoded_room_alias, *path;
JsonBuilder *builder;
JsonNode *body;
encoded_room_alias = soup_uri_encode(room_alias, NULL);
path = g_strdup_printf("room/%s", encoded_room_alias);
g_free(encoded_room_alias);
builder = json_builder_new();
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "room_id");
json_builder_add_string_value(builder, room_id);
json_builder_end_object(builder);
body = json_builder_get_root(builder);
g_object_unref(builder);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"PUT", path, NULL, NULL, body, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 14:53:11 +00:00
static void
i_ban_user(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_id,
const gchar *user_id,
const gchar *reason,
GError **error)
{
gchar *encoded_room_id, *path;
JsonBuilder *builder;
JsonNode *body;
encoded_room_id = soup_uri_encode(room_id, NULL);
path = g_strdup_printf("rooms/%s/ban", encoded_room_id);
g_free(encoded_room_id);
builder = json_builder_new();
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "user_id");
json_builder_add_string_value(builder, user_id);
if (reason) {
json_builder_set_member_name(builder, "reason");
json_builder_add_string_value(builder, reason);
}
json_builder_end_object(builder);
body = json_builder_get_root(builder);
g_object_unref(builder);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"POST", path, NULL, NULL, body, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 14:56:51 +00:00
static void
i_forget_room(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_id,
GError **error)
{
gchar *encoded_room_id, *path;
encoded_room_id = soup_uri_encode(room_id, NULL);
path = g_strdup_printf("rooms/%s/forget", encoded_room_id);
g_free(encoded_room_id);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"POST", path, NULL, NULL, NULL, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 15:03:33 +00:00
static void
i_invite_user_3rdparty(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_id,
const gchar *address,
const gchar *medium,
const gchar *id_server,
GError **error)
{
gchar *encoded_room_id, *path;
JsonBuilder *builder;
JsonNode *body;
encoded_room_id = soup_uri_encode(room_id, NULL);
path = g_strdup_printf("rooms/%s/invite", encoded_room_id);
g_free(encoded_room_id);
builder = json_builder_new();
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "id_server");
json_builder_add_string_value(builder, id_server);
json_builder_set_member_name(builder, "medium");
json_builder_add_string_value(builder, medium);
json_builder_set_member_name(builder, "address");
json_builder_add_string_value(builder, address);
json_builder_end_object(builder);
body = json_builder_get_root(builder);
g_object_unref(builder);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"POST", path, NULL, NULL, body, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 15:08:37 +00:00
static void
i_invite_user(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_id,
const gchar *user_id,
GError **error)
{
gchar *encoded_room_id, *path;
JsonBuilder *builder;
JsonNode *body;
encoded_room_id = soup_uri_encode(encoded_room_id, NULL);
path = g_strdup_printf("rooms/%s/invite", encoded_room_id);
g_free(encoded_room_id);
builder = json_builder_new();
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "user_id");
json_builder_add_string_value(builder, user_id);
json_builder_end_object(builder);
body = json_builder_get_root(builder);
g_object_unref(builder);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"POST", path, NULL, NULL, body, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 15:16:50 +00:00
static void
i_get_event(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *event_id,
GError **error)
{
gchar *encoded_event_id, *path;
encoded_event_id = soup_uri_encode(event_id, NULL);
path = g_strdup_printf("events/%s", encoded_event_id);
g_free(encoded_event_id);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"GET", path, NULL, NULL, NULL, NULL,
FALSE, error);
g_free(path);
}
static void
i_get_event_context(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_id,
const gchar *event_id,
guint limit,
GError **error)
{
gchar *encoded_room_id, *encoded_event_id, *path;
GHashTable *params = NULL;
encoded_room_id = soup_uri_encode(room_id, NULL);
encoded_event_id = soup_uri_encode(event_id, NULL);
path = g_strdup_printf("rooms/%s/context/%s",
encoded_room_id, encoded_event_id);
g_free(encoded_room_id);
g_free(encoded_event_id);
if (limit != 0) {
params = create_query_params();
g_hash_table_replace(params, "limit", g_strdup_printf("%u", limit));
}
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"GET", path, params, NULL, NULL, NULL,
FALSE, error);
g_free(params);
}
2016-01-15 15:21:08 +00:00
static void
i_initial_sync_room(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_id,
GError **error)
{
gchar *encoded_room_id, *path;
encoded_room_id = soup_uri_encode(room_id, NULL);
path = g_strdup_printf("rooms/%s/initialSync", encoded_room_id);
g_free(encoded_room_id);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"GET", path, NULL, NULL, NULL, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 15:24:39 +00:00
static void
i_list_room_members(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_id,
GError **error)
{
gchar *encoded_room_id, *path;
encoded_room_id = soup_uri_encode(room_id, NULL);
path = g_strdup_printf("rooms/%s/members", encoded_room_id);
g_free(encoded_room_id);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"GET", path, NULL, NULL, NULL, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 15:31:02 +00:00
static void
i_list_room_messages(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_id,
const gchar *from_token,
MatrixAPIEventDirection direction,
guint limit,
GError **error)
{
gchar *encoded_room_id, *path;
GHashTable *params;
encoded_room_id = soup_uri_encode(room_id, NULL);
path = g_strdup_printf("rooms/%s/messages", encoded_room_id);
g_free(encoded_room_id);
params = create_query_params();
g_hash_table_replace(params, "from", g_strdup(from_token));
switch (direction) {
case MATRIX_API_EVENT_DIRECTION_BACKWARD:
g_hash_table_replace(params, "dir", g_strdup("b"));
break;
case MATRIX_API_EVENT_DIRECTION_FORWARD:
g_hash_table_replace(params, "dir", g_strdup("f"));
break;
}
if (limit != 0) {
g_hash_table_replace(params, "limit", g_strdup_printf("%u", limit));
}
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"GET", path, params, NULL, NULL, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 15:38:52 +00:00
static void
i_send_event_receipt(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_id,
MatrixAPIReceiptType receipt_type,
const gchar *event_id,
JsonNode *receipt,
GError **error)
{
gchar *encoded_room_id, *receipt_type_string, *encoded_event_id, *path;
encoded_room_id = soup_uri_encode(room_id, NULL);
encoded_event_id = soup_uri_encode(event_id, NULL);
receipt_type_string = enum_to_string(MATRIX_TYPE_API_RECEIPT_TYPE,
receipt_type,
TRUE);
path = g_strdup_printf("rooms/%s/receipt/%s/%s",
encoded_room_id,
receipt_type_string,
encoded_event_id);
g_free(encoded_room_id);
g_free(encoded_event_id);
g_free(receipt_type_string);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"POST", path, NULL, NULL, receipt, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 15:45:30 +00:00
static void
i_redact_event(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_id,
const gchar *event_id,
const gchar *txn_id,
const gchar *reason,
GError **error)
{
gchar *encoded_room_id, *encoded_event_id, *encoded_txn_id, *path;
JsonBuilder *builder;
JsonNode *body = NULL;
encoded_room_id = soup_uri_encode(room_id, NULL);
encoded_event_id = soup_uri_encode(event_id, NULL);
encoded_txn_id = soup_uri_encode(txn_id, NULL);
path = g_strdup_printf("rooms/%s/redact/%s/%s",
encoded_room_id,
encoded_event_id,
encoded_txn_id);
g_free(encoded_room_id);
g_free(encoded_event_id);
g_free(encoded_txn_id);
if (reason) {
builder = json_builder_new();
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "reason");
json_builder_add_string_value(builder, reason);
json_builder_end_object(builder);
body = json_builder_get_root(builder);
g_object_unref(builder);
}
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"PUT", path, NULL, NULL, body, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 15:51:24 +00:00
static void
i_send_message_event(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_id,
const gchar *event_type,
const gchar *txn_id,
JsonNode *content,
GError **error)
{
gchar *encoded_room_id, *encoded_event_type, *encoded_txn_id, *path;
encoded_room_id = soup_uri_encode(room_id, NULL);
encoded_event_type = soup_uri_encode(event_type, NULL);
encoded_txn_id = soup_uri_encode(txn_id, NULL);
path = g_strdup_printf("rooms/%s/send/%s/%s",
encoded_room_id,
encoded_event_type,
encoded_txn_id);
g_free(encoded_room_id);
g_free(encoded_event_type);
g_free(encoded_txn_id);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"PUT", path, NULL, NULL, content, NULL,
FALSE, error);
g_free(path);
}
2016-01-15 15:54:58 +00:00
static void
i_get_room_state(MatrixAPI *api,
MatrixAPICallback callback,
gpointer user_data,
const gchar *room_id,
const gchar *event_type,
const gchar *state_key,
GError **error)
{
gchar *encoded_room_id, *path, *encoded_event_type, *encoded_state_key;
encoded_room_id = soup_uri_encode(room_id, NULL);
if (event_type) {
encoded_event_type = soup_uri_encode(event_type, NULL);
if (state_key) {
encoded_state_key = soup_uri_encode(state_key, NULL);
path = g_strdup_printf("rooms/%s/state/%s/%s",
encoded_room_id,
encoded_event_type,
encoded_state_key);
g_free(encoded_state_key);
} else {
path = g_strdup_printf("rooms/%s/state/%s",
encoded_room_id, encoded_event_type);
}
g_free(encoded_event_type);
} else {
path = g_strdup_printf("rooms/%s/state", encoded_room_id);
}
g_free(encoded_room_id);
_send(MATRIX_HTTP_API(api),
callback, user_data,
CALL_API,
"GET", path, NULL, NULL, NULL, NULL,
FALSE, error);
g_free(path);
}
static void
matrix_http_api_matrix_api_init(MatrixAPIInterface *iface)
{
iface->set_token = i_set_token;
iface->get_token = i_get_token;
2016-01-04 16:37:50 +00:00
iface->set_refresh_token = i_set_refresh_token;
iface->get_refresh_token = i_get_refresh_token;
2016-01-04 16:39:48 +00:00
iface->get_user_id = i_get_user_id;
2016-01-06 13:10:35 +00:00
iface->get_homeserver = i_get_homeserver;
/* Media */
2016-01-15 12:13:38 +00:00
iface->media_download = i_media_download;
2016-01-15 12:26:59 +00:00
iface->media_thumbnail = i_media_thumbnail;
2016-01-15 12:57:53 +00:00
iface->media_upload = i_media_upload;
/* Presence */
2016-01-15 09:37:10 +00:00
iface->get_presence_list = i_get_presence_list;
2016-01-15 13:04:50 +00:00
iface->update_presence_list = i_update_presence_list;
2016-01-15 11:45:32 +00:00
iface->get_user_presence = i_get_user_presence;
2016-01-15 13:31:45 +00:00
iface->set_user_presence = i_set_user_presence;
/* Push notifications */
2016-01-15 13:43:58 +00:00
iface->update_pusher = i_update_pusher;
2016-01-15 13:49:39 +00:00
iface->get_pushers = i_get_pushers;
2016-01-15 14:10:11 +00:00
iface->delete_pusher = i_delete_pusher;
2016-01-15 14:12:31 +00:00
iface->get_pusher = i_get_pusher;
2016-01-15 14:29:57 +00:00
iface->add_pusher = i_add_pusher;
2016-01-15 14:33:50 +00:00
iface->toggle_pusher = i_toggle_pusher;
/* Room creation */
2016-01-11 15:28:58 +00:00
iface->create_room = i_create_room;
/* Room directory */
2016-01-15 14:37:36 +00:00
iface->delete_room_alias = i_delete_room_alias;
2016-01-15 14:40:33 +00:00
iface->get_room_id = i_get_room_id;
2016-01-15 14:45:41 +00:00
iface->create_room_alias = i_create_room_alias;
/* Room discovery */
2016-01-13 09:35:59 +00:00
iface->list_public_rooms = i_list_public_rooms;
/* Room membership */
2016-01-15 14:53:11 +00:00
iface->ban_user = i_ban_user;
2016-01-15 14:56:51 +00:00
iface->forget_room = i_forget_room;
2016-01-15 15:03:33 +00:00
iface->invite_user_3rdparty = i_invite_user_3rdparty;
2016-01-15 15:08:37 +00:00
iface->invite_user = i_invite_user;
2016-01-13 09:02:00 +00:00
iface->join_room = i_join_room;
iface->leave_room = i_leave_room;
/* Room participation */
iface->event_stream = i_event_stream;
2016-01-15 15:16:50 +00:00
iface->get_event = i_get_event;
iface->initial_sync = i_initial_sync;
2016-01-15 15:16:50 +00:00
iface->get_event_context = i_get_event_context;
2016-01-15 15:21:08 +00:00
iface->initial_sync_room = i_initial_sync_room;
2016-01-15 15:24:39 +00:00
iface->list_room_members = i_list_room_members;
2016-01-15 15:31:02 +00:00
iface->list_room_messages = i_list_room_messages;
2016-01-15 15:38:52 +00:00
iface->send_event_receipt = i_send_event_receipt;
2016-01-15 15:45:30 +00:00
iface->redact_event = i_redact_event;
2016-01-15 15:51:24 +00:00
iface->send_message_event = i_send_message_event;
2016-01-15 15:54:58 +00:00
iface->get_room_state = i_get_room_state;
iface->send_room_event = NULL;
iface->notify_room_typing = NULL;
iface->sync = NULL;
iface->create_filter = NULL;
iface->download_filter = NULL;
/* Search */
/* Server administration */
iface->whois = NULL;
/* Session management */
iface->login = i_login;
iface->token_refresh = NULL;
/* User data */
iface->get_3pids = NULL;
iface->add_3pid = NULL;
iface->change_password = NULL;
iface->get_profile = NULL;
iface->get_avatar_url = NULL;
iface->set_avatar_url = NULL;
iface->get_display_name = NULL;
iface->set_display_name = NULL;
iface->register_account = NULL;
iface->set_account_data = NULL;
iface->get_room_tags = NULL;
iface->delete_room_tag = NULL;
iface->add_room_tag = NULL;
/* VoIP */
iface->get_turn_server = NULL;
2015-12-15 10:09:19 +00:00
}