hebdobot/src/org/april/hebdobot/bot/Hebdobot.java

851 lines
32 KiB
Java
Raw Normal View History

2017-12-14 15:33:27 +01:00
/**
* Copyright (C) 2011-2013,2017 Nicolas Vinot <aeris@imirhil.fr>
* Copyright (C) 2017-2018 Christian Pierre MOMON <cmomon@april.org>
2017-12-14 15:33:27 +01:00
*
* 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/>
*/
2018-09-20 18:25:29 +02:00
package org.april.hebdobot.bot;
2011-09-30 00:35:49 +02:00
2017-12-15 01:07:05 +01:00
import java.io.File;
import java.io.IOException;
2018-01-03 02:51:35 +01:00
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
2011-09-30 00:35:49 +02:00
import java.util.Collection;
import java.util.Locale;
2011-09-30 00:35:49 +02:00
import org.apache.commons.collections.CollectionUtils;
2017-12-15 01:07:05 +01:00
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.april.hebdobot.HebdobotException;
2018-09-20 18:25:29 +02:00
import org.april.hebdobot.bot.review.CollectiveTopic;
import org.april.hebdobot.bot.review.IndividualTopic;
import org.april.hebdobot.bot.review.Message;
import org.april.hebdobot.bot.review.Review;
import org.april.hebdobot.bot.review.Topic;
import org.april.hebdobot.bot.stats.ReviewData;
import org.april.hebdobot.bot.stats.ReviewDatas;
import org.april.hebdobot.bot.stats.ReviewDatasFile;
import org.april.hebdobot.bot.stats.ReviewStatsReporter;
2017-12-28 04:21:54 +01:00
import org.april.hebdobot.cron.CronManager;
import org.april.hebdobot.cron.CronSettings;
import org.april.hebdobot.identica.IdenticaSettings;
import org.april.hebdobot.pastebin.PastebinClient;
import org.april.hebdobot.pastebin.PastebinSettings;
2017-12-15 01:07:05 +01:00
import org.april.hebdobot.pastebin.Private;
import org.april.hebdobot.twitter.TwitterClient;
import org.april.hebdobot.twitter.TwitterSettings;
import org.april.hebdobot.util.BuildInformation;
import org.jibble.pircbot.IrcException;
import org.jibble.pircbot.NickAlreadyInUseException;
2011-09-30 00:35:49 +02:00
import org.jibble.pircbot.PircBot;
2017-12-28 04:21:54 +01:00
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2011-09-30 00:35:49 +02:00
import fr.devinsy.strings.StringList;
2018-06-12 11:10:23 +02:00
import fr.devinsy.strings.StringsUtils;
import twitter4j.TwitterException;
2017-12-14 16:58:45 +01:00
/**
* The Class Hebdobot.
2017-12-14 16:58:45 +01:00
*/
public class Hebdobot extends PircBot
{
private static final Logger logger = LoggerFactory.getLogger(Hebdobot.class);
private static String DEFAULT_SUFFIX = "-log-irc-revue-hebdomadaire.txt";
private File homeDirectory;
private String host;
private int port;
private String channel;
private File reviewDirectory;
private String reviewFileSuffix;
private LocalTime reviewWaitTime;
private Review review;
private IdenticaSettings identicaSettings;
private PastebinSettings pastebinSettings;
private TwitterSettings twitterSettings;
2017-12-28 04:21:54 +01:00
private CronSettings cronSettings;
2017-12-25 00:29:57 +01:00
private UserAliases aliases;
2017-12-28 04:21:54 +01:00
private CronManager cronManager;
2017-12-14 16:58:45 +01:00
/**
2018-01-04 23:28:30 +01:00
* Instantiates a new hebdobot.
2017-12-14 16:58:45 +01:00
*
* @param host
* the host
* @param port
* the port
2018-05-25 13:31:59 +02:00
* @param nickname
2017-12-14 16:58:45 +01:00
* the name
* @param channel
* the channel
2018-01-04 23:28:30 +01:00
* @param homeDirectory
* the home directory
2018-01-10 02:00:56 +01:00
* @param reviewDirectory
* the review directory
2017-12-14 16:58:45 +01:00
*/
2018-06-12 11:10:23 +02:00
public Hebdobot(final String host, final int port, final String nickname, final String channel, final File homeDirectory,
final File reviewDirectory)
{
this.homeDirectory = homeDirectory;
this.host = host;
this.port = port;
this.channel = channel;
2018-05-25 13:31:59 +02:00
this.setName(nickname);
this.reviewDirectory = reviewDirectory;
this.reviewFileSuffix = null;
this.reviewWaitTime = null;
this.review = null;
this.identicaSettings = new IdenticaSettings();
this.pastebinSettings = new PastebinSettings();
this.twitterSettings = new TwitterSettings();
2017-12-28 04:21:54 +01:00
this.cronSettings = new CronSettings();
2017-12-25 00:29:57 +01:00
this.aliases = new UserAliases();
2017-12-28 04:21:54 +01:00
this.cronManager = null;
}
2017-12-14 16:58:45 +01:00
/**
* Close.
*/
public void close()
{
this.disconnect();
this.dispose();
}
/**
* Gets the aliases.
*
* @return the aliases
*/
2017-12-25 00:29:57 +01:00
public UserAliases getAliases()
{
return this.aliases;
}
/**
* Gets the cron settings.
*
* @return the cron settings
*/
2017-12-28 04:21:54 +01:00
public CronSettings getCronSettings()
{
return this.cronSettings;
}
/**
* Gets the home directory.
*
* @return the home directory
*/
public File getHomeDirectory()
{
return this.homeDirectory;
}
/**
* Gets the identica settings.
*
* @return the identica settings
*/
public IdenticaSettings getIdenticaSettings()
{
return this.identicaSettings;
}
/**
* Gets the pastebin settings.
*
* @return the pastebin settings
*/
public PastebinSettings getPastebinSettings()
{
return this.pastebinSettings;
}
public File getReviewDirectory()
{
return this.reviewDirectory;
}
public String getReviewFileSuffix()
{
return this.reviewFileSuffix;
}
public LocalTime getReviewWaitTime()
{
return this.reviewWaitTime;
}
/**
* Gets the twitter settings.
*
* @return the twitter settings
*/
public TwitterSettings getTwitterSettings()
{
return this.twitterSettings;
}
2017-12-14 16:58:45 +01:00
/**
2018-01-04 23:28:30 +01:00
* Notify irc.
2017-12-14 16:58:45 +01:00
*
2018-01-04 23:28:30 +01:00
* @param message
* the message
2017-12-14 16:58:45 +01:00
*/
2017-12-28 04:21:54 +01:00
public void notifyIrc(final String message)
{
2017-12-28 04:21:54 +01:00
sendMessage(message);
}
2017-12-28 04:21:54 +01:00
/**
* Notify twitter.
*
* @param message
* the message
*/
public void notifyTwitter(final String message)
{
notifyTwitter(message, null);
}
/**
* Notify twitter.
*
* @param message
* the message
* @param imageFile
* the image file
*/
public void notifyTwitter(final String message, final File imageFile)
2017-12-28 04:21:54 +01:00
{
2018-01-19 10:24:48 +01:00
if ((this.twitterSettings.isValid()) && (StringUtils.isNotBlank(message)))
{
try
{
TwitterClient twitter = new TwitterClient(this.twitterSettings.getConsumerKey(), this.twitterSettings.getConsumerSecret(),
this.twitterSettings.getAccessToken(), this.twitterSettings.getAccessTokenSecret());
twitter.tweet(message, imageFile);
}
catch (TwitterException exception)
{
logger.error("Error in tweet", exception);
sendMessage("(pour information, la notification par Tweet a échoué : " + exception.getErrorMessage() + ")");
}
}
}
2017-12-14 16:58:45 +01:00
/* (non-Javadoc)
* @see org.jibble.pircbot.PircBot#onMessage(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
@Override
2017-12-14 22:54:06 +01:00
protected void onMessage(final String channel, final String sender, final String login, final String hostname, final String message)
{
2017-12-14 16:58:45 +01:00
logger.debug("Message received - channel : {}, sender : {}, message : {}", channel, sender, message);
2017-12-14 16:58:45 +01:00
if (channel.equalsIgnoreCase(this.channel))
{
2017-12-14 22:54:06 +01:00
String text = message.trim();
2018-01-12 02:23:26 +01:00
if (this.review != null)
{
this.review.addRaw(new Message(sender, text));
}
if ((StringUtils.equalsIgnoreCase(text, "!aide")) || (StringUtils.equalsIgnoreCase(text, "!help")))
{
2017-12-25 00:29:57 +01:00
logger.info("!help caught.");
2017-12-14 22:54:06 +01:00
// Help.
sendMessage(sender, "Bienvenue " + sender);
sendMessage(sender, "Je suis " + getName() + ", le robot de gestion des revues hebdomadaires de l'APRIL.");
2017-12-24 11:25:45 +01:00
sendMessage(sender, "Voici les commandes que je comprends :");
2017-12-14 22:54:06 +01:00
sendMessage(sender, " ");
sendMessage(sender, " !aide,!help : afficher cette aide");
2018-01-08 19:03:11 +01:00
sendMessage(sender, " !debut : commencer une nouvelle revue");
sendMessage(sender, " # titre  : démarrer un sujet individuel");
sendMessage(sender, " ## titre  : démarrer un sujet collectif");
sendMessage(sender, " % message  : traiter comme un commentaire");
sendMessage(sender, " !courant : afficher le sujet en cours");
sendMessage(sender, " !manquants : afficher qui n'a pas participé sur le dernier sujet");
2018-01-08 19:03:11 +01:00
sendMessage(sender, " !fin : terminer la revue en cours");
sendMessage(sender, " !stop  : abandonner la revue en cours");
2018-01-12 02:23:26 +01:00
sendMessage(sender, " !licence   : afficher la licence du logiciel Hebdobot et le lien vers ses sources");
sendMessage(sender, " !version   : afficher la version d'Hebdobot");
sendMessage(sender, " !stats : statistiques sur les précédentes revues");
2018-01-22 11:07:04 +01:00
sendMessage(sender, " ");
sendMessage(sender, "Autres commandes de dialogue : !bonjour, !date, !hello, !merci, !salut");
}
2017-12-14 22:54:06 +01:00
else if (StringUtils.equalsIgnoreCase(text, "!stop"))
{
2017-12-25 00:29:57 +01:00
logger.info("!stop caught.");
// Stop.
2017-12-14 22:54:06 +01:00
if (this.review == null)
{
sendMessage("Aucune revue en cours, abandon impossible.");
2017-12-14 16:58:45 +01:00
}
else
{
sendMessage("Abandon de la revue en cours.");
this.review = null;
}
}
else if (StringsUtils.equalsAnyIgnoreCase(text, "!debut", "!début"))
{
2017-12-25 00:29:57 +01:00
logger.info("!debut caught.");
if ((this.reviewWaitTime != null) && (LocalTime.now().isBefore(this.reviewWaitTime)))
2017-12-28 04:21:54 +01:00
{
sendMessage(sender + ", ce n'est pas encore l'heure.");
}
else
{
// Start.
if (this.cronManager != null)
2017-12-28 04:21:54 +01:00
{
try
{
this.cronManager.shutdown();
}
catch (SchedulerException exception)
{
logger.warn("Scheduler shutdown failed.", exception);
}
2017-12-28 04:21:54 +01:00
}
this.review = new Review(sender, this.aliases);
sendMessage(sender, "Bonjour " + 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");
sendMessage("% pour connaître le point courant, taper !courant");
2017-12-28 04:21:54 +01:00
}
}
2017-12-14 22:54:06 +01:00
else if (StringUtils.equalsIgnoreCase(text, "!fin"))
{
2017-12-25 00:29:57 +01:00
logger.info("!fin caught.");
2018-01-22 08:28:39 +01:00
// End.
if (this.review == null)
{
2017-12-14 22:54:06 +01:00
sendMessage(sender + ", pas de revue en cours.");
2017-12-14 16:58:45 +01:00
}
2017-12-14 22:54:06 +01:00
else if (!this.review.isOwner(sender))
2017-12-14 16:58:45 +01:00
{
2017-12-14 22:54:06 +01:00
sendMessage(sender + ", vous n'êtes pas le conducteur de la réunion");
}
else if (this.review.getParticipants().size() == 0)
{
sendMessage("Participation nulle détecté. La revue est ignorée.");
this.review = null;
}
2017-12-14 16:58:45 +01:00
else
{
String date = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
String textReview = this.review.toString();
// Display statistics. This feature has to not break
// Hebdobot.
String newMaxUserCountReport;
String userCountReport;
String durationReport;
try
{
File reviewDataFile = new File(this.reviewDirectory, "reviewstats.csv");
if (reviewDataFile.exists())
{
ReviewDatas datas = ReviewDatasFile.load(reviewDataFile);
datas.clean();
newMaxUserCountReport = ReviewStatsReporter.reportNewMaxUserCount(datas, this.review.getParticipants().size());
ReviewData currentReview = new ReviewData(LocalDateTime.now(), this.review.getParticipants().size(),
(int) this.review.getDurationInMinutes());
datas.add(currentReview);
userCountReport = ReviewStatsReporter.reportUserCount(datas, currentReview.getUserCount());
durationReport = ReviewStatsReporter.reportDuration(datas, currentReview.getDuration());
if (this.review.getParticipants().size() > 1)
{
ReviewDatasFile.append(reviewDataFile, currentReview);
}
}
else
{
logger.warn("Statistic file not found [{}]", reviewDataFile.getAbsolutePath());
sendMessage("% Fichier de statistiques absent.");
newMaxUserCountReport = null;
userCountReport = null;
durationReport = null;
}
}
catch (Exception exception)
{
logger.warn("Exception during statistics work.", exception);
newMaxUserCountReport = null;
userCountReport = null;
durationReport = null;
}
textReview = new StringList(textReview).appendln(newMaxUserCountReport, "\n", userCountReport, "\n", durationReport).toString();
//
if (this.pastebinSettings.isValid())
2017-12-14 16:58:45 +01:00
{
logger.info("Pastebin the review.");
try
{
PastebinClient pastebinClient = new PastebinClient(this.pastebinSettings.getApiKey());
2017-12-26 02:16:40 +01:00
String pastebinUrl = pastebinClient.paste(textReview, "Revue APRIL " + date, Private.UNLISTED);
sendMessage("% Compte-rendu de la revue : " + pastebinUrl);
}
catch (final Exception exception)
2017-12-15 01:07:05 +01:00
{
logger.error("Error during Pastebin submit.", exception);
2017-12-15 01:07:05 +01:00
}
}
2017-12-15 01:07:05 +01:00
//
logger.info("Write review file.");
File reviewFile = new File(this.reviewDirectory, date + StringUtils.defaultString(this.reviewFileSuffix, DEFAULT_SUFFIX));
if (reviewFile.exists())
{
LocalTime now = LocalTime.now();
String newSuffix = String.format("-%02dh%02d.txt", now.getHour(), now.getMinute());
reviewFile = new File(reviewFile.getAbsolutePath().replace(".txt", newSuffix));
}
try
{
if (!this.reviewDirectory.exists())
2017-12-15 01:07:05 +01:00
{
logger.info("Create review directory: " + this.reviewDirectory.getAbsolutePath());
this.reviewDirectory.mkdirs();
2017-12-15 01:07:05 +01:00
}
FileUtils.writeStringToFile(reviewFile, textReview, StandardCharsets.UTF_8);
logger.info("File review saved in: [{}]", reviewFile.getAbsolutePath());
}
catch (final Exception exception)
{
logger.error("Error during file writing", exception);
2017-12-14 16:58:45 +01:00
}
sendMessage("% Durée de la revue : " + this.review.getDurationInMinutes() + " minutes");
2018-01-26 17:25:28 +01:00
sendMessage("% Nombre de personnes participantes : " + this.review.getParticipants().size());
// Display statistics.
if (newMaxUserCountReport != null)
{
sendMessage("% " + newMaxUserCountReport);
}
if (userCountReport != null)
{
sendMessage(this.review.getOwner(), userCountReport);
}
if (durationReport != null)
{
sendMessage(this.review.getOwner(), durationReport);
}
2018-06-13 14:39:58 +02:00
String participants = StringUtils.join(this.review.getParticipants(), " ");
sendMessage("% " + participants + ", pensez à noter votre bénévalo : http://www.april.org/my?action=benevalo");
String reminderMessage = String.format(
"%% %s, ne pas oublier d'ajouter le compte-rendu de la revue sur https://agir.april.org/issues/135 en utilisant comme nom de fichier %s",
this.review.getOwner(), reviewFile.getName());
logger.info("reminderMessage=[{}]", reminderMessage);
sendMessage(reminderMessage);
// Finalize review.
2017-12-14 22:54:06 +01:00
sendMessage("% Fin de la revue hebdomadaire");
sendMessage(this.review.getOwner(), "Revue finie.");
2017-12-14 22:54:06 +01:00
this.review = null;
}
}
2017-12-14 22:54:06 +01:00
else if (text.matches("\\s*##.*"))
{
2017-12-25 00:29:57 +01:00
logger.info("\\s*##.* caught.");
2017-12-14 22:54:06 +01:00
// Collective topic, must be before individual topic.
if (this.review != null)
{
2017-12-14 22:54:06 +01:00
if (this.review.isOwner(sender))
2017-12-14 16:58:45 +01:00
{
2017-12-14 22:54:06 +01:00
CollectiveTopic topic = new CollectiveTopic(text.replaceFirst("##", "").trim());
if (!this.review.isEmpty())
2017-12-14 16:58:45 +01:00
{
String participants = StringUtils.join(this.review.getParticipants(), " ");
sendMessage(String.format("%% %s, on va passer à la suite : %s", participants, topic.getTitle()));
2017-12-14 16:58:45 +01:00
}
this.review.begin(topic);
sendMessage("Sujet collectif : " + topic.getTitle());
sendMessage("% 1 minute max");
2018-01-12 01:06:02 +01:00
sendMessage("% si rien à signaler vous pouvez écrire % ras");
sendMessage("% quand vous avez fini vous le dites par % fini");
2017-12-14 16:58:45 +01:00
}
else
{
2017-12-14 22:54:06 +01:00
sendMessage(sender + ", vous n'êtes pas le conducteur de la réunion");
2017-12-14 16:58:45 +01:00
}
}
}
2017-12-14 22:54:06 +01:00
else if (text.matches("\\s*#[^#].*"))
{
2017-12-25 00:29:57 +01:00
logger.info("\\s*#[^#].* caught.");
2017-12-14 22:54:06 +01:00
// Individual topic.
if (this.review != null)
{
2017-12-14 22:54:06 +01:00
if (this.review.isOwner(sender))
2017-12-14 16:58:45 +01:00
{
2017-12-14 22:54:06 +01:00
IndividualTopic topic = new IndividualTopic(text.replaceFirst("#", "").trim());
if (!this.review.isEmpty())
{
String participants = StringUtils.join(this.review.getParticipants(), " ");
sendMessage(String.format("%% %s, on va passer à la suite : %s", participants, topic.getTitle()));
}
2017-12-14 22:54:06 +01:00
this.review.begin(topic);
sendMessage("Sujet individuel : " + topic.getTitle());
2018-01-12 01:06:02 +01:00
sendMessage("% si rien à signaler vous pouvez écrire % ras");
2017-12-14 22:54:06 +01:00
sendMessage("% quand vous avez fini vous le dites par % fini");
2017-12-14 16:58:45 +01:00
}
else
{
sendMessage(sender + ", vous n'êtes pas le conducteur de la réunion");
}
}
2017-12-25 00:29:57 +01:00
}
else if (StringUtils.equalsIgnoreCase(text, "!manquants"))
{
logger.info("!manquants caught.");
// Missing.
if (this.review == null)
{
sendMessage("Pas de revue en cours.");
}
else
{
2017-12-25 00:29:57 +01:00
Topic topic = this.review.getCurrentTopic();
if (topic == null)
2017-12-14 16:58:45 +01:00
{
2017-12-25 00:29:57 +01:00
sendMessage("Aucun sujet traité");
2017-12-14 16:58:45 +01:00
}
else
{
2017-12-25 00:29:57 +01:00
Collection<String> participants = this.review.getParticipants();
Collection<String> currentParticipants = topic.getParticipants();
Collection<String> missing = CollectionUtils.subtract(participants, currentParticipants);
if (missing.isEmpty())
2017-12-14 16:58:45 +01:00
{
2017-12-25 00:29:57 +01:00
sendMessage("Aucun participant manquant \\o/");
2017-12-14 16:58:45 +01:00
}
else
{
sendMessage(
String.format("Les personnes participantes suivantes sont manquantes : %1s", StringUtils.join(missing, ", ")));
2017-12-14 16:58:45 +01:00
}
}
}
2017-12-25 00:29:57 +01:00
}
else if (StringUtils.equalsIgnoreCase(text, "!courant"))
{
logger.info("!courant caught.");
// Current.
if (this.review == null)
{
2017-12-25 00:29:57 +01:00
sendMessage("Pas de revue en cours.");
}
2017-12-25 00:29:57 +01:00
else
2017-12-14 16:58:45 +01:00
{
2017-12-25 00:29:57 +01:00
Topic current = this.review.getCurrentTopic();
if (current == null)
2017-12-14 22:54:06 +01:00
{
2017-12-25 00:29:57 +01:00
sendMessage("% Pas de sujet en cours");
2017-12-14 22:54:06 +01:00
}
2017-12-25 00:29:57 +01:00
else if (current instanceof IndividualTopic)
2017-12-14 22:54:06 +01:00
{
2017-12-25 00:29:57 +01:00
sendMessage("% Sujet individuel en cours : " + current.getTitle());
2017-12-14 22:54:06 +01:00
}
2017-12-25 00:29:57 +01:00
else if (current instanceof CollectiveTopic)
{
sendMessage("% Sujet collectif en cours : " + current.getTitle());
}
}
}
else if (StringUtils.equalsIgnoreCase(text, "!statut"))
{
logger.info("!status caught.");
sendMessage(sender, sender + ", voici l'état d'Hebdobot :");
sendMessage(sender, " revue en cours : " + (this.review != null));
if (this.review == null)
{
sendMessage(sender, " animateur revue : none");
}
else
{
sendMessage(sender, " animateur revue : " + this.review.getOwner());
}
sendMessage(sender, " Alias settings : " + (this.aliases.size()));
sendMessage(sender, " Identica settings : " + (this.identicaSettings.isValid()));
sendMessage(sender, " Pastebin settings : " + (this.pastebinSettings.isValid()));
sendMessage(sender, " Twitter settings : " + (this.twitterSettings.isValid()));
sendMessage(sender, " Cron settings : " + (this.cronSettings.size()));
sendMessage(sender, " Review Wait Time : " + (this.reviewWaitTime));
}
2017-12-28 14:17:38 +01:00
else if (StringsUtils.equalsAnyIgnoreCase(text, "!licence", "!license"))
{
logger.info("!licence caught.");
sendMessage(sender
+ ", Hebdobot est un logiciel libre de l'April sous licence GNU AGPL (sources : https://agir.april.org/projects/hebdobot/repository).");
}
else if (StringsUtils.equalsAnyIgnoreCase(text, "!version"))
{
logger.info("!version caught.");
sendMessage(new BuildInformation().toString());
}
2017-12-25 00:29:57 +01:00
else if (text.startsWith("%"))
{
logger.info("% caught.");
2018-01-12 02:23:26 +01:00
// Ignore.
2017-12-26 02:16:40 +01:00
}
else if (StringsUtils.equalsAnyIgnoreCase(text, "!salut", "!bonjour", "!hello"))
{
logger.info("!salut caught.");
2018-01-12 02:23:26 +01:00
// Salutation command.
sendMessage(sender + ", bonjour \\o/");
}
2018-01-21 07:54:09 +01:00
else if (StringsUtils.equalsAnyIgnoreCase(text, "!merci"))
{
logger.info("!merci caught.");
// Salutation command.
sendMessage(sender + ", de rien \\o/");
}
else if (StringsUtils.equalsAnyIgnoreCase(text, "!date", "!time"))
{
logger.info("!date caught.");
// Date command.
sendMessage(LocalDateTime.now().format(DateTimeFormatter.ofPattern("EEEE dd MMMM yyyy HH'h'mm", Locale.FRENCH)));
}
else if (StringsUtils.equalsAnyIgnoreCase(text, "!stats"))
{
logger.info("!stats caught.");
// Display statistics. This feature has to not break
// Hebdobot.
try
{
File reviewDataFile = new File(this.reviewDirectory, "reviewstats.csv");
if (reviewDataFile.exists())
{
ReviewDatas datas = ReviewDatasFile.load(reviewDataFile);
datas.clean();
sendMessage("% " + ReviewStatsReporter.reportUserCountBoard(datas));
sendMessage("% " + ReviewStatsReporter.reportDurationBoard(datas));
}
else
{
logger.warn("Statistic file not found [{}]", reviewDataFile.getAbsolutePath());
sendMessage("% Fichier de statistiques absent.");
}
}
catch (Exception exception)
{
logger.warn("Exception during statistics work.", exception);
sendMessage("% Impossible d'afficher des statistiques.");
}
}
2017-12-26 02:16:40 +01:00
else if (text.startsWith("!"))
{
logger.info("!??? caught.");
// Command unknown.
// Do not answer because it can be a command to another bot.
2017-12-26 02:16:40 +01:00
}
else
{
logger.info("All the other caught.");
2017-12-26 02:16:40 +01:00
2017-12-25 00:29:57 +01:00
// Topic message.
if (this.review == null)
{
if ((StringsUtils.containsAnyIgnoreCase(text, "bonjour", "salut", "hello")) && (StringUtils.containsIgnoreCase(text, "hebdobot")))
{
sendMessage(sender + ", bonjour \\o/");
}
2018-01-21 07:54:09 +01:00
else if ((StringsUtils.containsAnyIgnoreCase(text, "merci")) && (StringUtils.containsIgnoreCase(text, "hebdobot")))
{
sendMessage(sender + ", de rien \\o/");
}
}
2017-12-14 16:58:45 +01:00
else
{
2017-12-25 00:29:57 +01:00
this.review.add(new Message(sender, text));
}
}
2017-12-14 22:54:06 +01:00
}
}
2018-01-04 09:39:47 +01:00
/* (non-Javadoc)
* @see org.jibble.pircbot.PircBot#onPrivateMessage(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
@Override
protected void onPrivateMessage(final String sender, final String login, final String hostname, final String message)
{
logger.debug("Private message received - sender : {}, message : {}", sender, message);
String text = message.trim();
if (StringUtils.equalsIgnoreCase(text, "!vaten"))
{
logger.info("!die caught.");
// Die.
if (this.review == null)
{
try
{
sendMessage(sender + ", ok bye.");
sendMessage(sender, "ok bye.");
2018-01-04 09:39:47 +01:00
Thread.sleep(1000);
System.exit(0);
}
catch (InterruptedException exception)
{
logger.warn("Pause abort: " + exception.getMessage());
}
}
else
{
sendMessage("% Une revue est en cours, abandon impossible.");
}
}
else if (text.startsWith("!"))
{
logger.info("!??? caught.");
// Command unknown.
sendMessage(sender + ", command unknown: " + text);
}
else
{
// Nothing to say.
}
}
/**
2018-01-04 23:28:30 +01:00
* Run.
*
* @throws HebdobotException
2018-01-04 23:28:30 +01:00
* the hebdobot exception
*/
public void run() throws HebdobotException
{
try
{
2017-12-28 04:21:54 +01:00
logger.info("Cron initializing.");
{
this.cronManager = new CronManager(this, this.cronSettings);
this.cronManager.start();
}
logger.info("Cron initialized.");
2017-12-25 00:29:57 +01:00
logger.info("Bot connection.");
this.connect(this.host, this.port);
2017-12-28 04:21:54 +01:00
logger.info("Bot connected.");
2017-12-25 00:29:57 +01:00
logger.info("Bot joining channel ({}).", this.channel);
this.joinChannel(this.channel);
2017-12-25 00:29:57 +01:00
logger.info("Bot ready.");
}
catch (NickAlreadyInUseException exception)
{
throw new HebdobotException(exception);
}
catch (IOException exception)
{
throw new HebdobotException(exception);
}
catch (IrcException exception)
{
throw new HebdobotException(exception);
}
2017-12-28 04:21:54 +01:00
catch (SchedulerException exception)
{
throw new HebdobotException("Error in cron settings.", exception);
}
}
2017-12-14 16:58:45 +01:00
/**
* Send message.
*
* @param message
* the message
*/
public void sendMessage(final String message)
{
2017-12-14 16:58:45 +01:00
logger.debug("Send message : {}", message);
this.sendMessage(this.channel, message);
2018-01-12 02:23:26 +01:00
if (this.review != null)
{
this.review.addRaw(new Message("Hebdobot", message));
}
}
/**
* Sets the home directory.
*
* @param homeDirectory
* the new home directory
*/
public void setHomeDirectory(final File homeDirectory)
{
this.homeDirectory = homeDirectory;
}
public void setReviewFileSuffix(final String reviewFileSuffix)
{
this.reviewFileSuffix = reviewFileSuffix;
}
public void setReviewWaitTime(final LocalTime reviewWaitTime)
{
this.reviewWaitTime = reviewWaitTime;
}
2011-09-30 00:35:49 +02:00
}