From 548c2b18e03eacb9c6295df2520dd709506d80f5 Mon Sep 17 00:00:00 2001 From: luckydonald Date: Wed, 20 May 2015 00:13:39 +0200 Subject: [PATCH 1/2] added "online-status" event. E.g. ```{"event": "online-status", "online": true, "when": "2015-05-20 00:16:40", "state": 1}``` --- interface.c | 28 +++++++++++++++++++--------- json-tg.c | 30 ++++++++++++++++++++++++++++++ json-tg.h | 5 +++-- 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/interface.c b/interface.c index 303426d..16c3c33 100644 --- a/interface.c +++ b/interface.c @@ -1970,7 +1970,7 @@ void print_chat_info_gw (struct tgl_state *TLSR, void *extra, int success, struc } void print_user_status (struct tgl_user_status *S, struct in_ev *ev) { - if (enable_json) { return; } + assert(!enable_json); //calling functions print_user_info_gw() and user_status_upd() already check. if (S->online > 0) { mprintf (ev, "online (was online "); print_date_full (ev, S->when); @@ -2561,16 +2561,26 @@ void user_status_upd (struct tgl_state *TLS, struct tgl_user *U) { if (disable_output && !notify_ev) { return; } if (!binlog_read) { return; } if (log_level < 3) { return; } - if (enable_json) { return; } struct in_ev *ev = notify_ev; mprint_start (ev); - mpush_color (ev, COLOR_YELLOW); - mprintf (ev, "User "); - print_user_name (ev, U->id, (void *)U); - mprintf (ev, " "); - print_user_status (&U->status, ev); - mprintf (ev, "\n"); - mpop_color (ev); + if (!enable_json) + { + mpush_color (ev, COLOR_YELLOW); + mprintf (ev, "User "); + print_user_name(ev, U->id, (void *) U); + mprintf (ev, " "); + print_user_status(&U->status, ev); + mprintf (ev, "\n"); + mpop_color (ev); + } else { + #ifdef USE_JSON + json_t *res = json_pack_user_status(&U->status); + char *s = json_dumps (res, 0); + mprintf (ev, "%s\n", s); + json_decref (res); + free (s); + #endif + } mprint_end (ev); } diff --git a/json-tg.c b/json-tg.c index 376708c..3d201d6 100644 --- a/json-tg.c +++ b/json-tg.c @@ -6,6 +6,8 @@ #include #include #include +//format time: +#include #ifndef json_boolean #define json_boolean(val) ((val) ? json_true() : json_false()) @@ -381,4 +383,32 @@ json_t *json_pack_read (struct tgl_message *M) { return res; } +int str_format_time(long when, char* string) +{ + struct tm *tm = localtime ((void *)&when); + return sprintf (string, "%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); +} + +json_t *json_pack_user_status (struct tgl_user_status *S) { + json_t *res = json_object (); + assert (json_object_set (res, "online", json_boolean (S->online == 1)) >= 0); + assert (json_object_set (res, "state", json_integer (S->online)) >= 0); + if (S->online > 0 || S->online == -1) { + static char s[20]; + str_format_time(S->when, s); + assert (json_object_set (res, "when", json_string (s)) >= 0); + } else if (S->online == 0) { + assert (json_object_set(res, "when", json_string("long time ago")) >= 0); + } else if (S->online == -2) { + assert (json_object_set(res, "when", json_string("recently")) >= 0); + } else if (S->online == -3) { + assert (json_object_set(res, "when", json_string("last week")) >= 0); + } else if (S->online == -4) { + assert (json_object_set (res, "when", json_string ("last month")) >= 0); + } + assert (json_object_set (res, "event", json_string ("online-status")) >= 0); + //this will overwrite "event":"message" to "event":"read". + return res; +} + #endif diff --git a/json-tg.h b/json-tg.h index bafad9b..8ceaf4e 100644 --- a/json-tg.h +++ b/json-tg.h @@ -3,11 +3,12 @@ #include "config.h" #ifdef USE_JSON #include -#include -#include +#include +#include json_t *json_pack_message (struct tgl_message *M); json_t *json_pack_updates (unsigned flags); json_t *json_pack_peer (tgl_peer_id_t id, tgl_peer_t *P); json_t *json_pack_read (struct tgl_message *M); +json_t *json_pack_user_status (struct tgl_user_status *S); #endif #endif From 3963eca8caa0d678327dfb5b8a0a8fed9d805099 Mon Sep 17 00:00:00 2001 From: luckydonald Date: Wed, 20 May 2015 00:33:10 +0200 Subject: [PATCH 2/2] added missing "user" attribute to "online-status" event. E.g. ```{"event": "online-status", "online": true, "when": "2015-05-20 00:16:40", "state": 1, "user": {"username": ... }}``` --- interface.c | 2 +- json-tg.c | 7 ++++++- json-tg.h | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/interface.c b/interface.c index 16c3c33..ff18968 100644 --- a/interface.c +++ b/interface.c @@ -2574,7 +2574,7 @@ void user_status_upd (struct tgl_state *TLS, struct tgl_user *U) { mpop_color (ev); } else { #ifdef USE_JSON - json_t *res = json_pack_user_status(&U->status); + json_t *res = json_pack_user_status(U); char *s = json_dumps (res, 0); mprintf (ev, "%s\n", s); json_decref (res); diff --git a/json-tg.c b/json-tg.c index 3d201d6..a8cb49d 100644 --- a/json-tg.c +++ b/json-tg.c @@ -389,8 +389,13 @@ int str_format_time(long when, char* string) return sprintf (string, "%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); } -json_t *json_pack_user_status (struct tgl_user_status *S) { +json_t *json_pack_user_status (struct tgl_user *U) { json_t *res = json_object (); + struct tgl_user_status *S = &U->status; + json_object (); + json_t *user_res = json_object (); + json_pack_user(user_res, (void *) U); + assert (json_object_set (res, "user", user_res) >= 0); assert (json_object_set (res, "online", json_boolean (S->online == 1)) >= 0); assert (json_object_set (res, "state", json_integer (S->online)) >= 0); if (S->online > 0 || S->online == -1) { diff --git a/json-tg.h b/json-tg.h index 8ceaf4e..bd34b4b 100644 --- a/json-tg.h +++ b/json-tg.h @@ -9,6 +9,6 @@ json_t *json_pack_message (struct tgl_message *M); json_t *json_pack_updates (unsigned flags); json_t *json_pack_peer (tgl_peer_id_t id, tgl_peer_t *P); json_t *json_pack_read (struct tgl_message *M); -json_t *json_pack_user_status (struct tgl_user_status *S); +json_t *json_pack_user_status (struct tgl_user *U); #endif #endif