From 3b3f2f3168c80e53055891f9afcfc5c5120dabd7 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sat, 15 Apr 2017 16:05:28 +0200 Subject: server: Support cmd_is_online --- command.c | 12 ++++++++++++ net.c | 8 ++++++++ net.h | 1 + 3 files changed, 21 insertions(+) diff --git a/command.c b/command.c index b966a75..e66eb12 100644 --- a/command.c +++ b/command.c @@ -263,6 +263,17 @@ static bool cmd_ping(struct conn_data *data,const char *tag,const char **args){ return net_send_pong(data->fd,tag); } +static bool cmd_is_online(struct conn_data *data,const char *tag,const char **args){ + i64 userid=db_find_user(args[0]); + if(userid==-1){ + net_send_error(data->fd,tag,"User not found"); + return false; + } + i64 nfds; + (void)userdata_online(userid,&nfds); + return net_send_number(data->fd,tag,nfds); +} + struct cmd_info{ const char *cmdname; @@ -281,6 +292,7 @@ static const struct cmd_info commands[]={ {"send",2,true,cmd_send}, {"history",2,false,cmd_history}, {"ping",0,false,cmd_ping}, + {"is_online",1,false,cmd_is_online}, }; #define NCOMMANDS (sizeof(commands)/sizeof(commands[0])) diff --git a/net.c b/net.c index e5bdb6b..cce4dc6 100644 --- a/net.c +++ b/net.c @@ -27,6 +27,14 @@ bool net_send_ok(int fd,const char *tag){ return closed; } +bool net_send_number(int fd,const char *tag,i64 number){ + char *buf=NULL; + i64 len=asprintf(&buf,"%s number %" PRIi64 "\n",tag,number); + bool closed=net_send_raw_text(fd,buf,len); + free(buf); + return closed; +} + bool net_send_error(int fd,const char *tag,const char *msg){ char *buf=NULL; i64 len=asprintf(&buf,"%s error %s\n",tag,msg); diff --git a/net.h b/net.h index 4528e42..11475b7 100644 --- a/net.h +++ b/net.h @@ -5,6 +5,7 @@ bool net_send_raw_text(int fd,const char *text,i64 len); bool net_send_ok(int fd,const char *tag); +bool net_send_number(int fd,const char *tag,i64 number); bool net_send_error(int fd,const char *tag,const char *msg); bool net_send_name(int fd,const char *tag,const char *name); bool net_send_list(int fd,const char *tag,i64 count,const char **list); -- cgit v1.2.3-70-g09d2