diff --git a/.gradle/6.7/executionHistory/executionHistory.bin b/.gradle/6.7/executionHistory/executionHistory.bin index 67174aa..4924485 100644 Binary files a/.gradle/6.7/executionHistory/executionHistory.bin and b/.gradle/6.7/executionHistory/executionHistory.bin differ diff --git a/.gradle/6.7/executionHistory/executionHistory.lock b/.gradle/6.7/executionHistory/executionHistory.lock index a0a5af5..6e3d99f 100644 Binary files a/.gradle/6.7/executionHistory/executionHistory.lock and b/.gradle/6.7/executionHistory/executionHistory.lock differ diff --git a/.gradle/6.7/fileHashes/fileHashes.bin b/.gradle/6.7/fileHashes/fileHashes.bin index 30d62e5..4609bf7 100644 Binary files a/.gradle/6.7/fileHashes/fileHashes.bin and b/.gradle/6.7/fileHashes/fileHashes.bin differ diff --git a/.gradle/6.7/fileHashes/fileHashes.lock b/.gradle/6.7/fileHashes/fileHashes.lock index f3d921a..e95c8db 100644 Binary files a/.gradle/6.7/fileHashes/fileHashes.lock and b/.gradle/6.7/fileHashes/fileHashes.lock differ diff --git a/.gradle/6.7/javaCompile/classAnalysis.bin b/.gradle/6.7/javaCompile/classAnalysis.bin index 136100f..5f992f1 100644 Binary files a/.gradle/6.7/javaCompile/classAnalysis.bin and b/.gradle/6.7/javaCompile/classAnalysis.bin differ diff --git a/.gradle/6.7/javaCompile/javaCompile.lock b/.gradle/6.7/javaCompile/javaCompile.lock index 854aab8..1951ba2 100644 Binary files a/.gradle/6.7/javaCompile/javaCompile.lock and b/.gradle/6.7/javaCompile/javaCompile.lock differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index b3f0ab7..5c1580a 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/build/tmp/compileJava/source-classes-mapping.txt b/build/tmp/compileJava/source-classes-mapping.txt index c4c4351..7ac3826 100644 --- a/build/tmp/compileJava/source-classes-mapping.txt +++ b/build/tmp/compileJava/source-classes-mapping.txt @@ -1,16 +1,20 @@ tech/nevets/lunarbot/config/ConfigHandler.java tech.nevets.lunarbot.config.ConfigHandler -tech/nevets/lunarbot/commands/DiceCmd.java - tech.nevets.lunarbot.commands.DiceCmd -tech/nevets/lunarbot/config/Config.java - tech.nevets.lunarbot.config.Config -tech/nevets/lunarbot/commands/CoinCmd.java - tech.nevets.lunarbot.commands.CoinCmd -tech/nevets/lunarbot/commands/InfoCmd.java - tech.nevets.lunarbot.commands.InfoCmd -tech/nevets/lunarbot/commands/PingCmd.java - tech.nevets.lunarbot.commands.PingCmd tech/nevets/lunarbot/config/ConfigUtils.java tech.nevets.lunarbot.config.ConfigUtils +tech/nevets/lunarbot/config/Config.java + tech.nevets.lunarbot.config.Config +tech/nevets/lunarbot/webserver/WebServer.java + tech.nevets.lunarbot.webserver.WebServer +tech/nevets/lunarbot/commands/games/CoinCmd.java + tech.nevets.lunarbot.commands.games.CoinCmd +tech/nevets/lunarbot/commands/wiki/InfoCmd.java + tech.nevets.lunarbot.commands.wiki.InfoCmd +tech/nevets/lunarbot/webserver/WebAPI.java + tech.nevets.lunarbot.webserver.WebAPI tech/nevets/lunarbot/Bot.java tech.nevets.lunarbot.Bot +tech/nevets/lunarbot/commands/games/DiceCmd.java + tech.nevets.lunarbot.commands.games.DiceCmd +tech/nevets/lunarbot/commands/games/PingCmd.java + tech.nevets.lunarbot.commands.games.PingCmd diff --git a/config.yml b/config.yml index 4a0f378..3f5a0a2 100644 --- a/config.yml +++ b/config.yml @@ -1,2 +1,2 @@ prefix: "$" -botToken: "ODY2ODE5NzQ2MDM2ODQyNTI2.YPYGzA.qz-ELkl_GOWZ7qbCJSNvimLPZyo" \ No newline at end of file +botToken: "ODY2ODE5NzQ2MDM2ODQyNTI2.YPYGzA.bEdQlvhZdMbvZdN5WtTvh_URJ2k" \ No newline at end of file diff --git a/src/main/java/tech/nevets/lunarbot/Bot.java b/src/main/java/tech/nevets/lunarbot/Bot.java index 671dd5b..8ca1989 100644 --- a/src/main/java/tech/nevets/lunarbot/Bot.java +++ b/src/main/java/tech/nevets/lunarbot/Bot.java @@ -10,6 +10,7 @@ import tech.nevets.lunarbot.commands.games.PingCmd; import tech.nevets.lunarbot.config.Config; import tech.nevets.lunarbot.config.ConfigHandler; import tech.nevets.lunarbot.config.ConfigUtils; +import tech.nevets.lunarbot.webserver.WebServer; import java.io.File; @@ -37,5 +38,9 @@ public class Bot { .build(); jda.awaitReady(); System.out.println("Finished Building Bot!"); + + WebServer.main(null); + System.out.println("WebServer successfully loaded in main class!"); + } } \ No newline at end of file diff --git a/src/main/java/tech/nevets/lunarbot/webapi/WebAPI.java b/src/main/java/tech/nevets/lunarbot/webserver/WebAPI.java similarity index 60% rename from src/main/java/tech/nevets/lunarbot/webapi/WebAPI.java rename to src/main/java/tech/nevets/lunarbot/webserver/WebAPI.java index 24aa617..2f8a951 100644 --- a/src/main/java/tech/nevets/lunarbot/webapi/WebAPI.java +++ b/src/main/java/tech/nevets/lunarbot/webserver/WebAPI.java @@ -1,63 +1,13 @@ -package tech.nevets.lunarbot.webapi; +package tech.nevets.lunarbot.webserver; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.io.PrintWriter; -import java.net.ServerSocket; -import java.net.Socket; +//TODO Make up the last 3 hours of my life - Alec public class WebAPI { - public static void main(String args[]) { - WebServer ws = new WebServer(); - ws.start(); - } - } - - class WebServer { - - - protected void start() { - ServerSocket s; - - System.out.println("Webserver starting up on port 80"); - System.out.println("(press ctrl-c to exit)"); - try { - // create the main server socket - s = new ServerSocket(80); - } catch (Exception e) { - System.out.println("Error: " + e); - return; - } - - System.out.println("Waiting for connection"); - for (;;) { - try { - - Socket remote = s.accept(); - - System.out.println("Connection, sending data."); - BufferedReader in = new BufferedReader(new InputStreamReader( - remote.getInputStream())); - PrintWriter out = new PrintWriter(remote.getOutputStream()); - - String str = "."; - while (!str.equals("")) - str = in.readLine(); - - out.println("HTTP/1.0 200 OK"); - out.println("Content-Type: text/html"); - out.println("Server: Bot"); - out.println(""); - out.println("

WebAPI is running

"); - out.flush(); - remote.close(); - } catch (Exception e) { - System.out.println("Error: " + e); - } - } - } + public static void webAPI() { } +} + diff --git a/src/main/java/tech/nevets/lunarbot/webserver/WebServer.java b/src/main/java/tech/nevets/lunarbot/webserver/WebServer.java new file mode 100644 index 0000000..d293e7b --- /dev/null +++ b/src/main/java/tech/nevets/lunarbot/webserver/WebServer.java @@ -0,0 +1,200 @@ +package tech.nevets.lunarbot.webserver; + + +import java.io.*; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Date; +import java.util.StringTokenizer; + +public class WebServer implements Runnable { + + static final File WEB_ROOT = new File("."); + static final String DEFAULT_FILE = "index.html"; + static final String FILE_NOT_FOUND = "404.html"; + static final String METHOD_NOT_SUPPORTED = "mns.html"; + + static final int PORT = 80; + + private Socket connect; + public WebServer(Socket c) { + connect = c; + } + + public static void main(String[] args) { + try { + ServerSocket serverConnect = new ServerSocket(PORT); + while (true) { + WebServer myServer = new WebServer(serverConnect.accept()); + Thread thread = new Thread(myServer); + thread.start(); + } + } catch (IOException e) { + System.out.println("Error occurred while connecting"); + e.printStackTrace(); + } + } + + @Override + public void run() { + BufferedReader in = null; + PrintWriter out = null; + BufferedOutputStream dataOut = null; + String fileRequested = null; + + try { + in = new BufferedReader(new InputStreamReader(connect.getInputStream())); + out = new PrintWriter(connect.getOutputStream()); + dataOut = new BufferedOutputStream(connect.getOutputStream()); + String input = in.readLine(); + StringTokenizer parse = new StringTokenizer(input); + String method = parse.nextToken().toUpperCase(); + fileRequested = parse.nextToken().toLowerCase(); + + if (!method.equals("GET") && !method.equals("HEAD")) { + File file = new File(WEB_ROOT, METHOD_NOT_SUPPORTED); + int fileLength = (int) file.length(); + String contentMimeType = "text/html"; + byte[] fileData = readFileData(file, fileLength); + + out.println("HTTP/1.1 501 Not Implemented"); + out.println("Server: Java HTTP Server from SSaurel : 1.0"); + out.println("Date: " + new Date()); + out.println("Content-type: " + contentMimeType); + out.println("Content-length: " + fileLength); + out.println(); + out.flush(); + dataOut.write(fileData, 0, fileLength); + dataOut.flush(); + + } else { + if (fileRequested.endsWith("/")) { + fileRequested += DEFAULT_FILE; + } + + File file = new File(WEB_ROOT, fileRequested); + int fileLength = (int) file.length(); + String content = getContentType(fileRequested); + + if (method.equals("GET")) { + byte[] fileData = readFileData(file, fileLength); + + out.println("HTTP/1.1 200 OK"); + out.println("Server: Java HTTP Server from SSaurel : 1.0"); + out.println("Date: " + new Date()); + out.println("Content-type: " + content); + out.println("Content-length: " + fileLength); + out.println(); + out.flush(); + + dataOut.write(fileData, 0, fileLength); + dataOut.flush(); + } + } + + } catch (FileNotFoundException fnfe) { + try { + fileNotFound(out, dataOut, fileRequested); + } catch (IOException ioe) { + System.err.println("Error with file not found exception : " + ioe.getMessage()); + } + + } catch (IOException ioe) { + System.err.println("Server error : " + ioe); + } finally { + try { + in.close(); + out.close(); + dataOut.close(); + connect.close(); + } catch (Exception e) { + System.err.println("Error closing stream : " + e.getMessage()); + } + } + + + } + + private byte[] readFileData(File file, int fileLength) throws IOException { + FileInputStream fileIn = null; + byte[] fileData = new byte[fileLength]; + + try { + fileIn = new FileInputStream(file); + fileIn.read(fileData); + } finally { + if (fileIn != null) + fileIn.close(); + } + + return fileData; + } + + private String getContentType(String fileRequested) { + if (fileRequested.endsWith(".htm") || fileRequested.endsWith(".html")) + return "text/html"; + else + return "text/plain"; + } + + private void fileNotFound(PrintWriter out, OutputStream dataOut, String fileRequested) throws IOException { + File file = new File(WEB_ROOT, FILE_NOT_FOUND); + int fileLength = (int) file.length(); + String content = "text/html"; + byte[] fileData = readFileData(file, fileLength); + + out.println("HTTP/1.1 404 File Not Found"); + out.println("Server: Java HTTP Server from SSaurel : 1.0"); + out.println("Date: " + new Date()); + out.println("Content-type: " + content); + out.println("Content-length: " + fileLength); + out.println(); + out.flush(); + + dataOut.write(fileData, 0, fileLength); + dataOut.flush(); + } + + /** + protected void start() { + ServerSocket s; + + System.out.println("Webserver starting up on port 80"); + System.out.println("(press ctrl-c to exit)"); + try { + // create the main server socket + s = new ServerSocket(80); + } catch (Exception e) { + System.out.println("Error: " + e); + return; + } + + System.out.println("Waiting for connection"); + for (;;) { + try { + + Socket remote = s.accept(); + + System.out.println("Connection, sending data."); + BufferedReader in = new BufferedReader(new InputStreamReader( + remote.getInputStream())); + PrintWriter out = new PrintWriter(remote.getOutputStream()); + + String str = "."; + while (!str.equals("")) + str = in.readLine(); + + out.println("HTTP/1.0 200 OK"); + out.println("Content-Type: text/html"); + out.println("Server: Bot"); + out.println(""); + out.println("

WebAPI is running

"); + out.flush(); + remote.close(); + } catch (Exception e) { + System.out.println("Error: " + e); + } + } + } + **/ +}