2011-09-30 00:35:49 +02:00
|
|
|
|
package fr.imirhil.april.hebdobot.irc;
|
|
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
|
|
|
|
|
import org.jibble.pircbot.PircBot;
|
2012-09-08 17:34:28 +02:00
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
|
2012-02-24 14:08:07 +01:00
|
|
|
|
import fr.imirhil.april.hebdobot.Context;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
import fr.imirhil.april.hebdobot.review.CollectiveTopic;
|
|
|
|
|
import fr.imirhil.april.hebdobot.review.IndividualTopic;
|
|
|
|
|
import fr.imirhil.april.hebdobot.review.Message;
|
|
|
|
|
import fr.imirhil.april.hebdobot.review.Review;
|
|
|
|
|
import fr.imirhil.april.hebdobot.review.Topic;
|
|
|
|
|
|
|
|
|
|
public class Bot extends PircBot {
|
2012-09-08 17:34:28 +02:00
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(Bot.class);
|
2011-12-11 15:02:17 +01:00
|
|
|
|
private final String host;
|
|
|
|
|
private final int port;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
private final String channel;
|
2011-12-11 15:02:17 +01:00
|
|
|
|
private Review review = null;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
private final Collection<ReviewListener> listeners =
|
|
|
|
|
new LinkedList<ReviewListener>();
|
|
|
|
|
|
2011-12-11 15:02:17 +01:00
|
|
|
|
public Bot(final String host, final int port, final String name,
|
|
|
|
|
final String channel) {
|
|
|
|
|
this.host = host;
|
|
|
|
|
this.port = port;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
this.channel = channel;
|
2011-12-11 15:02:17 +01:00
|
|
|
|
this.setName(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void init() throws Exception {
|
|
|
|
|
this.connect(this.host, this.port);
|
|
|
|
|
this.joinChannel(this.channel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void close() {
|
|
|
|
|
this.disconnect();
|
|
|
|
|
this.dispose();
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void add(final ReviewListener listener) {
|
|
|
|
|
this.listeners.add(listener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onMessage(final String channel, final String sender,
|
|
|
|
|
final String login, final String hostname, String message) {
|
2012-09-08 17:34:28 +02:00
|
|
|
|
LOGGER.debug(
|
|
|
|
|
"Message received - channel : {}, sender : {}, message : {}",
|
|
|
|
|
new Object[] { channel, sender, message });
|
|
|
|
|
|
2011-09-30 00:35:49 +02:00
|
|
|
|
if (!channel.equalsIgnoreCase(this.channel)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message = message.trim();
|
|
|
|
|
|
|
|
|
|
this.handleDie(sender, message);
|
|
|
|
|
this.handleStartReview(sender, message);
|
|
|
|
|
this.handleStopReview(sender, message);
|
2011-11-05 15:27:38 +01:00
|
|
|
|
if (!this.handleStartIndividualTopic(sender, message)) {
|
|
|
|
|
if (!this.handleStartCollectiveTopic(sender, message)) {
|
|
|
|
|
this.handleMessage(sender, message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-09-30 00:35:49 +02:00
|
|
|
|
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, "— % message : un commentaire");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleMessage(final String sender, final String message) {
|
|
|
|
|
if (this.review == null || message.startsWith("%")) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.review.add(new Message(sender, message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleComment(final String sender, final String message) {
|
|
|
|
|
if (this.review == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.review.addRaw(new Message(sender, message));
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-05 15:27:38 +01:00
|
|
|
|
private boolean handleDie(final String sender, final String message) {
|
2011-09-30 00:35:49 +02:00
|
|
|
|
if (!"!stop".equalsIgnoreCase(message)) {
|
2011-11-05 15:27:38 +01:00
|
|
|
|
return false;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-08 17:34:28 +02:00
|
|
|
|
if (this.review != null) {
|
|
|
|
|
this.sendMessage("% Une revue est en cours, arrêt impossible");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-24 14:08:07 +01:00
|
|
|
|
Context.close();
|
2011-11-05 15:27:38 +01:00
|
|
|
|
return true;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-05 15:27:38 +01:00
|
|
|
|
private boolean
|
|
|
|
|
handleStartReview(final String sender, final String message) {
|
2011-09-30 00:35:49 +02:00
|
|
|
|
if (!"!debut".equalsIgnoreCase(message)) {
|
2011-11-05 15:27:38 +01:00
|
|
|
|
return false;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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\"");
|
2012-09-08 17:34:28 +02:00
|
|
|
|
this.sendMessage("% Début de la réunion hebdomadaire");
|
2011-11-05 15:27:38 +01:00
|
|
|
|
return true;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-05 15:27:38 +01:00
|
|
|
|
private boolean handleStopReview(final String sender, final String message) {
|
2011-09-30 00:35:49 +02:00
|
|
|
|
if (this.review == null || !"!fin".equalsIgnoreCase(message)) {
|
2011-11-05 15:27:38 +01:00
|
|
|
|
return false;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.review.isOwner(sender)) {
|
|
|
|
|
this.sendMessage(sender
|
|
|
|
|
+ ", vous n'êtes pas le conducteur de la réunion");
|
2011-11-05 15:27:38 +01:00
|
|
|
|
return false;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (final ReviewListener listener : this.listeners) {
|
|
|
|
|
listener.onEnd(this.review);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-24 22:53:33 +01:00
|
|
|
|
this.sendMessage("% "
|
|
|
|
|
+ this.review.getOwner()
|
|
|
|
|
+ ", ne pas oublier d'ajouter le compte-rendu de la revue sur https://agir.april.org/issues/135");
|
2012-09-08 17:34:28 +02:00
|
|
|
|
this.sendMessage("% Fin de la revue hebdomadaire");
|
2012-11-24 22:53:33 +01:00
|
|
|
|
this.review = null;
|
2011-11-05 15:27:38 +01:00
|
|
|
|
return true;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-05 15:27:38 +01:00
|
|
|
|
private boolean handleStartIndividualTopic(final String sender,
|
2011-09-30 00:35:49 +02:00
|
|
|
|
final String message) {
|
|
|
|
|
if (this.review == null || !message.matches("\\s*#[^#].*")) {
|
2011-11-05 15:27:38 +01:00
|
|
|
|
return false;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.review.isOwner(sender)) {
|
|
|
|
|
this.sendMessage(sender
|
|
|
|
|
+ ", vous n'êtes pas le conducteur de la réunion");
|
2011-11-05 15:27:38 +01:00
|
|
|
|
return false;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final IndividualTopic topic =
|
|
|
|
|
new IndividualTopic(message.replaceFirst("#", "").trim());
|
|
|
|
|
this.review.begin(topic);
|
|
|
|
|
this.sendMessage("Sujet individuel : " + topic.getTitle());
|
2011-11-05 15:27:38 +01:00
|
|
|
|
return true;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-05 15:27:38 +01:00
|
|
|
|
private boolean handleStartCollectiveTopic(final String sender,
|
2011-09-30 00:35:49 +02:00
|
|
|
|
final String message) {
|
|
|
|
|
if (this.review == null || !message.matches("\\s*##.*")) {
|
2011-11-05 15:27:38 +01:00
|
|
|
|
return false;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.review.isOwner(sender)) {
|
|
|
|
|
this.sendMessage(sender
|
|
|
|
|
+ ", vous n'êtes pas le conducteur de la réunion");
|
2011-11-05 15:27:38 +01:00
|
|
|
|
return false;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final CollectiveTopic topic =
|
|
|
|
|
new CollectiveTopic(message.replaceFirst("##", "").trim());
|
|
|
|
|
this.review.begin(topic);
|
|
|
|
|
this.sendMessage("Sujet collectif : " + topic.getTitle());
|
2011-11-05 15:27:38 +01:00
|
|
|
|
return true;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-05 15:27:38 +01:00
|
|
|
|
private boolean handleDisplayCurrent(final String sender,
|
|
|
|
|
final String message) {
|
2011-09-30 00:35:49 +02:00
|
|
|
|
if (this.review == null || !"!courant".equalsIgnoreCase(message)) {
|
2011-11-05 15:27:38 +01:00
|
|
|
|
return false;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final Topic current = this.review.getCurrentTopic();
|
|
|
|
|
if (current == null) {
|
2012-09-08 17:34:28 +02:00
|
|
|
|
this.sendMessage("% Pas de sujet en cours");
|
2011-09-30 00:35:49 +02:00
|
|
|
|
} else if (current instanceof IndividualTopic) {
|
2012-09-08 17:34:28 +02:00
|
|
|
|
this.sendMessage("% Sujet individuel en cours : "
|
2011-09-30 00:35:49 +02:00
|
|
|
|
+ current.getTitle());
|
|
|
|
|
} else if (current instanceof CollectiveTopic) {
|
2012-09-08 17:34:28 +02:00
|
|
|
|
this.sendMessage("% Sujet collectif en cours : "
|
|
|
|
|
+ current.getTitle());
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
2011-11-05 15:27:38 +01:00
|
|
|
|
return true;
|
2011-09-30 00:35:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void sendMessage(final String message) {
|
2012-09-08 17:34:28 +02:00
|
|
|
|
LOGGER.debug("Send message : {}", message);
|
2011-09-30 00:35:49 +02:00
|
|
|
|
this.sendMessage(this.channel, message);
|
|
|
|
|
}
|
|
|
|
|
}
|