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

417 lines
16 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 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;
2011-09-30 00:35:49 +02:00
2017-12-15 01:07:05 +01:00
import java.io.File;
import java.io.IOException;
2011-09-30 00:35:49 +02:00
import java.util.Collection;
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.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.review.CollectiveTopic;
import org.april.hebdobot.review.IndividualTopic;
import org.april.hebdobot.review.Message;
import org.april.hebdobot.review.Review;
import org.april.hebdobot.review.Topic;
import org.april.hebdobot.twitter.TwitterSettings;
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-15 01:07:05 +01:00
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.social.twitter.api.impl.TwitterTemplate;
2011-09-30 00:35:49 +02:00
2017-12-14 16:58:45 +01:00
/**
* The Class Bot.
*/
public class Hebdobot extends PircBot
{
private static final Logger logger = LoggerFactory.getLogger(Hebdobot.class);
private String host;
private int port;
private String channel;
private String reviewFileSuffix;
private Review review;
private IdenticaSettings identicaSettings;
private PastebinSettings pastebinSettings;
private TwitterSettings twitterSettings;
2017-12-14 16:58:45 +01:00
/**
* Instantiates a new bot.
*
* @param host
* the host
* @param port
* the port
* @param name
* the name
* @param channel
* the channel
*/
public Hebdobot(final String host, final int port, final String name, final String channel, final String reviewFileSuffix)
{
this.host = host;
this.port = port;
this.channel = channel;
this.setName(name);
this.reviewFileSuffix = reviewFileSuffix;
this.review = null;
}
2017-12-14 16:58:45 +01:00
/**
* Close.
*/
public void close()
{
this.disconnect();
this.dispose();
}
2017-12-14 16:58:45 +01:00
/**
* Notify.
2017-12-14 16:58:45 +01:00
*
* @param minutes
* the minutes
2017-12-14 16:58:45 +01:00
*/
public void notify(final int minutes)
{
{
String message = String.format("Revue hebdomadaire dans %dmin", minutes);
sendMessage(message);
}
if (this.twitterSettings != null)
{
String message = String.format("Revue hebdomadaire !april dans %dmin sur irc://irc.freenode.org/april ou http://apr1.org/8l", minutes);
TwitterTemplate twitterClient = new TwitterTemplate(this.twitterSettings.getConsumerKey(), this.twitterSettings.getConsumerSecret());
twitterClient.timelineOperations().updateStatus(message);
}
}
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);
if (channel.equalsIgnoreCase(this.channel))
{
2017-12-14 22:54:06 +01:00
String text = message.trim();
2017-12-14 22:54:06 +01:00
if (StringUtils.equalsIgnoreCase(text, "!help"))
{
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");
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");
}
2017-12-14 22:54:06 +01:00
else if (StringUtils.equalsIgnoreCase(text, "!stop"))
{
2017-12-14 22:54:06 +01:00
// Die.
if (this.review == null)
{
2017-12-14 22:54:06 +01:00
Context.close();
2017-12-14 16:58:45 +01:00
}
else
{
2017-12-14 22:54:06 +01:00
sendMessage("% Une revue est en cours, arrêt impossible");
}
}
2017-12-14 22:54:06 +01:00
else if ((StringUtils.equalsIgnoreCase(text, "!debut")) || (StringUtils.equalsIgnoreCase(text, "!début")))
{
2017-12-14 22:54:06 +01:00
// 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");
}
2017-12-14 22:54:06 +01:00
else if (StringUtils.equalsIgnoreCase(text, "!fin"))
{
2017-12-14 22:54:06 +01:00
// Stop.
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");
}
2017-12-14 16:58:45 +01:00
else
{
2017-12-14 16:58:45 +01:00
{
String date = ISODateTimeFormat.basicDate().print(new DateTime());
String textReview = this.review.toString();
2017-12-15 01:07:05 +01:00
try
{
PastebinClient pastebinClient = new PastebinClient(this.pastebinSettings.getApiKey());
String returnValue = pastebinClient.paste(textReview, "Revue APRIL " + date, Private.UNLISTED);
sendMessage("% Compte-rendu de la revue : " + returnValue);
2017-12-15 01:07:05 +01:00
}
catch (final Exception exception)
{
logger.error("Error during Pastebin submit.", exception);
2017-12-15 01:07:05 +01:00
}
if (this.reviewFileSuffix != null)
2017-12-15 01:07:05 +01:00
{
try
{
File file = new File(date + "_" + this.reviewFileSuffix);
2017-12-15 01:07:05 +01:00
FileUtils.writeStringToFile(file, text);
sendMessage("% Compte-rendu de la revue : " + file.getName());
2017-12-15 01:07:05 +01:00
}
catch (final Exception exception)
{
logger.error("Error during file generation", exception);
}
}
2017-12-14 16:58:45 +01:00
}
2017-12-14 22:54:06 +01:00
sendMessage("% " + this.review.getOwner()
+ ", ne pas oublier d'ajouter le compte-rendu de la revue sur https://agir.april.org/issues/135");
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");
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-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());
this.review.begin(topic);
sendMessage("Sujet collectif : " + topic.getTitle());
2017-12-14 16:58:45 +01:00
if (topic.getTitle().toLowerCase().contains("bloquage"))
{
2017-12-14 22:54:06 +01:00
sendMessage("% si rien à dire vous pouvez dire %ras");
2017-12-14 16:58:45 +01:00
}
else
{
2017-12-14 22:54:06 +01:00
sendMessage("% 1 minute max");
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-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
sendMessage(sender + ", vous n'êtes pas le conducteur de la réunion");
2017-12-14 16:58:45 +01:00
}
else
{
2017-12-14 22:54:06 +01:00
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");
2017-12-14 16:58:45 +01:00
}
}
2017-12-14 22:54:06 +01:00
else if (StringUtils.equalsIgnoreCase(text, "!manquants"))
{
2017-12-14 22:54:06 +01:00
// Missing.
if (this.review == null)
2017-12-14 16:58:45 +01:00
{
2017-12-14 22:54:06 +01:00
sendMessage("Pas de revue en cours.");
2017-12-14 16:58:45 +01:00
}
else
{
2017-12-14 22:54:06 +01:00
Topic topic = this.review.getCurrentTopic();
if (topic == null)
2017-12-14 16:58:45 +01:00
{
2017-12-14 22:54:06 +01:00
sendMessage("Aucun sujet traité");
2017-12-14 16:58:45 +01:00
}
else
{
2017-12-14 22:54:06 +01:00
Collection<String> participants = this.review.getParticipants();
Collection<String> currentParticipants = topic.getParticipants();
Collection<String> missing = CollectionUtils.subtract(participants, currentParticipants);
if (missing.isEmpty())
{
sendMessage("Aucun participant manquant \\o/");
}
else
{
sendMessage(String.format("Les participants suivants sont manquants : %1s", StringUtils.join(missing, ", ")));
}
2017-12-14 16:58:45 +01:00
}
}
}
2017-12-14 22:54:06 +01:00
else if (StringUtils.equalsIgnoreCase(text, "!courant"))
{
2017-12-14 22:54:06 +01:00
// Current.
if (this.review == null)
2017-12-14 16:58:45 +01:00
{
2017-12-14 22:54:06 +01:00
sendMessage("Pas de revue en cours.");
2017-12-14 16:58:45 +01:00
}
2017-12-14 22:54:06 +01:00
else
2017-12-14 16:58:45 +01:00
{
2017-12-14 22:54:06 +01:00
Topic current = this.review.getCurrentTopic();
if (current == null)
{
sendMessage("% Pas de sujet en cours");
}
else if (current instanceof IndividualTopic)
{
sendMessage("% Sujet individuel en cours : " + current.getTitle());
}
else if (current instanceof CollectiveTopic)
{
sendMessage("% Sujet collectif en cours : " + current.getTitle());
}
2017-12-14 16:58:45 +01:00
}
}
2017-12-14 22:54:06 +01:00
else if (text.startsWith("%"))
2017-12-14 16:58:45 +01:00
{
2017-12-14 22:54:06 +01:00
// Topic message.
if (this.review == null)
{
sendMessage("Pas de revue en cours.");
}
else
{
this.review.add(new Message(sender, text));
}
}
2017-12-14 16:58:45 +01:00
else
{
2017-12-14 22:54:06 +01:00
// All the other.
if (this.review != null)
{
this.review.addRaw(new Message(sender, text));
}
2017-12-14 16:58:45 +01:00
}
}
2017-12-14 22:54:06 +01:00
}
}
/**
* Inits the.
*
* the exception
*
* @throws HebdobotException
*/
public void run() throws HebdobotException
{
try
{
this.connect(this.host, this.port);
this.joinChannel(this.channel);
}
catch (NickAlreadyInUseException exception)
{
throw new HebdobotException(exception);
}
catch (IOException exception)
{
throw new HebdobotException(exception);
}
catch (IrcException exception)
{
throw new HebdobotException(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);
}
/**
* Sets the identica settings.
*
* @param settings
* the new identica settings
*/
public void setIdenticaSettings(final IdenticaSettings settings)
{
this.identicaSettings = new IdenticaSettings();
this.identicaSettings.setApiKey(settings.getApiKey());
this.identicaSettings.setApiSecret(settings.getApiSecret());
}
/**
* Sets the pastebin settings.
*
* @param settings
* the new pastebin settings
*/
public void setPastebinSettings(final PastebinSettings settings)
{
this.pastebinSettings = new PastebinSettings();
this.pastebinSettings.setApiKey(settings.getApiKey());
}
/**
* Sets the twitter settings.
*
* @param settings
* the new twitter settings
*/
public void setTwitterSettings(final TwitterSettings settings)
{
this.twitterSettings = new TwitterSettings();
this.twitterSettings.setAccessToken(settings.getAccessToken());
this.twitterSettings.setAccessTokenSecret(settings.getAccessTokenSecret());
this.twitterSettings.setConsumerKey(settings.getConsumerKey());
this.twitterSettings.setConsumerSecret(settings.getConsumerSecret());
}
2011-09-30 00:35:49 +02:00
}