1 #include "../../include/freenet/identityintroductioninserter.h"
\r
2 #include "../../include/freenet/identityintroductionxml.h"
\r
3 #include "../../include/xyssl/sha1.h"
\r
4 #include "../../include/stringfunctions.h"
\r
5 #include "../../include/hex.h"
\r
6 #include "../../include/option.h"
\r
12 IdentityIntroductionInserter::IdentityIntroductionInserter()
\r
17 IdentityIntroductionInserter::IdentityIntroductionInserter(FCPv2 *fcp):IFCPConnected(fcp)
\r
22 void IdentityIntroductionInserter::CheckForNewInserts()
\r
24 SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID, Day, UUID, Solution FROM tblIdentityIntroductionInserts WHERE Inserted='false';");
\r
27 if(rs.GetField(0) && rs.GetField(1) && rs.GetField(2))
\r
29 StartInsert(rs.GetInt(0),rs.GetField(1),rs.GetField(2),rs.GetField(3));
\r
34 void IdentityIntroductionInserter::FCPConnected()
\r
39 void IdentityIntroductionInserter::FCPDisconnected()
\r
44 const bool IdentityIntroductionInserter::HandleMessage(FCPMessage &message)
\r
47 if(message["Identifier"].find("IdentityIntroductionInserter")==0)
\r
49 std::vector<std::string> idparts;
\r
50 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
52 // no action for URIGenerated
\r
53 if(message.GetName()=="URIGenerated")
\r
58 if(message.GetName()=="PutFailed")
\r
60 // if fatal error, or data is already there - remove insert from database
\r
61 if(message["Fatal"]=="true" || message["Code"]=="9")
\r
63 m_db->Execute("DELETE FROM tblIdentityIntroductionInserts WHERE UUID='"+idparts[3]+"';");
\r
64 // update the puzzle from the request table (set to not found) because we don't need it anymore and don't want to user tyring to solve it again
\r
65 m_db->Execute("UPDATE tblIntroductionPuzzleRequests SET Found='false' WHERE UUID='"+idparts[3]+"';");
\r
66 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"IdentityIntroductionInserter::HandleMessage received fatal error trying to insert IdentityIntroduction "+idparts[3]);
\r
72 if(message.GetName()=="PutSuccessful")
\r
74 m_db->Execute("UPDATE tblIdentityIntroductionInserts SET Inserted='true' WHERE UUID='"+idparts[3]+"';");
\r
76 m_log->WriteLog(LogFile::LOGLEVEL_INFO,"IdentityIntroductionInserter::HandleMessage successfully inserted IdentityIntroduction "+idparts[3]);
\r
80 if(message.GetName()=="IdentifierCollision")
\r
90 void IdentityIntroductionInserter::Initialize()
\r
93 Option::Instance()->Get("MessageBase",m_messagebase);
\r
96 void IdentityIntroductionInserter::Process()
\r
101 // only do 1 insert at a time
\r
102 if(!m_inserting && m_lastchecked<(now-(1.0/1440.0)))
\r
104 CheckForNewInserts();
\r
109 void IdentityIntroductionInserter::RegisterWithThread(FreenetMasterThread *thread)
\r
111 thread->RegisterFCPConnected(this);
\r
112 thread->RegisterFCPMessageHandler(this);
\r
113 thread->RegisterPeriodicProcessor(this);
\r
116 void IdentityIntroductionInserter::StartInsert(const long localidentityid, const std::string &day, const std::string &UUID, const std::string &solution)
\r
118 FCPMessage message;
\r
119 IdentityIntroductionXML xml;
\r
120 std::string publickey;
\r
122 std::string datasizestr;
\r
123 std::vector<unsigned char> hash;
\r
124 std::string encodedhash;
\r
126 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblLocalIdentity WHERE PublicKey IS NOT NULL AND PublicKey<>'' AND LocalIdentityID=?;");
\r
127 st.Bind(0,localidentityid);
\r
130 if(st.RowReturned())
\r
132 st.ResultText(0,publickey);
\r
134 xml.SetIdentity(publickey);
\r
136 StringFunctions::Convert(data.size(),datasizestr);
\r
139 sha1((unsigned char *)solution.c_str(),solution.size(),&hash[0]);
\r
140 Hex::Encode(hash,encodedhash);
\r
142 message.SetName("ClientPut");
\r
143 message["URI"]="KSK@"+m_messagebase+"|"+day+"|"+UUID+"|"+encodedhash+".xml";
\r
144 message["Identifier"]="IdentityIntroductionInserter|"+message["URI"];
\r
145 message["UploadFrom"]="direct";
\r
146 message["DataLength"]=datasizestr;
\r
148 m_fcp->SendMessage(message);
\r
149 m_fcp->SendRaw(data.c_str(),data.size());
\r
155 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IdentityIntroductionInserter::StartInsert could not find a public key for identity. It is probably a new identity that doesn't have a key yet.");
\r