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
10 TrustListInserter::TrustListInserter()
\r
15 TrustListInserter::TrustListInserter(FCPv2 *fcp):IFCPConnected(fcp)
\r
20 void TrustListInserter::CheckForNeededInsert()
\r
24 int currentday=date.GetDay();
\r
26 // 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
27 if(currentday!=date.GetDay())
\r
29 date.Set(date.GetYear(),date.GetMonth(),currentday);
\r
31 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<='"+date.Format("%Y-%m-%d %H:%M:%S")+"' OR LastInsertedTrustList IS NULL);");
\r
33 if(rs.Empty()==false)
\r
35 StartInsert(rs.GetInt(0),rs.GetField(1));
\r
39 void TrustListInserter::FCPConnected()
\r
41 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false';");
\r
44 void TrustListInserter::FCPDisconnected()
\r
49 const bool TrustListInserter::HandleMessage(FCPMessage &message)
\r
52 if(message["Identifier"].find("TrustListInserter")==0)
\r
56 std::vector<std::string> idparts;
\r
59 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
61 // no action for URIGenerated
\r
62 if(message.GetName()=="URIGenerated")
\r
67 // no action for IdentifierCollision
\r
68 if(message.GetName()=="IdentifierCollision")
\r
73 if(message.GetName()=="PutSuccessful")
\r
76 if(idparts[0]=="TrustListInserter")
\r
78 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false', LastInsertedTrustList='"+now.Format("%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");
\r
79 m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
80 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListInserter::HandleMessage inserted TrustList xml");
\r
85 if(message.GetName()=="PutFailed" && idparts[0]=="TrustListInserter")
\r
88 if(idparts[0]=="TrustListInserter")
\r
90 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false' WHERE LocalIdentityID="+idparts[1]+";");
\r
91 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListInserter::HandleMessage failure inserting TrustList xml. Code="+message["Code"]+" Description="+message["CodeDescription"]);
\r
93 // if code 9 (collision), then insert index into inserted table
\r
94 if(message["Code"]=="9")
\r
96 m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
107 void TrustListInserter::Initialize()
\r
109 Option::Instance()->Get("MessageBase",m_messagebase);
\r
110 m_lastchecked.SetToGMTime();
\r
113 void TrustListInserter::Process()
\r
118 // check every minute
\r
119 if(m_lastchecked<=(now-(1.0/1440.0)))
\r
121 CheckForNeededInsert();
\r
126 void TrustListInserter::RegisterWithThread(FreenetMasterThread *thread)
\r
128 thread->RegisterFCPConnected(this);
\r
129 thread->RegisterFCPMessageHandler(this);
\r
130 thread->RegisterPeriodicProcessor(this);
\r
133 void TrustListInserter::StartInsert(const long localidentityid, const std::string &privatekey)
\r
135 FCPMessage message;
\r
138 std::string datasizestr;
\r
139 std::string publickey;
\r
141 int trustlisttrust;
\r
142 DateTime now,date,dateminus30,tempdate;
\r
144 std::string indexstr;
\r
145 std::string localidentityidstr;
\r
146 std::string messagetrustcomment="";
\r
147 std::string trustlisttrustcomment="";
\r
151 std::string dateadded="";
\r
154 date.SetToGMTime();
\r
155 dateminus30.SetToGMTime();
\r
156 dateminus30.Add(0,0,0,-30);
\r
158 // insert all identities not in trust list already
\r
159 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
161 // select statement for last message date for an identity
\r
162 SQLite3DB::Statement countst=m_db->Prepare("SELECT COUNT(*) FROM tblMessage WHERE IdentityID=? AND MessageDate>=?;");
\r
164 // build the xml file - we only want to add identities that we recently saw, otherwise we could be inserting a ton of identities
\r
165 date.Add(0,0,0,-15); // 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
166 //SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey, LocalMessageTrust, LocalTrustListTrust, MessageTrustComment, TrustListTrustComment FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey<>'' AND LastSeen>=?;");
\r
167 // we want to order by public key so we can't do identity correllation based on the sequence of identities in the list.
\r
168 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
169 st.Bind(0,date.Format("%Y-%m-%d"));
\r
170 st.Bind(1,localidentityid);
\r
172 while(st.RowReturned())
\r
174 st.ResultText(0,publickey);
\r
175 if(st.ResultNull(1)==false)
\r
177 st.ResultInt(1,messagetrust);
\r
183 if(st.ResultNull(2)==false)
\r
185 st.ResultInt(2,trustlisttrust);
\r
191 st.ResultText(3,messagetrustcomment);
\r
192 st.ResultText(4,trustlisttrustcomment);
\r
194 st.ResultInt(5,identityid);
\r
196 st.ResultText(6,dateadded);
\r
200 // add the identity to the trust list if they have posted a message in the last 30 days
\r
201 countst.Bind(0,identityid);
\r
202 countst.Bind(1,dateminus30.Format("%Y-%m-%d"));
\r
204 if(countst.RowReturned())
\r
207 countst.ResultInt(0,count);
\r
215 // no messages in last 30 days - add the identity if we learned about them less than 5 days ago
\r
216 if(add==false && dateadded!="")
\r
218 tempdate.Set(dateadded);
\r
219 if(tempdate>=(now-5.0))
\r
227 xml.AddTrust(publickey,messagetrust,trustlisttrust,messagetrustcomment,trustlisttrustcomment);
\r
233 // get next insert index
\r
234 st=m_db->Prepare("SELECT MAX(InsertIndex) FROM tblTrustListInserts WHERE LocalIdentityID=? AND Day=?;");
\r
235 st.Bind(0,localidentityid);
\r
236 st.Bind(1,now.Format("%Y-%m-%d"));
\r
240 if(st.RowReturned() && st.ResultNull(0)==false)
\r
242 st.ResultInt(0,index);
\r
246 StringFunctions::Convert(localidentityid,localidentityidstr);
\r
247 StringFunctions::Convert(index,indexstr);
\r
250 StringFunctions::Convert(data.size(),datasizestr);
\r
252 message.SetName("ClientPut");
\r
253 message["URI"]=privatekey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";
\r
254 message["Identifier"]="TrustListInserter|"+localidentityidstr+"|"+indexstr+"|"+message["URI"];
\r
255 message["UploadFrom"]="direct";
\r
256 message["DataLength"]=datasizestr;
\r
257 m_fcp->SendMessage(message);
\r
258 m_fcp->SendRaw(data.c_str(),data.size());
\r
262 message.SetName("ClientPutComplexDir");
\r
263 message["URI"]="USK"+privatekey.substr(3)+m_messagebase+"|"+now.Format("%Y.%m.%d")+"|TrustList/0/";
\r
264 message["Identifier"]="TrustListInserterUSK|"+message["URI"];
\r
265 message["DefaultName"]="TrustList.xml";
\r
266 message["Files.0.Name"]="TrustList.xml";
\r
267 message["Files.0.UplaodFrom"]="direct";
\r
268 message["Files.0.DataLength"]=datasizestr;
\r
269 m_fcp->SendMessage(message);
\r
270 m_fcp->SendRaw(data.c_str(),data.size());
\r
272 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='true' WHERE LocalIdentityID="+localidentityidstr+";");
\r