From b308ac2f376a891370aa93b5e13a8a838ead7c0e Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 26 Oct 2019 05:14:04 +0100 Subject: [PATCH] searches by hash --- .../muwire/clilanterna/MainWindowView.groovy | 8 +++++ .../com/muwire/clilanterna/SearchModel.groovy | 29 +++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/cli-lanterna/src/main/groovy/com/muwire/clilanterna/MainWindowView.groovy b/cli-lanterna/src/main/groovy/com/muwire/clilanterna/MainWindowView.groovy index b2111d0e..ec1e5aa2 100644 --- a/cli-lanterna/src/main/groovy/com/muwire/clilanterna/MainWindowView.groovy +++ b/cli-lanterna/src/main/groovy/com/muwire/clilanterna/MainWindowView.groovy @@ -25,6 +25,8 @@ import com.muwire.core.files.FileLoadedEvent import com.muwire.core.files.FileUnsharedEvent import com.muwire.core.hostcache.HostDiscoveredEvent +import net.i2p.data.Base64 + class MainWindowView extends BasicWindow { private final Core core @@ -191,6 +193,12 @@ class MainWindowView extends BasicWindow { private void search() { String query = searchTextBox.getText() + query = query.trim() + if (query.length() == 0) + return + if (query.length() > 128) + query = query.substring(0, 128) + SearchModel model = new SearchModel(query, core, textGUI.getGUIThread()) textGUI.addWindowAndWait(new SearchView(model,core, textGUI, sizeForTables())) } diff --git a/cli-lanterna/src/main/groovy/com/muwire/clilanterna/SearchModel.groovy b/cli-lanterna/src/main/groovy/com/muwire/clilanterna/SearchModel.groovy index 22f0eae0..c99c3d28 100644 --- a/cli-lanterna/src/main/groovy/com/muwire/clilanterna/SearchModel.groovy +++ b/cli-lanterna/src/main/groovy/com/muwire/clilanterna/SearchModel.groovy @@ -7,6 +7,9 @@ import com.muwire.core.search.QueryEvent import com.muwire.core.search.SearchEvent import com.muwire.core.search.UIResultBatchEvent import com.muwire.core.search.UIResultEvent + +import net.i2p.data.Base64 + import com.googlecode.lanterna.gui2.TextGUIThread import com.googlecode.lanterna.gui2.table.TableModel class SearchModel { @@ -25,12 +28,28 @@ class SearchModel { core.eventBus.register(UIResultBatchEvent.class, this) - def replaced = query.toLowerCase().trim().replaceAll(SplitPattern.SPLIT_PATTERN, " ") - def terms = replaced.split(" ") - def nonEmpty = [] - terms.each { if (it.length() > 0) nonEmpty << it } - def searchEvent = new SearchEvent(searchTerms : nonEmpty, uuid : UUID.randomUUID(), oobInfohash: true, + boolean hashSearch = false + byte [] root = null + if (query.length() == 44 && query.indexOf(" ") < 0) { + try { + root = Base64.decode(query) + hashSearch = true + } catch (Exception e) { + // not hash search + } + } + + def searchEvent + if (hashSearch) { + searchEvent = new SearchEvent(searchHash : root, uuid : UUID.randomUUID(), oobInfohash : true, compressedResults : true) + } else { + def replaced = query.toLowerCase().trim().replaceAll(SplitPattern.SPLIT_PATTERN, " ") + def terms = replaced.split(" ") + def nonEmpty = [] + terms.each { if (it.length() > 0) nonEmpty << it } + searchEvent = new SearchEvent(searchTerms : nonEmpty, uuid : UUID.randomUUID(), oobInfohash: true, searchComments : core.muOptions.searchComments, compressedResults : true) + } boolean firstHop = core.muOptions.allowUntrusted || core.muOptions.searchExtraHop core.eventBus.publish(new QueryEvent(searchEvent : searchEvent, firstHop : firstHop, replyTo: core.me.destination, receivedOn: core.me.destination,