Handlers instead of methods
This commit is contained in:
parent
ad5445be52
commit
5895fcc6db
@ -58,7 +58,8 @@ public class Application implements ReviewListener {
|
||||
}
|
||||
|
||||
public static void main(final String[] args) throws Exception {
|
||||
new FileSystemXmlApplicationContext("conf.xml").registerShutdownHook();
|
||||
new FileSystemXmlApplicationContext(System.getProperty("spring.conf",
|
||||
"conf.xml")).registerShutdownHook();
|
||||
new Application();
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package fr.imirhil.april.hebdobot.irc;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
@ -17,13 +18,19 @@ import fr.imirhil.april.hebdobot.review.Review;
|
||||
import fr.imirhil.april.hebdobot.review.Topic;
|
||||
|
||||
public class Bot extends PircBot {
|
||||
private abstract class Handler {
|
||||
public abstract boolean handle(String sender, String message);
|
||||
}
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Bot.class);
|
||||
|
||||
private final String host;
|
||||
private final int port;
|
||||
private final String channel;
|
||||
private Review review = null;
|
||||
private final Collection<ReviewListener> listeners =
|
||||
new LinkedList<ReviewListener>();
|
||||
private final List<Handler> handlers = new LinkedList<Handler>();
|
||||
|
||||
public Bot(final String host, final int port, final String name,
|
||||
final String channel) {
|
||||
@ -36,6 +43,238 @@ public class Bot extends PircBot {
|
||||
public void init() throws Exception {
|
||||
this.connect(this.host, this.port);
|
||||
this.joinChannel(this.channel);
|
||||
|
||||
this.registerHandlers();
|
||||
}
|
||||
|
||||
private void registerHandlers() {
|
||||
// Help
|
||||
this.handlers.add(new Handler() {
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message) {
|
||||
if (!"!help".equalsIgnoreCase(message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Bot.this.sendMessage(sender, "Bienvenue " + sender);
|
||||
Bot.this.sendMessage(
|
||||
sender,
|
||||
"Je suis "
|
||||
+ Bot.this.getName()
|
||||
+ ", le robot de gestion des revues hebdomadaires de l'APRIL");
|
||||
Bot.this.sendMessage(sender,
|
||||
"Voici les commandes que je comprend :");
|
||||
Bot.this.sendMessage(sender, " ");
|
||||
Bot.this.sendMessage(sender,
|
||||
"— !debut : commencer une nouvelle revue");
|
||||
Bot.this.sendMessage(sender,
|
||||
"— !fin : terminer la revue en cours");
|
||||
Bot.this.sendMessage(sender,
|
||||
"— # titre : démarrer un sujet individuel");
|
||||
Bot.this.sendMessage(sender,
|
||||
"— ## titre : démarrer un sujet collectif");
|
||||
Bot.this.sendMessage(sender,
|
||||
"— !courant : affiche le sujet en cours");
|
||||
Bot.this.sendMessage(
|
||||
sender,
|
||||
"— !manquants : affiche les participants qui n'ont pas répondu sur le dernier sujet");
|
||||
Bot.this.sendMessage(sender, "— % message : un commentaire");
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
// Die
|
||||
this.handlers.add(new Handler() {
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message) {
|
||||
if (!"!stop".equalsIgnoreCase(message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Bot.this.review != null) {
|
||||
Bot.this.sendMessage("% Une revue est en cours, arrêt impossible");
|
||||
return false;
|
||||
}
|
||||
|
||||
Context.close();
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
// Start
|
||||
this.handlers.add(new Handler() {
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message) {
|
||||
if (!"!debut".equalsIgnoreCase(message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Bot.this.review = new Review(sender);
|
||||
Bot.this.sendMessage(sender,
|
||||
"Vous êtes le conducteur de réunion");
|
||||
Bot.this.sendMessage(sender,
|
||||
"Pour terminer la réunion, tapez \"!fin\"");
|
||||
Bot.this.sendMessage("% Début de la réunion hebdomadaire");
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
// Stop
|
||||
this.handlers.add(new Handler() {
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message) {
|
||||
if (Bot.this.review == null
|
||||
|| !"!fin".equalsIgnoreCase(message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Bot.this.review.isOwner(sender)) {
|
||||
Bot.this.sendMessage(sender
|
||||
+ ", vous n'êtes pas le conducteur de la réunion");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (final ReviewListener listener : Bot.this.listeners) {
|
||||
listener.onEnd(Bot.this.review);
|
||||
}
|
||||
|
||||
Bot.this.sendMessage("% "
|
||||
+ Bot.this.review.getOwner()
|
||||
+ ", ne pas oublier d'ajouter le compte-rendu de la revue sur https://agir.april.org/issues/135");
|
||||
Bot.this.sendMessage("% Fin de la revue hebdomadaire");
|
||||
Bot.this.review = null;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
// Collective topic, must be before individual topic
|
||||
this.handlers.add(new Handler() {
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message) {
|
||||
if (Bot.this.review == null || !message.matches("\\s*##.*")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Bot.this.review.isOwner(sender)) {
|
||||
Bot.this.sendMessage(sender
|
||||
+ ", vous n'êtes pas le conducteur de la réunion");
|
||||
return false;
|
||||
}
|
||||
|
||||
final CollectiveTopic topic =
|
||||
new CollectiveTopic(message.replaceFirst("##", "")
|
||||
.trim());
|
||||
Bot.this.review.begin(topic);
|
||||
Bot.this.sendMessage("Sujet collectif : " + topic.getTitle());
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
// Individual topic
|
||||
this.handlers.add(new Handler() {
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message) {
|
||||
if (Bot.this.review == null || !message.matches("\\s*#[^#].*")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Bot.this.review.isOwner(sender)) {
|
||||
Bot.this.sendMessage(sender
|
||||
+ ", vous n'êtes pas le conducteur de la réunion");
|
||||
return false;
|
||||
}
|
||||
|
||||
final IndividualTopic topic =
|
||||
new IndividualTopic(message.replaceFirst("#", "")
|
||||
.trim());
|
||||
Bot.this.review.begin(topic);
|
||||
Bot.this.sendMessage("Sujet individuel : " + topic.getTitle());
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
// Missing
|
||||
this.handlers.add(new Handler() {
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message) {
|
||||
if (Bot.this.review == null
|
||||
|| !"!manquants".equalsIgnoreCase(message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final Topic topic = Bot.this.review.getCurrentTopic();
|
||||
if (topic == null) {
|
||||
Bot.this.sendMessage("Aucun sujet traité");
|
||||
return true;
|
||||
}
|
||||
|
||||
final Collection<String> participants =
|
||||
Bot.this.review.getParticipants();
|
||||
final Collection<String> currentParticipants =
|
||||
topic.getParticipants();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
final Collection<String> missing =
|
||||
CollectionUtils.subtract(participants,
|
||||
currentParticipants);
|
||||
if (missing.isEmpty()) {
|
||||
Bot.this.sendMessage("Aucun participant manquant \\o/");
|
||||
return true;
|
||||
}
|
||||
|
||||
Bot.this.sendMessage(String.format(
|
||||
"Les participants suivants sont manquants : %1s",
|
||||
StringUtils.join(missing, ", ")));
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
// Current
|
||||
this.handlers.add(new Handler() {
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message) {
|
||||
if (Bot.this.review == null
|
||||
|| !"!courant".equalsIgnoreCase(message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final Topic current = Bot.this.review.getCurrentTopic();
|
||||
if (current == null) {
|
||||
Bot.this.sendMessage("% Pas de sujet en cours");
|
||||
} else if (current instanceof IndividualTopic) {
|
||||
Bot.this.sendMessage("% Sujet individuel en cours : "
|
||||
+ current.getTitle());
|
||||
} else if (current instanceof CollectiveTopic) {
|
||||
Bot.this.sendMessage("% Sujet collectif en cours : "
|
||||
+ current.getTitle());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
// Topic message
|
||||
this.handlers.add(new Handler() {
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message) {
|
||||
if (Bot.this.review == null || message.startsWith("%")) {
|
||||
return false;
|
||||
}
|
||||
Bot.this.review.add(new Message(sender, message));
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
// All the other
|
||||
this.handlers.add(new Handler() {
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message) {
|
||||
if (Bot.this.review == null) {
|
||||
return false;
|
||||
}
|
||||
Bot.this.review.addRaw(new Message(sender, message));
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void close() {
|
||||
@ -53,197 +292,17 @@ public class Bot extends PircBot {
|
||||
LOGGER.debug(
|
||||
"Message received - channel : {}, sender : {}, message : {}",
|
||||
new Object[] { channel, sender, message });
|
||||
|
||||
if (!channel.equalsIgnoreCase(this.channel)) {
|
||||
return;
|
||||
}
|
||||
|
||||
message = message.trim();
|
||||
|
||||
this.handleDie(sender, message);
|
||||
this.handleStartReview(sender, message);
|
||||
this.handleStopReview(sender, message);
|
||||
if (!this.handleStartIndividualTopic(sender, message)) {
|
||||
if (!this.handleStartCollectiveTopic(sender, message)) {
|
||||
this.handleMessage(sender, message);
|
||||
for (final Handler handler : this.handlers) {
|
||||
if (handler.handle(sender, message)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.handleManquants(sender, message);
|
||||
this.handleComment(sender, message);
|
||||
this.handleDisplayCurrent(sender, message);
|
||||
this.handleHelp(sender, message);
|
||||
}
|
||||
|
||||
private void handleHelp(final String sender, final String message) {
|
||||
if (!"!help".equalsIgnoreCase(message)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.sendMessage(sender, "Bienvenue " + sender);
|
||||
this.sendMessage(sender, "Je suis " + this.getName()
|
||||
+ ", le robot de gestion des revues hebdomadaires de l'APRIL");
|
||||
this.sendMessage(sender, "Voici les commandes que je comprend :");
|
||||
this.sendMessage(sender, " ");
|
||||
this.sendMessage(sender, "— !debut : commencer une nouvelle revue");
|
||||
this.sendMessage(sender, "— !fin : terminer la revue en cours");
|
||||
this.sendMessage(sender, "— # titre : démarrer un sujet individuel");
|
||||
this.sendMessage(sender, "— ## titre : démarrer un sujet collectif");
|
||||
this.sendMessage(sender, "— !courant : affiche le sujet en cours");
|
||||
this.sendMessage(
|
||||
sender,
|
||||
"— !manquants : affiche les participants qui n'ont pas répondu sur le dernier sujet");
|
||||
this.sendMessage(sender, "— % message : un commentaire");
|
||||
}
|
||||
|
||||
private boolean handleMessage(final String sender, final String message) {
|
||||
if (this.review == null || message.startsWith("%")) {
|
||||
return false;
|
||||
}
|
||||
this.review.add(new Message(sender, message));
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean handleManquants(final String sender, final String message) {
|
||||
if (this.review == null || !"!manquants".equalsIgnoreCase(message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final Topic topic = this.review.getCurrentTopic();
|
||||
if (topic == null) {
|
||||
this.sendMessage("Aucun sujet traité");
|
||||
return true;
|
||||
}
|
||||
|
||||
final Collection<String> participants = this.review.getParticipants();
|
||||
final Collection<String> currentParticipants = topic.getParticipants();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
final Collection<String> missing =
|
||||
CollectionUtils.subtract(participants, currentParticipants);
|
||||
if (missing.isEmpty()) {
|
||||
this.sendMessage("Aucun participant manquant \\o/");
|
||||
return true;
|
||||
}
|
||||
|
||||
this.sendMessage(String.format(
|
||||
"Les participants suivants sont manquants : %1s",
|
||||
StringUtils.join(missing, ", ")));
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean handleComment(final String sender, final String message) {
|
||||
if (this.review == null) {
|
||||
return false;
|
||||
}
|
||||
this.review.addRaw(new Message(sender, message));
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean handleDie(final String sender, final String message) {
|
||||
if (!"!stop".equalsIgnoreCase(message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.review != null) {
|
||||
this.sendMessage("% Une revue est en cours, arrêt impossible");
|
||||
return false;
|
||||
}
|
||||
|
||||
Context.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean
|
||||
handleStartReview(final String sender, final String message) {
|
||||
if (!"!debut".equalsIgnoreCase(message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.review = new Review(sender);
|
||||
this.sendMessage(sender, "Vous êtes le conducteur de réunion");
|
||||
this.sendMessage(sender, "Pour terminer la réunion, tapez \"!fin\"");
|
||||
this.sendMessage("% Début de la réunion hebdomadaire");
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean handleStopReview(final String sender, final String message) {
|
||||
if (this.review == null || !"!fin".equalsIgnoreCase(message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this.review.isOwner(sender)) {
|
||||
this.sendMessage(sender
|
||||
+ ", vous n'êtes pas le conducteur de la réunion");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (final ReviewListener listener : this.listeners) {
|
||||
listener.onEnd(this.review);
|
||||
}
|
||||
|
||||
this.sendMessage("% "
|
||||
+ this.review.getOwner()
|
||||
+ ", ne pas oublier d'ajouter le compte-rendu de la revue sur https://agir.april.org/issues/135");
|
||||
this.sendMessage("% Fin de la revue hebdomadaire");
|
||||
this.review = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean handleStartIndividualTopic(final String sender,
|
||||
final String message) {
|
||||
if (this.review == null || !message.matches("\\s*#[^#].*")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this.review.isOwner(sender)) {
|
||||
this.sendMessage(sender
|
||||
+ ", vous n'êtes pas le conducteur de la réunion");
|
||||
return false;
|
||||
}
|
||||
|
||||
final IndividualTopic topic =
|
||||
new IndividualTopic(message.replaceFirst("#", "").trim());
|
||||
this.review.begin(topic);
|
||||
this.sendMessage("Sujet individuel : " + topic.getTitle());
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean handleStartCollectiveTopic(final String sender,
|
||||
final String message) {
|
||||
if (this.review == null || !message.matches("\\s*##.*")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this.review.isOwner(sender)) {
|
||||
this.sendMessage(sender
|
||||
+ ", vous n'êtes pas le conducteur de la réunion");
|
||||
return false;
|
||||
}
|
||||
|
||||
final CollectiveTopic topic =
|
||||
new CollectiveTopic(message.replaceFirst("##", "").trim());
|
||||
this.review.begin(topic);
|
||||
this.sendMessage("Sujet collectif : " + topic.getTitle());
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean handleDisplayCurrent(final String sender,
|
||||
final String message) {
|
||||
if (this.review == null || !"!courant".equalsIgnoreCase(message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final Topic current = this.review.getCurrentTopic();
|
||||
if (current == null) {
|
||||
this.sendMessage("% Pas de sujet en cours");
|
||||
} else if (current instanceof IndividualTopic) {
|
||||
this.sendMessage("% Sujet individuel en cours : "
|
||||
+ current.getTitle());
|
||||
} else if (current instanceof CollectiveTopic) {
|
||||
this.sendMessage("% Sujet collectif en cours : "
|
||||
+ current.getTitle());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void sendMessage(final String message) {
|
||||
|
@ -58,6 +58,7 @@ public class Review {
|
||||
this.participants.add(message.getAuthor());
|
||||
this.currentTopic.add(message);
|
||||
}
|
||||
this.addRaw(message);
|
||||
}
|
||||
|
||||
public void addRaw(final Message message) {
|
||||
|
Loading…
Reference in New Issue
Block a user