+#include "../../include/freenet/boardlistrequester.h"\r
+#include "../../include/freenet/boardlistxml.h"\r
+\r
+#ifdef XMEM\r
+ #include <xmem.h>\r
+#endif\r
+\r
+BoardListRequester::BoardListRequester()\r
+{\r
+ Initialize();\r
+}\r
+\r
+BoardListRequester::BoardListRequester(FCPv2 *fcp):IIndexRequester<long>(fcp)\r
+{\r
+ Initialize();\r
+}\r
+\r
+const bool BoardListRequester::HandleAllData(FCPMessage &message)\r
+{ \r
+ DateTime now;\r
+ SQLite3DB::Statement st;\r
+ std::vector<std::string> idparts;\r
+ long datalength;\r
+ std::vector<char> data;\r
+ BoardListXML xml;\r
+ long identityid;\r
+ long index;\r
+\r
+ now.SetToGMTime();\r
+ StringFunctions::Split(message["Identifier"],"|",idparts);\r
+ StringFunctions::Convert(message["DataLength"],datalength);\r
+ StringFunctions::Convert(idparts[1],identityid);\r
+ StringFunctions::Convert(idparts[2],index);\r
+\r
+ // wait for all data to be received from connection\r
+ while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)\r
+ {\r
+ m_fcp->Update(1);\r
+ }\r
+\r
+ // if we got disconnected- return immediately\r
+ if(m_fcp->Connected()==false)\r
+ {\r
+ return false;\r
+ }\r
+\r
+ // receive the file\r
+ data.resize(datalength);\r
+ m_fcp->ReceiveRaw(&data[0],datalength);\r
+\r
+ // parse file into xml and update the database\r
+ if(xml.ParseXML(std::string(data.begin(),data.end()))==true)\r
+ {\r
+\r
+ SQLite3DB::Statement brd=m_db->Prepare("SELECT BoardID,BoardName,BoardDescription FROM tblBoard WHERE BoardName=?;");\r
+ SQLite3DB::Statement ins=m_db->Prepare("INSERT INTO tblBoard(BoardName,BoardDescription,DateAdded) VALUES(?,?,?);");\r
+ SQLite3DB::Statement upd=m_db->Prepare("UPDATE tblBoard SET BoardDescription=? WHERE BoardID=?;");\r
+ for(long i=0; i<xml.GetCount(); i++)\r
+ {\r
+ int boardid;\r
+ std::string name="";\r
+ std::string description="";\r
+\r
+ brd.Bind(0,xml.GetName(i));\r
+ brd.Step();\r
+ \r
+ if(brd.RowReturned())\r
+ {\r
+ brd.ResultInt(0,boardid);\r
+ brd.ResultText(2,description);\r
+ if(description=="" && xml.GetDescription(i)!="")\r
+ {\r
+ upd.Bind(0,xml.GetDescription(i));\r
+ upd.Step();\r
+ upd.Reset();\r
+ }\r
+ }\r
+ else\r
+ {\r
+ ins.Bind(0,xml.GetName(i));\r
+ ins.Bind(1,xml.GetDescription(i));\r
+ ins.Bind(2,now.Format("%Y-%m-%d %H:%M:%S"));\r
+ ins.Step();\r
+ ins.Reset();\r
+ }\r
+ brd.Reset();\r
+ }\r
+\r
+ st=m_db->Prepare("INSERT INTO tblBoardListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");\r
+ st.Bind(0,identityid);\r
+ st.Bind(1,idparts[4]);\r
+ st.Bind(2,index);\r
+ st.Step();\r
+ st.Finalize();\r
+\r
+ m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"BoardListRequester::HandleAllData parsed BoardList XML file : "+message["Identifier"]);\r
+ }\r
+ else\r
+ {\r
+ // bad data - mark index\r
+ st=m_db->Prepare("INSERT INTO tblBoardListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
+ st.Bind(0,identityid);\r
+ st.Bind(1,idparts[4]);\r
+ st.Bind(2,index);\r
+ st.Step();\r
+ st.Finalize();\r
+\r
+ m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"BoardListRequester::HandleAllData error parsing BoardList XML file : "+message["Identifier"]);\r
+ }\r
+\r
+ // remove this identityid from request list\r
+ RemoveFromRequestList(identityid);\r
+\r
+ return true;\r
+\r
+}\r
+\r
+const bool BoardListRequester::HandleGetFailed(FCPMessage &message)\r
+{\r
+ DateTime now;\r
+ SQLite3DB::Statement st;\r
+ std::vector<std::string> idparts;\r
+ long identityid;\r
+ long index;\r
+\r
+ now.SetToGMTime();\r
+ StringFunctions::Split(message["Identifier"],"|",idparts);\r
+ StringFunctions::Convert(idparts[1],identityid);\r
+ StringFunctions::Convert(idparts[2],index); \r
+\r
+ // if this is a fatal error - insert index into database so we won't try to download this index again\r
+ if(message["Fatal"]=="true")\r
+ {\r
+ st=m_db->Prepare("INSERT INTO tblBoardListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");\r
+ st.Bind(0,identityid);\r
+ st.Bind(1,idparts[4]);\r
+ st.Bind(2,index);\r
+ st.Step();\r
+ st.Finalize();\r
+\r
+ m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"BoardListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);\r
+ }\r
+\r
+ // remove this identityid from request list\r
+ RemoveFromRequestList(identityid);\r
+\r
+ return true;\r
+}\r
+\r
+void BoardListRequester::Initialize()\r
+{\r
+ std::string tempval="";\r
+\r
+ m_fcpuniquename="BoardListRequester";\r
+ m_maxrequests=0;\r
+\r
+ Option::Instance()->Get("MaxBoardListRequests",tempval);\r
+ StringFunctions::Convert(tempval,m_maxrequests);\r
+ if(m_maxrequests<0)\r
+ {\r
+ m_maxrequests=0;\r
+ m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxBoardListRequests is currently set at "+tempval+". It must be 0 or greater.");\r
+ }\r
+ if(m_maxrequests>100)\r
+ {\r
+ m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxBoardListRequests is currently set at "+tempval+". This value might be incorrectly configured.");\r
+ }\r
+}\r
+\r
+void BoardListRequester::PopulateIDList()\r
+{\r
+ int id;\r
+ DateTime today;\r
+ today.SetToGMTime();\r
+\r
+ 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
+ st.Step();\r
+\r
+ m_ids.clear();\r
+\r
+ while(st.RowReturned())\r
+ {\r
+ st.ResultInt(0,id);\r
+ m_ids[id]=false;\r
+ st.Step();\r
+ }\r
+\r
+}\r
+\r
+void BoardListRequester::StartRequest(const long &identityid)\r
+{\r
+ DateTime now;\r
+ FCPMessage message;\r
+ std::string publickey;\r
+ std::string indexstr;\r
+ int index;\r
+ std::string identityidstr;\r
+\r
+ SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE identityid=?;");\r
+ st.Bind(0,identityid);\r
+ st.Step();\r
+\r
+ if(st.RowReturned())\r
+ {\r
+ st.ResultText(0,publickey);\r
+\r
+ now.SetToGMTime();\r
+\r
+ SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblBoardListRequests WHERE Day=? AND IdentityID=?;");\r
+ st2.Bind(0,now.Format("%Y-%m-%d"));\r
+ st2.Bind(1,identityid);\r
+ st2.Step();\r
+\r
+ index=0;\r
+ if(st2.RowReturned())\r
+ {\r
+ if(st2.ResultNull(0)==false)\r
+ {\r
+ st2.ResultInt(0,index);\r
+ index++;\r
+ }\r
+ }\r
+ st2.Finalize();\r
+\r
+ StringFunctions::Convert(index,indexstr);\r
+ StringFunctions::Convert(identityid,identityidstr);\r
+\r
+ message.SetName("ClientGet");\r
+ message["URI"]=publickey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|BoardList|"+indexstr+".xml";\r
+ message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];\r
+ message["ReturnType"]="direct";\r
+ message["MaxSize"]="100000"; // 100 KB\r
+\r
+ m_fcp->SendMessage(message);\r
+\r
+ m_requesting.push_back(identityid);\r
+\r
+ }\r
+ st.Finalize();\r
+\r
+ m_ids[identityid]=true;\r
+\r
+}\r