X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmessage.cpp;h=897ceedbcede5ba22c762f6daa83c783ee24c86b;hb=047cea32f848d605c549ec123c12c1c400dd7ec1;hp=eea0633477f6d423d609e0ff33f7aa4bd1fa12ee;hpb=b4f4686250878cdf4fcb2986a2ea6546cba867d1;p=fms.git diff --git a/src/message.cpp b/src/message.cpp index eea0633..897ceed 100644 --- a/src/message.cpp +++ b/src/message.cpp @@ -1,12 +1,19 @@ #include "../include/message.h" #include "../include/nntp/mime/Mime.h" -#include "../include/uuidgenerator.h" #include "../include/stringfunctions.h" #include "../include/freenet/messagexml.h" #include "../include/option.h" +#include +#include +#include +#include #include +#ifdef DO_CHARSET_CONVERSION + #include "../include/charsetconverter.h" +#endif + #ifdef XMEM #include #endif @@ -57,10 +64,10 @@ const int Message::FindLocalIdentityID(const std::string &name) { if(m_addnewpostfromidentities==true) { - DateTime now; - now.SetToGMTime(); - st=m_db->Prepare("INSERT INTO tblLocalIdentity(Name) VALUES(?);"); + Poco::DateTime now; + st=m_db->Prepare("INSERT INTO tblLocalIdentity(Name,DateCreated) VALUES(?,?);"); st.Bind(0,name); + st.Bind(1,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S")); st.Step(true); return st.GetLastInsertRowID(); } @@ -106,7 +113,7 @@ const std::string Message::GetNNTPHeaders() const rval+="\r\n"; rval+="Subject: "+m_subject+"\r\n"; // format time as : Wdy, DD Mon YY HH:MM:SS TIMEZONE - rval+="Date: "+m_datetime.Format("%a, %d %b %y %H:%M:%S -0000")+"\r\n"; + rval+="Date: "+Poco::DateTimeFormatter::format(m_datetime,"%w, %d %b %y %H:%M:%S -0000")+"\r\n"; if(m_inreplyto.size()>0) { rval+="References: "; @@ -179,7 +186,8 @@ void Message::HandleAdministrationMessage() } else { - origmessagetrust=m_minlocalmessagetrust; + //origmessagetrust=m_minlocalmessagetrust; + origmessagetrust=50; } if(origmess.ResultNull(3)==false) { @@ -187,7 +195,8 @@ void Message::HandleAdministrationMessage() } else { - origtrustlisttrust=m_minlocaltrustlisttrust; + //origtrustlisttrust=m_minlocaltrustlisttrust; + origtrustlisttrust=50; } origmessagetrust+=changemessagetrust; @@ -217,9 +226,20 @@ void Message::HandleAdministrationMessage() std::string messagebody; std::string messagetruststr=""; std::string trustlisttruststr=""; - UUIDGenerator uuid; - DateTime now; - now.SetToGMTime(); + + Poco::UUIDGenerator uuidgen; + Poco::UUID uuid; + + try + { + uuid=uuidgen.createRandom(); + } + catch(...) + { + m_log->fatal("Message::HandleAdministrationMessage could not generate a UUID"); + } + + Poco::DateTime now; StringFunctions::Convert(origmessagetrust,messagetruststr); StringFunctions::Convert(origtrustlisttrust,trustlisttruststr); messagebody="Trust List of "+m_fromname+"\r\n"; @@ -227,10 +247,12 @@ void Message::HandleAdministrationMessage() messagebody+="Local Message Trust : "+messagetruststr+"\r\n"; messagebody+="Local Trust List Trust : "+trustlisttruststr+"\r\n"; SQLite3DB::Statement insert=m_db->Prepare("INSERT INTO tblMessage(FromName,MessageDate,MessageTime,Subject,MessageUUID,ReplyBoardID,Body) VALUES('FMS',?,?,?,?,?,?);"); - insert.Bind(0,now.Format("%Y-%m-%d")); - insert.Bind(1,now.Format("%H:%M:%S")); + insert.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d")); + insert.Bind(1,Poco::DateTimeFormatter::format(now,"%H:%M:%S")); insert.Bind(2,identityname+" Trust Changed"); - insert.Bind(3,uuid.Generate()); + std::string uuidstr=uuid.toString(); + StringFunctions::UpperCase(uuidstr,uuidstr); + insert.Bind(3,uuidstr); insert.Bind(4,boardid); insert.Bind(5,messagebody); insert.Step(true); @@ -241,7 +263,7 @@ void Message::HandleAdministrationMessage() insert.Bind(1,boardid); insert.Step(); - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"Message::HandleAdministrationMessage updated "+identityname+" to "+messagetruststr+" , "+trustlisttruststr); + m_log->debug("Message::HandleAdministrationMessage updated "+identityname+" to "+messagetruststr+" , "+trustlisttruststr); } } @@ -278,7 +300,8 @@ void Message::HandleChangeTrust() } else { - localmessagetrust=m_minlocalmessagetrust; + //localmessagetrust=m_minlocalmessagetrust; + localmessagetrust=50; } localmessagetrust+=m_changemessagetrustonreply; @@ -314,10 +337,11 @@ void Message::Initialize() m_subject=""; m_body=""; m_replyboardname=""; - m_datetime.Set(); + m_datetime=Poco::Timestamp(); m_fromname=""; m_boards.clear(); m_inreplyto.clear(); + m_fileattachments.clear(); m_changemessagetrustonreply=0; Option::Instance()->Get("ChangeMessageTrustOnReply",tempval); StringFunctions::Convert(tempval,m_changemessagetrustonreply); @@ -373,10 +397,15 @@ const bool Message::Load(const long messageid, const long boardid) st.ResultText(4,m_replyboardname); st.ResultText(5,tempdate); st.ResultText(6,temptime); - m_datetime.Set(tempdate + " " + temptime); st.ResultText(7,m_fromname); st.Finalize(); + int tzdiff=0; + if(Poco::DateTimeParser::tryParse(tempdate + " " + temptime,m_datetime,tzdiff)==false) + { + m_log->error("Message::Load couldn't parse date/time "+tempdate+" "+temptime); + } + // strip off any \r\n in subject m_subject=StringFunctions::Replace(m_subject,"\r\n",""); @@ -517,16 +546,26 @@ const bool Message::ParseNNTPMessage(const std::string &nntpmessage) Initialize(); - UUIDGenerator uuid; + Poco::UUIDGenerator uuidgen; + Poco::UUID uuid; CMimeMessage mime; mime.Load(nntpmessage.c_str(),nntpmessage.size()); // get header info // date is always set to now regardless of what message has - m_datetime.SetToGMTime(); + m_datetime=Poco::Timestamp(); // messageuuid is always a unique id we generate regardless of message message-id - m_messageuuid=uuid.Generate(); + try + { + uuid=uuidgen.createRandom(); + m_messageuuid=uuid.toString(); + StringFunctions::UpperCase(m_messageuuid,m_messageuuid); + } + catch(...) + { + m_log->fatal("Message::ParseNNTPMessage could not create UUID"); + } // get from if(mime.GetFieldValue("From")) @@ -534,6 +573,7 @@ const bool Message::ParseNNTPMessage(const std::string &nntpmessage) m_fromname=mime.GetFieldValue("From"); // remove any path folding m_fromname=StringFunctions::Replace(m_fromname,"\r\n",""); + m_fromname=StringFunctions::Replace(m_fromname,"\t",""); // strip off everything between () and <> and any whitespace std::string::size_type startpos=m_fromname.find("("); std::string::size_type endpos; @@ -578,6 +618,7 @@ const bool Message::ParseNNTPMessage(const std::string &nntpmessage) std::string temp=mime.GetFieldValue("Newsgroups"); // remove any path folding temp=StringFunctions::Replace(temp,"\r\n",""); + temp=StringFunctions::Replace(temp,"\t",""); std::vector parts; StringFunctions::SplitMultiple(temp,", \t",parts); for(std::vector::iterator i=parts.begin(); i!=parts.end(); i++) @@ -597,6 +638,7 @@ const bool Message::ParseNNTPMessage(const std::string &nntpmessage) m_replyboardname=mime.GetFieldValue("Followup-To"); // remove any path folding m_replyboardname=StringFunctions::Replace(m_replyboardname,"\r\n",""); + m_replyboardname=StringFunctions::Replace(m_replyboardname,"\t",""); } else { @@ -611,6 +653,20 @@ const bool Message::ParseNNTPMessage(const std::string &nntpmessage) m_subject=mime.GetFieldValue("Subject"); // remove any path folding m_subject=StringFunctions::Replace(m_subject,"\r\n",""); + m_subject=StringFunctions::Replace(m_subject,"\t",""); +#if DO_CHARSET_CONVERSION + if(mime.GetFieldCharset("Subject")) + { + std::string charset=mime.GetFieldCharset("Subject"); + CharsetConverter ccv; + if(charset!="" && charset!="UTF-8" && ccv.SetConversion(charset,"UTF-8")) + { + std::string output=""; + ccv.Convert(m_subject,output); + m_subject=output; + } + } +#endif } else { @@ -622,6 +678,7 @@ const bool Message::ParseNNTPMessage(const std::string &nntpmessage) std::string temp=mime.GetFieldValue("References"); // remove any path folding temp=StringFunctions::Replace(temp,"\r\n",""); + temp=StringFunctions::Replace(temp,"\t",""); std::vector parts; int count=0; StringFunctions::SplitMultiple(temp,", \t",parts); @@ -658,7 +715,44 @@ const bool Message::ParseNNTPMessage(const std::string &nntpmessage) { if((*i)->IsText() && (*i)->GetContent()) { - m_body+=(char *)(*i)->GetContent(); + std::string bodypart=(char *)(*i)->GetContent(); +#ifdef DO_CHARSET_CONVERSION + std::string charset=(*i)->GetCharset(); + if(charset!="" && charset!="UTF-8") + { + CharsetConverter ccv; + if(ccv.SetConversion(charset,"UTF-8")) + { + std::string output=""; + ccv.Convert(bodypart,output); + bodypart=output; + } + } +#endif + m_body+=bodypart; + } + // add a binary file attachment + else if(((*i)->GetName()!="" || (*i)->GetFilename()!="") && (*i)->GetLength()>0 && (*i)->GetContent()) + { + std::string filename=""; + std::string contenttype=""; + std::vector data((*i)->GetContent(),(*i)->GetContent()+(*i)->GetContentLength()); + if((*i)->GetContentType()) + { + contenttype=(*i)->GetContentType(); + // find first ; tab cr or lf and erase it and everything after it + std::string::size_type endpos=contenttype.find_first_of(";\t\r\n "); + if(endpos!=std::string::npos) + { + contenttype.erase(endpos); + } + } + filename=(*i)->GetFilename(); + if(filename=="") + { + filename=(*i)->GetName(); + } + m_fileattachments.push_back(fileattachment(filename,contenttype,data)); } } @@ -671,66 +765,87 @@ const bool Message::StartFreenetInsert() MessageXML xml; int localidentityid=-1; - xml.SetMessageID(m_messageuuid); - xml.SetSubject(m_subject); - xml.SetBody(m_body); - xml.SetReplyBoard(m_replyboardname); - xml.SetDate(m_datetime.Format("%Y-%m-%d")); - xml.SetTime(m_datetime.Format("%H:%M:%S")); - StripAdministrationBoards(); - for(std::vector::iterator i=m_boards.begin(); i!=m_boards.end(); i++) - { - xml.AddBoard((*i)); - } - - for(std::map::iterator j=m_inreplyto.begin(); j!=m_inreplyto.end(); j++) + + if(m_boards.size()>0) { - xml.AddInReplyTo((*j).first,(*j).second); - } + xml.SetMessageID(m_messageuuid); + xml.SetSubject(m_subject); + xml.SetBody(m_body); + xml.SetReplyBoard(m_replyboardname); + + for(std::vector::iterator i=m_boards.begin(); i!=m_boards.end(); i++) + { + xml.AddBoard((*i)); + } + + for(std::map::iterator j=m_inreplyto.begin(); j!=m_inreplyto.end(); j++) + { + xml.AddInReplyTo((*j).first,(*j).second); + } - // find identity to insert with - /* - SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID FROM tblLocalIdentity WHERE Name=?;"); - st.Bind(0,m_fromname); - st.Step(); + localidentityid=FindLocalIdentityID(m_fromname); + if(localidentityid==-1) + { + return false; + } - // couldn't find identity with this name - insert a new identity - if(!st.RowReturned()) - { - if(m_addnewpostfromidentities==true) + // add the message delay if there is one + SQLite3DB::Statement st=m_db->Prepare("SELECT MinMessageDelay,MaxMessageDelay FROM tblLocalIdentity WHERE LocalIdentityID=?;"); + st.Bind(0,localidentityid); + st.Step(); + if(st.RowReturned()) { - DateTime now; - now.SetToGMTime(); - st=m_db->Prepare("INSERT INTO tblLocalIdentity(Name) VALUES(?);"); - st.Bind(0,m_fromname); - st.Step(true); - localidentityid=st.GetLastInsertRowID(); + int min=0; + int max=0; + st.ResultInt(0,min); + st.ResultInt(1,max); + + min<0 ? min=0 : false; + max<0 ? max=0 : false; + min>max ? min=max : false; + + if(min==max) + { + m_datetime+=Poco::Timespan(0,0,min,0,0); + } + else if(max>min) + { + int delay=(rand()%(max-min))+min; + m_datetime+=Poco::Timespan(0,0,delay,0,0); + } + } - else + st.Finalize(); + + // set date in xml file AFTER we set the delay + xml.SetDate(Poco::DateTimeFormatter::format(m_datetime,"%Y-%m-%d")); + xml.SetTime(Poco::DateTimeFormatter::format(m_datetime,"%H:%M:%S")); + + st=m_db->Prepare("INSERT INTO tblMessageInserts(LocalIdentityID,MessageUUID,MessageXML,SendDate) VALUES(?,?,?,?);"); + st.Bind(0,localidentityid); + st.Bind(1,m_messageuuid); + st.Bind(2,xml.GetXML()); + st.Bind(3,Poco::DateTimeFormatter::format(m_datetime,"%Y-%m-%d %H:%M:%S")); + st.Step(); + + // insert file attachments into database + st=m_db->Prepare("INSERT INTO tblFileInserts(MessageUUID,FileName,Size,MimeType,Data) VALUES(?,?,?,?,?);"); + for(std::vector::iterator i=m_fileattachments.begin(); i!=m_fileattachments.end(); i++) { - return false; + st.Bind(0,m_messageuuid); + st.Bind(1,(*i).m_filename); + st.Bind(2,(long)(*i).m_data.size()); + st.Bind(3,(*i).m_mimetype); + st.Bind(4,&((*i).m_data[0]),(*i).m_data.size()); + st.Step(); + st.Reset(); } - } - else - { - st.ResultInt(0,localidentityid); - } - */ - localidentityid=FindLocalIdentityID(m_fromname); - if(localidentityid==-1) - { - return false; - } - SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblMessageInserts(LocalIdentityID,MessageUUID,MessageXML) VALUES(?,?,?);"); - st.Bind(0,localidentityid); - st.Bind(1,m_messageuuid); - st.Bind(2,xml.GetXML()); - st.Step(); + HandleChangeTrust(); - HandleChangeTrust(); + } return true; @@ -745,6 +860,10 @@ void Message::StripAdministrationBoards() st.Step(); if(st.RowReturned()) { + if(m_replyboardname==(*i)) + { + m_replyboardname=""; + } i=m_boards.erase(i); } else @@ -753,4 +872,8 @@ void Message::StripAdministrationBoards() } st.Reset(); } + if(m_replyboardname=="" && m_boards.begin()!=m_boards.end()) + { + m_replyboardname=(*m_boards.begin()); + } }