- if (words[0].equalsIgnoreCase("search")) {
- lastResult.clear();
- for (Bot bot : core.bots()) {
- for (Pack pack : bot) {
- boolean found = true;
- for (int wordIndex = 1; wordIndex < words.length; ++wordIndex) {
- if (words[wordIndex].startsWith("-") && pack.name().toLowerCase().contains(words[wordIndex].toLowerCase().substring(1))) {
- found = false;
- break;
- }
- if (!words[wordIndex].startsWith("-") && !pack.name().toLowerCase().contains(words[wordIndex].toLowerCase())) {
- found = false;
- break;
- }
- }
- if (found) {
- lastResult.add(new Result(bot, pack));
- }
- }
- }
- Collections.sort(lastResult);
- int counter = 0;
- for (Result result : lastResult) {
- 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()));
- }
- writer.write("End of Search.\n");
- } else if (words[0].equalsIgnoreCase("dcc")) {
- int counter = 0;
- for (DccReceiver dccReceiver : core.dccReceivers()) {
- writer.write(String.format("[%d] %s (%s, %d%%%s)\n", counter++, dccReceiver.filename(), dccReceiver.size(), dccReceiver.progress() * 100 / dccReceiver.size(), dccReceiver.isRunning() ? "" : ", finished"));
- }
- writer.write("End of DCCs.\n");
- } else if (words[0].equalsIgnoreCase("get")) {
- Integer index = Ints.tryParse(words[1]);
- if ((index != null) && (index < lastResult.size())) {
- core.fetch(lastResult.get(index).bot(), lastResult.get(index).pack());
- }
- } else if (words[0].equalsIgnoreCase("stats")) {
- int configuredChannelsCount = core.channels().size();
- int joinedChannelsCount = core.joinedChannels().size();
- int extraChannelsCount = core.extraChannels().size();
- Collection<Bot> bots = core.bots();
- Set<String> packNames = Sets.newHashSet();
- int packsCount = 0;
- for (Bot bot : bots) {
- packsCount += bot.packs().size();
- for (Pack pack : bot) {
- packNames.add(pack.name());
- }
- }
-
- 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()));
+ String commandName = words[0];
+ Collection<Command> eligibleCommands = findEligibleCommands(commandName);
+ if (eligibleCommands.isEmpty()) {
+ writeLine(String.format("Invalid command: %s, valid: %s.", commandName, Joiner.on(' ').join(from(commands).transform(TO_NAME))));
+ } else if (eligibleCommands.size() > 1) {
+ writeLine(String.format("Commands: %s.", Joiner.on(' ').join(from(eligibleCommands).transform(TO_NAME))));
+ } else {
+ Command command = eligibleCommands.iterator().next();
+ List<String> parameters = from(asList(words)).skip(1).toList();
+ state = command.execute(state, parameters, writer);