aboutsummaryrefslogtreecommitdiff
path: root/command.c
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-03-17 19:57:12 +0100
committertomsmeding <tom.smeding@gmail.com>2017-03-17 19:57:12 +0100
commitadd6b39aece179a0e0ad425af72b8a043a0d3188 (patch)
tree0c64d959587f46d8d43471f9939a1847e32ce02d /command.c
parent9a82f91be11a5698c6f0da4dce665e081f3637d0 (diff)
History querying
Diffstat (limited to 'command.c')
-rw-r--r--command.c49
1 files changed, 49 insertions, 0 deletions
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]))