version 0.3.33
[fms.git] / src / freenet / frostmessagexml.cpp
diff --git a/src/freenet/frostmessagexml.cpp b/src/freenet/frostmessagexml.cpp
new file mode 100644 (file)
index 0000000..e9cd65d
--- /dev/null
@@ -0,0 +1,359 @@
+#include "../../include/freenet/frostmessagexml.h"\r
+\r
+const std::string FrostMessageXML::FrostMakeFileName(const std::string &input) const\r
+{\r
+       std::string output;\r
+       std::string::size_type strpos;\r
+       std::string invalidchars="/\\?*<>\":|#&";\r
+\r
+       output=input;\r
+\r
+       std::transform(input.begin(),input.end(),output.begin(),tolower);\r
+\r
+       if(output.size()>0 && output[0]=='.')\r
+       {\r
+               output[0]='_';\r
+       }\r
+\r
+       strpos=output.find_first_of(invalidchars);\r
+       while(strpos!=std::string::npos)\r
+       {\r
+               output.at(strpos)='_';\r
+               strpos=output.find_first_of(invalidchars,strpos);\r
+       }\r
+\r
+       return output;\r
+}\r
+\r
+const std::string FrostMessageXML::GetSignableContentV2() const\r
+{\r
+       char separator='|';\r
+       std::string temp;\r
+       long i;\r
+\r
+       temp=GetFrostDate()+separator+GetFrostTime()+separator;\r
+\r
+       temp+=GetFrostBoard()+separator;\r
+       temp+=GetFrostAuthor()+separator;\r
+       temp+=GetFrostMessageID()+separator;\r
+       if(GetFrostInReplyTo()!="")\r
+       {\r
+               temp+=GetFrostInReplyTo()+separator;\r
+       }\r
+       // recipient goes here - private messages ??\r
+\r
+       // add id info even if it is -1\r
+       {\r
+               std::string tempval="";\r
+               StringFunctions::Convert(GetFrostIDLinePos(),tempval);\r
+               temp+=tempval+separator;\r
+               StringFunctions::Convert(GetFrostIDLineLen(),tempval);\r
+               temp+=tempval+separator;\r
+       }\r
+\r
+\r
+       temp+=GetFrostSubject()+separator+GetBody()+separator;\r
+\r
+       std::vector<FrostMessageXML::frostboardattachment> boards=GetFrostBoardAttachments();\r
+       for(i=0; i<boards.size(); i++)\r
+       {\r
+               temp+=FrostMakeFileName(boards[i].m_name)+separator;\r
+               if(boards[i].m_publickey!="")\r
+               {\r
+                       temp+=boards[i].m_publickey+separator;\r
+               }\r
+               if(boards[i].m_privatekey!="")\r
+               {\r
+                       temp+=boards[i].m_privatekey+separator;\r
+               }\r
+       }\r
+\r
+       std::vector<FrostMessageXML::frostfileattachment> files=GetFrostFileAttachments();\r
+       for(i=0; i<files.size(); i++)\r
+       {\r
+               temp+=files[i].m_name+separator;\r
+               temp+=files[i].m_key+separator;\r
+       }\r
+\r
+       return temp;\r
+}\r
+\r
+std::string FrostMessageXML::GetXML()\r
+{\r
+       return std::string("");\r
+}\r
+\r
+void FrostMessageXML::Initialize()\r
+{\r
+       MessageXML::Initialize();\r
+       m_frostidlinepos=0;\r
+       m_frostidlinelen=0;\r
+       m_frostdate="";\r
+       m_frosttime="";\r
+       m_frostauthor="";\r
+       m_frostsubject="";\r
+       m_frostmessageid="";\r
+       m_frostboard="";\r
+       m_frostpublickey="";\r
+       m_frostsignature="";\r
+       m_frostsignaturev2="";\r
+}\r
+\r
+const bool FrostMessageXML::ParseXML(const std::string &xml)\r
+{\r
+       /*\r
+               FrostMessage\r
+                       MessageId       - CDATA\r
+                       InReplyTo       - CDATA\r
+                       IdLinePos       - long\r
+                       IdLenLen        - long\r
+                       From            - CDATA\r
+                       Subject         - CDATA\r
+                       Date            - YYYY.MM.DD\r
+                       Time            - HH:MM:SSGMT\r
+                       Body            - CDATA\r
+                       Board           - CDATA\r
+                       pubKey          - CDATA\r
+                       Signature       - CDATA\r
+                       AttachmentList\r
+                               Attachment type="file"\r
+                                       File\r
+                                               name    - CDATA\r
+                                               size    - long\r
+                                               key             - key without filename\r
+       */\r
+\r
+       bool parsed=false;\r
+       Poco::XML::DOMParser dp;\r
+\r
+       Initialize();\r
+\r
+       try\r
+       {\r
+               Poco::AutoPtr<Poco::XML::Document> doc=dp.parseString(FixCDATA(xml));\r
+               Poco::XML::Element *root=XMLGetFirstChild(doc,"FrostMessage");\r
+               Poco::XML::Element *txt=0;\r
+\r
+               txt=XMLGetFirstChild(root,"Date");\r
+               if(txt)\r
+               {\r
+                       if(txt->firstChild())\r
+                       {\r
+                               m_frostdate=txt->firstChild()->getNodeValue();\r
+                               m_date=SanitizeSingleString(m_frostdate);\r
+                               m_date=StringFunctions::Replace(m_date,".","-");\r
+                               if(m_date.size()<10)\r
+                               {\r
+                                       std::vector<std::string> dateparts;\r
+                                       StringFunctions::Split(m_date,"-",dateparts);\r
+                                       if(dateparts.size()==3)\r
+                                       {\r
+                                               m_date=dateparts[0]+"-";\r
+                                               if(dateparts[1].size()==1)\r
+                                               {\r
+                                                       m_date+="0";\r
+                                               }\r
+                                               m_date+=dateparts[1]+"-";\r
+                                               if(dateparts[2].size()==1)\r
+                                               {\r
+                                                       m_date+="0";\r
+                                               }\r
+                                               m_date+=dateparts[2];\r
+                                       }\r
+                               }\r
+                       }\r
+               }\r
+               txt=XMLGetFirstChild(root,"Time");\r
+               if(txt)\r
+               {\r
+                       if(txt->firstChild())\r
+                       {\r
+                               m_frosttime=txt->firstChild()->getNodeValue();\r
+                               m_time=SanitizeSingleString(m_frosttime);\r
+                               m_time=StringFunctions::Replace(m_time,"GMT","");\r
+                       }\r
+               }\r
+               txt=XMLGetFirstChild(root,"From");\r
+               if(txt)\r
+               {\r
+                       if(txt->firstChild())\r
+                       {\r
+                               m_frostauthor=txt->firstChild()->getNodeValue();\r
+                       }\r
+               }\r
+               txt=XMLGetFirstChild(root,"Subject");\r
+               if(txt)\r
+               {\r
+                       if(txt->firstChild())\r
+                       {\r
+                               m_frostsubject=txt->firstChild()->getNodeValue();\r
+                               m_subject=SanitizeSingleString(m_frostsubject);\r
+                       }\r
+               }\r
+               txt=XMLGetFirstChild(root,"MessageId");\r
+               if(txt)\r
+               {\r
+                       if(txt->firstChild())\r
+                       {\r
+                               // we have to append an @frost (or anything really) to the message ID, otherwise someone could insert valid FMS UUIDs here\r
+                               m_frostmessageid=txt->firstChild()->getNodeValue();\r
+                               m_messageid=SanitizeSingleString(m_frostmessageid)+"@frost";\r
+                       }\r
+               }\r
+               txt=XMLGetFirstChild(root,"Board");\r
+               if(txt)\r
+               {\r
+                       if(txt->firstChild())\r
+                       {\r
+                               m_frostboard=txt->firstChild()->getNodeValue();\r
+                               std::string boardname=SanitizeSingleString(m_frostboard);\r
+                               StringFunctions::LowerCase(boardname,boardname);\r
+                               if(boardname.size()>40)\r
+                               {\r
+                                       boardname.erase(40);\r
+                               }\r
+                               m_replyboard=boardname;\r
+                               m_boards.push_back(boardname);\r
+                       }\r
+               }\r
+               txt=XMLGetFirstChild(root,"Body");\r
+               if(txt)\r
+               {\r
+                       if(txt->firstChild())\r
+                       {\r
+                               m_body=txt->firstChild()->getNodeValue();\r
+                       }\r
+               }\r
+               txt=XMLGetFirstChild(root,"pubKey");\r
+               if(txt)\r
+               {\r
+                       if(txt->firstChild())\r
+                       {\r
+                               m_frostpublickey=txt->firstChild()->getNodeValue();\r
+                       }\r
+               }\r
+               txt=XMLGetFirstChild(root,"Signature");\r
+               if(txt)\r
+               {\r
+                       if(txt->firstChild())\r
+                       {\r
+                               m_frostsignature=txt->firstChild()->getNodeValue();\r
+                       }\r
+               }\r
+               txt=XMLGetFirstChild(root,"SignatureV2");\r
+               if(txt)\r
+               {\r
+                       if(txt->firstChild())\r
+                       {\r
+                               m_frostsignaturev2=txt->firstChild()->getNodeValue();\r
+                       }\r
+               }\r
+               txt=XMLGetFirstChild(root,"IdLinePos");\r
+               if(txt)\r
+               {\r
+                       if(txt->firstChild())\r
+                       {\r
+                               std::string temp=txt->firstChild()->getNodeValue();\r
+                               StringFunctions::Convert(temp,m_frostidlinepos);\r
+                       }\r
+               }\r
+               txt=XMLGetFirstChild(root,"IdLineLen");\r
+               if(txt)\r
+               {\r
+                       if(txt->firstChild())\r
+                       {\r
+                               std::string temp=txt->firstChild()->getNodeValue();\r
+                               StringFunctions::Convert(temp,m_frostidlinelen);\r
+                       }\r
+               }\r
+               Poco::XML::Element *inreplyto=XMLGetFirstChild(root,"InReplyTo");\r
+               if(inreplyto)\r
+               {\r
+                       if(inreplyto->firstChild())\r
+                       {\r
+                               int order=0;\r
+                               m_frostinreplyto=inreplyto->firstChild()->getNodeValue();\r
+                               std::vector<std::string> inreplytoids;\r
+                               StringFunctions::Split(m_frostinreplyto,",",inreplytoids);\r
+                               for(std::vector<std::string>::reverse_iterator i=inreplytoids.rbegin(); i!=inreplytoids.rend(); i++)\r
+                               {\r
+                                       m_inreplyto[order++]=(*i)+"@frost";\r
+                               }\r
+                       }\r
+               }\r
+               Poco::XML::Element *attachments=XMLGetFirstChild(root,"AttachmentList");\r
+               if(attachments)\r
+               {\r
+                       Poco::XML::Element *file=XMLGetFirstChild(attachments,"Attachment");\r
+                       while(file)\r
+                       {\r
+                               if(file->getAttribute("type")=="file")\r
+                               {\r
+                                       Poco::XML::Element *keyel=XMLGetFirstChild(file,"key");\r
+                                       Poco::XML::Element *sizeel=XMLGetFirstChild(file,"size");\r
+                                       Poco::XML::Element *nameel=XMLGetFirstChild(file,"name");\r
+\r
+                                       if(keyel && keyel->firstChild() && sizeel && sizeel->firstChild() && nameel && nameel->firstChild())\r
+                                       {\r
+                                               int size=-1;\r
+                                               std::string key="";\r
+                                               \r
+                                               StringFunctions::Convert(sizeel->firstChild()->getNodeValue(),size);\r
+                                               key=keyel->firstChild()->getNodeValue();\r
+                                               key+="/"+nameel->firstChild()->getNodeValue();\r
+\r
+                                               if(size!=-1 && key!="")\r
+                                               {\r
+                                                       m_fileattachments.push_back(fileattachment(key,size));\r
+                                               }\r
+\r
+                                               m_frostfileattachments.push_back(frostfileattachment(keyel->firstChild()->getNodeValue(),size,nameel->firstChild()->getNodeValue()));\r
+                                       }\r
+                               }\r
+                               else if(file->getAttribute("type")=="board")\r
+                               {\r
+                                       Poco::XML::Element *nameel=XMLGetFirstChild(file,"Name");\r
+                                       Poco::XML::Element *descel=XMLGetFirstChild(file,"description");\r
+                                       Poco::XML::Element *pubel=XMLGetFirstChild(file,"pubKey");\r
+                                       Poco::XML::Element *privel=XMLGetFirstChild(file,"privKey");\r
+                                       std::string name="";\r
+                                       std::string desc="";\r
+                                       std::string pubkey="";\r
+                                       std::string privkey="";\r
+\r
+                                       if(nameel && nameel->firstChild())\r
+                                       {\r
+                                               name=nameel->firstChild()->getNodeValue();\r
+                                       }\r
+                                       if(descel && descel->firstChild())\r
+                                       {\r
+                                               desc=descel->firstChild()->getNodeValue();\r
+                                       }\r
+                                       if(pubel && pubel->firstChild())\r
+                                       {\r
+                                               pubkey=pubel->firstChild()->getNodeValue();\r
+                                       }\r
+                                       if(privel && privel->firstChild())\r
+                                       {\r
+                                               privkey=privel->firstChild()->getNodeValue();\r
+                                       }\r
+\r
+                                       m_frostboardattachments.push_back(frostboardattachment(name,desc,pubkey,privkey));\r
+\r
+                               }\r
+\r
+                               file=static_cast<Poco::XML::Element *>(file->nextSibling());\r
+\r
+                       }\r
+               }\r
+\r
+               parsed=true;\r
+\r
+       }\r
+       catch(...)\r
+       {\r
+       }\r
+\r
+       return parsed;\r
+\r
+}\r