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
75 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false', LastInsertedTrustList='"+now.Format("%Y-%m-%d %H:%M:%S")+"' WHERE LocalIdentityID="+idparts[1]+";");
\r
76 m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
77 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListInserter::HandleMessage inserted TrustList xml");
\r
81 if(message.GetName()=="PutFailed")
\r
83 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='false' WHERE LocalIdentityID="+idparts[1]+";");
\r
84 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"TrustListInserter::HandleMessage failure inserting TrustList xml. Code="+message["Code"]+" Description="+message["CodeDescription"]);
\r
86 // if code 9 (collision), then insert index into inserted table
\r
87 if(message["Code"]=="9")
\r
89 m_db->Execute("INSERT INTO tblTrustListInserts(LocalIdentityID,Day,InsertIndex) VALUES("+idparts[1]+",'"+idparts[4]+"',"+idparts[2]+");");
\r
100 void TrustListInserter::Initialize()
\r
102 Option::Instance()->Get("MessageBase",m_messagebase);
\r
103 m_lastchecked.SetToGMTime();
\r
106 void TrustListInserter::Process()
\r
111 // check every minute
\r
112 if(m_lastchecked<=(now-(1.0/1440.0)))
\r
114 CheckForNeededInsert();
\r
119 void TrustListInserter::RegisterWithThread(FreenetMasterThread *thread)
\r
121 thread->RegisterFCPConnected(this);
\r
122 thread->RegisterFCPMessageHandler(this);
\r
123 thread->RegisterPeriodicProcessor(this);
\r
126 void TrustListInserter::StartInsert(const long localidentityid, const std::string &privatekey)
\r
128 FCPMessage message;
\r
131 std::string datasizestr;
\r
132 std::string publickey;
\r
134 int trustlisttrust;
\r
137 std::string indexstr;
\r
138 std::string localidentityidstr;
\r
139 std::string messagetrustcomment="";
\r
140 std::string trustlisttrustcomment="";
\r
143 date.SetToGMTime();
\r
145 // insert all identities not in trust list already
\r
146 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
148 // build the xml file - we only want to add identities that we recently saw, otherwise we could be inserting a ton of identities
\r
149 date.Add(0,0,0,-20); // identities seen in last 20 days
\r
150 //SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey, LocalMessageTrust, LocalTrustListTrust, MessageTrustComment, TrustListTrustComment FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey<>'' AND LastSeen>=?;");
\r
151 // we want to order by public key so we can't do identity correllation based on the sequence of identities in the list.
\r
152 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey, tblIdentityTrust.LocalMessageTrust, tblIdentityTrust.LocalTrustListTrust, tblIdentityTrust.MessageTrustComment, tblIdentityTrust.TrustListTrustComment 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
153 st.Bind(0,date.Format("%Y-%m-%d"));
\r
154 st.Bind(1,localidentityid);
\r
156 while(st.RowReturned())
\r
158 st.ResultText(0,publickey);
\r
159 if(st.ResultNull(1)==false)
\r
161 st.ResultInt(1,messagetrust);
\r
167 if(st.ResultNull(2)==false)
\r
169 st.ResultInt(2,trustlisttrust);
\r
175 st.ResultText(3,messagetrustcomment);
\r
176 st.ResultText(4,trustlisttrustcomment);
\r
177 xml.AddTrust(publickey,messagetrust,trustlisttrust,messagetrustcomment,trustlisttrustcomment);
\r
181 // get next insert index
\r
182 st=m_db->Prepare("SELECT MAX(InsertIndex) FROM tblTrustListInserts WHERE LocalIdentityID=? AND Day=?;");
\r
183 st.Bind(0,localidentityid);
\r
184 st.Bind(1,now.Format("%Y-%m-%d"));
\r
188 if(st.RowReturned() && st.ResultNull(0)==false)
\r
190 st.ResultInt(0,index);
\r
194 StringFunctions::Convert(localidentityid,localidentityidstr);
\r
195 StringFunctions::Convert(index,indexstr);
\r
198 StringFunctions::Convert(data.size(),datasizestr);
\r
200 message.SetName("ClientPut");
\r
201 message["URI"]=privatekey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";
\r
202 message["Identifier"]="TrustListInserter|"+localidentityidstr+"|"+indexstr+"|"+message["URI"];
\r
203 message["UploadFrom"]="direct";
\r
204 message["DataLength"]=datasizestr;
\r
205 m_fcp->SendMessage(message);
\r
206 m_fcp->SendRaw(data.c_str(),data.size());
\r
208 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='true' WHERE LocalIdentityID="+localidentityidstr+";");
\r