version 0.2.1
[fms.git] / src / freenet / boardlistrequester.cpp
1 #include "../../include/freenet/boardlistrequester.h"\r
2 #include "../../include/freenet/boardlistxml.h"\r
3 \r
4 #ifdef XMEM\r
5         #include <xmem.h>\r
6 #endif\r
7 \r
8 BoardListRequester::BoardListRequester()\r
9 {\r
10         Initialize();\r
11 }\r
12 \r
13 BoardListRequester::BoardListRequester(FCPv2 *fcp):IIndexRequester<long>(fcp)\r
14 {\r
15         Initialize();\r
16 }\r
17 \r
18 const bool BoardListRequester::HandleAllData(FCPMessage &message)\r
19 {       \r
20         DateTime now;\r
21         SQLite3DB::Statement st;\r
22         std::vector<std::string> idparts;\r
23         long datalength;\r
24         std::vector<char> data;\r
25         BoardListXML xml;\r
26         long identityid;\r
27         long index;\r
28 \r
29         now.SetToGMTime();\r
30         StringFunctions::Split(message["Identifier"],"|",idparts);\r
31         StringFunctions::Convert(message["DataLength"],datalength);\r
32         StringFunctions::Convert(idparts[1],identityid);\r
33         StringFunctions::Convert(idparts[2],index);\r
34 \r
35         // wait for all data to be received from connection\r
36         while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)\r
37         {\r
38                 m_fcp->Update(1);\r
39         }\r
40 \r
41         // if we got disconnected- return immediately\r
42         if(m_fcp->Connected()==false)\r
43         {\r
44                 return false;\r
45         }\r
46 \r
47         // receive the file\r
48         data.resize(datalength);\r
49         m_fcp->ReceiveRaw(&data[0],datalength);\r
50 \r
51         // parse file into xml and update the database\r
52         if(xml.ParseXML(std::string(data.begin(),data.end()))==true)\r
53         {\r
54 \r
55                 SQLite3DB::Statement brd=m_db->Prepare("SELECT BoardID,BoardName,BoardDescription FROM tblBoard WHERE BoardName=?;");\r
56                 SQLite3DB::Statement ins=m_db->Prepare("INSERT INTO tblBoard(BoardName,BoardDescription,DateAdded) VALUES(?,?,?);");\r
57                 SQLite3DB::Statement upd=m_db->Prepare("UPDATE tblBoard SET BoardDescription=? WHERE BoardID=?;");\r
58                 for(long i=0; i<xml.GetCount(); i++)\r
59                 {\r
60                         int boardid;\r
61                         std::string name="";\r
62                         std::string description="";\r
63 \r
64                         brd.Bind(0,xml.GetName(i));\r
65                         brd.Step();\r
66                         \r
67                         if(brd.RowReturned())\r
68                         {\r
69                                 brd.ResultInt(0,boardid);\r
70                                 brd.ResultText(2,description);\r
71                                 if(description=="" && xml.GetDescription(i)!="")\r
72                                 {\r
73                                         upd.Bind(0,xml.GetDescription(i));\r
74                                         upd.Bind(1,boardid);\r
75                                         upd.Step();\r
76                                         upd.Reset();\r
77                                 }\r
78                         }\r
79                         else\r
80                         {\r
81                                 ins.Bind(0,xml.GetName(i));\r
82                                 ins.Bind(1,xml.GetDescription(i));\r
83                                 ins.Bind(2,now.Format("%Y-%m-%d %H:%M:%S"));\r
84                                 ins.Step();\r
85                                 ins.Reset();\r
86                         }\r
87                         brd.Reset();\r
88                 }\r
89 \r
90                 st=m_db->Prepare("INSERT INTO tblBoardListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");\r
91                 st.Bind(0,identityid);\r
92                 st.Bind(1,idparts[4]);\r
93                 st.Bind(2,index);\r
94                 st.Step();\r
95                 st.Finalize();\r
96 \r
97                 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"BoardListRequester::HandleAllData parsed BoardList XML file : "+message["Identifier"]);\r
98         }\r
99         else\r
100         {\r
101                 // bad data - mark index\r
102                 st=m_db->Prepare("INSERT INTO tblBoardListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
103                 st.Bind(0,identityid);\r
104                 st.Bind(1,idparts[4]);\r
105                 st.Bind(2,index);\r
106                 st.Step();\r
107                 st.Finalize();\r
108 \r
109                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"BoardListRequester::HandleAllData error parsing BoardList XML file : "+message["Identifier"]);\r
110         }\r
111 \r
112         // remove this identityid from request list\r
113         RemoveFromRequestList(identityid);\r
114 \r
115         return true;\r
116 \r
117 }\r
118 \r
119 const bool BoardListRequester::HandleGetFailed(FCPMessage &message)\r
120 {\r
121         DateTime now;\r
122         SQLite3DB::Statement st;\r
123         std::vector<std::string> idparts;\r
124         long identityid;\r
125         long index;\r
126 \r
127         now.SetToGMTime();\r
128         StringFunctions::Split(message["Identifier"],"|",idparts);\r
129         StringFunctions::Convert(idparts[1],identityid);\r
130         StringFunctions::Convert(idparts[2],index);     \r
131 \r
132         // if this is a fatal error - insert index into database so we won't try to download this index again\r
133         if(message["Fatal"]=="true")\r
134         {\r
135                 st=m_db->Prepare("INSERT INTO tblBoardListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
136                 st.Bind(0,identityid);\r
137                 st.Bind(1,idparts[4]);\r
138                 st.Bind(2,index);\r
139                 st.Step();\r
140                 st.Finalize();\r
141 \r
142                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"BoardListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);\r
143         }\r
144 \r
145         // remove this identityid from request list\r
146         RemoveFromRequestList(identityid);\r
147 \r
148         return true;\r
149 }\r
150 \r
151 void BoardListRequester::Initialize()\r
152 {\r
153         std::string tempval="";\r
154 \r
155         m_fcpuniquename="BoardListRequester";\r
156         m_maxrequests=0;\r
157 \r
158         Option::Instance()->Get("MaxBoardListRequests",tempval);\r
159         StringFunctions::Convert(tempval,m_maxrequests);\r
160         if(m_maxrequests<0)\r
161         {\r
162                 m_maxrequests=0;\r
163                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxBoardListRequests is currently set at "+tempval+".  It must be 0 or greater.");\r
164         }\r
165         if(m_maxrequests>100)\r
166         {\r
167                 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxBoardListRequests is currently set at "+tempval+".  This value might be incorrectly configured.");\r
168         }\r
169 }\r
170 \r
171 void BoardListRequester::PopulateIDList()\r
172 {\r
173         int id;\r
174         DateTime today;\r
175         today.SetToGMTime();\r
176 \r
177         SQLite3DB::Statement st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey <> '' AND LastSeen>='"+today.Format("%Y-%m-%d")+"' AND (LocalMessageTrust IS NULL OR LocalMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalMessageTrust')) AND (PeerMessageTrust IS NULL OR PeerMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinPeerMessageTrust')) AND PublishBoardList='true' ORDER BY LocalMessageTrust+LocalTrustListTrust DESC, LastSeen;");\r
178         st.Step();\r
179 \r
180         m_ids.clear();\r
181 \r
182         while(st.RowReturned())\r
183         {\r
184                 st.ResultInt(0,id);\r
185                 m_ids[id]=false;\r
186                 st.Step();\r
187         }\r
188 \r
189 }\r
190 \r
191 void BoardListRequester::StartRequest(const long &identityid)\r
192 {\r
193         DateTime now;\r
194         FCPMessage message;\r
195         std::string publickey;\r
196         std::string indexstr;\r
197         int index;\r
198         std::string identityidstr;\r
199 \r
200         SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE identityid=?;");\r
201         st.Bind(0,identityid);\r
202         st.Step();\r
203 \r
204         if(st.RowReturned())\r
205         {\r
206                 st.ResultText(0,publickey);\r
207 \r
208                 now.SetToGMTime();\r
209 \r
210                 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblBoardListRequests WHERE Day=? AND IdentityID=?;");\r
211                 st2.Bind(0,now.Format("%Y-%m-%d"));\r
212                 st2.Bind(1,identityid);\r
213                 st2.Step();\r
214 \r
215                 index=0;\r
216                 if(st2.RowReturned())\r
217                 {\r
218                         if(st2.ResultNull(0)==false)\r
219                         {\r
220                                 st2.ResultInt(0,index);\r
221                                 index++;\r
222                         }\r
223                 }\r
224                 st2.Finalize();\r
225 \r
226                 StringFunctions::Convert(index,indexstr);\r
227                 StringFunctions::Convert(identityid,identityidstr);\r
228 \r
229                 message.SetName("ClientGet");\r
230                 message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|BoardList|"+indexstr+".xml";\r
231                 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];\r
232                 message["ReturnType"]="direct";\r
233                 message["MaxSize"]="100000";                    // 100 KB\r
234 \r
235                 m_fcp->SendMessage(message);\r
236 \r
237                 m_requesting.push_back(identityid);\r
238 \r
239         }\r
240         st.Finalize();\r
241 \r
242         m_ids[identityid]=true;\r
243 \r
244 }\r