746f01c763919a310e72ba58f438316f1e42d22a
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / SearchCommand.java
1 /*
2  * XdccDownloader - SearchCommand.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.xdcc.ui.stdin;
19
20 import static com.google.common.collect.Lists.newArrayList;
21 import static java.util.Arrays.asList;
22 import static java.util.regex.Pattern.CASE_INSENSITIVE;
23 import static java.util.regex.Pattern.UNICODE_CASE;
24 import static java.util.regex.Pattern.compile;
25 import static java.util.stream.Collectors.toList;
26
27 import java.io.IOException;
28 import java.io.Writer;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.regex.Pattern;
33
34 import net.pterodactylus.xdcc.core.Core;
35 import net.pterodactylus.xdcc.data.Bot;
36 import net.pterodactylus.xdcc.data.Pack;
37
38 /**
39  * Command that searches all {@link Pack}s of all {@link Bot}s for files
40  * matching the search parameters.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class SearchCommand implements Command {
45
46         /** The core to operate on. */
47         private final Core core;
48
49         /**
50          * Creates a new search command.
51          *
52          * @param core
53          *              The core to operate on
54          */
55         public SearchCommand(Core core) {
56                 this.core = core;
57         }
58
59         //
60         // COMMAND METHODS
61         //
62
63         @Override
64         public String getName() {
65                 return "search";
66         }
67
68         @Override
69         public Collection<String> getAliases() {
70                 return asList("find", "locate");
71         }
72
73         @Override
74         public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
75                 List<Result> lastResult = newArrayList();
76                 List<SearchParameter> searchParameters = parameters.stream().map(SearchParameter::from).collect(toList());
77                 for (Bot bot : newArrayList(core.bots())) {
78                         for (Pack pack : newArrayList(bot)) {
79                                 if (searchParameters.stream().allMatch((parameter) -> parameter.matches(pack.name()))) {
80                                         lastResult.add(new Result(core, bot, pack));
81                                 }
82                         }
83                 }
84                 Collections.sort(lastResult);
85                 int counter = 0;
86                 for (Result result : lastResult) {
87                         outputWriter.write(String.format("[%d] %s (%s) from %s (#%s) on %s\n", counter++, result.pack().name(), result.pack().size(), result.bot().name(), result.pack().id(), result.bot().network().name()));
88                 }
89                 outputWriter.write("End of Search.\n");
90                 return state.setLastResults(lastResult);
91         }
92
93         private static class SearchParameter {
94
95                 private final boolean exclude;
96                 private final Pattern pattern;
97
98                 private SearchParameter(boolean exclude, Pattern pattern) {
99                         this.exclude = exclude;
100                         this.pattern = pattern;
101                 }
102
103                 public boolean matches(String filename) {
104                         return pattern.matcher(filename).find() && !exclude;
105                 }
106
107                 public static SearchParameter from(String parameter) {
108                         boolean exclude = parameter.startsWith("-");
109                         Pattern pattern = compile(exclude ? parameter.substring(1) : parameter, CASE_INSENSITIVE | UNICODE_CASE);
110                         return new SearchParameter(exclude, pattern);
111                 }
112
113         }
114
115 }