Commit d94fc40e authored by Adam Gausmann's avatar Adam Gausmann

Add ability to configure automatic connections

parent 753c635e
Pipeline #5061766 passed with stages
in 13 seconds
......@@ -70,15 +70,8 @@ public class BotImpl implements Bot {
running = true;
logger.trace("Loading properties");
File propertiesFile = new File("bot.properties");
try {
if (propertiesFile.exists()) {
properties.load(new FileInputStream(propertiesFile));
} else {
properties.load(getClass().getResourceAsStream("bot.properties"));
properties.store(new FileOutputStream(propertiesFile), "Samurai bot properties");
}
loadConfig();
} catch (IOException e) {
logger.error("Unable to load bot properties.", e);
quit(1);
......@@ -108,8 +101,24 @@ public class BotImpl implements Bot {
connectionManager.cleanup();
eventSystem.cleanup();
consoleManager.cleanup();
}
running = false;
private void loadConfig() throws IOException {
logger.trace("Loading configuration");
File propertiesFile = new File("./bot.properties");
if (propertiesFile.exists()) {
properties.load(new FileInputStream(propertiesFile));
} else {
properties.load(getClass().getResourceAsStream("bot.properties"));
properties.store(new FileOutputStream(propertiesFile), "Samurai bot properties");
}
}
private void storeConfig() throws IOException {
logger.trace("Saving configuration");
properties.store(new FileOutputStream("./bot.properties"), "Samurai bot properties");
}
public boolean isRunning() {
......
package ninja.nonemu.samurai.command;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
......@@ -48,8 +47,8 @@ public class CommandExecutorImpl {
), this::handleBlacklist);
commandSystem.registerCommand(new CommandInfo(
"quit",
"Gracefully quits from the IRC network.",
"quit [connection]",
"Gracefully shuts the bot down",
"quit",
true
), this::handleQuit);
commandSystem.registerCommand(new CommandInfo(
......@@ -192,18 +191,9 @@ public class CommandExecutorImpl {
return false;
}
private boolean handleQuit(CommandSender sender, CommandInfo info, String label, String[] args) throws IOException {
if (args.length == 0) {
if (!(sender instanceof User)) {
sender.sendChat("Console must provide a connection name.");
return true;
}
sender.sendChat("Disconnecting");
((User) sender).getConnection().disconnect();
} else {
sender.sendChat("Disconnecting from " + args[0]);
getConnection(args[0]).disconnect();
}
private boolean handleQuit(CommandSender sender, CommandInfo info, String label, String[] args) {
sender.sendChat("Bye!");
bot.quit();
return true;
}
......
......@@ -12,6 +12,7 @@ public class ConnectionImpl implements Connection {
private final BotImpl bot;
private final String name;
private final Logger logger;
private String channels;
public ConnectionImpl(BotImpl bot, ninja.nonemu.irc.Connection connection) {
this(bot, connection.getHost(), connection);
......@@ -22,6 +23,7 @@ public class ConnectionImpl implements Connection {
this.name = name;
this.connection = connection;
logger = bot.getLogger();
channels = null;
}
public void run() {
......@@ -103,7 +105,6 @@ public class ConnectionImpl implements Connection {
else if (message.getCommand().equals("001")) { // RPL_WELCOME
bot.getEventSystem().dispatchEvent(new BotConnectEvent(getHost(), getPort(), getNickname()));
String channels = bot.getProperty("channels");
if (channels != null && !channels.isEmpty()) {
connection.joinChannel(channels);
}
......@@ -132,6 +133,10 @@ public class ConnectionImpl implements Connection {
return getUser(name);
}
public void setChannels(String channels) {
this.channels = channels;
}
public ninja.nonemu.irc.Connection getInnerConnection() {
return connection;
}
......@@ -220,4 +225,20 @@ public class ConnectionImpl implements Connection {
public String getNickname() {
return connection.getNickname();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Connection)) return false;
Connection that = (Connection) o;
return name.equalsIgnoreCase(that.getName());
}
@Override
public int hashCode() {
return name.toLowerCase().hashCode();
}
}
package ninja.nonemu.samurai.connection;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import ninja.nonemu.irc.ConnectionBuilder;
import ninja.nonemu.samurai.BotImpl;
......@@ -19,6 +26,8 @@ public class ConnectionManagerImpl implements ConnectionManager {
public void init() {
logger.trace("Initializing connection manager");
loadConnections();
}
public void run() {
......@@ -28,23 +37,91 @@ public class ConnectionManagerImpl implements ConnectionManager {
public void cleanup() {
logger.trace("Cleaning up connection manager");
connections.values().forEach((connection) -> {
if (connection.isConnected()) {
try {
connection.disconnect();
} catch (IOException e) {
logger.error("Unable to disconnect", e);
}
}
});
connections.clear();
}
private void loadConnections() {
logger.trace("Loading connections");
List<Properties> connectionProperties = new ArrayList<>();
File connectionDir = new File("./connections");
if (connectionDir.isDirectory()) {
File[] files = connectionDir.listFiles((dir, name) -> name.endsWith(".properties"));
if (files != null) {
for (File file : files) {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(file));
} catch (IOException e) {
logger.error("Cannot load connection properties", e);
continue;
}
connectionProperties.add(properties);
}
}
} else if (connectionDir.exists()) {
System.exit(1);
} else {
connectionDir.mkdir();
Properties properties = new Properties();
try {
properties.load(getClass().getResourceAsStream("connection.properties"));
properties.store(new FileOutputStream("./connections/default.properties"), "Example Connection");
} catch (IOException e) {
logger.error("Cannot write default connection configuration file", e);
System.exit(1);
}
connectionProperties.add(properties);
}
for (Properties properties : connectionProperties) {
ConnectionBuilder builder = new ConnectionBuilder();
builder.host(properties.getProperty("host"));
if (properties.containsKey("port")) {
builder.port(Integer.parseInt(properties.getProperty("port")));
}
builder.nickname(properties.getProperty("nickname"));
builder.password(properties.getProperty("password"));
ConnectionImpl connection = addConnection(builder);
String channels = properties.getProperty("channels");
if (channels != null && !channels.isEmpty());
connection.setChannels(channels);
try {
connection.connect();
} catch (IOException e) {
logger.error("Unable to connect to server", e);
continue;
}
}
}
@Override
public Connection[] getConnections() {
Connection[] result = new Connection[connections.size()];
public ConnectionImpl[] getConnections() {
ConnectionImpl[] result = new ConnectionImpl[connections.size()];
connections.values().toArray(result);
return result;
}
@Override
public Connection getConnection(String name) {
public ConnectionImpl getConnection(String name) {
return connections.get(name);
}
@Override
public Connection addConnection(ConnectionBuilder builder) {
public ConnectionImpl addConnection(ConnectionBuilder builder) {
builder.username(System.getProperty("user.name"));
builder.realname("Samurai IRC Bot");
......
......@@ -29,7 +29,7 @@ public class PluginManagerImpl implements PluginManager {
logger.debug("Loading JAR plugins");
File pluginDir = new File("./plugins");
if (pluginDir.exists()) {
if (pluginDir.isDirectory()) {
File[] files = pluginDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".jar"));
if (files != null) {
......@@ -65,6 +65,9 @@ public class PluginManagerImpl implements PluginManager {
}
}
}
} else if (pluginDir.exists()) {
logger.error("Plugin directory is not a directory. Please move that file and restart the bot.");
System.exit(1);
} else {
pluginDir.mkdir();
}
......
# Default bot properties
host=irc.nonemu.ninja
port=6667
nickname=Samurai
password=
channels=\#samurai
commandPrefix=>
enableWhitelist=false
\ No newline at end of file
# Default connection properties
host=irc.nonemu.ninja
port=6667
nickname=samurai
password=
channels=\#samurai
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment