Create basic IRC functionality

This commit is contained in:
Yash Karandikar 2021-08-10 12:03:38 -05:00
parent d5bd21c5df
commit 7fa1d40577
Signed by: karx
GPG key ID: A794DA2529474BA5
4 changed files with 173 additions and 1 deletions

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View file

@ -0,0 +1,94 @@
package xyz.karx.spirc;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import java.io.*;
import java.net.Socket;
public class Bot {
private final String USERNAME;
private final String HOST;
private final String CHANNEL;
private final int PORT;
private Socket s;
private BufferedWriter bw;
private BufferedReader br;
private Chat chat;
private boolean hasJoined = false;
public Bot(String USERNAME, String HOST, String CHANNEL, int PORT) {
this.USERNAME = USERNAME;
this.HOST = HOST;
this.CHANNEL = CHANNEL;
this.PORT = PORT;
}
private void init() {
try {
s = new Socket(HOST, PORT);
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
chat = new Chat(USERNAME, CHANNEL);
chat.sendToServer(bw, "USER " + USERNAME + " * * :" + USERNAME);
chat.sendToServer(bw, "NICK " + USERNAME);
chat.sendToServer(bw, "MODE +B");
} catch (IOException e) {
e.printStackTrace();
}
}
private void loop() {
final String[] placeholder = {""};
Bukkit.getScheduler().runTaskTimer(Main.getPlugin(Main.class), () -> {
try {
if ((placeholder[0] = br.readLine()) != null && Main.isRunning()) {
String line = placeholder[0];
System.out.println(line);
if (line.contains("PRIVMSG")) {
String user = line.substring(1, line.indexOf("!"));
String message = line.substring(line.indexOf(" :" + 2));
Bukkit.broadcastMessage(ChatColor.BLUE + "[IRC] <" + user + "> " + ChatColor.WHITE + message);
} else if (line.contains("PING")) {
chat.sendToServer(bw, line.replaceFirst("O", "I"));
}
// This will join the channel once and then not again
if (!hasJoined) {
chat.sendToServer(bw, "JOIN #" + CHANNEL);
hasJoined = true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}, 0, 0);
}
public void start() {
init();
loop();
}
public void stop() {
try {
chat.sendToChat(bw, "Server shutting down!");
chat.sendToServer(bw, "PART #" + CHANNEL);
s.close();
bw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,56 @@
package xyz.karx.spirc;
import java.io.BufferedWriter;
import java.io.IOException;
public class Chat
{
private final String USERNAME;
private final String CHANNEL;
public Chat(String USERNAME, String CHANNEL)
{
this.USERNAME = USERNAME;
this.CHANNEL = CHANNEL;
}
// public int processCommands(BufferedWriter bw, String user, String message)
// {
// String[] command = message.split(" ");
//
// //example commands.
//
// if(command[0].equals("!shutdown") && user.equals(CHANNEL))
// {
// return -1;
// }
// else if(command[0].equals("!ping"))
// {
// sendToChat(bw, "@" + user + " pong!");
// return 0;
// }
// else
// {
// return 0;
// }
// }
public void sendToServer(BufferedWriter bw, String message)
{
try
{
bw.write(message + "\r\n");
bw.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void sendToChat(BufferedWriter bw, String message)
{
sendToServer(bw, "PRIVMSG #" + CHANNEL + " :" + message);
System.out.println(USERNAME + " >> " + message);
}
}

View file

@ -3,15 +3,31 @@ package xyz.karx.spirc;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
private static boolean isRunning;
private Bot bot;
@Override
public void onEnable() {
super.onEnable();
System.out.println("Hello there!");
System.out.println("Hello there!");
isRunning = true;
bot = new Bot("spirc", "192.168.1.28", "main", 6667);
bot.start();
}
@Override
public void onDisable() {
super.onDisable();
System.out.println("General Kenobi");
isRunning = false;
bot.stop();
}
public static boolean isRunning() {
return isRunning;
}
}