X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fglobal.cpp;h=9bc8f0483bcc2d042e89648498cb41bd86bed30d;hb=6836fbb5db8464f56e682989996b2210b14231d0;hp=0c6f913ddbb19d57ddf9c4bdbdff1fca782d004e;hpb=52c0819bfc1d083c6e0738f75f0d7eeba521295a;p=fms.git diff --git a/src/global.cpp b/src/global.cpp index 0c6f913..9bc8f04 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -17,20 +17,40 @@ #include #endif -bool wantshutdown=false; +volatile bool wantshutdown=false; + +std::string CreateShortIdentityName(const std::string &name, const std::string &publickey) +{ + std::string result=""; + std::vector keyparts; + + StringFunctions::SplitMultiple(publickey,"@,",keyparts); + + result+=name; + if(keyparts.size()>1 && keyparts[1].size()>8) + { + result+="@"+keyparts[1].substr(0,4)+"..."; + } + + return result; +} void SetupDB() { DateTime date; + std::string tempval=""; SQLite3DB::DB *db=SQLite3DB::DB::Instance(); db->Open("fms.db3"); db->SetBusyTimeout(10000); // set timeout to 10 seconds - db->Execute("VACUUM;"); - - // TODO remove this - temp fix for problem in 0.1.8 - db->Execute("DELETE FROM tblMessageBoard WHERE MessageID NOT IN (SELECT MessageID FROM tblMessage);"); + + tempval=""; + Option::Instance()->Get("VacuumOnStartup",tempval); + if(tempval=="true") + { + db->Execute("VACUUM;"); + } db->Execute("CREATE TABLE IF NOT EXISTS tblDBVersion(\ Major INTEGER,\ @@ -52,13 +72,67 @@ void SetupDB() major=1; minor=1; } + if(major==1 && (minor==1 || minor==2)) + { + ConvertDB0101To0103(); + major=1; + minor=3; + } + if(major==1 && minor==3) + { + ConvertDB0103To0104(); + major=1; + minor=4; + } + if(major==1 && minor==4) + { + ConvertDB0104To0105(); + major=1; + minor=5; + } + if(major==1 && minor==5) + { + ConvertDB0105To0106(); + major=1; + minor=6; + } + if(major==1 && minor==6) + { + ConvertDB0106To0107(); + major=1; + minor=7; + } + if(major==1 && minor==7) + { + ConvertDB0107To0108(); + major=1; + minor=8; + } + if(major==1 && minor==8) + { + ConvertDB0108To0109(); + major=1; + minor=9; + } + if(major==1 && minor==9) + { + ConvertDB0109To0110(); + major=1; + minor=10; + } + if(major==1 && minor==10) + { + ConvertDB0110To0111(); + major=1; + minor=11; + } } else { - db->Execute("INSERT INTO tblDBVersion(Major,Minor) VALUES(1,1);"); + db->Execute("INSERT INTO tblDBVersion(Major,Minor) VALUES(1,11);"); } - db->Execute("UPDATE tblDBVersion SET Major=1, Minor=2;"); + db->Execute("UPDATE tblDBVersion SET Major=1, Minor=11;"); db->Execute("CREATE TABLE IF NOT EXISTS tblOption(\ Option TEXT UNIQUE,\ @@ -74,16 +148,20 @@ void SetupDB() SingleUse BOOL CHECK(SingleUse IN('true','false')) DEFAULT 'false',\ PublishTrustList BOOL CHECK(PublishTrustList IN('true','false')) DEFAULT 'false',\ PublishBoardList BOOL CHECK(PublishBoardList IN('true','false')) DEFAULT 'false',\ + PublishFreesite BOOL CHECK(PublishFreesite IN('true','false')) DEFAULT 'false',\ + FreesiteEdition INTEGER,\ InsertingIdentity BOOL CHECK(InsertingIdentity IN('true','false')) DEFAULT 'false',\ LastInsertedIdentity DATETIME,\ InsertingPuzzle BOOL CHECK(InsertingPuzzle IN('true','false')) DEFAULT 'false',\ LastInsertedPuzzle DATETIME,\ InsertingTrustList BOOL CHECK(InsertingTrustList IN('true','false')) DEFAULT 'false',\ LastInsertedTrustList DATETIME,\ - InsertingBoardList BOOL CHECK(InsertingBoardList IN('true','false')) DEFAULT 'false',\ LastInsertedBoardList DATETIME,\ - InsertingMessageList BOOL CHECK(InsertingMessageList IN('true','false')) DEFAULT 'false',\ - LastInsertedMessageList DATETIME\ + LastInsertedMessageList DATETIME,\ + LastInsertedFreesite DATETIME,\ + DateCreated DATETIME,\ + MinMessageDelay INTEGER DEFAULT 0,\ + MaxMessageDelay INTEGER DEFAULT 0\ );"); db->Execute("CREATE TABLE IF NOT EXISTS tblLocalIdentityInserts(\ @@ -118,18 +196,21 @@ void SetupDB() );"); db->Execute("CREATE TABLE IF NOT EXISTS tblIdentity(\ - IdentityID INTEGER PRIMARY KEY,\ - PublicKey TEXT UNIQUE,\ - Name TEXT,\ - SingleUse BOOL CHECK(SingleUse IN('true','false')) DEFAULT 'false',\ - PublishTrustList BOOL CHECK(PublishTrustList IN('true','false')) DEFAULT 'false',\ - PublishBoardList BOOL CHECK(PublishBoardList IN('true','false')) DEFAULT 'false',\ - DateAdded DATETIME,\ - LastSeen DATETIME,\ - LocalMessageTrust INTEGER CHECK(LocalMessageTrust BETWEEN 0 AND 100) DEFAULT 50,\ - PeerMessageTrust INTEGER CHECK(PeerMessageTrust BETWEEN 0 AND 100) DEFAULT 50,\ - LocalTrustListTrust INTEGER CHECK(LocalTrustListTrust BETWEEN 0 AND 100) DEFAULT 50,\ - PeerTrustListTrust INTEGER CHECK(PeerTrustListTrust BETWEEN 0 AND 100) DEFAULT 50\ + IdentityID INTEGER PRIMARY KEY,\ + PublicKey TEXT UNIQUE,\ + Name TEXT,\ + SingleUse BOOL CHECK(SingleUse IN('true','false')) DEFAULT 'false',\ + PublishTrustList BOOL CHECK(PublishTrustList IN('true','false')) DEFAULT 'false',\ + PublishBoardList BOOL CHECK(PublishBoardList IN('true','false')) DEFAULT 'false',\ + FreesiteEdition INTEGER,\ + DateAdded DATETIME,\ + LastSeen DATETIME,\ + LocalMessageTrust INTEGER CHECK(LocalMessageTrust BETWEEN 0 AND 100) DEFAULT NULL,\ + PeerMessageTrust INTEGER CHECK(PeerMessageTrust BETWEEN 0 AND 100) DEFAULT NULL,\ + LocalTrustListTrust INTEGER CHECK(LocalTrustListTrust BETWEEN 0 AND 100) DEFAULT NULL,\ + PeerTrustListTrust INTEGER CHECK(PeerTrustListTrust BETWEEN 0 AND 100) DEFAULT NULL,\ + AddedMethod TEXT,\ + Hidden BOOL CHECK(Hidden IN('true','false')) DEFAULT 'false'\ );"); db->Execute("CREATE TABLE IF NOT EXISTS tblIdentityRequests(\ @@ -158,24 +239,60 @@ void SetupDB() Inserted BOOL CHECK(Inserted IN('true','false')) DEFAULT 'false'\ );"); + db->Execute("CREATE TABLE IF NOT EXISTS tblIdentityTrust(\ + LocalIdentityID INTEGER,\ + IdentityID INTEGER,\ + LocalMessageTrust INTEGER CHECK(LocalMessageTrust BETWEEN 0 AND 100) DEFAULT NULL,\ + MessageTrustComment TEXT,\ + LocalTrustListTrust INTEGER CHECK(LocalTrustListTrust BETWEEN 0 AND 100) DEFAULT NULL,\ + TrustListTrustComment TEXT\ + );"); + + db->Execute("CREATE UNIQUE INDEX IF NOT EXISTS idxIdentityTrust_IDs ON tblIdentityTrust(LocalIdentityID,IdentityID);"); + + db->Execute("CREATE TRIGGER IF NOT EXISTS trgInsertOnIdentityTrust AFTER INSERT ON tblIdentityTrust \ + FOR EACH ROW \ + BEGIN \ + UPDATE tblIdentity SET LocalMessageTrust=(SELECT MAX(LocalMessageTrust) FROM tblIdentityTrust WHERE tblIdentityTrust.IdentityID=new.IdentityID GROUP BY tblIdentityTrust.IdentityID), LocalTrustListTrust=(SELECT MAX(LocalTrustListTrust) FROM tblIdentityTrust WHERE tblIdentityTrust.IdentityID=new.IdentityID GROUP BY tblIdentityTrust.IdentityID) WHERE tblIdentity.IdentityID=new.IdentityID; \ + END;"); + + db->Execute("CREATE TRIGGER IF NOT EXISTS trgUpdateOnIdentityTrust AFTER UPDATE OF LocalMessageTrust,LocalTrustListTrust ON tblIdentityTrust \ + FOR EACH ROW \ + BEGIN \ + UPDATE tblIdentity SET LocalMessageTrust=(SELECT MAX(LocalMessageTrust) FROM tblIdentityTrust WHERE tblIdentityTrust.IdentityID=new.IdentityID GROUP BY tblIdentityTrust.IdentityID), LocalTrustListTrust=(SELECT MAX(LocalTrustListTrust) FROM tblIdentityTrust WHERE tblIdentityTrust.IdentityID=new.IdentityID GROUP BY tblIdentityTrust.IdentityID) WHERE tblIdentity.IdentityID=new.IdentityID; \ + END;"); + + db->Execute("CREATE TRIGGER IF NOT EXISTS trgDeleteOnIdentityTrust AFTER DELETE ON tblIdentityTrust \ + FOR EACH ROW \ + BEGIN \ + UPDATE tblIdentity SET LocalMessageTrust=(SELECT MAX(LocalMessageTrust) FROM tblIdentityTrust WHERE tblIdentityTrust.IdentityID=old.IdentityID GROUP BY tblIdentityTrust.IdentityID), LocalTrustListTrust=(SELECT MAX(LocalTrustListTrust) FROM tblIdentityTrust WHERE tblIdentityTrust.IdentityID=old.IdentityID GROUP BY tblIdentityTrust.IdentityID) WHERE tblIdentity.IdentityID=old.IdentityID; \ + END;"); + db->Execute("CREATE TABLE IF NOT EXISTS tblPeerTrust(\ - IdentityID INTEGER,\ - TargetIdentityID INTEGER,\ - MessageTrust INTEGER CHECK(MessageTrust BETWEEN 0 AND 100),\ - TrustListTrust INTEGER CHECK(TrustListTrust BETWEEN 0 AND 100)\ + IdentityID INTEGER,\ + TargetIdentityID INTEGER,\ + MessageTrust INTEGER CHECK(MessageTrust BETWEEN 0 AND 100),\ + TrustListTrust INTEGER CHECK(TrustListTrust BETWEEN 0 AND 100),\ + MessageTrustComment TEXT,\ + TrustListTrustComment TEXT\ );"); + db->Execute("CREATE INDEX IF NOT EXISTS idxPeerTrust_IdentityID ON tblPeerTrust (IdentityID);"); + db->Execute("CREATE INDEX IF NOT EXISTS idxPeerTrust_TargetIdentityID ON tblPeerTrust (TargetIdentityID);"); + db->Execute("CREATE TABLE IF NOT EXISTS tblBoard(\ - BoardID INTEGER PRIMARY KEY,\ - BoardName TEXT UNIQUE,\ - BoardDescription TEXT,\ - DateAdded DATETIME\ + BoardID INTEGER PRIMARY KEY,\ + BoardName TEXT UNIQUE,\ + BoardDescription TEXT,\ + DateAdded DATETIME,\ + SaveReceivedMessages BOOL CHECK(SaveReceivedMessages IN('true','false')) DEFAULT 'true',\ + AddedMethod TEXT\ );"); - db->Execute("INSERT INTO tblBoard(BoardName,BoardDescription,DateAdded) VALUES('fms','Freenet Message System','2007-12-01 12:00:00');"); - db->Execute("INSERT INTO tblBoard(BoardName,BoardDescription,DateAdded) VALUES('freenet','Discussion about Freenet','2007-12-01 12:00:00');"); - db->Execute("INSERT INTO tblBoard(BoardName,BoardDescription,DateAdded) VALUES('public','Public discussion','2007-12-01 12:00:00');"); - db->Execute("INSERT INTO tblBoard(BoardName,BoardDescription,DateAdded) VALUES('test','Test board','2007-12-01 12:00:00');"); + db->Execute("INSERT INTO tblBoard(BoardName,BoardDescription,DateAdded,AddedMethod) VALUES('fms','Freenet Message System','2007-12-01 12:00:00','Initial Board');"); + db->Execute("INSERT INTO tblBoard(BoardName,BoardDescription,DateAdded,AddedMethod) VALUES('freenet','Discussion about Freenet','2007-12-01 12:00:00','Initial Board');"); + db->Execute("INSERT INTO tblBoard(BoardName,BoardDescription,DateAdded,AddedMethod) VALUES('public','Public discussion','2007-12-01 12:00:00','Initial Board');"); + db->Execute("INSERT INTO tblBoard(BoardName,BoardDescription,DateAdded,AddedMethod) VALUES('test','Test board','2007-12-01 12:00:00','Initial Board');"); db->Execute("CREATE TABLE IF NOT EXISTS tblMessage(\ MessageID INTEGER PRIMARY KEY,\ @@ -186,20 +303,28 @@ void SetupDB() Subject TEXT,\ MessageUUID TEXT UNIQUE,\ ReplyBoardID INTEGER,\ - Body TEXT\ + Body TEXT,\ + MessageIndex INTEGER\ );"); + db->Execute("CREATE INDEX IF NOT EXISTS idxMessage_IdentityID ON tblMessage (IdentityID);"); + db->Execute("CREATE TABLE IF NOT EXISTS tblMessageReplyTo(\ MessageID INTEGER,\ ReplyToMessageUUID TEXT,\ ReplyOrder INTEGER\ );"); + db->Execute("CREATE INDEX IF NOT EXISTS idxMessageReplyTo_MessageID ON tblMessageReplyTo (MessageID);"); + db->Execute("CREATE TABLE IF NOT EXISTS tblMessageBoard(\ MessageID INTEGER,\ BoardID INTEGER\ );"); + db->Execute("CREATE INDEX IF NOT EXISTS idxMessageBoard_MessageID ON tblMessageBoard (MessageID);"); + db->Execute("CREATE INDEX IF NOT EXISTS idxMessageBoard_BoardID ON tblMessageBoard (BoardID);"); + db->Execute("CREATE TABLE IF NOT EXISTS tblMessageListRequests(\ IdentityID INTEGER,\ Day DATE,\ @@ -215,13 +340,26 @@ void SetupDB() Found BOOL CHECK(Found IN('true','false')) DEFAULT 'false'\ );"); + db->Execute("CREATE UNIQUE INDEX IF NOT EXISTS idxMessageRequest ON tblMessageRequests(IdentityID,Day,RequestIndex);"); + db->Execute("CREATE TABLE IF NOT EXISTS tblMessageInserts(\ LocalIdentityID INTEGER,\ Day DATE,\ InsertIndex INTEGER,\ MessageUUID TEXT UNIQUE,\ MessageXML TEXT,\ - Inserted BOOL CHECK(Inserted IN('true','false')) DEFAULT 'false'\ + Inserted BOOL CHECK(Inserted IN('true','false')) DEFAULT 'false',\ + SendDate DATETIME\ + );"); + + db->Execute("CREATE TABLE IF NOT EXISTS tblFileInserts(\ + FileInsertID INTEGER PRIMARY KEY,\ + MessageUUID TEXT,\ + FileName TEXT,\ + Key TEXT,\ + Size INTEGER,\ + MimeType TEXT,\ + Data BLOB\ );"); db->Execute("CREATE TABLE IF NOT EXISTS tblMessageListInserts(\ @@ -265,10 +403,13 @@ void SetupDB() GROUP BY tblBoard.BoardID;"); // calculates peer trust + // do the (MessageTrust+1)*LocalTrustListTrust/(MessageTrust+1)/100.0 - so if MessageTrust or TrustListTrust is NULL, the calc will be NULL and it won't be included at all in the average + // need the +1 so that when the values are 0 the result is not 0 + db->Execute("DROP VIEW IF EXISTS vwCalculatedPeerTrust;"); db->Execute("CREATE VIEW IF NOT EXISTS vwCalculatedPeerTrust AS \ SELECT TargetIdentityID, \ - ROUND(SUM(MessageTrust*(LocalMessageTrust/100.0))/SUM(LocalMessageTrust/100.0),0) AS 'PeerMessageTrust', \ - ROUND(SUM(TrustListTrust*(LocalTrustListTrust/100.0))/SUM(LocalTrustListTrust/100.0),0) AS 'PeerTrustListTrust' \ + ROUND(SUM(MessageTrust*(LocalTrustListTrust/100.0))/SUM(((MessageTrust+1)*LocalTrustListTrust/(MessageTrust+1))/100.0),0) AS 'PeerMessageTrust', \ + ROUND(SUM(TrustListTrust*(LocalTrustListTrust/100.0))/SUM(((TrustListTrust+1)*LocalTrustListTrust/(TrustListTrust+1))/100.0),0) AS 'PeerTrustListTrust' \ FROM tblPeerTrust INNER JOIN tblIdentity ON tblPeerTrust.IdentityID=tblIdentity.IdentityID \ WHERE LocalTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalTrustListTrust') \ AND ( PeerTrustListTrust IS NULL OR PeerTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinPeerTrustListTrust') ) \ @@ -321,6 +462,7 @@ void SetupDB() DELETE FROM tblMessageReplyTo WHERE tblMessageReplyTo.MessageID=old.MessageID;\ END;"); + db->Execute("DROP TRIGGER IF EXISTS trgDeleteIdentity;"); db->Execute("CREATE TRIGGER IF NOT EXISTS trgDeleteIdentity AFTER DELETE ON tblIdentity \ FOR EACH ROW \ BEGIN \ @@ -330,8 +472,10 @@ void SetupDB() DELETE FROM tblMessageRequests WHERE IdentityID=old.IdentityID;\ DELETE FROM tblPeerTrust WHERE IdentityID=old.IdentityID;\ DELETE FROM tblTrustListRequests WHERE IdentityID=old.IdentityID;\ + DELETE FROM tblIdentityTrust WHERE IdentityID=old.IdentityID;\ END;"); + db->Execute("DROP TRIGGER IF EXISTS trgDeleteLocalIdentity;"); db->Execute("CREATE TRIGGER IF NOT EXISTS trgDeleteLocalIdentity AFTER DELETE ON tblLocalIdentity \ FOR EACH ROW \ BEGIN \ @@ -341,6 +485,7 @@ void SetupDB() DELETE FROM tblMessageInserts WHERE LocalIdentityID=old.LocalIdentityID;\ DELETE FROM tblMessageListInserts WHERE LocalIdentityID=old.LocalIdentityID;\ DELETE FROM tblTrustListInserts WHERE LocalIdentityID=old.LocalIdentityID;\ + DELETE FROM tblIdentityTrust WHERE LocalIdentityID=old.LocalIdentityID;\ END;"); db->Execute("CREATE TRIGGER IF NOT EXISTS trgDeleteBoard AFTER DELETE ON tblBoard \ @@ -360,11 +505,31 @@ void SetupDB() date.SetToGMTime(); // insert SomeDude's public key - db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded) VALUES('SSK@NuBL7aaJ6Cn4fB7GXFb9Zfi8w1FhPyW3oKgU9TweZMw,iXez4j3qCpd596TxXiJgZyTq9o-CElEuJxm~jNNZAuA,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"');"); + db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded,LocalTrustListTrust,AddedMethod) VALUES('SSK@NuBL7aaJ6Cn4fB7GXFb9Zfi8w1FhPyW3oKgU9TweZMw,iXez4j3qCpd596TxXiJgZyTq9o-CElEuJxm~jNNZAuA,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"',50,'Initial Identity');"); // insert Shadow Panther's public key - db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded) VALUES('SSK@~mimyB1kmH4f7Cgsd2wM2Qv2NxrZHRMM6IY8~7EWRVQ,fxTKkR0TYhgMYb-vEGAv55sMOxCGD2xhE4ZxWHxdPz4,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"');"); + db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES('SSK@~mimyB1kmH4f7Cgsd2wM2Qv2NxrZHRMM6IY8~7EWRVQ,fxTKkR0TYhgMYb-vEGAv55sMOxCGD2xhE4ZxWHxdPz4,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"','Initial Identity');"); // insert garfield's public key - db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded) VALUES('SSK@T8l1IEGU4-PoASFzgc2GYhIgRzUvZsKdoQWeuLHuTmM,QLxAPfkGis8l5NafNpSCdbxzXhBlu9WL8svcqJw9Mpo,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"');"); + db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES('SSK@T8l1IEGU4-PoASFzgc2GYhIgRzUvZsKdoQWeuLHuTmM,QLxAPfkGis8l5NafNpSCdbxzXhBlu9WL8svcqJw9Mpo,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"','Initial Identity');"); + // insert alek's public key + db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES('SSK@lTjeI6V0lQsktXqaqJ6Iwk4TdsHduQI54rdUpHfhGbg,0oTYfrxxx8OmdU1~60gqpf3781qzEicM4Sz97mJsBM4,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"','Initial Identity');"); + // insert Luke771's public key + db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES('SSK@mdXK~ZVlfTZhF1SLBrvZ--i0vOsOpa~w9wv~~psQ-04,gXonsXKc7aexKSO8Gt8Fwre4Qgmmbt2WueO7VzxNKkk,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"','Initial Identity');"); + // insert falafel's public key + db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES('SSK@IxVqeqM0LyYdTmYAf5z49SJZUxr7NtQkOqVYG0hvITw,RM2wnMn5zAufCMt5upkkgq25B1elfBAxc7htapIWg1c,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"','Initial Identity');"); + // insert cptn_insano's public key + db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES('SSK@bloE1LJ~qzSYUkU2nt7sB9kq060D4HTQC66pk5Q8NpA,DOOASUnp0kj6tOdhZJ-h5Tk7Ka50FSrUgsH7tCG1usU,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"','Initial Identity');"); + // insert Flink's public key + db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES('SSK@q2TtkNBOuuniyJ56~8NSopCs3ttwe5KlB31ugZtWmXA,6~PzIupS8YK7L6oFNpXGKJmHT2kBMDfwTg73nHdNur8,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"','Initial Identity');"); + // insert Kane's public key + db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES('SSK@Ofm~yZivDJ5Z2fSzZbMiLEUUQaIc0KHRdZMBTaPLO6I,WLm4s4hNbOOurJ6ijfOq4odz7-dN7uTUvYxJRwWnlMI,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"','Initial Identity');"); + // inserts boardstat's public key + db->Execute("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES('SSK@aYWBb6zo2AM13XCNhsmmRKMANEx6PG~C15CWjdZziKA,X1pAG4EIqR1gAiyGFVZ1iiw-uTlh460~rFACJ7ZHQXk,AQACAAE/','"+date.Format("%Y-%m-%d %H:%M:%S")+"','Initial Identity');"); + + // TODO remove sometime after 0.1.17 + FixCapitalBoardNames(); + + // run analyze - may speed up some queries + db->Execute("ANALYZE;"); } @@ -398,6 +563,177 @@ void ConvertDB0100To0101() db->Execute("UPDATE tblDBVersion SET Major=1, Minor=1;"); } +void ConvertDB0101To0103() +{ + // remove default 50 from trust fields and set default to NULL + SQLite3DB::DB *db=SQLite3DB::DB::Instance(); + db->Execute("CREATE TEMPORARY TABLE tblIdentityTemp AS SELECT * FROM tblIdentity;"); + db->Execute("DROP TABLE IF EXISTS tblIdentity;"); + db->Execute("CREATE TABLE IF NOT EXISTS tblIdentity(\ + IdentityID INTEGER PRIMARY KEY,\ + PublicKey TEXT UNIQUE,\ + Name TEXT,\ + SingleUse BOOL CHECK(SingleUse IN('true','false')) DEFAULT 'false',\ + PublishTrustList BOOL CHECK(PublishTrustList IN('true','false')) DEFAULT 'false',\ + PublishBoardList BOOL CHECK(PublishBoardList IN('true','false')) DEFAULT 'false',\ + DateAdded DATETIME,\ + LastSeen DATETIME,\ + LocalMessageTrust INTEGER CHECK(LocalMessageTrust BETWEEN 0 AND 100) DEFAULT NULL,\ + PeerMessageTrust INTEGER CHECK(PeerMessageTrust BETWEEN 0 AND 100) DEFAULT NULL,\ + LocalTrustListTrust INTEGER CHECK(LocalTrustListTrust BETWEEN 0 AND 100) DEFAULT NULL,\ + PeerTrustListTrust INTEGER CHECK(PeerTrustListTrust BETWEEN 0 AND 100) DEFAULT NULL\ + );"); + db->Execute("INSERT INTO tblIdentity SELECT * FROM tblIdentityTemp;"); + db->Execute("DROP TABLE IF EXISTS tblIdentityTemp;"); + + // add SaveReceivedMessages field to tblBoard + db->Execute("ALTER TABLE tblBoard ADD COLUMN SaveReceivedMessages BOOL CHECK(SaveReceivedMessages IN('true','false')) DEFAULT 'true';"); + + db->Execute("UPDATE tblDBVersion SET Major=1, Minor=3;"); +} + +void ConvertDB0103To0104() +{ + // add MessageIndex to tblMessage + DateTime date; + SQLite3DB::DB *db=SQLite3DB::DB::Instance(); + db->Execute("ALTER TABLE tblMessage ADD COLUMN MessageIndex INTEGER;"); + db->Execute("CREATE UNIQUE INDEX IF NOT EXISTS idxMessageRequest ON tblMessageRequests(IdentityID,Day,RequestIndex);"); + db->Execute("ALTER TABLE tblLocalIdentity ADD COLUMN DateCreated DATETIME;"); + date.SetToGMTime(); + db->Execute("UPDATE tblLocalIdentity SET DateCreated='"+date.Format("%Y-%m-%d %H:%M:%S")+"' WHERE DateCreated IS NULL;"); + db->Execute("UPDATE tblDBVersion SET Major=1, Minor=4;"); +} + +void ConvertDB0104To0105() +{ + // add AddedMethod, MessageTrustComment, TrustListTrustComment to tblIdentity + // add MessageTrustComment,TrustListTrustComment to tblPeerTrust + SQLite3DB::DB *db=SQLite3DB::DB::Instance(); + db->Execute("ALTER TABLE tblIdentity ADD COLUMN AddedMethod TEXT;"); + db->Execute("ALTER TABLE tblIdentity ADD COLUMN MessageTrustComment TEXT;"); + db->Execute("ALTER TABLE tblIdentity ADD COLUMN TrustListTrustComment TEXT;"); + db->Execute("ALTER TABLE tblPeerTrust ADD COLUMN MessageTrustComment TEXT;"); + db->Execute("ALTER TABLE tblPeerTrust ADD COLUMN TrustListTrustComment TEXT;"); + db->Execute("UPDATE tblDBVersion SET Major=1, Minor=5;"); +} + +void ConvertDB0105To0106() +{ + // add Publish Freesite + SQLite3DB::DB *db=SQLite3DB::DB::Instance(); + db->Execute("ALTER TABLE tblLocalIdentity ADD COLUMN PublishFreesite BOOL CHECK(PublishFreesite IN('true','false')) DEFAULT 'false';"); + db->Execute("ALTER TABLE tblLocalIdentity ADD COLUMN LastInsertedFreesite DATETIME;"); + db->Execute("UPDATE tblDBVersion SET Major=1, Minor=6;"); +} + +void ConvertDB0106To0107() +{ + // add AddedMethod to tblBoard + SQLite3DB::DB *db=SQLite3DB::DB::Instance(); + db->Execute("ALTER TABLE tblBoard ADD COLUMN AddedMethod TEXT;"); + db->Execute("ALTER TABLE tblIdentity ADD COLUMN Hidden BOOL CHECK(Hidden IN('true','false')) DEFAULT 'false';"); + db->Execute("UPDATE tblIdentity SET Hidden='false' WHERE Hidden IS NULL;"); + db->Execute("UPDATE tblDBVersion SET Major=1, Minor=7;"); +} + +void ConvertDB0107To0108() +{ + // add FreesiteEdition to tblLocalIdentity and tblIdentity + SQLite3DB::DB *db=SQLite3DB::DB::Instance(); + db->Execute("ALTER TABLE tblLocalIdentity ADD COLUMN FreesiteEdition INTEGER;"); + db->Execute("ALTER TABLE tblIdentity ADD COLUMN FreesiteEdition INTEGER;"); + db->Execute("UPDATE tblDBVersion SET Major=1, Minor=8;"); +} + +void ConvertDB0108To0109() +{ + SQLite3DB::DB *db=SQLite3DB::DB::Instance(); + db->Execute("CREATE TABLE IF NOT EXISTS tblFileInserts(\ + FileInsertID INTEGER PRIMARY KEY,\ + MessageUUID TEXT,\ + FileName TEXT,\ + Key TEXT,\ + Size INTEGER,\ + Data BLOB\ + );"); + db->Execute("UPDATE tblDBVersion SET Major=1, Minor=9;"); +} + +void ConvertDB0109To0110() +{ + SQLite3DB::DB *db=SQLite3DB::DB::Instance(); + db->Execute("ALTER TABLE tblFileInserts ADD COLUMN MimeType TEXT;"); + db->Execute("UPDATE tblDBVersion SET Major=1, Minor=10;"); +} + +void ConvertDB0110To0111() +{ + /* + Drop MessageTrustComment, TrustListTrustComment FROM tblIdentity + + Drop InsertingMessageList, InsertingBoardList FROM tblLocalIdentity + Add MinMessageDelay, MaxMessageDelay to tblLocalIdentity Default 0 + + Add SendDate to tblMessageInserts + */ + SQLite3DB::DB *db=SQLite3DB::DB::Instance(); + + db->Execute("ALTER TABLE tblMessageInserts ADD COLUMN SendDate DATETIME;"); + + db->Execute("CREATE TEMPORARY TABLE tblLocalIdentityTemp AS SELECT * FROM tblLocalIdentity;"); + db->Execute("DROP TABLE IF EXISTS tblLocalIdentity;"); + db->Execute("CREATE TABLE IF NOT EXISTS tblLocalIdentity(\ + LocalIdentityID INTEGER PRIMARY KEY,\ + Name TEXT,\ + PublicKey TEXT UNIQUE,\ + PrivateKey TEXT UNIQUE,\ + SingleUse BOOL CHECK(SingleUse IN('true','false')) DEFAULT 'false',\ + PublishTrustList BOOL CHECK(PublishTrustList IN('true','false')) DEFAULT 'false',\ + PublishBoardList BOOL CHECK(PublishBoardList IN('true','false')) DEFAULT 'false',\ + PublishFreesite BOOL CHECK(PublishFreesite IN('true','false')) DEFAULT 'false',\ + FreesiteEdition INTEGER,\ + InsertingIdentity BOOL CHECK(InsertingIdentity IN('true','false')) DEFAULT 'false',\ + LastInsertedIdentity DATETIME,\ + InsertingPuzzle BOOL CHECK(InsertingPuzzle IN('true','false')) DEFAULT 'false',\ + LastInsertedPuzzle DATETIME,\ + InsertingTrustList BOOL CHECK(InsertingTrustList IN('true','false')) DEFAULT 'false',\ + LastInsertedTrustList DATETIME,\ + LastInsertedBoardList DATETIME,\ + LastInsertedMessageList DATETIME,\ + LastInsertedFreesite DATETIME,\ + DateCreated DATETIME,\ + MinMessageDelay INTEGER DEFAULT 0,\ + MaxMessageDelay INTEGER DEFAULT 0\ + );"); + db->Execute("INSERT INTO tblLocalIdentity SELECT LocalIdentityID,Name,PublicKey,PrivateKey,SingleUse,PublishTrustList,PublishBoardList,PublishFreesite,FreesiteEdition,InsertingIdentity,LastInsertedIdentity,InsertingPuzzle,LastInsertedPuzzle,InsertingTrustList,LastInsertedTrustList,LastInsertedBoardList,LastInsertedMessageList,LastInsertedFreesite,DateCreated,0,0 FROM tblLocalIdentityTemp;"); + db->Execute("DROP TABLE IF EXISTS tblLocalIdentityTemp;"); + + db->Execute("CREATE TEMPORARY TABLE tblIdentityTemp AS SELECT * FROM tblIdentity;"); + db->Execute("DROP TABLE IF EXISTS tblIdentity;"); + db->Execute("CREATE TABLE IF NOT EXISTS tblIdentity(\ + IdentityID INTEGER PRIMARY KEY,\ + PublicKey TEXT UNIQUE,\ + Name TEXT,\ + SingleUse BOOL CHECK(SingleUse IN('true','false')) DEFAULT 'false',\ + PublishTrustList BOOL CHECK(PublishTrustList IN('true','false')) DEFAULT 'false',\ + PublishBoardList BOOL CHECK(PublishBoardList IN('true','false')) DEFAULT 'false',\ + FreesiteEdition INTEGER,\ + DateAdded DATETIME,\ + LastSeen DATETIME,\ + LocalMessageTrust INTEGER CHECK(LocalMessageTrust BETWEEN 0 AND 100) DEFAULT NULL,\ + PeerMessageTrust INTEGER CHECK(PeerMessageTrust BETWEEN 0 AND 100) DEFAULT NULL,\ + LocalTrustListTrust INTEGER CHECK(LocalTrustListTrust BETWEEN 0 AND 100) DEFAULT NULL,\ + PeerTrustListTrust INTEGER CHECK(PeerTrustListTrust BETWEEN 0 AND 100) DEFAULT NULL,\ + AddedMethod TEXT,\ + Hidden BOOL CHECK(Hidden IN('true','false')) DEFAULT 'false'\ + );"); + db->Execute("INSERT INTO tblIdentity SELECT IdentityID,PublicKey,Name,SingleUse,PublishTrustList,PublishBoardList,FreesiteEdition,DateAdded,LastSeen,LocalMessageTrust,PeerMessageTrust,LocalTrustListTrust,PeerTrustListTrust,AddedMethod,Hidden FROM tblIdentityTemp;"); + db->Execute("DROP TABLE IF EXISTS tblIdentityTemp;"); + + db->Execute("UPDATE tblDBVersion SET Major=1, Minor=11;"); +} + void SetupDefaultOptions() { // OptionValue should always be inserted as a string, even if the option really isn't a string - just to keep the field data type consistent @@ -444,7 +780,7 @@ void SetupDefaultOptions() st.Bind(0,"StartHTTP"); st.Bind(1,"true"); - st.Bind(2,"Start HTTP server."); + st.Bind(2,"Start HTTP server. WARNING: If you turn this off, you won't be able to access the administration pages."); st.Step(); st.Reset(); @@ -454,10 +790,16 @@ void SetupDefaultOptions() st.Step(); st.Reset(); + st.Bind(0,"HTTPAccessControl"); + st.Bind(1,"-0.0.0.0/0,+127.0.0.1"); + st.Bind(2,"Comma separated list of addresses and/or subnet masks that are allowed access to the administration pages. Default is localhost only. + allows a host, - denies a host."); + st.Step(); + st.Reset(); + // StartFreenetUpdater st.Bind(0,"StartFreenetUpdater"); st.Bind(1,"true"); - st.Bind(2,"Start Freenet Updater thread."); + st.Bind(2,"Set to true to start the Freenet Updater thread and connect to Freenet. Set to false to prevent communication with Freenet."); st.Step(); st.Reset(); @@ -475,6 +817,12 @@ void SetupDefaultOptions() st.Step(); st.Reset(); + st.Bind(0,"FProxyPort"); + st.Bind(1,"8888"); + st.Bind(2,"The port that Freenet is listening for http connections on."); + st.Step(); + st.Reset(); + st.Bind(0,"MessageBase"); st.Bind(1,"fms"); st.Bind(2,"A unique string shared by all clients who want to communicate with each other. This should not be changed unless you want to create your own separate communications network."); @@ -530,7 +878,7 @@ void SetupDefaultOptions() st.Reset(); st.Bind(0,"MinLocalTrustListTrust"); - st.Bind(1,"51"); + st.Bind(1,"50"); st.Bind(2,"Specifies a local trust list trust level that a peer must have before its trust list will be included in the weighted average. Any peers below this number will be excluded from the results."); st.Step(); st.Reset(); @@ -541,6 +889,12 @@ void SetupDefaultOptions() st.Step(); st.Reset(); + st.Bind(0,"LocalTrustOverridesPeerTrust"); + st.Bind(1,"false"); + st.Bind(2,"Set to true if you want your local trust levels to override the peer levels when determining which identities you will poll."); + st.Step(); + st.Reset(); + st.Bind(0,"MessageDownloadMaxDaysBackward"); st.Bind(1,"5"); st.Bind(2,"The maximum number of days backward that messages will be downloaded from each identity"); @@ -571,12 +925,36 @@ void SetupDefaultOptions() st.Step(); st.Reset(); + st.Bind(0,"SaveMessagesFromNewBoards"); + st.Bind(1,"true"); + st.Bind(2,"Set to true to automatically save messages posted to new boards. Set to false to ignore messages to new boards."); + st.Step(); + st.Reset(); + st.Bind(0,"ChangeMessageTrustOnReply"); st.Bind(1,"0"); st.Bind(2,"How much the local message trust level of an identity should change when you reply to one of their messages."); st.Step(); st.Reset(); + st.Bind(0,"AddNewPostFromIdentities"); + st.Bind(1,"false"); + st.Bind(2,"Set to true to automatically create new identities when you send a message using a new name. If you set this to false, posting messages will fail until you manually create the identity."); + st.Step(); + st.Reset(); + + st.Bind(0,"DeleteMessagesOlderThan"); + st.Bind(1,"180"); + st.Bind(2,"Automatically delete messages older than this many days."); + st.Step(); + st.Reset(); + + st.Bind(0,"VacuumOnStartup"); + st.Bind(1,"false"); + st.Bind(2,"VACUUM the database every time FMS starts. This will defragment the free space in the database and create a smaller database file. Vacuuming the database can be CPU and disk intensive."); + st.Step(); + st.Reset(); + } void SetupLogFile() @@ -616,6 +994,9 @@ void SetupNetwork() void Shutdown() { + + LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"FMS starting shutdown"); + ThreadController::Instance()->ShutdownThreads(); ShutdownNetwork(); @@ -637,68 +1018,66 @@ void SigHandler(int signum) exit(0); } -/* -void ShutdownThreads(std::vector &threads) -{ - std::vector::iterator i; - for(i=threads.begin(); i!=threads.end(); i++) - { - (*i)->Cancel(); - } - - for(i=threads.begin(); i!=threads.end(); i++) - { - LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_DEBUG,"ShutdownThreads waiting for thread to exit."); - //(*i)->wait(); - (*i)->Join(); - delete (*i); - } - - threads.clear(); - -} - -void StartThreads(std::vector &threads) +void FixCapitalBoardNames() { - std::string startfreenet; - std::string startnntp; - std::string starthttp; - - if(Option::Instance()->Get("StartFreenetUpdater",startfreenet)==false) - { - startfreenet="true"; - Option::Instance()->Set("StartFreenetUpdater","true"); - } - - if(Option::Instance()->Get("StartNNTP",startnntp)==false) - { - startnntp="true"; - Option::Instance()->Set("StartNNTP","true"); - } + SQLite3DB::DB *db=SQLite3DB::DB::Instance(); - if(Option::Instance()->Get("StartHTTP",starthttp)==false) - { - starthttp="true"; - Option::Instance()->Set("StartHTTP","true"); - } + SQLite3DB::Statement st=db->Prepare("SELECT BoardID,BoardName FROM tblBoard WHERE BoardID NOT IN (SELECT BoardID FROM tblAdministrationBoard);"); + SQLite3DB::Statement st2=db->Prepare("SELECT BoardID FROM tblBoard WHERE BoardName=?;"); + SQLite3DB::Statement del=db->Prepare("DELETE FROM tblBoard WHERE BoardID=?;"); + SQLite3DB::Statement upd=db->Prepare("UPDATE tblBoard SET BoardName=? WHERE BoardID=?;"); + SQLite3DB::Statement upd2=db->Prepare("UPDATE tblMessage SET ReplyBoardID=? WHERE ReplyBoardID=?;"); + SQLite3DB::Statement upd3=db->Prepare("UPDATE tblMessageBoard SET BoardID=? WHERE BoardID=?;"); - if(startfreenet=="true") - { - PThread::Thread *t=new PThread::Thread(new FreenetMasterThread()); - threads.push_back(t); - } - - if(startnntp=="true") - { - PThread::Thread *t=new PThread::Thread(new NNTPListener()); - threads.push_back(t); - } - - if(starthttp=="true") + st.Step(); + while(st.RowReturned()) { - PThread::Thread *t=new PThread::Thread(new HTTPThread()); - threads.push_back(t); + int boardid=0; + int newboardid=0; + std::string name=""; + std::string lowername=""; + + st.ResultInt(0,boardid); + st.ResultText(1,name); + + lowername=name; + StringFunctions::LowerCase(lowername,lowername); + + if(name!=lowername) + { + st2.Bind(0,lowername); + st2.Step(); + + if(st2.RowReturned()) + { + st2.ResultInt(0,newboardid); + + upd2.Bind(0,newboardid); + upd2.Bind(1,boardid); + upd2.Step(); + upd2.Reset(); + + upd3.Bind(0,newboardid); + upd3.Bind(1,boardid); + upd3.Step(); + upd3.Reset(); + + del.Bind(0,boardid); + del.Step(); + del.Reset(); + } + else + { + upd.Bind(0,lowername); + upd.Bind(1,boardid); + upd.Step(); + upd.Reset(); + } + + st2.Reset(); + } + + st.Step(); } } -*/