aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--command.c19
-rw-r--r--db.c28
-rw-r--r--db.h1
3 files changed, 38 insertions, 10 deletions
diff --git a/command.c b/command.c
index b38f3e6..431c8f1 100644
--- a/command.c
+++ b/command.c
@@ -90,15 +90,6 @@ static bool send_list(int fd,const char *tag,i64 count,const char **list){
return send_raw_text(fd,"\n",1);
}
-static bool send_push(int fd,const char *head,const char *arg){
- char *buf=NULL;
- i64 len=asprintf(&buf,"_push %s %s\n",head,arg);
- assert(buf);
- bool closed=send_raw_text(fd,buf,len);
- free(buf);
- return closed;
-}
-
static bool cmd_register(struct conn_data *data,const char *tag,const char **args){
i64 userid=db_find_user(args[0]);
@@ -239,6 +230,12 @@ 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);
+ char *pushbuf=NULL;
+ char *username=db_get_username(data->userid);
+ i64 pushbuflen=asprintf(&pushbuf,"_push message %s %s %s\n",args[0],username,args[1]);
+ assert(pushbuf);
+ free(username);
+
struct db_user_list members=db_list_members(roomid);
for(i64 i=0;i<members.count;i++){
i64 nfds;
@@ -246,11 +243,13 @@ static bool cmd_send(struct conn_data *data,const char *tag,const char **args){
if(nfds<=0)continue;
for(i64 j=0;j<nfds;j++){
if(fds[j]!=data->fd){
- send_push(fds[j],"message",args[1]);
+ send_raw_text(fds[j],pushbuf,pushbuflen);
}
}
}
+ free(pushbuf);
+
return closed;
}
diff --git a/db.c b/db.c
index e7740a0..80f9867 100644
--- a/db.c
+++ b/db.c
@@ -128,6 +128,20 @@ i64 db_find_room(const char *name){
return roomid;
}
+char* db_get_roomname(i64 roomid){
+ sqlite3_stmt *stmt;
+ SQLITE(prepare_v2,database,"select name from Rooms where id = ?",-1,&stmt,NULL);
+ SQLITE(bind_int64,stmt,1,roomid);
+ const unsigned char *name_sq=NULL;
+ if(sqlite3_step(stmt)==SQLITE_ROW){
+ name_sq=sqlite3_column_text(stmt,0);
+ }
+ char *name=NULL;
+ if(name_sq)name=strdup((const char*)name_sq);
+ SQLITE(finalize,stmt);
+ return name;
+}
+
struct db_room_list db_list_rooms(i64 userid){
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,
@@ -174,6 +188,20 @@ i64 db_create_user(const char *name,const char *pass){
}
}
+char* db_get_username(i64 userid){
+ sqlite3_stmt *stmt;
+ SQLITE(prepare_v2,database,"select name from Users where id = ?",-1,&stmt,NULL);
+ SQLITE(bind_int64,stmt,1,userid);
+ const unsigned char *name_sq=NULL;
+ if(sqlite3_step(stmt)==SQLITE_ROW){
+ name_sq=sqlite3_column_text(stmt,0);
+ }
+ char *name=NULL;
+ if(name_sq)name=strdup((const char*)name_sq);
+ SQLITE(finalize,stmt);
+ return name;
+}
+
char* db_get_pass(i64 userid){
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,"select pass from Users where id = ?",-1,&stmt,NULL);
diff --git a/db.h b/db.h
index a2fcf6e..f4fea4f 100644
--- a/db.h
+++ b/db.h
@@ -38,6 +38,7 @@ bool db_remove_member(i64 roomid,i64 userid);
bool db_is_member(i64 roomid,i64 userid);
struct db_user_list db_list_members(i64 roomid);
i64 db_find_room(const char *name); // -1 if not found
+char* db_get_roomname(i64 roomid);
struct db_room_list db_list_rooms(i64 userid);
i64 db_create_user(const char *name,const char *pass);