version 0.3.22
[fms.git] / src / freenet / introductionpuzzlerequester.cpp
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
7 \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
12 \r
13 #ifdef XMEM\r
14         #include <xmem.h>\r
15 #endif\r
16 \r
17 IntroductionPuzzleRequester::IntroductionPuzzleRequester()\r
18 {\r
19         Initialize();\r
20 }\r
21 \r
22 IntroductionPuzzleRequester::IntroductionPuzzleRequester(FCPv2 *fcp):IIndexRequester<long>(fcp)\r
23 {\r
24         Initialize();\r
25 }\r
26 \r
27 void IntroductionPuzzleRequester::FCPConnected()\r
28 {\r
29         m_requesting.clear();\r
30         PopulateIDList();\r
31 }\r
32 \r
33 void IntroductionPuzzleRequester::FCPDisconnected()\r
34 {\r
35         \r
36 }\r
37 \r
38 const bool IntroductionPuzzleRequester::HandleAllData(FCPMessage &message)\r
39 {\r
40         Poco::DateTime now;\r
41         SQLite3DB::Statement st;\r
42         std::vector<std::string> idparts;\r
43         long datalength;\r
44         std::vector<char> data;\r
45         IntroductionPuzzleXML xml;\r
46         long identityid;\r
47         long index;\r
48         bool validmessage=true;\r
49 \r
50         StringFunctions::Split(message["Identifier"],"|",idparts);\r
51         StringFunctions::Convert(message["DataLength"],datalength);\r
52         StringFunctions::Convert(idparts[1],identityid);\r
53         StringFunctions::Convert(idparts[2],index);\r
54 \r
55         // wait for all data to be received from connection\r
56         while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)\r
57         {\r
58                 m_fcp->Update(1);\r
59         }\r
60 \r
61         // if we got disconnected- return immediately\r
62         if(m_fcp->Connected()==false)\r
63         {\r
64                 return false;\r
65         }\r
66 \r
67         // receive the file\r
68         data.resize(datalength);\r
69         if(data.size()>0)\r
70         {\r
71                 m_fcp->ReceiveRaw(&data[0],datalength);\r
72         }\r
73 \r
74         // parse file into xml and update the database\r
75         if(data.size()>0 && xml.ParseXML(std::string(data.begin(),data.end()))==true)\r
76         {\r
77 \r
78                 // check if last part of UUID matches first part of public key of identity who inserted it\r
79                 st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");\r
80                 st.Bind(0,identityid);\r
81                 st.Step();\r
82                 if(st.RowReturned())\r
83                 {\r
84                         std::vector<std::string> uuidparts;\r
85                         std::vector<std::string> keyparts;\r
86                         std::string keypart="";\r
87                         std::string publickey="";\r
88 \r
89                         st.ResultText(0,publickey);\r
90 \r
91                         StringFunctions::SplitMultiple(publickey,"@,",keyparts);\r
92                         StringFunctions::SplitMultiple(xml.GetUUID(),"@",uuidparts);\r
93 \r
94                         if(uuidparts.size()>1 && keyparts.size()>1)\r
95                         {\r
96                                 keypart=StringFunctions::Replace(StringFunctions::Replace(keyparts[1],"-",""),"~","");\r
97                                 if(keypart!=uuidparts[1])\r
98                                 {\r
99                                         m_log->error("IntroductionPuzzleRequester::HandleAllData UUID in IntroductionPuzzle doesn't match public key of identity : "+message["Identifier"]);\r
100                                         validmessage=false;\r
101                                 }\r
102                         }\r
103                         else\r
104                         {\r
105                                 m_log->error("IntroductionPuzzleRequester::HandleAllData Error with identity's public key or UUID : "+message["Identifier"]);\r
106                                 validmessage=false;\r
107                         }\r
108 \r
109                 }\r
110                 else\r
111                 {\r
112                         m_log->error("IntroductionPuzzleRequester::HandleAllData Error couldn't find identity : "+message["Identifier"]);\r
113                         validmessage=false;\r
114                 }\r
115 \r
116                 // we can only validate bitmaps for now\r
117                 BitmapValidator val;\r
118                 val.SetMax(200,200);\r
119                 std::vector<unsigned char> puzzledata;\r
120                 Base64::Decode(xml.GetPuzzleData(),puzzledata);\r
121                 if(xml.GetMimeType()!="image/bmp" || val.Validate(puzzledata)==false)\r
122                 {\r
123                         m_log->error("IntroductionPuzzleRequester::HandleAllData received bad mime type and/or data for "+message["Identifier"]);\r
124                         validmessage=false;\r
125                 }\r
126 \r
127                 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found,UUID,Type,MimeType,PuzzleData) VALUES(?,?,?,?,?,?,?,?);");\r
128                 st.Bind(0,identityid);\r
129                 st.Bind(1,idparts[4]);\r
130                 st.Bind(2,index);\r
131                 if(validmessage)\r
132                 {\r
133                         st.Bind(3,"true");\r
134                         st.Bind(4,xml.GetUUID());\r
135                         st.Bind(5,xml.GetType());\r
136                         st.Bind(6,xml.GetMimeType());\r
137                         st.Bind(7,xml.GetPuzzleData());\r
138                 }\r
139                 else\r
140                 {\r
141                         st.Bind(3,"false");\r
142                         st.Bind(4);\r
143                         st.Bind(5);\r
144                         st.Bind(6);\r
145                         st.Bind(7);\r
146                 }\r
147                 st.Step();\r
148                 st.Finalize();\r
149 \r
150                 m_log->debug("IntroductionPuzzleRequester::HandleAllData parsed IntroductionPuzzle XML file : "+message["Identifier"]);\r
151         }\r
152         else\r
153         {\r
154                 // bad data - mark index\r
155                 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
156                 st.Bind(0,identityid);\r
157                 st.Bind(1,idparts[4]);\r
158                 st.Bind(2,index);\r
159                 st.Step();\r
160                 st.Finalize();\r
161 \r
162                 m_log->error("IntroductionPuzzleRequester::HandleAllData error parsing IntroductionPuzzle XML file : "+message["Identifier"]);\r
163         }\r
164 \r
165         // remove this identityid from request list\r
166         RemoveFromRequestList(identityid);\r
167 \r
168         return true;\r
169 \r
170 }\r
171 \r
172 const bool IntroductionPuzzleRequester::HandleGetFailed(FCPMessage &message)\r
173 {\r
174         SQLite3DB::Statement st;\r
175         std::vector<std::string> idparts;\r
176         long identityid;\r
177         long index;\r
178 \r
179         StringFunctions::Split(message["Identifier"],"|",idparts);\r
180         StringFunctions::Convert(idparts[1],identityid);\r
181         StringFunctions::Convert(idparts[2],index);     \r
182 \r
183         // if this is a fatal error - insert index into database so we won't try to download this index again\r
184         if(message["Fatal"]=="true")\r
185         {\r
186                 st=m_db->Prepare("INSERT INTO tblIntroductionPuzzleRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
187                 st.Bind(0,identityid);\r
188                 st.Bind(1,idparts[4]);\r
189                 st.Bind(2,index);\r
190                 st.Step();\r
191                 st.Finalize();\r
192 \r
193                 m_log->error("IntroductionPuzzleRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);\r
194         }\r
195 \r
196         // remove this identityid from request list\r
197         RemoveFromRequestList(identityid);\r
198 \r
199         return true;\r
200 \r
201 }\r
202 \r
203 void IntroductionPuzzleRequester::Initialize()\r
204 {\r
205         m_fcpuniquename="IntroductionPuzzleRequester";\r
206         m_maxrequests=0;\r
207         Option::Instance()->GetInt("MaxIntroductionPuzzleRequests",m_maxrequests);\r
208         if(m_maxrequests<1)\r
209         {\r
210                 m_maxrequests=1;\r
211                 m_log->error("Option MaxIntroductionPuzzleRequests is currently set at less than 1.  It must be 1 or greater.");\r
212         }\r
213         if(m_maxrequests>100)\r
214         {\r
215                 m_log->warning("Option MaxIntroductionPuzzleRequests is currently set at more than 100.  This value might be incorrectly configured.");\r
216         }\r
217 }\r
218 \r
219 void IntroductionPuzzleRequester::PopulateIDList()\r
220 {\r
221         Poco::DateTime now;\r
222         int id;\r
223         std::string limitnum="30";\r
224 \r
225         // if we don't have an identity that we haven't seen yet, then set limit to 5\r
226         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
227         st.Step();\r
228         if(!st.RowReturned())\r
229         {\r
230                 limitnum="5";\r
231         }\r
232         st.Finalize();\r
233 \r
234         // 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
235         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
236         st.Step();\r
237 \r
238         m_ids.clear();\r
239 \r
240         while(st.RowReturned())\r
241         {\r
242                 st.ResultInt(0,id);\r
243                 m_ids[id]=false;\r
244                 st.Step();\r
245         }\r
246 }\r
247 \r
248 void IntroductionPuzzleRequester::StartRequest(const long &identityid)\r
249 {\r
250         Poco::DateTime now;\r
251         FCPMessage message;\r
252         std::string publickey;\r
253         int index;\r
254         std::string indexstr;\r
255         std::string identityidstr;\r
256 \r
257         SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");\r
258         st.Bind(0,identityid);\r
259         st.Step();\r
260 \r
261         if(st.RowReturned())\r
262         {\r
263                 st.ResultText(0,publickey);\r
264 \r
265                 now=Poco::Timestamp();\r
266 \r
267                 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblIntroductionPuzzleRequests WHERE Day=? AND IdentityID=?;");\r
268                 st2.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));\r
269                 st2.Bind(1,identityid);\r
270                 st2.Step();\r
271 \r
272                 index=0;\r
273                 if(st2.RowReturned())\r
274                 {\r
275                         if(st2.ResultNull(0)==false)\r
276                         {\r
277                                 st2.ResultInt(0,index);\r
278                                 index++;\r
279                         }\r
280                 }\r
281                 st2.Finalize();\r
282 \r
283                 StringFunctions::Convert(index,indexstr);\r
284                 StringFunctions::Convert(identityid,identityidstr);\r
285 \r
286                 message.SetName("ClientGet");\r
287                 message["URI"]=publickey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml";\r
288                 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];\r
289                 message["ReturnType"]="direct";\r
290                 message["MaxSize"]="1000000";           // 1 MB\r
291 \r
292                 m_fcp->SendMessage(message);\r
293                 \r
294                 m_requesting.push_back(identityid);\r
295         }\r
296 \r
297         m_ids[identityid]=true;\r
298 }\r