version 0.1.12
[fms.git] / src / freenet / periodicdbmaintenance.cpp
index 0febbbd..c615d60 100644 (file)
@@ -1,4 +1,5 @@
 #include "../../include/freenet/periodicdbmaintenance.h"\r
+#include "../../include/stringfunctions.h"\r
 \r
 #ifdef XMEM\r
        #include <xmem.h>\r
@@ -13,8 +14,9 @@ PeriodicDBMaintenance::PeriodicDBMaintenance()
        m_check1day.SetToGMTime();\r
 \r
        // set back times so they will do their first maintenance soon ( within the next hour ) - stagger so not all run at once\r
-       m_check1hour.Add(0,-45);\r
-       m_check6hours.Add(0,0,-5);\r
+       m_check30mins.Add(0,-5);\r
+       m_check1hour.Add(0,-42);\r
+       m_check6hours.Add(0,-1,-5);\r
        m_check1day.Add(0,0,-23);\r
 \r
 }\r
@@ -83,7 +85,53 @@ void PeriodicDBMaintenance::Do1DayMaintenance()
        date.SetToGMTime();\r
        date.Add(0,0,0,-2);\r
        m_db->Execute("DELETE FROM tblTrustListInserts WHERE Day<'"+date.Format("%Y-%m-%d")+"';");\r
-       m_db->Execute("DELETE FROM tblTrustListRequests WHERE Day<'"+date.Format("%Y-%m-%d")+"';"); \r
+       m_db->Execute("DELETE FROM tblTrustListRequests WHERE Day<'"+date.Format("%Y-%m-%d")+"';");\r
+\r
+       // delete trust lists from identities we aren't trusting anymore\r
+       m_db->Execute("DELETE FROM tblPeerTrust WHERE IdentityID NOT IN (SELECT IdentityID FROM tblIdentity WHERE (LocalTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalTrustListTrust')) AND (PeerTrustListTrust IS NULL OR PeerTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinPeerTrustListTrust')));");\r
+\r
+       // remove identityid from messages where the identity has been deleted\r
+       m_db->Execute("UPDATE tblMessage SET IdentityID=NULL WHERE IdentityID NOT IN (SELECT IdentityID FROM tblIdentity);");\r
+\r
+       // try to re-attach messages from identities that were previously deleted, but have been since re-added\r
+       // first get the names from messages that have a NULL IdentityID\r
+       SQLite3DB::Statement st=m_db->Prepare("SELECT FromName FROM tblMessage WHERE IdentityID IS NULL GROUP BY FromName;");\r
+       st.Step();\r
+       while(st.RowReturned())\r
+       {\r
+               std::string name="";\r
+               std::string publickey="";\r
+               int identityid=0;\r
+               st.ResultText(0,name);\r
+\r
+               std::vector<std::string> parts;\r
+               StringFunctions::Split(name,"@",parts);\r
+\r
+               // find identities with this name\r
+               SQLite3DB::Statement st2=m_db->Prepare("SELECT IdentityID,PublicKey FROM tblIdentity WHERE Name=?;");\r
+               st2.Bind(0,name);\r
+               st2.Step();\r
+               while(st2.RowReturned())\r
+               {\r
+                       publickey="";\r
+                       identityid=0;\r
+                       st2.ResultText(1,publickey);\r
+                       // check if public key matches 2nd part\r
+                       if(parts.size()>1 && publickey.find(parts[1])==4)\r
+                       {\r
+                               // we have the identity - so update the messages table with the identityid\r
+                               st2.ResultInt(0,identityid);\r
+\r
+                               SQLite3DB::Statement st3=m_db->Prepare("UPDATE tblMessage SET IdentityID=? WHERE Name=? AND IdentityID IS NULL;");\r
+                               st3.Bind(0,identityid);\r
+                               st3.Bind(1,name);\r
+                               st3.Step();\r
+                       }\r
+                       st2.Step();\r
+               }\r
+\r
+               st.Step();\r
+       }\r
 \r
 }\r
 \r