diff options
author | tomsmeding <tom.smeding@gmail.com> | 2017-03-16 22:01:49 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2017-03-16 22:01:49 +0100 |
commit | 9ec8909fef512e71f3a002068cd0635cf959bef2 (patch) | |
tree | 989c9d92c6941b1b778f4ee7a06e53cdb43fffff | |
parent | cadfeb9d6f1a1f3299b919819d208cdaede717b4 (diff) |
Add command list_members
-rw-r--r-- | command.c | 32 | ||||
-rw-r--r-- | db.c | 10 | ||||
-rw-r--r-- | db.h | 3 |
3 files changed, 42 insertions, 3 deletions
@@ -153,6 +153,35 @@ static bool cmd_list_rooms(struct conn_data *data,const char *tag,const char **a return closed; } +static bool cmd_list_members(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; + } + + struct db_user_list ul=db_list_members(roomid); + if(ul.count<=0){ + db_nullify_user_list(ul); + return send_list(data->fd,tag,0,NULL); + } + const char *names[ul.count]; + for(i64 i=0;i<ul.count;i++){ + names[i]=ul.list[i].name; + } + bool closed=send_list(data->fd,tag,ul.count,names); + db_nullify_user_list(ul); + return closed; +} + static bool cmd_create_room(struct conn_data *data,const char *tag,const char **args){ (void)args; if(data->userid==-1){ @@ -210,7 +239,7 @@ static bool cmd_send(struct conn_data *data,const char *tag,const char **args){ db_create_message(roomid,data->userid,make_timestamp(),args[1]); bool closed=send_ok(data->fd,tag); - struct db_user_list members=db_get_members(roomid); + struct db_user_list members=db_list_members(roomid); for(i64 i=0;i<members.count;i++){ i64 nfds; const int *fds=userdata_online(members.list[i].id,&nfds); @@ -237,6 +266,7 @@ static const struct cmd_info commands[]={ {"register",2,true,cmd_register}, {"login",2,true,cmd_login}, {"list_rooms",0,false,cmd_list_rooms}, + {"list_members",1,false,cmd_list_members}, {"create_room",0,false,cmd_create_room}, {"invite",2,false,cmd_invite}, {"send",2,true,cmd_send}, @@ -85,7 +85,7 @@ bool db_is_member(i64 roomid,i64 userid){ return success; } -struct db_user_list db_get_members(i64 roomid){ +struct db_user_list db_list_members(i64 roomid){ sqlite3_stmt *stmt; SQLITE(prepare_v2,database, "select U.id, U.name " @@ -227,3 +227,11 @@ void db_nullify_room_list(struct db_room_list rl){ if(rl.list)free(rl.list); rl.list=NULL; } + +void db_nullify_user_list(struct db_user_list ul){ + for(i64 i=0;i<ul.count;i++){ + free(ul.list[i].name); + } + if(ul.list)free(ul.list); + ul.list=NULL; +} @@ -36,7 +36,7 @@ bool db_delete_room(i64 roomid); bool db_add_member(i64 roomid,i64 userid); bool db_remove_member(i64 roomid,i64 userid); bool db_is_member(i64 roomid,i64 userid); -struct db_user_list db_get_members(i64 roomid); +struct db_user_list db_list_members(i64 roomid); i64 db_find_room(const char *name); // -1 if not found struct db_room_list db_list_rooms(i64 userid); @@ -53,4 +53,5 @@ struct db_message_list db_get_messages(i64 roomid,i64 timestamp,i64 count); // void db_nullify_name_id(struct db_name_id ni); void db_nullify_room_list(struct db_room_list rl); +void db_nullify_user_list(struct db_user_list ul); void db_nullify_message_list(struct db_message_list ml); |