Split command reader into separate commands.
[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
23 import java.io.IOException;
24 import java.io.Writer;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
28
29 import net.pterodactylus.xdcc.core.Core;
30 import net.pterodactylus.xdcc.data.Bot;
31 import net.pterodactylus.xdcc.data.Pack;
32
33 /**
34  * Command that searches all {@link Pack}s of all {@link Bot}s for files
35  * matching the search parameters.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class SearchCommand implements Command {
40
41         /** The core to operate on. */
42         private final Core core;
43
44         /**
45          * Creates a new search command.
46          *
47          * @param core
48          *              The core to operate on
49          */
50         public SearchCommand(Core core) {
51                 this.core = core;
52         }
53
54         //
55         // COMMAND METHODS
56         //
57
58         @Override
59         public String getName() {
60                 return "search";
61         }
62
63         @Override
64         public Collection<String> getAliases() {
65                 return asList("find", "locate");
66         }
67
68         @Override
69         public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
70                 List<Result> lastResult = newArrayList();
71                 for (Bot bot : newArrayList(core.bots())) {
72                         for (Pack pack : newArrayList(bot)) {
73                                 boolean found = true;
74                                 for (String parameter : parameters) {
75                                         if (parameter.startsWith("-") && pack.name().toLowerCase().contains(parameter.toLowerCase().substring(1))) {
76                                                 found = false;
77                                                 break;
78                                         }
79                                         if (!parameter.startsWith("-") && !pack.name().toLowerCase().contains(parameter.toLowerCase())) {
80                                                 found = false;
81                                                 break;
82                                         }
83                                 }
84                                 if (found) {
85                                         lastResult.add(new Result(bot, pack));
86                                 }
87                         }
88                 }
89                 Collections.sort(lastResult);
90                 int counter = 0;
91                 for (Result result : lastResult) {
92                         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()));
93                 }
94                 outputWriter.write("End of Search.\n");
95                 outputWriter.flush();
96                 return state.setLastResults(lastResult);
97         }
98
99 }