version 0.3.9
[fms.git] / src / freenet / introductionpuzzleinserter.cpp
index a381084..1fa13c0 100644 (file)
@@ -3,9 +3,12 @@
 #include "../../include/stringfunctions.h"\r
 #include "../../include/option.h"\r
 #include "../../include/freenet/captcha/simplecaptcha.h"\r
-#include "../../include/uuidgenerator.h"\r
 #include "../../include/base64.h"\r
 \r
+#include <Poco/DateTimeFormatter.h>\r
+#include <Poco/UUIDGenerator.h>\r
+#include <Poco/UUID.h>\r
+\r
 #ifdef XMEM\r
        #include <xmem.h>\r
 #endif\r
@@ -31,8 +34,7 @@ void IntroductionPuzzleInserter::CheckForNeededInsert()
                while(!rs.AtEnd())\r
                {\r
                        std::string localidentityidstr;\r
-                       DateTime now;\r
-                       now.SetToGMTime();\r
+                       Poco::DateTime now;\r
 \r
                        if(rs.GetField(0))\r
                        {\r
@@ -40,7 +42,7 @@ void IntroductionPuzzleInserter::CheckForNeededInsert()
                        }\r
 \r
                        // if this identity has any non-solved puzzles for today, we don't need to insert a new puzzle\r
-                       SQLite3DB::Recordset rs2=m_db->Query("SELECT UUID FROM tblIntroductionPuzzleInserts WHERE Day='"+now.Format("%Y-%m-%d")+"' AND FoundSolution='false' AND LocalIdentityID="+localidentityidstr+";");\r
+                       SQLite3DB::Recordset rs2=m_db->Query("SELECT UUID FROM tblIntroductionPuzzleInserts WHERE Day='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"' AND FoundSolution='false' AND LocalIdentityID="+localidentityidstr+";");\r
 \r
                        // identity doesn't have any non-solved puzzles for today - start a new insert\r
                        if(rs2.Empty()==true)\r
@@ -91,7 +93,7 @@ const bool IntroductionPuzzleInserter::HandlePutFailed(FCPMessage &message)
 \r
                RemoveFromInsertList(localidentityid);\r
 \r
-               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::HandlePutFailed failed to insert puzzle "+idparts[3]);\r
+               m_log->debug("IntroductionPuzzleInserter::HandlePutFailed failed to insert puzzle "+idparts[3]);\r
        }\r
 \r
        return true;\r
@@ -99,13 +101,12 @@ const bool IntroductionPuzzleInserter::HandlePutFailed(FCPMessage &message)
 \r
 const bool IntroductionPuzzleInserter::HandlePutSuccessful(FCPMessage &message)\r
 {\r
-       DateTime now;\r
+       Poco::DateTime now;\r
        SQLite3DB::Statement st;\r
        std::vector<std::string> idparts;\r
        long localidentityid;\r
        long insertindex;\r
 \r
-       now.SetToGMTime();\r
        StringFunctions::Split(message["Identifier"],"|",idparts);\r
 \r
        // non USK\r
@@ -122,14 +123,14 @@ const bool IntroductionPuzzleInserter::HandlePutSuccessful(FCPMessage &message)
                st.Finalize();\r
 \r
                st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedPuzzle=? WHERE LocalIdentityID=?;");\r
-               st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S"));\r
+               st.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));\r
                st.Bind(1,localidentityid);\r
                st.Step();\r
                st.Finalize();\r
 \r
                RemoveFromInsertList(localidentityid);\r
 \r
-               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::HandlePutSuccessful inserted puzzle "+idparts[3]);\r
+               m_log->debug("IntroductionPuzzleInserter::HandlePutSuccessful inserted puzzle "+idparts[3]);\r
        }\r
 \r
        return true;\r
@@ -142,11 +143,12 @@ void IntroductionPuzzleInserter::Initialize()
 \r
 const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid)\r
 {\r
-       DateTime now;\r
+       Poco::DateTime now;\r
        std::string idstring;\r
        long index=0;\r
        std::string indexstr;\r
-       UUIDGenerator uuid;\r
+       Poco::UUIDGenerator uuidgen;\r
+       Poco::UUID uuid;\r
        std::string messagebase;\r
        IntroductionPuzzleXML xml;\r
        std::string encodedpuzzle;\r
@@ -159,8 +161,7 @@ const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid)
        std::string keypart="";\r
 \r
        StringFunctions::Convert(localidentityid,idstring);\r
-       now.SetToGMTime();\r
-       SQLite3DB::Recordset rs=m_db->Query("SELECT MAX(InsertIndex) FROM tblIntroductionPuzzleInserts WHERE Day='"+now.Format("%Y-%m-%d")+"' AND LocalIdentityID="+idstring+";");\r
+       SQLite3DB::Recordset rs=m_db->Query("SELECT MAX(InsertIndex) FROM tblIntroductionPuzzleInserts WHERE Day='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"' AND LocalIdentityID="+idstring+";");\r
 \r
        if(rs.Empty() || rs.GetField(0)==NULL)\r
        {\r
@@ -172,58 +173,76 @@ const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid)
        }\r
        StringFunctions::Convert(index,indexstr);\r
 \r
-       SQLite3DB::Recordset rs2=m_db->Query("SELECT PrivateKey,PublicKey FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";");\r
-       if(rs2.Empty()==false && rs2.GetField(0)!=NULL)\r
+       if(index<50)\r
        {\r
-               privatekey=rs2.GetField(0);\r
-               if(rs2.GetField(1))\r
-               {\r
-                       publickey=rs2.GetField(1);\r
-               }\r
-               if(publickey.size()>=50)\r
+               SQLite3DB::Recordset rs2=m_db->Query("SELECT PrivateKey,PublicKey FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";");\r
+               if(rs2.Empty()==false && rs2.GetField(0)!=NULL)\r
                {\r
-                       // remove - and ~\r
-                       keypart=StringFunctions::Replace(StringFunctions::Replace(publickey.substr(4,43),"-",""),"~","");\r
+                       privatekey=rs2.GetField(0);\r
+                       if(rs2.GetField(1))\r
+                       {\r
+                               publickey=rs2.GetField(1);\r
+                       }\r
+                       if(publickey.size()>=50)\r
+                       {\r
+                               // remove - and ~\r
+                               keypart=StringFunctions::Replace(StringFunctions::Replace(publickey.substr(4,43),"-",""),"~","");\r
+                       }\r
                }\r
-       }\r
-\r
-       Option::Instance()->Get("MessageBase",messagebase);\r
-\r
-       GenerateCaptcha(encodedpuzzle,solutionstring);\r
-\r
-       xml.SetType("captcha");\r
-       xml.SetUUID(uuid.Generate()+"@"+keypart);\r
-       xml.SetPuzzleData(encodedpuzzle);\r
-       xml.SetMimeType("image/bmp");\r
 \r
-       xmldata=xml.GetXML();\r
-       StringFunctions::Convert(xmldata.size(),xmldatasizestr);\r
+               Option::Instance()->Get("MessageBase",messagebase);\r
 \r
-       message.SetName("ClientPut");\r
-       message["URI"]=privatekey+messagebase+"|"+now.Format("%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml";\r
-       message["Identifier"]=m_fcpuniquename+"|"+idstring+"|"+indexstr+"|"+xml.GetUUID()+"|"+message["URI"];\r
-       message["UploadFrom"]="direct";\r
-       message["DataLength"]=xmldatasizestr;\r
-       m_fcp->SendMessage(message);\r
-       m_fcp->SendRaw(xmldata.c_str(),xmldata.size());\r
+               GenerateCaptcha(encodedpuzzle,solutionstring);\r
 \r
-       // insert to USK\r
-       message.Reset();\r
-       message.SetName("ClientPutComplexDir");\r
-       message["URI"]="USK"+privatekey.substr(3)+messagebase+"|"+now.Format("%Y.%m.%d")+"|IntroductionPuzzle/0/";\r
-       message["Identifier"]=m_fcpuniquename+"USK|"+message["URI"];\r
-       message["DefaultName"]="IntroductionPuzzle.xml";\r
-       message["Files.0.Name"]="IntroductionPuzzle.xml";\r
-       message["Files.0.UplaodFrom"]="direct";\r
-       message["Files.0.DataLength"]=xmldatasizestr;\r
-       m_fcp->SendMessage(message);\r
-       m_fcp->SendRaw(xmldata.c_str(),xmldata.size());\r
-\r
-       m_db->Execute("INSERT INTO tblIntroductionPuzzleInserts(UUID,Type,MimeType,LocalIdentityID,PuzzleData,PuzzleSolution) VALUES('"+xml.GetUUID()+"','captcha','image/bmp',"+idstring+",'"+encodedpuzzle+"','"+solutionstring+"');");\r
-\r
-       m_inserting.push_back(localidentityid);\r
+               try\r
+               {\r
+                       uuid=uuidgen.createRandom();\r
+               }\r
+               catch(...)\r
+               {\r
+                       m_log->fatal("IntroductionPuzzleInserter::StartInsert could not create UUID");\r
+               }\r
 \r
-       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::StartInsert started insert for id "+idstring);\r
+               xml.SetType("captcha");\r
+               std::string uuidstr=uuid.toString();\r
+               StringFunctions::UpperCase(uuidstr,uuidstr);\r
+               xml.SetUUID(uuidstr+"@"+keypart);\r
+               xml.SetPuzzleData(encodedpuzzle);\r
+               xml.SetMimeType("image/bmp");\r
+\r
+               xmldata=xml.GetXML();\r
+               StringFunctions::Convert(xmldata.size(),xmldatasizestr);\r
+\r
+               message.SetName("ClientPut");\r
+               message["URI"]=privatekey+messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml";\r
+               message["Identifier"]=m_fcpuniquename+"|"+idstring+"|"+indexstr+"|"+xml.GetUUID()+"|"+message["URI"];\r
+               message["UploadFrom"]="direct";\r
+               message["DataLength"]=xmldatasizestr;\r
+               m_fcp->SendMessage(message);\r
+               m_fcp->SendRaw(xmldata.c_str(),xmldata.size());\r
+\r
+               // insert to USK\r
+               message.Reset();\r
+               message.SetName("ClientPutComplexDir");\r
+               message["URI"]="USK"+privatekey.substr(3)+messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y.%m.%d")+"|IntroductionPuzzle/0/";\r
+               message["Identifier"]=m_fcpuniquename+"USK|"+message["URI"];\r
+               message["DefaultName"]="IntroductionPuzzle.xml";\r
+               message["Files.0.Name"]="IntroductionPuzzle.xml";\r
+               message["Files.0.UplaodFrom"]="direct";\r
+               message["Files.0.DataLength"]=xmldatasizestr;\r
+               m_fcp->SendMessage(message);\r
+               m_fcp->SendRaw(xmldata.c_str(),xmldata.size());\r
+\r
+               m_db->Execute("INSERT INTO tblIntroductionPuzzleInserts(UUID,Type,MimeType,LocalIdentityID,PuzzleData,PuzzleSolution) VALUES('"+xml.GetUUID()+"','captcha','image/bmp',"+idstring+",'"+encodedpuzzle+"','"+solutionstring+"');");\r
+\r
+               m_inserting.push_back(localidentityid);\r
+\r
+               m_log->debug("IntroductionPuzzleInserter::StartInsert started insert for id "+idstring);\r
+       }\r
+       else\r
+       {\r
+               m_log->warning("IntroductionPuzzleInserter::StartInsert already inserted 50 puzzles for "+idstring);\r
+       }\r
 \r
        return true;\r
 \r