v0.4 moar
This commit is contained in:
parent
7ed9d8615e
commit
c8ab5a0964
6 changed files with 78 additions and 37 deletions
|
@ -1,6 +1,6 @@
|
|||
# InnaIrcBot
|
||||
|
||||
Another one IRC bot in deep beta.
|
||||
Another one IRC bot in deep-deep beta.
|
||||
|
||||
## Usage
|
||||
` -c, --configuration <name.config> [<name1.config> ...] Read Config`
|
||||
|
@ -41,3 +41,6 @@ Used libraries:
|
|||
- [ ] Deep configuration files validation
|
||||
- [x] Maven ~~or Gradle~~ build
|
||||
- [ ] ncurses-like or/and GUI configuration files (server/chanel setting) editor
|
||||
- [ ] CTCP support for using @ 'ChanelCommander'
|
||||
- [ ] Access roles support (i.e. delegating some rights to another users)
|
||||
- [ ] Logs for application
|
|
@ -133,6 +133,10 @@ public class ChanelCommander implements Runnable {
|
|||
whatToSendRaw.append(cmdOrMsg[i]);
|
||||
StreamProvider.writeToStream(server, whatToSendRaw.toString()); //TODO
|
||||
break; //todo: add script
|
||||
case "\\whois": // result will be noted in 'system' log
|
||||
whoisAction(arg2);
|
||||
i++;
|
||||
break;
|
||||
default:
|
||||
i++;
|
||||
}
|
||||
|
@ -140,6 +144,9 @@ public class ChanelCommander implements Runnable {
|
|||
}
|
||||
}
|
||||
///////// /////////
|
||||
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 sendToPrivate){
|
||||
StringBuilder executiveStr = new StringBuilder();
|
||||
executiveStr.append("PRIVMSG ");
|
||||
|
|
|
@ -32,7 +32,7 @@ public class BotDriver {
|
|||
if (botMongoWorker.isConsistent())
|
||||
return botMongoWorker;
|
||||
else
|
||||
System.out.println("BotDriver: Unable to use MongoWorker. Using ZeroWorker instead."); // else, fall down and use BotZeroWorker.
|
||||
System.out.println("BotDriver: Unable to use MongoWorker for "+serverName+". Using ZeroWorker instead."); // else, fall down and use BotZeroWorker.
|
||||
case "Zero":
|
||||
return new BotZeroWorker();
|
||||
default:
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package InnaIrcBot.LogDriver;
|
||||
|
||||
import com.mongodb.ConnectionString;
|
||||
import com.mongodb.MongoClientSettings;
|
||||
import com.mongodb.MongoTimeoutException;
|
||||
import com.mongodb.*;
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoClients;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
|
@ -25,42 +23,63 @@ public class BotMongoWorker implements Worker {
|
|||
private MongoCollection<Document> collection;
|
||||
private boolean consistent = false; // TODO: clarify possible issues???
|
||||
|
||||
private boolean isItSystemThread = false;
|
||||
|
||||
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;
|
||||
|
||||
String mongoHostAddr;
|
||||
String mongoDBName;
|
||||
String mongoUser;
|
||||
String mongoPass;
|
||||
if (driverParameters.length >= 2 && !driverParameters[0].isEmpty() && !driverParameters[1].isEmpty()) {
|
||||
mongoHostAddr = driverParameters[0];
|
||||
mongoDBName = driverParameters[1];
|
||||
if (driverParameters.length == 4 && !driverParameters[2].isEmpty() && !driverParameters[3].isEmpty()) {
|
||||
mongoUser = driverParameters[2];
|
||||
mongoPass = driverParameters[3];
|
||||
}
|
||||
else { // Consider that DB does not require auth, therefore any credentials are fine, since not null ;)
|
||||
mongoUser = "anon";
|
||||
mongoPass = "anon";
|
||||
}
|
||||
}
|
||||
else
|
||||
this.ircServer = null;
|
||||
return; // consistent = false
|
||||
|
||||
if (channel.equals("system")) // Set ircServer variable only if it's 'system' log thread.
|
||||
this.isItSystemThread = true;
|
||||
|
||||
if (!serversMap.containsKey(ircServer)){
|
||||
|
||||
/* // Leave this validations for better times.
|
||||
CommandListener mongoCommandListener = new CommandListener() {
|
||||
@Override
|
||||
public void commandStarted(CommandStartedEvent commandStartedEvent) {
|
||||
System.out.println("C: commandStarted");
|
||||
System.out.println("BotMongoWorker (@"+this.ircServer+"): C: commandStarted");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commandSucceeded(CommandSucceededEvent commandSucceededEvent) {
|
||||
System.out.println("C: commandSucceeded");
|
||||
System.out.println("BotMongoWorker (@"+this.ircServer+"): C: commandSucceeded");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commandFailed(CommandFailedEvent commandFailedEvent) {
|
||||
System.out.println("C: commandFailed");
|
||||
consistent = false;
|
||||
close(ircServer); // ircServer recieved by constructor, not this.ircServer
|
||||
System.out.println("BotMongoWorker (@"+this.ircServer+"): C: commandFailed");
|
||||
//consistent = false;
|
||||
//close(ircServer); // ircServer recieved by constructor, not this.ircServer
|
||||
}
|
||||
};
|
||||
|
||||
*/
|
||||
ServerListener mongoServerListener = new ServerListener() {
|
||||
@Override
|
||||
public void serverOpening(ServerOpeningEvent serverOpeningEvent) {
|
||||
System.out.println("BotMongoWorker: ServerListener: Server opened successfully: "+serverOpeningEvent.getServerId());
|
||||
System.out.println("BotMongoWorker (@"+ircServer+"): ServerListener: Server opened successfully: "+serverOpeningEvent.getServerId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serverClosed(ServerClosedEvent serverClosedEvent) {
|
||||
System.out.println("BotMongoWorker: ServerListener: Server has been closed");
|
||||
System.out.println("BotMongoWorker (@"+ircServer+"): ServerListener: Server has been closed");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -68,35 +87,44 @@ public class BotMongoWorker implements Worker {
|
|||
if (!serverDescriptionChangedEvent.getNewDescription().isOk()) {
|
||||
consistent = false;
|
||||
close(ircServer); // ircServer recieved by constructor, not this.ircServer
|
||||
System.out.println("BotMongoWorker: ServerListener: Server description changed (exception occurs): "
|
||||
System.out.println("BotMongoWorker (@"+ircServer+"): ServerListener: Server description changed (exception occurs): "
|
||||
+ serverDescriptionChangedEvent.getNewDescription().getException());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
MongoClientSettings MCS = MongoClientSettings.builder()
|
||||
.addCommandListener(mongoCommandListener)
|
||||
.applyConnectionString(new ConnectionString("mongodb://192.168.1.186:27017")) // TODO: replace with driverParameters[0] - address
|
||||
// .addCommandListener(mongoCommandListener)
|
||||
.applyConnectionString(new ConnectionString("mongodb://"+mongoHostAddr))
|
||||
.applyToServerSettings(builder -> builder.addServerListener(mongoServerListener))
|
||||
.credential(MongoCredential.createCredential(mongoUser, mongoDBName, mongoPass.toCharArray()))
|
||||
.build();
|
||||
|
||||
MongoClient mongoClient = MongoClients.create(MCS);
|
||||
serversMap.put(ircServer, mongoClient);
|
||||
}
|
||||
|
||||
MongoDatabase mongoDB = serversMap.get(ircServer).getDatabase("irc"); // TODO: replace with driverParameters[1] - DB NAME
|
||||
MongoDatabase mongoDB = serversMap.get(ircServer).getDatabase(mongoDBName);
|
||||
collection = mongoDB.getCollection(ircServer + channel);
|
||||
|
||||
Document ping = new Document("ping", "1");
|
||||
try {
|
||||
collection.insertOne(ping);
|
||||
consistent = true; // if no exceptions, then true
|
||||
} catch (MongoTimeoutException e) {
|
||||
System.out.println("BotMongoWorker: Timeout exception");
|
||||
} catch (MongoCommandException mce){
|
||||
System.out.println("BotMongoWorker (@"+this.ircServer+"): Command exception. Check if username/password set correctly.");
|
||||
consistent = false;
|
||||
close(ircServer); // ircServer recieved by constructor, not this.ircServer
|
||||
close(ircServer); // ircServer received by constructor, not this.ircServer
|
||||
} catch (MongoTimeoutException mte) {
|
||||
System.out.println("BotMongoWorker (@"+this.ircServer+"): Timeout exception");
|
||||
consistent = false;
|
||||
close(ircServer); // ircServer received by constructor, not this.ircServer
|
||||
}catch (MongoException me){
|
||||
System.out.println("BotMongoWorker (@"+this.ircServer+"): MongoDB Exception");
|
||||
consistent = false;
|
||||
close(ircServer); // ircServer received by constructor, not this.ircServer
|
||||
} catch (IllegalStateException ise){
|
||||
System.out.println("BotMongoWorker: Illegal state exception: MongoDB server already closed (not an issue).");
|
||||
System.out.println("BotMongoWorker (@"+this.ircServer+"): Illegal state exception: MongoDB server already closed (not an issue).");
|
||||
consistent = false;
|
||||
}
|
||||
}
|
||||
|
@ -135,7 +163,7 @@ public class BotMongoWorker implements Worker {
|
|||
//preparedStatement.setString(5,null);
|
||||
break;
|
||||
}
|
||||
collection.insertOne(document);
|
||||
collection.insertOne(document); // TODO: try/catch and watch
|
||||
}
|
||||
|
||||
private long getDate(){ return System.currentTimeMillis() / 1000L; } // UNIX time
|
||||
|
@ -147,17 +175,17 @@ public class BotMongoWorker implements Worker {
|
|||
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)) {
|
||||
if (this.isItSystemThread && serversMap.containsKey(ircServer)) {
|
||||
serversMap.get(ircServer).close();
|
||||
serversMap.remove(ircServer);
|
||||
System.out.println("BotMongoWorker->close(): " + ircServer);
|
||||
System.out.println("BotMongoWorker (@"+this.ircServer+")->close(): " + ircServer);
|
||||
}
|
||||
}
|
||||
public void close(String server) {
|
||||
if (serversMap.containsKey(server)) {
|
||||
serversMap.get(server).close();
|
||||
serversMap.remove(server);
|
||||
System.out.println("BotMongoWorker->close(): " + server + " (forced by listeners)");
|
||||
System.out.println("BotMongoWorker (@"+this.ircServer+")->close(): " + server + " (forced by listeners)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ public class BotSQLiteWorker implements Worker {
|
|||
public BotSQLiteWorker(String server, String[] driverParameters, String channel){ // TODO: threads on SQLite level // remember: One file one DB
|
||||
driverParameters[0] = driverParameters[0].trim();
|
||||
File dir = new File(driverParameters[0]);
|
||||
dir.mkdirs();
|
||||
dir.mkdirs(); // TODO: Check if not-null
|
||||
if (!dir.exists()) {
|
||||
System.out.println("Unable to create directory to store DB file: " + driverParameters[0]); //TODO: notify requester
|
||||
this.consistent = false;
|
||||
|
@ -116,7 +116,7 @@ public class BotSQLiteWorker implements Worker {
|
|||
// Validating result: it table in DB have expected schema. If not, removing and recreating table.
|
||||
for (boolean element: schemaResultCheck) {
|
||||
if (!element) {
|
||||
System.out.println("BotSQLiteWorker: Found already existing table for channel with incoorect syntax: removing table and re-creating.");
|
||||
System.out.println("BotSQLiteWorker: Found already existing table for channel with incorrect syntax: removing table and re-creating.");
|
||||
statement.executeUpdate("DROP TABLE \"" + safeChanName + "\";");
|
||||
statement.executeUpdate(query);
|
||||
break;
|
||||
|
|
|
@ -6,7 +6,10 @@ import InnaIrcBot.LogDriver.Worker;
|
|||
public class DriverTest {
|
||||
|
||||
public static void main(String[] args){
|
||||
if (BotDriver.setLogDriver("irc.tomsk.net", "MongoDB", new String[]{"/tmp/"}))
|
||||
if (BotDriver.setLogDriver("irc.tomsk.net", "MongoDB", new String[]{"192.168.1.5:27017",
|
||||
"irc",
|
||||
"loper",
|
||||
"password"}))
|
||||
System.out.println("Successful driver initiation");
|
||||
else {
|
||||
System.out.println("Failed driver initiation");
|
||||
|
@ -17,11 +20,11 @@ public class DriverTest {
|
|||
Worker fw2 = BotDriver.getWorker("irc.tomsk.net","#main");
|
||||
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("LogFile2: "+fw2.isConsistent());
|
||||
System.out.println("LogFile3: "+fw3.isConsistent());
|
||||
/*
|
||||
|
||||
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");
|
||||
|
@ -42,10 +45,10 @@ public class DriverTest {
|
|||
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");
|
||||
*/
|
||||
|
||||
fw1.close();
|
||||
fw2.close();
|
||||
fw3.close();
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue