Use regular expressions to match filenames.
[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
26 import java.io.IOException;
27 import java.io.Writer;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.regex.Pattern;
32
33 import net.pterodactylus.xdcc.core.Core;
34 import net.pterodactylus.xdcc.data.Bot;
35 import net.pterodactylus.xdcc.data.Pack;
36
37 /**
38  * Command that searches all {@link Pack}s of all {@link Bot}s for files
39  * matching the search parameters.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class SearchCommand implements Command {
44
45         /** The core to operate on. */
46         private final Core core;
47
48         /**
49          * Creates a new search command.
50          *
51          * @param core
52          *              The core to operate on
53          */
54         public SearchCommand(Core core) {
55                 this.core = core;
56         }
57
58         //
59         // COMMAND METHODS
60         //
61
62         @Override
63         public String getName() {
64                 return "search";
65         }
66
67         @Override
68         public Collection<String> getAliases() {
69                 return asList("find", "locate");
70         }
71
72         @Override
73         public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
74                 List<Result> lastResult = newArrayList();
75                 for (Bot bot : newArrayList(core.bots())) {
76                         for (Pack pack : newArrayList(bot)) {
77                                 if (parameters.stream().map(SearchParameter::from).allMatch((parameter) -> parameter.matches(pack.name()))) {
78                                         lastResult.add(new Result(core, bot, pack));
79                                 }
80                         }
81                 }
82                 Collections.sort(lastResult);
83                 int counter = 0;
84                 for (Result result : lastResult) {
85                         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()));
86                 }
87                 outputWriter.write("End of Search.\n");
88                 outputWriter.flush();
89                 return state.setLastResults(lastResult);
90         }
91
92         private static class SearchParameter {
93
94                 private final boolean exclude;
95                 private final Pattern pattern;
96
97                 private SearchParameter(boolean exclude, Pattern pattern) {
98                         this.exclude = exclude;
99                         this.pattern = pattern;
100                 }
101
102                 public boolean matches(String filename) {
103                         return pattern.matcher(filename).find() && !exclude;
104                 }
105
106                 public static SearchParameter from(String parameter) {
107                         boolean exclude = parameter.startsWith("-");
108                         Pattern pattern = compile(exclude ? parameter.substring(1) : parameter, CASE_INSENSITIVE | UNICODE_CASE);
109                         return new SearchParameter(exclude, pattern);
110                 }
111
112         }
113
114 }