2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 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
) {
342 AddLogLineM(true, 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 AddDebugLogLineM(false, 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 AddDebugLogLineM(false, 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 AddDebugLogLineM(false, 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 AddDebugLogLineM(false, 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 AddDebugLogLineM(false, 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 AddDebugLogLineM(false, 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 AddDebugLogLineM( false, 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 AddDebugLogLineM(false, 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 AddDebugLogLineM(false, 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::StopGlobalSearch()
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);
635 void CSearchList::StopKadSearch()
637 if (m_searchType
== KadSearch
) {
638 Kademlia::CSearchManager::StopSearch(m_currentSearch
, false);
639 m_currentSearch
= -1;
644 CSearchList::CMemFilePtr
CSearchList::CreateSearchData(const CSearchParams
& params
, SearchType
WXUNUSED(type
), bool supports64bit
, bool& packetUsing64bit
)
646 // Count the number of used parameters
647 unsigned int parametercount
= 0;
648 if ( !params
.typeText
.IsEmpty() ) ++parametercount
;
649 if ( params
.minSize
> 0 ) ++parametercount
;
650 if ( params
.maxSize
> 0 ) ++parametercount
;
651 if ( params
.availability
> 0 ) ++parametercount
;
652 if ( !params
.extension
.IsEmpty() ) ++parametercount
;
654 wxString typeText
= params
.typeText
;
655 if (typeText
== ED2KFTSTR_ARCHIVE
){
656 // eDonkeyHybrid 0.48 uses type "Pro" for archives files
657 // www.filedonkey.com uses type "Pro" for archives files
658 typeText
= ED2KFTSTR_PROGRAM
;
659 } else if (typeText
== ED2KFTSTR_CDIMAGE
){
660 // eDonkeyHybrid 0.48 uses *no* type for iso/nrg/cue/img files
661 // www.filedonkey.com uses type "Pro" for CD-image files
662 typeText
= ED2KFTSTR_PROGRAM
;
665 // Must write parametercount - 1 parameter headers
666 CMemFilePtr
data(new CMemFile(100));
668 _astrParserErrors
.Empty();
669 _SearchExpr
.m_aExpr
.Empty();
671 LexInit(params
.searchString
);
672 int iParseResult
= yyparse();
676 AddLogLineNS(CFormat(wxT("Search parsing result for \"%s\": %i"))
677 % params
.searchString
% iParseResult
);
679 if (_astrParserErrors
.GetCount() > 0) {
680 for (unsigned int i
=0; i
< _astrParserErrors
.GetCount(); ++i
) {
681 AddLogLineNS(CFormat(wxT("Error %u: %s\n")) % i
% _astrParserErrors
[i
]);
684 return CMemFilePtr(NULL
);
687 if (iParseResult
!= 0) {
688 _astrParserErrors
.Add(wxString::Format(wxT("Undefined error %i on search expression"),iParseResult
));
690 return CMemFilePtr(NULL
);
694 wxString
mes(wxT("Search expression:"));
695 for (unsigned int i
= 0; i
< _SearchExpr
.m_aExpr
.GetCount(); i
++) {
696 mes
<< wxT(" ") << _SearchExpr
.m_aExpr
[i
];
699 AddLogLineNS(CFormat(wxT("Expression count: %i")) % _SearchExpr
.m_aExpr
.GetCount());
702 parametercount
+= _SearchExpr
.m_aExpr
.GetCount();
705 AddLogLineNS(CFormat(wxT("Parameters: %i")) % parametercount
);
708 /* Leave the unicode comment there, please... */
709 CSearchExprTarget
target(data
.get(), true /*I assume everyone is unicoded */ ? utf8strRaw
: utf8strNone
, supports64bit
, packetUsing64bit
);
711 unsigned int iParameterCount
= 0;
712 if (_SearchExpr
.m_aExpr
.GetCount() <= 1) {
713 // lugdunummaster requested that searchs without OR or NOT operators,
714 // and hence with no more expressions than the string itself, be sent
715 // using a series of ANDed terms, intersecting the ANDs on the terms
716 // (but prepending them) instead of putting the boolean tree at the start
717 // like other searches. This type of search is supposed to take less load
718 // on servers. Go figure.
720 // input: "a" AND min=1 AND max=2
721 // instead of: AND AND "a" min=1 max=2
722 // we use: AND "a" AND min=1 max=2
724 if (_SearchExpr
.m_aExpr
.GetCount() > 0) {
725 if (++iParameterCount
< parametercount
) {
726 target
.WriteBooleanAND();
728 target
.WriteMetaDataSearchParam(_SearchExpr
.m_aExpr
[0]);
731 if (!typeText
.IsEmpty()) {
732 if (++iParameterCount
< parametercount
) {
733 target
.WriteBooleanAND();
735 // Type is always ascii string
736 target
.WriteMetaDataSearchParamASCII(FT_FILETYPE
, typeText
);
739 if (params
.minSize
> 0) {
740 if (++iParameterCount
< parametercount
) {
741 target
.WriteBooleanAND();
743 target
.WriteMetaDataSearchParam(FT_FILESIZE
, ED2K_SEARCH_OP_GREATER
, params
.minSize
);
746 if (params
.maxSize
> 0){
747 if (++iParameterCount
< parametercount
) {
748 target
.WriteBooleanAND();
750 target
.WriteMetaDataSearchParam(FT_FILESIZE
, ED2K_SEARCH_OP_LESS
, params
.maxSize
);
753 if (params
.availability
> 0){
754 if (++iParameterCount
< parametercount
) {
755 target
.WriteBooleanAND();
757 target
.WriteMetaDataSearchParam(FT_SOURCES
, ED2K_SEARCH_OP_GREATER
, params
.availability
);
760 if (!params
.extension
.IsEmpty()){
761 if (++iParameterCount
< parametercount
) {
762 target
.WriteBooleanAND();
764 target
.WriteMetaDataSearchParam(FT_FILEFORMAT
, params
.extension
);
767 //#warning TODO - I keep this here, ready if we ever allow such searches...
770 if (++iParameterCount
< parametercount
) {
771 target
.WriteBooleanAND();
773 target
.WriteMetaDataSearchParam(FT_COMPLETE_SOURCES
, ED2K_SEARCH_OP_GREATER
, complete
);
777 if (++iParameterCount
< parametercount
) {
778 target
.WriteBooleanAND();
780 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_BITRATE
: FT_ED2K_MEDIA_BITRATE
, ED2K_SEARCH_OP_GREATER
, minBitrate
);
784 if (++iParameterCount
< parametercount
) {
785 target
.WriteBooleanAND();
787 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_LENGTH
: FT_ED2K_MEDIA_LENGTH
, ED2K_SEARCH_OP_GREATER
, minLength
);
790 if (!codec
.IsEmpty()){
791 if (++iParameterCount
< parametercount
) {
792 target
.WriteBooleanAND();
794 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_CODEC
: FT_ED2K_MEDIA_CODEC
, codec
);
797 if (!title
.IsEmpty()){
798 if (++iParameterCount
< parametercount
) {
799 target
.WriteBooleanAND();
801 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_TITLE
: FT_ED2K_MEDIA_TITLE
, title
);
804 if (!album
.IsEmpty()){
805 if (++iParameterCount
< parametercount
) {
806 target
.WriteBooleanAND();
808 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_ALBUM
: FT_ED2K_MEDIA_ALBUM
, album
);
811 if (!artist
.IsEmpty()){
812 if (++iParameterCount
< parametercount
) {
813 target
.WriteBooleanAND();
815 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_ARTIST
: FT_ED2K_MEDIA_ARTIST
, artist
);
819 // If this assert fails... we're seriously fucked up
821 wxASSERT( iParameterCount
== parametercount
);
824 if (!params
.extension
.IsEmpty()) {
825 if (++iParameterCount
< parametercount
) {
826 target
.WriteBooleanAND();
830 if (params
.availability
> 0) {
831 if (++iParameterCount
< parametercount
) {
832 target
.WriteBooleanAND();
836 if (params
.maxSize
> 0){
837 if (++iParameterCount
< parametercount
) {
838 target
.WriteBooleanAND();
842 if (params
.minSize
> 0) {
843 if (++iParameterCount
< parametercount
) {
844 target
.WriteBooleanAND();
848 if (!typeText
.IsEmpty()){
849 if (++iParameterCount
< parametercount
) {
850 target
.WriteBooleanAND();
854 //#warning TODO - same as above...
857 if (++iParameterCount
< parametercount
) {
858 target
.WriteBooleanAND();
863 if (++iParameterCount
< parametercount
) {
864 target
.WriteBooleanAND();
869 if (++iParameterCount
< parametercount
) {
870 target
.WriteBooleanAND();
874 if (!codec
.IsEmpty()){
875 if (++iParameterCount
< parametercount
) {
876 target
.WriteBooleanAND();
880 if (!title
.IsEmpty()){
881 if (++iParameterCount
< parametercount
) {
882 target
.WriteBooleanAND();
886 if (!album
.IsEmpty()) {
887 if (++iParameterCount
< parametercount
) {
888 target
.WriteBooleanAND();
892 if (!artist
.IsEmpty()) {
893 if (++iParameterCount
< parametercount
) {
894 target
.WriteBooleanAND();
899 // As above, if this fails, we're seriously fucked up.
900 wxASSERT( iParameterCount
+ _SearchExpr
.m_aExpr
.GetCount() == parametercount
);
902 for (unsigned int j
= 0; j
< _SearchExpr
.m_aExpr
.GetCount(); ++j
) {
903 if (_SearchExpr
.m_aExpr
[j
] == SEARCHOPTOK_AND
) {
904 target
.WriteBooleanAND();
905 } else if (_SearchExpr
.m_aExpr
[j
] == SEARCHOPTOK_OR
) {
906 target
.WriteBooleanOR();
907 } else if (_SearchExpr
.m_aExpr
[j
] == SEARCHOPTOK_NOT
) {
908 target
.WriteBooleanNOT();
910 target
.WriteMetaDataSearchParam(_SearchExpr
.m_aExpr
[j
]);
914 if (!params
.typeText
.IsEmpty()) {
915 // Type is always ASCII string
916 target
.WriteMetaDataSearchParamASCII(FT_FILETYPE
, params
.typeText
);
919 if (params
.minSize
> 0) {
920 target
.WriteMetaDataSearchParam(FT_FILESIZE
, ED2K_SEARCH_OP_GREATER
, params
.minSize
);
923 if (params
.maxSize
> 0) {
924 target
.WriteMetaDataSearchParam(FT_FILESIZE
, ED2K_SEARCH_OP_LESS
, params
.maxSize
);
927 if (params
.availability
> 0) {
928 target
.WriteMetaDataSearchParam(FT_SOURCES
, ED2K_SEARCH_OP_GREATER
, params
.availability
);
931 if (!params
.extension
.IsEmpty()) {
932 target
.WriteMetaDataSearchParam(FT_FILEFORMAT
, params
.extension
);
935 //#warning TODO - third and last warning of the same series.
938 target
.WriteMetaDataSearchParam(FT_COMPLETE_SOURCES
, ED2K_SEARCH_OP_GREATER
, pParams
->uComplete
);
941 if (minBitrate
> 0) {
942 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_BITRATE
: FT_ED2K_MEDIA_BITRATE
, ED2K_SEARCH_OP_GREATER
, minBitrate
);
946 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_LENGTH
: FT_ED2K_MEDIA_LENGTH
, ED2K_SEARCH_OP_GREATER
, minLength
);
949 if (!codec
.IsEmpty()) {
950 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_CODEC
: FT_ED2K_MEDIA_CODEC
, codec
);
953 if (!title
.IsEmpty()) {
954 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_TITLE
: FT_ED2K_MEDIA_TITLE
, title
);
957 if (!album
.IsEmpty()) {
958 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_ALBUM
: FT_ED2K_MEDIA_ALBUM
, album
);
961 if (!artist
.IsEmpty()) {
962 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_ARTIST
: FT_ED2K_MEDIA_ARTIST
, artist
);
968 // Packet ready to go.
973 void CSearchList::KademliaSearchKeyword(uint32_t searchID
, const Kademlia::CUInt128
*fileID
,
974 const wxString
& name
, uint64_t size
, const wxString
& type
, uint32_t kadPublishInfo
, const TagPtrList
& taglist
)
976 EUtf8Str eStrEncode
= utf8strRaw
;
980 fileID
->ToByteArray(fileid
);
981 temp
.WriteHash(CMD4Hash(fileid
));
983 temp
.WriteUInt32(0); // client IP
984 temp
.WriteUInt16(0); // client port
987 unsigned int uFilePosTagCount
= temp
.GetPosition();
989 temp
.WriteUInt32(tagcount
); // dummy tag count, will be filled later
992 CTagString
tagName(FT_FILENAME
, name
);
993 tagName
.WriteTagToFile(&temp
, eStrEncode
);
996 CTagInt64
tagSize(FT_FILESIZE
, size
);
997 tagSize
.WriteTagToFile(&temp
, eStrEncode
);
1000 if (!type
.IsEmpty()) {
1001 CTagString
tagType(FT_FILETYPE
, type
);
1002 tagType
.WriteTagToFile(&temp
, eStrEncode
);
1006 // Misc tags (bitrate, etc)
1007 for (TagPtrList::const_iterator it
= taglist
.begin(); it
!= taglist
.end(); ++it
) {
1008 (*it
)->WriteTagToFile(&temp
,eStrEncode
);
1012 temp
.Seek(uFilePosTagCount
, wxFromStart
);
1013 temp
.WriteUInt32(tagcount
);
1015 temp
.Seek(0, wxFromStart
);
1017 CSearchFile
*tempFile
= new CSearchFile(temp
, (eStrEncode
== utf8strRaw
), searchID
, 0, 0, wxEmptyString
, true);
1018 tempFile
->SetKadPublishInfo(kadPublishInfo
);
1020 AddToList(tempFile
);
1023 void CSearchList::UpdateSearchFileByHash(const CMD4Hash
& hash
)
1025 for (ResultMap::iterator it
= m_results
.begin(); it
!= m_results
.end(); it
++) {
1026 CSearchResultList
& results
= it
->second
;
1027 for (size_t i
= 0; i
< results
.size(); ++i
) {
1028 CSearchFile
* item
= results
.at(i
);
1030 if (hash
== item
->GetFileHash()) {
1031 // This covers only parent items,
1032 // child items have to be updated separately.
1033 Notify_Search_Update_Sources(item
);
1039 // File_checked_for_headers