hebdobot/src/main/java/fr/imirhil/april/hebdobot/irc/Bot.java
2012-09-08 17:34:28 +02:00

215 lines
6.1 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package fr.imirhil.april.hebdobot.irc;
import java.util.Collection;
import java.util.LinkedList;
import org.jibble.pircbot.PircBot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.imirhil.april.hebdobot.Context;
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 {
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>();
public Bot(final String host, final int port, final String name,
final String channel) {
this.host = host;
this.port = port;
this.channel = channel;
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();
}
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) {
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);
}
}
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));
}
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.review = null;
this.sendMessage("% Fin de la revue hebdomadaire");
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) {
LOGGER.debug("Send message : {}", message);
this.sendMessage(this.channel, message);
}
}