✅ Add more tests for current behaviour
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / DownloadCommand.java
1 /*
2  * XdccDownloader - DownloadCommand.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 java.util.Arrays.asList;
21
22 import java.io.IOException;
23 import java.io.Writer;
24 import java.util.Collection;
25 import java.util.List;
26
27 import net.pterodactylus.xdcc.core.Core;
28
29 import com.google.common.primitives.Ints;
30
31 /**
32  * Command that requests a download from a bot.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  * @see State#getLastResults()
36  */
37 public class DownloadCommand implements Command {
38
39         /** The core to operate on. */
40         private final Core core;
41
42         /**
43          * Creates a new download command.
44          *
45          * @param core
46          *              The core to operate on
47          */
48         public DownloadCommand(Core core) {
49                 this.core = core;
50         }
51
52         //
53         // COMMAND METHODS
54         //
55
56         @Override
57         public String getName() {
58                 return "download";
59         }
60
61         @Override
62         public Collection<String> getAliases() {
63                 return asList("get");
64         }
65
66         @Override
67         public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
68                 if (parameters.isEmpty()) {
69                         return state;
70                 }
71                 Integer index = Ints.tryParse(parameters.get(0));
72                 if ((index != null) && (index < state.getLastResults().size())) {
73                         core.fetch(state.getLastResults().get(index).bot(), state.getLastResults().get(index).pack());
74                 }
75                 return state;
76         }
77
78 }