diff --git a/.gradle/7.3/executionHistory/executionHistory.bin b/.gradle/7.3/executionHistory/executionHistory.bin index 3313e6f83482b79990577721f80d47249280895e..aebafa6042a6c3dca0998c4b34f9e428ba1fb472 100644 Binary files a/.gradle/7.3/executionHistory/executionHistory.bin and b/.gradle/7.3/executionHistory/executionHistory.bin differ diff --git a/.gradle/7.3/executionHistory/executionHistory.lock b/.gradle/7.3/executionHistory/executionHistory.lock index bd0866557779329f2f2196d4ec4568e67c1cc3c1..f5c0f289b524f71f271e8033e573f9752d4f828b 100644 Binary files a/.gradle/7.3/executionHistory/executionHistory.lock and b/.gradle/7.3/executionHistory/executionHistory.lock differ diff --git a/.gradle/7.3/fileHashes/fileHashes.bin b/.gradle/7.3/fileHashes/fileHashes.bin index 2a6edf6dca28607005cdc9a4f90f210902c52e70..ec995dad2542f5969f4ae9a3ba0211e9d5688308 100644 Binary files a/.gradle/7.3/fileHashes/fileHashes.bin and b/.gradle/7.3/fileHashes/fileHashes.bin differ diff --git a/.gradle/7.3/fileHashes/fileHashes.lock b/.gradle/7.3/fileHashes/fileHashes.lock index 237395412157d370fe65b09027b0aa438e28ffe5..aa582e805f83ac5fdc90e638d9c368efe544a988 100644 Binary files a/.gradle/7.3/fileHashes/fileHashes.lock and b/.gradle/7.3/fileHashes/fileHashes.lock differ diff --git a/.gradle/7.3/fileHashes/resourceHashesCache.bin b/.gradle/7.3/fileHashes/resourceHashesCache.bin index a3e1ee7ce92c8cafd0aec565fa3d3c58333e418b..43a75f04ad47787d9463dbbeef2dff52cefcaf35 100644 Binary files a/.gradle/7.3/fileHashes/resourceHashesCache.bin and b/.gradle/7.3/fileHashes/resourceHashesCache.bin differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index e3200a2fc0a8feeadb7ea0c212a942b36c459a4d..54bce009663ce12c3d6fbb1bfecd7ff54c729adc 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/CHANGELOG b/CHANGELOG index ac576b512e0218f4da77cc5a7be1645491cdb866..d87c50102a69a45f520f8c150a4fd8c4de5daa87 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ -1.0.0 -- Started the project -*Will add more later...* +1.1.0: +- Got the plugin working +- Connects to IRC +- Relays messages, joins, parts, quits, NICK +- CTCP kind of working + + diff --git a/README.md b/README.md index daab48aa5e17fa0e9665af5492bc0b90d22ff72a..09c37f2b9bcc84e7e94bdf3c0715ea04877966d0 100644 --- a/README.md +++ b/README.md @@ -1 +1,42 @@ -MindustrIRC -> IRC mod +# MindustrIRC + +## What? + +MindustrIRC is an IRC Plugin for the game [Mindustry](https://mindustrygame.github.io) + + +## What does it do? + +It relays all messages which are send in the game to the IRC channel and the other way round + + +It also relays JOIN, PART, QUIT, NICK and NOTICE + + +It also comes with some IRC sided commands, like `players`. To use them send at IRC: `<YOURBOTNICK>: <command>` + + +## How to install? + +To install this mod, go to your `config/mods/` folder and run the following command in shell: + +$ wget "https://edugit.org/Miniontoby/mindustrirc/-/raw/main/build/libs/mindustrircDesktop.jar?inline=false" + + +## How to use? + +At default it will connect to irc.ircforever.org:6667 #mindustry + +Using the Core.settings I have done that, but I don't know how to change it from commandline atm... Will change it soon + +You can change the code to change the server address and then build the .jar with the `./update.sh` + + +## How to stop your server again? + +At the moment I dont have it auto stop on the `exit` command, but I am still working on it! + +So just execute the `exit` command and then CTRL-C to stop the server + + + diff --git a/build/classes/java/main/com/miniontoby/MindustrIRC/IRCBot.class b/build/classes/java/main/com/miniontoby/MindustrIRC/IRCBot.class index eb3627d36095c48ae068a898f25c23ae7da95356..8b7eec9abe03893db59d1a638ac1a58b19aba41d 100644 Binary files a/build/classes/java/main/com/miniontoby/MindustrIRC/IRCBot.class and b/build/classes/java/main/com/miniontoby/MindustrIRC/IRCBot.class differ diff --git a/build/classes/java/main/com/miniontoby/MindustrIRC/MindustrIRC.class b/build/classes/java/main/com/miniontoby/MindustrIRC/MindustrIRC.class index 19f5f5df2948e312befccf6c935a2e605c43cdd5..53ae813c75ede17d415d54612693b8dec9afa43c 100644 Binary files a/build/classes/java/main/com/miniontoby/MindustrIRC/MindustrIRC.class and b/build/classes/java/main/com/miniontoby/MindustrIRC/MindustrIRC.class differ diff --git a/build/libs/mindustrircDesktop.jar b/build/libs/mindustrircDesktop.jar index 7257f9d6c0463677f51beabe78ff2a7940e671e3..97958b647c6d7d74f5dafe8af6bc1a0d82610f3c 100644 Binary files a/build/libs/mindustrircDesktop.jar and b/build/libs/mindustrircDesktop.jar differ diff --git a/build/tmp/compileJava/previous-compilation-data.bin b/build/tmp/compileJava/previous-compilation-data.bin index 982d22ff2070f1091c1cc52b2966e388e998527e..3ced466ebd83c127dce7f70ba34e21035ad09e46 100644 Binary files a/build/tmp/compileJava/previous-compilation-data.bin and b/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/src/com/miniontoby/MindustrIRC/IRCBot.java b/src/com/miniontoby/MindustrIRC/IRCBot.java index 5411e4ea377ab7c5f5562044a45b18890802e947..727b67ed823cdd7cb4f0957a471936b20ed085b0 100644 --- a/src/com/miniontoby/MindustrIRC/IRCBot.java +++ b/src/com/miniontoby/MindustrIRC/IRCBot.java @@ -44,13 +44,6 @@ public class IRCBot extends Thread { output.flush(); } - private void pingPong(String[] data) throws Exception { - if (data[0].equals("PING")) { - output.write("PONG " + data[1] + "\n"); - output.flush(); - } - } - private void joinChannel(String channel) throws Exception { output.write("JOIN " + channel + "\n"); output.flush(); @@ -60,56 +53,24 @@ public class IRCBot extends Thread { output.write("PRIVMSG " + to + " :" + message + "\n"); output.flush(); } - - private void verifyMOTD(String[] data) throws Exception { - if (data.length >= 2) { - // 376 is the protocol number (end of MOTD) - if (data[1].equals("376")) { - joinChannel(defaultChannel); - sendMessage(defaultChannel, "[MindustrIRC] Server Started!"); - MindustrIRC.ConsoleLog("Connected to IRC!"); - } - } + public void sendNotice(String to, String message) throws Exception { + output.write("NOTICE " + to + " :" + message + "\n"); + output.flush(); } +/* private boolean isCommand(String[] data) { if (data.length >= 4) { if (data[1].equals("PRIVMSG")) { if (data[3].substring(1, 2).equals("!")) { + String command = data[3].substring(2); return true; } } } return false; } - - private void verifyCommand(String[] data) throws Exception { - if (isCommand(data)) { - String from = data[2]; - String command = data[3].substring(2); - switch (command) { - case "help": - sendMessage(from, "Available commands: help, players"); - break; - case "players": - Seq<String> players = Seq.with(Vars.net.getConnections()).map(con -> con.player.name).removeAll(p -> p == null); - sendMessage(from, "Connected players: " + players.toString(", ")); - break; - case "avapro": - sendMessage(from, String.valueOf(Runtime.getRuntime().availableProcessors())); - break; - case "freememory": - sendMessage(from, String.valueOf(Runtime.getRuntime().freeMemory())); - break; - case "totalmemory": - sendMessage(from, String.valueOf(Runtime.getRuntime().totalMemory())); - break; - default: - sendMessage(from, "exception -> unknown function: \"" + command + "\""); - break; - } - } - } +*/ public void run() { try { @@ -120,52 +81,22 @@ public class IRCBot extends Thread { String data = null; while ((data = input.readLine()) != null) { String[] dataSplitted = data.split(" "); - pingPong(dataSplitted); - verifyMOTD(dataSplitted); - sendToChat(dataSplitted); - try { - verifyCommand(dataSplitted); - } catch (Exception ex) { + if (dataSplitted[0].equals("PING")) { + output.write("PONG " + dataSplitted[1] + "\n"); + output.flush(); + } + if (dataSplitted.length >= 2) { + if (dataSplitted[1].equals("376")) { + joinChannel(defaultChannel); + sendMessage(defaultChannel, "[MindustrIRC] Server Started!"); + MindustrIRC.ConsoleLog("Connected to IRC!"); + } } + MindustrIRC.handleIRCMessage(dataSplitted); } } } catch (Exception ex) { - System.out.println(ex.getMessage()); - } - } - - private void sendToChat(String[] data){ - if (data.length >= 4) { - if (data[1].equals("PRIVMSG")) { - String[] split = data[0].split("!"); - String user = split[0].substring(1); - String message = data[3].split(":")[1]; - for (int i = 4; i < data.length; i++){ - message = message + " " + data[i]; - } - Call.sendMessage("[red][[[grey]" + user + "@IRC[red]][white] " + message); - } else if (data[1].equals("JOIN")) { - String[] split = data[0].split("!"); - String user = split[0].substring(1); - String chn = data[1]; - Call.sendMessage("[grey]-!- " + user + " joined " + chn); - } else if (data[1].equals("PART")) { - String[] split = data[0].split("!"); - String user = split[0].substring(1); - String chn = data[2]; - String reason = data[3]; - Call.sendMessage("[grey]-!- " + user + " has left " + chn + " [" + reason + "]"); - } else if (data[1].equals("QUIT")) { - String[] split = data[0].split("!"); - String user = split[0].substring(1); - String reason = data[2]; - Call.sendMessage("[grey]-!- " + user + " has quit [" + reason + "]"); - } else if (data[1].equals("NICK")) { - String[] split = data[0].split("!"); - String oldUser = split[0].substring(1); - String newUser = data[2]; - Call.sendMessage("[grey]-!- " + oldUser + " is now known as " + newUser); - } + System.out.println("IRCBot.run(): Exception: " + ex.getMessage()); } } } diff --git a/src/com/miniontoby/MindustrIRC/MindustrIRC.java b/src/com/miniontoby/MindustrIRC/MindustrIRC.java index 1f4c42647395b1be747fd9a586720d4578d0b556..40018d448098547b96453e6e2ca18a50bc2c3f46 100644 --- a/src/com/miniontoby/MindustrIRC/MindustrIRC.java +++ b/src/com/miniontoby/MindustrIRC/MindustrIRC.java @@ -1,4 +1,5 @@ package com.miniontoby.MindustrIRC; + import com.miniontoby.MindustrIRC.IRCBot; import java.net.*; @@ -6,6 +7,7 @@ import java.io.*; import java.util.*; import arc.*; import arc.util.*; +import arc.struct.*; import mindustry.*; import mindustry.content.*; import mindustry.game.EventType.*; @@ -19,68 +21,204 @@ public class MindustrIRC extends Plugin { static public int port = Core.settings.getInt("ircPort"); static public String nickname = Core.settings.getString("ircNickname"); static public String channel = Core.settings.getString("ircChannel"); + static public String version = "1.0.0"; + static public String ctcp_version = "MindustrIRC v" + version; static public void ConsoleLog(String message){ Log.info("[MindustrIRC] " + message); } - static public void IRCMessage(String message) { + static public void IRCMessage(String message, String to, boolean withThing) { + try { + if (!withThing){ + bot.sendMessage(to, message); + } else { + bot.sendMessage(to, "[MindustrIRC] " + message); + } + } catch (Exception ex){ + ConsoleLog("IRCMessage: Exception: " + ex); + } + } + static public void IRCNotice(String message, String to) { try { - bot.sendMessage(channel, message); -// bot.sendMessage("#mindustry", "[MindustrIRC] " + message); + bot.sendNotice(to, message); } catch (Exception ex){ - ConsoleLog("Exception: " + ex); + ConsoleLog("IRCNotice: Exception: " + ex); } } + static public void ingameMessage(String message) { + Call.sendMessage(message); + + String ColorCoded = message.replace("[red]", "\u001B[31m").replace("[white]", "\u001B[37m").replace("[grey]", "\u001B[0m").replace("[[", "["); + Log.info(ColorCoded); + } + + public static int Seqsize(Iterable data) { + if (data instanceof Collection) { + return ((Collection<?>) data).size(); + } + int counter = 0; + for (Object i : data) { + counter++; + } + return counter; + } @Override public void init(){ if(Vars.headless){ ConsoleLog("Loaded!"); -// if (empty(server) || empty(port) || empty(nickname) || empty(channel)) { + if (Core.settings.has("ircServer") && Core.settings.has("ircPort") && Core.settings.has("ircNickname") && Core.settings.has("ircChannel")) { + server = Core.settings.getString("ircServer"); + port = Core.settings.getInt("ircPort"); + nickname = Core.settings.getString("ircNickname"); + channel = Core.settings.getString("ircChannel"); + } else { Core.settings.put("ircServer", "irc.ircforever.org"); Core.settings.put("ircPort", 6667); Core.settings.put("ircNickname", "MindustrIRC"); Core.settings.put("ircChannel", "#mindustry"); - String server = Core.settings.getString("ircServer"); - port = Core.settings.getInt("ircPort"); - nickname = Core.settings.getString("ircNickname"); - channel = Core.settings.getString("ircChannel"); -// } - setupListeners(); + server = "irc.ircforever.org"; + port = 6667; + nickname = "MindustrIRC"; + channel = "#mindustry"; + } + setupGameListeners(); connectToIRC(); } } - static void connectToIRC() { + static private void connectToIRC() { Core.app.post(() -> { bot = new IRCBot(server, port, nickname, channel); bot.start(); }); } - static private void setupListeners(){ + + static private void setupGameListeners(){ Events.on(GameOverEvent.class, event -> { - IRCMessage("Game over!"); + if (Seqsize(Vars.net.getConnections()) != 0){ + IRCMessage("Game over!", channel, true); + } }); Events.on(WinEvent.class, event -> { - IRCMessage("Win Event!"); + IRCMessage("Win Event!", channel, true); }); Events.on(LoseEvent.class, event -> { - IRCMessage("Lose Event!"); + IRCMessage("Lose Event!", channel, true); }); Events.on(PlayerChatEvent.class, event -> { String playername = event.player.name; String message = event.message; - IRCMessage("<" + playername + "> " + message); + IRCMessage("<" + playername + "> " + message, channel, false); }); Events.on(PlayerJoin.class, event -> { String playername = event.player.name; - IRCMessage("*** " + playername + " joined the game"); + IRCMessage("*** " + playername + " joined the game", channel, false); }); Events.on(PlayerLeave.class, event -> { String playername = event.player.name; - IRCMessage("*** " + playername + " left the game"); + IRCMessage("*** " + playername + " left the game", channel, false); }); } + + static public void handleIRCMessage(String[] data) { + // [---------0---------] [--1--] [---2--] [---3--] + // :NICK!IDENT@BIND.HOST PRIVMSG #CHN/USR :MESSAGE + // :NICK!IDENT@BIND.HOST NOTICE #CHN/USR :MESSAGE + // :NICK!IDENT@BIND.HOST JOIN :#channel + // :NICK!IDENT@BIND.HOST PART #channel :REASON + // :NICK!IDENT@BIND.HOST QUIT :REASON + // :NICK!IDENT@BIND.HOST NICK NEWNICK + if (data.length >= 3) { + String[] split = data[0].split("!"); + String user = split[0].substring(1); + String message = ""; + + switch (data[1]){ + case "PRIVMSG": + if (data.length >= 4){ + message = data[3].split(":")[1]; + if (message.startsWith("\\001")) { + switch (message){ + case "\\001VERSION": + IRCNotice("\001VERSION " + version + "", user); break; + case "\\001SOURCE": + IRCNotice("\001SOURCE https://edugit.org/Miniontoby/mindustrirc\\001", user); break; + default: + break; + } + } else { + for (int i = 4; i < data.length; i++){ message += " " + data[i]; } + + ingameMessage("[red][[[grey]" + user + "@IRC[red]]:[white] " + message); + if (data.length >= 5){ + handlePrivmsg(data); + } + } + } + break; + case "NOTICE": + if (data.length >= 4){ + message = data[3].split(":")[1]; + for (int i = 4; i < data.length; i++){ message += " " + data[i]; } + + ingameMessage("[red]-[grey]" + user + "@IRC[red]-[white] " + message); + if (data.length >= 5){ + handlePrivmsg(data); + } + } + break; + case "JOIN": + String chan = data[2].split(":")[1]; + ingameMessage("[grey]-!- " + user + " joined " + chan); + break; + case "PART": + if (data.length >= 4){ + message = data[3].split(":")[1]; + for (int i = 4; i < data.length; i++){ message += " " + data[i]; } + } + + ingameMessage("[grey]-!- " + user + " has left " + data[2] + " [" + message + "]"); + break; + case "QUIT": + message = data[2].split(":")[1]; + for (int i = 3; i < data.length; i++){ message += " " + data[i]; } + + ingameMessage("[grey]-!- " + user + " has quit [" + message + "]"); + break; + case "NICK": + ingameMessage("[grey]-!- " + user + " is now known as " + data[2]); + break; + default: + } + } + } + + static private void handlePrivmsg(String[] data){ + if (data[3].startsWith(":" + nickname)){ + String from = data[2]; + String command = data[4]; + switch (command) { + case "help": + IRCMessage("Available commands: help, players, version, source", from, false); + break; + case "players": + Seq<String> players = Seq.with(Vars.net.getConnections()).map(con -> con.player.name).removeAll(p -> p == null); + IRCMessage("Connected players: " + players.toString(", "), from, false); + break; + case "version": + IRCMessage("Version: " + ctcp_version, from, false); + break; + case "source": + IRCMessage("Source: https://edugit.org/Miniontoby/mindustrirc", from, false); + break; + default: + IRCMessage("Unknown command '" + command + "'. Try 'help'. Or use @playername <message> to send a private message", from, false); + break; + } + } + + } }