version 0.3.29
[fms.git] / src / freenet / introductionpuzzleinserter.cpp
index e422d29..000d973 100644 (file)
@@ -3,6 +3,10 @@
 #include "../../include/stringfunctions.h"\r
 #include "../../include/option.h"\r
 #include "../../include/freenet/captcha/simplecaptcha.h"\r
+#ifdef ALTERNATE_CAPTCHA\r
+#include "../../include/freenet/captcha/alternatecaptcha1.h"\r
+#include "../../include/freenet/captcha/alternatecaptcha2.h"\r
+#endif\r
 #include "../../include/base64.h"\r
 \r
 #include <Poco/DateTimeFormatter.h>\r
        #include <xmem.h>\r
 #endif\r
 \r
-IntroductionPuzzleInserter::IntroductionPuzzleInserter():IIndexInserter<long>()\r
+IntroductionPuzzleInserter::IntroductionPuzzleInserter(SQLite3DB::DB *db):IIndexInserter<long>(db)\r
 {\r
        Initialize();\r
 }\r
 \r
-IntroductionPuzzleInserter::IntroductionPuzzleInserter(FCPv2 *fcp):IIndexInserter<long>(fcp)\r
+IntroductionPuzzleInserter::IntroductionPuzzleInserter(SQLite3DB::DB *db, FCPv2::Connection *fcp):IIndexInserter<long>(db,fcp)\r
 {\r
        Initialize();\r
 }\r
@@ -33,8 +37,13 @@ void IntroductionPuzzleInserter::CheckForNeededInsert()
                \r
                while(!rs.AtEnd())\r
                {\r
-                       std::string localidentityidstr;\r
+                       int localidentityid=0;\r
+                       std::string localidentityidstr="";\r
                        Poco::DateTime now;\r
+                       float minutesbetweeninserts=0;\r
+                       minutesbetweeninserts=1440.0/(float)m_maxpuzzleinserts;\r
+                       Poco::DateTime lastinsert=now;\r
+                       lastinsert-=Poco::Timespan(0,0,minutesbetweeninserts,0,0);\r
 \r
                        if(rs.GetField(0))\r
                        {\r
@@ -47,7 +56,16 @@ void IntroductionPuzzleInserter::CheckForNeededInsert()
                        // identity doesn't have any non-solved puzzles for today - start a new insert\r
                        if(rs2.Empty()==true)\r
                        {\r
-                               StartInsert(rs.GetInt(0));\r
+                               // make sure we are on the next day or the appropriate amount of time has elapsed since the last insert\r
+                               if(m_lastinserted.find(rs.GetInt(0))==m_lastinserted.end() || m_lastinserted[rs.GetInt(0)]<=lastinsert || m_lastinserted[rs.GetInt(0)].day()!=now.day())\r
+                               {\r
+                                       StartInsert(rs.GetInt(0));\r
+                                       m_lastinserted[rs.GetInt(0)]=now;\r
+                               }\r
+                               else\r
+                               {\r
+                                       m_log->trace("IntroductionPuzzleInserter::CheckForNeededInsert waiting to insert puzzle for "+localidentityidstr);\r
+                               }\r
                        }\r
 \r
                        rs.Next();\r
@@ -57,13 +75,26 @@ void IntroductionPuzzleInserter::CheckForNeededInsert()
 \r
 void IntroductionPuzzleInserter::GenerateCaptcha(std::string &encodeddata, std::string &solution)\r
 {\r
-       SimpleCaptcha captcha;\r
+       ICaptcha *cap=0;\r
+#ifdef ALTERNATE_CAPTCHA\r
+       if(rand()%2==0)\r
+       {\r
+               cap=new AlternateCaptcha1();\r
+       }\r
+       else\r
+       {\r
+               cap=new AlternateCaptcha2();\r
+       }\r
+       m_log->trace("IntroductionPuzzleInserter::GenerateCaptcha using alternate captcha generator");\r
+#else\r
+       cap=new SimpleCaptcha();\r
+#endif\r
        std::vector<unsigned char> puzzle;\r
        std::vector<unsigned char> puzzlesolution;\r
 \r
-       captcha.Generate();\r
-       captcha.GetPuzzle(puzzle);\r
-       captcha.GetSolution(puzzlesolution);\r
+       cap->Generate();\r
+       cap->GetPuzzle(puzzle);\r
+       cap->GetSolution(puzzlesolution);\r
 \r
        encodeddata.clear();\r
        solution.clear();\r
@@ -71,9 +102,11 @@ void IntroductionPuzzleInserter::GenerateCaptcha(std::string &encodeddata, std::
        Base64::Encode(puzzle,encodeddata);\r
        solution.insert(solution.begin(),puzzlesolution.begin(),puzzlesolution.end());\r
 \r
+       delete cap;\r
+\r
 }\r
 \r
-const bool IntroductionPuzzleInserter::HandlePutFailed(FCPMessage &message)\r
+const bool IntroductionPuzzleInserter::HandlePutFailed(FCPv2::Message &message)\r
 {\r
        SQLite3DB::Statement st;\r
        std::vector<std::string> idparts;\r
@@ -99,7 +132,7 @@ const bool IntroductionPuzzleInserter::HandlePutFailed(FCPMessage &message)
        return true;\r
 }\r
 \r
-const bool IntroductionPuzzleInserter::HandlePutSuccessful(FCPMessage &message)\r
+const bool IntroductionPuzzleInserter::HandlePutSuccessful(FCPv2::Message &message)\r
 {\r
        Poco::DateTime now;\r
        SQLite3DB::Statement st;\r
@@ -139,23 +172,24 @@ const bool IntroductionPuzzleInserter::HandlePutSuccessful(FCPMessage &message)
 void IntroductionPuzzleInserter::Initialize()\r
 {\r
        m_fcpuniquename="IntroductionPuzzleInserter";\r
+       m_maxpuzzleinserts=50;\r
 }\r
 \r
 const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid)\r
 {\r
        Poco::DateTime now;\r
-       std::string idstring;\r
+       std::string idstring="";\r
        long index=0;\r
-       std::string indexstr;\r
+       std::string indexstr="";\r
        Poco::UUIDGenerator uuidgen;\r
        Poco::UUID uuid;\r
-       std::string messagebase;\r
+       std::string messagebase="";\r
        IntroductionPuzzleXML xml;\r
-       std::string encodedpuzzle;\r
-       std::string solutionstring;\r
-       FCPMessage message;\r
-       std::string xmldata;\r
-       std::string xmldatasizestr;\r
+       std::string encodedpuzzle="";\r
+       std::string solutionstring="";\r
+       FCPv2::Message message;\r
+       std::string xmldata="";\r
+       std::string xmldatasizestr="";\r
        std::string privatekey="";\r
        std::string publickey="";\r
        std::string keypart="";\r
@@ -173,68 +207,83 @@ 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<m_maxpuzzleinserts)\r
        {\r
-               privatekey=rs2.GetField(0);\r
-               if(rs2.GetField(1))\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
-                       publickey=rs2.GetField(1);\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
-               if(publickey.size()>=50)\r
+\r
+               Option option(m_db);\r
+               option.Get("MessageBase",messagebase);\r
+\r
+               GenerateCaptcha(encodedpuzzle,solutionstring);\r
+               if(encodedpuzzle.size()==0)\r
                {\r
-                       // remove - and ~\r
-                       keypart=StringFunctions::Replace(StringFunctions::Replace(publickey.substr(4,43),"-",""),"~","");\r
+                       m_log->fatal("IntroductionPuzzleInserter::StartInsert could not create introduction puzzle");\r
+                       return false;\r
                }\r
-       }\r
 \r
-       Option::Instance()->Get("MessageBase",messagebase);\r
-\r
-       GenerateCaptcha(encodedpuzzle,solutionstring);\r
+               try\r
+               {\r
+                       uuid=uuidgen.createRandom();\r
+               }\r
+               catch(...)\r
+               {\r
+                       m_log->fatal("IntroductionPuzzleInserter::StartInsert could not create UUID");\r
+               }\r
 \r
-       try\r
-       {\r
-               uuid=uuidgen.createRandom();\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->Send(message);\r
+               m_fcp->Send(std::vector<char>(xmldata.begin(),xmldata.end()));\r
+\r
+               // insert to USK\r
+               message.Clear();\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->Send(message);\r
+               m_fcp->Send(std::vector<char>(xmldata.begin(),xmldata.end()));\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
-       catch(...)\r
+       else\r
        {\r
-               m_log->fatal("IntroductionPuzzleInserter::StartInsert could not create UUID");\r
+               m_log->warning("IntroductionPuzzleInserter::StartInsert already inserted max puzzles for "+idstring);\r
        }\r
 \r
-       xml.SetType("captcha");\r
-       xml.SetUUID(uuid.toString()+"@"+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
        return true;\r
 \r
 }\r