2 // This file is part of the aMule Project.
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 )
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
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_*
48 #include "amuleDlg.h" // Needed for CamuleDlg
49 #include "SearchDlg.h" // Needed for CSearchDlg
52 #include "kademlia/kademlia/Kademlia.h"
53 #include "kademlia/kademlia/Search.h"
55 #include "SearchExpr.h"
56 #include "Scanner.h.in"
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
)
77 for (unsigned int i
= 0; i
< pexpr
->m_aExpr
.GetCount(); i
++) {
78 wxString
str(pexpr
->m_aExpr
[i
]);
79 if (str
== SEARCHOPTOK_AND
) {
81 } else if (str
== SEARCHOPTOK_OR
) {
83 } else if (str
== SEARCHOPTOK_NOT
) {
88 // this limit (+ the additional operators which will be added later) has to match the limit in 'CreateSearchExpressionTree'
89 // +1 Type (Audio, Video)
94 // +1 Complete sources
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()) {
121 strAndTerms
+= pexpr
->m_aExpr
[i
];
125 wxASSERT( _SearchExpr
.m_aExpr
.GetCount() == 0);
126 _SearchExpr
.m_aExpr
.Add(strAndTerms
);
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
138 CSearchExprTarget(CMemFile
* pData
, EUtf8Str eStrEncode
, bool supports64bit
, bool& using64bit
)
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
) {
199 m_data
->WriteUInt8(8); // numeric parameter type (int64)
200 m_data
->WriteUInt64(value
); // numeric value
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
) {
218 m_data
->WriteUInt8(8); // numeric parameter type (int64)
219 m_data
->WriteUInt64(value
); // numeric value
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
233 EUtf8Str m_eStrEncode
;
234 bool m_supports64bit
;
241 ///////////////////////////////////////////////////////////
244 BEGIN_EVENT_TABLE(CSearchList
, wxEvtHandler
)
245 EVT_MULE_TIMER(wxID_ANY
, CSearchList::OnGlobalSearchTimer
)
249 CSearchList::CSearchList()
250 : m_searchTimer(this, 0 /* Timer-id doesn't matter. */ ),
251 m_searchType(LocalSearch
),
252 m_searchInProgress(false),
254 m_searchPacket(NULL
),
255 m_64bitSearchPacket(false)
260 CSearchList::~CSearchList()
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
) {
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
;
301 m_resultType
.Clear();
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());
319 for (unsigned int i
= 0; i
< _astrParserErrors
.GetCount(); ++i
) {
320 error
+= _astrParserErrors
[i
] + wxT("\n");
327 if (type
== KadSearch
) {
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
) {
343 return _("Unexpected error while attempting Kad search: ") + what
;
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);
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 ;)
394 switch (m_searchType
) {
399 return 100 - (m_serverQueue
.GetRemaining() * 100)
400 / theApp
->serverlist
->GetServerCount();
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.
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();
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.
432 if (server
->SupportsLargeFilesUDP() && (server
->GetUDPFlags() & SRV_UDPFLG_EXT_GETFILES
)) {
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);
451 AddDebugLogLineN(logServerUDP
, wxT("Skipped UDP search on server ") + Uint32_16toStringIP_Port(server
->GetIP(), server
->GetPort()) + wxT(": No large file support"));
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);
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());
468 // No more servers left to ask.
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
);
481 if (!theApp
->amuledlg
->m_searchwnd
->CheckTabNameExists(sender
->GetUserName())) {
482 theApp
->amuledlg
->m_searchwnd
->CreateNewTab(sender
->GetUserName() + wxT(" (0)"), searchID
);
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
);
492 toadd
->SetClientID(sender
->GetUserIDHybrid());
493 toadd
->SetClientPort(sender
->GetUserPort());
496 AddToList(toadd
, true);
499 if (moreResultsAvailable
)
500 *moreResultsAvailable
= false;
502 int iAddData
= (int)(packet
.GetLength() - packet
.GetPosition());
504 uint8 ucMore
= packet
.ReadUInt8();
505 if (ucMore
== 0x00 || ucMore
== 0x01){
506 if (moreResultsAvailable
) {
507 *moreResultsAvailable
= (ucMore
== 1);
514 void CSearchList::ProcessSearchAnswer(const uint8_t* in_packet
, uint32_t size
, bool optUTF8
, uint32_t serverIP
, uint16_t serverPort
)
516 CMemFile
packet(in_packet
, size
);
518 uint32_t results
= packet
.ReadUInt32();
519 for (; results
> 0; --results
) {
520 AddToList(new CSearchFile(packet
, optUTF8
, m_currentSearch
, serverIP
, serverPort
), false);
525 void CSearchList::ProcessUDPSearchAnswer(const CMemFile
& packet
, bool optUTF8
, uint32_t serverIP
, uint16_t serverPort
)
527 AddToList(new CSearchFile(packet
, optUTF8
, m_currentSearch
, serverIP
, serverPort
), false);
531 bool CSearchList::AddToList(CSearchFile
* toadd
, bool clientResponse
)
533 const uint64 fileSize
= toadd
->GetFileSize();
534 // If filesize is 0, or file is too large for the network, drop it
535 if ((fileSize
== 0) || (fileSize
> MAX_FILE_SIZE
)) {
536 AddDebugLogLineN(logSearch
,
537 CFormat(wxT("Dropped result with filesize %u: %s"))
539 % toadd
->GetFileName());
545 // If the result was not the type the user wanted, drop it.
546 if ((clientResponse
== false) && !m_resultType
.IsEmpty()) {
547 if (GetFileTypeByName(toadd
->GetFileName()) != m_resultType
) {
548 AddDebugLogLineN(logSearch
,
549 CFormat( wxT("Dropped result type %s != %s, file %s") )
550 % GetFileTypeByName(toadd
->GetFileName())
552 % toadd
->GetFileName());
560 // Get, or implictly create, the map of results for this search
561 CSearchResultList
& results
= m_results
[toadd
->GetSearchID()];
563 for (size_t i
= 0; i
< results
.size(); ++i
) {
564 CSearchFile
* item
= results
.at(i
);
566 if ((toadd
->GetFileHash() == item
->GetFileHash()) && (toadd
->GetFileSize() == item
->GetFileSize())) {
567 AddDebugLogLineN(logSearch
, CFormat(wxT("Received duplicate results for '%s' : %s")) % item
->GetFileName() % item
->GetFileHash().Encode());
568 // Add the child, possibly updating the parents filename.
569 item
->AddChild(toadd
);
570 Notify_Search_Update_Sources(item
);
575 AddDebugLogLineN(logSearch
,
576 CFormat(wxT("Added new result '%s' : %s"))
577 % toadd
->GetFileName() % toadd
->GetFileHash().Encode());
579 // New unique result, simply add and display.
580 results
.push_back(toadd
);
581 Notify_Search_Add_Result(toadd
);
587 const CSearchResultList
& CSearchList::GetSearchResults(long searchID
) const
589 ResultMap::const_iterator it
= m_results
.find(searchID
);
590 if (it
!= m_results
.end()) {
594 // TODO: Should we assert in this case?
595 static CSearchResultList list
;
600 void CSearchList::AddFileToDownloadByHash(const CMD4Hash
& hash
, uint8 cat
)
602 ResultMap::iterator it
= m_results
.begin();
603 for ( ; it
!= m_results
.end(); ++it
) {
604 CSearchResultList
& list
= it
->second
;
606 for ( unsigned int i
= 0; i
< list
.size(); ++i
) {
607 if ( list
[i
]->GetFileHash() == hash
) {
608 CoreNotify_Search_Add_Download( list
[i
], cat
);
617 void CSearchList::StopSearch(bool globalOnly
)
619 if (m_searchType
== GlobalSearch
) {
620 m_currentSearch
= -1;
621 delete m_searchPacket
;
622 m_searchPacket
= NULL
;
623 m_searchInProgress
= false;
625 // Order is crucial here: on wx_MSW an additional event can be generated during the stop.
626 // So the packet has to be deleted first, so that OnGlobalSearchTimer() returns immediately
627 // without calling StopGlobalSearch() again.
628 m_searchTimer
.Stop();
630 CoreNotify_Search_Update_Progress(0xffff);
631 } else if (m_searchType
== KadSearch
&& !globalOnly
) {
632 Kademlia::CSearchManager::StopSearch(m_currentSearch
, false);
633 m_currentSearch
= -1;
638 CSearchList::CMemFilePtr
CSearchList::CreateSearchData(const CSearchParams
& params
, SearchType
WXUNUSED(type
), bool supports64bit
, bool& packetUsing64bit
)
640 // Count the number of used parameters
641 unsigned int parametercount
= 0;
642 if ( !params
.typeText
.IsEmpty() ) ++parametercount
;
643 if ( params
.minSize
> 0 ) ++parametercount
;
644 if ( params
.maxSize
> 0 ) ++parametercount
;
645 if ( params
.availability
> 0 ) ++parametercount
;
646 if ( !params
.extension
.IsEmpty() ) ++parametercount
;
648 wxString typeText
= params
.typeText
;
649 if (typeText
== ED2KFTSTR_ARCHIVE
){
650 // eDonkeyHybrid 0.48 uses type "Pro" for archives files
651 // www.filedonkey.com uses type "Pro" for archives files
652 typeText
= ED2KFTSTR_PROGRAM
;
653 } else if (typeText
== ED2KFTSTR_CDIMAGE
){
654 // eDonkeyHybrid 0.48 uses *no* type for iso/nrg/cue/img files
655 // www.filedonkey.com uses type "Pro" for CD-image files
656 typeText
= ED2KFTSTR_PROGRAM
;
659 // Must write parametercount - 1 parameter headers
660 CMemFilePtr
data(new CMemFile(100));
662 _astrParserErrors
.Empty();
663 _SearchExpr
.m_aExpr
.Empty();
665 LexInit(params
.searchString
);
666 int iParseResult
= yyparse();
670 AddLogLineNS(CFormat(wxT("Search parsing result for \"%s\": %i"))
671 % params
.searchString
% iParseResult
);
673 if (_astrParserErrors
.GetCount() > 0) {
674 for (unsigned int i
=0; i
< _astrParserErrors
.GetCount(); ++i
) {
675 AddLogLineNS(CFormat(wxT("Error %u: %s\n")) % i
% _astrParserErrors
[i
]);
678 return CMemFilePtr(NULL
);
681 if (iParseResult
!= 0) {
682 _astrParserErrors
.Add(CFormat(wxT("Undefined error %i on search expression")) % iParseResult
);
684 return CMemFilePtr(NULL
);
688 wxString
mes(wxT("Search expression:"));
689 for (unsigned int i
= 0; i
< _SearchExpr
.m_aExpr
.GetCount(); i
++) {
690 mes
<< wxT(" ") << _SearchExpr
.m_aExpr
[i
];
693 AddLogLineNS(CFormat(wxT("Expression count: %i")) % _SearchExpr
.m_aExpr
.GetCount());
696 parametercount
+= _SearchExpr
.m_aExpr
.GetCount();
699 AddLogLineNS(CFormat(wxT("Parameters: %i")) % parametercount
);
702 /* Leave the unicode comment there, please... */
703 CSearchExprTarget
target(data
.get(), true /*I assume everyone is unicoded */ ? utf8strRaw
: utf8strNone
, supports64bit
, packetUsing64bit
);
705 unsigned int iParameterCount
= 0;
706 if (_SearchExpr
.m_aExpr
.GetCount() <= 1) {
707 // lugdunummaster requested that searchs without OR or NOT operators,
708 // and hence with no more expressions than the string itself, be sent
709 // using a series of ANDed terms, intersecting the ANDs on the terms
710 // (but prepending them) instead of putting the boolean tree at the start
711 // like other searches. This type of search is supposed to take less load
712 // on servers. Go figure.
714 // input: "a" AND min=1 AND max=2
715 // instead of: AND AND "a" min=1 max=2
716 // we use: AND "a" AND min=1 max=2
718 if (_SearchExpr
.m_aExpr
.GetCount() > 0) {
719 if (++iParameterCount
< parametercount
) {
720 target
.WriteBooleanAND();
722 target
.WriteMetaDataSearchParam(_SearchExpr
.m_aExpr
[0]);
725 if (!typeText
.IsEmpty()) {
726 if (++iParameterCount
< parametercount
) {
727 target
.WriteBooleanAND();
729 // Type is always ascii string
730 target
.WriteMetaDataSearchParamASCII(FT_FILETYPE
, typeText
);
733 if (params
.minSize
> 0) {
734 if (++iParameterCount
< parametercount
) {
735 target
.WriteBooleanAND();
737 target
.WriteMetaDataSearchParam(FT_FILESIZE
, ED2K_SEARCH_OP_GREATER
, params
.minSize
);
740 if (params
.maxSize
> 0){
741 if (++iParameterCount
< parametercount
) {
742 target
.WriteBooleanAND();
744 target
.WriteMetaDataSearchParam(FT_FILESIZE
, ED2K_SEARCH_OP_LESS
, params
.maxSize
);
747 if (params
.availability
> 0){
748 if (++iParameterCount
< parametercount
) {
749 target
.WriteBooleanAND();
751 target
.WriteMetaDataSearchParam(FT_SOURCES
, ED2K_SEARCH_OP_GREATER
, params
.availability
);
754 if (!params
.extension
.IsEmpty()){
755 if (++iParameterCount
< parametercount
) {
756 target
.WriteBooleanAND();
758 target
.WriteMetaDataSearchParam(FT_FILEFORMAT
, params
.extension
);
761 //#warning TODO - I keep this here, ready if we ever allow such searches...
764 if (++iParameterCount
< parametercount
) {
765 target
.WriteBooleanAND();
767 target
.WriteMetaDataSearchParam(FT_COMPLETE_SOURCES
, ED2K_SEARCH_OP_GREATER
, complete
);
771 if (++iParameterCount
< parametercount
) {
772 target
.WriteBooleanAND();
774 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_BITRATE
: FT_ED2K_MEDIA_BITRATE
, ED2K_SEARCH_OP_GREATER
, minBitrate
);
778 if (++iParameterCount
< parametercount
) {
779 target
.WriteBooleanAND();
781 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_LENGTH
: FT_ED2K_MEDIA_LENGTH
, ED2K_SEARCH_OP_GREATER
, minLength
);
784 if (!codec
.IsEmpty()){
785 if (++iParameterCount
< parametercount
) {
786 target
.WriteBooleanAND();
788 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_CODEC
: FT_ED2K_MEDIA_CODEC
, codec
);
791 if (!title
.IsEmpty()){
792 if (++iParameterCount
< parametercount
) {
793 target
.WriteBooleanAND();
795 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_TITLE
: FT_ED2K_MEDIA_TITLE
, title
);
798 if (!album
.IsEmpty()){
799 if (++iParameterCount
< parametercount
) {
800 target
.WriteBooleanAND();
802 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_ALBUM
: FT_ED2K_MEDIA_ALBUM
, album
);
805 if (!artist
.IsEmpty()){
806 if (++iParameterCount
< parametercount
) {
807 target
.WriteBooleanAND();
809 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_ARTIST
: FT_ED2K_MEDIA_ARTIST
, artist
);
813 // If this assert fails... we're seriously fucked up
815 wxASSERT( iParameterCount
== parametercount
);
818 if (!params
.extension
.IsEmpty()) {
819 if (++iParameterCount
< parametercount
) {
820 target
.WriteBooleanAND();
824 if (params
.availability
> 0) {
825 if (++iParameterCount
< parametercount
) {
826 target
.WriteBooleanAND();
830 if (params
.maxSize
> 0){
831 if (++iParameterCount
< parametercount
) {
832 target
.WriteBooleanAND();
836 if (params
.minSize
> 0) {
837 if (++iParameterCount
< parametercount
) {
838 target
.WriteBooleanAND();
842 if (!typeText
.IsEmpty()){
843 if (++iParameterCount
< parametercount
) {
844 target
.WriteBooleanAND();
848 //#warning TODO - same as above...
851 if (++iParameterCount
< parametercount
) {
852 target
.WriteBooleanAND();
857 if (++iParameterCount
< parametercount
) {
858 target
.WriteBooleanAND();
863 if (++iParameterCount
< parametercount
) {
864 target
.WriteBooleanAND();
868 if (!codec
.IsEmpty()){
869 if (++iParameterCount
< parametercount
) {
870 target
.WriteBooleanAND();
874 if (!title
.IsEmpty()){
875 if (++iParameterCount
< parametercount
) {
876 target
.WriteBooleanAND();
880 if (!album
.IsEmpty()) {
881 if (++iParameterCount
< parametercount
) {
882 target
.WriteBooleanAND();
886 if (!artist
.IsEmpty()) {
887 if (++iParameterCount
< parametercount
) {
888 target
.WriteBooleanAND();
893 // As above, if this fails, we're seriously fucked up.
894 wxASSERT( iParameterCount
+ _SearchExpr
.m_aExpr
.GetCount() == parametercount
);
896 for (unsigned int j
= 0; j
< _SearchExpr
.m_aExpr
.GetCount(); ++j
) {
897 if (_SearchExpr
.m_aExpr
[j
] == SEARCHOPTOK_AND
) {
898 target
.WriteBooleanAND();
899 } else if (_SearchExpr
.m_aExpr
[j
] == SEARCHOPTOK_OR
) {
900 target
.WriteBooleanOR();
901 } else if (_SearchExpr
.m_aExpr
[j
] == SEARCHOPTOK_NOT
) {
902 target
.WriteBooleanNOT();
904 target
.WriteMetaDataSearchParam(_SearchExpr
.m_aExpr
[j
]);
908 if (!params
.typeText
.IsEmpty()) {
909 // Type is always ASCII string
910 target
.WriteMetaDataSearchParamASCII(FT_FILETYPE
, params
.typeText
);
913 if (params
.minSize
> 0) {
914 target
.WriteMetaDataSearchParam(FT_FILESIZE
, ED2K_SEARCH_OP_GREATER
, params
.minSize
);
917 if (params
.maxSize
> 0) {
918 target
.WriteMetaDataSearchParam(FT_FILESIZE
, ED2K_SEARCH_OP_LESS
, params
.maxSize
);
921 if (params
.availability
> 0) {
922 target
.WriteMetaDataSearchParam(FT_SOURCES
, ED2K_SEARCH_OP_GREATER
, params
.availability
);
925 if (!params
.extension
.IsEmpty()) {
926 target
.WriteMetaDataSearchParam(FT_FILEFORMAT
, params
.extension
);
929 //#warning TODO - third and last warning of the same series.
932 target
.WriteMetaDataSearchParam(FT_COMPLETE_SOURCES
, ED2K_SEARCH_OP_GREATER
, pParams
->uComplete
);
935 if (minBitrate
> 0) {
936 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_BITRATE
: FT_ED2K_MEDIA_BITRATE
, ED2K_SEARCH_OP_GREATER
, minBitrate
);
940 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_LENGTH
: FT_ED2K_MEDIA_LENGTH
, ED2K_SEARCH_OP_GREATER
, minLength
);
943 if (!codec
.IsEmpty()) {
944 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_CODEC
: FT_ED2K_MEDIA_CODEC
, codec
);
947 if (!title
.IsEmpty()) {
948 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_TITLE
: FT_ED2K_MEDIA_TITLE
, title
);
951 if (!album
.IsEmpty()) {
952 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_ALBUM
: FT_ED2K_MEDIA_ALBUM
, album
);
955 if (!artist
.IsEmpty()) {
956 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_ARTIST
: FT_ED2K_MEDIA_ARTIST
, artist
);
962 // Packet ready to go.
967 void CSearchList::KademliaSearchKeyword(uint32_t searchID
, const Kademlia::CUInt128
*fileID
,
968 const wxString
& name
, uint64_t size
, const wxString
& type
, uint32_t kadPublishInfo
, const TagPtrList
& taglist
)
970 EUtf8Str eStrEncode
= utf8strRaw
;
974 fileID
->ToByteArray(fileid
);
975 temp
.WriteHash(CMD4Hash(fileid
));
977 temp
.WriteUInt32(0); // client IP
978 temp
.WriteUInt16(0); // client port
981 unsigned int uFilePosTagCount
= temp
.GetPosition();
983 temp
.WriteUInt32(tagcount
); // dummy tag count, will be filled later
986 CTagString
tagName(FT_FILENAME
, name
);
987 tagName
.WriteTagToFile(&temp
, eStrEncode
);
990 CTagInt64
tagSize(FT_FILESIZE
, size
);
991 tagSize
.WriteTagToFile(&temp
, eStrEncode
);
994 if (!type
.IsEmpty()) {
995 CTagString
tagType(FT_FILETYPE
, type
);
996 tagType
.WriteTagToFile(&temp
, eStrEncode
);
1000 // Misc tags (bitrate, etc)
1001 for (TagPtrList::const_iterator it
= taglist
.begin(); it
!= taglist
.end(); ++it
) {
1002 (*it
)->WriteTagToFile(&temp
,eStrEncode
);
1006 temp
.Seek(uFilePosTagCount
, wxFromStart
);
1007 temp
.WriteUInt32(tagcount
);
1009 temp
.Seek(0, wxFromStart
);
1011 CSearchFile
*tempFile
= new CSearchFile(temp
, (eStrEncode
== utf8strRaw
), searchID
, 0, 0, wxEmptyString
, true);
1012 tempFile
->SetKadPublishInfo(kadPublishInfo
);
1014 AddToList(tempFile
);
1017 void CSearchList::UpdateSearchFileByHash(const CMD4Hash
& hash
)
1019 for (ResultMap::iterator it
= m_results
.begin(); it
!= m_results
.end(); it
++) {
1020 CSearchResultList
& results
= it
->second
;
1021 for (size_t i
= 0; i
< results
.size(); ++i
) {
1022 CSearchFile
* item
= results
.at(i
);
1024 if (hash
== item
->GetFileHash()) {
1025 // This covers only parent items,
1026 // child items have to be updated separately.
1027 Notify_Search_Update_Sources(item
);
1033 // File_checked_for_headers