version 0.3.29
[fms.git] / src / freenet / identityintroductionrequester.cpp
index 0e5e3dd..b9532f3 100644 (file)
@@ -3,19 +3,23 @@
 #include "../../include/freenet/freenetssk.h"\r
 #include "../../include/option.h"\r
 #include "../../include/stringfunctions.h"\r
-#include "../../include/xyssl/sha1.h"\r
 #include "../../include/hex.h"\r
 \r
+#include <Poco/DateTime.h>\r
+#include <Poco/Timestamp.h>\r
+#include <Poco/DateTimeFormatter.h>\r
+#include <Poco/SHA1Engine.h>\r
+\r
 #ifdef XMEM\r
        #include <xmem.h>\r
 #endif\r
 \r
-IdentityIntroductionRequester::IdentityIntroductionRequester()\r
+IdentityIntroductionRequester::IdentityIntroductionRequester(SQLite3DB::DB *db):IDatabase(db)\r
 {\r
        Initialize();\r
 }\r
 \r
-IdentityIntroductionRequester::IdentityIntroductionRequester(FCPv2 *fcp):IFCPConnected(fcp)\r
+IdentityIntroductionRequester::IdentityIntroductionRequester(SQLite3DB::DB *db, FCPv2::Connection *fcp):IDatabase(db),IFCPConnected(fcp)\r
 {\r
        Initialize();\r
 }\r
@@ -31,10 +35,10 @@ void IdentityIntroductionRequester::FCPDisconnected()
        \r
 }\r
 \r
-const bool IdentityIntroductionRequester::HandleAllData(FCPMessage &message)\r
+const bool IdentityIntroductionRequester::HandleAllData(FCPv2::Message &message)\r
 {\r
        FreenetSSK ssk;\r
-       DateTime date;\r
+       Poco::DateTime date;\r
        std::vector<std::string> idparts;\r
        std::vector<char> data;\r
        long datalength;\r
@@ -44,23 +48,19 @@ const bool IdentityIntroductionRequester::HandleAllData(FCPMessage &message)
        StringFunctions::Convert(message["DataLength"],datalength);\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
                ssk.SetPublicKey(xml.GetIdentity());\r
@@ -81,23 +81,23 @@ const bool IdentityIntroductionRequester::HandleAllData(FCPMessage &message)
                        {\r
                                // we don't already know about this id - add it\r
                                st.Finalize();\r
-                               date.SetToGMTime();\r
+                               date=Poco::Timestamp();\r
                                st=m_db->Prepare("INSERT INTO tblIdentity(PublicKey,DateAdded,AddedMethod) VALUES(?,?,?);");\r
                                st.Bind(0,xml.GetIdentity());\r
-                               st.Bind(1,date.Format("%Y-%m-%d %H:%M:%S"));\r
+                               st.Bind(1,Poco::DateTimeFormatter::format(date,"%Y-%m-%d %H:%M:%S"));\r
                                st.Bind(2,"solved captcha");\r
                                st.Step();\r
                        }\r
                        st.Finalize();\r
 \r
-                       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IdentityIntroductionRequester::HandleAddData parsed a valid identity.");\r
+                       m_log->debug("IdentityIntroductionRequester::HandleAddData parsed a valid identity.");\r
                }\r
                else\r
                {\r
-                       m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IdentityIntroductionRequester::HandleAllData parsed, public SSK key was not valid.");\r
+                       m_log->error("IdentityIntroductionRequester::HandleAllData parsed, public SSK key was not valid.");\r
                }\r
 \r
-               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IdentityIntroductionRequester::HandleAllData parsed IdentityIntroduction XML file : "+message["Identifier"]);\r
+               m_log->debug("IdentityIntroductionRequester::HandleAllData parsed IdentityIntroduction XML file : "+message["Identifier"]);\r
        }\r
        else\r
        {\r
@@ -107,7 +107,7 @@ const bool IdentityIntroductionRequester::HandleAllData(FCPMessage &message)
                st.Step();\r
                st.Finalize();          \r
 \r
-               m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"IdentityIntroductionRequester::HandleAllData error parsing IdentityIntroduction XML file : "+message["Identifier"]);\r
+               m_log->error("IdentityIntroductionRequester::HandleAllData error parsing IdentityIntroduction XML file : "+message["Identifier"]);\r
        }\r
 \r
        // remove UUID from request list\r
@@ -116,7 +116,7 @@ const bool IdentityIntroductionRequester::HandleAllData(FCPMessage &message)
        return true;\r
 }\r
 \r
-const bool IdentityIntroductionRequester::HandleGetFailed(FCPMessage &message)\r
+const bool IdentityIntroductionRequester::HandleGetFailed(FCPv2::Message &message)\r
 {\r
        std::vector<std::string> idparts;\r
 \r
@@ -130,7 +130,7 @@ const bool IdentityIntroductionRequester::HandleGetFailed(FCPMessage &message)
                st.Step();\r
                st.Finalize();\r
 \r
-               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IdentityIntroductionRequester::HandleAllData Fatal GetFailed for "+message["Identifier"]);\r
+               m_log->debug("IdentityIntroductionRequester::HandleAllData Fatal GetFailed for "+message["Identifier"]);\r
        }\r
 \r
        // remove UUID from request list\r
@@ -139,7 +139,7 @@ const bool IdentityIntroductionRequester::HandleGetFailed(FCPMessage &message)
        return true;\r
 }\r
 \r
-const bool IdentityIntroductionRequester::HandleMessage(FCPMessage &message)\r
+const bool IdentityIntroductionRequester::HandleMessage(FCPv2::Message &message)\r
 {\r
 \r
        if(message["Identifier"].find("IdentityIntroductionRequester")==0)\r
@@ -177,32 +177,31 @@ const bool IdentityIntroductionRequester::HandleMessage(FCPMessage &message)
 \r
 void IdentityIntroductionRequester::Initialize()\r
 {\r
-       std::string tempval="";\r
-       Option::Instance()->Get("MaxIdentityIntroductionRequests",tempval);\r
-       StringFunctions::Convert(tempval,m_maxrequests);\r
+       m_maxrequests=0;\r
+       Option option(m_db);\r
+       option.GetInt("MaxIdentityIntroductionRequests",m_maxrequests);\r
        if(m_maxrequests<1)\r
        {\r
                m_maxrequests=1;\r
-               m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxIdentityIntroductionRequests is currently set at "+tempval+".  It must be 1 or greater.");\r
+               m_log->error("Option MaxIdentityIntroductionRequests is currently less than 1.  It must be 1 or greater.");\r
        }\r
        if(m_maxrequests>100)\r
        {\r
-               m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxIdentityIntroductionRequests is currently set at "+tempval+".  This value might be incorrectly configured.");\r
+               m_log->warning("Option MaxIdentityIntroductionRequests 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
+       option.Get("MessageBase",m_messagebase);\r
+       m_tempdate=Poco::Timestamp();\r
 }\r
 \r
 void IdentityIntroductionRequester::PopulateIDList()\r
 {\r
-       DateTime date;\r
+       Poco::DateTime date;\r
        int id;\r
 \r
-       date.SetToGMTime();\r
-       date.Add(0,0,0,-1);\r
+       date-=Poco::Timespan(1,0,0,0,0);\r
 \r
        // get all identities that have unsolved puzzles from yesterday or today\r
-       SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID FROM tblIntroductionPuzzleInserts WHERE Day>='"+date.Format("%Y-%m-%d")+"' AND FoundSolution='false' GROUP BY LocalIdentityID;");\r
+       SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID FROM tblIntroductionPuzzleInserts WHERE Day>='"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"' AND FoundSolution='false' GROUP BY LocalIdentityID;");\r
        st.Step();\r
 \r
        m_ids.clear();\r
@@ -242,9 +241,8 @@ void IdentityIntroductionRequester::Process()
        }\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
+       Poco::DateTime now;\r
+       if(m_ids.size()==0 && m_tempdate<(now-Poco::Timespan(0,0,1,0,0)))\r
        {\r
                PopulateIDList();\r
                m_tempdate=now;\r
@@ -275,9 +273,8 @@ void IdentityIntroductionRequester::StartRequest(const std::string &UUID)
 {\r
        std::string day;\r
        std::string solution;\r
-       std::vector<unsigned char> solutionhash;\r
        std::string encodedhash;\r
-       FCPMessage message;\r
+       FCPv2::Message message;\r
        SQLite3DB::Statement st=m_db->Prepare("SELECT Day, PuzzleSolution FROM tblIntroductionPuzzleInserts WHERE FoundSolution='false' AND UUID=?;");\r
        st.Bind(0,UUID);\r
        st.Step();\r
@@ -288,9 +285,10 @@ void IdentityIntroductionRequester::StartRequest(const std::string &UUID)
                st.ResultText(1,solution);\r
 \r
                // get the hash of the solution\r
-               solutionhash.resize(20);\r
-               sha1((unsigned char *)solution.c_str(),solution.size(),&solutionhash[0]);\r
-               Hex::Encode(solutionhash,encodedhash);\r
+               Poco::SHA1Engine sha1;\r
+               sha1.update(solution);\r
+               encodedhash=Poco::DigestEngine::digestToHex(sha1.digest());\r
+               StringFunctions::UpperCase(encodedhash,encodedhash);\r
 \r
                //start request for the solution\r
                message.SetName("ClientGet");\r
@@ -299,7 +297,7 @@ void IdentityIntroductionRequester::StartRequest(const std::string &UUID)
                message["ReturnType"]="direct";\r
                message["MaxSize"]="10000";\r
 \r
-               m_fcp->SendMessage(message);\r
+               m_fcp->Send(message);\r
 \r
                m_requesting.push_back(UUID);\r
 \r
@@ -309,16 +307,15 @@ void IdentityIntroductionRequester::StartRequest(const std::string &UUID)
 \r
 void IdentityIntroductionRequester::StartRequests(const long localidentityid)\r
 {\r
-       DateTime date;\r
+       Poco::DateTime date;\r
        std::string localidentityidstr;\r
        std::string uuid;\r
 \r
-       date.SetToGMTime();\r
-       date.Add(0,0,0,-1);\r
+       date-=Poco::Timespan(1,0,0,0,0);\r
        StringFunctions::Convert(localidentityid,localidentityidstr);\r
 \r
        // get all non-solved puzzles from yesterday and today for this identity\r
-       SQLite3DB::Statement st=m_db->Prepare("SELECT UUID FROM tblIntroductionPuzzleInserts WHERE LocalIdentityID="+localidentityidstr+" AND Day>='"+date.Format("%Y-%m-%d")+"' AND FoundSolution='false';");\r
+       SQLite3DB::Statement st=m_db->Prepare("SELECT UUID FROM tblIntroductionPuzzleInserts WHERE LocalIdentityID="+localidentityidstr+" AND Day>='"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"' AND FoundSolution='false';");\r
        st.Step();\r
 \r
        // start requests for all non-solved puzzles\r