diff options
Diffstat (limited to 'command.c')
-rw-r--r-- | command.c | 30 |
1 files changed, 30 insertions, 0 deletions
@@ -3,11 +3,19 @@ #include <string.h> #include <errno.h> #include <assert.h> +#include <sys/time.h> #include <sys/socket.h> #include "command.h" #include "db.h" +static i64 make_timestamp(void){ + struct timeval tv; + gettimeofday(&tv,NULL); + return (i64)tv.tv_sec+tv.tv_usec; +} + + static bool send_raw_text(int fd,const char *text,i64 len){ i64 cursor=0; while(cursor<len){ @@ -170,6 +178,27 @@ static bool cmd_invite(struct conn_data *data,const char *tag,const char **args) return send_ok(data->fd,tag); } +static bool cmd_send(struct conn_data *data,const char *tag,const char **args){ + 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; + } + + db_create_message(roomid,data->userid,make_timestamp(),args[1]); + bool closed=send_ok(data->fd,tag); + +#warning TODO: broadcast +} + struct cmd_info{ const char *cmdname; @@ -184,6 +213,7 @@ static const struct cmd_info commands[]={ {"list_rooms",0,false,cmd_list_rooms}, {"create_room",0,false,cmd_create_room}, {"invite",2,false,cmd_invite}, + {"send",2,true,cmd_send}, }; #define NCOMMANDS (sizeof(commands)/sizeof(commands[0])) |