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
74 upd.Bind(1,boardid);
\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
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
97 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"BoardListRequester::HandleAllData parsed BoardList XML file : "+message["Identifier"]);
\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
109 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"BoardListRequester::HandleAllData error parsing BoardList XML file : "+message["Identifier"]);
\r
112 // remove this identityid from request list
\r
113 RemoveFromRequestList(identityid);
\r
119 const bool BoardListRequester::HandleGetFailed(FCPMessage &message)
\r
122 SQLite3DB::Statement st;
\r
123 std::vector<std::string> idparts;
\r
128 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
129 StringFunctions::Convert(idparts[1],identityid);
\r
130 StringFunctions::Convert(idparts[2],index);
\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
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
142 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"BoardListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
145 // remove this identityid from request list
\r
146 RemoveFromRequestList(identityid);
\r
151 void BoardListRequester::Initialize()
\r
153 std::string tempval="";
\r
155 m_fcpuniquename="BoardListRequester";
\r
158 Option::Instance()->Get("MaxBoardListRequests",tempval);
\r
159 StringFunctions::Convert(tempval,m_maxrequests);
\r
160 if(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
165 if(m_maxrequests>100)
\r
167 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxBoardListRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
171 void BoardListRequester::PopulateIDList()
\r
175 today.SetToGMTime();
\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
182 while(st.RowReturned())
\r
184 st.ResultInt(0,id);
\r
191 void BoardListRequester::StartRequest(const long &identityid)
\r
194 FCPMessage message;
\r
195 std::string publickey;
\r
196 std::string indexstr;
\r
198 std::string identityidstr;
\r
200 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE identityid=?;");
\r
201 st.Bind(0,identityid);
\r
204 if(st.RowReturned())
\r
206 st.ResultText(0,publickey);
\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
216 if(st2.RowReturned())
\r
218 if(st2.ResultNull(0)==false)
\r
220 st2.ResultInt(0,index);
\r
226 StringFunctions::Convert(index,indexstr);
\r
227 StringFunctions::Convert(identityid,identityidstr);
\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
235 m_fcp->SendMessage(message);
\r
237 m_requesting.push_back(identityid);
\r
242 m_ids[identityid]=true;
\r