From 6356ead2b2c01cebeb99f04759138f4dc6021dbf Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 22 Jan 2016 12:29:10 +0100 Subject: [PATCH] Add MatrixClient based test client --- .gitignore | 1 + src/Makefile.am | 9 +++- src/test-client.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 src/test-client.c diff --git a/.gitignore b/.gitignore index b38a40d..0773210 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ Makefile.in /src/*.gir /src/*.typelib /src/test-api-client +/src/test-client /aclocal.m4 /ar-lib /autom4te.cache diff --git a/src/Makefile.am b/src/Makefile.am index b560066..c8bb720 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ diff --git a/src/test-client.c b/src/test-client.c new file mode 100644 index 0000000..5c5b2b0 --- /dev/null +++ b/src/test-client.c @@ -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 + * . + */ + +#include +#include + +#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; +}