X-Git-Url: https://git.pterodactylus.net/?p=fms.git;a=blobdiff_plain;f=src%2Fmessage.cpp;h=5905021de147e290c95f86ec489832b6f7d6adb4;hp=123d370e5752c2ee443c3470f7b3a1e69ac7f963;hb=63376b2a82c3f6cdf2df56b1f134bd7df0aaab3a;hpb=f60495a029c54358f82956482fe203fe2b7b5b23 diff --git a/src/message.cpp b/src/message.cpp index 123d370..5905021 100644 --- a/src/message.cpp +++ b/src/message.cpp @@ -3,9 +3,14 @@ #include "../include/uuidgenerator.h" #include "../include/stringfunctions.h" #include "../include/freenet/messagexml.h" +#include "../include/option.h" #include +#ifdef DO_CHARSET_CONVERSION + #include "../include/charsetconverter.h" +#endif + #ifdef XMEM #include #endif @@ -41,9 +46,46 @@ 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 { - return "<"+m_messageuuid+"@freenetproject.org>"; + // old message - before 0.1.12 - doesn't have @domain so add @freenetproject.org + if(m_messageuuid.find("@")==std::string::npos) + { + return "<"+m_messageuuid+"@freenetproject.org>"; + } + else + { + return "<"+m_messageuuid+">"; + } } const std::string Message::GetNNTPBody() const @@ -78,7 +120,15 @@ const std::string Message::GetNNTPHeaders() const { rval+=" "; } - rval+="<"+(*j).second+"@freenetproject.org>"; + // old message - before 0.1.12 - doesn't have @domain so add @freenetproject.org + if((*j).second.find("@")==std::string::npos) + { + rval+="<"+(*j).second+"@freenetproject.org>"; + } + else + { + rval+="<"+(*j).second+">"; + } } rval+="\r\n"; } @@ -95,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=""; @@ -106,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); @@ -115,42 +168,54 @@ 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()) { origmess.ResultInt(0,identityid); origmess.ResultText(1,identityname); - origmess.ResultInt(2,origmessagetrust); - origmess.ResultInt(3,origtrustlisttrust); - - origmessagetrust+=changemessagetrust; - origtrustlisttrust+=changetrustlisttrust; - - if(origmessagetrust<0) + if(origmess.ResultNull(2)==false) { - origmessagetrust=0; + origmess.ResultInt(2,origmessagetrust); } - if(origmessagetrust>100) + else { - origmessagetrust=100; + //origmessagetrust=m_minlocalmessagetrust; + origmessagetrust=50; } - if(origtrustlisttrust<0) + if(origmess.ResultNull(3)==false) { - origtrustlisttrust=0; + origmess.ResultInt(3,origtrustlisttrust); } - if(origtrustlisttrust>100) + else { - origtrustlisttrust=100; + //origtrustlisttrust=m_minlocaltrustlisttrust; + origtrustlisttrust=50; } + origmessagetrust+=changemessagetrust; + origtrustlisttrust+=changetrustlisttrust; + + origmessagetrust<0 ? origmessagetrust=0 : false; + origmessagetrust>100 ? origmessagetrust=100 : false; + 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 @@ -163,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"; @@ -192,8 +258,64 @@ void Message::HandleAdministrationMessage() } +void Message::HandleChangeTrust() +{ + if(m_changemessagetrustonreply!=0 && m_inreplyto.size()>0) + { + int localidentityid=FindLocalIdentityID(m_fromname); + if(localidentityid!=-1) + { + // make sure we have a record in tblIdentityTrust + SQLite3DB::Statement ins=m_db->Prepare("INSERT INTO tblIdentityTrust(LocalIdentityID,IdentityID) VALUES(?,?);"); + + 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()) + { + int identityid=0; + int localmessagetrust=0; + + st.ResultInt(0,identityid); + if(st.ResultNull(1)==false) + { + st.ResultInt(1,localmessagetrust); + } + else + { + //localmessagetrust=m_minlocalmessagetrust; + localmessagetrust=50; + } + + 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(); + + } + } + } +} + void Message::Initialize() { + std::string tempval=""; m_messageid=-1; m_messageuuid=""; m_subject=""; @@ -203,6 +325,25 @@ 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); + Option::Instance()->Get("AddNewPostFromIdentities",tempval); + if(tempval=="true") + { + m_addnewpostfromidentities=true; + } + else + { + m_addnewpostfromidentities=false; + } + tempval="50"; + Option::Instance()->Get("MinLocalMessageTrust",tempval); + StringFunctions::Convert(tempval,m_minlocalmessagetrust); + tempval="51"; + Option::Instance()->Get("MinLocalTrustListTrust",tempval); + StringFunctions::Convert(tempval,m_minlocaltrustlisttrust); } const bool Message::Load(const long messageid, const long boardid) @@ -286,8 +427,24 @@ const bool Message::Load(const long messageid, const long boardid) const bool Message::Load(const std::string &messageuuid) { + + std::string uuid=messageuuid; + + if(uuid.size()>0 && uuid[0]=='<') + { + uuid.erase(0,1); + } + if(uuid.size()>0 && uuid[uuid.size()-1]=='>') + { + uuid.erase(uuid.size()-1); + } + if(uuid.find("@freenetproject.org")!=std::string::npos) + { + uuid.erase(uuid.find("@freenetproject.org")); + } + SQLite3DB::Statement st=m_db->Prepare("SELECT MessageID FROM tblMessage WHERE MessageUUID=?;"); - st.Bind(0,messageuuid); + st.Bind(0,uuid); st.Step(); if(st.RowReturned()) @@ -375,8 +532,10 @@ const bool Message::ParseNNTPMessage(const std::string &nntpmessage) // get header info // date is always set to now regardless of what message has m_datetime.SetToGMTime(); + // messageuuid is always a unique id we generate regardless of message message-id m_messageuuid=uuid.Generate(); + // get from if(mime.GetFieldValue("From")) { @@ -460,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 { @@ -480,11 +652,18 @@ const bool Message::ParseNNTPMessage(const std::string &nntpmessage) (*i)=StringFunctions::Replace((*i),"<",""); (*i)=StringFunctions::Replace((*i),">",""); (*i)=StringFunctions::TrimWhitespace((*i)); + /* // erase @ and everything after if((*i).find("@")!=std::string::npos) { (*i).erase((*i).find("@")); } + */ + // only erase after @ if message is old type with @freenetproject.org + if((*i).find("@freenetproject.org")!=std::string::npos) + { + (*i).erase((*i).find("@")); + } if((*i)!="") { m_inreplyto[count++]=(*i); @@ -500,14 +679,34 @@ 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)->GetLength()>0 && (*i)->GetContent()) + { + std::vector data((*i)->GetContent(),(*i)->GetContent()+(*i)->GetContentLength()); + m_fileattachments.push_back(fileattachment((*i)->GetName(),data)); } } return true; } -void Message::StartFreenetInsert() +const bool Message::StartFreenetInsert() { MessageXML xml; @@ -520,6 +719,7 @@ void Message::StartFreenetInsert() 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)); @@ -530,30 +730,59 @@ void Message::StartFreenetInsert() 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()) - { - DateTime now; - now.SetToGMTime(); - st=m_db->Prepare("INSERT INTO tblLocalIdentity(Name) VALUES(?);"); - st.Bind(0,m_fromname); - st.Step(true); - localidentityid=st.GetLastInsertRowID(); - } - else + localidentityid=FindLocalIdentityID(m_fromname); + if(localidentityid==-1) { - st.ResultInt(0,localidentityid); + return false; } - st=m_db->Prepare("INSERT INTO tblMessageInserts(LocalIdentityID,MessageUUID,MessageXML) VALUES(?,?,?);"); + 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(); + // insert file attachments into database + st=m_db->Prepare("INSERT INTO tblFileInserts(MessageUUID,FileName,Size,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_data[0]),(*i).m_data.size()); + st.Step(); + st.Reset(); + } + + HandleChangeTrust(); + + return true; + +} + +void Message::StripAdministrationBoards() +{ + SQLite3DB::Statement st=m_db->Prepare("SELECT tblBoard.BoardID FROM tblBoard INNER JOIN tblAdministrationBoard ON tblBoard.BoardID=tblAdministrationBoard.BoardID WHERE BoardName=?;"); + for(std::vector::iterator i=m_boards.begin(); i!=m_boards.end(); ) + { + st.Bind(0,(*i)); + st.Step(); + if(st.RowReturned()) + { + if(m_replyboardname==(*i)) + { + m_replyboardname=""; + } + i=m_boards.erase(i); + } + else + { + i++; + } + st.Reset(); + } + if(m_replyboardname=="" && m_boards.begin()!=m_boards.end()) + { + m_replyboardname=(*m_boards.begin()); + } }