version 0.3.29
[fms.git] / src / freenet / siteinserter.cpp
index e54a28e..7ffee25 100644 (file)
@@ -1,16 +1,21 @@
 #include "../../include/freenet/siteinserter.h"\r
 #include "../../include/global.h"\r
 \r
+#include <Poco/DateTime.h>\r
+#include <Poco/Timespan.h>\r
+#include <Poco/DateTimeFormatter.h>\r
+#include <cstdio>\r
+\r
 #ifdef XMEM\r
        #include <xmem.h>\r
 #endif\r
 \r
-SiteInserter::SiteInserter()\r
+SiteInserter::SiteInserter(SQLite3DB::DB *db):IIndexInserter<long>(db)\r
 {\r
        Initialize();\r
 }\r
 \r
-SiteInserter::SiteInserter(FCPv2 *fcp):IIndexInserter<long>(fcp)\r
+SiteInserter::SiteInserter(SQLite3DB::DB *db, FCPv2::Connection *fcp):IIndexInserter<long>(db,fcp)\r
 {\r
        Initialize();\r
 }\r
@@ -20,15 +25,11 @@ void SiteInserter::CheckForNeededInsert()
        // only do 1 insert at a time\r
        if(m_inserting.size()==0)\r
        {\r
-               DateTime date;\r
-               date.SetToGMTime();\r
-               date.SetHour(0);\r
-               date.SetMinute(0);\r
-               date.SetSecond(0);\r
-               date.Normalize();\r
+               Poco::DateTime date;\r
+               date.assign(date.year(),date.month(),date.day(),0,0,0);\r
 \r
                SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID FROM tblLocalIdentity WHERE PublishFreesite='true' AND (LastInsertedFreesite IS NULL OR LastInsertedFreesite<?);");\r
-               st.Bind(0,date.Format("%Y-%m-%d"));\r
+               st.Bind(0,Poco::DateTimeFormatter::format(date,"%Y-%m-%d"));\r
 \r
                st.Step();\r
                if(st.RowReturned())\r
@@ -44,8 +45,6 @@ std::string SiteInserter::GenerateIndex(const std::string &htmltemplate, const l
 {\r
        std::string content="";\r
 \r
-       content+="<h3>My last few posts</h3>";\r
-\r
        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;");\r
        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;");\r
        st.Bind(0,localidentityid);\r
@@ -156,11 +155,20 @@ void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey
                        StringFunctions::Convert(edition,editionstr);\r
                }\r
 \r
+               // make SSK a USK\r
+               if(key.find("SSK@")==0)\r
+               {\r
+                       key.erase(0,3);\r
+                       key="USK"+key;\r
+               }\r
+               key+=m_messagebase+"/"+editionstr+"/";\r
+               uskkey=key;\r
+\r
                filename=name+"-template.htm";\r
-               FILE *infile=fopen(filename.c_str(),"r+b");\r
+               FILE *infile=fopen(filename.c_str(),"rb");\r
                if(!infile)\r
                {\r
-                       infile=fopen("site-template.htm","r+b");\r
+                       infile=fopen("site-template.htm","rb");\r
                }\r
                if(infile)\r
                {\r
@@ -188,24 +196,15 @@ void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey
 //                             pages["boardlist.htm"]=GenerateBoardList(htmltemplate,localidentityid,name);\r
                        }\r
 \r
-                       // make SSK a USK\r
-                       if(key.find("SSK@")==0)\r
-                       {\r
-                               key.erase(0,3);\r
-                               key="USK"+key;\r
-                       }\r
-                       key+=m_messagebase+"/"+editionstr+"/";\r
-                       uskkey=key;\r
-\r
                }\r
                else\r
                {\r
-                       LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::GeneratePages unable to open "+filename+" or site-template.htm.");\r
+                       m_log->error("SiteInserter::GeneratePages unable to open "+filename+" or site-template.htm.");\r
                }\r
 \r
                // get extra files that the user wants to add to the Freesite\r
                filename=name+"-files.txt";\r
-               infile=fopen(filename.c_str(),"r+b");\r
+               infile=fopen(filename.c_str(),"rb");\r
                if(infile)\r
                {\r
                        std::vector<std::string> files;\r
@@ -228,7 +227,7 @@ void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey
                                if((*i)!="" && (*i).find("index.htm")==std::string::npos && (*i).find("trustlist.htm")==std::string::npos && (*i).find("files.htm")==std::string::npos)\r
                                {\r
                                        filename=(*i);\r
-                                       infile=fopen(filename.c_str(),"r+b");\r
+                                       infile=fopen(filename.c_str(),"rb");\r
                                        if(infile)\r
                                        {\r
                                                fseek(infile,0,SEEK_END);\r
@@ -256,7 +255,7 @@ void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey
                                        }\r
                                        else\r
                                        {\r
-                                               m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::GeneratePages could not include user file "+(*i));\r
+                                               m_log->error("SiteInserter::GeneratePages could not include user file "+(*i));\r
                                        }\r
                                }\r
                        }\r
@@ -269,20 +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)\r
 {\r
        std::string content="";\r
-       DateTime date;\r
+       Poco::DateTime date;\r
 \r
-       date.SetToGMTime();\r
-       date.Add(0,0,0,-20);\r
+       date-=Poco::Timespan(20,0,0,0,0);\r
        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;");\r
        st.Bind(0,localidentityid);\r
-       st.Bind(1,date.Format("%Y-%m-%d %H:%M:%S"));\r
+       st.Bind(1,Poco::DateTimeFormatter::format(date,"%Y-%m-%d %H:%M:%S"));\r
        st.Step();\r
 \r
-       content+="<table>";\r
-       content+="<tr><th colspan=\"5\">";\r
+       content+="<table class=\"trustlist\">";\r
+       content+="<tr class=\"title\"><thcolspan=\"5\">";\r
        content+="Trust List of "+SanitizeOutput(name);\r
        content+="</th></tr>";\r
-       content+="<tr><td></td><th>Message Trust</th><th>Message Comment</th><th>Trust List Trust</th><th>Trust Comment</th></tr>";\r
+       content+="<tr class=\"headings\"><th></th><th>Message Trust</th><th>Message Comment</th><th>Trust List Trust</th><th>Trust Comment</th></tr>";\r
        while(st.RowReturned())\r
        {\r
                std::string idname="";\r
@@ -318,11 +316,11 @@ std::string SiteInserter::GenerateTrustList(const std::string &htmltemplate, con
                content+="<tr>";\r
                if(freesiteedition!="")\r
                {\r
-                       content+="<td><a href=\""+uskkey+"\">"+SanitizeOutput(CreateShortIdentityName(idname,publickey))+"</a></td>";\r
+                       content+="<td><div><a href=\""+uskkey+"\">"+SanitizeOutput(CreateShortIdentityName(idname,publickey))+"</a></div></td>";\r
                }\r
                else\r
                {\r
-                       content+="<td>"+SanitizeOutput(CreateShortIdentityName(idname,publickey))+"</td>";\r
+                       content+="<td><div>"+SanitizeOutput(CreateShortIdentityName(idname,publickey))+"</div></td>";\r
                }\r
                content+="<td "+GetClassString(messagetrust)+">"+messagetrust+"</td>";\r
                content+="<td>"+SanitizeOutput(messagetrustcomment)+"</td>";\r
@@ -332,6 +330,7 @@ std::string SiteInserter::GenerateTrustList(const std::string &htmltemplate, con
 \r
                st.Step();\r
        }\r
+       content+="</table>";\r
 \r
        return StringFunctions::Replace(htmltemplate,"[CONTENT]",content);\r
 \r
@@ -356,7 +355,7 @@ const std::string SiteInserter::GetClassString(const std::string &trustlevel)
        }\r
 }\r
 \r
-const bool SiteInserter::HandlePutFailed(FCPMessage &message)\r
+const bool SiteInserter::HandlePutFailed(FCPv2::Message &message)\r
 {\r
        std::vector<std::string> idparts;\r
        long localidentityid;\r
@@ -366,20 +365,18 @@ const bool SiteInserter::HandlePutFailed(FCPMessage &message)
 \r
        RemoveFromInsertList(localidentityid);\r
 \r
-       m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::HandlePutFailed failed to insert Freesite, Freenet error code : "+message["Code"]);\r
+       m_log->error("SiteInserter::HandlePutFailed failed to insert Freesite, Freenet error code : "+message["Code"]);\r
 \r
        return true;\r
 }\r
 \r
-const bool SiteInserter::HandlePutSuccessful(FCPMessage &message)\r
+const bool SiteInserter::HandlePutSuccessful(FCPv2::Message &message)\r
 {\r
        std::vector<std::string> idparts;\r
        std::vector<std::string> uriparts;\r
        long localidentityid;\r
        int edition=-1;\r
-       DateTime now;\r
-\r
-       now.SetToGMTime();\r
+       Poco::DateTime now;\r
 \r
        StringFunctions::Split(message["Identifier"],"|",idparts);\r
        StringFunctions::Convert(idparts[1],localidentityid);\r
@@ -392,12 +389,12 @@ const bool SiteInserter::HandlePutSuccessful(FCPMessage &message)
        }\r
 \r
        SQLite3DB::Statement st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedFreesite=?, FreesiteEdition=? WHERE LocalIdentityID=?;");\r
-       st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S"));\r
+       st.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));\r
        st.Bind(1,edition);\r
        st.Bind(2,localidentityid);\r
        st.Step();\r
 \r
-       m_log->WriteLog(LogFile::LOGLEVEL_INFO,"SiteInserter::HandlePutSuccessful successfully inserted Freesite.");\r
+       m_log->information("SiteInserter::HandlePutSuccessful successfully inserted Freesite.");\r
 \r
        RemoveFromInsertList(localidentityid);\r
 \r
@@ -422,7 +419,7 @@ const std::string SiteInserter::SanitizeOutput(const std::string &input)
 \r
 const bool SiteInserter::StartInsert(const long &localidentityid)\r
 {\r
-       FCPMessage message;\r
+       FCPv2::Message message;\r
        std::string localidentityidstr="";\r
        std::string sizestr="";\r
        std::string uskkey="";\r
@@ -454,12 +451,12 @@ const bool SiteInserter::StartInsert(const long &localidentityid)
                filenum++;\r
        }\r
 \r
-       m_fcp->SendMessage(message);\r
+       m_fcp->Send(message);\r
 \r
        // send data of each page\r
        for(std::map<std::string,std::string>::iterator pagei=pages.begin(); pagei!=pages.end(); pagei++)\r
        {\r
-               m_fcp->SendRaw(&(*pagei).second[0],(*pagei).second.size());\r
+               m_fcp->Send(std::vector<char>((*pagei).second.begin(),(*pagei).second.end()));\r
        }\r
 \r
        m_inserting.push_back(localidentityid);\r