version 0.1.12
[fms.git] / src / freenet / periodicdbmaintenance.cpp
index 01a76c7..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
@@ -89,6 +90,49 @@ void PeriodicDBMaintenance::Do1DayMaintenance()
        // 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
 void PeriodicDBMaintenance::Process()\r