version 0.1.11
[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.Step();\r
75                                         upd.Reset();\r
76                                 }\r
77                         }\r
78                         else\r
79                         {\r
80                                 ins.Bind(0,xml.GetName(i));\r
81                                 ins.Bind(1,xml.GetDescription(i));\r
82                                 ins.Bind(2,now.Format("%Y-%m-%d %H:%M:%S"));\r
83                                 ins.Step();\r
84                                 ins.Reset();\r
85                         }\r
86                         brd.Reset();\r
87                 }\r
88 \r
89                 st=m_db->Prepare("INSERT INTO tblBoardListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");\r
90                 st.Bind(0,identityid);\r
91                 st.Bind(1,idparts[4]);\r
92                 st.Bind(2,index);\r
93                 st.Step();\r
94                 st.Finalize();\r
95 \r
96                 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"BoardListRequester::HandleAllData parsed BoardList XML file : "+message["Identifier"]);\r
97         }\r
98         else\r
99         {\r
100                 // bad data - mark index\r
101                 st=m_db->Prepare("INSERT INTO tblBoardListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
102                 st.Bind(0,identityid);\r
103                 st.Bind(1,idparts[4]);\r
104                 st.Bind(2,index);\r
105                 st.Step();\r
106                 st.Finalize();\r
107 \r
108                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"BoardListRequester::HandleAllData error parsing BoardList XML file : "+message["Identifier"]);\r
109         }\r
110 \r
111         // remove this identityid from request list\r
112         RemoveFromRequestList(identityid);\r
113 \r
114         return true;\r
115 \r
116 }\r
117 \r
118 const bool BoardListRequester::HandleGetFailed(FCPMessage &message)\r
119 {\r
120         DateTime now;\r
121         SQLite3DB::Statement st;\r
122         std::vector<std::string> idparts;\r
123         long identityid;\r
124         long index;\r
125 \r
126         now.SetToGMTime();\r
127         StringFunctions::Split(message["Identifier"],"|",idparts);\r
128         StringFunctions::Convert(idparts[1],identityid);\r
129         StringFunctions::Convert(idparts[2],index);     \r
130 \r
131         // if this is a fatal error - insert index into database so we won't try to download this index again\r
132         if(message["Fatal"]=="true")\r
133         {\r
134                 st=m_db->Prepare("INSERT INTO tblBoardListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
135                 st.Bind(0,identityid);\r
136                 st.Bind(1,idparts[4]);\r
137                 st.Bind(2,index);\r
138                 st.Step();\r
139                 st.Finalize();\r
140 \r
141                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"BoardListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);\r
142         }\r
143 \r
144         // remove this identityid from request list\r
145         RemoveFromRequestList(identityid);\r
146 \r
147         return true;\r
148 }\r
149 \r
150 void BoardListRequester::Initialize()\r
151 {\r
152         std::string tempval="";\r
153 \r
154         m_fcpuniquename="BoardListRequester";\r
155         m_maxrequests=0;\r
156 \r
157         Option::Instance()->Get("MaxBoardListRequests",tempval);\r
158         StringFunctions::Convert(tempval,m_maxrequests);\r
159         if(m_maxrequests<0)\r
160         {\r
161                 m_maxrequests=0;\r
162                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxBoardListRequests is currently set at "+tempval+".  It must be 0 or greater.");\r
163         }\r
164         if(m_maxrequests>100)\r
165         {\r
166                 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxBoardListRequests is currently set at "+tempval+".  This value might be incorrectly configured.");\r
167         }\r
168 }\r
169 \r
170 void BoardListRequester::PopulateIDList()\r
171 {\r
172         int id;\r
173         DateTime today;\r
174         today.SetToGMTime();\r
175 \r
176         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>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalMessageTrust') AND PublishBoardList='true' ORDER BY LocalMessageTrust+LocalTrustListTrust DESC, LastSeen;");\r
177         st.Step();\r
178 \r
179         m_ids.clear();\r
180 \r
181         while(st.RowReturned())\r
182         {\r
183                 st.ResultInt(0,id);\r
184                 m_ids[id]=false;\r
185                 st.Step();\r
186         }\r
187 \r
188 }\r
189 \r
190 void BoardListRequester::StartRequest(const long &identityid)\r
191 {\r
192         DateTime now;\r
193         FCPMessage message;\r
194         std::string publickey;\r
195         std::string indexstr;\r
196         int index;\r
197         std::string identityidstr;\r
198 \r
199         SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE identityid=?;");\r
200         st.Bind(0,identityid);\r
201         st.Step();\r
202 \r
203         if(st.RowReturned())\r
204         {\r
205                 st.ResultText(0,publickey);\r
206 \r
207                 now.SetToGMTime();\r
208 \r
209                 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblBoardListRequests WHERE Day=? AND IdentityID=?;");\r
210                 st2.Bind(0,now.Format("%Y-%m-%d"));\r
211                 st2.Bind(1,identityid);\r
212                 st2.Step();\r
213 \r
214                 index=0;\r
215                 if(st2.RowReturned())\r
216                 {\r
217                         if(st2.ResultNull(0)==false)\r
218                         {\r
219                                 st2.ResultInt(0,index);\r
220                                 index++;\r
221                         }\r
222                 }\r
223                 st2.Finalize();\r
224 \r
225                 StringFunctions::Convert(index,indexstr);\r
226                 StringFunctions::Convert(identityid,identityidstr);\r
227 \r
228                 message.SetName("ClientGet");\r
229                 message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|BoardList|"+indexstr+".xml";\r
230                 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];\r
231                 message["ReturnType"]="direct";\r
232                 message["MaxSize"]="100000";                    // 100 KB\r
233 \r
234                 m_fcp->SendMessage(message);\r
235 \r
236                 m_requesting.push_back(identityid);\r
237 \r
238         }\r
239         st.Finalize();\r
240 \r
241         m_ids[identityid]=true;\r
242 \r
243 }\r