From 1719796a3a10eccf91eadb38c019829110e96294 Mon Sep 17 00:00:00 2001 From: Iru Cai Date: Fri, 29 Jun 2018 11:20:51 +0800 Subject: update get_devices --- api.c | 17 ++++++++++++----- curl_util.h | 12 ------------ device.c | 34 ++++++++++++++++++++++++---------- device.h | 10 +++++++++- util.c | 21 +++++++++++++++++++++ util.h | 4 ++++ 6 files changed, 70 insertions(+), 28 deletions(-) diff --git a/api.c b/api.c index f5c0d81..3757a0f 100644 --- a/api.c +++ b/api.c @@ -9,11 +9,13 @@ #include "device.h" /* logout: return 1 if success, 0 if failed */ -int matrix_logout(matrix_session *sess) +int matrix_logout(matrix_session *sess, int logout_all) { char * requrl = alloc_build_string((const char *[]){ sess->url, - "/_matrix/client/r0/logout?access_token=", + "/_matrix/client/r0/logout", + logout_all?"/all":"", + "?access_token=", sess->token, NULL }); @@ -82,7 +84,8 @@ const char * matrix_resolv_alias(matrix_session *sess, const char *alias) void matrix_sync(matrix_session *sess) { - char *_req[] = { sess->url, "/_matrix/client/r0/sync?access_token=", sess->token , NULL }; + const char *_req[] = { sess->url, + "/_matrix/client/r0/sync?access_token=", sess->token , NULL }; char *requrl = alloc_build_string(_req); json_object *resp; @@ -127,7 +130,11 @@ int main() printf("%s\n", json_object_to_json_string(joined_rooms)); matrix_sync(sess); puts("Diveces:"); - get_devices(sess); + int ndev; + matrix_device *devs = get_devices(sess, &ndev); + for (int i = 0; i < ndev; i++) { + printf("%s %s\n", devs[i].device_id, devs[i].display_name); + } const char *roomid = matrix_resolv_alias(sess, "#hello:my.domain.name"); if (roomid == NULL) @@ -144,7 +151,7 @@ int main() if (roomid) free(roomid); - if (matrix_logout(sess)) + if (matrix_logout(sess, 1)) puts("logout!"); if (matrix_whoami(sess)) { diff --git a/curl_util.h b/curl_util.h index 509bcb9..5e86e5a 100644 --- a/curl_util.h +++ b/curl_util.h @@ -15,18 +15,6 @@ json_add_string(json_object *j, const char *k, const char *v) return json_object_object_add(j, k, json_object_new_string(v)); } -static inline -const char *json_gets(json_object *j, const char *key) -{ - json_object *val; - - if (json_object_object_get_ex(j, key, &val)) { - if (json_object_is_type(val, json_type_string)) - return json_object_get_string(val); - } - return NULL; -} - static inline char* copy_str(const char *s) { diff --git a/device.c b/device.c index f5b9942..3834388 100644 --- a/device.c +++ b/device.c @@ -10,8 +10,9 @@ DELETE /_matrix/client/r0/devices/{deviceId} #include "session.h" #include "util.h" #include "curl_util.h" +#include "device.h" -void get_devices(matrix_session *sess) +matrix_device * get_devices(matrix_session *sess, int *n) { /* GET /_matrix/client/r0/devices */ const char *u[] = { sess->url, @@ -20,15 +21,28 @@ void get_devices(matrix_session *sess) json_object *resp; _curl_get(sess->curl, requrl, &resp); if (resp) { - printf("%s\n", json_object_to_json_string(resp)); - /* - const char *roomid = json_gets(resp, "room_id"); - if (roomid) { - const char *r = copy_str(roomid); - json_object_put(resp); - return r; + json_object *devarr; + if (json_object_object_get_ex(resp, "devices", &devarr) + && json_object_is_type(devarr, json_type_array)) { + size_t ndev = json_object_array_length(devarr); + matrix_device *devs = (matrix_device *)malloc(sizeof(matrix_device) * ndev); + for (size_t i = 0; i < ndev; i++) { + json_object *devi = json_object_array_get_idx(devarr, i); + devs[i].device_id = json_gets_dup(devi, "device_id"); + devs[i].display_name = json_gets_dup(devi, "display_name"); + devs[i].last_seen_ip = json_gets_dup(devi, "last_seen_ip"); + json_object *ts; + if (json_object_object_get_ex(devi, "last_seen_ts", &ts)) { + devs[i].last_seen_ts = json_object_get_int64(ts); + } else { + devs[i].last_seen_ts = 0; + } + } + *n = ndev; + return devs; } - */ + json_object_put(resp); } - json_object_put(resp); + *n = 0; + return NULL; } diff --git a/device.h b/device.h index d1234e7..69bc43c 100644 --- a/device.h +++ b/device.h @@ -6,7 +6,15 @@ extern "C" { #endif /* } */ -void get_devices(matrix_session *sess); +typedef struct +{ + char *device_id; + char *display_name; + char *last_seen_ip; + int64_t last_seen_ts; +} matrix_device; + +matrix_device * get_devices(matrix_session *sess, int *n); #ifdef __cplusplus } diff --git a/util.c b/util.c index 6cd56ac..020485c 100644 --- a/util.c +++ b/util.c @@ -1,5 +1,6 @@ #include #include +#include "util.h" #define SLMAX 100 @@ -25,3 +26,23 @@ char * alloc_build_string(const char *ss[]) res[cur] = 0; return res; } + +const char *json_gets(json_object *j, const char *key) +{ + json_object *val; + + if (json_object_object_get_ex(j, key, &val)) { + if (json_object_is_type(val, json_type_string)) + return json_object_get_string(val); + } + return NULL; +} + +char *json_gets_dup(json_object *j, const char *key) +{ + const char *t = json_gets(j, key); + if (t == NULL) + return NULL; + else + return strdup(t); +} diff --git a/util.h b/util.h index ab0b158..b38338f 100644 --- a/util.h +++ b/util.h @@ -1,6 +1,8 @@ #ifndef LIBMC_UTIL_H #define LIBMC_UTIL_H +#include + #ifdef __cplusplus extern "C" { #endif /* } */ @@ -10,6 +12,8 @@ extern "C" { * @note callers should call free() to free the returned C string */ char * alloc_build_string(const char *ss[]); +const char *json_gets(json_object *j, const char *key); +char *json_gets_dup(json_object *j, const char *key); #ifdef __cplusplus } -- cgit v1.2.3