version 0.3.29
[fms.git] / src / freenet / trustlistrequester.cpp
index 90606dd..96d24df 100644 (file)
@@ -3,23 +3,26 @@
 #include "../../include/stringfunctions.h"\r
 #include "../../include/freenet/trustlistxml.h"\r
 \r
+#include <Poco/DateTimeFormatter.h>\r
+#include <Poco/Timespan.h>\r
+\r
 #ifdef XMEM\r
        #include <xmem.h>\r
 #endif\r
 \r
-TrustListRequester::TrustListRequester()\r
+TrustListRequester::TrustListRequester(SQLite3DB::DB *db):IIndexRequester<long>(db)\r
 {\r
        Initialize();\r
 }\r
 \r
-TrustListRequester::TrustListRequester(FCPv2 *fcp):IIndexRequester<long>(fcp)\r
+TrustListRequester::TrustListRequester(SQLite3DB::DB *db, FCPv2::Connection *fcp):IIndexRequester<long>(db,fcp)\r
 {\r
        Initialize();\r
 }\r
 \r
-const bool TrustListRequester::HandleAllData(FCPMessage &message)\r
+const bool TrustListRequester::HandleAllData(FCPv2::Message &message)\r
 {\r
-       DateTime now;\r
+       Poco::DateTime now;\r
        SQLite3DB::Statement st;\r
        SQLite3DB::Statement trustst;\r
        std::vector<std::string> idparts;\r
@@ -28,32 +31,85 @@ const bool TrustListRequester::HandleAllData(FCPMessage &message)
        TrustListXML xml;\r
        long identityid;\r
        long index;\r
+       int insertcount=0;\r
+       int dayinsertcount=0;\r
+       int previnsertcount=0;\r
 \r
-       now.SetToGMTime();\r
        StringFunctions::Split(message["Identifier"],"|",idparts);\r
        StringFunctions::Convert(message["DataLength"],datalength);\r
        StringFunctions::Convert(idparts[1],identityid);\r
        StringFunctions::Convert(idparts[2],index);\r
 \r
        // wait for all data to be received from connection\r
-       while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)\r
-       {\r
-               m_fcp->Update(1);\r
-       }\r
+       m_fcp->WaitForBytes(1000,datalength);\r
 \r
        // if we got disconnected- return immediately\r
-       if(m_fcp->Connected()==false)\r
+       if(m_fcp->IsConnected()==false)\r
        {\r
                return false;\r
        }\r
 \r
        // receive the file\r
-       data.resize(datalength);\r
-       m_fcp->ReceiveRaw(&data[0],datalength);\r
+       m_fcp->Receive(data,datalength);\r
+\r
+       // get count of identities added in last 24 hours\r
+       st=m_db->Prepare("SELECT COUNT(*) FROM tblIdentity WHERE DateAdded>=?;");\r
+       now-=Poco::Timespan(1,0,0,0,0);\r
+       st.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));\r
+       st.Step();\r
+       if(st.RowReturned())\r
+       {\r
+               if(st.ResultNull(0)==false)\r
+               {\r
+                       st.ResultInt(0,dayinsertcount);\r
+               }\r
+       }\r
+       else\r
+       {\r
+               m_log->error("TrustListRequester::HandleAllData couldn't get count of identities added in last 24 hours");\r
+       }\r
+\r
+       // get count of identities added more than 24 hours ago - if 0 then we will accept more than 100 identities now\r
+       st=m_db->Prepare("SELECT COUNT(*) FROM tblIdentity WHERE DateAdded<?;");\r
+       st.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));\r
+       st.Step();\r
+       if(st.RowReturned())\r
+       {\r
+               if(st.ResultNull(0)==false)\r
+               {\r
+                       st.ResultInt(0,previnsertcount);\r
+               }\r
+       }\r
+       else\r
+       {\r
+               m_log->error("TrustListRequester::HandleAllData couldn't get count of identities added more than 24 hours ago");\r
+       }\r
+\r
+       now=Poco::DateTime();\r
 \r
        // parse file into xml and update the database\r
-       if(xml.ParseXML(std::string(data.begin(),data.end()))==true)\r
+       if(data.size()>0 && 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
+               m_db->Execute("BEGIN;");\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
@@ -62,19 +118,34 @@ 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
+                       int id=-1;\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
+                               if(insertcount<50 && (dayinsertcount<100 || previnsertcount==0))\r
+                               {\r
+                                       idinsert.Bind(0,identity);\r
+                                       idinsert.Bind(1,Poco::DateTimeFormatter::format(now,"%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
+                               }\r
+                               insertcount++;\r
+                               dayinsertcount++;\r
                        }\r
                        else\r
                        {\r
@@ -83,31 +154,56 @@ const bool TrustListRequester::HandleAllData(FCPMessage &message)
                        st.Reset();\r
 \r
                        //insert trust for this identity\r
-                       trustst.Bind(0,identityid);\r
-                       trustst.Bind(1,id);\r
-                       if(xml.GetMessageTrust(i)==-1)\r
+                       if(id!=-1)\r
                        {\r
-                               trustst.Bind(2);\r
+                               trustst.Bind(0,identityid);\r
+                               trustst.Bind(1,id);\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
-                       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
-                       trustst.Step();\r
-                       trustst.Reset();\r
-\r
                }\r
+\r
                trustst.Finalize();\r
                st.Finalize();\r
 \r
+               if(insertcount>=50)\r
+               {\r
+                       m_log->warning("TrustListRequester::HandleAllData TrustList contained more than 50 new identities : "+message["Identifier"]);\r
+               }\r
+               if(dayinsertcount>=100 && previnsertcount>0)\r
+               {\r
+                       m_log->warning("TrustListRequester::HandleAllData TrustList would have inserted more than 100 new identities in the last 24 hours : "+message["Identifier"]);\r
+               }\r
+\r
                st=m_db->Prepare("INSERT INTO tblTrustListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");\r
                st.Bind(0,identityid);\r
                st.Bind(1,idparts[4]);\r
@@ -115,7 +211,9 @@ const bool TrustListRequester::HandleAllData(FCPMessage &message)
                st.Step();\r
                st.Finalize();\r
 \r
-               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListRequester::HandleAllData parsed TrustList XML file : "+message["Identifier"]);\r
+               m_db->Execute("COMMIT;");\r
+\r
+               m_log->debug("TrustListRequester::HandleAllData parsed TrustList XML file : "+message["Identifier"]);\r
        }\r
        else\r
        {\r
@@ -127,7 +225,7 @@ const bool TrustListRequester::HandleAllData(FCPMessage &message)
                st.Step();\r
                st.Finalize();\r
 \r
-               m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"TrustListRequester::HandleAllData error parsing TrustList XML file : "+message["Identifier"]);\r
+               m_log->error("TrustListRequester::HandleAllData error parsing TrustList XML file : "+message["Identifier"]);\r
        }\r
 \r
        // remove this identityid from request list\r
@@ -137,15 +235,13 @@ const bool TrustListRequester::HandleAllData(FCPMessage &message)
 \r
 }\r
 \r
-const bool TrustListRequester::HandleGetFailed(FCPMessage &message)\r
+const bool TrustListRequester::HandleGetFailed(FCPv2::Message &message)\r
 {\r
-       DateTime now;\r
        SQLite3DB::Statement st;\r
        std::vector<std::string> idparts;\r
        long identityid;\r
        long index;\r
 \r
-       now.SetToGMTime();\r
        StringFunctions::Split(message["Identifier"],"|",idparts);\r
        StringFunctions::Convert(idparts[1],identityid);\r
        StringFunctions::Convert(idparts[2],index);     \r
@@ -160,7 +256,7 @@ const bool TrustListRequester::HandleGetFailed(FCPMessage &message)
                st.Step();\r
                st.Finalize();\r
 \r
-               m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"TrustListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);\r
+               m_log->error("TrustListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);\r
        }\r
 \r
        // remove this identityid from request list\r
@@ -174,31 +270,31 @@ void TrustListRequester::Initialize()
 {\r
        std::string tempval="";\r
        m_fcpuniquename="TrustListRequester";\r
-       Option::Instance()->Get("MaxIdentityRequests",tempval);\r
-       StringFunctions::Convert(tempval,m_maxrequests);\r
+       m_maxrequests=0;\r
+       Option option(m_db);\r
+\r
+       option.GetInt("MaxIdentityRequests",m_maxrequests);\r
        if(m_maxrequests<1)\r
        {\r
                m_maxrequests=1;\r
-               m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxTrustListRequests is currently set at "+tempval+".  It must be 1 or greater.");\r
+               m_log->error("Option MaxTrustListRequests is currently set at "+tempval+".  It must be 1 or greater.");\r
        }\r
        if(m_maxrequests>100)\r
        {\r
-               m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxTrustListRequests is currently set at "+tempval+".  This value might be incorrectly configured.");\r
+               m_log->warning("Option MaxTrustListRequests is currently set at "+tempval+".  This value might be incorrectly configured.");\r
        }\r
-       m_tempdate.SetToGMTime();\r
+       m_tempdate=Poco::Timestamp();\r
 }\r
 \r
 void TrustListRequester::PopulateIDList()\r
 {\r
-       DateTime date;\r
+       Poco::DateTime date;\r
        int id;\r
        std::string sql;\r
 \r
-       date.SetToGMTime();\r
-\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 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+="WHERE Name IS NOT NULL AND Name <> '' AND PublicKey IS NOT NULL AND PublicKey <> '' AND LastSeen>='"+Poco::DateTimeFormatter::format(date,"%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
@@ -216,8 +312,8 @@ void TrustListRequester::PopulateIDList()
 \r
 void TrustListRequester::StartRequest(const long &identityid)\r
 {\r
-       DateTime now;\r
-       FCPMessage message;\r
+       Poco::DateTime now;\r
+       FCPv2::Message message;\r
        std::string publickey;\r
        int index;\r
        std::string indexstr;\r
@@ -231,10 +327,8 @@ void TrustListRequester::StartRequest(const long &identityid)
        {\r
                st.ResultText(0,publickey);\r
 \r
-               now.SetToGMTime();\r
-\r
                SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblTrustListRequests WHERE Day=? AND IdentityID=?;");\r
-               st2.Bind(0,now.Format("%Y-%m-%d"));\r
+               st2.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));\r
                st2.Bind(1,identityid);\r
                st2.Step();\r
 \r
@@ -253,12 +347,12 @@ void TrustListRequester::StartRequest(const long &identityid)
                StringFunctions::Convert(identityid,identityidstr);\r
 \r
                message.SetName("ClientGet");\r
-               message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";\r
+               message["URI"]=publickey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|TrustList|"+indexstr+".xml";\r
                message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];\r
                message["ReturnType"]="direct";\r
                message["MaxSize"]="1000000";                   // 1 MB\r
 \r
-               m_fcp->SendMessage(message);\r
+               m_fcp->Send(message);\r
 \r
                m_requesting.push_back(identityid);\r
        }\r