X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ffreenet%2Fmessagerequester.cpp;h=adf8168cef16a6b9e4b08cbbf1228f12c99998d1;hb=4430e7762844c66428b6f822288beb71b7f82b95;hp=54b0d2618a30949202730c3259df70d4f93e455c;hpb=52c0819bfc1d083c6e0738f75f0d7eeba521295a;p=fms.git diff --git a/src/freenet/messagerequester.cpp b/src/freenet/messagerequester.cpp index 54b0d26..adf8168 100644 --- a/src/freenet/messagerequester.cpp +++ b/src/freenet/messagerequester.cpp @@ -81,6 +81,8 @@ const bool MessageRequester::HandleAllData(FCPMessage &message) long identityid; long index; bool inserted=false; + bool validmessage=true; + long savetoboardcount=0; StringFunctions::Split(message["Identifier"],"|",idparts); StringFunctions::Convert(message["DataLength"],datalength); @@ -115,6 +117,8 @@ const bool MessageRequester::HandleAllData(FCPMessage &message) if(xml.ParseXML(std::string(data.begin(),data.end()))==true) { std::vector boards=xml.GetBoards(); + std::map replyto=xml.GetInReplyTo(); + if(boards.size()>m_maxboardspermessage) { boards.resize(m_maxboardspermessage); @@ -130,61 +134,112 @@ 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 + + // make sure the reply board is on the board list we are saving - if not, replace the last element of boards with 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 + // 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=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;"); st.Bind(0,identityid); - st.Bind(1,GetIdentityName(identityid)); - st.Bind(2,xml.GetDate()); - st.Bind(3,xml.GetTime()); - st.Bind(4,xml.GetSubject()); - st.Bind(5,xml.GetMessageID()); - st.Bind(6,GetBoardID(xml.GetReplyBoard())); - st.Bind(7,xml.GetBody()); - inserted=st.Step(true); - int messageid=st.GetLastInsertRowID(); - - if(inserted==true) + st.Step(); + if(st.RowReturned()) { + std::vector uuidparts; + std::vector keyparts; + std::string keypart=""; + std::string publickey=""; - st=m_db->Prepare("INSERT INTO tblMessageBoard(MessageID,BoardID) VALUES(?,?);"); - for(std::vector::iterator i=boards.begin(); i!=boards.end(); i++) + st.ResultText(0,publickey); + + StringFunctions::SplitMultiple(publickey,"@,",keyparts); + StringFunctions::SplitMultiple(xml.GetMessageID(),"@",uuidparts); + + if(uuidparts.size()>1 && keyparts.size()>1) { - st.Bind(0,messageid); - st.Bind(1,GetBoardID((*i))); - st.Step(); - st.Reset(); + keypart=StringFunctions::Replace(StringFunctions::Replace(keyparts[1],"-",""),"~",""); + if(keypart!=uuidparts[1]) + { + m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageRequester::HandleAllData MessageID in Message doesn't match public key of identity : "+message["Identifier"]); + validmessage=false; + } } - 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++) + else { - 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::HandleAllData Error with identity's public key or Message ID : "+message["Identifier"]); + validmessage=false; } - st.Finalize(); - - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageRequester::HandleAllData parsed Message XML file : "+message["Identifier"]); - } - else // couldn't insert - was already in database + else { - //m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageRequester::HandleAddData could not insert message into database. "+message["Identifier"]); + m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageRequester::HandleAllData Error couldn't find identity : "+message["Identifier"]); + validmessage=false; } + // make sure we will at least save to 1 board before inserting message + savetoboardcount=0; + for(std::vector::iterator bi=boards.begin(); bi!=boards.end(); bi++) + { + if(SaveToBoard((*bi))) + { + savetoboardcount++; + } + } + + if(validmessage && savetoboardcount>0) + { + st=m_db->Prepare("INSERT INTO tblMessage(IdentityID,FromName,MessageDate,MessageTime,Subject,MessageUUID,ReplyBoardID,Body,MessageIndex) VALUES(?,?,?,?,?,?,?,?,?);"); + st.Bind(0,identityid); + st.Bind(1,GetIdentityName(identityid)); + st.Bind(2,xml.GetDate()); + st.Bind(3,xml.GetTime()); + st.Bind(4,xml.GetSubject()); + st.Bind(5,xml.GetMessageID()); + st.Bind(6,GetBoardID(xml.GetReplyBoard())); + st.Bind(7,xml.GetBody()); + st.Bind(8,index); + inserted=st.Step(true); + int messageid=st.GetLastInsertRowID(); + + if(inserted==true) + { + + st=m_db->Prepare("INSERT INTO tblMessageBoard(MessageID,BoardID) VALUES(?,?);"); + for(std::vector::iterator i=boards.begin(); i!=boards.end(); i++) + { + if(SaveToBoard((*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(?,?,?);"); + 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 + { + //m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageRequester::HandleAddData could not insert message into database. "+message["Identifier"]); + } + + } // if validmessage } else { @@ -294,7 +349,8 @@ void MessageRequester::PopulateIDList() 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+="WHERE (tblIdentity.LocalMessageTrust IS NULL OR tblIdentity.LocalMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalMessageTrust')) "; + sql+="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+=";"; @@ -325,6 +381,28 @@ void MessageRequester::PopulateIDList() } +const bool MessageRequester::SaveToBoard(const std::string &boardname) +{ + bool save=true; + SQLite3DB::Statement st=m_db->Prepare("SELECT SaveReceivedMessages FROM tblBoard WHERE BoardName=?;"); + st.Bind(0,boardname); + st.Step(); + if(st.RowReturned()) + { + std::string val=""; + st.ResultText(0,val); + if(val=="true") + { + save=true; + } + else + { + save=false; + } + } + return save; +} + void MessageRequester::StartRequest(const std::string &requestid) { FCPMessage message;