aboutsummaryrefslogtreecommitdiff
path: root/db.c
diff options
context:
space:
mode:
Diffstat (limited to 'db.c')
-rw-r--r--db.c63
1 files changed, 54 insertions, 9 deletions
diff --git a/db.c b/db.c
index 0de620d..d94b13c 100644
--- a/db.c
+++ b/db.c
@@ -10,7 +10,8 @@
#define SQLITE(func,...) do{if(sqlite3_##func(__VA_ARGS__)!=SQLITE_OK){die_sqlite("sqlite3_" #func);}}while(0)
-#define DATABASE_VERSION 1
+#define DATABASE_VERSION 2
+
#define PASSHASH_OPSLIMIT 3
#define PASSHASH_MEMLIMIT crypto_pwhash_MEMLIMIT_INTERACTIVE
@@ -381,16 +382,18 @@ bool db_delete_token(i64 userid,const char *token){
}
-i64 db_create_message(i64 roomid,i64 userid,i64 timestamp,const char *message){
+i64 db_create_message(i64 roomid,i64 userid,i64 timestamp,i64 replyid,const char *message){
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,
- "insert into Messages (room, user, time, message) "
- "values (?, ?, ?, ?)"
+ "insert into Messages (room, user, time, reply, message) "
+ "values (?, ?, ?, ?, ?)"
,-1,&stmt,NULL);
SQLITE(bind_int64,stmt,1,roomid);
SQLITE(bind_int64,stmt,2,userid);
SQLITE(bind_int64,stmt,3,timestamp);
- SQLITE(bind_blob,stmt,4,message,strlen(message),SQLITE_STATIC);
+ if(replyid>=0)SQLITE(bind_int64,stmt,4,replyid);
+ else SQLITE(bind_null,stmt,4);
+ SQLITE(bind_blob,stmt,5,message,strlen(message),SQLITE_STATIC);
if(sqlite3_step(stmt)!=SQLITE_DONE)die_sqlite("sqlite3_step");
SQLITE(finalize,stmt);
@@ -407,7 +410,7 @@ struct db_message_list db_get_messages_before(i64 roomid,i64 count,i64 beforeid)
sqlite3_stmt *stmt;
if(beforeid<0){
SQLITE(prepare_v2,database,
- "select id, user, time, message "
+ "select id, user, time, reply, message "
"from Messages "
"where room = ? "
"order by time desc "
@@ -417,7 +420,7 @@ struct db_message_list db_get_messages_before(i64 roomid,i64 count,i64 beforeid)
SQLITE(bind_int64,stmt,2,count);
} else {
SQLITE(prepare_v2,database,
- "select id, user, time, message "
+ "select id, user, time, reply, message "
"from Messages "
"where room = ? and time < (select time from Messages where id = ?) "
"order by time desc "
@@ -442,7 +445,12 @@ struct db_message_list db_get_messages_before(i64 roomid,i64 count,i64 beforeid)
ml.list[ml.count].roomid=roomid;
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));
+ if(sqlite3_column_type(stmt,3)==SQLITE_INTEGER){
+ ml.list[ml.count].replyid=sqlite3_column_int64(stmt,3);
+ } else {
+ ml.list[ml.count].replyid=-1; // NULL, not a reply
+ }
+ ml.list[ml.count].message=strdup((const char*)sqlite3_column_text(stmt,4));
ml.count++;
}
@@ -452,6 +460,39 @@ struct db_message_list db_get_messages_before(i64 roomid,i64 count,i64 beforeid)
return ml;
}
+struct db_message db_get_message(i64 msgid) {
+ sqlite3_stmt *stmt;
+ SQLITE(prepare_v2, database,
+ "select room, user, time, reply, message "
+ "from Messages where id = ?",
+ -1, &stmt, NULL);
+ SQLITE(bind_int64, stmt, 1, msgid);
+
+ struct db_message msg;
+ if (sqlite3_step(stmt) == SQLITE_ROW) {
+ msg.msgid = msgid;
+ msg.roomid = sqlite3_column_int64(stmt,0);
+ msg.userid = sqlite3_column_int64(stmt,1);
+ msg.timestamp = sqlite3_column_int64(stmt,2);
+ if (sqlite3_column_type(stmt, 3) == SQLITE_INTEGER) {
+ msg.replyid = sqlite3_column_int64(stmt, 3);
+ } else {
+ msg.replyid = -1; // NULL, not a reply
+ }
+ msg.message = strdup((const char*)sqlite3_column_text(stmt, 4));
+ } else {
+ msg.msgid = -1;
+ msg.roomid = -1;
+ msg.userid = -1;
+ msg.timestamp = -1;
+ msg.replyid = -1;
+ msg.message = NULL;
+ }
+
+ SQLITE(finalize, stmt);
+ return msg;
+}
+
void db_nullify_name_id(struct db_name_id ni){
if(ni.name)free(ni.name);
@@ -473,9 +514,13 @@ void db_nullify_user_list(struct db_user_list ul){
ul.list=NULL;
}
+void db_nullify_message(struct db_message msg){
+ if(msg.message)free(msg.message);
+}
+
void db_nullify_message_list(struct db_message_list ml){
for(i64 i=0;i<ml.count;i++){
- free(ml.list[i].message);
+ db_nullify_message(ml.list[i]);
}
if(ml.list)free(ml.list);
ml.list=NULL;