hebdobot/src/org/april/hebdobot/bot/hooks/HelpHook.java

159 lines
5.3 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.

/**
* Copyright (C) 2018-2021 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.bot.hooks;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.april.hebdobot.HebdobotException;
import org.april.hebdobot.bot.Hebdobot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.strings.StringList;
import fr.devinsy.strings.StringsUtils;
/**
* The Class HelpHook.
*/
public class HelpHook extends Hook
{
private static final Pattern COMMAND_HELP_PATTERN = Pattern.compile("^!(aide|help)\\s+(?<token>\\S+)$");
private static final Logger logger = LoggerFactory.getLogger(HelpHook.class);
/* (non-Javadoc)
* @see org.april.hebdobot.bot.hooks.Hook#attemptHelp(java.lang.String)
*/
@Override
public StringList attemptHelp(final String token) throws HebdobotException
{
StringList result;
if (StringsUtils.equalsAnyIgnoreCase(token, "aide", "help"))
{
result = new StringList();
result.append("!aide, !help, !aide hebdobot, !help hebdobot : afficher l'aide générale");
result.append("!aide commande, !help commande : afficher l'aide de la commande !commande");
}
else
{
result = null;
}
//
return result;
}
/* (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) throws HebdobotException
{
boolean result;
if (StringUtils.equalsAnyIgnoreCase(message, "!aide", "!help", "!aide hebdobot", "!help hebdobot"))
{
logger.info("!help caught.");
// Help.
bot.sendMessage(sender,
String.format("Bienvenue %s. Je suis %s, le robot de gestion des revues hebdomadaires de l'April.", sender, bot.getName()));
bot.sendMessage(sender, "Voici les commandes que je comprends :");
bot.sendMessage(sender, " ");
bot.sendMessage(sender, " !aide,!help : afficher cette aide");
bot.sendMessage(sender, " !aide commande : afficher l'aide de la commande !commande");
bot.sendMessage(sender, " !début : commencer une nouvelle revue");
bot.sendMessage(sender, " % message  : traiter comme un commentaire");
bot.sendMessage(sender, " # titre  : démarrer un sujet individuel");
bot.sendMessage(sender, " ## titre  : démarrer un sujet collectif");
bot.sendMessage(sender, " !oups   : annuler la dernière entrée dans un point de revue");
bot.sendMessage(sender, " !courant : afficher le sujet en cours");
bot.sendMessage(sender, " !fin : terminer la revue en cours");
bot.sendMessage(sender, " !stop  : abandonner la revue en cours");
bot.sendMessage(sender, " ");
bot.sendMessage(sender,
"Autres commandes : !anniv, !bonjour, !chrono, !date, !hello, !licence, !manquants, !merci, !record, !salut, !stats, !status, !version");
result = true;
}
else
{
String token = extractTargetHelp(message);
if (token == null)
{
result = false;
}
else
{
StringList help = bot.getHelp(token);
if (help == null)
{
bot.sendMessage("aide indisponible pour la commande " + token);
}
else
{
for (String line : help)
{
bot.sendMessage(line);
}
}
result = true;
}
}
//
return result;
}
/**
* Extract target help.
*
* @return the string
*/
private String extractTargetHelp(final String message)
{
String result;
Matcher matcher = COMMAND_HELP_PATTERN.matcher(message);
if (matcher.find())
{
result = matcher.group("token");
if (StringUtils.startsWith(result, "!"))
{
result = result.substring(1);
}
}
else
{
result = null;
}
//
return result;
}
}