version 0.2.17
[fms.git] / src / freenet / trustlistinserter.cpp
index 6cd8843..d316790 100644 (file)
@@ -21,7 +21,13 @@ void TrustListInserter::CheckForNeededInsert()
 {\r
        DateTime date;\r
        date.SetToGMTime();\r
-       date.Add(0,0,-1);\r
+       int currentday=date.GetDay();\r
+       date.Add(0,0,-6);\r
+       // insert trust lists every 6 hours - if 6 hours ago was different day then set to midnight of current day to insert list today ASAP\r
+       if(currentday!=date.GetDay())\r
+       {\r
+               date.Set(date.GetYear(),date.GetMonth(),currentday);\r
+       }\r
        SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID, PrivateKey FROM tblLocalIdentity WHERE PrivateKey IS NOT NULL AND PrivateKey <> '' AND PublishTrustList='true' AND InsertingTrustList='false' AND (LastInsertedTrustList<='"+date.Format("%Y-%m-%d %H:%M:%S")+"' OR LastInsertedTrustList IS NULL);");\r
 \r
        if(rs.Empty()==false)\r
@@ -66,23 +72,30 @@ const bool TrustListInserter::HandleMessage(FCPMessage &message)
 \r
                if(message.GetName()=="PutSuccessful")\r
                {\r
-                       m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false', LastInsertedTrustList='"+now.Format("%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");\r
-                       m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
-                       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListInserter::HandleMessage inserted TrustList xml");\r
+                       // non USK\r
+                       if(idparts[0]=="TrustListInserter")\r
+                       {\r
+                               m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false', LastInsertedTrustList='"+now.Format("%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");\r
+                               m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
+                               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListInserter::HandleMessage inserted TrustList xml");\r
+                       }\r
                        return true;\r
                }\r
 \r
-               if(message.GetName()=="PutFailed")\r
+               if(message.GetName()=="PutFailed" && idparts[0]=="TrustListInserter")\r
                {\r
-                       m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false' WHERE LocalIdentityID="+idparts[1]+";");\r
-                       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListInserter::HandleMessage failure inserting TrustList xml.  Code="+message["Code"]+" Description="+message["CodeDescription"]);\r
-                       \r
-                       // if code 9 (collision), then insert index into inserted table\r
-                       if(message["Code"]=="9")\r
+                       // non USK\r
+                       if(idparts[0]=="TrustListInserter")\r
                        {\r
-                               m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
-                       }\r
+                               m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false' WHERE LocalIdentityID="+idparts[1]+";");\r
+                               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListInserter::HandleMessage failure inserting TrustList xml.  Code="+message["Code"]+" Description="+message["CodeDescription"]);\r
                        \r
+                               // if code 9 (collision), then insert index into inserted table\r
+                               if(message["Code"]=="9")\r
+                               {\r
+                                       m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
+                               }\r
+                       }\r
                        return true;\r
                }\r
 \r
@@ -93,7 +106,7 @@ const bool TrustListInserter::HandleMessage(FCPMessage &message)
 \r
 void TrustListInserter::Initialize()\r
 {\r
-       Option::instance()->Get("MessageBase",m_messagebase);\r
+       Option::Instance()->Get("MessageBase",m_messagebase);\r
        m_lastchecked.SetToGMTime();\r
 }\r
 \r
@@ -130,21 +143,45 @@ void TrustListInserter::StartInsert(const long localidentityid, const std::strin
        int index;\r
        std::string indexstr;\r
        std::string localidentityidstr;\r
+       std::string messagetrustcomment="";\r
+       std::string trustlisttrustcomment="";\r
 \r
        now.SetToGMTime();\r
        date.SetToGMTime();\r
 \r
+       // insert all identities not in trust list already\r
+       m_db->Execute("INSERT INTO tblIdentityTrust(LocalIdentityID,IdentityID) SELECT LocalIdentityID,IdentityID FROM tblLocalIdentity,tblIdentity WHERE LocalIdentityID || '_' || IdentityID NOT IN (SELECT LocalIdentityID || '_' || IdentityID FROM tblIdentityTrust);");\r
+\r
        // build the xml file - we only want to add identities that we recently saw, otherwise we could be inserting a ton of identities\r
        date.Add(0,0,0,-20);    // identities seen in last 20 days\r
-       SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey, LocalMessageTrust, LocalTrustListTrust FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey<>'' AND LastSeen>=?;");\r
+       //SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey, LocalMessageTrust, LocalTrustListTrust, MessageTrustComment, TrustListTrustComment FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey<>'' AND LastSeen>=?;");\r
+       // we want to order by public key so we can't do identity correllation based on the sequence of identities in the list.\r
+       SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey, tblIdentityTrust.LocalMessageTrust, tblIdentityTrust.LocalTrustListTrust, tblIdentityTrust.MessageTrustComment, tblIdentityTrust.TrustListTrustComment FROM tblIdentity INNER JOIN tblIdentityTrust ON tblIdentity.IdentityID=tblIdentityTrust.IdentityID WHERE PublicKey IS NOT NULL AND PublicKey<>'' AND LastSeen>=? AND tblIdentityTrust.LocalIdentityID=? ORDER BY PublicKey;");\r
        st.Bind(0,date.Format("%Y-%m-%d"));\r
+       st.Bind(1,localidentityid);\r
        st.Step();\r
        while(st.RowReturned())\r
        {\r
                st.ResultText(0,publickey);\r
-               st.ResultInt(1,messagetrust);\r
-               st.ResultInt(2,trustlisttrust);\r
-               xml.AddTrust(publickey,messagetrust,trustlisttrust);\r
+               if(st.ResultNull(1)==false)\r
+               {\r
+                       st.ResultInt(1,messagetrust);\r
+               }\r
+               else\r
+               {\r
+                       messagetrust=-1;\r
+               }\r
+               if(st.ResultNull(2)==false)\r
+               {\r
+                       st.ResultInt(2,trustlisttrust);\r
+               }\r
+               else\r
+               {\r
+                       trustlisttrust=-1;\r
+               }\r
+               st.ResultText(3,messagetrustcomment);\r
+               st.ResultText(4,trustlisttrustcomment);\r
+               xml.AddTrust(publickey,messagetrust,trustlisttrust,messagetrustcomment,trustlisttrustcomment);\r
                st.Step();\r
        }\r
 \r
@@ -175,6 +212,18 @@ void TrustListInserter::StartInsert(const long localidentityid, const std::strin
        m_fcp->SendMessage(message);\r
        m_fcp->SendRaw(data.c_str(),data.size());\r
 \r
+       // insert to USK\r
+       message.Reset();\r
+       message.SetName("ClientPutComplexDir");\r
+       message["URI"]="USK"+privatekey.substr(3)+m_messagebase+"|"+now.Format("%Y.%m.%d")+"|TrustList/0/";\r
+       message["Identifier"]="TrustListInserterUSK|"+message["URI"];\r
+       message["DefaultName"]="TrustList.xml";\r
+       message["Files.0.Name"]="TrustList.xml";\r
+       message["Files.0.UplaodFrom"]="direct";\r
+       message["Files.0.DataLength"]=datasizestr;\r
+       m_fcp->SendMessage(message);\r
+       m_fcp->SendRaw(data.c_str(),data.size());\r
+\r
        m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='true' WHERE LocalIdentityID="+localidentityidstr+";");\r
 \r
 }\r