Fix WinSock library version detection with MinGW-w64
[amule.git] / src / SearchList.cpp
blob32b28e9df88cbbdb24583fe9464d057b5a29124f
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "SearchList.h" // Interface declarations.
28 #include <protocol/Protocols.h>
29 #include <protocol/kad/Constants.h>
30 #include <tags/ClientTags.h>
31 #include <tags/FileTags.h>
33 #include "updownclient.h" // Needed for CUpDownClient
34 #include "MemFile.h" // Needed for CMemFile
35 #include "amule.h" // Needed for theApp
36 #include "ServerConnect.h" // Needed for theApp->serverconnect
37 #include "Server.h" // Needed for CServer
38 #include "ServerList.h" // Needed for theApp->serverlist
39 #include "Statistics.h" // Needed for theStats
40 #include "ObservableQueue.h"// Needed for CQueueObserver
41 #include <common/Format.h>
42 #include "Logger.h" // Needed for AddLogLineM/...
43 #include "Packet.h" // Needed for CPacket
44 #include "GuiEvents.h" // Needed for Notify_*
47 #ifndef AMULE_DAEMON
48 #include "amuleDlg.h" // Needed for CamuleDlg
49 #include "SearchDlg.h" // Needed for CSearchDlg
50 #endif
52 #include "kademlia/kademlia/Kademlia.h"
53 #include "kademlia/kademlia/Search.h"
55 #include "SearchExpr.h"
56 #include "Scanner.h.in"
59 extern int yyparse();
60 extern int yyerror(const char* errstr);
61 extern int yyerror(wxString errstr);
63 static wxString s_strCurKadKeyword;
65 static CSearchExpr _SearchExpr;
67 wxArrayString _astrParserErrors;
70 // Helper function for lexer.
71 void ParsedSearchExpression(const CSearchExpr* pexpr)
73 int iOpAnd = 0;
74 int iOpOr = 0;
75 int iOpNot = 0;
77 for (unsigned int i = 0; i < pexpr->m_aExpr.GetCount(); i++) {
78 wxString str(pexpr->m_aExpr[i]);
79 if (str == SEARCHOPTOK_AND) {
80 iOpAnd++;
81 } else if (str == SEARCHOPTOK_OR) {
82 iOpOr++;
83 } else if (str == SEARCHOPTOK_NOT) {
84 iOpNot++;
88 // this limit (+ the additional operators which will be added later) has to match the limit in 'CreateSearchExpressionTree'
89 // +1 Type (Audio, Video)
90 // +1 MinSize
91 // +1 MaxSize
92 // +1 Avail
93 // +1 Extension
94 // +1 Complete sources
95 // +1 Codec
96 // +1 Bitrate
97 // +1 Length
98 // +1 Title
99 // +1 Album
100 // +1 Artist
101 // ---------------
102 // 12
103 if (iOpAnd + iOpOr + iOpNot > 10) {
104 yyerror(wxT("Search expression is too complex"));
107 _SearchExpr.m_aExpr.Empty();
109 // optimize search expression, if no OR nor NOT specified
110 if (iOpAnd > 0 && iOpOr == 0 && iOpNot == 0) {
111 wxString strAndTerms;
112 for (unsigned int i = 0; i < pexpr->m_aExpr.GetCount(); i++) {
113 if (pexpr->m_aExpr[i] != SEARCHOPTOK_AND) {
114 // Minor optimization: Because we added the Kad keyword to the boolean search expression,
115 // we remove it here (and only here) again because we know that the entire search expression
116 // does only contain (implicit) ANDed strings.
117 if (pexpr->m_aExpr[i] != s_strCurKadKeyword) {
118 if (!strAndTerms.IsEmpty()) {
119 strAndTerms += ' ';
121 strAndTerms += pexpr->m_aExpr[i];
125 wxASSERT( _SearchExpr.m_aExpr.GetCount() == 0);
126 _SearchExpr.m_aExpr.Add(strAndTerms);
127 } else {
128 if (pexpr->m_aExpr.GetCount() != 1 || pexpr->m_aExpr[0] != s_strCurKadKeyword)
129 _SearchExpr.Add(pexpr);
134 //! Helper class for packet creation
135 class CSearchExprTarget
137 public:
138 CSearchExprTarget(CMemFile* pData, EUtf8Str eStrEncode, bool supports64bit, bool& using64bit)
139 : m_data(pData),
140 m_eStrEncode(eStrEncode),
141 m_supports64bit(supports64bit),
142 m_using64bit(using64bit)
144 m_using64bit = false;
147 void WriteBooleanAND()
149 m_data->WriteUInt8(0); // boolean operator parameter type
150 m_data->WriteUInt8(0x00); // "AND"
153 void WriteBooleanOR()
155 m_data->WriteUInt8(0); // boolean operator parameter type
156 m_data->WriteUInt8(0x01); // "OR"
159 void WriteBooleanNOT()
161 m_data->WriteUInt8(0); // boolean operator parameter type
162 m_data->WriteUInt8(0x02); // "NOT"
165 void WriteMetaDataSearchParam(const wxString& rstrValue)
167 m_data->WriteUInt8(1); // string parameter type
168 m_data->WriteString(rstrValue, m_eStrEncode); // string value
171 void WriteMetaDataSearchParam(uint8 uMetaTagID, const wxString& rstrValue)
173 m_data->WriteUInt8(2); // string parameter type
174 m_data->WriteString(rstrValue, m_eStrEncode); // string value
175 m_data->WriteUInt16(sizeof(uint8)); // meta tag ID length
176 m_data->WriteUInt8(uMetaTagID); // meta tag ID name
179 void WriteMetaDataSearchParamASCII(uint8 uMetaTagID, const wxString& rstrValue)
181 m_data->WriteUInt8(2); // string parameter type
182 m_data->WriteString(rstrValue, utf8strNone); // string value
183 m_data->WriteUInt16(sizeof(uint8)); // meta tag ID length
184 m_data->WriteUInt8(uMetaTagID); // meta tag ID name
187 void WriteMetaDataSearchParam(const wxString& pszMetaTagID, const wxString& rstrValue)
189 m_data->WriteUInt8(2); // string parameter type
190 m_data->WriteString(rstrValue, m_eStrEncode); // string value
191 m_data->WriteString(pszMetaTagID); // meta tag ID
194 void WriteMetaDataSearchParam(uint8_t uMetaTagID, uint8_t uOperator, uint64_t value)
196 bool largeValue = value > wxULL(0xFFFFFFFF);
197 if (largeValue && m_supports64bit) {
198 m_using64bit = true;
199 m_data->WriteUInt8(8); // numeric parameter type (int64)
200 m_data->WriteUInt64(value); // numeric value
201 } else {
202 if (largeValue) {
203 value = 0xFFFFFFFFu;
205 m_data->WriteUInt8(3); // numeric parameter type (int32)
206 m_data->WriteUInt32(value); // numeric value
208 m_data->WriteUInt8(uOperator); // comparison operator
209 m_data->WriteUInt16(sizeof(uint8)); // meta tag ID length
210 m_data->WriteUInt8(uMetaTagID); // meta tag ID name
213 void WriteMetaDataSearchParam(const wxString& pszMetaTagID, uint8_t uOperator, uint64_t value)
215 bool largeValue = value > wxULL(0xFFFFFFFF);
216 if (largeValue && m_supports64bit) {
217 m_using64bit = true;
218 m_data->WriteUInt8(8); // numeric parameter type (int64)
219 m_data->WriteUInt64(value); // numeric value
220 } else {
221 if (largeValue) {
222 value = 0xFFFFFFFFu;
224 m_data->WriteUInt8(3); // numeric parameter type (int32)
225 m_data->WriteUInt32(value); // numeric value
227 m_data->WriteUInt8(uOperator); // comparison operator
228 m_data->WriteString(pszMetaTagID); // meta tag ID
231 protected:
232 CMemFile* m_data;
233 EUtf8Str m_eStrEncode;
234 bool m_supports64bit;
235 bool& m_using64bit;
241 ///////////////////////////////////////////////////////////
242 // CSearchList
244 BEGIN_EVENT_TABLE(CSearchList, wxEvtHandler)
245 EVT_MULE_TIMER(wxID_ANY, CSearchList::OnGlobalSearchTimer)
246 END_EVENT_TABLE()
249 CSearchList::CSearchList()
250 : m_searchTimer(this, 0 /* Timer-id doesn't matter. */ ),
251 m_searchType(LocalSearch),
252 m_searchInProgress(false),
253 m_currentSearch(-1),
254 m_searchPacket(NULL),
255 m_64bitSearchPacket(false),
256 m_KadSearchFinished(true)
260 CSearchList::~CSearchList()
262 StopSearch();
264 while (!m_results.empty()) {
265 RemoveResults(m_results.begin()->first);
270 void CSearchList::RemoveResults(long searchID)
272 // A non-existant search id will just be ignored
273 Kademlia::CSearchManager::StopSearch(searchID, true);
275 ResultMap::iterator it = m_results.find(searchID);
276 if ( it != m_results.end() ) {
277 CSearchResultList& list = it->second;
279 for (size_t i = 0; i < list.size(); ++i) {
280 delete list.at(i);
283 m_results.erase( it );
288 wxString CSearchList::StartNewSearch(uint32* searchID, SearchType type, const CSearchParams& params)
290 // Check that we can actually perform the specified desired search.
291 if ((type == KadSearch) && !Kademlia::CKademlia::IsRunning()) {
292 return _("Kad search can't be done if Kad is not running");
293 } else if ((type != KadSearch) && !theApp->IsConnectedED2K()) {
294 return _("eD2k search can't be done if eD2k is not connected");
297 if (params.typeText != ED2KFTSTR_PROGRAM) {
298 if (params.typeText.CmpNoCase(wxT("Any"))) {
299 m_resultType = params.typeText;
300 } else {
301 m_resultType.Clear();
303 } else {
304 // No check is to be made on returned results if the
305 // type is 'Programs', since this returns multiple types.
306 m_resultType.Clear();
309 bool supports64bit = type == KadSearch ? true : theApp->serverconnect->GetCurrentServer() != NULL && (theApp->serverconnect->GetCurrentServer()->GetTCPFlags() & SRV_TCPFLG_LARGEFILES);
310 bool packetUsing64bit;
312 // This MemFile is automatically free'd
313 CMemFilePtr data = CreateSearchData(params, type, supports64bit, packetUsing64bit);
315 if (data.get() == NULL) {
316 wxASSERT(_astrParserErrors.GetCount());
317 wxString error;
319 for (unsigned int i = 0; i < _astrParserErrors.GetCount(); ++i) {
320 error += _astrParserErrors[i] + wxT("\n");
323 return error;
326 m_searchType = type;
327 if (type == KadSearch) {
328 try {
329 if (*searchID == 0xffffffff) {
330 Kademlia::CSearchManager::StopSearch(0xffffffff, false);
333 // searchstring will get tokenized there
334 // The tab must be created with the Kad search ID, so searchID is updated.
335 Kademlia::CSearch* search = Kademlia::CSearchManager::PrepareFindKeywords(
336 params.searchString, data->GetLength(), data->GetRawBuffer(), *searchID);
338 *searchID = search->GetSearchID();
339 m_currentSearch = *searchID;
340 m_KadSearchFinished = false;
341 } catch (const wxString& what) {
342 AddLogLineC(what);
343 return _("Unexpected error while attempting Kad search: ") + what;
345 } else {
346 // This is an ed2k search, local or global
347 m_currentSearch = *(searchID);
348 m_searchInProgress = true;
350 CPacket* searchPacket = new CPacket(*data.get(), OP_EDONKEYPROT, OP_SEARCHREQUEST);
352 theStats::AddUpOverheadServer(searchPacket->GetPacketSize());
353 theApp->serverconnect->SendPacket(searchPacket, (type == LocalSearch));
355 if (type == GlobalSearch) {
356 delete m_searchPacket;
357 m_searchPacket = searchPacket;
358 m_64bitSearchPacket = packetUsing64bit;
359 m_searchPacket->SetOpCode(OP_GLOBSEARCHREQ); // will be changed later when actually sending the packet!!
363 return wxEmptyString;
367 void CSearchList::LocalSearchEnd()
369 if (m_searchType == GlobalSearch) {
370 wxCHECK_RET(m_searchPacket, wxT("Global search, but no packet"));
372 // Ensure that every global search starts over.
373 theApp->serverlist->RemoveObserver(&m_serverQueue);
374 m_searchTimer.Start(750);
375 } else {
376 m_searchInProgress = false;
377 Notify_SearchLocalEnd();
382 uint32 CSearchList::GetSearchProgress() const
384 if (m_searchType == KadSearch) {
385 // We cannot measure the progress of Kad searches.
386 // But we can tell when they are over.
387 return m_KadSearchFinished ? 0xfffe : 0;
389 if (m_searchInProgress == false) { // true only for ED2K search
390 // No search, no progress ;)
391 return 0;
394 switch (m_searchType) {
395 case LocalSearch:
396 return 0xffff;
398 case GlobalSearch:
399 return 100 - (m_serverQueue.GetRemaining() * 100)
400 / theApp->serverlist->GetServerCount();
402 default:
403 wxFAIL;
405 return 0;
409 void CSearchList::OnGlobalSearchTimer(CTimerEvent& WXUNUSED(evt))
411 // Ensure that the server-queue contains the current servers.
412 if (m_searchPacket == NULL) {
413 // This was a pending event, handled after 'Stop' was pressed.
414 return;
415 } else if (!m_serverQueue.IsActive()) {
416 theApp->serverlist->AddObserver(&m_serverQueue);
419 // UDP requests must not be sent to this server.
420 const CServer* localServer = theApp->serverconnect->GetCurrentServer();
421 if (localServer) {
422 uint32 localIP = localServer->GetIP();
423 uint16 localPort = localServer->GetPort();
424 while (m_serverQueue.GetRemaining()) {
425 CServer* server = m_serverQueue.GetNext();
427 // Compare against the currently connected server.
428 if ((server->GetPort() == localPort) && (server->GetIP() == localIP)) {
429 // We've already requested from the local server.
430 continue;
431 } else {
432 if (server->SupportsLargeFilesUDP() && (server->GetUDPFlags() & SRV_UDPFLG_EXT_GETFILES)) {
433 CMemFile data(50);
434 uint32_t tagCount = 1;
435 data.WriteUInt32(tagCount);
436 CTagVarInt flags(CT_SERVER_UDPSEARCH_FLAGS, SRVCAP_UDP_NEWTAGS_LARGEFILES);
437 flags.WriteNewEd2kTag(&data);
438 CPacket *extSearchPacket = new CPacket(OP_GLOBSEARCHREQ3, m_searchPacket->GetPacketSize() + (uint32_t)data.GetLength(), OP_EDONKEYPROT);
439 extSearchPacket->CopyToDataBuffer(0, data.GetRawBuffer(), data.GetLength());
440 extSearchPacket->CopyToDataBuffer(data.GetLength(), m_searchPacket->GetDataBuffer(), m_searchPacket->GetPacketSize());
441 theStats::AddUpOverheadServer(extSearchPacket->GetPacketSize());
442 theApp->serverconnect->SendUDPPacket(extSearchPacket, server, true);
443 AddDebugLogLineN(logServerUDP, wxT("Sending OP_GLOBSEARCHREQ3 to server ") + Uint32_16toStringIP_Port(server->GetIP(), server->GetPort()));
444 } else if (server->GetUDPFlags() & SRV_UDPFLG_EXT_GETFILES) {
445 if (!m_64bitSearchPacket || server->SupportsLargeFilesUDP()) {
446 m_searchPacket->SetOpCode(OP_GLOBSEARCHREQ2);
447 AddDebugLogLineN(logServerUDP, wxT("Sending OP_GLOBSEARCHREQ2 to server ") + Uint32_16toStringIP_Port(server->GetIP(), server->GetPort()));
448 theStats::AddUpOverheadServer(m_searchPacket->GetPacketSize());
449 theApp->serverconnect->SendUDPPacket(m_searchPacket, server, false);
450 } else {
451 AddDebugLogLineN(logServerUDP, wxT("Skipped UDP search on server ") + Uint32_16toStringIP_Port(server->GetIP(), server->GetPort()) + wxT(": No large file support"));
453 } else {
454 if (!m_64bitSearchPacket || server->SupportsLargeFilesUDP()) {
455 m_searchPacket->SetOpCode(OP_GLOBSEARCHREQ);
456 AddDebugLogLineN(logServerUDP, wxT("Sending OP_GLOBSEARCHREQ to server ") + Uint32_16toStringIP_Port(server->GetIP(), server->GetPort()));
457 theStats::AddUpOverheadServer(m_searchPacket->GetPacketSize());
458 theApp->serverconnect->SendUDPPacket(m_searchPacket, server, false);
459 } else {
460 AddDebugLogLineN(logServerUDP, wxT("Skipped UDP search on server ") + Uint32_16toStringIP_Port(server->GetIP(), server->GetPort()) + wxT(": No large file support"));
463 CoreNotify_Search_Update_Progress(GetSearchProgress());
464 return;
468 // No more servers left to ask.
469 StopSearch(true);
473 void CSearchList::ProcessSharedFileList(const byte* in_packet, uint32 size,
474 CUpDownClient* sender, bool *moreResultsAvailable, const wxString& directory)
476 wxCHECK_RET(sender, wxT("No sender in search-results from client."));
478 long searchID = reinterpret_cast<wxUIntPtr>(sender);
480 #ifndef AMULE_DAEMON
481 if (!theApp->amuledlg->m_searchwnd->CheckTabNameExists(sender->GetUserName())) {
482 theApp->amuledlg->m_searchwnd->CreateNewTab(sender->GetUserName() + wxT(" (0)"), searchID);
484 #endif
486 const CMemFile packet(in_packet, size);
487 uint32 results = packet.ReadUInt32();
488 bool unicoded = (sender->GetUnicodeSupport() != utf8strNone);
489 for (unsigned int i = 0; i != results; ++i){
490 CSearchFile* toadd = new CSearchFile(packet, unicoded, searchID, 0, 0, directory);
491 toadd->SetClientID(sender->GetUserIDHybrid());
492 toadd->SetClientPort(sender->GetUserPort());
493 AddToList(toadd, true);
496 if (moreResultsAvailable)
497 *moreResultsAvailable = false;
499 int iAddData = (int)(packet.GetLength() - packet.GetPosition());
500 if (iAddData == 1) {
501 uint8 ucMore = packet.ReadUInt8();
502 if (ucMore == 0x00 || ucMore == 0x01){
503 if (moreResultsAvailable) {
504 *moreResultsAvailable = (ucMore == 1);
511 void CSearchList::ProcessSearchAnswer(const uint8_t* in_packet, uint32_t size, bool optUTF8, uint32_t serverIP, uint16_t serverPort)
513 CMemFile packet(in_packet, size);
515 uint32_t results = packet.ReadUInt32();
516 for (; results > 0; --results) {
517 AddToList(new CSearchFile(packet, optUTF8, m_currentSearch, serverIP, serverPort), false);
522 void CSearchList::ProcessUDPSearchAnswer(const CMemFile& packet, bool optUTF8, uint32_t serverIP, uint16_t serverPort)
524 AddToList(new CSearchFile(packet, optUTF8, m_currentSearch, serverIP, serverPort), false);
528 bool CSearchList::AddToList(CSearchFile* toadd, bool clientResponse)
530 const uint64 fileSize = toadd->GetFileSize();
531 // If filesize is 0, or file is too large for the network, drop it
532 if ((fileSize == 0) || (fileSize > MAX_FILE_SIZE)) {
533 AddDebugLogLineN(logSearch,
534 CFormat(wxT("Dropped result with filesize %u: %s"))
535 % fileSize
536 % toadd->GetFileName());
538 delete toadd;
539 return false;
542 // If the result was not the type the user wanted, drop it.
543 if ((clientResponse == false) && !m_resultType.IsEmpty()) {
544 if (GetFileTypeByName(toadd->GetFileName()) != m_resultType) {
545 AddDebugLogLineN(logSearch,
546 CFormat( wxT("Dropped result type %s != %s, file %s") )
547 % GetFileTypeByName(toadd->GetFileName())
548 % m_resultType
549 % toadd->GetFileName());
551 delete toadd;
552 return false;
557 // Get, or implictly create, the map of results for this search
558 CSearchResultList& results = m_results[toadd->GetSearchID()];
560 for (size_t i = 0; i < results.size(); ++i) {
561 CSearchFile* item = results.at(i);
563 if ((toadd->GetFileHash() == item->GetFileHash()) && (toadd->GetFileSize() == item->GetFileSize())) {
564 AddDebugLogLineN(logSearch, CFormat(wxT("Received duplicate results for '%s' : %s")) % item->GetFileName() % item->GetFileHash().Encode());
565 // Add the child, possibly updating the parents filename.
566 item->AddChild(toadd);
567 Notify_Search_Update_Sources(item);
568 return true;
572 AddDebugLogLineN(logSearch,
573 CFormat(wxT("Added new result '%s' : %s"))
574 % toadd->GetFileName() % toadd->GetFileHash().Encode());
576 // New unique result, simply add and display.
577 results.push_back(toadd);
578 Notify_Search_Add_Result(toadd);
580 return true;
584 const CSearchResultList& CSearchList::GetSearchResults(long searchID) const
586 ResultMap::const_iterator it = m_results.find(searchID);
587 if (it != m_results.end()) {
588 return it->second;
591 // TODO: Should we assert in this case?
592 static CSearchResultList list;
593 return list;
597 void CSearchList::AddFileToDownloadByHash(const CMD4Hash& hash, uint8 cat)
599 ResultMap::iterator it = m_results.begin();
600 for ( ; it != m_results.end(); ++it ) {
601 CSearchResultList& list = it->second;
603 for ( unsigned int i = 0; i < list.size(); ++i ) {
604 if ( list[i]->GetFileHash() == hash ) {
605 CoreNotify_Search_Add_Download( list[i], cat );
607 return;
614 void CSearchList::StopSearch(bool globalOnly)
616 if (m_searchType == GlobalSearch) {
617 m_currentSearch = -1;
618 delete m_searchPacket;
619 m_searchPacket = NULL;
620 m_searchInProgress = false;
622 // Order is crucial here: on wx_MSW an additional event can be generated during the stop.
623 // So the packet has to be deleted first, so that OnGlobalSearchTimer() returns immediately
624 // without calling StopGlobalSearch() again.
625 m_searchTimer.Stop();
627 CoreNotify_Search_Update_Progress(0xffff);
628 } else if (m_searchType == KadSearch && !globalOnly) {
629 Kademlia::CSearchManager::StopSearch(m_currentSearch, false);
630 m_currentSearch = -1;
635 CSearchList::CMemFilePtr CSearchList::CreateSearchData(const CSearchParams& params, SearchType WXUNUSED(type), bool supports64bit, bool& packetUsing64bit)
637 // Count the number of used parameters
638 unsigned int parametercount = 0;
639 if ( !params.typeText.IsEmpty() ) ++parametercount;
640 if ( params.minSize > 0 ) ++parametercount;
641 if ( params.maxSize > 0 ) ++parametercount;
642 if ( params.availability > 0 ) ++parametercount;
643 if ( !params.extension.IsEmpty() ) ++parametercount;
645 wxString typeText = params.typeText;
646 if (typeText == ED2KFTSTR_ARCHIVE){
647 // eDonkeyHybrid 0.48 uses type "Pro" for archives files
648 // www.filedonkey.com uses type "Pro" for archives files
649 typeText = ED2KFTSTR_PROGRAM;
650 } else if (typeText == ED2KFTSTR_CDIMAGE){
651 // eDonkeyHybrid 0.48 uses *no* type for iso/nrg/cue/img files
652 // www.filedonkey.com uses type "Pro" for CD-image files
653 typeText = ED2KFTSTR_PROGRAM;
656 // Must write parametercount - 1 parameter headers
657 CMemFilePtr data(new CMemFile(100));
659 _astrParserErrors.Empty();
660 _SearchExpr.m_aExpr.Empty();
662 LexInit(params.searchString);
663 int iParseResult = yyparse();
664 LexFree();
666 #ifdef __DEBUG__
667 AddLogLineNS(CFormat(wxT("Search parsing result for \"%s\": %i"))
668 % params.searchString % iParseResult);
669 #endif
670 if (_astrParserErrors.GetCount() > 0) {
671 for (unsigned int i=0; i < _astrParserErrors.GetCount(); ++i) {
672 AddLogLineNS(CFormat(wxT("Error %u: %s\n")) % i % _astrParserErrors[i]);
675 return CMemFilePtr(NULL);
678 if (iParseResult != 0) {
679 _astrParserErrors.Add(CFormat(wxT("Undefined error %i on search expression")) % iParseResult);
681 return CMemFilePtr(NULL);
684 #ifdef __DEBUG__
685 wxString mes(wxT("Search expression:"));
686 for (unsigned int i = 0; i < _SearchExpr.m_aExpr.GetCount(); i++) {
687 mes << wxT(" ") << _SearchExpr.m_aExpr[i];
689 AddLogLineNS(mes);
690 AddLogLineNS(CFormat(wxT("Expression count: %i")) % _SearchExpr.m_aExpr.GetCount());
691 #endif
693 parametercount += _SearchExpr.m_aExpr.GetCount();
695 #ifdef __DEBUG__
696 AddLogLineNS(CFormat(wxT("Parameters: %i")) % parametercount);
697 #endif
699 /* Leave the unicode comment there, please... */
700 CSearchExprTarget target(data.get(), true /*I assume everyone is unicoded */ ? utf8strRaw : utf8strNone, supports64bit, packetUsing64bit);
702 unsigned int iParameterCount = 0;
703 if (_SearchExpr.m_aExpr.GetCount() <= 1) {
704 // lugdunummaster requested that searchs without OR or NOT operators,
705 // and hence with no more expressions than the string itself, be sent
706 // using a series of ANDed terms, intersecting the ANDs on the terms
707 // (but prepending them) instead of putting the boolean tree at the start
708 // like other searches. This type of search is supposed to take less load
709 // on servers. Go figure.
711 // input: "a" AND min=1 AND max=2
712 // instead of: AND AND "a" min=1 max=2
713 // we use: AND "a" AND min=1 max=2
715 if (_SearchExpr.m_aExpr.GetCount() > 0) {
716 if (++iParameterCount < parametercount) {
717 target.WriteBooleanAND();
719 target.WriteMetaDataSearchParam(_SearchExpr.m_aExpr[0]);
722 if (!typeText.IsEmpty()) {
723 if (++iParameterCount < parametercount) {
724 target.WriteBooleanAND();
726 // Type is always ascii string
727 target.WriteMetaDataSearchParamASCII(FT_FILETYPE, typeText);
730 if (params.minSize > 0) {
731 if (++iParameterCount < parametercount) {
732 target.WriteBooleanAND();
734 target.WriteMetaDataSearchParam(FT_FILESIZE, ED2K_SEARCH_OP_GREATER, params.minSize);
737 if (params.maxSize > 0){
738 if (++iParameterCount < parametercount) {
739 target.WriteBooleanAND();
741 target.WriteMetaDataSearchParam(FT_FILESIZE, ED2K_SEARCH_OP_LESS, params.maxSize);
744 if (params.availability > 0){
745 if (++iParameterCount < parametercount) {
746 target.WriteBooleanAND();
748 target.WriteMetaDataSearchParam(FT_SOURCES, ED2K_SEARCH_OP_GREATER, params.availability);
751 if (!params.extension.IsEmpty()){
752 if (++iParameterCount < parametercount) {
753 target.WriteBooleanAND();
755 target.WriteMetaDataSearchParam(FT_FILEFORMAT, params.extension);
758 //#warning TODO - I keep this here, ready if we ever allow such searches...
759 #if 0
760 if (complete > 0){
761 if (++iParameterCount < parametercount) {
762 target.WriteBooleanAND();
764 target.WriteMetaDataSearchParam(FT_COMPLETE_SOURCES, ED2K_SEARCH_OP_GREATER, complete);
767 if (minBitrate > 0){
768 if (++iParameterCount < parametercount) {
769 target.WriteBooleanAND();
771 target.WriteMetaDataSearchParam(type == KadSearch ? TAG_MEDIA_BITRATE : FT_ED2K_MEDIA_BITRATE, ED2K_SEARCH_OP_GREATER, minBitrate);
774 if (minLength > 0){
775 if (++iParameterCount < parametercount) {
776 target.WriteBooleanAND();
778 target.WriteMetaDataSearchParam(type == KadSearch ? TAG_MEDIA_LENGTH : FT_ED2K_MEDIA_LENGTH, ED2K_SEARCH_OP_GREATER, minLength);
781 if (!codec.IsEmpty()){
782 if (++iParameterCount < parametercount) {
783 target.WriteBooleanAND();
785 target.WriteMetaDataSearchParam(type == KadSearch ? TAG_MEDIA_CODEC : FT_ED2K_MEDIA_CODEC, codec);
788 if (!title.IsEmpty()){
789 if (++iParameterCount < parametercount) {
790 target.WriteBooleanAND();
792 target.WriteMetaDataSearchParam(type == KadSearch ? TAG_MEDIA_TITLE : FT_ED2K_MEDIA_TITLE, title);
795 if (!album.IsEmpty()){
796 if (++iParameterCount < parametercount) {
797 target.WriteBooleanAND();
799 target.WriteMetaDataSearchParam(type == KadSearch ? TAG_MEDIA_ALBUM : FT_ED2K_MEDIA_ALBUM, album);
802 if (!artist.IsEmpty()){
803 if (++iParameterCount < parametercount) {
804 target.WriteBooleanAND();
806 target.WriteMetaDataSearchParam(type == KadSearch ? TAG_MEDIA_ARTIST : FT_ED2K_MEDIA_ARTIST, artist);
808 #endif // 0
810 // If this assert fails... we're seriously fucked up
812 wxASSERT( iParameterCount == parametercount );
814 } else {
815 if (!params.extension.IsEmpty()) {
816 if (++iParameterCount < parametercount) {
817 target.WriteBooleanAND();
821 if (params.availability > 0) {
822 if (++iParameterCount < parametercount) {
823 target.WriteBooleanAND();
827 if (params.maxSize > 0){
828 if (++iParameterCount < parametercount) {
829 target.WriteBooleanAND();
833 if (params.minSize > 0) {
834 if (++iParameterCount < parametercount) {
835 target.WriteBooleanAND();
839 if (!typeText.IsEmpty()){
840 if (++iParameterCount < parametercount) {
841 target.WriteBooleanAND();
845 //#warning TODO - same as above...
846 #if 0
847 if (complete > 0){
848 if (++iParameterCount < parametercount) {
849 target.WriteBooleanAND();
853 if (minBitrate > 0){
854 if (++iParameterCount < parametercount) {
855 target.WriteBooleanAND();
859 if (minLength > 0) {
860 if (++iParameterCount < parametercount) {
861 target.WriteBooleanAND();
865 if (!codec.IsEmpty()){
866 if (++iParameterCount < parametercount) {
867 target.WriteBooleanAND();
871 if (!title.IsEmpty()){
872 if (++iParameterCount < parametercount) {
873 target.WriteBooleanAND();
877 if (!album.IsEmpty()) {
878 if (++iParameterCount < parametercount) {
879 target.WriteBooleanAND();
883 if (!artist.IsEmpty()) {
884 if (++iParameterCount < parametercount) {
885 target.WriteBooleanAND();
888 #endif // 0
890 // As above, if this fails, we're seriously fucked up.
891 wxASSERT( iParameterCount + _SearchExpr.m_aExpr.GetCount() == parametercount );
893 for (unsigned int j = 0; j < _SearchExpr.m_aExpr.GetCount(); ++j) {
894 if (_SearchExpr.m_aExpr[j] == SEARCHOPTOK_AND) {
895 target.WriteBooleanAND();
896 } else if (_SearchExpr.m_aExpr[j] == SEARCHOPTOK_OR) {
897 target.WriteBooleanOR();
898 } else if (_SearchExpr.m_aExpr[j] == SEARCHOPTOK_NOT) {
899 target.WriteBooleanNOT();
900 } else {
901 target.WriteMetaDataSearchParam(_SearchExpr.m_aExpr[j]);
905 if (!params.typeText.IsEmpty()) {
906 // Type is always ASCII string
907 target.WriteMetaDataSearchParamASCII(FT_FILETYPE, params.typeText);
910 if (params.minSize > 0) {
911 target.WriteMetaDataSearchParam(FT_FILESIZE, ED2K_SEARCH_OP_GREATER, params.minSize);
914 if (params.maxSize > 0) {
915 target.WriteMetaDataSearchParam(FT_FILESIZE, ED2K_SEARCH_OP_LESS, params.maxSize);
918 if (params.availability > 0) {
919 target.WriteMetaDataSearchParam(FT_SOURCES, ED2K_SEARCH_OP_GREATER, params.availability);
922 if (!params.extension.IsEmpty()) {
923 target.WriteMetaDataSearchParam(FT_FILEFORMAT, params.extension);
926 //#warning TODO - third and last warning of the same series.
927 #if 0
928 if (complete > 0) {
929 target.WriteMetaDataSearchParam(FT_COMPLETE_SOURCES, ED2K_SEARCH_OP_GREATER, pParams->uComplete);
932 if (minBitrate > 0) {
933 target.WriteMetaDataSearchParam(type == KadSearch ? TAG_MEDIA_BITRATE : FT_ED2K_MEDIA_BITRATE, ED2K_SEARCH_OP_GREATER, minBitrate);
936 if (minLength > 0) {
937 target.WriteMetaDataSearchParam(type == KadSearch ? TAG_MEDIA_LENGTH : FT_ED2K_MEDIA_LENGTH, ED2K_SEARCH_OP_GREATER, minLength);
940 if (!codec.IsEmpty()) {
941 target.WriteMetaDataSearchParam(type == KadSearch ? TAG_MEDIA_CODEC : FT_ED2K_MEDIA_CODEC, codec);
944 if (!title.IsEmpty()) {
945 target.WriteMetaDataSearchParam(type == KadSearch ? TAG_MEDIA_TITLE : FT_ED2K_MEDIA_TITLE, title);
948 if (!album.IsEmpty()) {
949 target.WriteMetaDataSearchParam(type == KadSearch ? TAG_MEDIA_ALBUM : FT_ED2K_MEDIA_ALBUM, album);
952 if (!artist.IsEmpty()) {
953 target.WriteMetaDataSearchParam(type == KadSearch ? TAG_MEDIA_ARTIST : FT_ED2K_MEDIA_ARTIST, artist);
956 #endif // 0
959 // Packet ready to go.
960 return data;
964 void CSearchList::KademliaSearchKeyword(uint32_t searchID, const Kademlia::CUInt128 *fileID,
965 const wxString& name, uint64_t size, const wxString& type, uint32_t kadPublishInfo, const TagPtrList& taglist)
967 EUtf8Str eStrEncode = utf8strRaw;
969 CMemFile temp(250);
970 byte fileid[16];
971 fileID->ToByteArray(fileid);
972 temp.WriteHash(CMD4Hash(fileid));
974 temp.WriteUInt32(0); // client IP
975 temp.WriteUInt16(0); // client port
977 // write tag list
978 unsigned int uFilePosTagCount = temp.GetPosition();
979 uint32 tagcount = 0;
980 temp.WriteUInt32(tagcount); // dummy tag count, will be filled later
982 // standard tags
983 CTagString tagName(FT_FILENAME, name);
984 tagName.WriteTagToFile(&temp, eStrEncode);
985 tagcount++;
987 CTagInt64 tagSize(FT_FILESIZE, size);
988 tagSize.WriteTagToFile(&temp, eStrEncode);
989 tagcount++;
991 if (!type.IsEmpty()) {
992 CTagString tagType(FT_FILETYPE, type);
993 tagType.WriteTagToFile(&temp, eStrEncode);
994 tagcount++;
997 // Misc tags (bitrate, etc)
998 for (TagPtrList::const_iterator it = taglist.begin(); it != taglist.end(); ++it) {
999 (*it)->WriteTagToFile(&temp,eStrEncode);
1000 tagcount++;
1003 temp.Seek(uFilePosTagCount, wxFromStart);
1004 temp.WriteUInt32(tagcount);
1006 temp.Seek(0, wxFromStart);
1008 CSearchFile *tempFile = new CSearchFile(temp, (eStrEncode == utf8strRaw), searchID, 0, 0, wxEmptyString, true);
1009 tempFile->SetKadPublishInfo(kadPublishInfo);
1011 AddToList(tempFile);
1014 void CSearchList::UpdateSearchFileByHash(const CMD4Hash& hash)
1016 for (ResultMap::iterator it = m_results.begin(); it != m_results.end(); ++it) {
1017 CSearchResultList& results = it->second;
1018 for (size_t i = 0; i < results.size(); ++i) {
1019 CSearchFile* item = results.at(i);
1021 if (hash == item->GetFileHash()) {
1022 // This covers only parent items,
1023 // child items have to be updated separately.
1024 Notify_Search_Update_Sources(item);
1030 // File_checked_for_headers