version 0.2.2
[fms.git] / src / freenet / trustlistrequester.cpp
index 1d20869..89da83a 100644 (file)
@@ -12,22 +12,11 @@ TrustListRequester::TrustListRequester()
        Initialize();\r
 }\r
 \r
-TrustListRequester::TrustListRequester(FCPv2 *fcp):IFCPConnected(fcp)\r
+TrustListRequester::TrustListRequester(FCPv2 *fcp):IIndexRequester<long>(fcp)\r
 {\r
        Initialize();\r
 }\r
 \r
-void TrustListRequester::FCPConnected()\r
-{\r
-       m_requesting.clear();\r
-       PopulateIDList();\r
-}\r
-\r
-void TrustListRequester::FCPDisconnected()\r
-{\r
-       \r
-}\r
-\r
 const bool TrustListRequester::HandleAllData(FCPMessage &message)\r
 {\r
        DateTime now;\r
@@ -65,6 +54,24 @@ const bool TrustListRequester::HandleAllData(FCPMessage &message)
        // parse file into xml and update the database\r
        if(xml.ParseXML(std::string(data.begin(),data.end()))==true)\r
        {\r
+               // find the identity name and public key of the identity publishing the trust list\r
+               std::string publisherid="";\r
+               st=m_db->Prepare("SELECT Name,PublicKey FROM tblIdentity WHERE IdentityID=?;");\r
+               st.Bind(0,identityid);\r
+               st.Step();\r
+               if(st.RowReturned())\r
+               {\r
+                       std::string publishername="";\r
+                       std::string publisherpublickey="";\r
+                       st.ResultText(0,publishername);\r
+                       st.ResultText(1,publisherpublickey);\r
+                       publisherid=publishername;\r
+                       if(publisherpublickey.size()>4)\r
+                       {\r
+                               publisherid+=publisherpublickey.substr(3,44);\r
+                       }\r
+               }\r
+               st.Finalize();\r
 \r
                // drop all existing peer trust from this identity - we will rebuild it when we go through each trust in the xml file\r
                st=m_db->Prepare("DELETE FROM tblPeerTrust WHERE IdentityID=?;");\r
@@ -73,19 +80,30 @@ const bool TrustListRequester::HandleAllData(FCPMessage &message)
                st.Finalize();\r
 \r
                st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey=?;");\r
-               trustst=m_db->Prepare("INSERT INTO tblPeerTrust(IdentityID,TargetIdentityID,MessageTrust,TrustListTrust) VALUES(?,?,?,?);");\r
+               trustst=m_db->Prepare("INSERT INTO tblPeerTrust(IdentityID,TargetIdentityID,MessageTrust,TrustListTrust,MessageTrustComment,TrustListTrustComment) VALUES(?,?,?,?,?,?);");\r
+               \r
+               SQLite3DB::Statement idinsert=m_db->Prepare("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES(?,?,?);");\r
+               \r
                // loop through all trust entries in xml and add to database if we don't already know them\r
                for(long i=0; i<xml.TrustCount(); i++)\r
                {\r
                        int id;\r
                        std::string identity;\r
+                       std::string messagetrustcomment="";\r
+                       std::string trustlisttrustcomment="";\r
                        identity=xml.GetIdentity(i);\r
 \r
                        st.Bind(0,identity);\r
                        st.Step();\r
                        if(st.RowReturned()==false)\r
                        {\r
-                               m_db->ExecuteInsert("INSERT INTO tblIdentity(PublicKey,DateAdded) VALUES('"+identity+"','"+now.Format("%Y-%m-%d %H:%M:%S")+"');",(long &)id);\r
+                               idinsert.Bind(0,identity);\r
+                               idinsert.Bind(1,now.Format("%Y-%m-%d %H:%M:%S"));\r
+                               idinsert.Bind(2,"trust list of "+publisherid);\r
+                               idinsert.Step(true);\r
+                               id=idinsert.GetLastInsertRowID();\r
+                               idinsert.Reset();\r
+                       //      m_db->ExecuteInsert("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES('"+identity+"','"+now.Format("%Y-%m-%d %H:%M:%S")+"');",(long &)id);\r
                        }\r
                        else\r
                        {\r
@@ -96,8 +114,35 @@ const bool TrustListRequester::HandleAllData(FCPMessage &message)
                        //insert trust for this identity\r
                        trustst.Bind(0,identityid);\r
                        trustst.Bind(1,id);\r
-                       trustst.Bind(2,xml.GetMessageTrust(i));\r
-                       trustst.Bind(3,xml.GetTrustListTrust(i));\r
+                       if(xml.GetMessageTrust(i)==-1)\r
+                       {\r
+                               trustst.Bind(2);\r
+                       }\r
+                       else\r
+                       {\r
+                               trustst.Bind(2,xml.GetMessageTrust(i));\r
+                       }\r
+                       if(xml.GetTrustListTrust(i)==-1)\r
+                       {\r
+                               trustst.Bind(3);\r
+                       }\r
+                       else\r
+                       {\r
+                               trustst.Bind(3,xml.GetTrustListTrust(i));\r
+                       }\r
+                       messagetrustcomment=xml.GetMessageTrustComment(i);\r
+                       trustlisttrustcomment=xml.GetTrustListTrustComment(i);\r
+                       // limit comments to 50 characters each\r
+                       if(messagetrustcomment.size()>50)\r
+                       {\r
+                               messagetrustcomment.erase(50);\r
+                       }\r
+                       if(trustlisttrustcomment.size()>50)\r
+                       {\r
+                               trustlisttrustcomment.erase(50);\r
+                       }\r
+                       trustst.Bind(4,messagetrustcomment);\r
+                       trustst.Bind(5,trustlisttrustcomment);\r
                        trustst.Step();\r
                        trustst.Reset();\r
 \r
@@ -167,44 +212,10 @@ const bool TrustListRequester::HandleGetFailed(FCPMessage &message)
 \r
 }\r
 \r
-const bool TrustListRequester::HandleMessage(FCPMessage &message)\r
-{\r
-\r
-       if(message["Identifier"].find("TrustListRequester")==0)\r
-       {\r
-               if(message.GetName()=="DataFound")\r
-               {\r
-                       return true;\r
-               }\r
-\r
-               if(message.GetName()=="AllData")\r
-               {\r
-                       return HandleAllData(message);\r
-               }\r
-\r
-               if(message.GetName()=="GetFailed")\r
-               {\r
-                       return HandleGetFailed(message);\r
-               }\r
-\r
-               if(message.GetName()=="IdentifierCollision")\r
-               {\r
-                       // remove one of the ids from the requesting list\r
-                       long identityid=0;\r
-                       std::vector<std::string> idparts;\r
-                       StringFunctions::Split(message["Identifier"],"|",idparts);\r
-                       StringFunctions::Convert(idparts[1],identityid);\r
-                       RemoveFromRequestList(identityid);\r
-                       return true;\r
-               }\r
-       }\r
-\r
-       return false;\r
-}\r
-\r
 void TrustListRequester::Initialize()\r
 {\r
        std::string tempval="";\r
+       m_fcpuniquename="TrustListRequester";\r
        Option::Instance()->Get("MaxIdentityRequests",tempval);\r
        StringFunctions::Convert(tempval,m_maxrequests);\r
        if(m_maxrequests<1)\r
@@ -216,7 +227,6 @@ void TrustListRequester::Initialize()
        {\r
                m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxTrustListRequests is currently set at "+tempval+".  This value might be incorrectly configured.");\r
        }\r
-       Option::Instance()->Get("MessageBase",m_messagebase);\r
        m_tempdate.SetToGMTime();\r
 }\r
 \r
@@ -230,8 +240,8 @@ void TrustListRequester::PopulateIDList()
 \r
        // select identities we want to query (we've seen them today and they are publishing trust list) - sort by their trust level (descending) with secondary sort on how long ago we saw them (ascending)\r
        sql="SELECT IdentityID FROM tblIdentity ";\r
-       sql+="WHERE PublicKey IS NOT NULL AND PublicKey <> '' AND LastSeen>='"+date.Format("%Y-%m-%d")+"' AND PublishTrustList='true' AND LocalTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalTrustListTrust') AND ( PeerTrustListTrust IS NULL OR PeerTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinPeerTrustListTrust') )";\r
-       sql+="ORDER BY LocalMessageTrust+LocalTrustListTrust DESC, LastSeen;";\r
+       sql+="WHERE Name IS NOT NULL AND Name <> '' AND PublicKey IS NOT NULL AND PublicKey <> '' AND LastSeen>='"+date.Format("%Y-%m-%d")+"' AND PublishTrustList='true' AND LocalTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalTrustListTrust') AND ( PeerTrustListTrust IS NULL OR PeerTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinPeerTrustListTrust') )";\r
+       sql+="ORDER BY LocalTrustListTrust DESC, LastSeen;";\r
 \r
        SQLite3DB::Statement st=m_db->Prepare(sql);\r
        st.Step();\r
@@ -246,63 +256,7 @@ void TrustListRequester::PopulateIDList()
        }\r
 }\r
 \r
-void TrustListRequester::Process()\r
-{\r
-       // max is the smaller of the config value or the total number of identities we will request from\r
-       long max=m_maxrequests>m_ids.size() ? m_ids.size() : m_maxrequests;\r
-\r
-       // try to keep up to max requests going\r
-       if(m_requesting.size()<max)\r
-       {\r
-               std::map<long,bool>::iterator i=m_ids.begin();\r
-               while(i!=m_ids.end() && (*i).second==true)\r
-               {\r
-                       i++;\r
-               }\r
-\r
-               if(i!=m_ids.end())\r
-               {\r
-                       StartRequest((*i).first);\r
-               }\r
-               else\r
-               {\r
-                       // we requested from all ids in the list, repopulate the list\r
-                       PopulateIDList();\r
-               }\r
-       }\r
-       // special case - if there were 0 identities on the list when we started then we will never get a chance to repopulate the list\r
-       // this will recheck for ids every minute\r
-       DateTime now;\r
-       now.SetToGMTime();\r
-       if(m_tempdate<(now-(1.0/1440.0)))\r
-       {\r
-               PopulateIDList();\r
-               m_tempdate=now;\r
-       }\r
-\r
-}\r
-\r
-void TrustListRequester::RegisterWithThread(FreenetMasterThread *thread)\r
-{\r
-       thread->RegisterFCPConnected(this);\r
-       thread->RegisterFCPMessageHandler(this);\r
-       thread->RegisterPeriodicProcessor(this);\r
-}\r
-\r
-void TrustListRequester::RemoveFromRequestList(const long identityid)\r
-{\r
-       std::vector<long>::iterator i=m_requesting.begin();\r
-       while(i!=m_requesting.end() && (*i)!=identityid)\r
-       {\r
-               i++;\r
-       }\r
-       if(i!=m_requesting.end())\r
-       {\r
-               m_requesting.erase(i);\r
-       }\r
-}\r
-\r
-void TrustListRequester::StartRequest(const long identityid)\r
+void TrustListRequester::StartRequest(const long &identityid)\r
 {\r
        DateTime now;\r
        FCPMessage message;\r
@@ -342,7 +296,7 @@ void TrustListRequester::StartRequest(const long identityid)
 \r
                message.SetName("ClientGet");\r
                message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";\r
-               message["Identifier"]="TrustListRequester|"+identityidstr+"|"+indexstr+"|"+message["URI"];\r
+               message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];\r
                message["ReturnType"]="direct";\r
                message["MaxSize"]="1000000";                   // 1 MB\r
 \r