1 #include "../../include/freenet/introductionpuzzleinserter.h"
\r
2 #include "../../include/freenet/introductionpuzzlexml.h"
\r
3 #include "../../include/stringfunctions.h"
\r
4 #include "../../include/option.h"
\r
5 #include "../../include/freenet/captcha/simplecaptcha.h"
\r
6 #include "../../include/uuidgenerator.h"
\r
7 #include "../../include/base64.h"
\r
13 IntroductionPuzzleInserter::IntroductionPuzzleInserter()
\r
18 IntroductionPuzzleInserter::IntroductionPuzzleInserter(FCPv2 *fcp):IFCPConnected(fcp)
\r
23 void IntroductionPuzzleInserter::CheckForNeededInsert()
\r
25 // select all local ids that aren't single use and that aren't currently inserting a puzzle and are publishing a trust list
\r
26 SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID FROM tblLocalIdentity WHERE PublishTrustList='true' AND SingleUse='false' AND InsertingPuzzle='false' AND PrivateKey IS NOT NULL AND PrivateKey <> '' ORDER BY LastInsertedPuzzle;");
\r
30 std::string localidentityidstr;
\r
36 localidentityidstr=rs.GetField(0);
\r
39 // if this identity has any non-solved puzzles for today, we don't need to insert a new puzzle
\r
40 SQLite3DB::Recordset rs2=m_db->Query("SELECT UUID FROM tblIntroductionPuzzleInserts WHERE Day='"+now.Format("%Y-%m-%d")+"' AND FoundSolution='false' AND LocalIdentityID="+localidentityidstr+";");
\r
42 // identity doesn't have any non-solved puzzles for today - start a new insert
\r
43 if(rs2.Empty()==true)
\r
45 StartInsert(rs.GetInt(0));
\r
52 void IntroductionPuzzleInserter::FCPConnected()
\r
54 m_db->Execute("UPDATE tblLocalIdentity SET InsertingPuzzle='false';");
\r
57 void IntroductionPuzzleInserter::FCPDisconnected()
\r
62 void IntroductionPuzzleInserter::GenerateCaptcha(std::string &encodeddata, std::string &solution)
\r
64 SimpleCaptcha captcha;
\r
65 std::vector<unsigned char> puzzle;
\r
66 std::vector<unsigned char> puzzlesolution;
\r
69 captcha.GetPuzzle(puzzle);
\r
70 captcha.GetSolution(puzzlesolution);
\r
72 encodeddata.clear();
\r
75 Base64::Encode(puzzle,encodeddata);
\r
76 solution.insert(solution.begin(),puzzlesolution.begin(),puzzlesolution.end());
\r
80 const bool IntroductionPuzzleInserter::HandleMessage(FCPMessage &message)
\r
83 if(message["Identifier"].find("IntroductionPuzzleInserter")==0)
\r
86 // ignore URIGenerated message
\r
87 if(message.GetName()=="URIGenerated")
\r
92 if(message.GetName()=="PutSuccessful")
\r
94 return HandlePutSuccessful(message);
\r
97 if(message.GetName()=="PutFailed")
\r
99 return HandlePutFailed(message);
\r
107 const bool IntroductionPuzzleInserter::HandlePutFailed(FCPMessage &message)
\r
109 SQLite3DB::Statement st;
\r
110 std::vector<std::string> idparts;
\r
111 long localidentityid;
\r
113 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
114 StringFunctions::Convert(idparts[1],localidentityid);
\r
116 st=m_db->Prepare("UPDATE tblLocalIdentity SET InsertingPuzzle='false' WHERE LocalIdentityID=?;");
\r
117 st.Bind(0,localidentityid);
\r
121 // if fatal error or collision - mark index
\r
122 if(message["Fatal"]=="true" || message["Code"]=="9")
\r
124 m_db->Execute("UPDATE tblIntroductionPuzzleInserts SET Day='"+idparts[5]+"', InsertIndex="+idparts[2]+", FoundSolution='true' WHERE UUID='"+idparts[3]+"';");
\r
127 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::HandlePutFailed failed to insert puzzle "+idparts[3]);
\r
132 const bool IntroductionPuzzleInserter::HandlePutSuccessful(FCPMessage &message)
\r
135 SQLite3DB::Statement st;
\r
136 std::vector<std::string> idparts;
\r
137 long localidentityid;
\r
141 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
142 StringFunctions::Convert(idparts[1],localidentityid);
\r
143 StringFunctions::Convert(idparts[2],insertindex);
\r
145 st=m_db->Prepare("UPDATE tblIntroductionPuzzleInserts SET Day=?, InsertIndex=? WHERE UUID=?;");
\r
146 st.Bind(0,idparts[5]);
\r
147 st.Bind(1,insertindex);
\r
148 st.Bind(2,idparts[3]);
\r
152 st=m_db->Prepare("UPDATE tblLocalIdentity SET InsertingPuzzle='false', LastInsertedPuzzle=? WHERE LocalIdentityID=?;");
\r
153 st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S"));
\r
154 st.Bind(1,localidentityid);
\r
158 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::HandlePutSuccessful inserted puzzle "+idparts[3]);
\r
163 void IntroductionPuzzleInserter::Initialize()
\r
165 m_lastchecked.SetToGMTime();
\r
168 void IntroductionPuzzleInserter::Process()
\r
175 if(m_lastchecked<(now-(1.0/1440.0)))
\r
177 CheckForNeededInsert();
\r
183 void IntroductionPuzzleInserter::RegisterWithThread(FreenetMasterThread *thread)
\r
185 thread->RegisterFCPConnected(this);
\r
186 thread->RegisterFCPMessageHandler(this);
\r
187 thread->RegisterPeriodicProcessor(this);
\r
190 void IntroductionPuzzleInserter::StartInsert(const long localidentityid)
\r
193 std::string idstring;
\r
195 std::string indexstr;
\r
196 UUIDGenerator uuid;
\r
197 std::string messagebase;
\r
198 IntroductionPuzzleXML xml;
\r
199 std::string encodedpuzzle;
\r
200 std::string solutionstring;
\r
201 FCPMessage message;
\r
202 std::string xmldata;
\r
203 std::string xmldatasizestr;
\r
204 std::string privatekey="";
\r
205 std::string publickey="";
\r
206 std::string keypart="";
\r
208 StringFunctions::Convert(localidentityid,idstring);
\r
210 SQLite3DB::Recordset rs=m_db->Query("SELECT MAX(InsertIndex) FROM tblIntroductionPuzzleInserts WHERE Day='"+now.Format("%Y-%m-%d")+"' AND LocalIdentityID="+idstring+";");
\r
212 if(rs.Empty() || rs.GetField(0)==NULL)
\r
218 index=rs.GetInt(0)+1;
\r
220 StringFunctions::Convert(index,indexstr);
\r
222 SQLite3DB::Recordset rs2=m_db->Query("SELECT PrivateKey,PublicKey FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";");
\r
223 if(rs2.Empty()==false && rs2.GetField(0)!=NULL)
\r
225 privatekey=rs2.GetField(0);
\r
226 if(rs2.GetField(1))
\r
228 publickey=rs2.GetField(1);
\r
230 if(publickey.size()>=50)
\r
233 keypart=StringFunctions::Replace(StringFunctions::Replace(publickey.substr(4,43),"-",""),"~","");
\r
237 Option::Instance()->Get("MessageBase",messagebase);
\r
239 GenerateCaptcha(encodedpuzzle,solutionstring);
\r
241 xml.SetType("captcha");
\r
242 xml.SetUUID(uuid.Generate()+"@"+keypart);
\r
243 xml.SetPuzzleData(encodedpuzzle);
\r
244 xml.SetMimeType("image/bmp");
\r
246 xmldata=xml.GetXML();
\r
247 StringFunctions::Convert(xmldata.size(),xmldatasizestr);
\r
249 message.SetName("ClientPut");
\r
250 message["URI"]=privatekey+messagebase+"|"+now.Format("%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml";
\r
251 message["Identifier"]="IntroductionPuzzleInserter|"+idstring+"|"+indexstr+"|"+xml.GetUUID()+"|"+message["URI"];
\r
252 message["UploadFrom"]="direct";
\r
253 message["DataLength"]=xmldatasizestr;
\r
254 m_fcp->SendMessage(message);
\r
255 m_fcp->SendRaw(xmldata.c_str(),xmldata.size());
\r
257 m_db->Execute("UPDATE tblLocalIdentity SET InsertingPuzzle='true' WHERE LocalIdentityID="+idstring+";");
\r
258 m_db->Execute("INSERT INTO tblIntroductionPuzzleInserts(UUID,Type,MimeType,LocalIdentityID,PuzzleData,PuzzleSolution) VALUES('"+xml.GetUUID()+"','captcha','image/bmp',"+idstring+",'"+encodedpuzzle+"','"+solutionstring+"');");
\r
260 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::StartInsert started insert for id "+idstring);
\r