X-Git-Url: https://git.pterodactylus.net/?p=fms.git;a=blobdiff_plain;f=src%2Ffreenet%2Fmessagerequester.cpp;h=54b0d2618a30949202730c3259df70d4f93e455c;hp=7acb44d9af7119fa76f2206d725d66be5e31eff4;hb=52c0819bfc1d083c6e0738f75f0d7eeba521295a;hpb=6b896a9e1dc143bba86795be1e9336549db9b85f diff --git a/src/freenet/messagerequester.cpp b/src/freenet/messagerequester.cpp index 7acb44d..54b0d26 100644 --- a/src/freenet/messagerequester.cpp +++ b/src/freenet/messagerequester.cpp @@ -1,6 +1,8 @@ #include "../../include/freenet/messagerequester.h" #include "../../include/freenet/messagexml.h" +#include + #ifdef XMEM #include #endif @@ -10,7 +12,7 @@ MessageRequester::MessageRequester() Initialize(); } -MessageRequester::MessageRequester(FCPv2 *fcp):IIndexRequester(fcp) +MessageRequester::MessageRequester(FCPv2 *fcp):IIndexRequester(fcp) { Initialize(); } @@ -78,6 +80,7 @@ const bool MessageRequester::HandleAllData(FCPMessage &message) MessageXML xml; long identityid; long index; + bool inserted=false; StringFunctions::Split(message["Identifier"],"|",idparts); StringFunctions::Convert(message["DataLength"],datalength); @@ -112,6 +115,10 @@ const bool MessageRequester::HandleAllData(FCPMessage &message) if(xml.ParseXML(std::string(data.begin(),data.end()))==true) { std::vector boards=xml.GetBoards(); + if(boards.size()>m_maxboardspermessage) + { + boards.resize(m_maxboardspermessage); + } if(boards.size()<=0) { @@ -123,6 +130,15 @@ const bool MessageRequester::HandleAllData(FCPMessage &message) m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageRequester::HandleAllData Message XML did not contain a reply board! "+message["Identifier"]); return true; } + + // make sure the reply board is on the board list - if not, replace the last element of boardswith the reply board + if(xml.GetReplyBoard()!="" && std::find(boards.begin(),boards.end(),xml.GetReplyBoard())==boards.end() && boards.size()>0) + { + boards[boards.size()-1]=xml.GetReplyBoard(); + } + + // TODO make sure domain of message id match 43 characters of public key of identity (remove - and ~) - if not, discard message + // implement after 0.1.12 is released st=m_db->Prepare("INSERT INTO tblMessage(IdentityID,FromName,MessageDate,MessageTime,Subject,MessageUUID,ReplyBoardID,Body) VALUES(?,?,?,?,?,?,?,?);"); st.Bind(0,identityid); @@ -133,32 +149,41 @@ const bool MessageRequester::HandleAllData(FCPMessage &message) st.Bind(5,xml.GetMessageID()); st.Bind(6,GetBoardID(xml.GetReplyBoard())); st.Bind(7,xml.GetBody()); - st.Step(true); + inserted=st.Step(true); int messageid=st.GetLastInsertRowID(); - st=m_db->Prepare("INSERT INTO tblMessageBoard(MessageID,BoardID) VALUES(?,?);"); - for(std::vector::iterator i=boards.begin(); i!=boards.end(); i++) + if(inserted==true) { - st.Bind(0,messageid); - st.Bind(1,GetBoardID((*i))); - st.Step(); - st.Reset(); - } - st.Finalize(); - st=m_db->Prepare("INSERT INTO tblMessageReplyTo(MessageID,ReplyToMessageUUID,ReplyOrder) VALUES(?,?,?);"); - std::map replyto=xml.GetInReplyTo(); - for(std::map::iterator j=replyto.begin(); j!=replyto.end(); j++) + st=m_db->Prepare("INSERT INTO tblMessageBoard(MessageID,BoardID) VALUES(?,?);"); + for(std::vector::iterator i=boards.begin(); i!=boards.end(); i++) + { + st.Bind(0,messageid); + st.Bind(1,GetBoardID((*i))); + st.Step(); + st.Reset(); + } + st.Finalize(); + + st=m_db->Prepare("INSERT INTO tblMessageReplyTo(MessageID,ReplyToMessageUUID,ReplyOrder) VALUES(?,?,?);"); + std::map replyto=xml.GetInReplyTo(); + for(std::map::iterator j=replyto.begin(); j!=replyto.end(); j++) + { + st.Bind(0,messageid); + st.Bind(1,(*j).second); + st.Bind(2,(*j).first); + st.Step(); + st.Reset(); + } + st.Finalize(); + + m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageRequester::HandleAllData parsed Message XML file : "+message["Identifier"]); + + } + else // couldn't insert - was already in database { - st.Bind(0,messageid); - st.Bind(1,(*j).second); - st.Bind(2,(*j).first); - st.Step(); - st.Reset(); + //m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageRequester::HandleAddData could not insert message into database. "+message["Identifier"]); } - st.Finalize(); - - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageRequester::HandleAllData parsed Message XML file : "+message["Identifier"]); } else @@ -209,7 +234,7 @@ void MessageRequester::Initialize() { m_fcpuniquename="MessageRequester"; std::string tempval; - Option::instance()->Get("MaxMessageRequests",tempval); + Option::Instance()->Get("MaxMessageRequests",tempval); StringFunctions::Convert(tempval,m_maxrequests); if(m_maxrequests<1) { @@ -220,7 +245,7 @@ void MessageRequester::Initialize() { m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxMessageRequests is currently set at "+tempval+". This value might be incorrectly configured."); } - Option::instance()->Get("MessageDownloadMaxDaysBackward",tempval); + Option::Instance()->Get("MessageDownloadMaxDaysBackward",tempval); StringFunctions::Convert(tempval,m_maxdaysbackward); if(m_maxdaysbackward<0) { @@ -231,6 +256,28 @@ void MessageRequester::Initialize() { m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MessageDownloadMaxDaysBackward is currently set at "+tempval+". This value might be incorrectly configured."); } + Option::Instance()->Get("MaxPeerMessagesPerDay",tempval); + StringFunctions::Convert(tempval,m_maxpeermessages); + if(m_maxpeermessages<1) + { + m_maxpeermessages=1; + m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxPeerMessagesPerDay is currently set at "+tempval+". It must be 1 or greater."); + } + if(m_maxpeermessages<20 || m_maxpeermessages>1000) + { + m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxPeerMessagesPerDay is currently set at "+tempval+". This value might be incorrectly configured. The suggested value is 200."); + } + Option::Instance()->Get("MaxBoardsPerMessage",tempval); + StringFunctions::Convert(tempval,m_maxboardspermessage); + if(m_maxboardspermessage<1) + { + m_maxboardspermessage=1; + m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxBoardsPerMessage is currently set at "+tempval+". It must be 1 or greater."); + } + if(m_maxboardspermessage>20) + { + m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxBoardsPerMessage is currently set at "+tempval+". This value might be incorrectly configured."); + } } void MessageRequester::PopulateIDList() @@ -239,21 +286,39 @@ void MessageRequester::PopulateIDList() std::string val1; std::string val2; std::string val3; + std::string sql; + long requestindex; date.SetToGMTime(); date.Add(0,0,0,-m_maxdaysbackward); - SQLite3DB::Statement st=m_db->Prepare("SELECT tblIdentity.IdentityID,Day,RequestIndex FROM tblMessageRequests INNER JOIN tblIdentity ON tblMessageRequests.IdentityID=tblIdentity.IdentityID WHERE tblIdentity.LocalMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalMessageTrust') AND FromMessageList='true' AND Found='false' AND Day>='"+date.Format("%Y-%m-%d")+"';"); + sql="SELECT tblIdentity.IdentityID,Day,RequestIndex "; + sql+="FROM tblMessageRequests INNER JOIN tblIdentity ON tblMessageRequests.IdentityID=tblIdentity.IdentityID "; + sql+="WHERE tblIdentity.LocalMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalMessageTrust') AND FromMessageList='true' AND Found='false' AND Day>='"+date.Format("%Y-%m-%d")+"' "; + sql+="AND (tblIdentity.PeerMessageTrust IS NULL OR tblIdentity.PeerMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinPeerMessageTrust')) "; + sql+=";"; + + SQLite3DB::Statement st=m_db->Prepare(sql); st.Step(); + m_ids.clear(); + while(st.RowReturned()) { st.ResultText(0,val1); st.ResultText(1,val2); st.ResultText(2,val3); - if(m_ids.find(val1+"*"+val2+"*"+val3)==m_ids.end()) + + requestindex=0; + StringFunctions::Convert(val3,requestindex); + + // only continue if index is < max messages we will accept from a peer + if(requestindexSendMessage(message); m_requesting.push_back(requestid); + + m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageRequester::StartRequest requesting "+message["Identifier"]); } m_ids[requestid]=true; -} \ No newline at end of file +}