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