1 #include "../../include/freenet/boardlistrequester.h"
\r
2 #include "../../include/freenet/boardlistxml.h"
\r
8 BoardListRequester::BoardListRequester()
\r
13 BoardListRequester::BoardListRequester(FCPv2 *fcp):IIndexRequester<long>(fcp)
\r
18 const bool BoardListRequester::HandleAllData(FCPMessage &message)
\r
21 SQLite3DB::Statement st;
\r
22 std::vector<std::string> idparts;
\r
24 std::vector<char> data;
\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
35 // wait for all data to be received from connection
\r
36 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
41 // if we got disconnected- return immediately
\r
42 if(m_fcp->Connected()==false)
\r
48 data.resize(datalength);
\r
49 m_fcp->ReceiveRaw(&data[0],datalength);
\r
51 // parse file into xml and update the database
\r
52 if(xml.ParseXML(std::string(data.begin(),data.end()))==true)
\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
61 std::string name="";
\r
62 std::string description="";
\r
64 brd.Bind(0,xml.GetName(i));
\r
67 if(brd.RowReturned())
\r
69 brd.ResultInt(0,boardid);
\r
70 brd.ResultText(2,description);
\r
71 if(description=="" && xml.GetDescription(i)!="")
\r
73 upd.Bind(0,xml.GetDescription(i));
\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
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
96 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"BoardListRequester::HandleAllData parsed BoardList XML file : "+message["Identifier"]);
\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
108 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"BoardListRequester::HandleAllData error parsing BoardList XML file : "+message["Identifier"]);
\r
111 // remove this identityid from request list
\r
112 RemoveFromRequestList(identityid);
\r
118 const bool BoardListRequester::HandleGetFailed(FCPMessage &message)
\r
121 SQLite3DB::Statement st;
\r
122 std::vector<std::string> idparts;
\r
127 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
128 StringFunctions::Convert(idparts[1],identityid);
\r
129 StringFunctions::Convert(idparts[2],index);
\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
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
141 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"BoardListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
144 // remove this identityid from request list
\r
145 RemoveFromRequestList(identityid);
\r
150 void BoardListRequester::Initialize()
\r
152 std::string tempval="";
\r
154 m_fcpuniquename="BoardListRequester";
\r
157 Option::Instance()->Get("MaxBoardListRequests",tempval);
\r
158 StringFunctions::Convert(tempval,m_maxrequests);
\r
159 if(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
164 if(m_maxrequests>100)
\r
166 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxBoardListRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
170 void BoardListRequester::PopulateIDList()
\r
174 today.SetToGMTime();
\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 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
181 while(st.RowReturned())
\r
183 st.ResultInt(0,id);
\r
190 void BoardListRequester::StartRequest(const long &identityid)
\r
193 FCPMessage message;
\r
194 std::string publickey;
\r
195 std::string indexstr;
\r
197 std::string identityidstr;
\r
199 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE identityid=?;");
\r
200 st.Bind(0,identityid);
\r
203 if(st.RowReturned())
\r
205 st.ResultText(0,publickey);
\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
215 if(st2.RowReturned())
\r
217 if(st2.ResultNull(0)==false)
\r
219 st2.ResultInt(0,index);
\r
225 StringFunctions::Convert(index,indexstr);
\r
226 StringFunctions::Convert(identityid,identityidstr);
\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
234 m_fcp->SendMessage(message);
\r
236 m_requesting.push_back(identityid);
\r
241 m_ids[identityid]=true;
\r