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
144 std::string indexstr;
\r
145 std::string localidentityidstr;
\r
146 std::string messagetrustcomment="";
\r
147 std::string trustlisttrustcomment="";
\r
150 date.SetToGMTime();
\r
152 // insert all identities not in trust list already
\r
153 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
155 // build the xml file - we only want to add identities that we recently saw, otherwise we could be inserting a ton of identities
\r
156 date.Add(0,0,0,-15); // identities seen in last 15 days
\r
157 //SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey, LocalMessageTrust, LocalTrustListTrust, MessageTrustComment, TrustListTrustComment FROM tblIdentity WHERE PublicKey IS NOT NULL AND PublicKey<>'' AND LastSeen>=?;");
\r
158 // we want to order by public key so we can't do identity correllation based on the sequence of identities in the list.
\r
159 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
160 st.Bind(0,date.Format("%Y-%m-%d"));
\r
161 st.Bind(1,localidentityid);
\r
163 while(st.RowReturned())
\r
165 st.ResultText(0,publickey);
\r
166 if(st.ResultNull(1)==false)
\r
168 st.ResultInt(1,messagetrust);
\r
174 if(st.ResultNull(2)==false)
\r
176 st.ResultInt(2,trustlisttrust);
\r
182 st.ResultText(3,messagetrustcomment);
\r
183 st.ResultText(4,trustlisttrustcomment);
\r
184 xml.AddTrust(publickey,messagetrust,trustlisttrust,messagetrustcomment,trustlisttrustcomment);
\r
188 // get next insert index
\r
189 st=m_db->Prepare("SELECT MAX(InsertIndex) FROM tblTrustListInserts WHERE LocalIdentityID=? AND Day=?;");
\r
190 st.Bind(0,localidentityid);
\r
191 st.Bind(1,now.Format("%Y-%m-%d"));
\r
195 if(st.RowReturned() && st.ResultNull(0)==false)
\r
197 st.ResultInt(0,index);
\r
201 StringFunctions::Convert(localidentityid,localidentityidstr);
\r
202 StringFunctions::Convert(index,indexstr);
\r
205 StringFunctions::Convert(data.size(),datasizestr);
\r
207 message.SetName("ClientPut");
\r
208 message["URI"]=privatekey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|TrustList|"+indexstr+".xml";
\r
209 message["Identifier"]="TrustListInserter|"+localidentityidstr+"|"+indexstr+"|"+message["URI"];
\r
210 message["UploadFrom"]="direct";
\r
211 message["DataLength"]=datasizestr;
\r
212 m_fcp->SendMessage(message);
\r
213 m_fcp->SendRaw(data.c_str(),data.size());
\r
217 message.SetName("ClientPutComplexDir");
\r
218 message["URI"]="USK"+privatekey.substr(3)+m_messagebase+"|"+now.Format("%Y.%m.%d")+"|TrustList/0/";
\r
219 message["Identifier"]="TrustListInserterUSK|"+message["URI"];
\r
220 message["DefaultName"]="TrustList.xml";
\r
221 message["Files.0.Name"]="TrustList.xml";
\r
222 message["Files.0.UplaodFrom"]="direct";
\r
223 message["Files.0.DataLength"]=datasizestr;
\r
224 m_fcp->SendMessage(message);
\r
225 m_fcp->SendRaw(data.c_str(),data.size());
\r
227 m_db->Execute("UPDATE tblLocalIdentity SET InsertingTrustList='true' WHERE LocalIdentityID="+localidentityidstr+";");
\r