version 0.3.0
[fms.git] / src / message.cpp
index 8281db7..526dbd2 100644 (file)
@@ -1,10 +1,13 @@
 #include "../include/message.h"\r
 #include "../include/nntp/mime/Mime.h"\r
-#include "../include/uuidgenerator.h"\r
 #include "../include/stringfunctions.h"\r
 #include "../include/freenet/messagexml.h"\r
 #include "../include/option.h"\r
 \r
+#include <Poco/DateTimeParser.h>\r
+#include <Poco/DateTimeFormatter.h>\r
+#include <Poco/UUIDGenerator.h>\r
+#include <Poco/UUID.h>\r
 #include <algorithm>\r
 \r
 #ifdef DO_CHARSET_CONVERSION\r
@@ -61,10 +64,10 @@ const int Message::FindLocalIdentityID(const std::string &name)
        {\r
                if(m_addnewpostfromidentities==true)\r
                {\r
-                       DateTime now;\r
-                       now.SetToGMTime();\r
-                       st=m_db->Prepare("INSERT INTO tblLocalIdentity(Name) VALUES(?);");\r
+                       Poco::DateTime now;\r
+                       st=m_db->Prepare("INSERT INTO tblLocalIdentity(Name,DateCreated) VALUES(?,?);");\r
                        st.Bind(0,name);\r
+                       st.Bind(1,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));\r
                        st.Step(true);\r
                        return st.GetLastInsertRowID();\r
                }\r
@@ -110,7 +113,7 @@ const std::string Message::GetNNTPHeaders() const
        rval+="\r\n";\r
        rval+="Subject: "+m_subject+"\r\n";\r
        // format time as  : Wdy, DD Mon YY HH:MM:SS TIMEZONE\r
-       rval+="Date: "+m_datetime.Format("%a, %d %b %y %H:%M:%S -0000")+"\r\n";\r
+       rval+="Date: "+Poco::DateTimeFormatter::format(m_datetime,"%w, %d %b %y %H:%M:%S -0000")+"\r\n";\r
        if(m_inreplyto.size()>0)\r
        {\r
                rval+="References: ";\r
@@ -223,9 +226,20 @@ void Message::HandleAdministrationMessage()
                                        std::string messagebody;\r
                                        std::string messagetruststr="";\r
                                        std::string trustlisttruststr="";\r
-                                       UUIDGenerator uuid;\r
-                                       DateTime now;\r
-                                       now.SetToGMTime();\r
+\r
+                                       Poco::UUIDGenerator uuidgen;\r
+                                       Poco::UUID uuid;\r
+\r
+                                       try\r
+                                       {\r
+                                               uuid=uuidgen.createRandom();\r
+                                       }\r
+                                       catch(...)\r
+                                       {\r
+                                               m_log->fatal("Message::HandleAdministrationMessage could not generate a UUID");\r
+                                       }\r
+\r
+                                       Poco::DateTime now;\r
                                        StringFunctions::Convert(origmessagetrust,messagetruststr);\r
                                        StringFunctions::Convert(origtrustlisttrust,trustlisttruststr);\r
                                        messagebody="Trust List of "+m_fromname+"\r\n";\r
@@ -233,10 +247,10 @@ void Message::HandleAdministrationMessage()
                                        messagebody+="Local Message Trust : "+messagetruststr+"\r\n";\r
                                        messagebody+="Local Trust List Trust : "+trustlisttruststr+"\r\n";\r
                                        SQLite3DB::Statement insert=m_db->Prepare("INSERT INTO tblMessage(FromName,MessageDate,MessageTime,Subject,MessageUUID,ReplyBoardID,Body) VALUES('FMS',?,?,?,?,?,?);");\r
-                                       insert.Bind(0,now.Format("%Y-%m-%d"));\r
-                                       insert.Bind(1,now.Format("%H:%M:%S"));\r
+                                       insert.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));\r
+                                       insert.Bind(1,Poco::DateTimeFormatter::format(now,"%H:%M:%S"));\r
                                        insert.Bind(2,identityname+" Trust Changed");\r
-                                       insert.Bind(3,uuid.Generate());\r
+                                       insert.Bind(3,uuid.toString());\r
                                        insert.Bind(4,boardid);\r
                                        insert.Bind(5,messagebody);\r
                                        insert.Step(true);\r
@@ -247,7 +261,7 @@ void Message::HandleAdministrationMessage()
                                        insert.Bind(1,boardid);\r
                                        insert.Step();\r
 \r
-                                       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"Message::HandleAdministrationMessage updated "+identityname+" to "+messagetruststr+" , "+trustlisttruststr);\r
+                                       m_log->debug("Message::HandleAdministrationMessage updated "+identityname+" to "+messagetruststr+" , "+trustlisttruststr);\r
 \r
                                }\r
                        }\r
@@ -321,7 +335,7 @@ void Message::Initialize()
        m_subject="";\r
        m_body="";\r
        m_replyboardname="";\r
-       m_datetime.Set();\r
+       m_datetime=Poco::Timestamp();\r
        m_fromname="";\r
        m_boards.clear();\r
        m_inreplyto.clear();\r
@@ -381,10 +395,15 @@ const bool Message::Load(const long messageid, const long boardid)
                st.ResultText(4,m_replyboardname);\r
                st.ResultText(5,tempdate);\r
                st.ResultText(6,temptime);\r
-               m_datetime.Set(tempdate + " " + temptime);\r
                st.ResultText(7,m_fromname);\r
                st.Finalize();\r
 \r
+               int tzdiff=0;\r
+               if(Poco::DateTimeParser::tryParse(tempdate + " " + temptime,m_datetime,tzdiff)==false)\r
+               {\r
+                       m_log->error("Message::Load couldn't parse date/time "+tempdate+" "+temptime);\r
+               }\r
+\r
                // strip off any \r\n in subject\r
                m_subject=StringFunctions::Replace(m_subject,"\r\n","");\r
 \r
@@ -525,16 +544,24 @@ const bool Message::ParseNNTPMessage(const std::string &nntpmessage)
 \r
        Initialize();\r
 \r
-       UUIDGenerator uuid;\r
+       Poco::UUIDGenerator uuidgen;\r
+       Poco::UUID uuid;\r
        CMimeMessage mime;\r
        mime.Load(nntpmessage.c_str(),nntpmessage.size());\r
 \r
        // get header info\r
        // date is always set to now regardless of what message has\r
-       m_datetime.SetToGMTime();\r
+       m_datetime=Poco::Timestamp();\r
 \r
        // messageuuid is always a unique id we generate regardless of message message-id\r
-       m_messageuuid=uuid.Generate();\r
+       try\r
+       {\r
+               uuid=uuidgen.createRandom();\r
+       }\r
+       catch(...)\r
+       {\r
+               m_log->fatal("Message::ParseNNTPMessage could not create UUID");\r
+       }\r
        \r
        // get from\r
        if(mime.GetFieldValue("From"))\r
@@ -777,26 +804,26 @@ const bool Message::StartFreenetInsert()
 \r
                        if(min==max)\r
                        {\r
-                               m_datetime.Add(0,min);\r
+                               m_datetime+=Poco::Timespan(0,0,min,0,0);\r
                        }\r
                        else if(max>min)\r
                        {\r
                                int delay=(rand()%(max-min))+min;\r
-                               m_datetime.Add(0,delay);\r
+                               m_datetime+=Poco::Timespan(0,0,delay,0,0);\r
                        }\r
 \r
                }\r
                st.Finalize();\r
 \r
                // set date in xml file AFTER we set the delay\r
-               xml.SetDate(m_datetime.Format("%Y-%m-%d"));\r
-               xml.SetTime(m_datetime.Format("%H:%M:%S"));\r
+               xml.SetDate(Poco::DateTimeFormatter::format(m_datetime,"%Y-%m-%d"));\r
+               xml.SetTime(Poco::DateTimeFormatter::format(m_datetime,"%H:%M:%S"));\r
 \r
                st=m_db->Prepare("INSERT INTO tblMessageInserts(LocalIdentityID,MessageUUID,MessageXML,SendDate) VALUES(?,?,?,?);");\r
                st.Bind(0,localidentityid);\r
                st.Bind(1,m_messageuuid);\r
                st.Bind(2,xml.GetXML());\r
-               st.Bind(3,m_datetime.Format("%Y-%m-%d %H:%M:%S"));\r
+               st.Bind(3,Poco::DateTimeFormatter::format(m_datetime,"%Y-%m-%d %H:%M:%S"));\r
                st.Step();\r
 \r
                // insert file attachments into database\r