Add command to show active DCC receivers.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / CommandReader.java
1 /*
2  * XdccDownloader - CommandReader.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 java.io.BufferedReader;
21 import java.io.Reader;
22 import java.io.Writer;
23 import java.util.Collections;
24 import java.util.List;
25
26 import net.pterodactylus.irc.DccReceiver;
27 import net.pterodactylus.xdcc.core.Core;
28 import net.pterodactylus.xdcc.data.Bot;
29 import net.pterodactylus.xdcc.data.Pack;
30
31 import com.google.common.collect.Lists;
32 import com.google.common.util.concurrent.AbstractExecutionThreadService;
33
34 /**
35  * Command interface for arbitrary {@link Reader}s.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class CommandReader extends AbstractExecutionThreadService {
40
41         /** The core being controlled. */
42         private final Core core;
43
44         /** The reader to read commands from. */
45         private final BufferedReader reader;
46
47         /** The writer to write the results to. */
48         private final Writer writer;
49
50         /**
51          * Creates a new command reader.
52          *
53          * @param core
54          *              The core being controlled
55          * @param reader
56          *              The reader to read commands from
57          * @param writer
58          *              The write to write results to
59          */
60         public CommandReader(Core core, Reader reader, Writer writer) {
61                 this.core = core;
62                 this.reader = new BufferedReader(reader);
63                 this.writer = writer;
64         }
65
66         //
67         // ABSTRACTEXECUTIONTHREADSERVICE METHODS
68         //
69
70         @Override
71         protected void run() throws Exception {
72                 String lastLine = "";
73                 String line;
74                 final List<Result> lastResult = Lists.newArrayList();
75                 while ((line = reader.readLine()) != null) {
76                         if (line.equals("")) {
77                                 line = lastLine;
78                         }
79                         String[] words = line.split(" +");
80                         if (words[0].equalsIgnoreCase("search")) {
81                                 lastResult.clear();
82                                 for (Bot bot : core.bots()) {
83                                         for (Pack pack : bot) {
84                                                 boolean found = true;
85                                                 for (int wordIndex = 1; wordIndex < words.length; ++wordIndex) {
86                                                         if (words[wordIndex].startsWith("-") && pack.name().toLowerCase().contains(words[wordIndex].toLowerCase().substring(1))) {
87                                                                 found = false;
88                                                                 break;
89                                                         }
90                                                         if (!words[wordIndex].startsWith("-") && !pack.name().toLowerCase().contains(words[wordIndex].toLowerCase())) {
91                                                                 found = false;
92                                                                 break;
93                                                         }
94                                                 }
95                                                 if (found) {
96                                                         lastResult.add(new Result(bot, pack));
97                                                 }
98                                         }
99                                 }
100                                 Collections.sort(lastResult);
101                                 int counter = 0;
102                                 for (Result result : lastResult) {
103                                         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()));
104                                 }
105                                 writer.write("End of Search.\n");
106                         } else if (words[0].equalsIgnoreCase("dcc")) {
107                                 int counter = 0;
108                                 for (DccReceiver dccReceiver : core.dccReceivers()) {
109                                         writer.write(String.format("[%d] %s (%s, %d%%%s)\n", counter++, dccReceiver.filename(), dccReceiver.size(), dccReceiver.progress() * 100 / dccReceiver.size(), dccReceiver.isRunning() ? "" : ", finished"));
110                                 }
111                                 writer.write("End of DCCs.\n");
112                         }
113
114                         lastLine = line;
115                         writer.flush();
116                 }
117         }
118
119         /** Container for result information. */
120         private static class Result implements Comparable<Result> {
121
122                 /** The bot carrying the pack. */
123                 private final Bot bot;
124
125                 /** The pack. */
126                 private final Pack pack;
127
128                 /**
129                  * Creates a new result.
130                  *
131                  * @param bot
132                  *              The bot carrying the pack
133                  * @param pack
134                  *              The pack
135                  */
136                 private Result(Bot bot, Pack pack) {
137                         this.bot = bot;
138                         this.pack = pack;
139                 }
140
141                 //
142                 // ACCESSORS
143                 //
144
145                 /**
146                  * Returns the bot carrying the pack.
147                  *
148                  * @return The bot carrying the pack
149                  */
150                 public Bot bot() {
151                         return bot;
152                 }
153
154                 /**
155                  * Returns the pack.
156                  *
157                  * @return The pack
158                  */
159                 public Pack pack() {
160                         return pack;
161                 }
162
163                 //
164                 // COMPARABLE METHODS
165                 //
166
167                 @Override
168                 public int compareTo(Result result) {
169                         return pack().name().compareToIgnoreCase(result.pack().name());
170                 }
171
172         }
173
174 }