Channel consumer flow structure corrections: separating things inside application architecture to finally get determined core and independent standardized subroutines with appropriate interfaces
This commit is contained in:
parent
cd450475f7
commit
2c9760785e
21 changed files with 445 additions and 349 deletions
|
@ -15,7 +15,7 @@ public class CTCPHelper {
|
|||
private final HashMap<String, List<CTCPRequest>> waitersQueue = new HashMap<>();
|
||||
private CTCPHelper(){}
|
||||
|
||||
void registerRequest(String server, String requesterChanelOrUser, String ctcpType, String target, String notFoundMessage){
|
||||
public void registerRequest(String server, String requesterChanelOrUser, String ctcpType, String target, String notFoundMessage){
|
||||
if (! waitersQueue.containsKey(server)){ // TODO: meeeeeeeehh.. looks bad
|
||||
waitersQueue.put(server, new ArrayList<>());
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ public class CTCPHelper {
|
|||
while (iterator.hasNext()){
|
||||
CTCPRequest current = iterator.next();
|
||||
if (current.isValid(currentTime)){
|
||||
String channelOrUser = current.getRequesterChanelOrUser(whoReplied);
|
||||
String channelOrUser = current.getRequester(whoReplied);
|
||||
|
||||
if (channelOrUser == null || ! current.getType().equals(whatReplied.replaceAll("\\s.*$", "")))
|
||||
continue;
|
||||
|
@ -76,7 +76,7 @@ public class CTCPHelper {
|
|||
CTCPRequest current = iterator.next();
|
||||
if (! current.isValid(currentTime))
|
||||
iterator.remove();
|
||||
String channelOrUser = current.getRequesterChanelOrUser(whoNotFound);
|
||||
String channelOrUser = current.getRequester(whoNotFound);
|
||||
|
||||
if (channelOrUser == null)
|
||||
continue;
|
||||
|
|
|
@ -17,7 +17,7 @@ class CTCPRequest {
|
|||
this.CTCPtype = CTCPType;
|
||||
}
|
||||
|
||||
String getRequesterChanelOrUser(String userResponds){ // return channel name
|
||||
String getRequester(String userResponds){ // return channel name
|
||||
if (userResponding.equals(userResponds))
|
||||
return requesterChanelOrUser;
|
||||
return null;
|
||||
|
|
|
@ -1,271 +1,111 @@
|
|||
package InnaIrcBot.Commanders;
|
||||
|
||||
import InnaIrcBot.ProvidersConsumers.StreamProvider;
|
||||
import InnaIrcBot.Commanders.flood.EventHandler;
|
||||
import InnaIrcBot.Commanders.flood.JoinCloneHandler;
|
||||
import InnaIrcBot.Commanders.flood.JoinFloodHandler;
|
||||
import InnaIrcBot.Commanders.talk.TalkGenericHandler;
|
||||
import InnaIrcBot.Commanders.talk.TalkHandler;
|
||||
import InnaIrcBot.Commanders.talk.TalkZeroHandler;
|
||||
import InnaIrcBot.config.ConfigurationChannel;
|
||||
import InnaIrcBot.config.ConfigurationManager;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.regex.Pattern;
|
||||
//TODO: FLOOD
|
||||
|
||||
public class ChanelCommander implements Runnable {
|
||||
private final BlockingQueue<String> streamQueue;
|
||||
private final BlockingQueue<String> commanderQueue;
|
||||
private final String server;
|
||||
private final String channel;
|
||||
//TODO: add timers
|
||||
private HashMap<String, String[]> joinMap; // Mask(Pattern) ->, Action | Where Action[0] could be: raw
|
||||
private HashMap<String, String[]> msgMap; // Mask(Pattern) ->, Action | Where Action[0] could be: raw
|
||||
private HashMap<String, String[]> nickMap; // Mask(Pattern) ->, Action | Where Action[0] could be: raw
|
||||
//TODO: add timers, flood
|
||||
private TalkHandler talkHandler;
|
||||
private final List<EventHandler> eventHandlers;
|
||||
|
||||
private boolean joinFloodTrackNeed;
|
||||
private JoinFloodHandler jfh;
|
||||
|
||||
private boolean joinCloneTrackNeed;
|
||||
private JoinCloneHandler jch;
|
||||
|
||||
public ChanelCommander(BlockingQueue<String> stream, String serverName, String channel) throws Exception{
|
||||
this.streamQueue = stream;
|
||||
this.server = serverName;
|
||||
public ChanelCommander(BlockingQueue<String> commanderQueue, String server, String channel) throws Exception{
|
||||
this.commanderQueue = commanderQueue;
|
||||
this.server = server;
|
||||
this.channel = channel;
|
||||
readConfing();
|
||||
this.eventHandlers = new ArrayList<>();
|
||||
readConfig();
|
||||
}
|
||||
private void readConfig() throws Exception{
|
||||
ConfigurationChannel configChannel = ConfigurationManager.getConfiguration(server).getChannelConfig(channel);
|
||||
|
||||
if (configChannel == null){
|
||||
talkHandler = new TalkZeroHandler();
|
||||
return;
|
||||
}
|
||||
|
||||
talkHandler = new TalkGenericHandler(
|
||||
server, channel,
|
||||
configChannel.getJoinMap(),
|
||||
configChannel.getMsgMap(),
|
||||
configChannel.getNickMap()
|
||||
);
|
||||
|
||||
if (configChannel.isJoinFloodControl()) {
|
||||
JoinFloodHandler jfh = new JoinFloodHandler(
|
||||
server, channel,
|
||||
configChannel.getJoinFloodControlEvents(),
|
||||
configChannel.getJoinFloodControlTimeframe());
|
||||
eventHandlers.add(jfh);
|
||||
}
|
||||
|
||||
if (configChannel.isJoinCloneControl()) {
|
||||
JoinCloneHandler jch = new JoinCloneHandler(
|
||||
server, channel,
|
||||
configChannel.getJoinCloneControlPattern(),
|
||||
configChannel.getJoinCloneControlTimeframe());
|
||||
eventHandlers.add(jch);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("["+LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"))+"] ChanelCommander thread "
|
||||
+server+":"+this.channel +" started");// TODO:REMOVE DEBUG
|
||||
runRoutine();
|
||||
System.out.println("["+LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"))+"] ChanelCommander thread "
|
||||
+server+":"+this.channel +" ended");// TODO:REMOVE DEBUG
|
||||
}
|
||||
|
||||
private void runRoutine(){
|
||||
try {
|
||||
while (true) {
|
||||
String data = streamQueue.take();
|
||||
String[] dataStrings = data.split(" :?",3);
|
||||
|
||||
switch (dataStrings[1]) {
|
||||
case "NICK":
|
||||
nickCame(dataStrings[2]+dataStrings[0].replaceAll("^.+?!","!"));
|
||||
break; // todo: need to track join flood
|
||||
case "JOIN":
|
||||
if (joinFloodTrackNeed)
|
||||
jfh.track(simplifyNick(dataStrings[0]));
|
||||
if (joinCloneTrackNeed)
|
||||
jch.track(dataStrings[0]);
|
||||
joinCame(dataStrings[0]);
|
||||
break;
|
||||
case "PRIVMSG":
|
||||
privmsgCame(dataStrings[0], dataStrings[2]);
|
||||
break;
|
||||
/*
|
||||
case "PART": // todo: need to track join flood? Fuck that. Track using JOIN
|
||||
case "QUIT": // todo: need this?
|
||||
case "TOPIC": // todo: need this?
|
||||
case "MODE": // todo: need this?
|
||||
case "KICK": // todo: need this? */
|
||||
default:
|
||||
break;
|
||||
}
|
||||
parse();
|
||||
}
|
||||
}
|
||||
catch (InterruptedException ie){
|
||||
System.out.println("ChanelCommander interrupted.");
|
||||
}
|
||||
System.out.println("["+LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"))+"] ChanelCommander thread "
|
||||
+server+":"+this.channel +" ended");// TODO:REMOVE DEBUG
|
||||
}
|
||||
|
||||
// Do we need old nick?
|
||||
private void nickCame(String newNick){
|
||||
came(nickMap, newNick, newNick);
|
||||
private void parse() throws InterruptedException{
|
||||
String data = commanderQueue.take();
|
||||
String[] dataStrings = data.split(" :?",3);
|
||||
|
||||
switch (dataStrings[1]) {
|
||||
case "NICK":
|
||||
talkHandler.nickCame(dataStrings[2]+dataStrings[0].replaceAll("^.+?!","!"));
|
||||
break; // todo: need to track join flood
|
||||
case "JOIN":
|
||||
for (EventHandler handler : eventHandlers){
|
||||
handler.track(dataStrings[0]);
|
||||
}
|
||||
private void joinCame(String who){
|
||||
came(joinMap, who, who);
|
||||
}
|
||||
private void privmsgCame(String who, String what){
|
||||
if (what.indexOf(":")+1 < what.length()){
|
||||
what = what.substring(what.indexOf(":")+1);
|
||||
came(msgMap, what, who);
|
||||
}
|
||||
}
|
||||
|
||||
private void came(HashMap<String, String[]> map, String arg1, String arg2){
|
||||
for (String pattern : map.keySet())
|
||||
if (Pattern.matches(pattern, arg1)){ // NOTE: validation based on new nick //TODO: parse here
|
||||
String[] cmdOrMsg = map.get(pattern);
|
||||
|
||||
StringBuilder whatToSendStringBuilder;
|
||||
ArrayList<String> whatToSendList;
|
||||
for (int i = 0; i < cmdOrMsg.length;) {
|
||||
switch (cmdOrMsg[i]) {
|
||||
case "\\chanmsg":
|
||||
whatToSendList = new ArrayList<>();
|
||||
for (i++; (i < cmdOrMsg.length) && !(cmdOrMsg[i].startsWith("\\")); i++)
|
||||
whatToSendList.add(cmdOrMsg[i]);
|
||||
msgAction(whatToSendList.toArray(new String[0]), arg2, false);
|
||||
talkHandler.joinCame(dataStrings[0]);
|
||||
break;
|
||||
case "\\privmsg":
|
||||
whatToSendList = new ArrayList<>();
|
||||
for (i++; (i < cmdOrMsg.length) && !(cmdOrMsg[i].startsWith("\\")); i++)
|
||||
whatToSendList.add(cmdOrMsg[i]);
|
||||
msgAction(whatToSendList.toArray(new String[0]), arg2, true);
|
||||
break;
|
||||
case "\\ban":
|
||||
banAction(arg2);
|
||||
i++;
|
||||
break;
|
||||
case "\\voice":
|
||||
voiceAction(arg2);
|
||||
i++;
|
||||
break;
|
||||
case "\\kick":
|
||||
whatToSendList = new ArrayList<>();
|
||||
for (i++; (i < cmdOrMsg.length) && !(cmdOrMsg[i].startsWith("\\")); i++)
|
||||
whatToSendList.add(cmdOrMsg[i]);
|
||||
kickAction(whatToSendList.toArray(new String[0]), arg2);
|
||||
break;
|
||||
case "\\kickban":
|
||||
whatToSendList = new ArrayList<>();
|
||||
for (i++; (i < cmdOrMsg.length) && !(cmdOrMsg[i].startsWith("\\")); i++)
|
||||
whatToSendList.add(cmdOrMsg[i]);
|
||||
banAction(arg2);
|
||||
kickAction(whatToSendList.toArray(new String[0]), arg2);
|
||||
break;
|
||||
case "\\raw":
|
||||
whatToSendStringBuilder = new StringBuilder();
|
||||
for (i++; (i < cmdOrMsg.length) && !(cmdOrMsg[i].startsWith("\\")); i++)
|
||||
whatToSendStringBuilder.append(cmdOrMsg[i]);
|
||||
StreamProvider.writeToStream(server, whatToSendStringBuilder.toString()); //TODO
|
||||
break; //todo: add script
|
||||
case "\\whois": // result will be noted in 'system' log
|
||||
whoisAction(arg2);
|
||||
i++;
|
||||
break;
|
||||
case "\\cclientinfo": // NOTE: All this handled by CTCPHelper instance
|
||||
case "\\cfinger": // C - publish request result to chan
|
||||
case "\\cping":
|
||||
case "\\csource":
|
||||
case "\\ctime":
|
||||
case "\\cuserinfo":
|
||||
case "\\cversion":
|
||||
case "\\pclientinfo": // P - reply to privmsg
|
||||
case "\\pfinger":
|
||||
case "\\pping":
|
||||
case "\\psource":
|
||||
case "\\ptime":
|
||||
case "\\puserinfo":
|
||||
case "\\pversion":
|
||||
String CTCPType = cmdOrMsg[i];
|
||||
String objectRegexp = null;
|
||||
whatToSendStringBuilder = new StringBuilder();
|
||||
|
||||
for (i++; (i < cmdOrMsg.length) && !(cmdOrMsg[i].startsWith("\\")); i++){
|
||||
if (objectRegexp == null && !cmdOrMsg[i].trim().isEmpty())
|
||||
objectRegexp = cmdOrMsg[i].trim();
|
||||
else
|
||||
whatToSendStringBuilder.append(cmdOrMsg[i]);
|
||||
}
|
||||
|
||||
if (objectRegexp == null)
|
||||
break;
|
||||
|
||||
String objectToCtcp = arg1.trim().replaceAll(objectRegexp, ""); // note: trim() ?
|
||||
|
||||
if (objectToCtcp.isEmpty())
|
||||
break;
|
||||
|
||||
if (CTCPType.startsWith("\\c"))
|
||||
registerCTCPforChannel(CTCPType.substring(2).toUpperCase(), objectToCtcp, whatToSendStringBuilder.toString());
|
||||
else
|
||||
registerCTCPforUser(simplifyNick(arg2), CTCPType.substring(2).toUpperCase(), objectToCtcp, whatToSendStringBuilder.toString());
|
||||
|
||||
case "PRIVMSG":
|
||||
talkHandler.privmsgCame(dataStrings[0], dataStrings[2]);
|
||||
break;
|
||||
/* case "PART":
|
||||
case "QUIT":
|
||||
case "TOPIC":
|
||||
case "MODE":
|
||||
case "KICK": */
|
||||
default:
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////// /////////
|
||||
private void registerCTCPforChannel(String CTCPType, String object, String message){
|
||||
CTCPHelper.getInstance().registerRequest(server, channel, CTCPType, object, message);
|
||||
}
|
||||
private void registerCTCPforUser(String user, String CTCPType, String object, String message){
|
||||
CTCPHelper.getInstance().registerRequest(server, user, CTCPType, object, message);
|
||||
}
|
||||
private void whoisAction(String who){ // TODO: maybe we have to extend functionality to reuse received information.
|
||||
StreamProvider.writeToStream(server, "WHOIS "+simplifyNick(who));
|
||||
}
|
||||
|
||||
private void msgAction(String[] messages, String who, boolean isToUser){
|
||||
StringBuilder executiveStr = new StringBuilder();
|
||||
executiveStr.append("PRIVMSG ");
|
||||
if(isToUser) {
|
||||
executiveStr.append(simplifyNick(who));
|
||||
executiveStr.append(" :");
|
||||
}
|
||||
else {
|
||||
executiveStr.append(channel);
|
||||
executiveStr.append(" :");
|
||||
executiveStr.append(simplifyNick(who));
|
||||
executiveStr.append(": ");
|
||||
}
|
||||
|
||||
for (String message : messages) {
|
||||
if (!message.startsWith("\\"))
|
||||
executiveStr.append(message);
|
||||
else if (message.equals("\\time")) // TODO: remove this shit
|
||||
executiveStr.append(LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
|
||||
}
|
||||
//System.out.println(executiveStr.toString()); //TODO: debug
|
||||
StreamProvider.writeToStream(server, executiveStr.toString());
|
||||
}
|
||||
private void banAction(String whom){
|
||||
StreamProvider.writeToStream(server, "MODE "+ channel +" +b "+simplifyNick(whom)+"*!*@*");
|
||||
StreamProvider.writeToStream(server, "MODE "+ channel +" +b "+"*!*@"+whom.replaceAll("^.+@",""));
|
||||
}
|
||||
private void voiceAction(String whom){
|
||||
StreamProvider.writeToStream(server, "MODE "+ channel +" +v "+simplifyNick(whom));
|
||||
}
|
||||
private void kickAction(String[] messages, String whom){
|
||||
StringBuilder executiveStr = new StringBuilder();
|
||||
executiveStr.append("KICK ");
|
||||
executiveStr.append(channel);
|
||||
executiveStr.append(" ");
|
||||
executiveStr.append(simplifyNick(whom));
|
||||
executiveStr.append(" :");
|
||||
|
||||
for (String message : messages) {
|
||||
if (!message.startsWith("\\"))
|
||||
executiveStr.append(message);
|
||||
else if (message.equals("\\time"))
|
||||
executiveStr.append(LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
|
||||
}
|
||||
StreamProvider.writeToStream(server, executiveStr.toString());
|
||||
}
|
||||
|
||||
private String simplifyNick(String nick){ return nick.replaceAll("!.*$",""); }
|
||||
|
||||
private void readConfing() throws Exception{
|
||||
ConfigurationChannel configChannel = ConfigurationManager.getConfiguration(server).getChannelConfig(channel);
|
||||
if (configChannel == null){
|
||||
joinMap = new HashMap<>();
|
||||
msgMap = new HashMap<>();
|
||||
nickMap = new HashMap<>();
|
||||
return;
|
||||
}
|
||||
|
||||
joinMap = configChannel.getJoinMap();
|
||||
msgMap = configChannel.getMsgMap();
|
||||
nickMap = configChannel.getNickMap();
|
||||
|
||||
if (configChannel.isJoinFloodControl()) {
|
||||
jfh = new JoinFloodHandler(configChannel.getJoinFloodControlEvents(), configChannel.getJoinFloodControlTimeframe(), server, channel);
|
||||
joinFloodTrackNeed = true;
|
||||
}
|
||||
if (configChannel.isJoinCloneControl()) {
|
||||
jch = new JoinCloneHandler(configChannel.getJoinCloneControlPattern(), configChannel.getJoinCloneControlTimeframe(), server, channel); // TODO: REMOVE
|
||||
joinCloneTrackNeed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package InnaIrcBot.Commanders.flood;
|
||||
|
||||
public interface EventHandler {
|
||||
void track(String user);
|
||||
}
|
|
@ -1,24 +1,24 @@
|
|||
package InnaIrcBot.Commanders;
|
||||
package InnaIrcBot.Commanders.flood;
|
||||
|
||||
import InnaIrcBot.ProvidersConsumers.StreamProvider;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class JoinCloneHandler {
|
||||
public class JoinCloneHandler implements EventHandler {
|
||||
|
||||
private String pattern;
|
||||
private String server;
|
||||
private String chanel;
|
||||
private int timeFrameInSeconds;
|
||||
private final String pattern;
|
||||
private final String server;
|
||||
private final String channel;
|
||||
private final int timeFrameInSeconds;
|
||||
|
||||
private LocalDateTime lastCame;
|
||||
private String prevUserNick;
|
||||
|
||||
public JoinCloneHandler(String pattern, int timeFrameInSeconds, String serverName, String chanelName){
|
||||
public JoinCloneHandler(String server, String channel, String pattern, int timeFrameInSeconds){
|
||||
this.server = server;
|
||||
this.channel = channel;
|
||||
this.pattern = pattern;
|
||||
this.timeFrameInSeconds = timeFrameInSeconds;
|
||||
this.server = serverName;
|
||||
this.chanel = chanelName;
|
||||
prevUserNick = "";
|
||||
lastCame = LocalDateTime.now().minusDays(1L);
|
||||
}
|
||||
|
@ -39,25 +39,26 @@ public class JoinCloneHandler {
|
|||
}
|
||||
*/
|
||||
// RUSNET
|
||||
@Override
|
||||
public void track(String userNick){
|
||||
if (userNick.matches(pattern)){
|
||||
if (lastCame.isAfter(LocalDateTime.now().minusSeconds(timeFrameInSeconds)) && !prevUserNick.equals(userNick)){
|
||||
if (getNickOnly(userNick).replaceAll("[0-9].*", "").length() > 2){
|
||||
StreamProvider.writeToStream(server,
|
||||
"MODE "+chanel+" +b "+userNick.replaceAll("[0-9].*", "*!*@*")+"\n"+
|
||||
"MODE "+chanel+" +b *!*@"+getIdentHost(userNick)+"*\n"+
|
||||
"MODE "+chanel+" +b "+prevUserNick.replaceAll("[0-9].*", "*!*@*")+"\n"+
|
||||
"MODE "+chanel+" +b *!*@"+getIdentHost(prevUserNick)+"*\n"+
|
||||
"KICK "+chanel+" "+getNickOnly(userNick)+" :clone\n"+
|
||||
"KICK "+chanel+" "+getNickOnly(prevUserNick)+" :clone"
|
||||
"MODE "+ channel +" +b "+userNick.replaceAll("[0-9].*", "*!*@*")+"\n"+
|
||||
"MODE "+ channel +" +b *!*@"+getIdentHost(userNick)+"*\n"+
|
||||
"MODE "+ channel +" +b "+prevUserNick.replaceAll("[0-9].*", "*!*@*")+"\n"+
|
||||
"MODE "+ channel +" +b *!*@"+getIdentHost(prevUserNick)+"*\n"+
|
||||
"KICK "+ channel +" "+getNickOnly(userNick)+" :clone\n"+
|
||||
"KICK "+ channel +" "+getNickOnly(prevUserNick)+" :clone"
|
||||
);
|
||||
}
|
||||
else {
|
||||
StreamProvider.writeToStream(server,
|
||||
"MODE "+chanel+" +b *!*@"+getIdentHost(userNick)+"*\n"+
|
||||
"MODE "+chanel+" +b *!*@"+getIdentHost(prevUserNick)+"*\n"+
|
||||
"KICK "+chanel+" "+getNickOnly(userNick)+" :clone\n"+
|
||||
"KICK "+chanel+" "+getNickOnly(prevUserNick)+" :clone"
|
||||
"MODE "+ channel +" +b *!*@"+getIdentHost(userNick)+"*\n"+
|
||||
"MODE "+ channel +" +b *!*@"+getIdentHost(prevUserNick)+"*\n"+
|
||||
"KICK "+ channel +" "+getNickOnly(userNick)+" :clone\n"+
|
||||
"KICK "+ channel +" "+getNickOnly(prevUserNick)+" :clone"
|
||||
);
|
||||
}
|
||||
|
|
@ -1,34 +1,36 @@
|
|||
package InnaIrcBot.Commanders;
|
||||
package InnaIrcBot.Commanders.flood;
|
||||
|
||||
import InnaIrcBot.ProvidersConsumers.StreamProvider;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
public class JoinFloodHandler {
|
||||
public class JoinFloodHandler implements EventHandler {
|
||||
private final int joinMaxNumber; // How many events should happens before we start validation
|
||||
private final int timeFrameInSeconds; // For which period critical amount of events should happens
|
||||
private final String server;
|
||||
private final String channel;
|
||||
protected final HashMap<String, LinkedList<LocalDateTime>> users;
|
||||
|
||||
public JoinFloodHandler(int joinMaxNumber, int timeFrameInSeconds, String serverName, String channelName){
|
||||
public JoinFloodHandler(String server, String channel, int joinMaxNumber, int timeFrameInSeconds){
|
||||
this.server = server;
|
||||
this.channel = channel;
|
||||
this.joinMaxNumber = joinMaxNumber;
|
||||
this.timeFrameInSeconds = timeFrameInSeconds;
|
||||
this.server = serverName;
|
||||
this.channel = channelName;
|
||||
this.users = new HashMap<>();
|
||||
}
|
||||
@Override
|
||||
public void track(String nick){
|
||||
nick = simplifyNick(nick);
|
||||
|
||||
public void track(String userNickname){
|
||||
if (isNewcomer(userNickname)) {
|
||||
registerNewUser(userNickname);
|
||||
if (isNewcomer(nick)) {
|
||||
registerNewUser(nick);
|
||||
return;
|
||||
}
|
||||
|
||||
if(isJoinFlooder(userNickname)){
|
||||
kickBanUser(userNickname);
|
||||
users.remove(userNickname);
|
||||
if(isJoinFlooder(nick)){
|
||||
kickBanUser(nick);
|
||||
users.remove(nick);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -67,4 +69,6 @@ public class JoinFloodHandler {
|
|||
"PRIVMSG "+ channel +" :"+user+": join flood ("+ joinMaxNumber +" connections in "+timeFrameInSeconds+" seconds).\n"+
|
||||
"MODE "+ channel +" +b "+user+"!*@*"); // TODO: consider other ban methods
|
||||
}
|
||||
|
||||
private String simplifyNick(String nick){ return nick.replaceAll("!.*$",""); }
|
||||
}
|
202
src/main/java/InnaIrcBot/Commanders/talk/TalkGenericHandler.java
Normal file
202
src/main/java/InnaIrcBot/Commanders/talk/TalkGenericHandler.java
Normal file
|
@ -0,0 +1,202 @@
|
|||
package InnaIrcBot.Commanders.talk;
|
||||
|
||||
import InnaIrcBot.Commanders.CTCPHelper;
|
||||
import InnaIrcBot.ProvidersConsumers.StreamProvider;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class TalkGenericHandler implements TalkHandler{
|
||||
private final String server;
|
||||
private final String channel;
|
||||
private final HashMap<String, String[]> joinMap; // Mask(Pattern) ->, Action | Where Action[0] could be: raw
|
||||
private final HashMap<String, String[]> msgMap; // Mask(Pattern) ->, Action | Where Action[0] could be: raw
|
||||
private final HashMap<String, String[]> nickMap; // Mask(Pattern) ->, Action | Where Action[0] could be: raw
|
||||
|
||||
public TalkGenericHandler(String server, String channel,
|
||||
HashMap<String, String[]> joinMap,
|
||||
HashMap<String, String[]> msgMap,
|
||||
HashMap<String, String[]> nickMap)
|
||||
{
|
||||
this.server = server;
|
||||
this.channel = channel;
|
||||
this.joinMap = joinMap;
|
||||
this.msgMap = msgMap;
|
||||
this.nickMap = nickMap;
|
||||
}
|
||||
@Override
|
||||
public void nickCame(String newNick){
|
||||
came(nickMap, newNick, newNick);
|
||||
}
|
||||
@Override
|
||||
public void joinCame(String who){
|
||||
came(joinMap, who, who);
|
||||
}
|
||||
@Override
|
||||
public void privmsgCame(String who, String what){
|
||||
if (what.indexOf(":")+1 < what.length()){
|
||||
what = what.substring(what.indexOf(":")+1);
|
||||
came(msgMap, what, who);
|
||||
}
|
||||
}
|
||||
|
||||
private void came(HashMap<String, String[]> map, String arg1, String arg2) {
|
||||
for (String pattern : map.keySet()){
|
||||
|
||||
if (! Pattern.matches(pattern, arg1)) // NOTE: validation based on new nick //TODO: parse here
|
||||
continue;
|
||||
|
||||
String[] cmdOrMsg = map.get(pattern);
|
||||
|
||||
ArrayList<String> whatToSendList;
|
||||
for (int i = 0; i < cmdOrMsg.length; ) {
|
||||
switch (cmdOrMsg[i]) {
|
||||
case "\\chanmsg":
|
||||
whatToSendList = new ArrayList<>();
|
||||
for (i++; (i < cmdOrMsg.length) && !(cmdOrMsg[i].startsWith("\\")); i++)
|
||||
whatToSendList.add(cmdOrMsg[i]);
|
||||
msgAction(whatToSendList.toArray(new String[0]), arg2, false);
|
||||
break;
|
||||
case "\\privmsg":
|
||||
whatToSendList = new ArrayList<>();
|
||||
for (i++; (i < cmdOrMsg.length) && !(cmdOrMsg[i].startsWith("\\")); i++)
|
||||
whatToSendList.add(cmdOrMsg[i]);
|
||||
msgAction(whatToSendList.toArray(new String[0]), arg2, true);
|
||||
break;
|
||||
case "\\ban":
|
||||
banAction(arg2);
|
||||
i++;
|
||||
break;
|
||||
case "\\voice":
|
||||
voiceAction(arg2);
|
||||
i++;
|
||||
break;
|
||||
case "\\kick":
|
||||
whatToSendList = new ArrayList<>();
|
||||
for (i++; (i < cmdOrMsg.length) && !(cmdOrMsg[i].startsWith("\\")); i++)
|
||||
whatToSendList.add(cmdOrMsg[i]);
|
||||
kickAction(whatToSendList.toArray(new String[0]), arg2);
|
||||
break;
|
||||
case "\\kickban":
|
||||
whatToSendList = new ArrayList<>();
|
||||
for (i++; (i < cmdOrMsg.length) && !(cmdOrMsg[i].startsWith("\\")); i++)
|
||||
whatToSendList.add(cmdOrMsg[i]);
|
||||
banAction(arg2);
|
||||
kickAction(whatToSendList.toArray(new String[0]), arg2);
|
||||
break;
|
||||
case "\\raw":
|
||||
StringBuilder whatToSend = new StringBuilder();
|
||||
for (i++; (i < cmdOrMsg.length) && !(cmdOrMsg[i].startsWith("\\")); i++)
|
||||
whatToSend.append(cmdOrMsg[i]);
|
||||
StreamProvider.writeToStream(server, whatToSend.toString()); //TODO
|
||||
break; //todo: add script
|
||||
case "\\whois": // result will be noted in 'system' log
|
||||
whoisAction(arg2);
|
||||
i++;
|
||||
break;
|
||||
case "\\cclientinfo": // NOTE: All this handled by CTCPHelper instance
|
||||
case "\\cfinger": // C - publish request result to chan
|
||||
case "\\cping":
|
||||
case "\\csource":
|
||||
case "\\ctime":
|
||||
case "\\cuserinfo":
|
||||
case "\\cversion":
|
||||
case "\\pclientinfo": // P - reply to privmsg
|
||||
case "\\pfinger":
|
||||
case "\\pping":
|
||||
case "\\psource":
|
||||
case "\\ptime":
|
||||
case "\\puserinfo":
|
||||
case "\\pversion":
|
||||
String CTCPType = cmdOrMsg[i];
|
||||
String objectRegexp = null;
|
||||
whatToSend = new StringBuilder();
|
||||
|
||||
for (i++; (i < cmdOrMsg.length) && !(cmdOrMsg[i].startsWith("\\")); i++) {
|
||||
if (objectRegexp == null && !cmdOrMsg[i].trim().isEmpty())
|
||||
objectRegexp = cmdOrMsg[i].trim();
|
||||
else
|
||||
whatToSend.append(cmdOrMsg[i]);
|
||||
}
|
||||
|
||||
if (objectRegexp == null)
|
||||
break;
|
||||
|
||||
String objectToCtcp = arg1.trim().replaceAll(objectRegexp, ""); // note: trim() ?
|
||||
|
||||
if (objectToCtcp.isEmpty())
|
||||
break;
|
||||
|
||||
if (CTCPType.startsWith("\\c"))
|
||||
registerCTCPforChannel(CTCPType.substring(2).toUpperCase(), objectToCtcp, whatToSend.toString());
|
||||
else
|
||||
registerCTCPforUser(simplifyNick(arg2), CTCPType.substring(2).toUpperCase(), objectToCtcp, whatToSend.toString());
|
||||
|
||||
break;
|
||||
default:
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private void registerCTCPforChannel(String CTCPType, String object, String message){
|
||||
CTCPHelper.getInstance().registerRequest(server, channel, CTCPType, object, message);
|
||||
}
|
||||
private void registerCTCPforUser(String user, String CTCPType, String object, String message){
|
||||
CTCPHelper.getInstance().registerRequest(server, user, CTCPType, object, message);
|
||||
}
|
||||
private void whoisAction(String who){ // TODO: maybe we have to extend functionality to reuse received information.
|
||||
StreamProvider.writeToStream(server, "WHOIS "+simplifyNick(who));
|
||||
}
|
||||
|
||||
private void msgAction(String[] messages, String who, boolean isToUser){
|
||||
StringBuilder executiveStr = new StringBuilder();
|
||||
executiveStr.append("PRIVMSG ");
|
||||
if(isToUser) {
|
||||
executiveStr.append(simplifyNick(who));
|
||||
executiveStr.append(" :");
|
||||
}
|
||||
else {
|
||||
executiveStr.append(channel);
|
||||
executiveStr.append(" :");
|
||||
executiveStr.append(simplifyNick(who));
|
||||
executiveStr.append(": ");
|
||||
}
|
||||
|
||||
for (String message : messages) {
|
||||
if (!message.startsWith("\\"))
|
||||
executiveStr.append(message);
|
||||
else if (message.equals("\\time")) // TODO: remove this shit
|
||||
executiveStr.append(LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
|
||||
}
|
||||
//System.out.println(executiveStr.toString()); //TODO: debug
|
||||
StreamProvider.writeToStream(server, executiveStr.toString());
|
||||
}
|
||||
private void banAction(String whom){
|
||||
StreamProvider.writeToStream(server, "MODE "+ channel +" +b "+simplifyNick(whom)+"*!*@*");
|
||||
StreamProvider.writeToStream(server, "MODE "+ channel +" +b "+"*!*@"+whom.replaceAll("^.+@",""));
|
||||
}
|
||||
private void voiceAction(String whom){
|
||||
StreamProvider.writeToStream(server, "MODE "+ channel +" +v "+simplifyNick(whom));
|
||||
}
|
||||
private void kickAction(String[] messages, String whom){
|
||||
StringBuilder executiveStr = new StringBuilder();
|
||||
executiveStr.append("KICK ");
|
||||
executiveStr.append(channel);
|
||||
executiveStr.append(" ");
|
||||
executiveStr.append(simplifyNick(whom));
|
||||
executiveStr.append(" :");
|
||||
|
||||
for (String message : messages) {
|
||||
if (!message.startsWith("\\"))
|
||||
executiveStr.append(message);
|
||||
else if (message.equals("\\time"))
|
||||
executiveStr.append(LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
|
||||
}
|
||||
StreamProvider.writeToStream(server, executiveStr.toString());
|
||||
}
|
||||
private String simplifyNick(String nick){ return nick.replaceAll("!.*$",""); }
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package InnaIrcBot.Commanders.talk;
|
||||
|
||||
public interface TalkHandler {
|
||||
void nickCame(String newNick);
|
||||
void joinCame(String who);
|
||||
void privmsgCame(String who, String what);
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package InnaIrcBot.Commanders.talk;
|
||||
|
||||
public class TalkZeroHandler implements TalkHandler{
|
||||
@Override
|
||||
public void nickCame(String newNick) { }
|
||||
|
||||
@Override
|
||||
public void joinCame(String who) { }
|
||||
|
||||
@Override
|
||||
public void privmsgCame(String who, String what) { }
|
||||
}
|
|
@ -3,10 +3,8 @@ package InnaIrcBot.ProvidersConsumers;
|
|||
import InnaIrcBot.Commanders.ChanelCommander;
|
||||
import InnaIrcBot.GlobalData;
|
||||
import InnaIrcBot.IrcChannel;
|
||||
import InnaIrcBot.logging.LogDriver;
|
||||
import InnaIrcBot.logging.Worker;
|
||||
import InnaIrcBot.logging.LogManager;
|
||||
import InnaIrcBot.config.ConfigurationManager;
|
||||
import InnaIrcBot.logging.WorkerZero;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
@ -22,7 +20,7 @@ public class ChanConsumer implements Runnable {
|
|||
private final String serverName;
|
||||
private final String channelName;
|
||||
private final ArrayList<String> users;
|
||||
private Worker logWorker;
|
||||
private final LogManager log;
|
||||
private String nick;
|
||||
private final boolean autoRejoin;
|
||||
private final Map<String, IrcChannel> channels;
|
||||
|
@ -42,7 +40,7 @@ public class ChanConsumer implements Runnable {
|
|||
this.chanConsumerQueue = thisIrcChannel.getChannelQueue();
|
||||
this.serverName = serverName;
|
||||
this.channelName = thisIrcChannel.toString();
|
||||
this.logWorker = LogDriver.getWorker(serverName, channelName);
|
||||
this.log = new LogManager(serverName, channelName);
|
||||
this.users = new ArrayList<>();
|
||||
this.nick = ownNick;
|
||||
this.autoRejoin = ConfigurationManager.getConfiguration(serverName).getRejoinOnKick();
|
||||
|
@ -56,30 +54,35 @@ public class ChanConsumer implements Runnable {
|
|||
this.channelCommanderThread = new Thread(commander);
|
||||
this.channelCommanderThread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
System.out.println("["+LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"))+"] ChanConsumer thread "+serverName+":"+this.channelName +" started"); // TODO:REMOVE DEBUG
|
||||
System.out.println("["+LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"))
|
||||
+"] ChanConsumer thread "+serverName+":"+this.channelName +" started"); // TODO:REMOVE DEBUG
|
||||
runRoutine();
|
||||
close();
|
||||
System.out.println("["+LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"))
|
||||
+"] THREAD "+serverName+":"+this.channelName +" ended"); // TODO:REMOVE DEBUG
|
||||
}
|
||||
|
||||
private void runRoutine(){
|
||||
try {
|
||||
while (! endThread) {
|
||||
parse();
|
||||
}
|
||||
} catch (InterruptedException e){
|
||||
System.out.println("ChanConsumer "+serverName+"/"+channelName+"Interrupted "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void parse() throws InterruptedException{
|
||||
String data = chanConsumerQueue.take();
|
||||
String[] dataStrings = data.split(" :?",3);
|
||||
|
||||
if (trackUsers(dataStrings[1], dataStrings[0], dataStrings[2]))
|
||||
continue;
|
||||
// Send to channel commander thread
|
||||
// TODO: Check and add consistency validation
|
||||
queue.add(data);
|
||||
return;
|
||||
|
||||
if (! logWorker.logAdd(dataStrings[1], dataStrings[0], dataStrings[2])){ // Write logs checks if LogDriver consistent.
|
||||
this.fixLogDriverIssues(dataStrings[1], dataStrings[0], dataStrings[2]);
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e){
|
||||
System.out.println("ChanConsumer (@"+serverName+"/"+channelName+")->run(): Interrupted\n\t"+e.getMessage()); // TODO: reconnect?
|
||||
}
|
||||
|
||||
close();
|
||||
System.out.println("["+LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"))+"] THREAD "+serverName+":"+this.channelName +" ended"); // TODO:REMOVE DEBUG
|
||||
queue.add(data); // Send to channel commander thread TODO: Check and add consistency validation
|
||||
log.add(dataStrings[1], dataStrings[0], dataStrings[2]);
|
||||
}
|
||||
|
||||
private boolean trackUsers(String event, String initiator, String subject){
|
||||
|
@ -101,12 +104,11 @@ public class ChanConsumer implements Runnable {
|
|||
return true; // user quit, but he/she is not in this channel
|
||||
case "KICK":
|
||||
String kickedUser = subject.replaceAll("(^.+?\\s)|(\\s.+$)", "");
|
||||
if (nick.equals(kickedUser) && autoRejoin) { // TODO: FIX
|
||||
hasBeenKicked = true;
|
||||
deleteUser(kickedUser);
|
||||
if (nick.equals(kickedUser)) { // TODO: FIX
|
||||
hasBeenKicked = true;
|
||||
return true;
|
||||
}
|
||||
deleteUser(kickedUser);
|
||||
return false;
|
||||
case "NICK":
|
||||
if (users.contains(initiator)) {
|
||||
|
@ -146,7 +148,7 @@ public class ChanConsumer implements Runnable {
|
|||
private void close(){
|
||||
try{
|
||||
channels.remove(channelName);
|
||||
logWorker.close();
|
||||
log.close();
|
||||
channelCommanderThread.interrupt(); //kill sub-thread
|
||||
channelCommanderThread.join();
|
||||
handleAutoRejoin();
|
||||
|
@ -161,13 +163,4 @@ public class ChanConsumer implements Runnable {
|
|||
StreamProvider.writeToStream(serverName, "JOIN " + channelName);
|
||||
}
|
||||
}
|
||||
|
||||
private void fixLogDriverIssues(String a, String b, String c){
|
||||
System.out.println("ChanConsumer (@"+serverName+"/"+channelName+")->fixLogDriverIssues(): Some issues detected. Trying to fix...");
|
||||
logWorker = LogDriver.getWorker(serverName, channelName); // Reset logDriver and try using the same one
|
||||
if (! logWorker.logAdd(a, b, c)){ // Write to it what was not written (most likely) and if it's still not consistent:
|
||||
logWorker = new WorkerZero();
|
||||
System.out.println("ChanConsumer (@"+serverName+"/"+channelName+")->fixLogDriverIssues(): failed to use defined LogDriver. Using ZeroWorker instead.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package InnaIrcBot.ProvidersConsumers;
|
|||
|
||||
import InnaIrcBot.config.ConfigurationFile;
|
||||
import InnaIrcBot.IrcChannel;
|
||||
import InnaIrcBot.logging.LogDriver;
|
||||
import InnaIrcBot.ReconnectControl;
|
||||
|
||||
import java.io.*;
|
||||
|
@ -37,7 +36,6 @@ public class DataProvider implements Runnable {
|
|||
connectSocket();
|
||||
|
||||
ReconnectControl.register(server);
|
||||
LogDriver.setLogDriver(server);
|
||||
|
||||
ircChannels = Collections.synchronizedMap(new HashMap<>());
|
||||
systemConsumerChannel = new IrcChannel("");
|
||||
|
|
|
@ -5,7 +5,6 @@ import InnaIrcBot.Commanders.PrivateMsgCommander;
|
|||
import InnaIrcBot.ReconnectControl;
|
||||
import InnaIrcBot.config.ConfigurationFile;
|
||||
import InnaIrcBot.IrcChannel;
|
||||
import InnaIrcBot.logging.LogDriver;
|
||||
import InnaIrcBot.logging.WorkerSystem;
|
||||
|
||||
import java.time.LocalTime;
|
||||
|
@ -38,7 +37,8 @@ public class SystemConsumer implements Runnable{
|
|||
this.systemQueue = channels.get("").getChannelQueue();
|
||||
this.nick = userNick;
|
||||
this.server = configurationFile.getServerName();
|
||||
this.writerWorker = LogDriver.getSystemWorker(server);
|
||||
WorkerSystem.setLogDriver(server);
|
||||
this.writerWorker = WorkerSystem.getSystemWorker(server);
|
||||
this.channels = channels;
|
||||
this.channelThreads = new ArrayList<>();
|
||||
this.configurationFile = configurationFile;
|
||||
|
|
|
@ -4,24 +4,32 @@ import InnaIrcBot.config.ConfigurationFile;
|
|||
import InnaIrcBot.config.ConfigurationManager;
|
||||
import InnaIrcBot.config.LogDriverConfiguration;
|
||||
|
||||
import java.util.HashMap;
|
||||
public class LogManager {
|
||||
private final String server;
|
||||
private final String channel;
|
||||
private Worker worker;
|
||||
|
||||
public class LogDriver {
|
||||
private final static HashMap<String, WorkerSystem> systemLogWorkerMap = new HashMap<>();
|
||||
public LogManager(String server, String channel){
|
||||
this.server = server;
|
||||
this.channel = channel;
|
||||
this.worker = getWorker(server, channel);
|
||||
}
|
||||
|
||||
// TODO: add proxy multiple drivers support
|
||||
public static synchronized void setLogDriver(String server){
|
||||
String applicationLogDir;
|
||||
public void add(String event, String initiator, String message) {
|
||||
try {
|
||||
applicationLogDir = ConfigurationManager.getConfiguration(server).getApplicationLogDir();
|
||||
worker.logAdd(event, initiator, message);
|
||||
}
|
||||
catch (Exception e){
|
||||
applicationLogDir = "";
|
||||
System.out.println("Unable to use LogDriver for "+server+"/"+channel+" "+e.getMessage());
|
||||
worker = new WorkerZero();
|
||||
}
|
||||
systemLogWorkerMap.put(server, new WorkerSystem(server, applicationLogDir));
|
||||
}
|
||||
|
||||
public static synchronized Worker getWorker(String server, String channel){
|
||||
public void close() {
|
||||
worker.close();
|
||||
}
|
||||
|
||||
private Worker getWorker(String server, String channel) {
|
||||
try {
|
||||
ConfigurationFile serverConfiguration = ConfigurationManager.getConfiguration(server);
|
||||
LogDriverConfiguration logDriverConfiguration = serverConfiguration.getLogDriverConfiguration();
|
||||
|
@ -45,8 +53,4 @@ public class LogDriver {
|
|||
return new WorkerZero();
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized WorkerSystem getSystemWorker(String serverName){
|
||||
return systemLogWorkerMap.get(serverName);
|
||||
}
|
||||
}
|
|
@ -1,13 +1,9 @@
|
|||
package InnaIrcBot.logging;
|
||||
|
||||
public interface Worker {
|
||||
boolean consistent = false;
|
||||
|
||||
boolean isConsistent();
|
||||
|
||||
boolean logAdd(String event,
|
||||
String initiator,
|
||||
String message);
|
||||
void logAdd(String event, String initiator, String message) throws Exception;
|
||||
|
||||
void close();
|
||||
}
|
||||
|
|
|
@ -79,7 +79,7 @@ public class WorkerFiles implements Worker {
|
|||
* argument[1] should be always 'subject'
|
||||
* */
|
||||
@Override
|
||||
public boolean logAdd(String event, String initiator, String message) {
|
||||
public void logAdd(String event, String initiator, String message) throws Exception{
|
||||
switch (event){
|
||||
case "PRIVMSG":
|
||||
PRIVMSG(initiator, message);
|
||||
|
@ -109,7 +109,10 @@ public class WorkerFiles implements Worker {
|
|||
prettyPrint("["+LocalTime.now().format(dateFormat)+"] "+event+" "+initiator+" "+message+"\n"); // TODO: QA @ big data
|
||||
break;
|
||||
}
|
||||
return consistent;
|
||||
if (consistent)
|
||||
return;
|
||||
|
||||
throw new Exception();
|
||||
}
|
||||
private void prettyPrint(String string){
|
||||
try {
|
||||
|
|
|
@ -122,11 +122,11 @@ public class WorkerMongoDB implements Worker { //TODO consider ski
|
|||
}
|
||||
};
|
||||
|
||||
LogDriver.getSystemWorker(server).registerInSystemWorker(thing);
|
||||
WorkerSystem.getSystemWorker(server).registerInSystemWorker(thing);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean logAdd(String event, String initiator, String message) {
|
||||
public void logAdd(String event, String initiator, String message) throws Exception{
|
||||
Document document = new Document("date", getDate())
|
||||
.append("event", event)
|
||||
.append("initiator", initiator);
|
||||
|
@ -156,7 +156,10 @@ public class WorkerMongoDB implements Worker { //TODO consider ski
|
|||
|
||||
insert(document);
|
||||
|
||||
return consistent;
|
||||
if (consistent)
|
||||
return;
|
||||
|
||||
throw new Exception();
|
||||
}
|
||||
private void insert(Document document){
|
||||
try {
|
||||
|
|
|
@ -150,7 +150,7 @@ public class WorkerSQLite implements Worker {
|
|||
public boolean isConsistent() {return consistent; }
|
||||
|
||||
@Override
|
||||
public boolean logAdd(String event, String initiator, String message) {
|
||||
public void logAdd(String event, String initiator, String message) throws Exception {
|
||||
try {
|
||||
preparedStatement.setLong(1, getDate());
|
||||
preparedStatement.setString(2, event);
|
||||
|
@ -184,10 +184,9 @@ public class WorkerSQLite implements Worker {
|
|||
preparedStatement.executeUpdate();
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.out.println("BotSQLiteWorker (@" + server + ")->logAdd() failed:\n\t" + e.getMessage());
|
||||
this.close();
|
||||
throw new Exception("BotSQLiteWorker (@" + server + ")->logAdd() failed:\n\t" + e.getMessage());
|
||||
}
|
||||
return consistent;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,13 +1,36 @@
|
|||
package InnaIrcBot.logging;
|
||||
|
||||
import InnaIrcBot.config.ConfigurationFile;
|
||||
import InnaIrcBot.config.ConfigurationManager;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class WorkerSystem{
|
||||
private final static HashMap<String, WorkerSystem> systemLogWorkerMap = new HashMap<>();
|
||||
|
||||
// TODO: add proxy multiple drivers support
|
||||
public static synchronized void setLogDriver(String server){
|
||||
String applicationLogDir;
|
||||
try {
|
||||
ConfigurationFile configuration = ConfigurationManager.getConfiguration(server);
|
||||
applicationLogDir = configuration.getApplicationLogDir();
|
||||
}
|
||||
catch (Exception e){
|
||||
applicationLogDir = "";
|
||||
}
|
||||
systemLogWorkerMap.put(server, new WorkerSystem(server, applicationLogDir));
|
||||
}
|
||||
|
||||
public static synchronized WorkerSystem getSystemWorker(String serverName){
|
||||
return systemLogWorkerMap.get(serverName);
|
||||
}
|
||||
|
||||
|
||||
private FileWriter fileWriter;
|
||||
private final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("HH:mm:ss");
|
||||
|
|
|
@ -5,7 +5,7 @@ public class WorkerZero implements Worker{
|
|||
public boolean isConsistent() {return true;}
|
||||
|
||||
@Override
|
||||
public boolean logAdd(String event, String initiator, String message) { return true; }
|
||||
public void logAdd(String event, String initiator, String message) {}
|
||||
|
||||
@Override
|
||||
public void close() {}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package InnaIrcBot.Commanders;
|
||||
package InnaIrcBot.Commanders.flood;
|
||||
|
||||
import InnaIrcBot.ProvidersConsumers.StreamProvider;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
|
@ -14,7 +14,7 @@ class JoinFloodHandlerTest {
|
|||
private static final String serverName = "testServer";
|
||||
private static final String channelName = "testChannel";
|
||||
|
||||
private final JoinFloodHandler joinFloodHandler = new JoinFloodHandler(3, 5, serverName, channelName);
|
||||
private final JoinFloodHandler joinFloodHandler = new JoinFloodHandler(serverName, channelName, 3, 5);
|
||||
private static final String userNickName = "John";
|
||||
private Thread socketTestThread;
|
||||
|
|
@ -76,9 +76,12 @@ class LogDriverTest {
|
|||
}
|
||||
|
||||
private void createWorkers(String server){
|
||||
/*
|
||||
fw1 = LogDriver.getWorker(server,"system");
|
||||
fw2 = LogDriver.getWorker(server,"#main");
|
||||
fw3 = LogDriver.getWorker(server,"#lpr");
|
||||
|
||||
*/
|
||||
}
|
||||
void checkConsistency(){
|
||||
assertTrue(fw1.isConsistent());
|
||||
|
@ -87,6 +90,7 @@ class LogDriverTest {
|
|||
}
|
||||
|
||||
void validateDriver(){
|
||||
/*
|
||||
assertTrue(fw1.logAdd("JOIN", "de_su!loper@desktop.lan", "message1"));
|
||||
assertTrue(fw1.logAdd("PRIVMSG", "de_su!loper@desktop.lan", ": some text here"));
|
||||
assertTrue(fw1.logAdd("PRIVMSG", "de_su!loper@desktop.lan", ": more random tests"));
|
||||
|
@ -107,6 +111,8 @@ class LogDriverTest {
|
|||
assertTrue(fw3.logAdd("NICK", "de_su!loper@desktop.lan", "developer_su"));
|
||||
assertTrue(fw3.logAdd("MODE", "de_su!loper@desktop.lan", "+b username"));
|
||||
assertTrue(fw3.logAdd("PART", "de_su!loper@desktop.lan", "#chan3"));
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
private void initializeFilesLogDriver(){
|
||||
|
@ -116,7 +122,7 @@ class LogDriverTest {
|
|||
null,
|
||||
null,
|
||||
null);
|
||||
LogDriver.setLogDriver(serverNameFiles);
|
||||
WorkerSystem.setLogDriver(serverNameFiles);
|
||||
}
|
||||
|
||||
private void initializeSQLiteLogDriver(){
|
||||
|
@ -127,7 +133,7 @@ class LogDriverTest {
|
|||
null,
|
||||
null);
|
||||
|
||||
LogDriver.setLogDriver(serverNameSQLite);
|
||||
WorkerSystem.setLogDriver(serverNameSQLite);
|
||||
}
|
||||
|
||||
private void initializeMongoDBLogDriver(){
|
||||
|
@ -137,7 +143,7 @@ class LogDriverTest {
|
|||
"irc",
|
||||
"loper",
|
||||
"password");
|
||||
LogDriver.setLogDriver("irc.tomsk.net");
|
||||
WorkerSystem.setLogDriver("irc.tomsk.net");
|
||||
}
|
||||
|
||||
private void close(){
|
||||
|
|
Loading…
Reference in a new issue