1 #include "../../include/freenet/boardlistrequester.h"
\r
2 #include "../../include/freenet/boardlistxml.h"
\r
4 #include <Poco/DateTime.h>
\r
5 #include <Poco/DateTimeFormatter.h>
\r
11 BoardListRequester::BoardListRequester(SQLite3DB::DB *db):IIndexRequester<long>(db)
\r
16 BoardListRequester::BoardListRequester(SQLite3DB::DB *db, FCPv2::Connection *fcp):IIndexRequester<long>(db,fcp)
\r
21 std::string BoardListRequester::GetIdentityName(const long identityid)
\r
23 SQLite3DB::Statement st=m_db->Prepare("SELECT Name,PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
24 st.Bind(0,identityid);
\r
26 if(st.RowReturned())
\r
28 std::vector<std::string> keyparts;
\r
31 st.ResultText(0,name);
\r
32 st.ResultText(1,key);
\r
34 StringFunctions::SplitMultiple(key,"@,",keyparts);
\r
36 if(keyparts.size()>1)
\r
38 return name+"@"+keyparts[1];
\r
42 return name+"@invalidpublickey";
\r
51 const bool BoardListRequester::HandleAllData(FCPv2::Message &message)
\r
54 SQLite3DB::Statement st;
\r
55 std::vector<std::string> idparts;
\r
57 std::vector<char> data;
\r
61 std::string identityname="";
\r
63 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
64 StringFunctions::Convert(message["DataLength"],datalength);
\r
65 StringFunctions::Convert(idparts[1],identityid);
\r
66 StringFunctions::Convert(idparts[2],index);
\r
68 identityname=GetIdentityName(identityid);
\r
70 // wait for all data to be received from connection
\r
71 m_fcp->WaitForBytes(1000,datalength);
\r
73 // if we got disconnected- return immediately
\r
74 if(m_fcp->IsConnected()==false)
\r
80 m_fcp->Receive(data,datalength);
\r
82 // parse file into xml and update the database
\r
83 if(data.size()>0 && xml.ParseXML(std::string(data.begin(),data.end()))==true)
\r
86 SQLite3DB::Statement brd=m_db->Prepare("SELECT BoardID,BoardName,BoardDescription FROM tblBoard WHERE BoardName=?;");
\r
87 SQLite3DB::Statement ins=m_db->Prepare("INSERT INTO tblBoard(BoardName,BoardDescription,DateAdded,SaveReceivedMessages,AddedMethod) VALUES(?,?,?,?,?);");
\r
88 SQLite3DB::Statement upd=m_db->Prepare("UPDATE tblBoard SET BoardDescription=? WHERE BoardID=?;");
\r
89 for(long i=0; i<xml.GetCount(); i++)
\r
92 std::string name="";
\r
93 std::string description="";
\r
95 brd.Bind(0,xml.GetName(i));
\r
98 if(brd.RowReturned())
\r
100 brd.ResultInt(0,boardid);
\r
101 brd.ResultText(2,description);
\r
102 if(description=="" && xml.GetDescription(i)!="")
\r
104 upd.Bind(0,xml.GetDescription(i));
\r
105 upd.Bind(1,boardid);
\r
112 ins.Bind(0,xml.GetName(i));
\r
113 ins.Bind(1,xml.GetDescription(i));
\r
114 ins.Bind(2,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));
\r
115 if(m_savemessagesfromnewboards)
\r
117 ins.Bind(3,"true");
\r
121 ins.Bind(3,"false");
\r
123 ins.Bind(4,"Board List of "+identityname);
\r
130 st=m_db->Prepare("INSERT INTO tblBoardListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'true');");
\r
131 st.Bind(0,identityid);
\r
132 st.Bind(1,idparts[4]);
\r
137 m_log->debug("BoardListRequester::HandleAllData parsed BoardList XML file : "+message["Identifier"]);
\r
141 // bad data - mark index
\r
142 st=m_db->Prepare("INSERT INTO tblBoardListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
143 st.Bind(0,identityid);
\r
144 st.Bind(1,idparts[4]);
\r
149 m_log->error("BoardListRequester::HandleAllData error parsing BoardList XML file : "+message["Identifier"]);
\r
152 // remove this identityid from request list
\r
153 RemoveFromRequestList(identityid);
\r
159 const bool BoardListRequester::HandleGetFailed(FCPv2::Message &message)
\r
161 SQLite3DB::Statement st;
\r
162 std::vector<std::string> idparts;
\r
166 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
167 StringFunctions::Convert(idparts[1],identityid);
\r
168 StringFunctions::Convert(idparts[2],index);
\r
170 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
171 if(message["Fatal"]=="true")
\r
173 st=m_db->Prepare("INSERT INTO tblBoardListRequests(IdentityID,Day,RequestIndex,Found) VALUES(?,?,?,'false');");
\r
174 st.Bind(0,identityid);
\r
175 st.Bind(1,idparts[4]);
\r
180 m_log->error("BoardListRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
183 // remove this identityid from request list
\r
184 RemoveFromRequestList(identityid);
\r
189 void BoardListRequester::Initialize()
\r
191 std::string tempval="";
\r
193 m_fcpuniquename="BoardListRequester";
\r
195 Option option(m_db);
\r
197 option.GetInt("MaxBoardListRequests",m_maxrequests);
\r
198 if(m_maxrequests<0)
\r
201 m_log->error("Option MaxBoardListRequests is currently set at "+tempval+". It must be 0 or greater.");
\r
203 if(m_maxrequests>100)
\r
205 m_log->warning("Option MaxBoardListRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
208 option.Get("SaveMessagesFromNewBoards",tempval);
\r
209 if(tempval=="true")
\r
211 m_savemessagesfromnewboards=true;
\r
215 m_savemessagesfromnewboards=false;
\r
218 option.Get("LocalTrustOverridesPeerTrust",tempval);
\r
219 if(tempval=="true")
\r
221 m_localtrustoverrides=true;
\r
225 m_localtrustoverrides=false;
\r
230 void BoardListRequester::PopulateIDList()
\r
233 Poco::DateTime today;
\r
235 SQLite3DB::Statement st;
\r
237 if(m_localtrustoverrides==false)
\r
239 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey <> '' AND LastSeen>='"+Poco::DateTimeFormatter::format(today,"%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' AND FailureCount<=(SELECT OptionValue FROM tblOption WHERE Option='MaxFailureCount') ORDER BY LocalMessageTrust+LocalTrustListTrust DESC, LastSeen;");
\r
243 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey <> '' AND LastSeen>='"+Poco::DateTimeFormatter::format(today,"%Y-%m-%d")+"' AND (LocalMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalMessageTrust') OR (LocalMessageTrust IS NULL AND (PeerMessageTrust IS NULL OR PeerMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinPeerMessageTrust')))) AND PublishBoardList='true' AND FailureCount<=(SELECT OptionValue FROM tblOption WHERE Option='MaxFailureCount') ORDER BY LocalMessageTrust+LocalTrustListTrust DESC, LastSeen;");
\r
249 while(st.RowReturned())
\r
251 st.ResultInt(0,id);
\r
258 void BoardListRequester::StartRequest(const long &identityid)
\r
260 Poco::DateTime now;
\r
261 FCPv2::Message message;
\r
262 std::string publickey;
\r
263 std::string indexstr;
\r
265 std::string identityidstr;
\r
267 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
268 st.Bind(0,identityid);
\r
271 if(st.RowReturned())
\r
273 st.ResultText(0,publickey);
\r
275 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblBoardListRequests WHERE Day=? AND IdentityID=?;");
\r
276 st2.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));
\r
277 st2.Bind(1,identityid);
\r
281 if(st2.RowReturned())
\r
283 if(st2.ResultNull(0)==false)
\r
285 st2.ResultInt(0,index);
\r
291 StringFunctions::Convert(index,indexstr);
\r
292 StringFunctions::Convert(identityid,identityidstr);
\r
294 message.SetName("ClientGet");
\r
295 message["URI"]=publickey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|BoardList|"+indexstr+".xml";
\r
296 message["Identifier"]=m_fcpuniquename+"|"+identityidstr+"|"+indexstr+"|"+message["URI"];
\r
297 message["ReturnType"]="direct";
\r
298 message["MaxSize"]="100000"; // 100 KB
\r
300 m_fcp->Send(message);
\r
302 m_requesting.push_back(identityid);
\r
307 m_ids[identityid]=true;
\r