#include "../../include/stringfunctions.h"\r
#include "../../include/option.h"\r
#include "../../include/freenet/captcha/simplecaptcha.h"\r
-#include "../../include/uuidgenerator.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 <Poco/UUIDGenerator.h>\r
+#include <Poco/UUID.h>\r
+\r
#ifdef XMEM\r
#include <xmem.h>\r
#endif\r
\r
-IntroductionPuzzleInserter::IntroductionPuzzleInserter()\r
+IntroductionPuzzleInserter::IntroductionPuzzleInserter():IIndexInserter<long>()\r
{\r
Initialize();\r
}\r
\r
-IntroductionPuzzleInserter::IntroductionPuzzleInserter(FCPv2 *fcp):IFCPConnected(fcp)\r
+IntroductionPuzzleInserter::IntroductionPuzzleInserter(FCPv2 *fcp):IIndexInserter<long>(fcp)\r
{\r
Initialize();\r
}\r
\r
void IntroductionPuzzleInserter::CheckForNeededInsert()\r
{\r
- // select all local ids that aren't single use and that aren't currently inserting a puzzle\r
- SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID FROM tblLocalIdentity WHERE SingleUse='false' AND InsertingPuzzle='false' AND PrivateKey IS NOT NULL AND PrivateKey <> '' ORDER BY LastInsertedPuzzle;");\r
- \r
- while(!rs.AtEnd())\r
+ // only do 1 insert at a time\r
+ if(m_inserting.size()==0)\r
{\r
- DateTime now;\r
- now.SetToGMTime();\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';");\r
-\r
- // identity doesn't have any non-solved puzzles for today - start a new insert\r
- if(rs2.Empty()==true)\r
+ // select all local ids that aren't single use and that aren't currently inserting a puzzle and are publishing a trust list\r
+ SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID FROM tblLocalIdentity WHERE PublishTrustList='true' AND SingleUse='false' AND PrivateKey IS NOT NULL AND PrivateKey <> '' ORDER BY LastInsertedPuzzle;");\r
+ \r
+ while(!rs.AtEnd())\r
{\r
- StartInsert(rs.GetInt(0));\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
+ localidentityidstr=rs.GetField(0);\r
+ }\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='"+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
+ {\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
}\r
-\r
- rs.Next();\r
}\r
}\r
\r
-void IntroductionPuzzleInserter::FCPConnected()\r
-{\r
- m_db->Execute("UPDATE tblLocalIdentity SET InsertingPuzzle='false';");\r
-}\r
-\r
-void IntroductionPuzzleInserter::FCPDisconnected()\r
-{\r
- \r
-}\r
-\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
Base64::Encode(puzzle,encodeddata);\r
solution.insert(solution.begin(),puzzlesolution.begin(),puzzlesolution.end());\r
\r
-}\r
+ delete cap;\r
\r
-const bool IntroductionPuzzleInserter::HandleMessage(FCPMessage &message)\r
-{\r
-\r
- if(message["Identifier"].find("IntroductionPuzzleInserter")==0)\r
- {\r
-\r
- // ignore URIGenerated message\r
- if(message.GetName()=="URIGenerated")\r
- {\r
- return true;\r
- }\r
-\r
- if(message.GetName()=="PutSuccessful")\r
- {\r
- return HandlePutSuccessful(message);\r
- }\r
-\r
- if(message.GetName()=="PutFailed")\r
- {\r
- return HandlePutFailed(message);\r
- }\r
-\r
- }\r
-\r
- return false;\r
}\r
\r
const bool IntroductionPuzzleInserter::HandlePutFailed(FCPMessage &message)\r
StringFunctions::Split(message["Identifier"],"|",idparts);\r
StringFunctions::Convert(idparts[1],localidentityid);\r
\r
- st=m_db->Prepare("UPDATE tblLocalIdentity SET InsertingPuzzle='false' WHERE LocalIdentityID=?;");\r
- st.Bind(0,localidentityid);\r
- st.Step();\r
- st.Finalize();\r
+ // non USK\r
+ if(idparts[0]==m_fcpuniquename)\r
+ {\r
+ // if fatal error or collision - mark index\r
+ if(message["Fatal"]=="true" || message["Code"]=="9")\r
+ {\r
+ m_db->Execute("UPDATE tblIntroductionPuzzleInserts SET Day='"+idparts[5]+"', InsertIndex="+idparts[2]+", FoundSolution='true' WHERE UUID='"+idparts[3]+"';");\r
+ }\r
+\r
+ RemoveFromInsertList(localidentityid);\r
\r
- m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" failed to insert puzzle "+idparts[3]);\r
+ m_log->debug("IntroductionPuzzleInserter::HandlePutFailed failed to insert puzzle "+idparts[3]);\r
+ }\r
\r
return true;\r
}\r
\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
- StringFunctions::Convert(idparts[1],localidentityid);\r
- StringFunctions::Convert(idparts[2],insertindex);\r
-\r
- st=m_db->Prepare("UPDATE tblIntroductionPuzzleInserts SET Day=?, InsertIndex=? WHERE UUID=?;");\r
- st.Bind(0,idparts[5]);\r
- st.Bind(1,insertindex);\r
- st.Bind(2,idparts[3]);\r
- st.Step();\r
- st.Finalize();\r
\r
- st=m_db->Prepare("UPDATE tblLocalIdentity SET InsertingPuzzle='false', LastInsertedPuzzle=? WHERE LocalIdentityID=?;");\r
- st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S"));\r
- st.Bind(1,localidentityid);\r
- st.Step();\r
- st.Finalize();\r
-\r
- m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" inserted puzzle "+idparts[3]);\r
-\r
- return true;\r
-}\r
-\r
-void IntroductionPuzzleInserter::Initialize()\r
-{\r
- m_lastchecked.SetToGMTime();\r
-}\r
+ // non USK\r
+ if(idparts[0]==m_fcpuniquename)\r
+ {\r
+ StringFunctions::Convert(idparts[1],localidentityid);\r
+ StringFunctions::Convert(idparts[2],insertindex);\r
\r
-void IntroductionPuzzleInserter::Process()\r
-{\r
+ st=m_db->Prepare("UPDATE tblIntroductionPuzzleInserts SET Day=?, InsertIndex=? WHERE UUID=?;");\r
+ st.Bind(0,idparts[5]);\r
+ st.Bind(1,insertindex);\r
+ st.Bind(2,idparts[3]);\r
+ st.Step();\r
+ st.Finalize();\r
\r
- DateTime now;\r
+ st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedPuzzle=? WHERE LocalIdentityID=?;");\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
- now.SetToGMTime();\r
+ RemoveFromInsertList(localidentityid);\r
\r
- if(m_lastchecked<(now-(1.0/1440.0)))\r
- {\r
- CheckForNeededInsert();\r
- m_lastchecked=now;\r
+ m_log->debug("IntroductionPuzzleInserter::HandlePutSuccessful inserted puzzle "+idparts[3]);\r
}\r
\r
+ return true;\r
}\r
\r
-void IntroductionPuzzleInserter::RegisterWithThread(FreenetMasterThread *thread)\r
+void IntroductionPuzzleInserter::Initialize()\r
{\r
- thread->RegisterFCPConnected(this);\r
- thread->RegisterFCPMessageHandler(this);\r
- thread->RegisterPeriodicProcessor(this);\r
+ m_fcpuniquename="IntroductionPuzzleInserter";\r
+ m_maxpuzzleinserts=50;\r
}\r
\r
-void IntroductionPuzzleInserter::StartInsert(const long localidentityid)\r
+const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid)\r
{\r
- DateTime now;\r
- std::string idstring;\r
+ Poco::DateTime now;\r
+ std::string idstring="";\r
long index=0;\r
- std::string indexstr;\r
- UUIDGenerator uuid;\r
- std::string messagebase;\r
+ std::string indexstr="";\r
+ Poco::UUIDGenerator uuidgen;\r
+ Poco::UUID uuid;\r
+ std::string messagebase="";\r
IntroductionPuzzleXML xml;\r
- std::string encodedpuzzle;\r
- std::string solutionstring;\r
+ std::string encodedpuzzle="";\r
+ std::string solutionstring="";\r
FCPMessage message;\r
- std::string xmldata;\r
- std::string xmldatasizestr;\r
- std::string privatekey;\r
+ std::string xmldata="";\r
+ std::string xmldatasizestr="";\r
+ std::string privatekey="";\r
+ std::string publickey="";\r
+ 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
}\r
StringFunctions::Convert(index,indexstr);\r
\r
- SQLite3DB::Recordset rs2=m_db->Query("SELECT PrivateKey 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
- }\r
-\r
- Option::instance()->Get("MessageBase",messagebase);\r
-\r
- GenerateCaptcha(encodedpuzzle,solutionstring);\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
+ 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
- xml.SetType("captcha");\r
- xml.SetUUID(uuid.Generate());\r
- xml.SetPuzzleData(encodedpuzzle);\r
- xml.SetMimeType("bitmap/image");\r
+ Option::Instance()->Get("MessageBase",messagebase);\r
\r
- xmldata=xml.GetXML();\r
- StringFunctions::Convert(xmldata.size(),xmldatasizestr);\r
+ GenerateCaptcha(encodedpuzzle,solutionstring);\r
+ if(encodedpuzzle.size()==0)\r
+ {\r
+ m_log->fatal("IntroductionPuzzleInserter::StartInsert could not create introduction puzzle");\r
+ return false;\r
+ }\r
\r
- message.SetName("ClientPut");\r
- message["URI"]=privatekey+messagebase+"|"+now.Format("%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml";\r
- message["Identifier"]="IntroductionPuzzleInserter|"+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
+ try\r
+ {\r
+ uuid=uuidgen.createRandom();\r
+ }\r
+ catch(...)\r
+ {\r
+ m_log->fatal("IntroductionPuzzleInserter::StartInsert could not create UUID");\r
+ }\r
\r
- m_db->Execute("UPDATE tblLocalIdentity SET InsertingPuzzle='true' WHERE LocalIdentityID="+idstring+";");\r
- m_db->Execute("INSERT INTO tblIntroductionPuzzleInserts(UUID,Type,MimeType,LocalIdentityID,PuzzleData,PuzzleSolution) VALUES('"+xml.GetUUID()+"','captcha','image/bmp',"+idstring+",'"+encodedpuzzle+"','"+solutionstring+"');");\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 max puzzles for "+idstring);\r
+ }\r
\r
- m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,__FUNCTION__" started insert for id "+idstring);\r
+ return true;\r
\r
}\r