1 #include "../../include/freenet/messagelistrequester.h"
\r
2 #include "../../include/freenet/messagelistxml.h"
\r
8 MessageListRequester::MessageListRequester()
\r
13 MessageListRequester::MessageListRequester(FCPv2 *fcp):IIndexRequester<long>(fcp)
\r
18 const bool MessageListRequester::HandleAllData(FCPMessage &message)
\r
21 SQLite3DB::Statement st;
\r
22 SQLite3DB::Statement trustst;
\r
23 std::vector<std::string> idparts;
\r
25 std::vector<char> data;
\r
31 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
32 StringFunctions::Convert(message["DataLength"],datalength);
\r
33 StringFunctions::Convert(idparts[1],identityid);
\r
34 StringFunctions::Convert(idparts[2],index);
\r
36 // wait for all data to be received from connection
\r
37 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
42 // if we got disconnected- return immediately
\r
43 if(m_fcp->Connected()==false)
\r
49 data.resize(datalength);
\r
50 m_fcp->ReceiveRaw(&data[0],datalength);
\r
52 // parse file into xml and update the database
\r
53 if(xml.ParseXML(std::string(data.begin(),data.end()))==true)
\r
56 SQLite3DB::Statement st=m_db->Prepare("SELECT IdentityID FROM tblMessageRequests WHERE IdentityID=? AND Day=? AND RequestIndex=?;");
\r
57 SQLite3DB::Statement spk=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey=?;");
\r
58 SQLite3DB::Statement mst=m_db->Prepare("INSERT INTO tblMessageRequests(IdentityID,Day,RequestIndex,FromMessageList) VALUES(?,?,?,'true');");
\r
59 for(long i=0; i<xml.MessageCount(); i++)
\r
61 st.Bind(0,identityid);
\r
62 st.Bind(1,xml.GetDate(i));
\r
63 st.Bind(2,xml.GetIndex(i));
\r
65 if(st.RowReturned()==false)
\r
67 mst.Bind(0,identityid);
\r
68 mst.Bind(1,xml.GetDate(i));
\r
69 mst.Bind(2,xml.GetIndex(i));
\r
76 // insert external message indexes
\r
77 for(long i=0; i<xml.ExternalMessageCount(); i++)
\r
79 if(xml.GetExternalType(i)=="Keyed")
\r
81 spk.Bind(0,xml.GetExternalIdentity(i));
\r
83 if(spk.RowReturned())
\r
85 int thisidentityid=0;
\r
86 spk.ResultInt(0,thisidentityid);
\r
87 mst.Bind(0,thisidentityid);
\r
88 mst.Bind(1,xml.GetExternalDate(i));
\r
89 mst.Bind(2,xml.GetExternalIndex(i));
\r
97 st=m_db->Prepare("INSERT INTO tblMessageListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");
\r
98 st.Bind(0,identityid);
\r
99 st.Bind(1,idparts[4]);
\r
104 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageListRequester::HandleAllData parsed MessageList XML file : "+message["Identifier"]);
\r
108 // bad data - mark index
\r
109 st=m_db->Prepare("INSERT INTO tblMessageListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
110 st.Bind(0,identityid);
\r
111 st.Bind(1,idparts[4]);
\r
116 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageListRequester::HandleAllData error parsing MessageList XML file : "+message["Identifier"]);
\r
119 // remove this identityid from request list
\r
120 RemoveFromRequestList(identityid);
\r
126 const bool MessageListRequester::HandleGetFailed(FCPMessage &message)
\r
129 SQLite3DB::Statement st;
\r
130 std::vector<std::string> idparts;
\r
135 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
136 StringFunctions::Convert(idparts[1],identityid);
\r
137 StringFunctions::Convert(idparts[2],index);
\r
139 // code 27 - permanent redirect
\r
140 if(message["Code"]=="27")
\r
142 StartRedirectRequest(message);
\r
146 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
147 if(message["Fatal"]=="true")
\r
149 st=m_db->Prepare("INSERT INTO tblMessageListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
150 st.Bind(0,identityid);
\r
151 st.Bind(1,idparts[4]);
\r
156 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageListRequester::HandleGetFailed fatal error code="+message["Code"]+" requesting "+message["Identifier"]);
\r
159 // remove this identityid from request list
\r
160 RemoveFromRequestList(identityid);
\r
165 void MessageListRequester::Initialize()
\r
167 m_fcpuniquename="MessageListRequester";
\r
168 std::string tempval;
\r
169 Option::Instance()->Get("MaxMessageListRequests",tempval);
\r
170 StringFunctions::Convert(tempval,m_maxrequests);
\r
171 if(m_maxrequests<1)
\r
174 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxMessageListRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
176 if(m_maxrequests>100)
\r
178 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxMessageListRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
181 Option::Instance()->Get("LocalTrustOverridesPeerTrust",tempval);
\r
182 if(tempval=="true")
\r
184 m_localtrustoverrides=true;
\r
188 m_localtrustoverrides=false;
\r
193 void MessageListRequester::PopulateIDList()
\r
198 date.SetToGMTime();
\r
200 SQLite3DB::Statement st;
\r
202 // select identities we want to query (we've seen them today) - sort by their trust level (descending) with secondary sort on how long ago we saw them (ascending)
\r
203 if(m_localtrustoverrides==false)
\r
205 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey <> '' AND LastSeen>='"+date.Format("%Y-%m-%d")+"' AND (LocalMessageTrust IS NULL OR LocalMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalMessageTrust')) AND (PeerMessageTrust IS NULL OR PeerMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinPeerMessageTrust')) ORDER BY LocalMessageTrust+LocalTrustListTrust DESC, LastSeen;");
\r
209 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey <> '' AND LastSeen>='"+date.Format("%Y-%m-%d")+"' AND (LocalMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalMessageTrust') OR (LocalMessageTrust IS NULL AND (PeerMessageTrust IS NULL OR PeerMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinPeerMessageTrust')))) ORDER BY LocalMessageTrust+LocalTrustListTrust DESC, LastSeen;");
\r
215 while(st.RowReturned())
\r
217 st.ResultInt(0,id);
\r
223 void MessageListRequester::StartRedirectRequest(FCPMessage &message)
\r
225 std::vector<std::string> parts;
\r
226 std::string indexstr="";
\r
227 std::string identityidstr="";
\r
228 std::string datestr="";
\r
229 FCPMessage newmessage;
\r
231 // get the new edition #
\r
232 StringFunctions::Split(message["RedirectURI"],"/",parts);
\r
233 //edition # is 2nd to last part
\r
236 indexstr=parts[parts.size()-2];
\r
241 StringFunctions::Split(message["Identifier"],"|",parts);
\r
244 identityidstr=parts[1];
\r
251 newmessage.SetName("ClientGet");
\r
252 newmessage["URI"]=StringFunctions::UriDecode(message["RedirectURI"]);
\r
253 newmessage["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|_|"+datestr+"|"+newmessage["URI"];
\r
254 newmessage["ReturnType"]="direct";
\r
255 newmessage["MaxSize"]="1000000";
\r
257 m_fcp->SendMessage(newmessage);
\r
261 void MessageListRequester::StartRequest(const long &id)
\r
264 FCPMessage message;
\r
265 std::string publickey;
\r
267 std::string indexstr;
\r
268 std::string identityidstr;
\r
270 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
274 if(st.RowReturned())
\r
276 st.ResultText(0,publickey);
\r
280 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblMessageListRequests WHERE Day=? AND IdentityID=?;");
\r
281 st2.Bind(0,now.Format("%Y-%m-%d"));
\r
286 if(st2.RowReturned())
\r
288 if(st2.ResultNull(0)==false)
\r
290 st2.ResultInt(0,index);
\r
291 // don't increment index here - the node will let us know if there is a new edition
\r
292 // 2008-05-31 - well actually the node isn't reliably retreiving the latest edition for USKs, so we DO need to increment the index
\r
298 StringFunctions::Convert(index,indexstr);
\r
299 StringFunctions::Convert(id,identityidstr);
\r
301 message.SetName("ClientGet");
\r
302 message["URI"]="USK"+publickey.substr(3)+m_messagebase+"|"+now.Format("%Y.%m.%d")+"|MessageList/"+indexstr+"/MessageList.xml";
\r
303 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|_|"+now.Format("%Y-%m-%d")+"|"+message["URI"];
\r
304 message["ReturnType"]="direct";
\r
305 message["MaxSize"]="1000000";
\r
307 m_fcp->SendMessage(message);
\r
309 m_requesting.push_back(id);
\r