Add MatrixClient based test client

This commit is contained in:
Gergely Polonkai 2016-01-22 12:29:10 +01:00
parent 444a93bddf
commit 6356ead2b2
3 changed files with 143 additions and 1 deletions

1
.gitignore vendored
View File

@ -7,6 +7,7 @@ Makefile.in
/src/*.gir
/src/*.typelib
/src/test-api-client
/src/test-client
/aclocal.m4
/ar-lib
/autom4te.cache

View File

@ -79,7 +79,7 @@ $(libmatrix_glib_0_0_la_VALA_SOURCES:.vala=.c): vala-stamp
fi
# Binaries to create
bin_PROGRAMS = test-api-client
bin_PROGRAMS = test-api-client test-client
INST_H_SRC_FILES = \
$(NULL)
@ -138,6 +138,13 @@ test_api_client_LDADD = \
libmatrix-glib-$(MATRIX_GLIB_API_VERSION).la \
$(NULL)
test_client_SOURCES = test-client.c
test_client_CFLAGS = $(libmatrix_glib_0_0_la_CFLAGS) $(AM_CFLAGS)
test_client_LDADD = \
$(libmatrix_glib_0_0_la_LIBADD) \
libmatrix-glib-$(MATRIX_GLIB_API_VERSION).la \
$(NULL)
CLEANFILES += $(BUILT_SOURCES)
EXTRA_DIST += \
matrix-marshalers.list \

134
src/test-client.c Normal file
View File

@ -0,0 +1,134 @@
/*
* 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 <glib.h>
#include <glib/gprintf.h>
#include "matrix-glib.h"
static gchar *user = NULL;
static gchar *password = NULL;
static gboolean no_validate_certs = FALSE;
static gchar **homeserver = NULL;
static GOptionEntry entries[] = {
{"user", 'u', 0, G_OPTION_ARG_STRING, &user},
{"password", 'p', 0, G_OPTION_ARG_STRING, &password},
{"no-validate-certs", 'n', 0, G_OPTION_ARG_NONE, &no_validate_certs},
{G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &homeserver, "The homeserver to connect to", "homeserver"},
};
void
login_finished(MatrixClient *client, gboolean success, GMainLoop *loop)
{
g_printerr("Login %s.\n", (success) ? "succesful" : "failed");
if (success) {
matrix_client_begin_polling(client, NULL);
} else {
g_main_loop_quit(loop);
}
}
static void
cb_presence_event(MatrixClient *client,
const gchar *room_id,
JsonNode *raw_event,
MatrixEvent *event,
gpointer user_data)
{
g_printerr("Incoming presence event from %s!\n",
matrix_event_get_sender(event));
}
static void
cb_room_member_event(MatrixClient *client,
const gchar *room_id,
JsonNode *raw_event,
MatrixEvent *event,
gpointer user_data)
{
g_printerr("Incoming room member event from %s in room %s (%s)\n",
matrix_event_get_sender(event),
matrix_event_get_room_id(MATRIX_EVENT(event)),
room_id);
}
static void
cb_room_message_event(MatrixClient *client,
const gchar *room_id,
JsonNode *raw_event,
MatrixEvent *event,
gpointer user_data)
{
g_printf("Message from %s: %s\n",
matrix_event_get_sender(event),
matrix_room_message_event_get_body(
MATRIX_ROOM_MESSAGE_EVENT(event)));
}
int
main(int argc, char *argv[])
{
GError *err = NULL;
GOptionContext *opts;
MatrixClient *client;
GMainLoop *loop;
opts = g_option_context_new(NULL);
g_option_context_add_main_entries(opts, entries, NULL);
if (!g_option_context_parse(opts, &argc, &argv, &err)) {
g_printerr("Could not parse arguments: %s\n", err->message);
g_printerr("%s", g_option_context_get_help(opts, TRUE, NULL));
return 1;
}
if ((argc != 1) || !homeserver) {
g_printerr("%s", g_option_context_get_help(opts, TRUE, NULL));
return 1;
}
g_option_context_free(opts);
g_info("Starting up: %s with %s:%s", *homeserver, user, password);
loop = g_main_loop_new(NULL, FALSE);
client = MATRIX_CLIENT(matrix_http_client_new(*homeserver));
matrix_http_api_set_validate_certificate(MATRIX_HTTP_API(client),
!no_validate_certs);
g_signal_connect(G_OBJECT(client),
"login-finished",
G_CALLBACK(login_finished),
loop);
matrix_client_connect_event(client, MATRIX_TYPE_PRESENCE_EVENT,
cb_presence_event, NULL, NULL);
matrix_client_connect_event(client, MATRIX_TYPE_ROOM_MEMBER_EVENT,
cb_room_member_event, NULL, NULL);
matrix_client_connect_event(client, MATRIX_TYPE_ROOM_MESSAGE_EVENT,
cb_room_message_event, NULL, NULL);
matrix_client_login_with_password(client, user, password, NULL);
g_main_loop_run(loop);
return 0;
}