version 0.2.16
[fms.git] / src / nntp / extensiontrust.cpp
1 #include "../../include/nntp/extensiontrust.h"\r
2 #include "../../include/stringfunctions.h"\r
3 \r
4 #ifdef XMEM\r
5         #include <xmem.h>\r
6 #endif\r
7 \r
8 TrustExtension::TrustExtension()\r
9 {\r
10         m_localidentityid=-1;\r
11 }\r
12 \r
13 TrustExtension::TrustExtension(const int &localidentityid)\r
14 {\r
15         m_localidentityid=localidentityid;\r
16 }\r
17 \r
18 const int TrustExtension::GetIdentityID(const std::string &nntpname)\r
19 {\r
20         std::vector<std::string> nameparts;\r
21         StringFunctions::Split(nntpname,"@",nameparts);\r
22 \r
23         SQLite3DB::Statement st=m_db->Prepare("SELECT IdentityID, PublicKey FROM tblIdentity WHERE Name=? AND PublicKey IS NOT NULL AND PublicKey  <> '' ;");\r
24         st.Bind(0,nameparts[0]);\r
25         st.Step();\r
26 \r
27         while(st.RowReturned())\r
28         {\r
29                 int id=-1;\r
30                 std::vector<std::string> keyparts;\r
31                 std::string publickey="";\r
32 \r
33                 st.ResultText(1,publickey);\r
34                 StringFunctions::SplitMultiple(publickey,"@,",keyparts);\r
35 \r
36                 if(keyparts.size()>1)\r
37                 {\r
38                         publickey=StringFunctions::Replace(StringFunctions::Replace(keyparts[1],"~",""),"-","");\r
39                         if(nameparts[0]+"@"+publickey==nntpname)\r
40                         {\r
41                                 st.ResultInt(0,id);\r
42                                 return id;\r
43                         }\r
44                 }\r
45 \r
46                 st.Step();\r
47         }\r
48 \r
49         return -1;\r
50 \r
51 }\r
52 \r
53 const bool TrustExtension::GetMessageTrust(const std::string &nntpname, int &trust)\r
54 {\r
55         if(m_localidentityid>=0)\r
56         {\r
57                 int id=GetIdentityID(nntpname);\r
58                 if(id>=0)\r
59                 {\r
60                         SQLite3DB::Statement st=m_db->Prepare("SELECT LocalMessageTrust FROM tblIdentityTrust WHERE LocalIdentityID=? AND IdentityID=?;");\r
61                         st.Bind(0,m_localidentityid);\r
62                         st.Bind(1,id);\r
63                         st.Step();\r
64 \r
65                         if(st.RowReturned())\r
66                         {\r
67                                 int tr=-1;\r
68                                 if(st.ResultNull(0)==false)\r
69                                 {\r
70                                         st.ResultInt(0,tr);\r
71                                 }\r
72                                 trust=tr;\r
73                         }\r
74                         else\r
75                         {\r
76                                 trust=-1;\r
77                         }\r
78                         return true;\r
79                 }\r
80                 else\r
81                 {\r
82                         return false;\r
83                 }\r
84         }\r
85         else\r
86         {\r
87                 return false;\r
88         }\r
89 }\r
90 \r
91 const bool TrustExtension::GetTrustList(std::map<std::string,std::pair<int,int> > &trustlist)\r
92 {\r
93         if(m_localidentityid>=0)\r
94         {\r
95                 SQLite3DB::Statement st=m_db->Prepare("SELECT tblIdentityTrust.LocalMessageTrust,tblIdentityTrust.LocalTrustListTrust,tblIdentity.Name,tblIdentity.PublicKey FROM tblIdentityTrust INNER JOIN tblIdentity ON tblIdentityTrust.IdentityID=tblIdentity.IdentityID WHERE tblIdentityTrust.LocalIdentityID=? AND tblIdentity.Name IS NOT NULL AND tblIdentity.PublicKey IS NOT NULL AND tblIdentity.PublicKey <> '' ;");\r
96                 st.Bind(0,m_localidentityid);\r
97                 st.Step();\r
98                 while(st.RowReturned())\r
99                 {\r
100                         int messagetrust=-1;\r
101                         int trustlisttrust=-1;\r
102                         std::string name="";\r
103                         std::string publickey="";\r
104                         std::vector<std::string> keyparts;\r
105                         std::string nntpname="";\r
106 \r
107                         if(st.ResultNull(0)==false)\r
108                         {\r
109                                 st.ResultInt(0,messagetrust);\r
110                         }\r
111                         if(st.ResultNull(1)==false)\r
112                         {\r
113                                 st.ResultInt(1,trustlisttrust);\r
114                         }\r
115                         st.ResultText(2,name);\r
116                         st.ResultText(3,publickey);\r
117 \r
118                         StringFunctions::SplitMultiple(publickey,"@,",keyparts);\r
119                         if(keyparts.size()>1)\r
120                         {\r
121                                 publickey=StringFunctions::Replace(StringFunctions::Replace(keyparts[1],"~",""),"-","");\r
122                                 nntpname=name+"@"+publickey;\r
123                         }\r
124 \r
125                         trustlist[nntpname]=std::pair<int,int>(messagetrust,trustlisttrust);\r
126 \r
127                         st.Step();\r
128                 }\r
129                 return true;\r
130         }\r
131         else\r
132         {\r
133                 return false;\r
134         }\r
135 }\r
136 \r
137 const bool TrustExtension::GetTrustListTrust(const std::string &nntpname, int &trust)\r
138 {\r
139         if(m_localidentityid>=0)\r
140         {\r
141                 int id=GetIdentityID(nntpname);\r
142                 if(id>=0)\r
143                 {\r
144                         SQLite3DB::Statement st=m_db->Prepare("SELECT LocalTrustListTrust FROM tblIdentityTrust WHERE LocalIdentityID=? AND IdentityID=?;");\r
145                         st.Bind(0,m_localidentityid);\r
146                         st.Bind(1,id);\r
147                         st.Step();\r
148 \r
149                         if(st.RowReturned())\r
150                         {\r
151                                 int tr=-1;\r
152                                 if(st.ResultNull(0)==false)\r
153                                 {\r
154                                         st.ResultInt(0,tr);\r
155                                 }\r
156                                 trust=tr;\r
157                         }\r
158                         else\r
159                         {\r
160                                 trust=-1;\r
161                         }\r
162                         return true;\r
163                 }\r
164                 else\r
165                 {\r
166                         return false;\r
167                 }\r
168         }\r
169         else\r
170         {\r
171                 return false;\r
172         }\r
173 }\r
174 \r
175 const bool TrustExtension::SetMessageTrust(const std::string &nntpname, const int trust)\r
176 {\r
177         if(m_localidentityid>=0 && trust>=-1 && trust<=100)\r
178         {\r
179                 int id=GetIdentityID(nntpname);\r
180                 if(id>=0)\r
181                 {\r
182                         SQLite3DB::Statement st=m_db->Prepare("UPDATE tblIdentityTrust SET LocalMessageTrust=? WHERE LocalIdentityID=? AND IdentityID=?;");\r
183                         if(trust==-1)\r
184                         {\r
185                                 st.Bind(0);\r
186                         }\r
187                         else\r
188                         {\r
189                                 st.Bind(0,trust);\r
190                         }\r
191                         st.Bind(1,m_localidentityid);\r
192                         st.Bind(2,id);\r
193                         st.Step();\r
194 \r
195                         return true;\r
196                 }\r
197                 else\r
198                 {\r
199                         return false;\r
200                 }\r
201         }\r
202         else\r
203         {\r
204                 return false;\r
205         }\r
206 }\r
207 \r
208 const bool TrustExtension::SetTrustListTrust(const std::string &nntpname, const int trust)\r
209 {\r
210         if(m_localidentityid>=0 && trust>=-1 && trust<=100)\r
211         {\r
212                 int id=GetIdentityID(nntpname);\r
213                 if(id>=0)\r
214                 {\r
215                         SQLite3DB::Statement st=m_db->Prepare("UPDATE tblIdentityTrust SET LocalTrustListTrust=? WHERE LocalIdentityID=? AND IdentityID=?;");\r
216                         if(trust==-1)\r
217                         {\r
218                                 st.Bind(0);\r
219                         }\r
220                         else\r
221                         {\r
222                                 st.Bind(0,trust);\r
223                         }\r
224                         st.Bind(1,m_localidentityid);\r
225                         st.Bind(2,id);\r
226                         st.Step();\r
227 \r
228                         return true;\r
229                 }\r
230                 else\r
231                 {\r
232                         return false;\r
233                 }\r
234         }\r
235         else\r
236         {\r
237                 return false;\r
238         }\r
239 }\r