aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDamien Zammit <damien@zamaudio.com>2016-12-31 21:13:34 +1100
committerDamien Zammit <damien@zamaudio.com>2017-01-05 21:53:21 +1100
commit2c17db466d7631eee7bcf4e7505b03115ce901e3 (patch)
tree106f9f8bb646c775742a3a76a2276699214cc910
parent61ffd59e2e658daec87f79a8d05a9d202d76e9a3 (diff)
downloadpurple-matrix-2c17db466d7631eee7bcf4e7505b03115ce901e3.tar.gz
Enable room invites when matrix handle is known
Signed-off-by: Damien Zammit <damien@zamaudio.com>
-rw-r--r--AUTHORS.rst3
-rw-r--r--README.md1
-rw-r--r--libmatrix.c11
-rw-r--r--matrix-api.c41
-rw-r--r--matrix-api.h24
5 files changed, 78 insertions, 2 deletions
diff --git a/AUTHORS.rst b/AUTHORS.rst
index eb5b4e4..2d2470d 100644
--- a/AUTHORS.rst
+++ b/AUTHORS.rst
@@ -7,3 +7,6 @@ Eion Robb <eion at robbmob.com>
Dr. David Alan Gilbert <dave at treblig.org>
* Sending images, emote handling
+
+Damien Zammit <damien at zamaudio.com>
+ * Invite users
diff --git a/README.md b/README.md
index d414dda..205eaab 100644
--- a/README.md
+++ b/README.md
@@ -12,7 +12,6 @@ implemented. Sending and receiving simple text messages is supported, as is
joining rooms you are invited to by other users.
The following are not yet supported:
- * Sending invitations
* Creating new rooms (and one-to-one chats)
* Joining existing rooms by alias instead of room_id
* Presence indication
diff --git a/libmatrix.c b/libmatrix.c
index 37e7450..5b3118c 100644
--- a/libmatrix.c
+++ b/libmatrix.c
@@ -36,6 +36,7 @@
#include "matrix-connection.h"
#include "matrix-room.h"
+#include "matrix-api.h"
/**
* Called to get the icon name for the given buddy and account.
@@ -178,7 +179,15 @@ static void matrixprpl_reject_chat(PurpleConnection *gc, GHashTable *components)
matrix_connection_reject_invite(gc, room_id);
}
+static void matrixprpl_chat_invite(PurpleConnection *gc, int id,
+ const char *message, const char *who)
+{
+ PurpleConversation *conv = purple_find_chat(gc, id);
+ MatrixConnectionData *conn;
+ conn = (MatrixConnectionData *)(conv->account->gc->proto_data);
+ matrix_api_invite_user(conn, conv->name, who, NULL, NULL, NULL, NULL);
+}
/**
* handle leaving a chat: notify the server that we are leaving, and
@@ -279,7 +288,7 @@ static PurplePluginProtocolInfo prpl_info =
matrixprpl_join_chat, /* join_chat */
matrixprpl_reject_chat, /* reject_chat */
matrixprpl_get_chat_name, /* get_chat_name */
- NULL, /* chat_invite */
+ matrixprpl_chat_invite, /* chat_invite */
matrixprpl_chat_leave, /* chat_leave */
NULL, /* chat_whisper */
matrixprpl_chat_send, /* chat_send */
diff --git a/matrix-api.c b/matrix-api.c
index a8528ce..bf35605 100644
--- a/matrix-api.c
+++ b/matrix-api.c
@@ -717,6 +717,47 @@ MatrixApiRequestData *matrix_api_send(MatrixConnectionData *conn,
return fetch_data;
}
+void matrix_api_invite_user(MatrixConnectionData *conn,
+ const gchar *room_id,
+ const gchar *who,
+ MatrixApiCallback callback,
+ MatrixApiErrorCallback error_callback,
+ MatrixApiBadResponseCallback bad_response_callback,
+ gpointer user_data)
+{
+ GString *url;
+ JsonNode *body_node;
+ JsonGenerator *generator;
+ gchar *json;
+
+ JsonObject *invitee;
+ invitee = json_object_new();
+ json_object_set_string_member(invitee, "user_id", who);
+
+ url = g_string_new(conn->homeserver);
+ g_string_append(url, "_matrix/client/r0/rooms/");
+ g_string_append(url, purple_url_encode(room_id));
+ g_string_append(url, "/invite?access_token=");
+ g_string_append(url, purple_url_encode(conn->access_token));
+
+ body_node = json_node_new(JSON_NODE_OBJECT);
+ json_node_set_object(body_node, invitee);
+
+ generator = json_generator_new();
+ json_generator_set_root(generator, body_node);
+ json = json_generator_to_data(generator, NULL);
+ g_object_unref(G_OBJECT(generator));
+ json_node_free(body_node);
+
+ purple_debug_info("matrixprpl", "sending an invite on %s\n", room_id);
+
+ matrix_api_start(url->str, "POST", json, conn, callback,
+ error_callback, bad_response_callback,
+ user_data, 0);
+ g_free(json);
+ g_string_free(url, TRUE);
+ json_object_unref(invitee);
+}
MatrixApiRequestData *matrix_api_join_room(MatrixConnectionData *conn,
const gchar *room,
diff --git a/matrix-api.h b/matrix-api.h
index 4245cd7..3359e0e 100644
--- a/matrix-api.h
+++ b/matrix-api.h
@@ -189,6 +189,30 @@ MatrixApiRequestData *matrix_api_send(MatrixConnectionData *conn,
MatrixApiBadResponseCallback bad_response_callback,
gpointer user_data);
+/**
+ * Invite a user to a room
+ *
+ * @param conn The connection with which to make the request
+ * @param room_id The room id to invite the user to
+ *
+ * @param who The mxid of the person to invite
+ *
+ * @param callback Function to be called when the request completes
+ * @param error_callback Function to be called if there is an error making
+ * the request. If NULL, matrix_api_error will be
+ * used.
+ * @param bad_response_callback Function to be called if the API gives a non-200
+ * response. If NULL, matrix_api_bad_response will be
+ * used.
+ * @param user_data Opaque data to be passed to the callbacks
+ */
+void matrix_api_invite_user(MatrixConnectionData *conn,
+ const gchar *room_id,
+ const gchar *who,
+ MatrixApiCallback callback,
+ MatrixApiErrorCallback error_callback,
+ MatrixApiBadResponseCallback bad_response_callback,
+ gpointer user_data);
/**
* Make a request to join a room