diff options
-rw-r--r-- | command.c | 8 | ||||
-rw-r--r-- | db.c | 11 | ||||
-rw-r--r-- | db.h | 1 |
3 files changed, 13 insertions, 7 deletions
@@ -250,7 +250,8 @@ static bool cmd_history(struct conn_data *data,const char *tag,const char **args net_send_error(data->fd,tag,"Not logged in"); return false; } - i64 roomid=db_find_room(args[0]); + const char *roomname=args[0]; + i64 roomid=db_find_room(roomname); if(roomid==-1){ net_send_error(data->fd,tag,"Room not found"); return false; @@ -273,8 +274,9 @@ static bool cmd_history(struct conn_data *data,const char *tag,const char **args for(i64 i=ml.count-1;i>=0;i--){ char *username=db_get_username(ml.list[i].userid); - len=asprintf(&buf,"%s history_message %" PRIi64 " %s %s %" PRIi64 " %s\n", - tag,ml.count-1-i,args[0],username,ml.list[i].timestamp,ml.list[i].message); + len=asprintf(&buf,"%s history_message %" PRIi64 " %s %s %" PRIi64 " %" PRIi64 " %s\n", + tag,ml.count-1-i,roomname,username,ml.list[i].timestamp, + ml.list[i].msgid,ml.list[i].message); closed=net_send_raw_text(data->fd,buf,len); free(buf); if(closed)break; @@ -304,9 +304,11 @@ void db_create_message(i64 roomid,i64 userid,i64 timestamp,const char *message){ } struct db_message_list db_get_messages(i64 roomid,i64 count){ + assert(count>=0); + sqlite3_stmt *stmt; SQLITE(prepare_v2,database, - "select user, time, message " + "select id, user, time, message " "from Messages " "where room = ? " "order by time desc " @@ -325,10 +327,11 @@ struct db_message_list db_get_messages(i64 roomid,i64 count){ if(ml.count==cap){ die("sqlite gave too many rows while 'limit %" PRIi64 "' was present",count); } + ml.list[ml.count].msgid=sqlite3_column_int64(stmt,0); ml.list[ml.count].roomid=roomid; - ml.list[ml.count].userid=sqlite3_column_int64(stmt,0); - ml.list[ml.count].timestamp=sqlite3_column_int64(stmt,1); - ml.list[ml.count].message=strdup((const char*)sqlite3_column_text(stmt,2)); + ml.list[ml.count].userid=sqlite3_column_int64(stmt,1); + ml.list[ml.count].timestamp=sqlite3_column_int64(stmt,2); + ml.list[ml.count].message=strdup((const char*)sqlite3_column_text(stmt,3)); ml.count++; } @@ -14,6 +14,7 @@ struct db_room_list{ }; struct db_message{ + i64 msgid; i64 roomid,userid,timestamp; char *message; }; |