X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ffreenet%2Fsiteinserter.cpp;h=7ffee2553f3953c9a1029d06b44cb14ef98fece7;hb=59a5414ec47a2932a7802fcd1d98c4d80166564f;hp=f943f5618a144e17ea150bf0a2810760e4b10ef3;hpb=8a0a83a78390f22f99d4487cda2569909dfbc28e;p=fms.git diff --git a/src/freenet/siteinserter.cpp b/src/freenet/siteinserter.cpp index f943f56..7ffee25 100644 --- a/src/freenet/siteinserter.cpp +++ b/src/freenet/siteinserter.cpp @@ -1,15 +1,21 @@ #include "../../include/freenet/siteinserter.h" +#include "../../include/global.h" + +#include +#include +#include +#include #ifdef XMEM #include #endif -SiteInserter::SiteInserter() +SiteInserter::SiteInserter(SQLite3DB::DB *db):IIndexInserter(db) { Initialize(); } -SiteInserter::SiteInserter(FCPv2 *fcp):IIndexInserter(fcp) +SiteInserter::SiteInserter(SQLite3DB::DB *db, FCPv2::Connection *fcp):IIndexInserter(db,fcp) { Initialize(); } @@ -19,11 +25,11 @@ void SiteInserter::CheckForNeededInsert() // only do 1 insert at a time if(m_inserting.size()==0) { - DateTime date; - date.SetToGMTime(); + Poco::DateTime date; + date.assign(date.year(),date.month(),date.day(),0,0,0); SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID FROM tblLocalIdentity WHERE PublishFreesite='true' AND (LastInsertedFreesite IS NULL OR LastInsertedFreesiteFMS site of "+SanitizeOutput(name)+""; - - content+="

My last few posts

"; - SQLite3DB::Statement boardst=m_db->Prepare("SELECT tblBoard.BoardName FROM tblBoard INNER JOIN tblMessageBoard ON tblBoard.BoardID=tblMessageBoard.BoardID WHERE tblMessageBoard.MessageID=? ORDER BY tblBoard.BoardName COLLATE NOCASE;"); SQLite3DB::Statement st=m_db->Prepare("SELECT tblMessage.Body, tblMessage.Subject, tblMessage.MessageID FROM tblMessage INNER JOIN tblIdentity ON tblMessage.IdentityID=tblIdentity.IdentityID INNER JOIN tblLocalIdentity ON tblIdentity.PublicKey=tblLocalIdentity.PublicKey WHERE tblLocalIdentity.LocalIdentityID=? ORDER BY tblMessage.MessageDate DESC, tblMessage.MessageTime DESC LIMIT 0,10;"); st.Bind(0,localidentityid); @@ -83,6 +85,9 @@ std::string SiteInserter::GenerateIndex(const std::string &htmltemplate, const l content+=""; content+="
"; content+=SanitizeOutput(post); + //post=SanitizeOutput(post); + //StringFunctions::Replace(post,"\r\n","
"); + //content+=post; content+="
"; content+=""; @@ -112,7 +117,7 @@ std::string SiteInserter::GenerateLinks(const bool publishtrustlist, const bool void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey, std::map &pages) { - SQLite3DB::Statement st=m_db->Prepare("SELECT Name, PrivateKey, PublishTrustList, PublishBoardList FROM tblLocalIdentity WHERE LocalIdentityID=?;"); + SQLite3DB::Statement st=m_db->Prepare("SELECT Name, PrivateKey, PublishTrustList, PublishBoardList, FreesiteEdition FROM tblLocalIdentity WHERE LocalIdentityID=?;"); st.Bind(0,localidentityid); st.Step(); @@ -126,20 +131,44 @@ void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey std::string publishboardliststr=""; bool publishtrustlist=false; bool publishboardlist=false; + std::string editionstr=""; st.ResultText(0,name); st.ResultText(1,key); st.ResultText(2,publishtrustliststr); st.ResultText(3,publishboardliststr); + st.ResultText(4,editionstr); publishtrustliststr=="true" ? publishtrustlist=true : publishtrustlist=false; publishboardliststr=="true" ? publishboardlist=true : publishboardlist=false; + // no edition exists - start at 0 + if(editionstr=="") + { + editionstr="0"; + } + // previous edition exists - add 1 + else + { + int edition=0; + StringFunctions::Convert(editionstr,edition); + edition++; + StringFunctions::Convert(edition,editionstr); + } + + // make SSK a USK + if(key.find("SSK@")==0) + { + key.erase(0,3); + key="USK"+key; + } + key+=m_messagebase+"/"+editionstr+"/"; + uskkey=key; filename=name+"-template.htm"; - FILE *infile=fopen(filename.c_str(),"r+b"); + FILE *infile=fopen(filename.c_str(),"rb"); if(!infile) { - infile=fopen("site-template.htm","r+b"); + infile=fopen("site-template.htm","rb"); } if(infile) { @@ -155,6 +184,7 @@ void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey htmltemplate.append(data.begin(),data.end()); htmltemplate=StringFunctions::Replace(htmltemplate,"[LINKS]",GenerateLinks(publishtrustlist,publishboardlist)); + htmltemplate=StringFunctions::Replace(htmltemplate,"[IDENTITYNAME]",SanitizeOutput(name)); pages["index.htm"]=GenerateIndex(htmltemplate,localidentityid,name); if(publishtrustlist) @@ -166,19 +196,70 @@ void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey // pages["boardlist.htm"]=GenerateBoardList(htmltemplate,localidentityid,name); } - // make SSK a USK - if(key.find("SSK@")==0) - { - key.erase(0,3); - key="USK"+key; - } - key+=m_messagebase+"/0/"; - uskkey=key; - } else { - LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::GeneratePages unable to open "+filename+" or site-template.htm."); + m_log->error("SiteInserter::GeneratePages unable to open "+filename+" or site-template.htm."); + } + + // get extra files that the user wants to add to the Freesite + filename=name+"-files.txt"; + infile=fopen(filename.c_str(),"rb"); + if(infile) + { + std::vector files; + + fseek(infile,0,SEEK_END); + long len=ftell(infile); + fseek(infile,0,SEEK_SET); + + std::vector data; + data.resize(len); + fread(&data[0],1,data.size(),infile); + fclose(infile); + + // split on \r and \n - on systems with \r\n line endings there will be blank entries, but we'll just skip those + std::string filecontent(data.begin(),data.end()); + StringFunctions::SplitMultiple(filecontent,"\r\n",files); + + for(std::vector::iterator i=files.begin(); i!=files.end(); i++) + { + if((*i)!="" && (*i).find("index.htm")==std::string::npos && (*i).find("trustlist.htm")==std::string::npos && (*i).find("files.htm")==std::string::npos) + { + filename=(*i); + infile=fopen(filename.c_str(),"rb"); + if(infile) + { + fseek(infile,0,SEEK_END); + len=ftell(infile); + fseek(infile,0,SEEK_SET); + + data.resize(len); + fread(&data[0],1,data.size(),infile); + fclose(infile); + + filecontent=""; + filecontent.append(data.begin(),data.end()); + + // strip off path from filename + while(filename.find_first_of("/")!=std::string::npos) + { + filename.erase(0,filename.find_first_of("/")+1); + } + + if(filecontent.size()>0) + { + pages[filename]=filecontent; + } + + } + else + { + m_log->error("SiteInserter::GeneratePages could not include user file "+(*i)); + } + } + } + } } @@ -187,19 +268,19 @@ void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey std::string SiteInserter::GenerateTrustList(const std::string &htmltemplate, const long localidentityid, const std::string &name) { std::string content=""; - DateTime date; + Poco::DateTime date; - date.SetToGMTime(); - date.Add(0,0,0,-20); - SQLite3DB::Statement st=m_db->Prepare("SELECT Name,PublicKey,LocalMessageTrust,LocalTrustListTrust,IdentityID,MessageTrustComment,TrustListTrustComment FROM tblIdentity WHERE PublicKey IS NOT NULL AND LastSeen IS NOT NULL AND LastSeen>=? ORDER BY Name COLLATE NOCASE;"); - st.Bind(0,date.Format("%Y-%m-%d %H:%M:%S")); + date-=Poco::Timespan(20,0,0,0,0); + SQLite3DB::Statement st=m_db->Prepare("SELECT Name,PublicKey,tblIdentityTrust.LocalMessageTrust,tblIdentityTrust.LocalTrustListTrust,tblIdentity.IdentityID,tblIdentityTrust.MessageTrustComment,tblIdentityTrust.TrustListTrustComment,tblIdentity.FreesiteEdition FROM tblIdentity LEFT JOIN (SELECT IdentityID,LocalMessageTrust,LocalTrustListTrust,MessageTrustComment,TrustListTrustComment FROM tblIdentityTrust WHERE LocalIdentityID=?) AS 'tblIdentityTrust' ON tblIdentity.IdentityID=tblIdentityTrust.IdentityID WHERE PublicKey IS NOT NULL AND LastSeen IS NOT NULL AND LastSeen>=? ORDER BY Name COLLATE NOCASE;"); + st.Bind(0,localidentityid); + st.Bind(1,Poco::DateTimeFormatter::format(date,"%Y-%m-%d %H:%M:%S")); st.Step(); - content+=""; - content+="
"; + content+=""; + content+=""; content+="Trust List of "+SanitizeOutput(name); content+=""; - content+=""; + content+=""; while(st.RowReturned()) { std::string idname=""; @@ -209,8 +290,8 @@ std::string SiteInserter::GenerateTrustList(const std::string &htmltemplate, con std::string messagetrust=""; std::string trustlisttrust=""; std::string publickey=""; - std::string keypart=""; std::string uskkey=""; + std::string freesiteedition=""; st.ResultText(0,idname); st.ResultText(1,publickey); @@ -219,22 +300,28 @@ std::string SiteInserter::GenerateTrustList(const std::string &htmltemplate, con st.ResultText(4,thisid); st.ResultText(5,messagetrustcomment); st.ResultText(6,trustlisttrustcomment); + st.ResultText(7,freesiteedition); - if(publickey.size()>8) + if(freesiteedition!="") { - keypart=publickey.substr(3,5); + if(publickey.find("SSK@")==0) + { + uskkey=publickey; + uskkey.erase(0,3); + uskkey="USK"+uskkey; + uskkey+=m_messagebase+"/"+freesiteedition+"/"; + } } - if(publickey.find("SSK@")==0) + content+=""; + if(freesiteedition!="") { - uskkey=publickey; - uskkey.erase(0,3); - uskkey="USK"+uskkey; - uskkey+=m_messagebase+"/0/"; + content+=""; + } + else + { + content+=""; } - - content+=""; - content+=""; content+=""; content+=""; content+=""; @@ -243,6 +330,7 @@ std::string SiteInserter::GenerateTrustList(const std::string &htmltemplate, con st.Step(); } + content+="
Message TrustMessage CommentTrust List TrustTrust Comment
Message TrustMessage CommentTrust List TrustTrust Comment
"+SanitizeOutput(CreateShortIdentityName(idname,publickey))+"
"+SanitizeOutput(idname+keypart)+"..."+messagetrust+""+SanitizeOutput(messagetrustcomment)+""+trustlisttrust+"
"; return StringFunctions::Replace(htmltemplate,"[CONTENT]",content); @@ -267,7 +355,7 @@ const std::string SiteInserter::GetClassString(const std::string &trustlevel) } } -const bool SiteInserter::HandlePutFailed(FCPMessage &message) +const bool SiteInserter::HandlePutFailed(FCPv2::Message &message) { std::vector idparts; long localidentityid; @@ -277,26 +365,36 @@ const bool SiteInserter::HandlePutFailed(FCPMessage &message) RemoveFromInsertList(localidentityid); + m_log->error("SiteInserter::HandlePutFailed failed to insert Freesite, Freenet error code : "+message["Code"]); + return true; } -const bool SiteInserter::HandlePutSuccessful(FCPMessage &message) +const bool SiteInserter::HandlePutSuccessful(FCPv2::Message &message) { std::vector idparts; + std::vector uriparts; long localidentityid; - DateTime now; - - now.SetToGMTime(); + int edition=-1; + Poco::DateTime now; StringFunctions::Split(message["Identifier"],"|",idparts); StringFunctions::Convert(idparts[1],localidentityid); - SQLite3DB::Statement st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedFreesite=? WHERE LocalIdentityID=?;"); - st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S")); - st.Bind(1,localidentityid); + // edition is very last part of uri + StringFunctions::Split(message["URI"],"/",uriparts); + if(uriparts.size()>0) + { + StringFunctions::Convert(uriparts[uriparts.size()-1],edition); + } + + SQLite3DB::Statement st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedFreesite=?, FreesiteEdition=? WHERE LocalIdentityID=?;"); + st.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S")); + st.Bind(1,edition); + st.Bind(2,localidentityid); st.Step(); - m_log->WriteLog(LogFile::LOGLEVEL_INFO,"SiteInserter::HandlePutSuccessful successfully inserted Freesite."); + m_log->information("SiteInserter::HandlePutSuccessful successfully inserted Freesite."); RemoveFromInsertList(localidentityid); @@ -319,9 +417,9 @@ const std::string SiteInserter::SanitizeOutput(const std::string &input) return output; } -void SiteInserter::StartInsert(const long &localidentityid) +const bool SiteInserter::StartInsert(const long &localidentityid) { - FCPMessage message; + FCPv2::Message message; std::string localidentityidstr=""; std::string sizestr=""; std::string uskkey=""; @@ -353,14 +451,16 @@ void SiteInserter::StartInsert(const long &localidentityid) filenum++; } - m_fcp->SendMessage(message); + m_fcp->Send(message); // send data of each page for(std::map::iterator pagei=pages.begin(); pagei!=pages.end(); pagei++) { - m_fcp->SendRaw(&(*pagei).second[0],(*pagei).second.size()); + m_fcp->Send(std::vector((*pagei).second.begin(),(*pagei).second.end())); } m_inserting.push_back(localidentityid); + return true; + }