2 * XdccDownloader - CommandReader.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.xdcc.ui.stdin;
20 import java.io.BufferedReader;
21 import java.io.Reader;
22 import java.io.Writer;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
28 import net.pterodactylus.irc.DccReceiver;
29 import net.pterodactylus.xdcc.core.Core;
30 import net.pterodactylus.xdcc.data.Bot;
31 import net.pterodactylus.xdcc.data.Pack;
33 import com.google.common.collect.Lists;
34 import com.google.common.collect.Sets;
35 import com.google.common.primitives.Ints;
36 import com.google.common.util.concurrent.AbstractExecutionThreadService;
39 * Command interface for arbitrary {@link Reader}s.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class CommandReader extends AbstractExecutionThreadService {
45 /** The core being controlled. */
46 private final Core core;
48 /** The reader to read commands from. */
49 private final BufferedReader reader;
51 /** The writer to write the results to. */
52 private final Writer writer;
55 * Creates a new command reader.
58 * The core being controlled
60 * The reader to read commands from
62 * The write to write results to
64 public CommandReader(Core core, Reader reader, Writer writer) {
66 this.reader = new BufferedReader(reader);
71 // ABSTRACTEXECUTIONTHREADSERVICE METHODS
75 protected void run() throws Exception {
78 final List<Result> lastResult = Lists.newArrayList();
79 while ((line = reader.readLine()) != null) {
80 if (line.equals("")) {
83 String[] words = line.split(" +");
84 if (words[0].equalsIgnoreCase("search")) {
86 for (Bot bot : core.bots()) {
87 for (Pack pack : bot) {
89 for (int wordIndex = 1; wordIndex < words.length; ++wordIndex) {
90 if (words[wordIndex].startsWith("-") && pack.name().toLowerCase().contains(words[wordIndex].toLowerCase().substring(1))) {
94 if (!words[wordIndex].startsWith("-") && !pack.name().toLowerCase().contains(words[wordIndex].toLowerCase())) {
100 lastResult.add(new Result(bot, pack));
104 Collections.sort(lastResult);
106 for (Result result : lastResult) {
107 writer.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()));
109 writer.write("End of Search.\n");
110 } else if (words[0].equalsIgnoreCase("dcc")) {
112 for (DccReceiver dccReceiver : core.dccReceivers()) {
113 writer.write(String.format("[%d] %s (%s, %d%%%s)\n", counter++, dccReceiver.filename(), dccReceiver.size(), dccReceiver.progress() * 100 / dccReceiver.size(), dccReceiver.isRunning() ? "" : ", finished"));
115 writer.write("End of DCCs.\n");
116 } else if (words[0].equalsIgnoreCase("get")) {
117 Integer index = Ints.tryParse(words[1]);
118 if ((index != null) && (index < lastResult.size())) {
119 core.fetch(lastResult.get(index).bot(), lastResult.get(index).pack());
121 } else if (words[0].equalsIgnoreCase("stats")) {
122 int configuredChannelsCount = core.channels().size();
123 int joinedChannelsCount = core.joinedChannels().size();
124 int extraChannelsCount = core.extraChannels().size();
125 Collection<Bot> bots = core.bots();
126 Set<String> packNames = Sets.newHashSet();
128 for (Bot bot : bots) {
129 packsCount += bot.packs().size();
130 for (Pack pack : bot) {
131 packNames.add(pack.name());
135 writer.write(String.format("%d channels (%d joined, %d extra), %d bots offering %d packs (%d unique).\n", configuredChannelsCount, joinedChannelsCount, extraChannelsCount, bots.size(), packsCount, packNames.size()));
143 /** Container for result information. */
144 private static class Result implements Comparable<Result> {
146 /** The bot carrying the pack. */
147 private final Bot bot;
150 private final Pack pack;
153 * Creates a new result.
156 * The bot carrying the pack
160 private Result(Bot bot, Pack pack) {
170 * Returns the bot carrying the pack.
172 * @return The bot carrying the pack
188 // COMPARABLE METHODS
192 public int compareTo(Result result) {
193 return pack().name().compareToIgnoreCase(result.pack().name());