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;
|
2018-03-27 16:08:26 +03:00
|
|
|
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;
|
2018-03-28 10:53:07 +03:00
|
|
|
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;
|
2017-11-16 15:26:44 +03:00
|
|
|
import ru.bvn13.jircbot.listeners.*;
|
2018-01-27 09:44:15 +03:00
|
|
|
import ru.bvn13.jircbot.listeners.advices.AdviceListener;
|
2018-01-09 12:01:55 +03:00
|
|
|
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;
|
2018-03-28 10:53:07 +03:00
|
|
|
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;
|
2018-02-01 11:28:28 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-01 11:28:28 +03:00
|
|
|
private ScheduledExecutorService executorService;
|
|
|
|
|
2018-03-28 20:47:13 +03:00
|
|
|
private MultiBotManager manager = new MultiBotManager();
|
2018-02-01 11:28:28 +03:00
|
|
|
|
2018-01-30 17:22:27 +03:00
|
|
|
@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;
|
2018-01-31 22:05:04 +03:00
|
|
|
@Autowired
|
|
|
|
private DeferredMessagesListener deferredMessagesListener;
|
2018-01-30 17:22:27 +03:00
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private LinkPreviewListener linkPreviewListener;
|
|
|
|
@Autowired
|
|
|
|
private HelloOnJoinListener helloOnJoinListener;
|
|
|
|
|
2018-02-04 18:59:36 +03:00
|
|
|
@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;
|
|
|
|
|
2018-03-27 13:55:54 +03:00
|
|
|
@Autowired
|
|
|
|
private AdminListener adminListener;
|
|
|
|
|
2017-11-17 01:22:18 +03:00
|
|
|
@PostConstruct
|
2018-02-01 11:28:28 +03:00
|
|
|
public void postConstruct() {
|
2018-03-28 20:47:13 +03:00
|
|
|
logger.warn("VERSION: "+version);
|
2018-03-28 10:53:07 +03:00
|
|
|
this.executorService = Executors.newScheduledThreadPool(10);
|
2018-02-01 11:28:28 +03:00
|
|
|
this.executorService.schedule(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2018-03-28 10:53:07 +03:00
|
|
|
initBots();
|
|
|
|
startBots();
|
2018-02-01 11:28:28 +03:00
|
|
|
}
|
|
|
|
}, 5, TimeUnit.SECONDS);
|
2018-03-28 10:53:07 +03:00
|
|
|
this.executorService.scheduleAtFixedRate(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2018-03-28 20:47:13 +03:00
|
|
|
//logger.warn("check");
|
2018-03-28 10:53:07 +03:00
|
|
|
checkBots();
|
|
|
|
}
|
2018-03-28 20:47:13 +03:00
|
|
|
}, 30, 5, TimeUnit.SECONDS);
|
2018-02-01 11:28:28 +03:00
|
|
|
}
|
|
|
|
|
2018-03-28 10:53:07 +03:00
|
|
|
@PreDestroy
|
|
|
|
public void preDestroy() {
|
2018-03-28 20:47:13 +03:00
|
|
|
logger.warn("Bot is shutting down...");
|
2018-03-28 10:53:07 +03:00
|
|
|
this.executorService.shutdown();
|
2018-03-28 20:47:13 +03:00
|
|
|
this.manager.stop("Bot is shutting down...");
|
2018-03-28 10:53:07 +03:00
|
|
|
}
|
2018-02-01 11:28:28 +03:00
|
|
|
|
2017-11-15 17:49:03 +03:00
|
|
|
|
2018-03-28 10:53:07 +03:00
|
|
|
private void initBots() {
|
2018-03-28 20:47:13 +03:00
|
|
|
logger.info(">>>>>>>>>>>>>>>>>>>> BOT INIT : "+getVersion()+" <<<<<<<<<<<<<<<<<<<<");
|
2018-02-01 11:28:28 +03:00
|
|
|
|
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()));
|
2018-03-28 11:31:24 +03:00
|
|
|
|
|
|
|
Configuration.Builder confBuilder = templateConfig
|
2018-03-28 20:47:13 +03:00
|
|
|
.setRealName("JIrcBot v"+getVersion()+" | github.com/bvn13/JIrcBot")
|
2018-03-28 11:31:24 +03:00
|
|
|
.setName(c.getBotName())
|
|
|
|
.addListener(adminListener)
|
|
|
|
.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(
|
2018-01-31 22:05:04 +03:00
|
|
|
String.format("%s/%s", c.getServer(), "1"),
|
2018-03-28 11:31:24 +03:00
|
|
|
new PircBotX(confBuilder.buildForServer(c.getServer())
|
2017-11-15 17:49:03 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
2018-03-28 10:53:07 +03:00
|
|
|
}
|
2017-11-15 17:49:03 +03:00
|
|
|
|
2018-03-28 10:53:07 +03:00
|
|
|
|
|
|
|
private void startBots() {
|
|
|
|
logger.info(">>>>>>>>>>>>>>>>>>>> BOT STARTING <<<<<<<<<<<<<<<<<<<<");
|
2018-03-27 16:08:26 +03:00
|
|
|
|
|
|
|
this.bots.forEach((id, bot) -> {
|
|
|
|
manager.addBot(bot);
|
2017-11-15 17:49:03 +03:00
|
|
|
});
|
|
|
|
|
2018-03-27 16:08:26 +03:00
|
|
|
manager.start();
|
2018-03-28 10:53:07 +03:00
|
|
|
}
|
2018-03-27 16:08:26 +03:00
|
|
|
|
2018-03-28 10:53:07 +03:00
|
|
|
private void checkBots() {
|
2018-03-28 20:47:13 +03:00
|
|
|
|
2018-03-28 10:53:07 +03:00
|
|
|
this.manager.getBots().forEach(bot -> {
|
|
|
|
if (bot.getState().equals(PircBotX.State.DISCONNECTED)) {
|
|
|
|
try {
|
|
|
|
bot.startBot();
|
|
|
|
} catch (IOException e) {
|
2018-03-28 20:47:13 +03:00
|
|
|
logger.error("Could not start bot at "+bot.getServerHostname(), e);
|
2018-03-28 10:53:07 +03:00
|
|
|
} catch (IrcException e) {
|
2018-03-28 20:47:13 +03:00
|
|
|
logger.error("IrcException while starting bot at "+bot.getServerHostname(), e);
|
2018-03-28 10:53:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
|
|
|
|
}
|