mirror of https://github.com/bvn13/JIrcBot.git
code review. all listeners become Spring Components and autowired now into main class
parent
cc8cf8cb42
commit
0e86884eb6
|
@ -42,10 +42,30 @@ public class JircBot extends ListenerAdapter {
|
|||
@Autowired
|
||||
public JircBot(JircBotConfiguration config) {
|
||||
this.config = config;
|
||||
//this.start();
|
||||
}
|
||||
|
||||
|
||||
@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 LinkPreviewListener linkPreviewListener;
|
||||
@Autowired
|
||||
private HelloOnJoinListener helloOnJoinListener;
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void start() {
|
||||
|
||||
|
@ -63,22 +83,25 @@ public class JircBot extends ListenerAdapter {
|
|||
String.format("%s/%s", c.getServer(), "1"), //c.getChannelName()),
|
||||
new PircBotX(templateConfig
|
||||
.setName(c.getBotName())
|
||||
.addListener(new PingPongListener())
|
||||
.addListener(new CalculatorListener()) //This class is a listener, so add it to the bots known listeners
|
||||
.addListener(new GoogleDoodleListener(this.config))
|
||||
.addListener(pingPongListener)
|
||||
.addListener(calculatorListener)
|
||||
.addListener(regexCheckerListener)
|
||||
.addListener(adviceListener)
|
||||
.addListener(quizListener)
|
||||
.addListener(bashOrgListener)
|
||||
.addListener(autoRejoinListener)
|
||||
|
||||
// disabled yet
|
||||
//.addListener(linkPreviewListener)
|
||||
//.addListener(helloOnJoinListener)
|
||||
|
||||
// not tested
|
||||
//.addListener(new GoogleDoodleListener(this.config))
|
||||
//.addListener(new GoogleSearchListener(this.config))
|
||||
//.addListener(new UrlRetrieverListener())
|
||||
.addListener(new RegexCheckerListener())
|
||||
//.addListener(new YandexSearchListener(this.config, this.yandexSearchService))
|
||||
.addListener(new AdviceListener())
|
||||
//.addListener(new LinkPreviewListener())
|
||||
//.addListener(new HelloOnJoinListener())
|
||||
.addListener(new QuizListener())
|
||||
.addListener(new BashOrgListener())
|
||||
.addListener(new AutoRejoinListener())
|
||||
|
||||
.setServers(servers)
|
||||
.setAutoReconnect(true)
|
||||
//.addAutoJoinChannel(c.getChannelName()) //Join the official #pircbotx channel
|
||||
.addAutoJoinChannels(c.getChannelsNames())
|
||||
.buildForServer(c.getServer())
|
||||
)
|
||||
|
|
|
@ -5,11 +5,13 @@ import org.pircbotx.hooks.events.JoinEvent;
|
|||
import org.pircbotx.hooks.events.KickEvent;
|
||||
import org.pircbotx.hooks.events.MessageEvent;
|
||||
import org.pircbotx.hooks.types.GenericMessageEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.bvn13.jircbot.listeners.advices.AdviceEngine;
|
||||
|
||||
/**
|
||||
* Created by bvn13 on 27.01.2018.
|
||||
*/
|
||||
@Component
|
||||
public class AutoRejoinListener extends ListenerAdapter {
|
||||
|
||||
private Boolean wasKicked = false;
|
||||
|
|
|
@ -7,6 +7,7 @@ import org.jsoup.select.Elements;
|
|||
import org.pircbotx.hooks.ListenerAdapter;
|
||||
import org.pircbotx.hooks.events.MessageEvent;
|
||||
import org.pircbotx.hooks.types.GenericMessageEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
|
@ -16,6 +17,7 @@ import java.net.URL;
|
|||
/**
|
||||
* Created by bvn13 on 26.01.2018.
|
||||
*/
|
||||
@Component
|
||||
public class BashOrgListener extends ListenerAdapter {
|
||||
|
||||
private static final String COMMAND = "?bash";
|
||||
|
|
|
@ -3,10 +3,12 @@ package ru.bvn13.jircbot.listeners;
|
|||
import org.pircbotx.hooks.ListenerAdapter;
|
||||
import org.pircbotx.hooks.events.JoinEvent;
|
||||
import org.pircbotx.hooks.events.MessageEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Created by bvn13 on 25.01.2018.
|
||||
*/
|
||||
@Component
|
||||
public class HelloOnJoinListener extends ListenerAdapter {
|
||||
|
||||
@Override
|
||||
|
|
|
@ -3,6 +3,7 @@ package ru.bvn13.jircbot.listeners;
|
|||
import org.pircbotx.hooks.ListenerAdapter;
|
||||
import org.pircbotx.hooks.events.MessageEvent;
|
||||
import org.pircbotx.hooks.types.GenericMessageEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
|
@ -21,6 +22,7 @@ import static java.lang.System.out;
|
|||
/**
|
||||
* Created by bvn13 on 23.01.2018.
|
||||
*/
|
||||
@Component
|
||||
public class LinkPreviewListener extends ListenerAdapter {
|
||||
|
||||
private static final Pattern REGEX = Pattern.compile("(?i)(?:(?:https?|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))\\.?)(?::\\d{2,5})?(?:[/?#]\\S*)?");
|
||||
|
|
|
@ -2,7 +2,9 @@ package ru.bvn13.jircbot.listeners;
|
|||
|
||||
import org.pircbotx.hooks.ListenerAdapter;
|
||||
import org.pircbotx.hooks.types.GenericMessageEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PingPongListener extends ListenerAdapter {
|
||||
|
||||
private static final String COMMAND = "?ping";
|
||||
|
|
|
@ -4,12 +4,14 @@ import org.pircbotx.hooks.ListenerAdapter;
|
|||
import org.pircbotx.hooks.WaitForQueue;
|
||||
import org.pircbotx.hooks.events.MessageEvent;
|
||||
import org.pircbotx.hooks.types.GenericMessageEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Component
|
||||
public class RegexCheckerListener extends ListenerAdapter {
|
||||
|
||||
private static final String COMMAND = "?regex ";
|
||||
|
|
|
@ -1,66 +0,0 @@
|
|||
package ru.bvn13.jircbot.listeners;
|
||||
|
||||
import org.pircbotx.dcc.ReceiveChat;
|
||||
import org.pircbotx.hooks.ListenerAdapter;
|
||||
import org.pircbotx.hooks.WaitForQueue;
|
||||
import org.pircbotx.hooks.events.IncomingChatRequestEvent;
|
||||
import org.pircbotx.hooks.events.MessageEvent;
|
||||
import org.pircbotx.hooks.types.GenericMessageEvent;
|
||||
|
||||
public class TestListener extends ListenerAdapter {
|
||||
|
||||
@Override
|
||||
public void onGenericMessage(final GenericMessageEvent event) throws Exception {
|
||||
//Hello world
|
||||
//This way to handle commands is useful for listeners that listen for multiple commands
|
||||
if (event.getMessage().startsWith("?hello"))
|
||||
event.respond("Hello World!");
|
||||
|
||||
//If this isn't a waittest, ignore
|
||||
//This way to handle commands is useful for listers that only listen for one command
|
||||
if (!event.getMessage().startsWith("?waitTest start"))
|
||||
return;
|
||||
|
||||
//WaitTest has started
|
||||
event.respond("Started...");
|
||||
WaitForQueue queue = new WaitForQueue(event.getBot());
|
||||
//Infinate loop since we might recieve messages that aren't WaitTest's.
|
||||
while (true) {
|
||||
//Use the waitFor() method to wait for a MessageEvent.
|
||||
//This will block (wait) until a message event comes in, ignoring
|
||||
//everything else
|
||||
MessageEvent currentEvent = queue.waitFor(MessageEvent.class);
|
||||
//Check if this message is the "ping" command
|
||||
if (currentEvent.getMessage().startsWith("?waitTest ping"))
|
||||
event.respond("pong");
|
||||
//Check if this message is the "end" command
|
||||
else if (currentEvent.getMessage().startsWith("?waitTest end")) {
|
||||
event.respond("Stopping");
|
||||
queue.close();
|
||||
//Very important that we end the infinate loop or else the test
|
||||
//will continue forever!
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onIncomingChatRequest(IncomingChatRequestEvent event) throws Exception {
|
||||
//Accept the incoming chat request. If it fails it will throw an exception
|
||||
ReceiveChat chat = event.accept();
|
||||
//Read lines from the server
|
||||
String line;
|
||||
while ((line = chat.readLine()) != null)
|
||||
if (line.equalsIgnoreCase("done")) {
|
||||
//Shut down the chat
|
||||
chat.close();
|
||||
break;
|
||||
} else {
|
||||
//Fun example
|
||||
int lineLength = line.length();
|
||||
chat.sendLine("Line '" + line + "' contains " + lineLength + " characters");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,111 +0,0 @@
|
|||
package ru.bvn13.jircbot.listeners;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.pircbotx.hooks.ListenerAdapter;
|
||||
import org.pircbotx.hooks.events.MessageEvent;
|
||||
import org.pircbotx.hooks.types.GenericMessageEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class UrlRetrieverListener extends ListenerAdapter {
|
||||
|
||||
private static final String PATTERN = "(?i)(?:(?:https?|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))\\.?)(?::\\d{2,5})?(?:[/?#]\\S*)?";
|
||||
//"(?i)^(?:(?:https?|ftp)://)(?:\\\\S+(?::\\\\S*)?@)?(?:(?!(?:10|127)(?:\\\\.\\\\d{1,3}){3})(?!(?:169\\\\.254|192\\\\.168)(?:\\\\.\\\\d{1,3}){2})(?!172\\\\.(?:1[6-9]|2\\\\d|3[0-1])(?:\\\\.\\\\d{1,3}){2})(?:[1-9]\\\\d?|1\\\\d\\\\d|2[01]\\\\d|22[0-3])(?:\\\\.(?:1?\\\\d{1,2}|2[0-4]\\\\d|25[0-5])){2}(?:\\\\.(?:[1-9]\\\\d?|1\\\\d\\\\d|2[0-4]\\\\d|25[0-4]))|(?:(?:[a-z\\\\u00a1-\\\\uffff0-9]-*)*[a-z\\\\u00a1-\\\\uffff0-9]+)(?:\\\\.(?:[a-z\\\\u00a1-\\\\uffff0-9]-*)*[a-z\\\\u00a1-\\\\uffff0-9]+)*(?:\\\\.(?:[a-z\\\\u00a1-\\\\uffff]{2,}))\\\\.?)(?::\\\\d{2,5})?(?:[/?#]\\\\S*)?$";
|
||||
|
||||
|
||||
public static class UrlInfo {
|
||||
@Getter
|
||||
@Setter
|
||||
private String url = "";
|
||||
@Getter
|
||||
@Setter
|
||||
private String title = "";
|
||||
@Getter
|
||||
@Setter
|
||||
private String description = "";
|
||||
@Getter
|
||||
@Setter
|
||||
private Boolean error = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGenericMessage(final GenericMessageEvent event) throws Exception {
|
||||
|
||||
if (event.getUser().getUserId().equals(event.getBot().getUserBot().getUserId())) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> foundUrls = this.findUrl(event.getMessage().trim());
|
||||
if (foundUrls.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (String url : foundUrls) {
|
||||
UrlInfo info = this.checkUrl(url);
|
||||
if (info.getError()) {
|
||||
event.respond(info.getDescription());
|
||||
} else {
|
||||
//event.respondWith(String.format("/notice %s %s", ((MessageEvent) event).getChannel().getName(), String.format("%s (%s)", info.getTitle(), info.getDescription())));
|
||||
event.getBot().sendIRC().notice(((MessageEvent) event).getChannel().getName(), String.format("%s (%s)", info.getTitle(), info.getDescription()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private List<String> findUrl(String message) {
|
||||
List<String> result = new ArrayList<>();
|
||||
try {
|
||||
Pattern pattern = Pattern.compile(PATTERN);
|
||||
Matcher matcher = pattern.matcher(message);
|
||||
while (matcher.find()) {
|
||||
result.add(matcher.group());
|
||||
}
|
||||
} catch (RuntimeException rte) {
|
||||
rte.printStackTrace();
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private UrlInfo checkUrl(String url) {
|
||||
UrlInfo info = new UrlInfo();
|
||||
info.setUrl(url);
|
||||
|
||||
Document doc = null;
|
||||
try {
|
||||
doc = Jsoup.connect(url).get();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
info.setError(true);
|
||||
info.setDescription("ERROR retrieving url: "+url);
|
||||
return info;
|
||||
}
|
||||
try {
|
||||
Element title = doc.head().select("title").first();
|
||||
info.setTitle(title.text());
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
//info.setError(false);
|
||||
info.setDescription("ERROR retrieving title: "+url);
|
||||
}
|
||||
try {
|
||||
Element descr = doc.head().select("meta[name=\"description\"]").first();
|
||||
if (descr != null && descr.attr("content") != null) {
|
||||
info.setDescription(descr.attr("content").trim());
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
}
|
|
@ -8,6 +8,7 @@ import org.pircbotx.hooks.ListenerAdapter;
|
|||
import org.pircbotx.hooks.events.MessageEvent;
|
||||
import org.pircbotx.hooks.events.NoticeEvent;
|
||||
import org.pircbotx.hooks.types.GenericMessageEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
|
@ -18,6 +19,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||
/**
|
||||
* Created by bvn13 on 23.01.2018.
|
||||
*/
|
||||
@Component
|
||||
public class AdviceListener extends ListenerAdapter {
|
||||
|
||||
private static final String COMMAND = "?advice";
|
||||
|
|
|
@ -1,17 +1,14 @@
|
|||
package ru.bvn13.jircbot.listeners.calculator;
|
||||
|
||||
import net.objecthunter.exp4j.Expression;
|
||||
import net.objecthunter.exp4j.ExpressionBuilder;
|
||||
import org.pircbotx.hooks.ListenerAdapter;
|
||||
import org.pircbotx.hooks.WaitForQueue;
|
||||
import org.pircbotx.hooks.events.MessageEvent;
|
||||
import org.pircbotx.hooks.types.GenericMessageEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
@Component
|
||||
public class CalculatorListener extends ListenerAdapter {
|
||||
|
||||
private Map<UUID, CalculatorDialog> dialogs = new HashMap<>();
|
||||
|
|
|
@ -3,6 +3,7 @@ package ru.bvn13.jircbot.listeners.quiz;
|
|||
import org.pircbotx.hooks.ListenerAdapter;
|
||||
import org.pircbotx.hooks.events.MessageEvent;
|
||||
import org.pircbotx.hooks.types.GenericMessageEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.bvn13.fsm.Exceptions.FSMException;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
@ -11,6 +12,7 @@ import java.util.Map;
|
|||
/**
|
||||
* Created by bvn13 on 26.01.2018.
|
||||
*/
|
||||
@Component
|
||||
public class QuizListener extends ListenerAdapter {
|
||||
|
||||
private Map<String, QuizDialog> dialogs = new HashMap<>();
|
||||
|
|
Loading…
Reference in New Issue