1 #include "../../include/freenet/messagerequester.h"
\r
2 #include "../../include/freenet/messagexml.h"
\r
10 MessageRequester::MessageRequester()
\r
15 MessageRequester::MessageRequester(FCPv2 *fcp):IIndexRequester<std::string>(fcp)
\r
20 const long MessageRequester::GetBoardID(const std::string &boardname, const std::string &identityname)
\r
22 std::string lowerboard=boardname;
\r
23 StringFunctions::LowerCase(lowerboard,lowerboard);
\r
24 SQLite3DB::Statement st=m_db->Prepare("SELECT BoardID FROM tblBoard WHERE BoardName=?;");
\r
25 st.Bind(0,lowerboard);
\r
28 if(st.RowReturned())
\r
31 st.ResultInt(0,boardid);
\r
38 st=m_db->Prepare("INSERT INTO tblBoard(BoardName,DateAdded,SaveReceivedMessages,AddedMethod) VALUES(?,?,?,?);");
\r
39 st.Bind(0,boardname);
\r
40 st.Bind(1,now.Format("%Y-%m-%d %H:%M:%S"));
\r
41 if(m_savemessagesfromnewboards)
\r
49 st.Bind(3,"Message from "+identityname);
\r
51 return st.GetLastInsertRowID();
\r
55 const std::string MessageRequester::GetIdentityName(const long identityid)
\r
57 SQLite3DB::Statement st=m_db->Prepare("SELECT Name,PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
58 st.Bind(0,identityid);
\r
60 if(st.RowReturned())
\r
62 std::vector<std::string> keyparts;
\r
65 st.ResultText(0,name);
\r
66 st.ResultText(1,key);
\r
68 StringFunctions::SplitMultiple(key,"@,",keyparts);
\r
70 if(keyparts.size()>1)
\r
72 return name+"@"+keyparts[1];
\r
76 return name+"@invalidpublickey";
\r
85 const bool MessageRequester::HandleAllData(FCPMessage &message)
\r
87 SQLite3DB::Statement st;
\r
88 std::vector<std::string> idparts;
\r
90 std::vector<char> data;
\r
94 bool inserted=false;
\r
95 bool validmessage=true;
\r
96 long savetoboardcount=0;
\r
98 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
99 StringFunctions::Convert(message["DataLength"],datalength);
\r
100 StringFunctions::Convert(idparts[2],identityid);
\r
101 StringFunctions::Convert(idparts[4],index);
\r
103 // wait for all data to be received from connection
\r
104 while(m_fcp->Connected() && m_fcp->ReceiveBufferSize()<datalength)
\r
109 // if we got disconnected- return immediately
\r
110 if(m_fcp->Connected()==false)
\r
115 // receive the file
\r
116 data.resize(datalength);
\r
117 m_fcp->ReceiveRaw(&data[0],datalength);
\r
119 // mark this index as received
\r
120 st=m_db->Prepare("UPDATE tblMessageRequests SET Found='true' WHERE IdentityID=? AND Day=? AND RequestIndex=?;");
\r
121 st.Bind(0,identityid);
\r
122 st.Bind(1,idparts[3]);
\r
127 // parse file into xml and update the database
\r
128 if(xml.ParseXML(std::string(data.begin(),data.end()))==true)
\r
130 std::vector<std::string> boards=xml.GetBoards();
\r
131 std::map<long,std::string> replyto=xml.GetInReplyTo();
\r
133 if(boards.size()>m_maxboardspermessage)
\r
135 boards.resize(m_maxboardspermessage);
\r
138 if(boards.size()<=0)
\r
140 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageRequester::HandleAllData Message XML did not contain any boards! "+message["Identifier"]);
\r
141 // remove this identityid from request list
\r
142 RemoveFromRequestList(idparts[1]);
\r
145 if(xml.GetReplyBoard()=="")
\r
147 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageRequester::HandleAllData Message XML did not contain a reply board! "+message["Identifier"]);
\r
148 // remove this identityid from request list
\r
149 RemoveFromRequestList(idparts[1]);
\r
153 // make sure the reply board is on the board list we are saving - if not, replace the last element of boards with the reply board
\r
154 if(xml.GetReplyBoard()!="" && std::find(boards.begin(),boards.end(),xml.GetReplyBoard())==boards.end() && boards.size()>0)
\r
156 boards[boards.size()-1]=xml.GetReplyBoard();
\r
159 // make sure domain of message id match 43 characters of public key of identity (remove - and ~) - if not, discard message
\r
160 // implement after 0.1.12 is released
\r
161 st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
162 st.Bind(0,identityid);
\r
164 if(st.RowReturned())
\r
166 std::vector<std::string> uuidparts;
\r
167 std::vector<std::string> keyparts;
\r
168 std::string keypart="";
\r
169 std::string publickey="";
\r
171 st.ResultText(0,publickey);
\r
173 StringFunctions::SplitMultiple(publickey,"@,",keyparts);
\r
174 StringFunctions::SplitMultiple(xml.GetMessageID(),"@",uuidparts);
\r
176 if(uuidparts.size()>1 && keyparts.size()>1)
\r
178 keypart=StringFunctions::Replace(StringFunctions::Replace(keyparts[1],"-",""),"~","");
\r
179 if(keypart!=uuidparts[1])
\r
181 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageRequester::HandleAllData MessageID in Message doesn't match public key of identity : "+message["Identifier"]);
\r
182 validmessage=false;
\r
187 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageRequester::HandleAllData Error with identity's public key or Message ID : "+message["Identifier"]);
\r
188 validmessage=false;
\r
193 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageRequester::HandleAllData Error couldn't find identity : "+message["Identifier"]);
\r
194 validmessage=false;
\r
197 // make sure we will at least save to 1 board before inserting message
\r
198 savetoboardcount=0;
\r
199 for(std::vector<std::string>::iterator bi=boards.begin(); bi!=boards.end(); bi++)
\r
201 if(SaveToBoard((*bi)))
\r
203 savetoboardcount++;
\r
207 if(validmessage && savetoboardcount>0)
\r
209 std::string nntpbody="";
\r
210 nntpbody=xml.GetBody();
\r
212 //add file keys/sizes to body
\r
213 std::vector<MessageXML::fileattachment> fileattachments=xml.GetFileAttachments();
\r
214 if(fileattachments.size()>0)
\r
216 nntpbody+="\r\nAttachments";
\r
218 for(std::vector<MessageXML::fileattachment>::iterator i=fileattachments.begin(); i!=fileattachments.end(); i++)
\r
220 std::string sizestr="0";
\r
221 StringFunctions::Convert((*i).m_size,sizestr);
\r
223 nntpbody+="\r\n"+(*i).m_key;
\r
224 nntpbody+="\r\n"+sizestr+" bytes";
\r
228 st=m_db->Prepare("INSERT INTO tblMessage(IdentityID,FromName,MessageDate,MessageTime,Subject,MessageUUID,ReplyBoardID,Body,MessageIndex) VALUES(?,?,?,?,?,?,?,?,?);");
\r
229 st.Bind(0,identityid);
\r
230 st.Bind(1,GetIdentityName(identityid));
\r
231 st.Bind(2,xml.GetDate());
\r
232 st.Bind(3,xml.GetTime());
\r
233 st.Bind(4,xml.GetSubject());
\r
234 st.Bind(5,xml.GetMessageID());
\r
235 st.Bind(6,GetBoardID(xml.GetReplyBoard(),GetIdentityName(identityid)));
\r
236 st.Bind(7,nntpbody);
\r
238 inserted=st.Step(true);
\r
239 int messageid=st.GetLastInsertRowID();
\r
244 st=m_db->Prepare("INSERT INTO tblMessageBoard(MessageID,BoardID) VALUES(?,?);");
\r
245 for(std::vector<std::string>::iterator i=boards.begin(); i!=boards.end(); i++)
\r
247 if(SaveToBoard((*i)))
\r
249 st.Bind(0,messageid);
\r
250 st.Bind(1,GetBoardID((*i),GetIdentityName(identityid)));
\r
257 st=m_db->Prepare("INSERT INTO tblMessageReplyTo(MessageID,ReplyToMessageUUID,ReplyOrder) VALUES(?,?,?);");
\r
258 for(std::map<long,std::string>::iterator j=replyto.begin(); j!=replyto.end(); j++)
\r
260 st.Bind(0,messageid);
\r
261 st.Bind(1,(*j).second);
\r
262 st.Bind(2,(*j).first);
\r
268 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageRequester::HandleAllData parsed Message XML file : "+message["Identifier"]);
\r
271 else // couldn't insert - was already in database
\r
273 //m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageRequester::HandleAddData could not insert message into database. "+message["Identifier"]);
\r
276 } // if validmessage
\r
280 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageRequester::HandleAllData error parsing Message XML file : "+message["Identifier"]);
\r
283 RemoveFromRequestList(idparts[1]);
\r
288 const bool MessageRequester::HandleGetFailed(FCPMessage &message)
\r
291 SQLite3DB::Statement st;
\r
292 std::vector<std::string> idparts;
\r
293 std::string requestid;
\r
298 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
299 requestid=idparts[1];
\r
300 StringFunctions::Convert(idparts[2],identityid);
\r
301 StringFunctions::Convert(idparts[4],index);
\r
303 // if this is a fatal error - insert index into database so we won't try to download this index again
\r
304 if(message["Fatal"]=="true")
\r
306 st=m_db->Prepare("UPDATE tblMessageRequests SET Found='true' WHERE IdentityID=? AND Day=? AND RequestIndex=?;");
\r
307 st.Bind(0,identityid);
\r
308 st.Bind(1,idparts[3]);
\r
313 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"MessageRequester::HandleGetFailed fatal error requesting "+message["Identifier"]);
\r
316 // remove this identityid from request list
\r
317 RemoveFromRequestList(requestid);
\r
322 void MessageRequester::Initialize()
\r
324 m_fcpuniquename="MessageRequester";
\r
325 std::string tempval;
\r
326 Option::Instance()->Get("MaxMessageRequests",tempval);
\r
327 StringFunctions::Convert(tempval,m_maxrequests);
\r
328 if(m_maxrequests<1)
\r
331 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxMessageRequests is currently set at "+tempval+". It must be 1 or greater.");
\r
333 if(m_maxrequests>100)
\r
335 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxMessageRequests is currently set at "+tempval+". This value might be incorrectly configured.");
\r
337 Option::Instance()->Get("MessageDownloadMaxDaysBackward",tempval);
\r
338 StringFunctions::Convert(tempval,m_maxdaysbackward);
\r
339 if(m_maxdaysbackward<0)
\r
341 m_maxdaysbackward=0;
\r
342 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MessageDownloadMaxDaysBackward is currently set at "+tempval+". It must be 0 or greater.");
\r
344 if(m_maxdaysbackward>30)
\r
346 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MessageDownloadMaxDaysBackward is currently set at "+tempval+". This value might be incorrectly configured.");
\r
348 Option::Instance()->Get("MaxPeerMessagesPerDay",tempval);
\r
349 StringFunctions::Convert(tempval,m_maxpeermessages);
\r
350 if(m_maxpeermessages<1)
\r
352 m_maxpeermessages=1;
\r
353 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxPeerMessagesPerDay is currently set at "+tempval+". It must be 1 or greater.");
\r
355 if(m_maxpeermessages<20 || m_maxpeermessages>1000)
\r
357 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxPeerMessagesPerDay is currently set at "+tempval+". This value might be incorrectly configured. The suggested value is 200.");
\r
359 Option::Instance()->Get("MaxBoardsPerMessage",tempval);
\r
360 StringFunctions::Convert(tempval,m_maxboardspermessage);
\r
361 if(m_maxboardspermessage<1)
\r
363 m_maxboardspermessage=1;
\r
364 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"Option MaxBoardsPerMessage is currently set at "+tempval+". It must be 1 or greater.");
\r
366 if(m_maxboardspermessage>20)
\r
368 m_log->WriteLog(LogFile::LOGLEVEL_WARNING,"Option MaxBoardsPerMessage is currently set at "+tempval+". This value might be incorrectly configured.");
\r
371 Option::Instance()->Get("SaveMessagesFromNewBoards",tempval);
\r
372 if(tempval=="true")
\r
374 m_savemessagesfromnewboards=true;
\r
378 m_savemessagesfromnewboards=false;
\r
381 Option::Instance()->Get("LocalTrustOverridesPeerTrust",tempval);
\r
382 if(tempval=="true")
\r
384 m_localtrustoverrides=true;
\r
388 m_localtrustoverrides=false;
\r
393 void MessageRequester::PopulateIDList()
\r
402 date.SetToGMTime();
\r
403 date.Add(0,0,0,-m_maxdaysbackward);
\r
405 sql="SELECT tblIdentity.IdentityID,Day,RequestIndex ";
\r
406 sql+="FROM tblMessageRequests INNER JOIN tblIdentity ON tblMessageRequests.IdentityID=tblIdentity.IdentityID ";
\r
407 sql+="WHERE FromMessageList='true' AND Found='false' AND Day>='"+date.Format("%Y-%m-%d")+"' ";
\r
408 if(m_localtrustoverrides==false)
\r
410 sql+="AND (tblIdentity.LocalMessageTrust IS NULL OR tblIdentity.LocalMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalMessageTrust')) ";
\r
411 sql+="AND (tblIdentity.PeerMessageTrust IS NULL OR tblIdentity.PeerMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinPeerMessageTrust')) ";
\r
415 sql+="AND (tblIdentity.LocalMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalMessageTrust') OR (tblIdentity.LocalMessageTrust IS NULL AND (tblIdentity.PeerMessageTrust IS NULL OR tblIdentity.PeerMessageTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinPeerMessageTrust')))) ";
\r
417 sql+="AND tblIdentity.Name <> '' ";
\r
418 // sort by day descending - in case there is a bunch of messages on a day that keep timing out, we will eventually get to the next day and hopefully find messages there
\r
419 sql+="ORDER BY tblMessageRequests.Day DESC ";
\r
422 SQLite3DB::Statement st=m_db->Prepare(sql);
\r
427 while(st.RowReturned())
\r
429 st.ResultText(0,val1);
\r
430 st.ResultText(1,val2);
\r
431 st.ResultText(2,val3);
\r
434 StringFunctions::Convert(val3,requestindex);
\r
436 // only continue if index is < max messages we will accept from a peer
\r
437 if(requestindex<m_maxpeermessages)
\r
439 if(m_ids.find(val1+"*"+val2+"*"+val3)==m_ids.end())
\r
441 m_ids[val1+"*"+val2+"*"+val3]=false;
\r
449 const bool MessageRequester::SaveToBoard(const std::string &boardname)
\r
452 SQLite3DB::Statement st=m_db->Prepare("SELECT SaveReceivedMessages FROM tblBoard WHERE BoardName=?;");
\r
453 st.Bind(0,boardname);
\r
455 if(st.RowReturned())
\r
457 std::string val="";
\r
458 st.ResultText(0,val);
\r
471 void MessageRequester::StartRequest(const std::string &requestid)
\r
473 FCPMessage message;
\r
474 std::vector<std::string> parts;
\r
475 std::string tempval;
\r
478 std::string indexstr;
\r
479 std::string publickey;
\r
481 StringFunctions::Split(requestid,"*",parts);
\r
482 StringFunctions::Convert(parts[0],identityid);
\r
483 StringFunctions::Convert(parts[1],date);
\r
486 SQLite3DB::Statement st=m_db->Prepare("SELECT PublicKey FROM tblIdentity WHERE IdentityID=?;");
\r
487 st.Bind(0,identityid);
\r
490 if(st.RowReturned())
\r
492 st.ResultText(0,publickey);
\r
494 message.SetName("ClientGet");
\r
495 message["URI"]=publickey+m_messagebase+"|"+date+"|Message|"+indexstr+".xml";
\r
496 message["Identifier"]=m_fcpuniquename+"|"+requestid+"|"+parts[0]+"|"+parts[1]+"|"+parts[2]+"|"+message["URI"];
\r
497 message["ReturnType"]="direct";
\r
498 message["MaxSize"]="1000000"; // 1 MB
\r
499 message["MaxRetries"]="-1"; // use ULPR since we are fairly sure message exists since the author says it does
\r
501 m_fcp->SendMessage(message);
\r
503 m_requesting.push_back(requestid);
\r
505 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageRequester::StartRequest requesting "+message["Identifier"]);
\r
508 m_ids[requestid]=true;
\r