diff --git a/src/main/java/net/thauvin/erik/mobibot/Mobibot.java b/src/main/java/net/thauvin/erik/mobibot/Mobibot.java index 0ed1aa7..a7d83c1 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Mobibot.java +++ b/src/main/java/net/thauvin/erik/mobibot/Mobibot.java @@ -258,6 +258,9 @@ public class Mobibot extends PircBot { commands.add(new Comment()); commands.add(new Posting()); commands.add(new Tags()); + // Get the tell command settings + tell = new Tell(this, p.getProperty("tell-max-days"), p.getProperty("tell-max-size")); + commands.add(tell); commands.add(new UrlMgr(p.getProperty("tags", ""), p.getProperty("tags-keywords", ""))); commands.add(new View()); @@ -290,9 +293,6 @@ public class Mobibot extends PircBot { } }); - // Get the tell command settings - tell = new Tell(this, p.getProperty("tell-max-days"), p.getProperty("tell-max-size")); - // Save the entries UrlMgr.saveEntries(this, true); } @@ -624,8 +624,7 @@ public class Mobibot extends PircBot { send(sender, "Type a URL on " + ircChannel + " to post it.", isPrivate); send(sender, "For more information on a specific command, type:", isPrivate); send(sender, - Utils.helpIndent( - ((isPrivate) ? "/msg " + getNick() : getNick() + ':') + " " + Constants.HELP_CMD + " "), + Utils.helpIndent(Utils.botCommand(getNick(), isPrivate) + Constants.HELP_CMD + " "), isPrivate); send(sender, "The commands are:", isPrivate); @@ -702,8 +701,6 @@ public class Mobibot extends PircBot { if (lcTopic.equals(getChannelName())) { send(sender, "To list the last 5 posts from the channel's weblog:", isPrivate); send(sender, Utils.helpIndent(getNick() + ": " + getChannelName()), isPrivate); - } else if (Tell.TELL_CMD.equals(lcTopic) && tell.isEnabled()) { - tell.helpResponse(sender, isPrivate); } else { // Command, Modules or Default if (!helpCommands(sender, topic, isPrivate) && !helpModules(sender, lcTopic, isPrivate)) { @@ -796,8 +793,6 @@ public class Mobibot extends PircBot { helpResponse(sender, args, false); } else if (cmd.equalsIgnoreCase(getChannelName())) { // mobibot: feedResponse(sender); - } else if (cmd.startsWith(Tell.TELL_CMD) && tell.isEnabled()) { // mobibot: tell - tell.response(sender, args, false); } else { boolean skip = false; // Commands @@ -820,8 +815,7 @@ public class Mobibot extends PircBot { } } } - } else { - // Commands + } else { // Commands for (final AbstractCommand command : commands) { if (command.matches(message)) { command.commandResponse(this, sender, login, message, isOp(sender), false); @@ -880,8 +874,6 @@ public class Mobibot extends PircBot { sleep(3); quitServer("The Bot Is Out There!"); System.exit(0); - } else if (Tell.TELL_CMD.equals(cmd) && tell.isEnabled()) { // tell - tell.response(sender, args, true); } else { for (final AbstractCommand command : commands) { if (command.getCommand().startsWith(cmd)) { diff --git a/src/main/java/net/thauvin/erik/mobibot/commands/tell/Tell.java b/src/main/java/net/thauvin/erik/mobibot/commands/tell/Tell.java index 5d7ba31..c42fbf2 100644 --- a/src/main/java/net/thauvin/erik/mobibot/commands/tell/Tell.java +++ b/src/main/java/net/thauvin/erik/mobibot/commands/tell/Tell.java @@ -34,8 +34,10 @@ package net.thauvin.erik.mobibot.commands.tell; import net.thauvin.erik.mobibot.Mobibot; import net.thauvin.erik.mobibot.Utils; +import net.thauvin.erik.mobibot.commands.AbstractCommand; import net.thauvin.erik.mobibot.commands.links.View; import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; @@ -47,7 +49,7 @@ import java.util.concurrent.CopyOnWriteArrayList; * @created 2016-07-02 * @since 1.0 */ -public class Tell { +public class Tell extends AbstractCommand { /** * The tell command. */ @@ -111,8 +113,8 @@ public class Tell { } // Delete message. - private void deleteMessage(final String sender, final String cmds, final boolean isPrivate) { - final String[] split = cmds.split(" "); + private void deleteMessage(final String sender, final String args, final boolean isOp, final boolean isPrivate) { + final String[] split = args.split(" "); if (split.length == 2) { final String id = split[1]; @@ -158,26 +160,65 @@ public class Tell { } } } else { - helpResponse(sender, isPrivate); + helpResponse(bot, args, sender, isOp, isPrivate); } } - /** - * Responds with Constants. - * - * @param sender The sender. - * @param isPrivate The private flag. - */ - public void helpResponse(final String sender, final boolean isPrivate) { - bot.send(sender, "To send a message to someone when they join the channel:", isPrivate); - bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + TELL_CMD + " "), isPrivate); + @NotNull + @Override + public String getCommand() { + return TELL_CMD; + } - bot.send(sender, "To view queued and sent messages:", isPrivate); - bot.send(sender, Utils.helpIndent(bot.getNick() + ": " + TELL_CMD + ' ' + View.VIEW_CMD), isPrivate); + @NotNull + @Override + public List getHelp() { + return List.of("To send a message to someone when they join the channel:", + Utils.helpIndent("%s " + TELL_CMD + " "), + "To view queued and sent messages:", + Utils.helpIndent("%s " + TELL_CMD + ' ' + View.VIEW_CMD), + "Messages are kept for " + Utils.bold(maxDays) + Utils.plural(maxDays, " day.", " days.")); + } - bot.send(sender, - "Messages are kept for " + Utils.bold(maxDays) + Utils.plural(maxDays, " day.", " days."), - isPrivate); + @Override + public boolean isOp() { + return false; + } + + @Override + public boolean isPublic() { + return true; + } + + @Override + public boolean isVisible() { + return true; + } + + @Override + public void commandResponse(@NotNull final Mobibot bot, + @NotNull final String sender, + @NotNull final String login, + @NotNull final String args, + final boolean isOp, + final boolean isPrivate) { + if (StringUtils.isBlank(args)) { + helpResponse(bot, args, sender, isOp, isPrivate); + } else if (args.startsWith(View.VIEW_CMD)) { + if (bot.isOp(sender) && (View.VIEW_CMD + ' ' + TELL_ALL_KEYWORD).equals(args)) { + viewAll(sender, isPrivate); + } else { + viewMessages(sender, isPrivate); + } + } else if (args.startsWith(TELL_DEL_KEYWORD + ' ')) { + deleteMessage(sender, args, isOp, isPrivate); + } else { + newMessage(sender, args, isOp, isPrivate); + } + + if (clean()) { + save(); + } } /** @@ -190,8 +231,8 @@ public class Tell { } // New message. - private void newMessage(final String sender, final String cmds, final boolean isPrivate) { - final String[] split = cmds.split(" ", 2); + private void newMessage(final String sender, final String args, final boolean isOp, final boolean isPrivate) { + final String[] split = args.split(" ", 2); if (split.length == 2 && (StringUtils.isNotBlank(split[1]) && split[1].contains(" "))) { if (messages.size() < maxSize) { @@ -202,39 +243,12 @@ public class Tell { save(); bot.send(sender, "Message [ID " + message.getId() + "] was queued for " - + Utils.bold(message.getRecipient()), true); + + Utils.bold(message.getRecipient()), isPrivate); } else { - bot.send(sender, "Sorry, the messages queue is currently full.", true); + bot.send(sender, "Sorry, the messages queue is currently full.", isPrivate); } } else { - helpResponse(sender, isPrivate); - } - } - - /** - * Processes the commands. - * - * @param sender The sender's nick. - * @param cmds The commands string. - * @param isPrivate The private flag. - */ - public void response(final String sender, final String cmds, final boolean isPrivate) { - if (StringUtils.isBlank(cmds)) { - helpResponse(sender, isPrivate); - } else if (cmds.startsWith(View.VIEW_CMD)) { - if (bot.isOp(sender) && (View.VIEW_CMD + ' ' + TELL_ALL_KEYWORD).equals(cmds)) { - viewAll(sender, isPrivate); - } else { - viewMessages(sender, isPrivate); - } - } else if (cmds.startsWith(TELL_DEL_KEYWORD + ' ')) { - deleteMessage(sender, cmds, isPrivate); - } else { - newMessage(sender, cmds, isPrivate); - } - - if (clean()) { - save(); + helpResponse(bot, args, sender, isOp, isPrivate); } } @@ -360,8 +374,10 @@ public class Tell { } else { bot.send(sender, "To delete one or all delivered messages:", isPrivate); bot.send(sender, - Utils.helpIndent(bot.getNick() + ": " + TELL_CMD + ' ' + TELL_DEL_KEYWORD + " '), isPrivate); + Utils.helpIndent( + Utils.botCommand(bot.getNick(), isPrivate) + TELL_CMD + ' ' + TELL_DEL_KEYWORD + " '), + isPrivate); bot.send(sender, "Messages are kept for " + Utils.bold(maxDays) + Utils.plural(maxDays, " day.", " days."), isPrivate);