aboutsummaryrefslogtreecommitdiff
path: root/command.c
diff options
context:
space:
mode:
Diffstat (limited to 'command.c')
-rw-r--r--command.c28
1 files changed, 25 insertions, 3 deletions
diff --git a/command.c b/command.c
index 3d406e1..e4d75d3 100644
--- a/command.c
+++ b/command.c
@@ -1,6 +1,7 @@
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
+#include <limits.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/socket.h>
@@ -238,11 +239,14 @@ 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){
+static bool history_cmd_helper(
+ struct conn_data *data,const char *tag,const char **args,
+ const char *cmdname,i64 beforeid){
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]);
+ debug("Connection fd=%d sent an invalid number for '%s': '%s'",
+ data->fd,cmdname,args[1]);
return true;
}
@@ -261,7 +265,7 @@ static bool cmd_history(struct conn_data *data,const char *tag,const char **args
return false;
}
- struct db_message_list ml=db_get_messages(roomid,nrequested);
+ struct db_message_list ml=db_get_messages_before(roomid,nrequested,beforeid);
char *buf=NULL;
i64 len=asprintf(&buf,"%s history %" PRIi64 "\n",tag,ml.count);
bool closed=net_send_raw_text(data->fd,buf,len);
@@ -286,6 +290,23 @@ static bool cmd_history(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){
+ return history_cmd_helper(data,tag,args,"history",-1);
+}
+
+static bool cmd_history_before(struct conn_data *data,const char *tag,const char **args){
+ char *endp;
+ i64 beforeid=strtoll(args[2],&endp,10);
+ if(args[2][0]=='\0'||*endp!='\0'){
+ debug("Connection fd=%d sent an invalid id for 'history_before': '%s'",
+ data->fd,args[2]);
+ return true;
+ }
+ if(beforeid<0)beforeid=INT64_MAX;
+
+ return history_cmd_helper(data,tag,args,"history_before",beforeid);
+}
+
static bool cmd_ping(struct conn_data *data,const char *tag,const char **args){
(void)args;
return net_send_pong(data->fd,tag);
@@ -353,6 +374,7 @@ static const struct cmd_info commands[]={
{"invite",2,false,cmd_invite},
{"send",2,true,cmd_send},
{"history",2,false,cmd_history},
+ {"history_before",3,false,cmd_history_before},
{"ping",0,false,cmd_ping},
{"is_online",1,false,cmd_is_online},
{"firebase_token",1,false,cmd_firebase_token},