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,SaveReceivedMessages) 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
84 if(m_savemessagesfromnewboards)
\r
90 ins.Bind(3,"false");
\r
98 st=m_db->Prepare("INSERT INTO tblBoardListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");
\r
99 st.Bind(0,identityid);
\r
100 st.Bind(1,idparts[4]);
\r
105 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"BoardListRequester::HandleAllData parsed BoardList XML file : "+message["Identifier"]);
\r
109 // bad data - mark index
\r
110 st=m_db->Prepare("INSERT INTO tblBoardListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
111 st.Bind(0,identityid);
\r
112 st.Bind(1,idparts[4]);
\r
117 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"BoardListRequester::HandleAllData error parsing BoardList XML file : "+message["Identifier"]);
\r
120 // remove this identityid from request list
\r
121 RemoveFromRequestList(identityid);
\r
127 const bool BoardListRequester::HandleGetFailed(FCPMessage &message)
\r
130 SQLite3DB::Statement st;
\r
131 std::vector<std::string> idparts;
\r
136 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
137 StringFunctions::Convert(idparts[1],identityid);
\r
138 StringFunctions::Convert(idparts[2],index);
\r
140 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
141 if(message["Fatal"]=="true")
\r
143 st=m_db->Prepare("INSERT INTO tblBoardListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
144 st.Bind(0,identityid);
\r
145 st.Bind(1,idparts[4]);
\r
150 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"BoardListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
153 // remove this identityid from request list
\r
154 RemoveFromRequestList(identityid);
\r
159 void BoardListRequester::Initialize()
\r
161 std::string tempval="";
\r
163 m_fcpuniquename="BoardListRequester";
\r
166 Option::Instance()->Get("MaxBoardListRequests",tempval);
\r
167 StringFunctions::Convert(tempval,m_maxrequests);
\r
168 if(m_maxrequests<0)
\r
171 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxBoardListRequests is currently set at "+tempval+". It must be 0 or greater.");
\r
173 if(m_maxrequests>100)
\r
175 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxBoardListRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
178 Option::Instance()->Get("SaveMessagesFromNewBoards",tempval);
\r
179 if(tempval=="true")
\r
181 m_savemessagesfromnewboards=true;
\r
185 m_savemessagesfromnewboards=false;
\r
190 void BoardListRequester::PopulateIDList()
\r
194 today.SetToGMTime();
\r
196 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
201 while(st.RowReturned())
\r
203 st.ResultInt(0,id);
\r
210 void BoardListRequester::StartRequest(const long &identityid)
\r
213 FCPMessage message;
\r
214 std::string publickey;
\r
215 std::string indexstr;
\r
217 std::string identityidstr;
\r
219 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE identityid=?;");
\r
220 st.Bind(0,identityid);
\r
223 if(st.RowReturned())
\r
225 st.ResultText(0,publickey);
\r
229 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblBoardListRequests WHERE Day=? AND IdentityID=?;");
\r
230 st2.Bind(0,now.Format("%Y-%m-%d"));
\r
231 st2.Bind(1,identityid);
\r
235 if(st2.RowReturned())
\r
237 if(st2.ResultNull(0)==false)
\r
239 st2.ResultInt(0,index);
\r
245 StringFunctions::Convert(index,indexstr);
\r
246 StringFunctions::Convert(identityid,identityidstr);
\r
248 message.SetName("ClientGet");
\r
249 message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|BoardList|"+indexstr+".xml";
\r
250 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
251 message["ReturnType"]="direct";
\r
252 message["MaxSize"]="100000"; // 100 KB
\r
254 m_fcp->SendMessage(message);
\r
256 m_requesting.push_back(identityid);
\r
261 m_ids[identityid]=true;
\r