1 #include "../../include/freenet/messagelistrequester.h"
\r
2 #include "../../include/freenet/messagelistxml.h"
\r
4 #include <Poco/DateTime.h>
\r
5 #include <Poco/DateTimeFormatter.h>
\r
6 #include <Poco/DateTimeParser.h>
\r
7 #include <Poco/Timestamp.h>
\r
13 MessageListRequester::MessageListRequester()
\r
18 MessageListRequester::MessageListRequester(FCPv2::Connection *fcp):IIndexRequester<long>(fcp)
\r
23 const bool MessageListRequester::CheckDateNotFuture(const std::string &datestr) const
\r
25 std::vector<std::string> dateparts;
\r
29 Poco::DateTime today;
\r
31 StringFunctions::Split(datestr,"-",dateparts);
\r
32 if(dateparts.size()==3)
\r
34 StringFunctions::Convert(dateparts[0],year);
\r
35 StringFunctions::Convert(dateparts[1],month);
\r
36 StringFunctions::Convert(dateparts[2],day);
\r
37 if(today.year()>year || (today.year()==year && today.month()>month) || (today.year()==year && today.month()==month && today.day()>=day))
\r
53 const bool MessageListRequester::CheckDateWithinMaxDays(const std::string &datestr) const
\r
55 Poco::DateTime checkdate;
\r
56 Poco::DateTime date;
\r
58 if(Poco::DateTimeParser::tryParse(datestr,date,tzdiff))
\r
60 checkdate-=Poco::Timespan(m_messagedownloadmaxdaysbackward,0,0,0,0);
\r
76 void MessageListRequester::GetBoardList(std::map<std::string,bool> &boards)
\r
78 SQLite3DB::Statement st=m_db->Prepare("SELECT BoardName, SaveReceivedMessages FROM tblBoard;");
\r
80 while(st.RowReturned())
\r
82 std::string boardname="";
\r
83 std::string tempval="";
\r
84 st.ResultText(0,boardname);
\r
85 st.ResultText(1,tempval);
\r
89 boards[boardname]=true;
\r
93 boards[boardname]=false;
\r
100 const bool MessageListRequester::HandleAllData(FCPv2::Message &message)
\r
102 SQLite3DB::Statement st;
\r
103 SQLite3DB::Statement trustst;
\r
104 std::vector<std::string> idparts;
\r
106 std::vector<char> data;
\r
107 MessageListXML xml;
\r
110 std::map<std::string,bool> boards; // list of boards and if we will save messages for that board or not
\r
111 bool addmessage=false;
\r
112 std::string boardsstr="";
\r
113 std::string datestr="";
\r
114 std::vector<std::string> dateparts;
\r
116 GetBoardList(boards);
\r
118 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
119 StringFunctions::Convert(message["DataLength"],datalength);
\r
120 StringFunctions::Convert(idparts[1],identityid);
\r
121 StringFunctions::Convert(idparts[2],index);
\r
123 // wait for all data to be received from connection
\r
124 m_fcp->WaitForBytes(1000,datalength);
\r
126 // if we got disconnected- return immediately
\r
127 if(m_fcp->IsConnected()==false)
\r
132 // receive the file
\r
133 m_fcp->Receive(data,datalength);
\r
135 // parse file into xml and update the database
\r
136 if(data.size()>0 && xml.ParseXML(std::string(data.begin(),data.end()))==true)
\r
139 SQLite3DB::Statement st=m_db->Prepare("SELECT IdentityID FROM tblMessageRequests WHERE IdentityID=? AND Day=? AND RequestIndex=?;");
\r
140 SQLite3DB::Statement spk=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey=?;");
\r
141 SQLite3DB::Statement mst=m_db->Prepare("INSERT INTO tblMessageRequests(IdentityID,Day,RequestIndex,FromMessageList) VALUES(?,?,?,'true');");
\r
142 for(long i=0; i<xml.MessageCount(); i++)
\r
145 // go through each board the message was posted to and see if we are saving messages to that board
\r
146 // if the board isn't found, see if we are saving messages to new boards
\r
149 std::vector<std::string> messageboards=xml.GetBoards(i);
\r
150 for(std::vector<std::string>::iterator j=messageboards.begin(); j!=messageboards.end(); j++)
\r
152 if(boards.find((*j))!=boards.end())
\r
154 if(boards[(*j)]==true)
\r
159 else if(m_savetonewboards==true)
\r
163 if(j!=messageboards.begin())
\r
170 if(CheckDateNotFuture(xml.GetDate(i))==false)
\r
173 m_log->error(m_fcpuniquename+"::HandleAllData date for message is in future! "+xml.GetDate(i));
\r
176 if(addmessage==true && CheckDateWithinMaxDays(xml.GetDate(i))==false)
\r
181 if(addmessage==true)
\r
183 st.Bind(0,identityid);
\r
184 st.Bind(1,xml.GetDate(i));
\r
185 st.Bind(2,xml.GetIndex(i));
\r
187 if(st.RowReturned()==false)
\r
189 mst.Bind(0,identityid);
\r
190 mst.Bind(1,xml.GetDate(i));
\r
191 mst.Bind(2,xml.GetIndex(i));
\r
199 //m_log->trace("MessageListRequester::HandleAllData will not download message posted to "+boardsstr+" on "+xml.GetDate(i));
\r
203 // insert external message indexes
\r
204 for(long i=0; i<xml.ExternalMessageCount(); i++)
\r
206 if(xml.GetExternalType(i)=="Keyed")
\r
208 // go through each board the message was posted to and see if we are saving messages to that board
\r
209 // if the board isn't found, see if we are saving messages to new boards
\r
212 std::vector<std::string> messageboards=xml.GetExternalBoards(i);
\r
213 for(std::vector<std::string>::iterator j=messageboards.begin(); j!=messageboards.end(); j++)
\r
215 if(boards.find((*j))!=boards.end())
\r
217 if(boards[(*j)]==true)
\r
222 else if(m_savetonewboards==true)
\r
226 if(j!=messageboards.begin())
\r
233 if(CheckDateNotFuture(xml.GetExternalDate(i))==false)
\r
236 m_log->error(m_fcpuniquename+"::HandleAllData date for external message is in future! "+xml.GetExternalDate(i));
\r
239 if(addmessage==true && CheckDateWithinMaxDays(xml.GetExternalDate(i))==false)
\r
244 if(addmessage==true)
\r
246 spk.Bind(0,xml.GetExternalIdentity(i));
\r
248 if(spk.RowReturned())
\r
250 int thisidentityid=0;
\r
251 spk.ResultInt(0,thisidentityid);
\r
252 mst.Bind(0,thisidentityid);
\r
253 mst.Bind(1,xml.GetExternalDate(i));
\r
254 mst.Bind(2,xml.GetExternalIndex(i));
\r
262 //m_log->trace("MessageListRequester::HandleAllData will not download external message posted to "+boardsstr+" from " + xml.GetExternalIdentity(i) + " on " + xml.GetExternalDate(i));
\r
267 st=m_db->Prepare("INSERT INTO tblMessageListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");
\r
268 st.Bind(0,identityid);
\r
269 st.Bind(1,idparts[4]);
\r
274 m_log->debug(m_fcpuniquename+"::HandleAllData parsed MessageList XML file : "+message["Identifier"]);
\r
278 // bad data - mark index
\r
279 st=m_db->Prepare("INSERT INTO tblMessageListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
280 st.Bind(0,identityid);
\r
281 st.Bind(1,idparts[4]);
\r
286 m_log->error(m_fcpuniquename+"::HandleAllData error parsing MessageList XML file : "+message["Identifier"]);
\r
289 // remove this identityid from request list
\r
290 RemoveFromRequestList(identityid);
\r
296 const bool MessageListRequester::HandleGetFailed(FCPv2::Message &message)
\r
298 SQLite3DB::Statement st;
\r
299 std::vector<std::string> idparts;
\r
303 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
304 StringFunctions::Convert(idparts[1],identityid);
\r
305 StringFunctions::Convert(idparts[2],index);
\r
307 // code 27 - permanent redirect
\r
308 if(message["Code"]=="27")
\r
310 StartRedirectRequest(message);
\r
314 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
315 if(message["Fatal"]=="true")
\r
317 st=m_db->Prepare("INSERT INTO tblMessageListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
318 st.Bind(0,identityid);
\r
319 st.Bind(1,idparts[4]);
\r
324 m_log->error(m_fcpuniquename+"::HandleGetFailed fatal error code="+message["Code"]+" requesting "+message["Identifier"]);
\r
327 // remove this identityid from request list
\r
328 RemoveFromRequestList(identityid);
\r
333 void MessageListRequester::Initialize()
\r
335 m_fcpuniquename="ActiveMessageListRequester";
\r
336 std::string tempval="";
\r
339 Option::Instance()->GetInt("MaxMessageListRequests",m_maxrequests);
\r
341 // active identities get 1/2 of the max requests option + any remaining if not evenly divisible - inactive identities get 1/2
\r
342 m_maxrequests=(m_maxrequests/2)+(m_maxrequests%2);
\r
344 if(m_maxrequests<1)
\r
347 m_log->error("Option MaxMessageListRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
349 if(m_maxrequests>100)
\r
351 m_log->warning("Option MaxMessageListRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
355 Option::Instance()->Get("LocalTrustOverridesPeerTrust",tempval);
\r
356 if(tempval=="true")
\r
358 m_localtrustoverrides=true;
\r
362 m_localtrustoverrides=false;
\r
366 Option::Instance()->Get("SaveMessagesFromNewBoards",tempval);
\r
367 if(tempval=="true")
\r
369 m_savetonewboards=true;
\r
373 m_savetonewboards=false;
\r
376 m_messagedownloadmaxdaysbackward=5;
\r
378 Option::Instance()->Get("MessageDownloadMaxDaysBackward",tempval);
\r
379 StringFunctions::Convert(tempval,m_messagedownloadmaxdaysbackward);
\r
383 void MessageListRequester::PopulateIDList()
\r
385 Poco::DateTime date;
\r
386 Poco::DateTime yesterday=date-Poco::Timespan(1,0,0,0,0);
\r
389 SQLite3DB::Statement st;
\r
391 // 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
392 if(m_localtrustoverrides==false)
\r
394 st=m_db->Prepare("SELECT tblIdentity.IdentityID FROM tblIdentity INNER JOIN vwIdentityStats ON tblIdentity.IdentityID=vwIdentityStats.IdentityID WHERE PublicKey IS NOT NULL AND PublicKey <> '' AND LastSeen>='"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"' AND (vwIdentityStats.LastMessageDate>='"+Poco::DateTimeFormatter::format(yesterday,"%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
398 st=m_db->Prepare("SELECT tblIdentity.IdentityID FROM tblIdentity INNER JOIN vwIdentityStats ON tblIdentity.IdentityID=vwIdentityStats.IdentityID WHERE PublicKey IS NOT NULL AND PublicKey <> '' AND LastSeen>='"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"' AND (vwIdentityStats.LastMessageDate>='"+Poco::DateTimeFormatter::format(yesterday,"%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
404 while(st.RowReturned())
\r
406 st.ResultInt(0,id);
\r
412 void MessageListRequester::StartRedirectRequest(FCPv2::Message &message)
\r
414 std::vector<std::string> parts;
\r
415 std::string indexstr="";
\r
416 std::string identityidstr="";
\r
417 std::string datestr="";
\r
418 FCPv2::Message newmessage;
\r
420 // get the new edition #
\r
421 StringFunctions::Split(message["RedirectURI"],"/",parts);
\r
422 //edition # is 2nd to last part
\r
425 indexstr=parts[parts.size()-2];
\r
430 StringFunctions::Split(message["Identifier"],"|",parts);
\r
433 identityidstr=parts[1];
\r
440 newmessage.SetName("ClientGet");
\r
441 newmessage["URI"]=StringFunctions::UriDecode(message["RedirectURI"]);
\r
442 newmessage["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|_|"+datestr+"|"+newmessage["URI"];
\r
443 newmessage["ReturnType"]="direct";
\r
444 newmessage["MaxSize"]="1000000";
\r
446 m_fcp->Send(newmessage);
\r
450 void MessageListRequester::StartRequest(const long &id)
\r
452 Poco::DateTime now;
\r
453 FCPv2::Message message;
\r
454 std::string publickey;
\r
456 std::string indexstr;
\r
457 std::string identityidstr;
\r
459 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
463 if(st.RowReturned())
\r
465 st.ResultText(0,publickey);
\r
467 now=Poco::Timestamp();
\r
469 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblMessageListRequests WHERE Day=? AND IdentityID=?;");
\r
470 st2.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));
\r
475 if(st2.RowReturned())
\r
477 if(st2.ResultNull(0)==false)
\r
479 st2.ResultInt(0,index);
\r
480 // don't increment index here - the node will let us know if there is a new edition
\r
481 // 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
487 StringFunctions::Convert(index,indexstr);
\r
488 StringFunctions::Convert(id,identityidstr);
\r
490 message.SetName("ClientGet");
\r
491 message["URI"]="USK"+publickey.substr(3)+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y.%m.%d")+"|MessageList/"+indexstr+"/MessageList.xml";
\r
492 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|_|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|"+message["URI"];
\r
493 message["ReturnType"]="direct";
\r
494 message["MaxSize"]="1000000";
\r
496 m_fcp->Send(message);
\r
498 m_requesting.push_back(id);
\r