1 #include "../../include/freenet/introductionpuzzlerequester.h"
\r
2 #include "../../include/freenet/introductionpuzzlexml.h"
\r
3 #include "../../include/option.h"
\r
4 #include "../../include/stringfunctions.h"
\r
5 #include "../../include/bitmapvalidator.h"
\r
6 #include "../../include/base64.h"
\r
8 #include <Poco/DateTime.h>
\r
9 #include <Poco/Timespan.h>
\r
10 #include <Poco/Timestamp.h>
\r
11 #include <Poco/DateTimeFormatter.h>
\r
17 IntroductionPuzzleRequester::IntroductionPuzzleRequester()
\r
22 IntroductionPuzzleRequester::IntroductionPuzzleRequester(FCPv2 *fcp):IIndexRequester<long>(fcp)
\r
27 const bool IntroductionPuzzleRequester::HandleAllData(FCPMessage &message)
\r
30 SQLite3DB::Statement st;
\r
31 std::vector<std::string> idparts;
\r
33 std::vector<char> data;
\r
34 IntroductionPuzzleXML xml;
\r
37 bool validmessage=true;
\r
39 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
40 StringFunctions::Convert(message["DataLength"],datalength);
\r
41 StringFunctions::Convert(idparts[1],identityid);
\r
42 StringFunctions::Convert(idparts[2],index);
\r
44 // wait for all data to be received from connection
\r
45 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
50 // if we got disconnected- return immediately
\r
51 if(m_fcp->Connected()==false)
\r
57 data.resize(datalength);
\r
60 m_fcp->ReceiveRaw(&data[0],datalength);
\r
63 // parse file into xml and update the database
\r
64 if(data.size()>0 && xml.ParseXML(std::string(data.begin(),data.end()))==true)
\r
67 // check if last part of UUID matches first part of public key of identity who inserted it
\r
68 st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
69 st.Bind(0,identityid);
\r
71 if(st.RowReturned())
\r
73 std::vector<std::string> uuidparts;
\r
74 std::vector<std::string> keyparts;
\r
75 std::string keypart="";
\r
76 std::string publickey="";
\r
78 st.ResultText(0,publickey);
\r
80 StringFunctions::SplitMultiple(publickey,"@,",keyparts);
\r
81 StringFunctions::SplitMultiple(xml.GetUUID(),"@",uuidparts);
\r
83 if(uuidparts.size()>1 && keyparts.size()>1)
\r
85 keypart=StringFunctions::Replace(StringFunctions::Replace(keyparts[1],"-",""),"~","");
\r
86 if(keypart!=uuidparts[1])
\r
88 m_log->error("IntroductionPuzzleRequester::HandleAllData UUID in IntroductionPuzzle doesn't match public key of identity : "+message["Identifier"]);
\r
94 m_log->error("IntroductionPuzzleRequester::HandleAllData Error with identity's public key or UUID : "+message["Identifier"]);
\r
101 m_log->error("IntroductionPuzzleRequester::HandleAllData Error couldn't find identity : "+message["Identifier"]);
\r
102 validmessage=false;
\r
105 // we can only validate bitmaps for now
\r
106 BitmapValidator val;
\r
107 val.SetMax(200,200);
\r
108 std::vector<unsigned char> puzzledata;
\r
109 Base64::Decode(xml.GetPuzzleData(),puzzledata);
\r
110 if(xml.GetMimeType()!="image/bmp" || val.Validate(puzzledata)==false)
\r
112 m_log->error("IntroductionPuzzleRequester::HandleAllData received bad mime type and/or data for "+message["Identifier"]);
\r
113 validmessage=false;
\r
116 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found,UUID,Type,MimeType,PuzzleData) VALUES(?,?,?,?,?,?,?,?);");
\r
117 st.Bind(0,identityid);
\r
118 st.Bind(1,idparts[4]);
\r
123 st.Bind(4,xml.GetUUID());
\r
124 st.Bind(5,xml.GetType());
\r
125 st.Bind(6,xml.GetMimeType());
\r
126 st.Bind(7,xml.GetPuzzleData());
\r
130 st.Bind(3,"false");
\r
139 m_log->debug("IntroductionPuzzleRequester::HandleAllData parsed IntroductionPuzzle XML file : "+message["Identifier"]);
\r
143 // bad data - mark index
\r
144 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
145 st.Bind(0,identityid);
\r
146 st.Bind(1,idparts[4]);
\r
151 m_log->error("IntroductionPuzzleRequester::HandleAllData error parsing IntroductionPuzzle XML file : "+message["Identifier"]);
\r
154 // remove this identityid from request list
\r
155 RemoveFromRequestList(identityid);
\r
161 const bool IntroductionPuzzleRequester::HandleGetFailed(FCPMessage &message)
\r
163 SQLite3DB::Statement st;
\r
164 std::vector<std::string> idparts;
\r
168 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
169 StringFunctions::Convert(idparts[1],identityid);
\r
170 StringFunctions::Convert(idparts[2],index);
\r
172 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
173 if(message["Fatal"]=="true")
\r
175 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
176 st.Bind(0,identityid);
\r
177 st.Bind(1,idparts[4]);
\r
182 m_log->error("IntroductionPuzzleRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
185 // remove this identityid from request list
\r
186 RemoveFromRequestList(identityid);
\r
192 void IntroductionPuzzleRequester::Initialize()
\r
194 m_fcpuniquename="IntroductionPuzzleRequester";
\r
196 Option::Instance()->GetInt("MaxIntroductionPuzzleRequests",m_maxrequests);
\r
197 if(m_maxrequests<1)
\r
200 m_log->error("Option MaxIntroductionPuzzleRequests is currently set at less than 1. It must be 1 or greater.");
\r
202 if(m_maxrequests>100)
\r
204 m_log->warning("Option MaxIntroductionPuzzleRequests is currently set at more than 100. This value might be incorrectly configured.");
\r
208 void IntroductionPuzzleRequester::PopulateIDList()
\r
210 Poco::DateTime now;
\r
212 std::string limitnum="30";
\r
214 // if we don't have an identity that we haven't seen yet, then set limit to 5
\r
215 SQLite3DB::Statement st=m_db->Prepare("SELECT tblLocalIdentity.LocalIdentityID FROM tblLocalIdentity LEFT JOIN tblIdentity ON tblLocalIdentity.PublicKey=tblIdentity.PublicKey WHERE tblIdentity.IdentityID IS NULL;");
\r
217 if(!st.RowReturned())
\r
223 // select identities that aren't single use, are publishing a trust list, and have been seen today ( order by trust DESC and limit to limitnum )
\r
224 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublishTrustList='true' AND PublicKey IS NOT NULL AND PublicKey <> '' AND SingleUse='false' AND (LocalTrustListTrust IS NULL OR LocalTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalTrustListTrust')) AND LastSeen>='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"' ORDER BY LocalMessageTrust DESC LIMIT 0,"+limitnum+";");
\r
229 while(st.RowReturned())
\r
231 st.ResultInt(0,id);
\r
237 void IntroductionPuzzleRequester::StartRequest(const long &identityid)
\r
239 Poco::DateTime now;
\r
240 FCPMessage message;
\r
241 std::string publickey;
\r
243 std::string indexstr;
\r
244 std::string identityidstr;
\r
246 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
247 st.Bind(0,identityid);
\r
250 if(st.RowReturned())
\r
252 st.ResultText(0,publickey);
\r
254 now=Poco::Timestamp();
\r
256 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblIntroductionPuzzleRequests WHERE Day=? AND IdentityID=?;");
\r
257 st2.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));
\r
258 st2.Bind(1,identityid);
\r
262 if(st2.RowReturned())
\r
264 if(st2.ResultNull(0)==false)
\r
266 st2.ResultInt(0,index);
\r
272 StringFunctions::Convert(index,indexstr);
\r
273 StringFunctions::Convert(identityid,identityidstr);
\r
275 message.SetName("ClientGet");
\r
276 message["URI"]=publickey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml";
\r
277 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
278 message["ReturnType"]="direct";
\r
279 message["MaxSize"]="1000000"; // 1 MB
\r
281 m_fcp->SendMessage(message);
\r
283 m_requesting.push_back(identityid);
\r
286 m_ids[identityid]=true;
\r