From add6b39aece179a0e0ad425af72b8a043a0d3188 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Fri, 17 Mar 2017 19:57:12 +0100 Subject: History querying --- command.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'command.c') diff --git a/command.c b/command.c index 50b9534..a691171 100644 --- a/command.c +++ b/command.c @@ -253,6 +253,54 @@ static bool cmd_send(struct conn_data *data,const char *tag,const char **args){ return closed; } +static bool cmd_history(struct conn_data *data,const char *tag,const char **args){ + char *endp; + i64 nrequested=strtoll(args[1],&endp,10); + if(args[1][0]=='\0'||*endp!='\0'||nrequested<0){ + debug("Connection fd=%d sent an invalid number for 'history': '%s'",data->fd,args[1]); + return true; + } + + if(data->userid==-1){ + send_error(data->fd,tag,"Not logged in"); + return false; + } + i64 roomid=db_find_room(args[0]); + if(roomid==-1){ + send_error(data->fd,tag,"Room not found"); + return false; + } + if(!db_is_member(roomid,data->userid)){ + send_error(data->fd,tag,"Not in that room"); + return false; + } + + struct db_message_list ml=db_get_messages(roomid,nrequested); + char *buf=NULL; + i64 len=asprintf(&buf,"%s history %" PRIi64 "\n",tag,ml.count); + assert(buf); + bool closed=send_raw_text(data->fd,buf,len); + free(buf); + + if(closed){ + db_nullify_message_list(ml); + return true; + } + + for(i64 i=ml.count-1;i>=0;i--){ + char *username=db_get_username(ml.list[i].userid); + len=asprintf(&buf,"%s history_item %" PRIi64 " %s %" PRIi64 " %s\n", + tag,ml.count-1-i,username,ml.list[i].timestamp,ml.list[i].message); + assert(buf); + closed=send_raw_text(data->fd,buf,len); + free(buf); + if(closed)break; + } + + db_nullify_message_list(ml); + return closed; +} + struct cmd_info{ const char *cmdname; @@ -269,6 +317,7 @@ static const struct cmd_info commands[]={ {"create_room",0,false,cmd_create_room}, {"invite",2,false,cmd_invite}, {"send",2,true,cmd_send}, + {"history",2,false,cmd_history}, }; #define NCOMMANDS (sizeof(commands)/sizeof(commands[0])) -- cgit v1.2.3-70-g09d2