From 40164b64876f25c9cf53b2b288dabc91e23551d0 Mon Sep 17 00:00:00 2001 From: Iru Cai Date: Fri, 29 Jun 2018 13:44:41 +0800 Subject: room/message --- Makefile | 2 +- api.c | 5 ++++ room.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ room.h | 26 +++++++++++++++++++++ 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 room.c create mode 100644 room.h diff --git a/Makefile b/Makefile index 514df2c..379670b 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ LDLIBS=-lcurl -ljson-c CFLAGS=-g -Wall -api: api.o session.o curl_util.o util.o device.o +api: api.o session.o curl_util.o util.o device.o room.o diff --git a/api.c b/api.c index 3757a0f..967e3ae 100644 --- a/api.c +++ b/api.c @@ -7,6 +7,7 @@ #include "curl_util.h" #include "util.h" #include "device.h" +#include "room.h" /* logout: return 1 if success, 0 if failed */ int matrix_logout(matrix_session *sess, int logout_all) @@ -136,6 +137,9 @@ int main() printf("%s %s\n", devs[i].device_id, devs[i].display_name); } + json_object *msg = get_room_messages(sess, "!DxdsutHRWpSeicUCKs:matrixim.cc", NULL); + printf("%s\n", json_object_to_json_string(msg)); +#if 0 const char *roomid = matrix_resolv_alias(sess, "#hello:my.domain.name"); if (roomid == NULL) puts("fail to resolv #hello:my.domain.name"); @@ -150,6 +154,7 @@ int main() printf("create room %s\n", roomid); if (roomid) free(roomid); +#endif if (matrix_logout(sess, 1)) puts("logout!"); diff --git a/room.c b/room.c new file mode 100644 index 0000000..a759b2d --- /dev/null +++ b/room.c @@ -0,0 +1,81 @@ +#include "room.h" +#include "util.h" +#include "curl_util.h" +#include + +json_object * get_room_messages(matrix_session *sess, const char *roomid, query_param *param) +{ + json_object *resp; + const char *req[100]; + char dirs[] = "dir=b&"; + char limit[40]; + char *esc_roomid = NULL; + char *esc_filter = NULL; + int rn; + + req[0] = sess->url; + req[1] = "/_matrix/client/r0/rooms/"; + if (roomid[0] != '%') { + esc_roomid = curl_easy_escape(sess->curl, roomid, strlen(roomid)); + req[2] = esc_roomid; + } else { + req[2] = roomid; + } + req[3] = "/messages?"; + rn = 4; + if (param != NULL) { + if (param->from) { + req[rn] = "from="; + req[rn+1] = param->from; + req[rn+2] = "&"; + rn += 3; + } + if (param->to) { + req[rn] = "to="; + req[rn+1] = param->to; + req[rn+2] = "&"; + rn += 3; + } + switch (param->dir) { + case 'f': + case 'b': + dirs[4] = param->dir; + req[rn] = dirs; + rn++; + break; + default: + break; + } + if (param->limit > 0) { + sprintf(limit, "limit=%d&", param->limit); + req[rn] = limit; + rn++; + } + if (param->filter) { + if (param->filter[0] != '%') { + esc_filter = curl_easy_escape(sess->curl, param->filter, strlen(param->filter)); + req[rn] = esc_filter; + } else { + req[rn] = param->filter; + } + req[rn+1] = "&"; + rn += 2; + } + } + req[rn] = "access_token="; + req[rn+1] = sess->token; + req[rn+2] = NULL; + + char *requrl = alloc_build_string(req); + _curl_get(sess->curl, requrl, &resp); + + if (esc_roomid) + curl_free(esc_roomid); + + if (esc_filter) + curl_free(esc_filter); + + free(requrl); + + return resp; +} diff --git a/room.h b/room.h new file mode 100644 index 0000000..4678f01 --- /dev/null +++ b/room.h @@ -0,0 +1,26 @@ +#ifndef LIBMC_ROOM_H +#define LIBMC_ROOM_H + +#include +#include "session.h" + +#ifdef __cplusplus +extern "C" { +#endif /* } */ + +typedef struct +{ + char *from; + char *to; + char dir; + int limit; + char *filter; +} query_param; + +json_object * get_room_messages(matrix_session *sess, const char *roomid, query_param *param); + +#ifdef __cplusplus +} +#endif + +#endif -- cgit v1.2.3