1 #include "../../include/freenet/trustlistinserter.h"
\r
2 #include "../../include/option.h"
\r
3 #include "../../include/freenet/trustlistxml.h"
\r
4 #include "../../include/stringfunctions.h"
\r
6 #include <Poco/Timespan.h>
\r
7 #include <Poco/DateTimeFormatter.h>
\r
8 #include <Poco/DateTimeParser.h>
\r
14 TrustListInserter::TrustListInserter()
\r
19 TrustListInserter::TrustListInserter(FCPv2 *fcp):IFCPConnected(fcp)
\r
24 void TrustListInserter::CheckForNeededInsert()
\r
26 Poco::DateTime date;
\r
27 int currentday=date.day();
\r
28 date-=Poco::Timespan(0,6,0,0,0);
\r
29 // insert trust lists every 6 hours - if 6 hours ago was different day then set to midnight of current day to insert list today ASAP
\r
30 if(currentday!=date.day())
\r
32 date.assign(date.year(),date.month(),currentday,0,0,0);
\r
34 SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID, PrivateKey FROM tblLocalIdentity WHERE PrivateKey IS NOT NULL AND PrivateKey <> '' AND PublishTrustList='true' AND InsertingTrustList='false' AND (LastInsertedTrustList<='"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d %H:%M:%S")+"' OR LastInsertedTrustList IS NULL);");
\r
36 if(rs.Empty()==false)
\r
38 StartInsert(rs.GetInt(0),rs.GetField(1));
\r
42 void TrustListInserter::FCPConnected()
\r
44 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false';");
\r
47 void TrustListInserter::FCPDisconnected()
\r
52 const bool TrustListInserter::HandleMessage(FCPMessage &message)
\r
55 if(message["Identifier"].find("TrustListInserter")==0)
\r
59 std::vector<std::string> idparts;
\r
61 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
63 // no action for URIGenerated
\r
64 if(message.GetName()=="URIGenerated")
\r
69 // no action for IdentifierCollision
\r
70 if(message.GetName()=="IdentifierCollision")
\r
75 if(message.GetName()=="PutSuccessful")
\r
78 if(idparts[0]=="TrustListInserter")
\r
80 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false', LastInsertedTrustList='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");
\r
81 m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
82 m_log->debug("TrustListInserter::HandleMessage inserted TrustList xml");
\r
87 if(message.GetName()=="PutFailed" && idparts[0]=="TrustListInserter")
\r
90 if(idparts[0]=="TrustListInserter")
\r
92 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false' WHERE LocalIdentityID="+idparts[1]+";");
\r
93 m_log->debug("TrustListInserter::HandleMessage failure inserting TrustList xml. Code="+message["Code"]+" Description="+message["CodeDescription"]);
\r
95 // if code 9 (collision), then insert index into inserted table
\r
96 if(message["Code"]=="9")
\r
98 m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
109 void TrustListInserter::Initialize()
\r
111 Option::Instance()->Get("MessageBase",m_messagebase);
\r
112 m_lastchecked=Poco::Timestamp();
\r
115 void TrustListInserter::Process()
\r
117 Poco::DateTime now;
\r
119 // check every minute
\r
120 if(m_lastchecked<=(now-Poco::Timespan(0,0,1,0,0)))
\r
122 CheckForNeededInsert();
\r
127 void TrustListInserter::RegisterWithThread(FreenetMasterThread *thread)
\r
129 thread->RegisterFCPConnected(this);
\r
130 thread->RegisterFCPMessageHandler(this);
\r
131 thread->RegisterPeriodicProcessor(this);
\r
134 void TrustListInserter::StartInsert(const long localidentityid, const std::string &privatekey)
\r
136 FCPMessage message;
\r
139 std::string datasizestr;
\r
140 std::string publickey;
\r
142 int trustlisttrust;
\r
143 Poco::DateTime now,date,dateminus30,tempdate;
\r
145 std::string indexstr;
\r
146 std::string localidentityidstr;
\r
147 std::string messagetrustcomment="";
\r
148 std::string trustlisttrustcomment="";
\r
152 std::string dateadded="";
\r
154 dateminus30-=Poco::Timespan(30,0,0,0,0);
\r
156 // insert all identities not in trust list already
\r
157 m_db->Execute("INSERT INTO tblIdentityTrust(LocalIdentityID,IdentityID) SELECT LocalIdentityID,IdentityID FROM tblLocalIdentity,tblIdentity WHERE LocalIdentityID || '_' || IdentityID NOT IN (SELECT LocalIdentityID || '_' || IdentityID FROM tblIdentityTrust);");
\r
159 // select statement for last message date for an identity
\r
160 SQLite3DB::Statement countst=m_db->Prepare("SELECT COUNT(*) FROM tblMessage WHERE IdentityID=? AND MessageDate>=?;");
\r
162 // build the xml file - we only want to add identities that we recently saw, otherwise we could be inserting a ton of identities
\r
163 date-=Poco::Timespan(15,0,0,0,0); // identities seen in last 15 days - the maintenance page lets us delete identities not seen in 20 days, so this gives us a window where the identity won't be deleted and then found in a trust list and readded immediately
\r
164 //SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey, LocalMessageTrust, LocalTrustListTrust, MessageTrustComment, TrustListTrustComment FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey<>'' AND LastSeen>=?;");
\r
165 // we want to order by public key so we can't do identity correllation based on the sequence of identities in the list.
\r
166 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey, tblIdentityTrust.LocalMessageTrust, tblIdentityTrust.LocalTrustListTrust, tblIdentityTrust.MessageTrustComment, tblIdentityTrust.TrustListTrustComment, tblIdentity.IdentityID, tblIdentity.DateAdded FROM tblIdentity INNER JOIN tblIdentityTrust ON tblIdentity.IdentityID=tblIdentityTrust.IdentityID WHERE PublicKey IS NOT NULL AND PublicKey<>'' AND LastSeen>=? AND tblIdentityTrust.LocalIdentityID=? ORDER BY PublicKey;");
\r
167 st.Bind(0,Poco::DateTimeFormatter::format(date,"%Y-%m-%d"));
\r
168 st.Bind(1,localidentityid);
\r
170 while(st.RowReturned())
\r
172 st.ResultText(0,publickey);
\r
173 if(st.ResultNull(1)==false)
\r
175 st.ResultInt(1,messagetrust);
\r
181 if(st.ResultNull(2)==false)
\r
183 st.ResultInt(2,trustlisttrust);
\r
189 st.ResultText(3,messagetrustcomment);
\r
190 st.ResultText(4,trustlisttrustcomment);
\r
192 st.ResultInt(5,identityid);
\r
194 st.ResultText(6,dateadded);
\r
198 // add the identity to the trust list if they have posted a message in the last 30 days
\r
199 countst.Bind(0,identityid);
\r
200 countst.Bind(1,Poco::DateTimeFormatter::format(dateminus30,"%Y-%m-%d"));
\r
202 if(countst.RowReturned())
\r
205 countst.ResultInt(0,count);
\r
213 // no messages in last 30 days - add the identity if we learned about them less than 5 days ago
\r
214 if(add==false && dateadded!="")
\r
217 if(Poco::DateTimeParser::tryParse(dateadded,tempdate,tzdiff)==false)
\r
219 tempdate=Poco::Timestamp();
\r
220 m_log->fatal("TrustListInserter::StartInsert could not parse date "+dateadded);
\r
222 if(tempdate>=(now-Poco::Timespan(5,0,0,0,0)))
\r
230 xml.AddTrust(publickey,messagetrust,trustlisttrust,messagetrustcomment,trustlisttrustcomment);
\r
236 // get next insert index
\r
237 st=m_db->Prepare("SELECT MAX(InsertIndex) FROM tblTrustListInserts WHERE LocalIdentityID=? AND Day=?;");
\r
238 st.Bind(0,localidentityid);
\r
239 st.Bind(1,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));
\r
243 if(st.RowReturned() && st.ResultNull(0)==false)
\r
245 st.ResultInt(0,index);
\r
249 StringFunctions::Convert(localidentityid,localidentityidstr);
\r
250 StringFunctions::Convert(index,indexstr);
\r
253 StringFunctions::Convert(data.size(),datasizestr);
\r
255 message.SetName("ClientPut");
\r
256 message["URI"]=privatekey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|TrustList|"+indexstr+".xml";
\r
257 message["Identifier"]="TrustListInserter|"+localidentityidstr+"|"+indexstr+"|"+message["URI"];
\r
258 message["UploadFrom"]="direct";
\r
259 message["DataLength"]=datasizestr;
\r
260 m_fcp->SendMessage(message);
\r
261 m_fcp->SendRaw(data.c_str(),data.size());
\r
265 message.SetName("ClientPutComplexDir");
\r
266 message["URI"]="USK"+privatekey.substr(3)+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y.%m.%d")+"|TrustList/0/";
\r
267 message["Identifier"]="TrustListInserterUSK|"+message["URI"];
\r
268 message["DefaultName"]="TrustList.xml";
\r
269 message["Files.0.Name"]="TrustList.xml";
\r
270 message["Files.0.UplaodFrom"]="direct";
\r
271 message["Files.0.DataLength"]=datasizestr;
\r
272 m_fcp->SendMessage(message);
\r
273 m_fcp->SendRaw(data.c_str(),data.size());
\r
275 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='true' WHERE LocalIdentityID="+localidentityidstr+";");
\r