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 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
.Count(); 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
.Count(); 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
.Count() == 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
.Count());
319 for (unsigned int i
= 0; i
< _astrParserErrors
.Count(); ++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 seardhID is updated.
335 Kademlia::CSearch
* search
= Kademlia::CSearchManager::PrepareFindKeywords(
336 params
.searchString
, data
->GetLength(), data
->GetRawBuffer(), *searchID
);
338 *searchID
= search
->GetSearchID();
339 } catch (const wxString
& what
) {
340 AddLogLineM(true, what
);
341 return _("Unexpected error while attempting Kad search: ") + what
;
344 // This is an ed2k search, local or global
345 m_currentSearch
= *(searchID
);
346 m_searchInProgress
= true;
348 CPacket
* searchPacket
= new CPacket(*data
.get(), OP_EDONKEYPROT
, OP_SEARCHREQUEST
);
350 theStats::AddUpOverheadServer(searchPacket
->GetPacketSize());
351 theApp
->serverconnect
->SendPacket(searchPacket
, (type
== LocalSearch
));
353 if (type
== GlobalSearch
) {
354 m_searchPacket
= searchPacket
;
355 m_64bitSearchPacket
= packetUsing64bit
;
356 m_searchPacket
->SetOpCode(OP_GLOBSEARCHREQ
); // will be changed later when actually sending the packet!!
360 return wxEmptyString
;
364 void CSearchList::LocalSearchEnd()
366 if (m_searchType
== GlobalSearch
) {
367 wxCHECK_RET(m_searchPacket
, wxT("Global search, but no packet"));
369 // Ensure that every global search starts over.
370 theApp
->serverlist
->RemoveObserver(&m_serverQueue
);
371 m_searchTimer
.Start(750);
373 m_searchInProgress
= false;
374 Notify_SearchLocalEnd();
379 uint32
CSearchList::GetSearchProgress() const
381 if (m_searchInProgress
== false) {
382 // No search, no progress ;)
386 switch (m_searchType
) {
391 return 100 - (m_serverQueue
.GetRemaining() * 100)
392 / theApp
->serverlist
->GetServerCount();
395 // We cannot measure the progress of Kad searches.
404 void CSearchList::OnGlobalSearchTimer(CTimerEvent
& WXUNUSED(evt
))
406 // Ensure that the server-queue contains the current servers.
407 if (m_searchPacket
== NULL
) {
408 // This was a pending event, handled after 'Stop' was pressed.
410 } else if (!m_serverQueue
.IsActive()) {
411 theApp
->serverlist
->AddObserver(&m_serverQueue
);
414 // UDP requests must not be sent to this server.
415 const CServer
* localServer
= theApp
->serverconnect
->GetCurrentServer();
417 uint32 localIP
= localServer
->GetIP();
418 uint16 localPort
= localServer
->GetPort();
419 while (m_serverQueue
.GetRemaining()) {
420 CServer
* server
= m_serverQueue
.GetNext();
422 // Compare against the currently connected server.
423 if ((server
->GetPort() == localPort
) && (server
->GetIP() == localIP
)) {
424 // We've already requested from the local server.
427 if (server
->SupportsLargeFilesUDP() && (server
->GetUDPFlags() & SRV_UDPFLG_EXT_GETFILES
)) {
429 uint32_t tagCount
= 1;
430 data
.WriteUInt32(tagCount
);
431 CTagVarInt
flags(CT_SERVER_UDPSEARCH_FLAGS
, SRVCAP_UDP_NEWTAGS_LARGEFILES
);
432 flags
.WriteNewEd2kTag(&data
);
433 CPacket
*extSearchPacket
= new CPacket(OP_GLOBSEARCHREQ3
, m_searchPacket
->GetPacketSize() + (uint32_t)data
.GetLength(), OP_EDONKEYPROT
);
434 extSearchPacket
->CopyToDataBuffer(0, data
.GetRawBuffer(), data
.GetLength());
435 extSearchPacket
->CopyToDataBuffer(data
.GetLength(), m_searchPacket
->GetDataBuffer(), m_searchPacket
->GetPacketSize());
436 theStats::AddUpOverheadServer(extSearchPacket
->GetPacketSize());
437 theApp
->serverconnect
->SendUDPPacket(extSearchPacket
, server
, true);
438 AddDebugLogLineM(false, logServerUDP
, wxT("Sending OP_GLOBSEARCHREQ3 to server ") + Uint32_16toStringIP_Port(server
->GetIP(), server
->GetPort()));
439 } else if (server
->GetUDPFlags() & SRV_UDPFLG_EXT_GETFILES
) {
440 if (!m_64bitSearchPacket
|| server
->SupportsLargeFilesUDP()) {
441 m_searchPacket
->SetOpCode(OP_GLOBSEARCHREQ2
);
442 AddDebugLogLineM(false, logServerUDP
, wxT("Sending OP_GLOBSEARCHREQ2 to server ") + Uint32_16toStringIP_Port(server
->GetIP(), server
->GetPort()));
443 theStats::AddUpOverheadServer(m_searchPacket
->GetPacketSize());
444 theApp
->serverconnect
->SendUDPPacket(m_searchPacket
, server
, false);
446 AddDebugLogLineM(false, logServerUDP
, wxT("Skipped UDP search on server ") + Uint32_16toStringIP_Port(server
->GetIP(), server
->GetPort()) + wxT(": No large file support"));
449 if (!m_64bitSearchPacket
|| server
->SupportsLargeFilesUDP()) {
450 m_searchPacket
->SetOpCode(OP_GLOBSEARCHREQ
);
451 AddDebugLogLineM(false, logServerUDP
, wxT("Sending OP_GLOBSEARCHREQ to server ") + Uint32_16toStringIP_Port(server
->GetIP(), server
->GetPort()));
452 theStats::AddUpOverheadServer(m_searchPacket
->GetPacketSize());
453 theApp
->serverconnect
->SendUDPPacket(m_searchPacket
, server
, false);
455 AddDebugLogLineM(false, logServerUDP
, wxT("Skipped UDP search on server ") + Uint32_16toStringIP_Port(server
->GetIP(), server
->GetPort()) + wxT(": No large file support"));
458 CoreNotify_Search_Update_Progress(GetSearchProgress());
463 // No more servers left to ask.
468 void CSearchList::ProcessSharedFileList(const byte
* in_packet
, uint32 size
,
469 CUpDownClient
* sender
, bool *moreResultsAvailable
, const wxString
& directory
)
471 wxCHECK_RET(sender
, wxT("No sender in search-results from client."));
473 long searchID
= reinterpret_cast<wxUIntPtr
>(sender
);
476 if (!theApp
->amuledlg
->m_searchwnd
->CheckTabNameExists(sender
->GetUserName())) {
477 theApp
->amuledlg
->m_searchwnd
->CreateNewTab(sender
->GetUserName() + wxT(" (0)"), searchID
);
481 const CMemFile
packet(in_packet
, size
);
482 uint32 results
= packet
.ReadUInt32();
483 bool unicoded
= (sender
->GetUnicodeSupport() != utf8strNone
);
484 for (unsigned int i
= 0; i
!= results
; ++i
){
485 CSearchFile
* toadd
= new CSearchFile(packet
, unicoded
, searchID
, 0, 0, directory
);
487 toadd
->SetClientID(sender
->GetUserIDHybrid());
488 toadd
->SetClientPort(sender
->GetUserPort());
491 AddToList(toadd
, true);
494 if (moreResultsAvailable
)
495 *moreResultsAvailable
= false;
497 int iAddData
= (int)(packet
.GetLength() - packet
.GetPosition());
499 uint8 ucMore
= packet
.ReadUInt8();
500 if (ucMore
== 0x00 || ucMore
== 0x01){
501 if (moreResultsAvailable
) {
502 *moreResultsAvailable
= (ucMore
== 1);
509 void CSearchList::ProcessSearchAnswer(const byte
* in_packet
, uint32 size
, bool optUTF8
, uint32
WXUNUSED(serverIP
), uint16
WXUNUSED(serverPort
))
511 CMemFile
packet(in_packet
,size
);
513 uint32 results
= packet
.ReadUInt32();
514 for (unsigned int i
= 0; i
!= results
; ++i
) {
515 AddToList(new CSearchFile(packet
, optUTF8
, m_currentSearch
));
520 void CSearchList::ProcessUDPSearchAnswer(const CMemFile
& packet
, bool optUTF8
, uint32 serverIP
, uint16 serverPort
)
522 AddToList(new CSearchFile(packet
, optUTF8
, m_currentSearch
, serverIP
, serverPort
));
526 bool CSearchList::AddToList(CSearchFile
* toadd
, bool clientResponse
)
528 const uint64 fileSize
= toadd
->GetFileSize();
529 // If filesize is 0, or file is too large for the network, drop it
530 if ((fileSize
== 0) || (fileSize
> MAX_FILE_SIZE
)) {
531 AddDebugLogLineM(false, logSearch
,
532 CFormat(wxT("Dropped result with filesize %u: %s"))
534 % toadd
->GetFileName());
540 // If the result was not the type the user wanted, drop it.
541 if ((clientResponse
== false) && !m_resultType
.IsEmpty()) {
542 if (GetFileTypeByName(toadd
->GetFileName()) != m_resultType
) {
543 AddDebugLogLineM( false, logSearch
,
544 CFormat( wxT("Dropped result type %s != %s, file %s") )
545 % GetFileTypeByName(toadd
->GetFileName())
547 % toadd
->GetFileName());
555 // Get, or implictly create, the map of results for this search
556 CSearchResultList
& results
= m_results
[toadd
->GetSearchID()];
558 for (size_t i
= 0; i
< results
.size(); ++i
) {
559 CSearchFile
* item
= results
.at(i
);
561 if ((toadd
->GetFileHash() == item
->GetFileHash()) && (toadd
->GetFileSize() == item
->GetFileSize())) {
562 AddDebugLogLineM(false, logSearch
, CFormat(wxT("Received duplicate results for '%s' : %s")) % item
->GetFileName() % item
->GetFileHash().Encode());
563 // Add the child, possibly updating the parents filename.
564 item
->AddChild(toadd
);
565 Notify_Search_Update_Sources(item
);
570 AddDebugLogLineM(false, logSearch
,
571 CFormat(wxT("Added new result '%s' : %s"))
572 % toadd
->GetFileName() % toadd
->GetFileHash().Encode());
574 // New unique result, simply add and display.
575 results
.push_back(toadd
);
576 Notify_Search_Add_Result(toadd
);
582 const CSearchResultList
& CSearchList::GetSearchResults(long searchID
) const
584 ResultMap::const_iterator it
= m_results
.find(searchID
);
585 if (it
!= m_results
.end()) {
589 // TODO: Should we assert in this case?
590 static CSearchResultList list
;
595 void CSearchList::AddFileToDownloadByHash(const CMD4Hash
& hash
, uint8 cat
)
597 ResultMap::iterator it
= m_results
.begin();
598 for ( ; it
!= m_results
.end(); ++it
) {
599 CSearchResultList
& list
= it
->second
;
601 for ( unsigned int i
= 0; i
< list
.size(); ++i
) {
602 if ( list
[i
]->GetFileHash() == hash
) {
603 CoreNotify_Search_Add_Download( list
[i
], cat
);
612 void CSearchList::StopGlobalSearch()
614 m_searchTimer
.Stop();
616 m_currentSearch
= -1;
617 delete m_searchPacket
;
618 m_searchPacket
= NULL
;
619 m_searchInProgress
= false;
621 CoreNotify_Search_Update_Progress(0xffff);
625 CSearchList::CMemFilePtr
CSearchList::CreateSearchData(const CSearchParams
& params
, SearchType type
, bool supports64bit
, bool& packetUsing64bit
)
627 // Count the number of used parameters
628 unsigned int parametercount
= 0;
629 if ( !params
.typeText
.IsEmpty() ) ++parametercount
;
630 if ( params
.minSize
> 0 ) ++parametercount
;
631 if ( params
.maxSize
> 0 ) ++parametercount
;
632 if ( params
.availability
> 0 ) ++parametercount
;
633 if ( !params
.extension
.IsEmpty() ) ++parametercount
;
635 wxString typeText
= params
.typeText
;
636 if (typeText
== ED2KFTSTR_ARCHIVE
){
637 // eDonkeyHybrid 0.48 uses type "Pro" for archives files
638 // www.filedonkey.com uses type "Pro" for archives files
639 typeText
= ED2KFTSTR_PROGRAM
;
640 } else if (typeText
== ED2KFTSTR_CDIMAGE
){
641 // eDonkeyHybrid 0.48 uses *no* type for iso/nrg/cue/img files
642 // www.filedonkey.com uses type "Pro" for CD-image files
643 typeText
= ED2KFTSTR_PROGRAM
;
646 // Must write parametercount - 1 parameter headers
647 CMemFilePtr
data(new CMemFile(100));
649 _astrParserErrors
.Empty();
650 _SearchExpr
.m_aExpr
.Empty();
652 LexInit(params
.searchString
);
653 int iParseResult
= yyparse();
657 printf("Search parsing result for \"%s\": %i\n",
658 (const char*)unicode2UTF8(params
.searchString
),iParseResult
);
660 if (_astrParserErrors
.Count() > 0) {
661 for (unsigned int i
=0; i
< _astrParserErrors
.Count(); ++i
) {
662 printf("Error %u: %s\n",i
,(const char*)unicode2UTF8(_astrParserErrors
[i
]));
665 return CMemFilePtr(NULL
);
668 if (iParseResult
!= 0) {
669 _astrParserErrors
.Add(wxString::Format(wxT("Undefined error %i on search expression"),iParseResult
));
671 return CMemFilePtr(NULL
);
675 printf("Search expression: ");
676 for (unsigned int i
= 0; i
< _SearchExpr
.m_aExpr
.Count(); i
++){
677 printf("%s ",(const char*)unicode2char(_SearchExpr
.m_aExpr
[i
]));
679 printf("\nExpression count: %i\n",(int)_SearchExpr
.m_aExpr
.GetCount());
682 parametercount
+= _SearchExpr
.m_aExpr
.GetCount();
685 printf("Parameters: %i\n",parametercount
);
688 /* Leave the unicode comment there, please... */
689 CSearchExprTarget
target(data
.get(), true /*I assume everyone is unicoded */ ? utf8strRaw
: utf8strNone
, supports64bit
, packetUsing64bit
);
691 unsigned int iParameterCount
= 0;
692 if (_SearchExpr
.m_aExpr
.GetCount() <= 1) {
693 // lugdunummaster requested that searchs without OR or NOT operators,
694 // and hence with no more expressions than the string itself, be sent
695 // using a series of ANDed terms, intersecting the ANDs on the terms
696 // (but prepending them) instead of putting the boolean tree at the start
697 // like other searches. This type of search is supposed to take less load
698 // on servers. Go figure.
700 // input: "a" AND min=1 AND max=2
701 // instead of: AND AND "a" min=1 max=2
702 // we use: AND "a" AND min=1 max=2
704 if (_SearchExpr
.m_aExpr
.GetCount() > 0) {
705 if (++iParameterCount
< parametercount
) {
706 target
.WriteBooleanAND();
708 target
.WriteMetaDataSearchParam(_SearchExpr
.m_aExpr
[0]);
711 if (!typeText
.IsEmpty()) {
712 if (++iParameterCount
< parametercount
) {
713 target
.WriteBooleanAND();
715 // Type is always ascii string
716 target
.WriteMetaDataSearchParamASCII(FT_FILETYPE
, typeText
);
719 if (params
.minSize
> 0) {
720 if (++iParameterCount
< parametercount
) {
721 target
.WriteBooleanAND();
723 target
.WriteMetaDataSearchParam(FT_FILESIZE
, ED2K_SEARCH_OP_GREATER
, params
.minSize
);
726 if (params
.maxSize
> 0){
727 if (++iParameterCount
< parametercount
) {
728 target
.WriteBooleanAND();
730 target
.WriteMetaDataSearchParam(FT_FILESIZE
, ED2K_SEARCH_OP_LESS
, params
.maxSize
);
733 if (params
.availability
> 0){
734 if (++iParameterCount
< parametercount
) {
735 target
.WriteBooleanAND();
737 target
.WriteMetaDataSearchParam(FT_SOURCES
, ED2K_SEARCH_OP_GREATER
, params
.availability
);
740 if (!params
.extension
.IsEmpty()){
741 if (++iParameterCount
< parametercount
) {
742 target
.WriteBooleanAND();
744 target
.WriteMetaDataSearchParam(FT_FILEFORMAT
, params
.extension
);
747 //#warning TODO - I keep this here, ready if we ever allow such searches...
750 if (++iParameterCount
< parametercount
) {
751 target
.WriteBooleanAND();
753 target
.WriteMetaDataSearchParam(FT_COMPLETE_SOURCES
, ED2K_SEARCH_OP_GREATER
, complete
);
757 if (++iParameterCount
< parametercount
) {
758 target
.WriteBooleanAND();
760 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_BITRATE
: FT_ED2K_MEDIA_BITRATE
, ED2K_SEARCH_OP_GREATER
, minBitrate
);
764 if (++iParameterCount
< parametercount
) {
765 target
.WriteBooleanAND();
767 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_LENGTH
: FT_ED2K_MEDIA_LENGTH
, ED2K_SEARCH_OP_GREATER
, minLength
);
770 if (!codec
.IsEmpty()){
771 if (++iParameterCount
< parametercount
) {
772 target
.WriteBooleanAND();
774 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_CODEC
: FT_ED2K_MEDIA_CODEC
, codec
);
777 if (!title
.IsEmpty()){
778 if (++iParameterCount
< parametercount
) {
779 target
.WriteBooleanAND();
781 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_TITLE
: FT_ED2K_MEDIA_TITLE
, title
);
784 if (!album
.IsEmpty()){
785 if (++iParameterCount
< parametercount
) {
786 target
.WriteBooleanAND();
788 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_ALBUM
: FT_ED2K_MEDIA_ALBUM
, album
);
791 if (!artist
.IsEmpty()){
792 if (++iParameterCount
< parametercount
) {
793 target
.WriteBooleanAND();
795 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_ARTIST
: FT_ED2K_MEDIA_ARTIST
, artist
);
799 // If this assert fails... we're seriously fucked up
801 wxASSERT( iParameterCount
== parametercount
);
804 if (!params
.extension
.IsEmpty()) {
805 if (++iParameterCount
< parametercount
) {
806 target
.WriteBooleanAND();
810 if (params
.availability
> 0) {
811 if (++iParameterCount
< parametercount
) {
812 target
.WriteBooleanAND();
816 if (params
.maxSize
> 0){
817 if (++iParameterCount
< parametercount
) {
818 target
.WriteBooleanAND();
822 if (params
.minSize
> 0) {
823 if (++iParameterCount
< parametercount
) {
824 target
.WriteBooleanAND();
828 if (!typeText
.IsEmpty()){
829 if (++iParameterCount
< parametercount
) {
830 target
.WriteBooleanAND();
834 //#warning TODO - same as above...
837 if (++iParameterCount
< parametercount
) {
838 target
.WriteBooleanAND();
843 if (++iParameterCount
< parametercount
) {
844 target
.WriteBooleanAND();
849 if (++iParameterCount
< parametercount
) {
850 target
.WriteBooleanAND();
854 if (!codec
.IsEmpty()){
855 if (++iParameterCount
< parametercount
) {
856 target
.WriteBooleanAND();
860 if (!title
.IsEmpty()){
861 if (++iParameterCount
< parametercount
) {
862 target
.WriteBooleanAND();
866 if (!album
.IsEmpty()) {
867 if (++iParameterCount
< parametercount
) {
868 target
.WriteBooleanAND();
872 if (!artist
.IsEmpty()) {
873 if (++iParameterCount
< parametercount
) {
874 target
.WriteBooleanAND();
879 // As above, if this fails, we're seriously fucked up.
880 wxASSERT( iParameterCount
+ _SearchExpr
.m_aExpr
.GetCount() == parametercount
);
882 for (unsigned int j
= 0; j
< _SearchExpr
.m_aExpr
.GetCount(); ++j
) {
883 if (_SearchExpr
.m_aExpr
[j
] == SEARCHOPTOK_AND
) {
884 target
.WriteBooleanAND();
885 } else if (_SearchExpr
.m_aExpr
[j
] == SEARCHOPTOK_OR
) {
886 target
.WriteBooleanOR();
887 } else if (_SearchExpr
.m_aExpr
[j
] == SEARCHOPTOK_NOT
) {
888 target
.WriteBooleanNOT();
890 target
.WriteMetaDataSearchParam(_SearchExpr
.m_aExpr
[j
]);
894 if (!params
.typeText
.IsEmpty()) {
895 // Type is always ASCII string
896 target
.WriteMetaDataSearchParamASCII(FT_FILETYPE
, params
.typeText
);
899 if (params
.minSize
> 0) {
900 target
.WriteMetaDataSearchParam(FT_FILESIZE
, ED2K_SEARCH_OP_GREATER
, params
.minSize
);
903 if (params
.maxSize
> 0) {
904 target
.WriteMetaDataSearchParam(FT_FILESIZE
, ED2K_SEARCH_OP_LESS
, params
.maxSize
);
907 if (params
.availability
> 0) {
908 target
.WriteMetaDataSearchParam(FT_SOURCES
, ED2K_SEARCH_OP_GREATER
, params
.availability
);
911 if (!params
.extension
.IsEmpty()) {
912 target
.WriteMetaDataSearchParam(FT_FILEFORMAT
, params
.extension
);
915 //#warning TODO - third and last warning of the same series.
918 target
.WriteMetaDataSearchParam(FT_COMPLETE_SOURCES
, ED2K_SEARCH_OP_GREATER
, pParams
->uComplete
);
921 if (minBitrate
> 0) {
922 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_BITRATE
: FT_ED2K_MEDIA_BITRATE
, ED2K_SEARCH_OP_GREATER
, minBitrate
);
926 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_LENGTH
: FT_ED2K_MEDIA_LENGTH
, ED2K_SEARCH_OP_GREATER
, minLength
);
929 if (!codec
.IsEmpty()) {
930 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_CODEC
: FT_ED2K_MEDIA_CODEC
, codec
);
933 if (!title
.IsEmpty()) {
934 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_TITLE
: FT_ED2K_MEDIA_TITLE
, title
);
937 if (!album
.IsEmpty()) {
938 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_ALBUM
: FT_ED2K_MEDIA_ALBUM
, album
);
941 if (!artist
.IsEmpty()) {
942 target
.WriteMetaDataSearchParam(type
== KadSearch
? TAG_MEDIA_ARTIST
: FT_ED2K_MEDIA_ARTIST
, artist
);
948 // Packet ready to go.
953 void CSearchList::KademliaSearchKeyword(uint32_t searchID
, const Kademlia::CUInt128
*fileID
,
954 const wxString
& name
, uint64_t size
, const wxString
& type
, uint32_t kadPublishInfo
, const TagPtrList
& taglist
)
956 EUtf8Str eStrEncode
= utf8strRaw
;
960 fileID
->ToByteArray(fileid
);
961 temp
.WriteHash(CMD4Hash(fileid
));
963 temp
.WriteUInt32(0); // client IP
964 temp
.WriteUInt16(0); // client port
967 unsigned int uFilePosTagCount
= temp
.GetPosition();
969 temp
.WriteUInt32(tagcount
); // dummy tag count, will be filled later
972 CTagString
tagName(FT_FILENAME
, name
);
973 tagName
.WriteTagToFile(&temp
, eStrEncode
);
976 CTagInt64
tagSize(FT_FILESIZE
, size
);
977 tagSize
.WriteTagToFile(&temp
, eStrEncode
);
980 if (!type
.IsEmpty()) {
981 CTagString
tagType(FT_FILETYPE
, type
);
982 tagType
.WriteTagToFile(&temp
, eStrEncode
);
986 // Misc tags (bitrate, etc)
987 for (TagPtrList::const_iterator it
= taglist
.begin(); it
!= taglist
.end(); ++it
) {
988 (*it
)->WriteTagToFile(&temp
,eStrEncode
);
992 temp
.Seek(uFilePosTagCount
, wxFromStart
);
993 temp
.WriteUInt32(tagcount
);
995 temp
.Seek(0, wxFromStart
);
997 CSearchFile
*tempFile
= new CSearchFile(temp
, (eStrEncode
== utf8strRaw
), searchID
, 0, 0, wxEmptyString
, true);
998 tempFile
->SetKadPublishInfo(kadPublishInfo
);
1000 AddToList(tempFile
);
1003 // File_checked_for_headers