diff options
author | tomsmeding <tom.smeding@gmail.com> | 2017-03-14 14:06:14 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2017-03-14 14:06:14 +0100 |
commit | a7a5b95990e0d6c1617c796dba2c45dab816161d (patch) | |
tree | ac58c3975e4eb0b546669bbcfe658cacf8ebfd8b | |
parent | 230e9775f5b61e21aa085825fbbd0232e9a360ef (diff) |
Inviting
-rw-r--r-- | command.c | 29 | ||||
-rw-r--r-- | db.c | 22 | ||||
-rw-r--r-- | db.h | 3 | ||||
-rw-r--r-- | main.c | 1 |
4 files changed, 53 insertions, 2 deletions
@@ -95,7 +95,7 @@ static bool cmd_register(struct conn_data *data,const char *tag,const char **arg static bool cmd_login(struct conn_data *data,const char *tag,const char **args){ i64 userid=db_find_user(args[0]); if(userid==-1){ - send_error(data->fd,tag,"Username does not exist"); + send_error(data->fd,tag,"User not found"); return false; } char *pass=db_get_pass(userid); @@ -144,6 +144,32 @@ static bool cmd_create_room(struct conn_data *data,const char *tag,const char ** return closed; } +static bool cmd_invite(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; + } + + i64 user2=db_find_user(args[1]); + if(user2==-1){ + send_error(data->fd,tag,"User not found"); + return false; + } + + if(!db_is_member(roomid,data->userid)){ + send_error(data->fd,tag,"Not in that room"); + return false; + } + + db_add_member(roomid,user2); + return send_ok(data->fd,tag); +} + struct cmd_info{ const char *cmdname; @@ -157,6 +183,7 @@ static const struct cmd_info commands[]={ {"login",2,true,cmd_login}, {"list_rooms",0,false,cmd_list_rooms}, {"create_room",0,false,cmd_create_room}, + {"invite",2,false,cmd_invite}, }; #define NCOMMANDS (sizeof(commands)/sizeof(commands[0])) @@ -75,6 +75,28 @@ bool db_add_member(i64 roomid,i64 userid){ return success; } +bool db_is_member(i64 roomid,i64 userid){ + sqlite3_stmt *stmt; + SQLITE(prepare_v2,database,"select 1 from Members where room = ? and user = ?",-1,&stmt,NULL); + SQLITE(bind_int64,stmt,1,roomid); + SQLITE(bind_int64,stmt,2,userid); + bool success=sqlite3_step(stmt)==SQLITE_ROW; + SQLITE(finalize,stmt); + return success; +} + +i64 db_find_room(const char *name){ + sqlite3_stmt *stmt; + SQLITE(prepare_v2,database,"select id from Rooms where name = ?",-1,&stmt,NULL); + SQLITE(bind_text,stmt,1,name,-1,SQLITE_STATIC); + i64 roomid=-1; + if(sqlite3_step(stmt)==SQLITE_ROW){ + roomid=sqlite3_column_int64(stmt,0); + } + SQLITE(finalize,stmt); + return roomid; +} + struct db_room_list db_list_rooms(i64 userid){ sqlite3_stmt *stmt; SQLITE(prepare_v2,database, @@ -29,7 +29,8 @@ void db_close(void); struct db_name_id db_create_room(void); bool db_delete_room(i64 roomid); bool db_add_member(i64 roomid,i64 userid); -bool db_remove_member(i64 roomid,u64 userid); +bool db_remove_member(i64 roomid,i64 userid); +bool db_is_member(i64 roomid,i64 userid); i64 db_find_room(const char *name); // -1 if not found struct db_room_list db_list_rooms(i64 userid); @@ -46,6 +46,7 @@ static struct conn_data* find_conn_data(int fd){ } static void delete_conn_data(int fd){ + debug("Deleting conn_data for fd=%d",fd); struct hash_item *item=conn_hash[fd%CONN_HASH_SIZE]; assert(item); if(item->cd.fd==fd){ |