version 0.3.20
[fms.git] / src / freenet / identityintroductionrequester.cpp
index 80a27c2..951e12d 100644 (file)
@@ -3,9 +3,13 @@
 #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
@@ -34,7 +38,7 @@ void IdentityIntroductionRequester::FCPDisconnected()
 const bool IdentityIntroductionRequester::HandleAllData(FCPMessage &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
@@ -57,10 +61,13 @@ const bool IdentityIntroductionRequester::HandleAllData(FCPMessage &message)
 \r
        // receive the file\r
        data.resize(datalength);\r
-       m_fcp->ReceiveRaw(&data[0],datalength);\r
+       if(data.size()>0)\r
+       {\r
+               m_fcp->ReceiveRaw(&data[0],datalength);\r
+       }\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,22 +88,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
-                               st=m_db->Prepare("INSERT INTO tblIdentity(PublicKey,DateAdded) VALUES(?,?);");\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
@@ -106,7 +114,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
@@ -129,7 +137,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
@@ -176,32 +184,30 @@ 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::Instance()->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
+       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
@@ -241,9 +247,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_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
@@ -274,7 +279,6 @@ 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
        SQLite3DB::Statement st=m_db->Prepare("SELECT Day, PuzzleSolution FROM tblIntroductionPuzzleInserts WHERE FoundSolution='false' AND UUID=?;");\r
@@ -287,9 +291,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
@@ -308,16 +313,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