diff --git a/src/org/april/hebdobot/bot/Hebdobot.java b/src/org/april/hebdobot/bot/Hebdobot.java index bb1020d..93c2bd0 100644 --- a/src/org/april/hebdobot/bot/Hebdobot.java +++ b/src/org/april/hebdobot/bot/Hebdobot.java @@ -27,6 +27,7 @@ import java.time.LocalTime; import org.apache.commons.lang3.StringUtils; import org.april.hebdobot.HebdobotException; import org.april.hebdobot.bot.hooks.BadCommandHook; +import org.april.hebdobot.bot.hooks.CancelPreviousInputHook; import org.april.hebdobot.bot.hooks.CollectiveSubjectHook; import org.april.hebdobot.bot.hooks.CommentHook; import org.april.hebdobot.bot.hooks.CurrentHook; @@ -147,6 +148,7 @@ public class Hebdobot extends PircBot this.hooker.add(new StopReviewHook()); this.hooker.add(new StatsHook()); this.hooker.add(new StatusHook()); + this.hooker.add(new CancelPreviousInputHook()); this.hooker.add(new DateHook()); this.hooker.add(new HelloHook()); diff --git a/src/org/april/hebdobot/bot/hooks/CancelPreviousInputHook.java b/src/org/april/hebdobot/bot/hooks/CancelPreviousInputHook.java new file mode 100644 index 0000000..4416a0c --- /dev/null +++ b/src/org/april/hebdobot/bot/hooks/CancelPreviousInputHook.java @@ -0,0 +1,82 @@ +/** + * Copyright (C) 2019-2021 Christian Pierre MOMON + * + * This file is part of (April) Hebdobot. + * + * Hebdobot is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Hebdobot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Hebdobot. If not, see + */ +package org.april.hebdobot.bot.hooks; + +import org.apache.commons.lang3.StringUtils; +import org.april.hebdobot.bot.Hebdobot; +import org.april.hebdobot.bot.review.Topic; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Class CancelPreviousInputHook. + */ +public class CancelPreviousInputHook extends Hook +{ + private static final Logger logger = LoggerFactory.getLogger(CancelPreviousInputHook.class); + + /* (non-Javadoc) + * @see org.april.hebdobot.bot.hooks.Hook#attemptProcess(org.april.hebdobot.bot.Hebdobot, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public boolean attemptProcess(final Hebdobot bot, final String channel, final String sender, final String login, final String hostname, + final String message) + { + boolean result; + + if (StringUtils.equalsIgnoreCase(message, "!manquants")) + { + logger.info("!manquants caught."); + + // Missing. + if (bot.getReview() == null) + { + bot.sendMessage(sender + ", pas de revue en cours."); + } + else + { + Topic topic = bot.getReview().getCurrentTopic(); + if (topic == null) + { + bot.sendMessage("Pas de sujet en cours."); + } + else + { + if (bot.getReview().getParticipants().contains(sender)) + { + topic.cancelPreviousMessage(sender); + } + else + { + bot.sendMessage("Vous n'avez pas d'entrée en cours."); + } + } + } + + result = true; + } + else + { + result = false; + } + + // + return result; + } +} diff --git a/src/org/april/hebdobot/bot/review/CollectiveTopic.java b/src/org/april/hebdobot/bot/review/CollectiveTopic.java index 63a85fb..f81671d 100644 --- a/src/org/april/hebdobot/bot/review/CollectiveTopic.java +++ b/src/org/april/hebdobot/bot/review/CollectiveTopic.java @@ -52,6 +52,19 @@ public class CollectiveTopic extends Topic this.messages.add(message); } + /* (non-Javadoc) + * @see org.april.hebdobot.bot.review.Topic#cancelPrevious(java.lang.String) + */ + @Override + public void cancelPreviousMessage(final String author) + { + Message previousMessage = this.messages.getByAuthor(author).getLast(); + if (previousMessage != null) + { + this.messages.remove(previousMessage); + } + } + /** * Gets the messages. * diff --git a/src/org/april/hebdobot/bot/review/IndividualTopic.java b/src/org/april/hebdobot/bot/review/IndividualTopic.java index 6e0a7e0..556e6c4 100644 --- a/src/org/april/hebdobot/bot/review/IndividualTopic.java +++ b/src/org/april/hebdobot/bot/review/IndividualTopic.java @@ -58,6 +58,19 @@ public class IndividualTopic extends Topic this.messages.get(author).add(message); } + /* (non-Javadoc) + * @see org.april.hebdobot.bot.review.Topic#cancelPrevious(java.lang.String) + */ + @Override + public void cancelPreviousMessage(final String participant) + { + Messages authorMessages = this.messages.get(participant); + if (authorMessages != null) + { + authorMessages.removeLast(); + } + } + /** * Gets the messages. * diff --git a/src/org/april/hebdobot/bot/review/Messages.java b/src/org/april/hebdobot/bot/review/Messages.java index 432eb2e..906edbc 100644 --- a/src/org/april/hebdobot/bot/review/Messages.java +++ b/src/org/april/hebdobot/bot/review/Messages.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2017-2018 Christian Pierre MOMON + * Copyright (C) 2017-2019 Christian Pierre MOMON * * This file is part of (April) Hebdobot. * @@ -20,6 +20,8 @@ package org.april.hebdobot.bot.review; import java.util.LinkedList; +import org.apache.commons.lang3.StringUtils; + /** * The Class MessageLinkedList. */ @@ -34,4 +36,29 @@ public class Messages extends LinkedList { super(); } + + /** + * Gets the by author. + * + * @param author + * the author + * @return the by author + */ + public Messages getByAuthor(final String author) + { + Messages result; + + result = new Messages(); + + for (Message message : this) + { + if (StringUtils.equals(message.getAuthor(), author)) + { + result.add(message); + } + } + + // + return result; + } } diff --git a/src/org/april/hebdobot/bot/review/Topic.java b/src/org/april/hebdobot/bot/review/Topic.java index 135c6d5..1737020 100644 --- a/src/org/april/hebdobot/bot/review/Topic.java +++ b/src/org/april/hebdobot/bot/review/Topic.java @@ -47,6 +47,14 @@ public abstract class Topic */ public abstract void add(Message message); + /** + * Cancel previous. + * + * @param participant + * the participant + */ + public abstract void cancelPreviousMessage(final String participant); + /** * Gets the participants. *