JIrcBot/src/main/java/ru/bvn13/jircbot/bot/JircBot.java

235 lines
7.6 KiB
Java
Raw Normal View History

2017-11-15 17:49:03 +03:00
package ru.bvn13.jircbot.bot;
2018-03-28 20:47:13 +03:00
import lombok.Getter;
2017-11-15 17:49:03 +03:00
import org.pircbotx.Configuration;
import org.pircbotx.MultiBotManager;
2017-11-15 17:49:03 +03:00
import org.pircbotx.PircBotX;
import org.pircbotx.UtilSSLSocketFactory;
import org.pircbotx.cap.TLSCapHandler;
import org.pircbotx.exception.IrcException;
2017-11-15 17:49:03 +03:00
import org.pircbotx.hooks.ListenerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
2018-03-28 20:47:13 +03:00
import org.springframework.beans.factory.annotation.Value;
2017-11-15 17:49:03 +03:00
import org.springframework.stereotype.Component;
2018-01-30 17:23:38 +03:00
import ru.bvn13.jircbot.services.YandexSearchService;
2017-11-15 17:49:03 +03:00
import ru.bvn13.jircbot.config.JircBotConfiguration;
import ru.bvn13.jircbot.listeners.*;
import ru.bvn13.jircbot.listeners.advices.AdviceListener;
import ru.bvn13.jircbot.listeners.calculator.CalculatorListener;
2018-01-26 13:39:58 +03:00
import ru.bvn13.jircbot.listeners.quiz.QuizListener;
2017-11-15 17:49:03 +03:00
2017-11-17 01:22:18 +03:00
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.IOException;
2017-11-15 17:49:03 +03:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
2017-11-15 17:49:03 +03:00
@Component
public class JircBot extends ListenerAdapter {
private static Logger logger = LoggerFactory.getLogger(JircBot.class);
2018-03-28 20:47:13 +03:00
@Value("${bot.version}")
private String version;
public String getVersion() {
return version == null ? "" : version;
}
2017-11-15 17:49:03 +03:00
private JircBotConfiguration config;
private Map<String, PircBotX> bots = new HashMap<>();
2017-11-17 01:22:18 +03:00
@Autowired
private YandexSearchService yandexSearchService;
2017-11-15 17:49:03 +03:00
@Autowired
public JircBot(JircBotConfiguration config) {
this.config = config;
}
private ScheduledExecutorService executorService;
2018-03-28 20:47:13 +03:00
private MultiBotManager manager = new MultiBotManager();
@Autowired
private PingPongListener pingPongListener;
@Autowired
private CalculatorListener calculatorListener;
@Autowired
private RegexCheckerListener regexCheckerListener;
@Autowired
private AdviceListener adviceListener;
@Autowired
private QuizListener quizListener;
@Autowired
private BashOrgListener bashOrgListener;
@Autowired
private AutoRejoinListener autoRejoinListener;
@Autowired
private DeferredMessagesListener deferredMessagesListener;
@Autowired
private LinkPreviewListener linkPreviewListener;
@Autowired
private HelloOnJoinListener helloOnJoinListener;
@Autowired
private GrammarCorrectorListener grammarCorrectorListener;
2018-02-06 12:25:30 +03:00
@Autowired
private GoogleSearchListener googleSearchListener;
2018-03-10 12:10:10 +03:00
@Autowired
private LoggerListener loggerListener;
@Autowired
private AdminListener adminListener;
2018-04-11 13:07:12 +03:00
@Autowired
private StatisticsListener statisticsListener;
2017-11-17 01:22:18 +03:00
@PostConstruct
public void postConstruct() {
2018-03-28 20:47:13 +03:00
logger.warn("VERSION: "+version);
this.executorService = Executors.newScheduledThreadPool(10);
this.executorService.schedule(new Runnable() {
@Override
public void run() {
initBots();
startBots();
}
}, 5, TimeUnit.SECONDS);
this.executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
logger.debug("check");
checkBots();
}
}, 15, 5, TimeUnit.SECONDS);
}
@PreDestroy
public void preDestroy() {
2018-03-28 20:47:13 +03:00
logger.warn("Bot is shutting down...");
this.executorService.shutdown();
2018-03-28 20:47:13 +03:00
this.manager.stop("Bot is shutting down...");
}
2017-11-15 17:49:03 +03:00
private void initBots() {
2018-03-28 20:47:13 +03:00
logger.info(">>>>>>>>>>>>>>>>>>>> BOT INIT : "+getVersion()+" <<<<<<<<<<<<<<<<<<<<");
2017-11-15 17:49:03 +03:00
//Setup this bot
Configuration.Builder templateConfig = new Configuration.Builder()
.setLogin("JIrcBot") //login part of hostmask, eg name:login@host
.setAutoNickChange(true) //Automatically change nick when the current one is in use
.setCapEnabled(true) //Enable CAP features
.addCapHandler(new TLSCapHandler(new UtilSSLSocketFactory().trustAllCertificates(), true));
this.config.getConnections().forEach(c -> {
List<Configuration.ServerEntry> servers = new ArrayList<>();
servers.add(new Configuration.ServerEntry(c.getServer(), c.getPort()));
Configuration.Builder confBuilder = templateConfig
2018-03-28 20:47:13 +03:00
.setRealName("JIrcBot v"+getVersion()+" | github.com/bvn13/JIrcBot")
.setName(c.getBotName())
.addListener(adminListener)
2018-04-11 13:07:12 +03:00
.addListener(statisticsListener)
.addListener(pingPongListener)
.addListener(calculatorListener)
.addListener(regexCheckerListener)
.addListener(adviceListener)
.addListener(quizListener)
.addListener(bashOrgListener)
.addListener(autoRejoinListener)
.addListener(deferredMessagesListener)
.addListener(linkPreviewListener)
.addListener(helloOnJoinListener)
.addListener(grammarCorrectorListener)
.addListener(googleSearchListener)
.addListener(loggerListener)
// not tested
//.addListener(new GoogleDoodleListener(this.config))
//.addListener(new YandexSearchListener(this.config, this.yandexSearchService))
.setServers(servers)
.setAutoReconnect(true)
.addAutoJoinChannels(c.getChannelsNames());
if (c.getBotPassword() != null && !c.getBotPassword().isEmpty()) {
confBuilder.setNickservPassword(c.getBotPassword());
}
2017-11-15 17:49:03 +03:00
this.bots.put(
String.format("%s/%s", c.getServer(), "1"),
new PircBotX(confBuilder.buildForServer(c.getServer())
2017-11-15 17:49:03 +03:00
)
);
});
}
2017-11-15 17:49:03 +03:00
private void startBots() {
logger.info(">>>>>>>>>>>>>>>>>>>> BOT STARTING <<<<<<<<<<<<<<<<<<<<");
this.bots.forEach((id, bot) -> {
manager.addBot(bot);
2017-11-15 17:49:03 +03:00
});
manager.start();
}
private void checkBots() {
2018-03-28 20:47:13 +03:00
if (this.manager.getBots().size() < this.bots.size()) {
logger.warn("CHECKING BOTS");
logger.debug("BOTS COUNT: " + this.manager.getBots().size());
}
this.bots.forEach((id, bot) -> {
if (!this.manager.getBots().contains(bot)) {
logger.warn("RECONNECTION BOT "+bot.getServerHostname());
bot.stopBotReconnect();
this.manager.addBot(bot);
} else {
if (bot.getState().equals(PircBotX.State.DISCONNECTED)) {
logger.warn("RECONNECTION BOT "+bot.getServerHostname());
bot.stopBotReconnect();
}
}
});
}
private void startBot(PircBotX bot) {
logger.warn("BOT "+bot.getServerHostname()+" STATUS "+bot.getState().toString());
try {
bot.startBot();
} catch (IOException e) {
logger.error("Could not start bot at "+bot.getServerHostname(), e);
} catch (IrcException e) {
logger.error("IrcException while starting bot at "+bot.getServerHostname(), e);
}
2017-11-15 17:49:03 +03:00
}
2018-03-27 16:43:42 +03:00
public static String extractServer(String s) {
String d[] = s.split("[\\.]");
return ""+d[d.length-2]+"."+d[d.length-1];
}
2017-11-15 17:49:03 +03:00
}