6b0329afead7b5585d2bfc1aa98ab20c34e63b81
[fms.git] / src / freenet / trustlistxml.cpp
1 #include "../../include/freenet/trustlistxml.h"\r
2 #include "../../include/stringfunctions.h"\r
3 \r
4 #include <algorithm>\r
5 \r
6 #ifdef XMEM\r
7         #include <xmem.h>\r
8 #endif\r
9 \r
10 TrustListXML::TrustListXML()\r
11 {\r
12         Initialize();\r
13 }\r
14 \r
15 void TrustListXML::AddTrust(const std::string &identity, const long messagetrust, const long trustlisttrust, const std::string &messagetrustcomment, const std::string &trustlisttrustcomment)\r
16 {\r
17         m_trust.push_back(trust(identity,messagetrust,trustlisttrust,messagetrustcomment,trustlisttrustcomment));\r
18 }\r
19 \r
20 std::string TrustListXML::GetIdentity(const long index)\r
21 {\r
22         if(index>=0 && index<m_trust.size())\r
23         {\r
24                 return m_trust[index].m_identity;\r
25         }\r
26         else\r
27         {\r
28                 return "";\r
29         }\r
30 }\r
31 \r
32 long TrustListXML::GetMessageTrust(const long index)\r
33 {\r
34         if(index>=0 && index<m_trust.size())\r
35         {\r
36                 return m_trust[index].m_messagetrust;\r
37         }\r
38         else\r
39         {\r
40                 return -1;\r
41         }       \r
42 }\r
43 \r
44 std::string TrustListXML::GetMessageTrustComment(const long index)\r
45 {\r
46         if(index>=0 && index<m_trust.size())\r
47         {\r
48                 return m_trust[index].m_messagetrustcomment;\r
49         }\r
50         else\r
51         {\r
52                 return "";\r
53         }       \r
54 }\r
55 \r
56 long TrustListXML::GetTrustListTrust(const long index)\r
57 {\r
58         if(index>=0 && index<m_trust.size())\r
59         {\r
60                 return m_trust[index].m_trustlisttrust;\r
61         }\r
62         else\r
63         {\r
64                 return -1;\r
65         }\r
66 }\r
67 \r
68 std::string TrustListXML::GetTrustListTrustComment(const long index)\r
69 {\r
70         if(index>=0 && index<m_trust.size())\r
71         {\r
72                 return m_trust[index].m_trustlisttrustcomment;\r
73         }\r
74         else\r
75         {\r
76                 return "";\r
77         }\r
78 }\r
79 \r
80 std::string TrustListXML::GetXML()\r
81 {\r
82         TiXmlDocument td;\r
83         TiXmlDeclaration *tdec=new TiXmlDeclaration("1.0","UTF-8","");\r
84         TiXmlElement *tid;\r
85         TiXmlPrinter tp;\r
86 \r
87         td.LinkEndChild(tdec);\r
88         tid=new TiXmlElement("TrustList");\r
89         td.LinkEndChild(tid);\r
90 \r
91         for(std::vector<trust>::iterator i=m_trust.begin(); i!=m_trust.end(); i++)\r
92         {\r
93                 std::string messagetrust;\r
94                 std::string trustlisttrust;\r
95                 StringFunctions::Convert((*i).m_messagetrust,messagetrust);\r
96                 StringFunctions::Convert((*i).m_trustlisttrust,trustlisttrust);\r
97                 TiXmlElement *tr=new TiXmlElement("Trust");\r
98                 tid->LinkEndChild(tr);\r
99                 tr->LinkEndChild(XMLCreateCDATAElement("Identity",(*i).m_identity));\r
100                 if((*i).m_messagetrust>=0)\r
101                 {\r
102                         tr->LinkEndChild(XMLCreateTextElement("MessageTrustLevel",messagetrust));\r
103                 }\r
104                 if((*i).m_trustlisttrust>=0)\r
105                 {\r
106                         tr->LinkEndChild(XMLCreateTextElement("TrustListTrustLevel",trustlisttrust));\r
107                 }\r
108                 if((*i).m_messagetrustcomment!="")\r
109                 {\r
110                         tr->LinkEndChild(XMLCreateCDATAElement("MessageTrustComment",(*i).m_messagetrustcomment));\r
111                 }\r
112                 if((*i).m_trustlisttrustcomment!="")\r
113                 {\r
114                         tr->LinkEndChild(XMLCreateCDATAElement("TrustListTrustComment",(*i).m_trustlisttrustcomment));\r
115                 }\r
116         }\r
117 \r
118         td.Accept(&tp);\r
119         return std::string(tp.CStr());\r
120 }\r
121 \r
122 void TrustListXML::Initialize()\r
123 {\r
124         m_trust.clear();\r
125 }\r
126 \r
127 const bool TrustListXML::ParseXML(const std::string &xml)\r
128 {\r
129         TiXmlDocument td;\r
130         td.Parse(xml.c_str());\r
131 \r
132         if(!td.Error())\r
133         {\r
134                 std::string identity;\r
135                 std::string messagetruststr;\r
136                 std::string trustlisttruststr;\r
137                 std::string messagetrustcomment="";\r
138                 std::string trustlisttrustcomment="";\r
139                 long messagetrust;\r
140                 long trustlisttrust;\r
141                 TiXmlText *txt;\r
142                 TiXmlHandle hnd(&td);\r
143                 TiXmlNode *node;\r
144                 std::vector<std::string> m_foundkeys;\r
145 \r
146                 Initialize();\r
147 \r
148                 node=hnd.FirstChild("TrustList").FirstChild("Trust").ToElement();\r
149                 while(node)\r
150                 {\r
151                         identity="";\r
152                         messagetrust=-1;\r
153                         trustlisttrust=-1;\r
154 \r
155                         TiXmlHandle hnd2(node);\r
156                         txt=hnd2.FirstChild("Identity").FirstChild().ToText();\r
157                         if(txt)\r
158                         {\r
159                                 identity=txt->ValueStr();\r
160                         }\r
161                         txt=hnd2.FirstChild("MessageTrustLevel").FirstChild().ToText();\r
162                         if(txt)\r
163                         {\r
164                                 messagetruststr=txt->ValueStr();\r
165                                 if(messagetruststr!="")\r
166                                 {\r
167                                         StringFunctions::Convert(messagetruststr,messagetrust);\r
168                                 }\r
169                         }\r
170                         txt=hnd2.FirstChild("TrustListTrustLevel").FirstChild().ToText();\r
171                         if(txt)\r
172                         {\r
173                                 trustlisttruststr=txt->ValueStr();\r
174                                 if(trustlisttruststr!="")\r
175                                 {\r
176                                         StringFunctions::Convert(trustlisttruststr,trustlisttrust);\r
177                                 }\r
178                         }\r
179                         txt=hnd2.FirstChild("MessageTrustComment").FirstChild().ToText();\r
180                         if(txt)\r
181                         {\r
182                                 messagetrustcomment=txt->ValueStr();\r
183                         }\r
184                         txt=hnd2.FirstChild("TrustListTrustComment").FirstChild().ToText();\r
185                         if(txt)\r
186                         {\r
187                                 trustlisttrustcomment=txt->ValueStr();\r
188                         }\r
189 \r
190                         if(identity!="" && messagetrust>=-1 && messagetrust<=100 && trustlisttrust>=-1 && trustlisttrust<=100)\r
191                         {\r
192                                 // check so we don't add the same identity multiple times from a trust list\r
193                                 if(std::find(m_foundkeys.begin(),m_foundkeys.end(),identity)==m_foundkeys.end())\r
194                                 {\r
195                                         m_foundkeys.push_back(identity);\r
196                                         m_trust.push_back(trust(identity,messagetrust,trustlisttrust,messagetrustcomment,trustlisttrustcomment));\r
197                                 }\r
198                         }\r
199                         else\r
200                         {\r
201                                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"TrustListXML::ParseXML malformed Trust in TrustList.xml");\r
202                         }\r
203                         \r
204                         node=node->NextSibling("Trust");\r
205                 }\r
206                 return true;\r
207 \r
208         }\r
209         else\r
210         {\r
211                 return false;\r
212         }\r
213 }\r