Replaced handler with if else way.
This commit is contained in:
parent
ca0f25afb2
commit
84365660e0
@ -1,37 +0,0 @@
|
||||
/**
|
||||
* Copyright (C) 2011-2013,2017 Nicolas Vinot <aeris@imirhil.fr>
|
||||
* Copyright (C) 2017 Christian Pierre MOMON <cmomon@april.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package org.april.hebdobot.irc;
|
||||
|
||||
/**
|
||||
* The Class Handler.
|
||||
*/
|
||||
public abstract class Handler
|
||||
{
|
||||
/**
|
||||
* Handle.
|
||||
*
|
||||
* @param sender
|
||||
* the sender
|
||||
* @param message
|
||||
* the message
|
||||
* @return true, if successful
|
||||
*/
|
||||
public abstract boolean handle(String sender, String message);
|
||||
}
|
@ -20,9 +20,7 @@
|
||||
package org.april.hebdobot.irc;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
@ -48,7 +46,6 @@ public class Hebdobot extends PircBot
|
||||
private final String channel;
|
||||
private Review review = null;
|
||||
private final Collection<ReviewListener> listeners;
|
||||
private final List<Handler> handlers;
|
||||
|
||||
/**
|
||||
* Instantiates a new bot.
|
||||
@ -70,7 +67,6 @@ public class Hebdobot extends PircBot
|
||||
this.setName(name);
|
||||
|
||||
this.listeners = new LinkedList<ReviewListener>();
|
||||
this.handlers = new LinkedList<Handler>();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -103,410 +99,200 @@ public class Hebdobot extends PircBot
|
||||
{
|
||||
this.connect(this.host, this.port);
|
||||
this.joinChannel(this.channel);
|
||||
|
||||
this.registerHandlers();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.jibble.pircbot.PircBot#onMessage(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
protected void onMessage(final String channel, final String sender, final String login, final String hostname, String message)
|
||||
protected void onMessage(final String channel, final String sender, final String login, final String hostname, final String message)
|
||||
{
|
||||
logger.debug("Message received - channel : {}, sender : {}, message : {}", channel, sender, message);
|
||||
if (channel.equalsIgnoreCase(this.channel))
|
||||
{
|
||||
message = message.trim();
|
||||
String text = message.trim();
|
||||
|
||||
boolean ended = false;
|
||||
Iterator<Handler> iterator = this.handlers.iterator();
|
||||
while (!ended)
|
||||
if (StringUtils.equalsIgnoreCase(text, "!help"))
|
||||
{
|
||||
if (iterator.hasNext())
|
||||
{
|
||||
Handler currentHandler = iterator.next();
|
||||
|
||||
if (currentHandler.handle(sender, message))
|
||||
{
|
||||
ended = true;
|
||||
// Help.
|
||||
sendMessage(sender, "Bienvenue " + sender);
|
||||
sendMessage(sender, "Je suis " + getName() + ", le robot de gestion des revues hebdomadaires de l'APRIL");
|
||||
sendMessage(sender, "Voici les commandes que je comprend :");
|
||||
sendMessage(sender, " ");
|
||||
sendMessage(sender, "— !debut : commencer une nouvelle revue");
|
||||
sendMessage(sender, "— !fin : terminer la revue en cours");
|
||||
sendMessage(sender, "— # titre : démarrer un sujet individuel");
|
||||
sendMessage(sender, "— ## titre : démarrer un sujet collectif");
|
||||
sendMessage(sender, "— !courant : affiche le sujet en cours");
|
||||
sendMessage(sender, "— !manquants : affiche les participants qui n'ont pas répondu sur le dernier sujet");
|
||||
sendMessage(sender, "— % message : un commentaire");
|
||||
}
|
||||
}
|
||||
else
|
||||
else if (StringUtils.equalsIgnoreCase(text, "!stop"))
|
||||
{
|
||||
ended = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register handlers.
|
||||
*/
|
||||
private void registerHandlers()
|
||||
{
|
||||
// Help
|
||||
this.handlers.add(new Handler()
|
||||
{
|
||||
/* (non-Javadoc)
|
||||
* @see org.april.hebdobot.irc.Bot.Handler#handle(java.lang.String, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (StringUtils.equalsIgnoreCase(message, "!help"))
|
||||
{
|
||||
Hebdobot.this.sendMessage(sender, "Bienvenue " + sender);
|
||||
Hebdobot.this.sendMessage(sender,
|
||||
"Je suis " + Hebdobot.this.getName() + ", le robot de gestion des revues hebdomadaires de l'APRIL");
|
||||
Hebdobot.this.sendMessage(sender, "Voici les commandes que je comprend :");
|
||||
Hebdobot.this.sendMessage(sender, " ");
|
||||
Hebdobot.this.sendMessage(sender, "— !debut : commencer une nouvelle revue");
|
||||
Hebdobot.this.sendMessage(sender, "— !fin : terminer la revue en cours");
|
||||
Hebdobot.this.sendMessage(sender, "— # titre : démarrer un sujet individuel");
|
||||
Hebdobot.this.sendMessage(sender, "— ## titre : démarrer un sujet collectif");
|
||||
Hebdobot.this.sendMessage(sender, "— !courant : affiche le sujet en cours");
|
||||
Hebdobot.this.sendMessage(sender, "— !manquants : affiche les participants qui n'ont pas répondu sur le dernier sujet");
|
||||
Hebdobot.this.sendMessage(sender, "— % message : un commentaire");
|
||||
result = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
// Die
|
||||
this.handlers.add(new Handler()
|
||||
{
|
||||
/* (non-Javadoc)
|
||||
* @see org.april.hebdobot.irc.Bot.Handler#handle(java.lang.String, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (!StringUtils.equalsIgnoreCase(message, "!stop"))
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else if (Hebdobot.this.review != null)
|
||||
{
|
||||
Hebdobot.this.sendMessage("% Une revue est en cours, arrêt impossible");
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
// Die.
|
||||
if (this.review == null)
|
||||
{
|
||||
Context.close();
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
// Start
|
||||
this.handlers.add(new Handler()
|
||||
{
|
||||
/* (non-Javadoc)
|
||||
* @see org.april.hebdobot.irc.Bot.Handler#handle(java.lang.String, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (!StringUtils.equalsIgnoreCase(message, "!debut"))
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Hebdobot.this.review = new Review(sender);
|
||||
Hebdobot.this.sendMessage(sender, "Vous êtes le conducteur de réunion");
|
||||
Hebdobot.this.sendMessage(sender, "Pour terminer la réunion, tapez \"!fin\"");
|
||||
Hebdobot.this.sendMessage("% Début de la réunion hebdomadaire");
|
||||
Hebdobot.this.sendMessage(
|
||||
"% rappel : toute ligne commençant par % sera considérée comme un commentaire et non prise en compte dans la synthèse");
|
||||
result = true;
|
||||
sendMessage("% Une revue est en cours, arrêt impossible");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
// Stop
|
||||
this.handlers.add(new Handler()
|
||||
else if ((StringUtils.equalsIgnoreCase(text, "!debut")) || (StringUtils.equalsIgnoreCase(text, "!début")))
|
||||
{
|
||||
/* (non-Javadoc)
|
||||
* @see org.april.hebdobot.irc.Bot.Handler#handle(java.lang.String, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message)
|
||||
// Start.
|
||||
this.review = new Review(sender);
|
||||
sendMessage(sender, "Vous êtes le conducteur de réunion");
|
||||
sendMessage(sender, "Pour terminer la réunion, tapez \"!fin\"");
|
||||
sendMessage("% Début de la réunion hebdomadaire");
|
||||
sendMessage("% rappel : toute ligne commençant par % sera considérée comme un commentaire et non prise en compte dans la synthèse");
|
||||
}
|
||||
else if (StringUtils.equalsIgnoreCase(text, "!fin"))
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (Hebdobot.this.review == null || !StringUtils.equalsIgnoreCase(message, "!fin"))
|
||||
// Stop.
|
||||
if (this.review != null)
|
||||
{
|
||||
result = false;
|
||||
sendMessage(sender + ", pas de revue en cours.");
|
||||
}
|
||||
else if (!this.review.isOwner(sender))
|
||||
{
|
||||
sendMessage(sender + ", vous n'êtes pas le conducteur de la réunion");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!Hebdobot.this.review.isOwner(sender))
|
||||
for (final ReviewListener listener : this.listeners)
|
||||
{
|
||||
Hebdobot.this.sendMessage(sender + ", vous n'êtes pas le conducteur de la réunion");
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (final ReviewListener listener : Hebdobot.this.listeners)
|
||||
{
|
||||
listener.onEnd(Hebdobot.this.review);
|
||||
listener.onEnd(this.review);
|
||||
}
|
||||
|
||||
Hebdobot.this.sendMessage("% " + Hebdobot.this.review.getOwner()
|
||||
sendMessage("% " + this.review.getOwner()
|
||||
+ ", ne pas oublier d'ajouter le compte-rendu de la revue sur https://agir.april.org/issues/135");
|
||||
final String participants = StringUtils.join(Hebdobot.this.review.getParticipants(), " ");
|
||||
Hebdobot.this.sendMessage("% " + participants + ", pensez à noter votre bénévalo : http://www.april.org/my?action=benevalo");
|
||||
Hebdobot.this.sendMessage("% Fin de la revue hebdomadaire");
|
||||
Hebdobot.this.review = null;
|
||||
|
||||
result = true;
|
||||
String participants = StringUtils.join(this.review.getParticipants(), " ");
|
||||
sendMessage("% " + participants + ", pensez à noter votre bénévalo : http://www.april.org/my?action=benevalo");
|
||||
sendMessage("% Fin de la revue hebdomadaire");
|
||||
this.review = null;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
// Collective topic, must be before individual topic
|
||||
this.handlers.add(new Handler()
|
||||
else if (text.matches("\\s*##.*"))
|
||||
{
|
||||
/* (non-Javadoc)
|
||||
* @see org.april.hebdobot.irc.Bot.Handler#handle(java.lang.String, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message)
|
||||
// Collective topic, must be before individual topic.
|
||||
if (this.review != null)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (Hebdobot.this.review == null || !message.matches("\\s*##.*"))
|
||||
if (this.review.isOwner(sender))
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Hebdobot.this.review.isOwner(sender))
|
||||
{
|
||||
final CollectiveTopic topic = new CollectiveTopic(message.replaceFirst("##", "").trim());
|
||||
Hebdobot.this.review.begin(topic);
|
||||
Hebdobot.this.sendMessage("Sujet collectif : " + topic.getTitle());
|
||||
CollectiveTopic topic = new CollectiveTopic(text.replaceFirst("##", "").trim());
|
||||
this.review.begin(topic);
|
||||
sendMessage("Sujet collectif : " + topic.getTitle());
|
||||
if (topic.getTitle().toLowerCase().contains("bloquage"))
|
||||
{
|
||||
Hebdobot.this.sendMessage("% si rien à dire vous pouvez dire %ras");
|
||||
sendMessage("% si rien à dire vous pouvez dire %ras");
|
||||
}
|
||||
else
|
||||
{
|
||||
Hebdobot.this.sendMessage("% 1 minute max");
|
||||
sendMessage("% 1 minute max");
|
||||
}
|
||||
result = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Hebdobot.this.sendMessage(sender + ", vous n'êtes pas le conducteur de la réunion");
|
||||
result = false;
|
||||
sendMessage(sender + ", vous n'êtes pas le conducteur de la réunion");
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
// Individual topic
|
||||
this.handlers.add(new Handler()
|
||||
else if (text.matches("\\s*#[^#].*"))
|
||||
{
|
||||
/* (non-Javadoc)
|
||||
* @see org.april.hebdobot.irc.Bot.Handler#handle(java.lang.String, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message)
|
||||
// Individual topic.
|
||||
if (this.review == null)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (Hebdobot.this.review == null || !message.matches("\\s*#[^#].*"))
|
||||
if (this.review.isOwner(sender))
|
||||
{
|
||||
result = false;
|
||||
sendMessage(sender + ", vous n'êtes pas le conducteur de la réunion");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Hebdobot.this.review.isOwner(sender))
|
||||
IndividualTopic topic = new IndividualTopic(text.replaceFirst("#", "").trim());
|
||||
this.review.begin(topic);
|
||||
sendMessage("Sujet individuel : " + topic.getTitle());
|
||||
sendMessage("% quand vous avez fini vous le dites par % fini");
|
||||
}
|
||||
}
|
||||
else if (StringUtils.equalsIgnoreCase(text, "!manquants"))
|
||||
{
|
||||
Hebdobot.this.sendMessage(sender + ", vous n'êtes pas le conducteur de la réunion");
|
||||
result = false;
|
||||
// Missing.
|
||||
if (this.review == null)
|
||||
{
|
||||
sendMessage("Pas de revue en cours.");
|
||||
}
|
||||
else
|
||||
{
|
||||
final IndividualTopic topic = new IndividualTopic(message.replaceFirst("#", "").trim());
|
||||
Hebdobot.this.review.begin(topic);
|
||||
Hebdobot.this.sendMessage("Sujet individuel : " + topic.getTitle());
|
||||
Hebdobot.this.sendMessage("% quand vous avez fini vous le dites par % fini");
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
// Missing
|
||||
this.handlers.add(new Handler()
|
||||
{
|
||||
/* (non-Javadoc)
|
||||
* @see org.april.hebdobot.irc.Bot.Handler#handle(java.lang.String, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (Hebdobot.this.review == null || !StringUtils.equalsIgnoreCase(message, "!manquants"))
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
final Topic topic = Hebdobot.this.review.getCurrentTopic();
|
||||
Topic topic = this.review.getCurrentTopic();
|
||||
if (topic == null)
|
||||
{
|
||||
Hebdobot.this.sendMessage("Aucun sujet traité");
|
||||
result = true;
|
||||
sendMessage("Aucun sujet traité");
|
||||
}
|
||||
else
|
||||
{
|
||||
final Collection<String> participants = Hebdobot.this.review.getParticipants();
|
||||
final Collection<String> currentParticipants = topic.getParticipants();
|
||||
Collection<String> participants = this.review.getParticipants();
|
||||
Collection<String> currentParticipants = topic.getParticipants();
|
||||
|
||||
final Collection<String> missing = CollectionUtils.subtract(participants, currentParticipants);
|
||||
Collection<String> missing = CollectionUtils.subtract(participants, currentParticipants);
|
||||
if (missing.isEmpty())
|
||||
{
|
||||
Hebdobot.this.sendMessage("Aucun participant manquant \\o/");
|
||||
result = true;
|
||||
sendMessage("Aucun participant manquant \\o/");
|
||||
}
|
||||
else
|
||||
{
|
||||
Hebdobot.this
|
||||
.sendMessage(String.format("Les participants suivants sont manquants : %1s", StringUtils.join(missing, ", ")));
|
||||
result = true;
|
||||
sendMessage(String.format("Les participants suivants sont manquants : %1s", StringUtils.join(missing, ", ")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
// Current
|
||||
this.handlers.add(new Handler()
|
||||
else if (StringUtils.equalsIgnoreCase(text, "!courant"))
|
||||
{
|
||||
/* (non-Javadoc)
|
||||
* @see org.april.hebdobot.irc.Bot.Handler#handle(java.lang.String, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message)
|
||||
// Current.
|
||||
if (this.review == null)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (Hebdobot.this.review == null || !StringUtils.equalsIgnoreCase(message, "!courant"))
|
||||
{
|
||||
result = false;
|
||||
sendMessage("Pas de revue en cours.");
|
||||
}
|
||||
else
|
||||
{
|
||||
final Topic current = Hebdobot.this.review.getCurrentTopic();
|
||||
Topic current = this.review.getCurrentTopic();
|
||||
if (current == null)
|
||||
{
|
||||
Hebdobot.this.sendMessage("% Pas de sujet en cours");
|
||||
sendMessage("% Pas de sujet en cours");
|
||||
}
|
||||
else if (current instanceof IndividualTopic)
|
||||
{
|
||||
Hebdobot.this.sendMessage("% Sujet individuel en cours : " + current.getTitle());
|
||||
sendMessage("% Sujet individuel en cours : " + current.getTitle());
|
||||
}
|
||||
else if (current instanceof CollectiveTopic)
|
||||
{
|
||||
Hebdobot.this.sendMessage("% Sujet collectif en cours : " + current.getTitle());
|
||||
sendMessage("% Sujet collectif en cours : " + current.getTitle());
|
||||
}
|
||||
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
// Topic message
|
||||
this.handlers.add(new Handler()
|
||||
else if (text.startsWith("%"))
|
||||
{
|
||||
/* (non-Javadoc)
|
||||
* @see org.april.hebdobot.irc.Bot.Handler#handle(java.lang.String, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message)
|
||||
// Topic message.
|
||||
if (this.review == null)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (Hebdobot.this.review == null || message.startsWith("%"))
|
||||
{
|
||||
result = false;
|
||||
sendMessage("Pas de revue en cours.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Hebdobot.this.review.add(new Message(sender, message));
|
||||
result = true;
|
||||
this.review.add(new Message(sender, text));
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
// All the other
|
||||
this.handlers.add(new Handler()
|
||||
{
|
||||
/* (non-Javadoc)
|
||||
* @see org.april.hebdobot.irc.Bot.Handler#handle(java.lang.String, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public boolean handle(final String sender, final String message)
|
||||
{
|
||||
boolean result;
|
||||
|
||||
if (Hebdobot.this.review == null)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Hebdobot.this.review.addRaw(new Message(sender, message));
|
||||
result = true;
|
||||
// All the other.
|
||||
if (this.review != null)
|
||||
{
|
||||
this.review.addRaw(new Message(sender, text));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user