/* * The MIT License (MIT) * * Copyright (c) 2020 ROMVoid * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package net.romvoid.crashbot.utilities; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import org.json.JSONObject; import org.jsoup.Connection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; public class HasteUtil { /** The hastebin server URL. * TODO: Create a per-guild config-option to choose a custom URL * * */ private static String HASTEBIN_SERVER = "http://hastebin.mfnd.ru/"; // requires trailing slash private static URI uri; /** * A simple implementation of the Hastebin Client API, allowing data to be pasted online for * players to access. * * @param urlParameters The string to be sent in the body of the POST request * @return A formatted URL which links to the pasted file */ public synchronized static void paste(String urlParameters) { HttpURLConnection connection = null; try { //Create connection URL url = new URL(HASTEBIN_SERVER + "documents"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestMethod("POST"); connection.setDoInput(true); connection.setDoOutput(true); connection.setConnectTimeout(0); //Send request DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); //Get Response BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); JSONObject object = new JSONObject(rd.readLine()); String[] out = object.toString().replace("{", "").replace("}", "").replace("\"", "").split(":"); String output = HASTEBIN_SERVER + out[1]; uri = new URI(output + ".crashlog"); } catch (IOException | URISyntaxException e) { } finally { connection.disconnect(); } } public static URI getLink() { return uri; } /** * Returns the URL of the server being used. * * @return API to use for posting data */ public static String getPasteURL() { return HASTEBIN_SERVER; } /** * Sets the URL used by the paste method, allowing for the server logs are pasted to to be * dynamically changed. * * @param URL API URL of HasteBin instance */ public static void setPasteURL(String URL) { HASTEBIN_SERVER = URL; } /** * Grabs a HasteBin file from the Internet and attempts to return the file with formatting * intact. * * @return String HasteBin Raw Text */ public static synchronized String getPaste(String ID) { String URLString = HASTEBIN_SERVER + "raw/" + ID + "/"; try { URL URL = new URL(URLString); HttpURLConnection connection = (HttpURLConnection) URL.openConnection(); connection.setDoOutput(true); connection.setConnectTimeout(0); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String paste = ""; while (reader.ready()) { String line = reader.readLine(); if (line.contains("package")) continue; if (paste.equals("")) paste = line; else paste = paste + "\n" + line; } return paste; } catch (IOException e) { return ""; } } public static synchronized String getOtherPaste(String url) { try { URL URL = new URL(url); HttpURLConnection connection = (HttpURLConnection) URL.openConnection(); connection.setDoOutput(true); connection.setConnectTimeout(0); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String paste = ""; while (reader.ready()) { String line = reader.readLine(); if (line.contains("package")) continue; if (paste.equals("")) paste = line; else paste = paste + "\n" + line; uri = new URI(url); } return paste; } catch (IOException | URISyntaxException e) { return ""; } } public static synchronized String getCrashFromUbuntuPaste(String url) { try { Connection con = Jsoup.connect(url).ignoreHttpErrors(true).timeout(0); Document doc = con.get(); Element paste = doc.select("pre").get(1); return paste.text(); } catch (IOException e) { e.printStackTrace(); } return ""; } }