version 0.3.0
[fms.git] / src / freenet / trustlistinserter.cpp
index a858de1..fc02e8b 100644 (file)
@@ -3,6 +3,10 @@
 #include "../../include/freenet/trustlistxml.h"\r
 #include "../../include/stringfunctions.h"\r
 \r
+#include <Poco/Timespan.h>\r
+#include <Poco/DateTimeFormatter.h>\r
+#include <Poco/DateTimeParser.h>\r
+\r
 #ifdef XMEM\r
        #include <xmem.h>\r
 #endif\r
@@ -19,16 +23,15 @@ TrustListInserter::TrustListInserter(FCPv2 *fcp):IFCPConnected(fcp)
 \r
 void TrustListInserter::CheckForNeededInsert()\r
 {\r
-       DateTime date;\r
-       date.SetToGMTime();\r
-       int currentday=date.GetDay();\r
-       date.Add(0,0,-6);\r
+       Poco::DateTime date;\r
+       int currentday=date.day();\r
+       date-=Poco::Timespan(0,6,0,0,0);\r
        // insert trust lists every 6 hours - if 6 hours ago was different day then set to midnight of current day to insert list today ASAP\r
-       if(currentday!=date.GetDay())\r
+       if(currentday!=date.day())\r
        {\r
-               date.Set(date.GetYear(),date.GetMonth(),currentday);\r
+               date.assign(date.year(),date.month(),currentday,0,0,0);\r
        }\r
-       SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID, PrivateKey FROM tblLocalIdentity WHERE PrivateKey IS NOT NULL AND PrivateKey <> '' AND PublishTrustList='true' AND InsertingTrustList='false' AND (LastInsertedTrustList<='"+date.Format("%Y-%m-%d %H:%M:%S")+"' OR LastInsertedTrustList IS NULL);");\r
+       SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID, PrivateKey FROM tblLocalIdentity WHERE PrivateKey IS NOT NULL AND PrivateKey <> '' AND PublishTrustList='true' AND InsertingTrustList='false' AND (LastInsertedTrustList<='"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d %H:%M:%S")+"' OR LastInsertedTrustList IS NULL);");\r
 \r
        if(rs.Empty()==false)\r
        {\r
@@ -52,10 +55,9 @@ const bool TrustListInserter::HandleMessage(FCPMessage &message)
        if(message["Identifier"].find("TrustListInserter")==0)\r
        {\r
                \r
-               DateTime now;\r
+               Poco::DateTime now;\r
                std::vector<std::string> idparts;\r
 \r
-               now.SetToGMTime();\r
                StringFunctions::Split(message["Identifier"],"|",idparts);\r
 \r
                // no action for URIGenerated\r
@@ -75,9 +77,9 @@ const bool TrustListInserter::HandleMessage(FCPMessage &message)
                        // non USK\r
                        if(idparts[0]=="TrustListInserter")\r
                        {\r
-                               m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false', LastInsertedTrustList='"+now.Format("%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");\r
+                               m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false', LastInsertedTrustList='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");\r
                                m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");\r
-                               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListInserter::HandleMessage inserted TrustList xml");\r
+                               m_log->debug("TrustListInserter::HandleMessage inserted TrustList xml");\r
                        }\r
                        return true;\r
                }\r
@@ -88,7 +90,7 @@ const bool TrustListInserter::HandleMessage(FCPMessage &message)
                        if(idparts[0]=="TrustListInserter")\r
                        {\r
                                m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false' WHERE LocalIdentityID="+idparts[1]+";");\r
-                               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListInserter::HandleMessage failure inserting TrustList xml.  Code="+message["Code"]+" Description="+message["CodeDescription"]);\r
+                               m_log->debug("TrustListInserter::HandleMessage failure inserting TrustList xml.  Code="+message["Code"]+" Description="+message["CodeDescription"]);\r
                        \r
                                // if code 9 (collision), then insert index into inserted table\r
                                if(message["Code"]=="9")\r
@@ -107,16 +109,15 @@ const bool TrustListInserter::HandleMessage(FCPMessage &message)
 void TrustListInserter::Initialize()\r
 {\r
        Option::Instance()->Get("MessageBase",m_messagebase);\r
-       m_lastchecked.SetToGMTime();\r
+       m_lastchecked=Poco::Timestamp();\r
 }\r
 \r
 void TrustListInserter::Process()\r
 {\r
-       DateTime now;\r
-       now.SetToGMTime();\r
+       Poco::DateTime now;\r
 \r
        // check every minute\r
-       if(m_lastchecked<=(now-(1.0/1440.0)))\r
+       if(m_lastchecked<=(now-Poco::Timespan(0,0,1,0,0)))\r
        {\r
                CheckForNeededInsert();\r
                m_lastchecked=now;\r
@@ -139,7 +140,7 @@ void TrustListInserter::StartInsert(const long localidentityid, const std::strin
        std::string publickey;\r
        int messagetrust;\r
        int trustlisttrust;\r
-       DateTime now,date,dateminus30,tempdate;\r
+       Poco::DateTime now,date,dateminus30,tempdate;\r
        int index;\r
        std::string indexstr;\r
        std::string localidentityidstr;\r
@@ -150,10 +151,7 @@ void TrustListInserter::StartInsert(const long localidentityid, const std::strin
        bool add=false;\r
        std::string dateadded="";\r
 \r
-       now.SetToGMTime();\r
-       date.SetToGMTime();\r
-       dateminus30.SetToGMTime();\r
-       dateminus30.Add(0,0,0,-30);\r
+       dateminus30-=Poco::Timespan(30,0,0,0,0);\r
 \r
        // insert all identities not in trust list already\r
        m_db->Execute("INSERT INTO tblIdentityTrust(LocalIdentityID,IdentityID) SELECT LocalIdentityID,IdentityID FROM tblLocalIdentity,tblIdentity WHERE LocalIdentityID || '_' || IdentityID NOT IN (SELECT LocalIdentityID || '_' || IdentityID FROM tblIdentityTrust);");\r
@@ -162,11 +160,11 @@ void TrustListInserter::StartInsert(const long localidentityid, const std::strin
        SQLite3DB::Statement countst=m_db->Prepare("SELECT COUNT(*) FROM tblMessage WHERE IdentityID=? AND MessageDate>=?;");\r
 \r
        // build the xml file - we only want to add identities that we recently saw, otherwise we could be inserting a ton of identities\r
-       date.Add(0,0,0,-15);    // identities seen in last 15 days - the maintenance page lets us delete identities not seen in 20 days, so this gives us a window where the identity won't be deleted and then found in a trust list and readded immediately\r
+       date-=Poco::Timespan(15,0,0,0,0);       // identities seen in last 15 days - the maintenance page lets us delete identities not seen in 20 days, so this gives us a window where the identity won't be deleted and then found in a trust list and readded immediately\r
        //SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey, LocalMessageTrust, LocalTrustListTrust, MessageTrustComment, TrustListTrustComment FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey<>'' AND LastSeen>=?;");\r
        // we want to order by public key so we can't do identity correllation based on the sequence of identities in the list.\r
        SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey, tblIdentityTrust.LocalMessageTrust, tblIdentityTrust.LocalTrustListTrust, tblIdentityTrust.MessageTrustComment, tblIdentityTrust.TrustListTrustComment, tblIdentity.IdentityID, tblIdentity.DateAdded FROM tblIdentity INNER JOIN tblIdentityTrust ON tblIdentity.IdentityID=tblIdentityTrust.IdentityID WHERE PublicKey IS NOT NULL AND PublicKey<>'' AND LastSeen>=? AND tblIdentityTrust.LocalIdentityID=? ORDER BY PublicKey;");\r
-       st.Bind(0,date.Format("%Y-%m-%d"));\r
+       st.Bind(0,Poco::DateTimeFormatter::format(date,"%Y-%m-%d"));\r
        st.Bind(1,localidentityid);\r
        st.Step();\r
        while(st.RowReturned())\r
@@ -199,7 +197,7 @@ void TrustListInserter::StartInsert(const long localidentityid, const std::strin
 \r
                // add the identity to the trust list if they have posted a message in the last 30 days\r
                countst.Bind(0,identityid);\r
-               countst.Bind(1,dateminus30.Format("%Y-%m-%d"));\r
+               countst.Bind(1,Poco::DateTimeFormatter::format(dateminus30,"%Y-%m-%d"));\r
                countst.Step();\r
                if(countst.RowReturned())\r
                {\r
@@ -215,8 +213,13 @@ void TrustListInserter::StartInsert(const long localidentityid, const std::strin
                // no messages in last 30 days - add the identity if we learned about them less than 5 days ago\r
                if(add==false && dateadded!="")\r
                {\r
-                       tempdate.Set(dateadded);\r
-                       if(tempdate>=(now-5.0))\r
+                       int tzdiff=0;\r
+                       if(Poco::DateTimeParser::tryParse(dateadded,tempdate,tzdiff)==false)\r
+                       {\r
+                               tempdate=Poco::Timestamp();\r
+                               m_log->fatal("TrustListInserter::StartInsert could not parse date "+dateadded);\r
+                       }\r
+                       if(tempdate>=(now-Poco::Timespan(5,0,0,0,0)))\r
                        {\r
                                add=true;\r
                        }\r
@@ -233,7 +236,7 @@ void TrustListInserter::StartInsert(const long localidentityid, const std::strin
        // get next insert index\r
        st=m_db->Prepare("SELECT MAX(InsertIndex) FROM tblTrustListInserts WHERE LocalIdentityID=? AND Day=?;");\r
        st.Bind(0,localidentityid);\r
-       st.Bind(1,now.Format("%Y-%m-%d"));\r
+       st.Bind(1,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));\r
        st.Step();\r
 \r
        index=0;\r
@@ -250,7 +253,7 @@ void TrustListInserter::StartInsert(const long localidentityid, const std::strin
        StringFunctions::Convert(data.size(),datasizestr);\r
 \r
        message.SetName("ClientPut");\r
-       message["URI"]=privatekey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";\r
+       message["URI"]=privatekey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|TrustList|"+indexstr+".xml";\r
        message["Identifier"]="TrustListInserter|"+localidentityidstr+"|"+indexstr+"|"+message["URI"];\r
        message["UploadFrom"]="direct";\r
        message["DataLength"]=datasizestr;\r
@@ -260,7 +263,7 @@ void TrustListInserter::StartInsert(const long localidentityid, const std::strin
        // insert to USK\r
        message.Reset();\r
        message.SetName("ClientPutComplexDir");\r
-       message["URI"]="USK"+privatekey.substr(3)+m_messagebase+"|"+now.Format("%Y.%m.%d")+"|TrustList/0/";\r
+       message["URI"]="USK"+privatekey.substr(3)+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y.%m.%d")+"|TrustList/0/";\r
        message["Identifier"]="TrustListInserterUSK|"+message["URI"];\r
        message["DefaultName"]="TrustList.xml";\r
        message["Files.0.Name"]="TrustList.xml";\r