X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmessage.cpp;h=f8b64e58ac4a964eaa150480b84cdcb06cc9b77a;hb=6836fbb5db8464f56e682989996b2210b14231d0;hp=f95f541ec5ccce2c9490f9720a1b258e35cbced0;hpb=8a0a83a78390f22f99d4487cda2569909dfbc28e;p=fms.git diff --git a/src/message.cpp b/src/message.cpp index f95f541..f8b64e5 100644 --- a/src/message.cpp +++ b/src/message.cpp @@ -7,6 +7,10 @@ #include +#ifdef DO_CHARSET_CONVERSION + #include "../include/charsetconverter.h" +#endif + #ifdef XMEM #include #endif @@ -42,6 +46,35 @@ const bool Message::CheckForAdministrationBoard(const std::vector & return false; } +const int Message::FindLocalIdentityID(const std::string &name) +{ + SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID FROM tblLocalIdentity WHERE Name=?;"); + st.Bind(0,name); + st.Step(); + if(st.RowReturned()) + { + int result=-1; + st.ResultInt(0,result); + return result; + } + else + { + if(m_addnewpostfromidentities==true) + { + DateTime now; + now.SetToGMTime(); + st=m_db->Prepare("INSERT INTO tblLocalIdentity(Name) VALUES(?);"); + st.Bind(0,name); + st.Step(true); + return st.GetLastInsertRowID(); + } + else + { + return -1; + } + } +} + const std::string Message::GetNNTPArticleID() const { // old message - before 0.1.12 - doesn't have @domain so add @freenetproject.org @@ -112,6 +145,7 @@ void Message::HandleAdministrationMessage() // only continue if this message was actually a reply to another message if(m_inreplyto.size()>0) { + int localidentityid=-1; int boardid=0; std::string boardname=""; std::string identityname=""; @@ -123,7 +157,9 @@ void Message::HandleAdministrationMessage() SQLite3DB::Statement st=m_db->Prepare("SELECT tblBoard.BoardID,BoardName,ModifyLocalMessageTrust,ModifyLocalTrustListTrust FROM tblBoard INNER JOIN tblAdministrationBoard ON tblBoard.BoardID=tblAdministrationBoard.BoardID;"); st.Step(); - while(st.RowReturned()) + localidentityid=FindLocalIdentityID(m_fromname); + + while(st.RowReturned() && localidentityid!=-1) { st.ResultInt(0,boardid); st.ResultText(1,boardname); @@ -132,8 +168,9 @@ void Message::HandleAdministrationMessage() if(std::find(m_boards.begin(),m_boards.end(),boardname)!=m_boards.end()) { - SQLite3DB::Statement origmess=m_db->Prepare("SELECT tblIdentity.IdentityID,tblIdentity.Name,tblIdentity.LocalMessageTrust,tblIdentity.LocalTrustListTrust FROM tblIdentity INNER JOIN tblMessage ON tblIdentity.IdentityID=tblMessage.IdentityID WHERE tblMessage.MessageUUID=?;"); - origmess.Bind(0,m_inreplyto[0]); + SQLite3DB::Statement origmess=m_db->Prepare("SELECT tblIdentity.IdentityID,tblIdentity.Name,tblIdentityTrust.LocalMessageTrust,tblIdentityTrust.LocalTrustListTrust FROM tblIdentity INNER JOIN tblMessage ON tblIdentity.IdentityID=tblMessage.IdentityID LEFT JOIN (SELECT IdentityID,LocalMessageTrust,LocalTrustListTrust FROM tblIdentityTrust WHERE LocalIdentityID=?) AS 'tblIdentityTrust' ON tblIdentity.IdentityID=tblIdentityTrust.IdentityID WHERE tblMessage.MessageUUID=?;"); + origmess.Bind(0,localidentityid); + origmess.Bind(1,m_inreplyto[0]); origmess.Step(); if(origmess.RowReturned()) @@ -146,7 +183,8 @@ void Message::HandleAdministrationMessage() } else { - origmessagetrust=m_minlocalmessagetrust; + //origmessagetrust=m_minlocalmessagetrust; + origmessagetrust=50; } if(origmess.ResultNull(3)==false) { @@ -154,7 +192,8 @@ void Message::HandleAdministrationMessage() } else { - origtrustlisttrust=m_minlocaltrustlisttrust; + //origtrustlisttrust=m_minlocaltrustlisttrust; + origtrustlisttrust=50; } origmessagetrust+=changemessagetrust; @@ -165,11 +204,18 @@ void Message::HandleAdministrationMessage() origtrustlisttrust<0 ? origtrustlisttrust=0 : false; origtrustlisttrust>100 ? origtrustlisttrust=100 : false; + // make sure we have a record in tblIdentityTrust + SQLite3DB::Statement ins=m_db->Prepare("INSERT INTO tblIdentityTrust(LocalIdentityID,IdentityID) VALUES(?,?);"); + ins.Bind(0,localidentityid); + ins.Bind(1,identityid); + ins.Step(); + // update new trust levels - SQLite3DB::Statement update=m_db->Prepare("UPDATE tblIdentity SET LocalMessageTrust=?, LocalTrustListTrust=? WHERE IdentityID=?;"); + SQLite3DB::Statement update=m_db->Prepare("UPDATE tblIdentityTrust SET LocalMessageTrust=?, LocalTrustListTrust=? WHERE IdentityID=? AND LocalIdentityID=?;"); update.Bind(0,origmessagetrust); update.Bind(1,origtrustlisttrust); update.Bind(2,identityid); + update.Bind(3,localidentityid); update.Step(); // insert message to show what id was changed and what current levels are @@ -182,6 +228,7 @@ void Message::HandleAdministrationMessage() now.SetToGMTime(); StringFunctions::Convert(origmessagetrust,messagetruststr); StringFunctions::Convert(origtrustlisttrust,trustlisttruststr); + messagebody="Trust List of "+m_fromname+"\r\n"; messagebody="Trust Changed for "+identityname+"\r\n"; messagebody+="Local Message Trust : "+messagetruststr+"\r\n"; messagebody+="Local Trust List Trust : "+trustlisttruststr+"\r\n"; @@ -215,39 +262,53 @@ void Message::HandleChangeTrust() { if(m_changemessagetrustonreply!=0 && m_inreplyto.size()>0) { - SQLite3DB::Statement st=m_db->Prepare("SELECT tblIdentity.IdentityID,tblIdentity.LocalMessageTrust FROM tblIdentity INNER JOIN tblMessage ON tblIdentity.IdentityID=tblMessage.IdentityID WHERE tblMessage.MessageUUID=?;"); - st.Bind(0,m_inreplyto[0]); - st.Step(); - if(st.RowReturned()) + int localidentityid=FindLocalIdentityID(m_fromname); + if(localidentityid!=-1) { - int identityid=0; - int localmessagetrust=0; + // make sure we have a record in tblIdentityTrust + SQLite3DB::Statement ins=m_db->Prepare("INSERT INTO tblIdentityTrust(LocalIdentityID,IdentityID) VALUES(?,?);"); - st.ResultInt(0,identityid); - if(st.ResultNull(1)==false) - { - st.ResultInt(1,localmessagetrust); - } - else + SQLite3DB::Statement st=m_db->Prepare("SELECT tblIdentity.IdentityID,tblIdentityTrust.LocalMessageTrust FROM tblIdentity INNER JOIN tblMessage ON tblIdentity.IdentityID=tblMessage.IdentityID LEFT JOIN (SELECT IdentityID,LocalMessageTrust FROM tblIdentityTrust WHERE LocalIdentityID=?) AS 'tblIdentityTrust' ON tblIdentity.IdentityID=tblIdentityTrust.IdentityID WHERE tblMessage.MessageUUID=?;"); + st.Bind(0,localidentityid); + st.Bind(1,m_inreplyto[0]); + st.Step(); + if(st.RowReturned()) { - localmessagetrust=m_minlocalmessagetrust; - } + int identityid=0; + int localmessagetrust=0; - localmessagetrust+=m_changemessagetrustonreply; - if(localmessagetrust<0) - { - localmessagetrust=0; - } - if(localmessagetrust>100) - { - localmessagetrust=100; - } + st.ResultInt(0,identityid); + if(st.ResultNull(1)==false) + { + st.ResultInt(1,localmessagetrust); + } + else + { + //localmessagetrust=m_minlocalmessagetrust; + localmessagetrust=50; + } - SQLite3DB::Statement st2=m_db->Prepare("UPDATE tblIdentity SET LocalMessageTrust=? WHERE IdentityID=?;"); - st2.Bind(0,localmessagetrust); - st2.Bind(1,identityid); - st2.Step(); + localmessagetrust+=m_changemessagetrustonreply; + if(localmessagetrust<0) + { + localmessagetrust=0; + } + if(localmessagetrust>100) + { + localmessagetrust=100; + } + + ins.Bind(0,localidentityid); + ins.Bind(1,identityid); + ins.Step(); + SQLite3DB::Statement st2=m_db->Prepare("UPDATE tblIdentityTrust SET LocalMessageTrust=? WHERE IdentityID=? AND LocalIdentityID=?;"); + st2.Bind(0,localmessagetrust); + st2.Bind(1,identityid); + st2.Bind(2,localidentityid); + st2.Step(); + + } } } } @@ -264,6 +325,7 @@ void Message::Initialize() m_fromname=""; m_boards.clear(); m_inreplyto.clear(); + m_fileattachments.clear(); m_changemessagetrustonreply=0; Option::Instance()->Get("ChangeMessageTrustOnReply",tempval); StringFunctions::Convert(tempval,m_changemessagetrustonreply); @@ -557,6 +619,19 @@ 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",""); +#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 { @@ -604,7 +679,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)); } } @@ -617,58 +729,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++) - { - 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(); - // couldn't find identity with this name - insert a new identity - if(!st.RowReturned()) + if(m_boards.size()>0) { - if(m_addnewpostfromidentities==true) + + 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++) { - DateTime now; - now.SetToGMTime(); - st=m_db->Prepare("INSERT INTO tblLocalIdentity(Name) VALUES(?);"); - st.Bind(0,m_fromname); - st.Step(true); - localidentityid=st.GetLastInsertRowID(); + xml.AddBoard((*i)); } - else + + for(std::map::iterator j=m_inreplyto.begin(); j!=m_inreplyto.end(); j++) + { + xml.AddInReplyTo((*j).first,(*j).second); + } + + localidentityid=FindLocalIdentityID(m_fromname); + if(localidentityid==-1) { return false; } - } - else - { - st.ResultInt(0,localidentityid); - } - 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(); + // 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()) + { + 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; - HandleChangeTrust(); + if(min==max) + { + m_datetime.Add(0,min); + } + else if(max>min) + { + int delay=(rand()%(max-min))+min; + m_datetime.Add(0,delay); + } + + } + st.Finalize(); + + // set date in xml file AFTER we set the delay + xml.SetDate(m_datetime.Format("%Y-%m-%d")); + xml.SetTime(m_datetime.Format("%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,m_datetime.Format("%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++) + { + 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(); + } + + HandleChangeTrust(); + + } return true; @@ -683,6 +824,10 @@ void Message::StripAdministrationBoards() st.Step(); if(st.RowReturned()) { + if(m_replyboardname==(*i)) + { + m_replyboardname=""; + } i=m_boards.erase(i); } else @@ -691,4 +836,8 @@ void Message::StripAdministrationBoards() } st.Reset(); } + if(m_replyboardname=="" && m_boards.begin()!=m_boards.end()) + { + m_replyboardname=(*m_boards.begin()); + } }