v0.4 drafts

This commit is contained in:
Dmitry Isaenko 2019-01-17 04:47:35 +03:00
parent 0093fe84d3
commit 434e7e3715
10 changed files with 189 additions and 21 deletions

View file

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>loper</groupId> <groupId>loper</groupId>
<artifactId>InnaIrcBot</artifactId> <artifactId>InnaIrcBot</artifactId>
<version>0.3-SNAPSHOT</version> <version>0.4-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>InnaIrcBot</name> <name>InnaIrcBot</name>
@ -24,6 +24,12 @@
<version>2.8.5</version> <version>2.8.5</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>3.9.1</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<build><plugins> <build><plugins>
<plugin> <plugin>

View file

@ -11,6 +11,7 @@ public class BotStart {
//TODO: flood control //TODO: flood control
//TODO: setDaemon(true) //TODO: setDaemon(true)
//TODO: multiple connections to one server not allowed //TODO: multiple connections to one server not allowed
// TODO switch to zero-worker in case of inconsistent state of Worker
public static void main(String[] args){ public static void main(String[] args){
if (args.length != 0) { if (args.length != 0) {

View file

@ -290,7 +290,7 @@ public class PrivateMsgCommander { // T
} }
} }
private void kickban(String chanel, String user, String reason){ private void kickban(String chanel, String user, String reason){
cmode(chanel, "+b", simplifyNick(user)+"*!*@*"); ban(chanel, user);
kick(chanel, user, reason); kick(chanel, user, reason);
} }
private void voice(String chanel, String user){ private void voice(String chanel, String user){

View file

@ -1,7 +1,7 @@
package InnaIrcBot; package InnaIrcBot;
public class GlobalData { public class GlobalData {
private static final String version = "InnaIrcBot v0.3 \"Карелия\""; private static final String version = "InnaIrcBot v0.4 \"Карские Ворота\"";
public static synchronized String getAppVersion(){ public static synchronized String getAppVersion(){
return version; return version;
} }

View file

@ -27,6 +27,8 @@ public class BotDriver {
return new BotFilesWorker(serverName, serverDriver.get(serverName)[1], chanelName); return new BotFilesWorker(serverName, serverDriver.get(serverName)[1], chanelName);
case "SQLite": case "SQLite":
return new BotSQLiteWorker(serverName, serverDriver.get(serverName)[1], chanelName); return new BotSQLiteWorker(serverName, serverDriver.get(serverName)[1], chanelName);
case "MongoDB":
return new BotMongoWorker(serverName, serverDriver.get(serverName)[1], chanelName);
case "Zero": case "Zero":
return new BotZeroWorker(); return new BotZeroWorker();
default: default:

View file

@ -0,0 +1,147 @@
package InnaIrcBot.LogDriver;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoTimeoutException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.event.*;
import org.bson.Document;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/** For each IRC server we create one DB that stored in the hashmap ('static', since we may have few configuration that have to work at once)
* For each channel we store collection that have name of the ircServer + chanelName.
* If user decides to use one MongoDB for various servers, he may use even one DB (declared in configuration file) and store collections in there.
* **/
public class BotMongoWorker implements Worker {
private static Map<String, MongoClient> serversMap = Collections.synchronizedMap(new HashMap<String, MongoClient>());
private String ircServer;
private MongoCollection<Document> collection;
private boolean consistent = true; // TODO: clarify possible issues???
public BotMongoWorker(String ircServer, String[] driverParameters, String channel){
if (channel.equals("system")) // Set ircServer variable only if it's 'system' log thread.
this.ircServer = ircServer;
else
this.ircServer = null;
if (!serversMap.containsKey(ircServer)){
MongoClientSettings MCS = MongoClientSettings.builder().addCommandListener(new CommandListener() {
@Override
public void commandStarted(CommandStartedEvent commandStartedEvent) {
System.out.println("commandStarted");
}
@Override
public void commandSucceeded(CommandSucceededEvent commandSucceededEvent) {
System.out.println("commandSucceeded");
}
@Override
public void commandFailed(CommandFailedEvent commandFailedEvent) {
System.out.println("commandFailed");
}
})
.applyConnectionString(new ConnectionString("mongodb://asasa:27017"))
.applyToServerSettings(builder -> builder.addServerListener(new ServerListener() {
@Override
public void serverOpening(ServerOpeningEvent serverOpeningEvent) {
System.out.println("Server Listener: 1 SRV ID: "+serverOpeningEvent.getServerId());
}
@Override
public void serverClosed(ServerClosedEvent serverClosedEvent) {
System.out.println("Server Listener: Server has been closed");
}
@Override
public void serverDescriptionChanged(ServerDescriptionChangedEvent serverDescriptionChangedEvent) {
System.out.println("Server Listener: Desc Changed: "+serverDescriptionChangedEvent.getNewDescription().getException());
close();
}
}))
.build();
MongoClient mongoClient = MongoClients.create(MCS);
//MongoClient mongoClient = MongoClients.create("mongodb://asasa:27017"); // TODO: replace with driverParameters[0] - address
serversMap.put(ircServer, mongoClient);
}
MongoDatabase mongoDB = serversMap.get(ircServer).getDatabase("irc"); // TODO: replace with driverParameters[1] - DB NAME
collection = mongoDB.getCollection(ircServer + channel);
Document ping = new Document("ping", "1");
try {
collection.insertOne(ping);
}catch (MongoTimeoutException e) {
System.out.println("Timeout exception");
consistent = false;
} catch (IllegalStateException ise){
System.out.println("Illegal state exception: MongoDB server already closed.");
consistent = false;
}
}
@Override
public void logAdd(String event, String initiatorArg, String messageArg) {
Document document = new Document("date", getDate())
.append("event", event)
.append("initiator", initiatorArg);
switch (event) {
case "NICK":
case "JOIN":
document.append("message1", messageArg);
//preparedStatement.setString(5, null);
break;
case "PART":
case "QUIT":
case "TOPIC":
document.append("message1", messageArg.replaceAll("^.+?:", ""));
//preparedStatement.setString(5, null);
break;
case "MODE":
document.append("message1", messageArg.replaceAll("(^(.+?\\s){1})|(\\s.+$)",""));
document.append("message2", messageArg.replaceAll("^(.+?\\s){2}", ""));
break;
case "KICK":
document.append("message1", messageArg.replaceAll("^.+?:", ""));
document.append("message2", messageArg.replaceAll("(^.+?\\s)|(\\s.+$)", ""));
break;
case "PRIVMSG":
document.append("message1", messageArg.replaceAll("^:", ""));
//preparedStatement.setString(5,null);
break;
default:
document.append("message1", messageArg);
//preparedStatement.setString(5,null);
break;
}
collection.insertOne(document);
}
private long getDate(){ return System.currentTimeMillis() / 1000L; } // UNIX time
@Override
public boolean isConsistent() { return consistent; }
@Override
public void close() {
// If ircServer != null then it's system thread and when it's interrupted we have to close connection to DB for used server
// And remove it from HashMap
if (this.ircServer != null && serversMap.containsKey(ircServer)) {
serversMap.get(ircServer).close();
serversMap.remove(ircServer);
System.out.println("BotMongoWorker->close(): " + ircServer);
}
}
}

View file

@ -168,15 +168,15 @@ public class BotSQLiteWorker implements Worker {
preparedStatement.setString(5, messageArg.replaceAll("^(.+?\\s){2}", "")); preparedStatement.setString(5, messageArg.replaceAll("^(.+?\\s){2}", ""));
break; break;
case "KICK": case "KICK":
preparedStatement.setString(4,messageArg.replaceAll("^.+?:", "")); preparedStatement.setString(4, messageArg.replaceAll("^.+?:", ""));
preparedStatement.setString(5,messageArg.replaceAll("(^.+?\\s)|(\\s.+$)", "")); preparedStatement.setString(5, messageArg.replaceAll("(^.+?\\s)|(\\s.+$)", ""));
break; break;
case "PRIVMSG": case "PRIVMSG":
preparedStatement.setString(4,messageArg.replaceAll("^:", "")); preparedStatement.setString(4, messageArg.replaceAll("^:", ""));
preparedStatement.setString(5,null); preparedStatement.setString(5,null);
break; break;
default: default:
preparedStatement.setString(4,messageArg); preparedStatement.setString(4, messageArg);
preparedStatement.setString(5,null); preparedStatement.setString(5,null);
break; break;
} }

View file

@ -65,7 +65,7 @@ public class SystemConsumer implements Runnable{
private void setMainRoutine(){ private void setMainRoutine(){
String data; String data;
String dataStrings[]; String[] dataStrings;
try { try {
while ((data = reader.readLine()) != null) { while ((data = reader.readLine()) != null) {
dataStrings = data.split(" ",3); dataStrings = data.split(" ",3);

View file

@ -6,34 +6,46 @@ import InnaIrcBot.LogDriver.Worker;
public class DriverTest { public class DriverTest {
public static void main(String[] args){ public static void main(String[] args){
if (BotDriver.setLogDriver("irc.tomsk.net", "SQLiteDriver", new String[]{"/tmp/"})) if (BotDriver.setLogDriver("irc.tomsk.net", "MongoDB", new String[]{"/tmp/"}))
System.out.println("Successful driver initiation"); System.out.println("Successful driver initiation");
else { else {
System.out.println("Failed driver initiation"); System.out.println("Failed driver initiation");
return; return;
} }
Worker fw1 = BotDriver.getWorker("irc.tomsk.net","#lpr"); Worker fw1 = BotDriver.getWorker("irc.tomsk.net","system");
Worker fw2 = BotDriver.getWorker("irc.tomsk.net","#main"); //Worker fw2 = BotDriver.getWorker("irc.tomsk.net","#main");
Worker fw3 = BotDriver.getWorker("irc.tomsk.net","##loper"); //Worker fw3 = BotDriver.getWorker("irc.tomsk.net","#lpr");
if ((fw1 !=null) && (fw2 !=null) && (fw3 !=null)){ //if ((fw1 !=null) && (fw2 !=null) && (fw3 !=null)){
System.out.println("LogFile1: "+fw1.isConsistent()); System.out.println("LogFile1: "+fw1.isConsistent());
System.out.println("LogFile2: "+fw2.isConsistent()); //System.out.println("LogFile2: "+fw2.isConsistent());
System.out.println("LogFile3: "+fw3.isConsistent()); //System.out.println("LogFile3: "+fw3.isConsistent());
/*
fw1.logAdd("JOIN", "de_su!loper@desktop.lan", "message1"); fw1.logAdd("JOIN", "de_su!loper@desktop.lan", "message1");
fw1.logAdd("PRIVMSG", "de_su!loper@desktop.lan", ": some text here");
fw1.logAdd("PRIVMSG", "de_su!loper@desktop.lan", ": more random tests");
fw1.logAdd("NICK", "de_su!loper@desktop.lan", "developer_su");
fw1.logAdd("MODE", "de_su!loper@desktop.lan", "+b username");
fw1.logAdd("PART", "de_su!loper@desktop.lan", "#chan1"); fw1.logAdd("PART", "de_su!loper@desktop.lan", "#chan1");
fw2.logAdd("JOIN", "de_su!loper@desktop.lan", "message2"); fw2.logAdd("JOIN", "de_su!loper@desktop.lan", "message2");
fw2.logAdd("PRIVMSG", "de_su!loper@desktop.lan", ": some text here");
fw2.logAdd("PRIVMSG", "de_su!loper@desktop.lan", ": more random tests");
fw2.logAdd("NICK", "de_su!loper@desktop.lan", "developer_su");
fw2.logAdd("MODE", "de_su!loper@desktop.lan", "+b username");
fw2.logAdd("PART", "de_su!loper@desktop.lan", "#chan2"); fw2.logAdd("PART", "de_su!loper@desktop.lan", "#chan2");
fw3.logAdd("JOIN", "de_su!loper@desktop.lan", "message3"); fw3.logAdd("JOIN", "de_su!loper@desktop.lan", "message3");
fw3.logAdd("PRIVMSG", "de_su!loper@desktop.lan", ": some text here");
fw3.logAdd("PRIVMSG", "de_su!loper@desktop.lan", ": more random tests");
fw3.logAdd("NICK", "de_su!loper@desktop.lan", "developer_su");
fw3.logAdd("MODE", "de_su!loper@desktop.lan", "+b username");
fw3.logAdd("PART", "de_su!loper@desktop.lan", "#chan3"); fw3.logAdd("PART", "de_su!loper@desktop.lan", "#chan3");
*/
fw1.close(); fw1.close();
fw2.close(); //fw2.close();
fw3.close(); //fw3.close();
} //}
} }
} }

View file

@ -3,7 +3,7 @@ package Temporary;
import InnaIrcBot.ReconnectControl; import InnaIrcBot.ReconnectControl;
public class ReconnectControlTest { public class ReconnectControlTest {
public static void main(String args[]){ public static void main(String[] args){
ReconnectControl.register("testing"); ReconnectControl.register("testing");
ReconnectControl.register("testing1"); ReconnectControl.register("testing1");
ReconnectControl.update("testing1", false); ReconnectControl.update("testing1", false);