version 0.3.29
[fms.git] / src / freenet / introductionpuzzlerequester.cpp
index 03d4a41..9f1ee76 100644 (file)
@@ -2,35 +2,31 @@
 #include "../../include/freenet/introductionpuzzlexml.h"\r
 #include "../../include/option.h"\r
 #include "../../include/stringfunctions.h"\r
+#include "../../include/bitmapvalidator.h"\r
+#include "../../include/base64.h"\r
+\r
+#include <Poco/DateTime.h>\r
+#include <Poco/Timespan.h>\r
+#include <Poco/Timestamp.h>\r
+#include <Poco/DateTimeFormatter.h>\r
 \r
 #ifdef XMEM\r
        #include <xmem.h>\r
 #endif\r
 \r
-IntroductionPuzzleRequester::IntroductionPuzzleRequester()\r
+IntroductionPuzzleRequester::IntroductionPuzzleRequester(SQLite3DB::DB *db):IIndexRequester<long>(db)\r
 {\r
        Initialize();\r
 }\r
 \r
-IntroductionPuzzleRequester::IntroductionPuzzleRequester(FCPv2 *fcp):IFCPConnected(fcp)\r
+IntroductionPuzzleRequester::IntroductionPuzzleRequester(SQLite3DB::DB *db, FCPv2::Connection *fcp):IIndexRequester<long>(db,fcp)\r
 {\r
        Initialize();\r
 }\r
 \r
-void IntroductionPuzzleRequester::FCPConnected()\r
-{\r
-       m_requesting.clear();\r
-       PopulateIDList();\r
-}\r
-\r
-void IntroductionPuzzleRequester::FCPDisconnected()\r
-{\r
-       \r
-}\r
-\r
-const bool IntroductionPuzzleRequester::HandleAllData(FCPMessage &message)\r
+const bool IntroductionPuzzleRequester::HandleAllData(FCPv2::Message &message)\r
 {\r
-       DateTime now;\r
+       Poco::DateTime now;\r
        SQLite3DB::Statement st;\r
        std::vector<std::string> idparts;\r
        long datalength;\r
@@ -38,48 +34,102 @@ const bool IntroductionPuzzleRequester::HandleAllData(FCPMessage &message)
        IntroductionPuzzleXML xml;\r
        long identityid;\r
        long index;\r
+       bool validmessage=true;\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
        // 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
 \r
-               // TODO - check if last part of UUID matches public key of identity who inserted it\r
+               // check if last part of UUID matches first part of public key of identity who inserted it\r
+               st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");\r
+               st.Bind(0,identityid);\r
+               st.Step();\r
+               if(st.RowReturned())\r
+               {\r
+                       std::vector<std::string> uuidparts;\r
+                       std::vector<std::string> keyparts;\r
+                       std::string keypart="";\r
+                       std::string publickey="";\r
+\r
+                       st.ResultText(0,publickey);\r
+\r
+                       StringFunctions::SplitMultiple(publickey,"@,",keyparts);\r
+                       StringFunctions::SplitMultiple(xml.GetUUID(),"@",uuidparts);\r
+\r
+                       if(uuidparts.size()>1 && keyparts.size()>1)\r
+                       {\r
+                               keypart=StringFunctions::Replace(StringFunctions::Replace(keyparts[1],"-",""),"~","");\r
+                               if(keypart!=uuidparts[1])\r
+                               {\r
+                                       m_log->error("IntroductionPuzzleRequester::HandleAllData UUID in IntroductionPuzzle doesn't match public key of identity : "+message["Identifier"]);\r
+                                       validmessage=false;\r
+                               }\r
+                       }\r
+                       else\r
+                       {\r
+                               m_log->error("IntroductionPuzzleRequester::HandleAllData Error with identity's public key or UUID : "+message["Identifier"]);\r
+                               validmessage=false;\r
+                       }\r
+\r
+               }\r
+               else\r
+               {\r
+                       m_log->error("IntroductionPuzzleRequester::HandleAllData Error couldn't find identity : "+message["Identifier"]);\r
+                       validmessage=false;\r
+               }\r
+\r
+               // we can only validate bitmaps for now\r
+               BitmapValidator val;\r
+               val.SetMax(200,200);\r
+               std::vector<unsigned char> puzzledata;\r
+               Base64::Decode(xml.GetPuzzleData(),puzzledata);\r
+               if(xml.GetMimeType()!="image/bmp" || val.Validate(puzzledata)==false)\r
+               {\r
+                       m_log->error("IntroductionPuzzleRequester::HandleAllData received bad mime type and/or data for "+message["Identifier"]);\r
+                       validmessage=false;\r
+               }\r
 \r
                st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found,UUID,Type,MimeType,PuzzleData) VALUES(?,?,?,?,?,?,?,?);");\r
                st.Bind(0,identityid);\r
                st.Bind(1,idparts[4]);\r
                st.Bind(2,index);\r
-               st.Bind(3,"true");\r
-               st.Bind(4,xml.GetUUID());\r
-               st.Bind(5,xml.GetType());\r
-               st.Bind(6,xml.GetMimeType());\r
-               st.Bind(7,xml.GetPuzzleData());\r
+               if(validmessage)\r
+               {\r
+                       st.Bind(3,"true");\r
+                       st.Bind(4,xml.GetUUID());\r
+                       st.Bind(5,xml.GetType());\r
+                       st.Bind(6,xml.GetMimeType());\r
+                       st.Bind(7,xml.GetPuzzleData());\r
+               }\r
+               else\r
+               {\r
+                       st.Bind(3,"false");\r
+                       st.Bind(4);\r
+                       st.Bind(5);\r
+                       st.Bind(6);\r
+                       st.Bind(7);\r
+               }\r
                st.Step();\r
                st.Finalize();\r
 \r
-               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleRequester::HandleAllData parsed IntroductionPuzzle XML file : "+message["Identifier"]);\r
+               m_log->debug("IntroductionPuzzleRequester::HandleAllData parsed IntroductionPuzzle XML file : "+message["Identifier"]);\r
        }\r
        else\r
        {\r
@@ -91,7 +141,7 @@ const bool IntroductionPuzzleRequester::HandleAllData(FCPMessage &message)
                st.Step();\r
                st.Finalize();\r
 \r
-               m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleAllData error parsing IntroductionPuzzle XML file : "+message["Identifier"]);\r
+               m_log->error("IntroductionPuzzleRequester::HandleAllData error parsing IntroductionPuzzle XML file : "+message["Identifier"]);\r
        }\r
 \r
        // remove this identityid from request list\r
@@ -101,15 +151,13 @@ const bool IntroductionPuzzleRequester::HandleAllData(FCPMessage &message)
 \r
 }\r
 \r
-const bool IntroductionPuzzleRequester::HandleGetFailed(FCPMessage &message)\r
+const bool IntroductionPuzzleRequester::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
@@ -124,7 +172,7 @@ const bool IntroductionPuzzleRequester::HandleGetFailed(FCPMessage &message)
                st.Step();\r
                st.Finalize();\r
 \r
-               m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IntroductionPuzzleRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);\r
+               m_log->error("IntroductionPuzzleRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);\r
        }\r
 \r
        // remove this identityid from request list\r
@@ -134,62 +182,26 @@ const bool IntroductionPuzzleRequester::HandleGetFailed(FCPMessage &message)
 \r
 }\r
 \r
-const bool IntroductionPuzzleRequester::HandleMessage(FCPMessage &message)\r
-{\r
-\r
-       if(message["Identifier"].find("IntroductionPuzzleRequester")==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 IntroductionPuzzleRequester::Initialize()\r
 {\r
-       std::string tempval="";\r
-       Option::Instance()->Get("MaxIntroductionPuzzleRequests",tempval);\r
-       StringFunctions::Convert(tempval,m_maxrequests);\r
+       m_fcpuniquename="IntroductionPuzzleRequester";\r
+       m_maxrequests=0;\r
+       Option option(m_db);\r
+       option.GetInt("MaxIntroductionPuzzleRequests",m_maxrequests);\r
        if(m_maxrequests<1)\r
        {\r
                m_maxrequests=1;\r
-               m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxIntroductionPuzzleRequests is currently set at "+tempval+".  It must be 1 or greater.");\r
+               m_log->error("Option MaxIntroductionPuzzleRequests is currently set at less than 1.  It must be 1 or greater.");\r
        }\r
        if(m_maxrequests>100)\r
        {\r
-               m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxIntroductionPuzzleRequests is currently set at "+tempval+".  This value might be incorrectly configured.");\r
+               m_log->warning("Option MaxIntroductionPuzzleRequests is currently set at more than 100.  This value might be incorrectly configured.");\r
        }\r
-       Option::Instance()->Get("MessageBase",m_messagebase);\r
-       m_tempdate.SetToGMTime();\r
 }\r
 \r
 void IntroductionPuzzleRequester::PopulateIDList()\r
 {\r
-       DateTime now;\r
+       Poco::DateTime now;\r
        int id;\r
        std::string limitnum="30";\r
 \r
@@ -202,10 +214,8 @@ void IntroductionPuzzleRequester::PopulateIDList()
        }\r
        st.Finalize();\r
 \r
-       now.SetToGMTime();\r
-\r
-       // select identities that aren't single use and have been seen today ( order by trust DESC and limit to limitnum )\r
-       st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey <> '' AND SingleUse='false' AND LastSeen>='"+now.Format("%Y-%m-%d")+"' ORDER BY LocalMessageTrust DESC LIMIT 0,"+limitnum+";");\r
+       // select identities that aren't single use, are publishing a trust list, and have been seen today ( order by trust DESC and limit to limitnum )\r
+       st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublishTrustList='true' AND PublicKey IS NOT NULL AND PublicKey <> '' AND SingleUse='false' AND (LocalTrustListTrust IS NULL OR LocalTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalTrustListTrust')) AND LastSeen>='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"' ORDER BY LocalMessageTrust DESC LIMIT 0,"+limitnum+";");\r
        st.Step();\r
 \r
        m_ids.clear();\r
@@ -218,65 +228,10 @@ void IntroductionPuzzleRequester::PopulateIDList()
        }\r
 }\r
 \r
-void IntroductionPuzzleRequester::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_ids.size()==0 && m_tempdate<(now-(1.0/1440.0)))\r
-       {\r
-               PopulateIDList();\r
-               m_tempdate=now;\r
-       }\r
-}\r
-\r
-void IntroductionPuzzleRequester::RegisterWithThread(FreenetMasterThread *thread)\r
-{\r
-       thread->RegisterFCPConnected(this);\r
-       thread->RegisterFCPMessageHandler(this);\r
-       thread->RegisterPeriodicProcessor(this);\r
-}\r
-\r
-void IntroductionPuzzleRequester::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 IntroductionPuzzleRequester::StartRequest(const long identityid)\r
+void IntroductionPuzzleRequester::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
@@ -290,10 +245,10 @@ void IntroductionPuzzleRequester::StartRequest(const long identityid)
        {\r
                st.ResultText(0,publickey);\r
 \r
-               now.SetToGMTime();\r
+               now=Poco::Timestamp();\r
 \r
                SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblIntroductionPuzzleRequests 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
@@ -312,12 +267,12 @@ void IntroductionPuzzleRequester::StartRequest(const long identityid)
                StringFunctions::Convert(identityid,identityidstr);\r
 \r
                message.SetName("ClientGet");\r
-               message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml";\r
-               message["Identifier"]="IntroductionPuzzleRequester|"+identityidstr+"|"+indexstr+"|"+message["URI"];\r
+               message["URI"]=publickey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|IntroductionPuzzle|"+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