/** * Copyright (C) 2011-2013,2017 Nicolas Vinot * Copyright (C) 2017-2018 Christian Pierre MOMON * * 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 */ package org.april.hebdobot.bot; import java.io.File; import java.io.IOException; import java.text.ParseException; import java.time.LocalTime; import org.apache.commons.lang3.StringUtils; import org.april.hebdobot.HebdobotException; import org.april.hebdobot.bot.hooks.CollectiveSubjectHook; import org.april.hebdobot.bot.hooks.CommentHook; import org.april.hebdobot.bot.hooks.CurrentHook; import org.april.hebdobot.bot.hooks.DateHook; import org.april.hebdobot.bot.hooks.DefaultHook; import org.april.hebdobot.bot.hooks.FinishReviewHook; import org.april.hebdobot.bot.hooks.HelloHook; import org.april.hebdobot.bot.hooks.HelpHook; import org.april.hebdobot.bot.hooks.HookManager; import org.april.hebdobot.bot.hooks.IndividualSubjectHook; import org.april.hebdobot.bot.hooks.LicenseHook; import org.april.hebdobot.bot.hooks.MissingHook; import org.april.hebdobot.bot.hooks.StartReviewHook; import org.april.hebdobot.bot.hooks.StatusHook; import org.april.hebdobot.bot.hooks.StopReviewHook; import org.april.hebdobot.bot.hooks.ThanksHook; import org.april.hebdobot.bot.hooks.VersionHook; import org.april.hebdobot.bot.review.Message; import org.april.hebdobot.bot.review.Review; import org.april.hebdobot.cron.CronManager; import org.april.hebdobot.cron.CronSettings; import org.april.hebdobot.identica.IdenticaSettings; import org.april.hebdobot.pastebin.PastebinSettings; import org.april.hebdobot.twitter.TwitterClient; import org.april.hebdobot.twitter.TwitterSettings; import org.jibble.pircbot.IrcException; import org.jibble.pircbot.NickAlreadyInUseException; import org.jibble.pircbot.PircBot; import org.quartz.SchedulerException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import twitter4j.TwitterException; /** * The Class Hebdobot. */ public class Hebdobot extends PircBot { private static final Logger logger = LoggerFactory.getLogger(Hebdobot.class); 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; private CronSettings cronSettings; private UserAliases aliases; private CronManager cronManager; private HookManager hooker; /** * Instantiates a new hebdobot. * * @param host * the host * @param port * the port * @param nickname * the name * @param channel * the channel * @param homeDirectory * the home directory * @param reviewDirectory * the review directory */ 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; 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(); this.cronSettings = new CronSettings(); this.aliases = new UserAliases(); this.cronManager = null; // this.hooker = new HookManager(); this.hooker.add(new CollectiveSubjectHook()); this.hooker.add(new CommentHook()); this.hooker.add(new CurrentHook()); this.hooker.add(new DateHook()); this.hooker.add(new FinishReviewHook()); this.hooker.add(new HelpHook()); this.hooker.add(new IndividualSubjectHook()); this.hooker.add(new MissingHook()); this.hooker.add(new StartReviewHook()); this.hooker.add(new StopReviewHook()); this.hooker.add(new StatusHook()); this.hooker.add(new LicenseHook()); this.hooker.add(new VersionHook()); this.hooker.add(new HelloHook()); this.hooker.add(new ThanksHook()); this.hooker.add(new DefaultHook()); } /** * Close. */ public void close() { this.disconnect(); this.dispose(); } /** * Gets the aliases. * * @return the aliases */ public UserAliases getAliases() { return this.aliases; } public CronManager getCronManager() { return this.cronManager; } /** * Gets the cron settings. * * @return the cron settings */ 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 Review getReview() { return this.review; } 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; } /** * Notify irc. * * @param message * the message */ public void notifyIrc(final String message) { sendMessage(message); } /** * 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) { 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() + ")"); } } } /* (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, final String message) { logger.debug("Message received - channel : {}, sender : {}, message : {}", channel, sender, message); if (channel.equalsIgnoreCase(this.channel)) { String text = message.trim(); if (this.review != null) { this.review.addRaw(new Message(sender, text)); } text = message.replaceAll("^" + getName().replace("[", "\\[").replaceAll("]", "\\]") + "[,:]\\s*", "!"); try { this.hooker.attemptProcess(this, channel, sender, login, hostname, text); } catch (HebdobotException exception) { logger.error("Error managing post.", exception); sendMessage("/me a choppé un bug : ", exception.getMessage()); } } } /* (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."); 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. } } /** * Run. * * @throws HebdobotException * the hebdobot exception */ public void run() throws HebdobotException { try { logger.info("Cron initializing."); { this.cronManager = new CronManager(this, this.cronSettings); this.cronManager.start(); } logger.info("Cron initialized."); logger.info("Bot connection."); this.connect(this.host, this.port); logger.info("Bot connected."); logger.info("Bot joining channel ({}).", this.channel); this.joinChannel(this.channel); 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); } catch (SchedulerException exception) { throw new HebdobotException("Error in cron settings.", exception); } catch (ParseException exception) { throw new HebdobotException(exception); } } /** * Send message. * * @param message * the message */ public void sendMessage(final String message) { logger.debug("Send message : {}", message); this.sendMessage(this.channel, message); 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 setReview(final Review review) { this.review = review; } public void setReviewDirectory(final File reviewDirectory) { this.reviewDirectory = reviewDirectory; } public void setReviewFileSuffix(final String reviewFileSuffix) { this.reviewFileSuffix = reviewFileSuffix; } public void setReviewWaitTime(final LocalTime reviewWaitTime) { this.reviewWaitTime = reviewWaitTime; } }