mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-07 16:49:10 +00:00
This commit is contained in:
@@ -10,21 +10,17 @@ import org.parabot.core.network.proxy.ProxyType;
|
|||||||
import org.parabot.core.ui.BotUI;
|
import org.parabot.core.ui.BotUI;
|
||||||
import org.parabot.core.ui.ServerSelector;
|
import org.parabot.core.ui.ServerSelector;
|
||||||
import org.parabot.core.ui.utils.UILog;
|
import org.parabot.core.ui.utils.UILog;
|
||||||
import org.parabot.environment.api.utils.WebUtil;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URL;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parabot v2.1
|
* Parabot v2.1
|
||||||
*
|
*
|
||||||
* @author Everel/Parnassian/Clisprail, Paradox, Matt, Dane
|
* @author Everel/Parnassian/Clisprail, Paradox/JKetelaar, Matt, Dane
|
||||||
* @version 2.1
|
* @version 2.1
|
||||||
* @see <a href="http://www.parabot.org">Homepage</a>
|
* @see <a href="http://www.parabot.org">Homepage</a>
|
||||||
*/
|
*/
|
||||||
@@ -91,14 +87,14 @@ public final class Landing {
|
|||||||
* @return String array with username and password
|
* @return String array with username and password
|
||||||
*/
|
*/
|
||||||
private static String[] getCredentials() {
|
private static String[] getCredentials() {
|
||||||
try {
|
// try {
|
||||||
BufferedReader bufferedReader = WebUtil.getReader(new URL(
|
// BufferedReader bufferedReader = WebUtil.getReader(new URL(
|
||||||
Configuration.GET_PASSWORD));
|
// Configuration.GET_PASSWORD));
|
||||||
if (bufferedReader.readLine() != null) {
|
// if (bufferedReader.readLine() != null) {
|
||||||
}
|
// }
|
||||||
} catch (IOException e) {
|
// } catch (IOException e) {
|
||||||
e.printStackTrace();
|
// e.printStackTrace();
|
||||||
}
|
// }
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ public class Configuration {
|
|||||||
public static final String DATA_API = "http://bdn.parabot.org/api/v2/data/";
|
public static final String DATA_API = "http://bdn.parabot.org/api/v2/data/";
|
||||||
public static final String ITEM_API = DATA_API + "items/";
|
public static final String ITEM_API = DATA_API + "items/";
|
||||||
|
|
||||||
public static final String BOT_VERSION = "2.1.12";
|
public static final String BOT_VERSION = "2.1.14";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import org.parabot.Landing;
|
|||||||
import org.parabot.environment.api.utils.WebUtil;
|
import org.parabot.environment.api.utils.WebUtil;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
@@ -99,8 +100,6 @@ public class Core {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private static boolean checksumValid(){
|
private static boolean checksumValid(){
|
||||||
String checksum = "";
|
|
||||||
|
|
||||||
File f = new File(Landing.class.getProtectionDomain().getCodeSource().getLocation().getFile());
|
File f = new File(Landing.class.getProtectionDomain().getCodeSource().getLocation().getFile());
|
||||||
if (f.isFile()) {
|
if (f.isFile()) {
|
||||||
try {
|
try {
|
||||||
@@ -152,11 +151,13 @@ public class Core {
|
|||||||
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(br);
|
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(br);
|
||||||
version = (String) object.get("result");
|
version = (String) object.get("result");
|
||||||
}
|
}
|
||||||
|
if (version != null) {
|
||||||
if (!Configuration.BOT_VERSION.equals(version)) {
|
if (!Configuration.BOT_VERSION.equals(version)) {
|
||||||
Core.verbose("Our version: " + Configuration.BOT_VERSION);
|
Core.verbose("Our version: " + Configuration.BOT_VERSION);
|
||||||
Core.verbose("Latest version: " + version);
|
Core.verbose("Latest version: " + version);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (NumberFormatException | IOException | ParseException e) {
|
} catch (NumberFormatException | IOException | ParseException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@@ -172,36 +173,30 @@ public class Core {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean policyValid(){
|
private static void validateCache(){
|
||||||
return new File(Directories.getSettingsPath() + "/java.policy").exists();
|
File[] cache = Directories.getCachePath().listFiles();
|
||||||
|
Integer lowest = null;
|
||||||
|
if (cache != null) {
|
||||||
|
for (File f : cache) {
|
||||||
|
int date = (int) (f.lastModified()/ 1000);
|
||||||
|
if (lowest == null || date < lowest){
|
||||||
|
lowest = date;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void createPolicy(){
|
|
||||||
File policy = new File(Directories.getSettingsPath() + "/java.policy");
|
|
||||||
if (!policy.exists()){
|
|
||||||
try {
|
try {
|
||||||
final BufferedReader in = WebUtil.getReader(Configuration.DATA_API + "policy");
|
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(WebUtil.getContents("http://bdn.parabot.org/api/v2/bot/cache", "date=" + lowest));
|
||||||
if (in != null) {
|
if ((boolean) object.get("result")){
|
||||||
String line;
|
Core.verbose("Making space for the latest cache files");
|
||||||
PrintWriter printWriter = new PrintWriter(Directories.getSettingsPath() + "/java.policy");
|
Directories.clearCache();
|
||||||
while ((line = in.readLine()) != null) {
|
}else{
|
||||||
if (line.contains("%parabot_resources%")){
|
Core.verbose("Cache is up to date");
|
||||||
line = line.replace("%parabot_resources%", Directories.getResourcesPath().getAbsolutePath());
|
|
||||||
}
|
}
|
||||||
printWriter.println(line);
|
} catch (MalformedURLException | ParseException e) {
|
||||||
}
|
|
||||||
printWriter.close();
|
|
||||||
in.close();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args){
|
|
||||||
createPolicy();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks for updates.
|
* Checks for updates.
|
||||||
@@ -210,6 +205,7 @@ public class Core {
|
|||||||
*/
|
*/
|
||||||
public static boolean isValid() {
|
public static boolean isValid() {
|
||||||
Core.verbose("Checking for updates...");
|
Core.verbose("Checking for updates...");
|
||||||
|
validateCache();
|
||||||
|
|
||||||
if (versionValid() && checksumValid()){
|
if (versionValid() && checksumValid()){
|
||||||
Core.verbose("No updates available.");
|
Core.verbose("No updates available.");
|
||||||
|
|||||||
@@ -194,4 +194,13 @@ public class Directories {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void clearCache() {
|
||||||
|
File[] cache = getCachePath().listFiles();
|
||||||
|
if (cache != null) {
|
||||||
|
for (File f : cache) {
|
||||||
|
Core.verbose("Clearing " + f.getName() + " from cache...");
|
||||||
|
f.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,14 +2,17 @@ package org.parabot.core.forum;
|
|||||||
|
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
import org.parabot.core.Configuration;
|
import org.parabot.core.Configuration;
|
||||||
|
import org.parabot.core.Context;
|
||||||
import org.parabot.core.Core;
|
import org.parabot.core.Core;
|
||||||
import org.parabot.core.parsers.scripts.BDNScripts;
|
import org.parabot.core.parsers.scripts.BDNScripts;
|
||||||
import org.parabot.core.parsers.servers.PublicServers;
|
import org.parabot.core.parsers.servers.PublicServers;
|
||||||
import org.parabot.core.ui.components.VerboseLoader;
|
import org.parabot.core.ui.components.VerboseLoader;
|
||||||
|
import org.parabot.environment.api.utils.PBPreferences;
|
||||||
import org.parabot.environment.api.utils.WebUtil;
|
import org.parabot.environment.api.utils.WebUtil;
|
||||||
import org.parabot.environment.scripts.executers.BDNScriptsExecuter;
|
import org.parabot.environment.scripts.executers.BDNScriptsExecuter;
|
||||||
import org.parabot.environment.servers.executers.PublicServerExecuter;
|
import org.parabot.environment.servers.executers.PublicServerExecuter;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
@@ -45,6 +48,7 @@ public final class AccountManager {
|
|||||||
accessors.add(BDNScriptsExecuter.MANAGER_FETCHER);
|
accessors.add(BDNScriptsExecuter.MANAGER_FETCHER);
|
||||||
accessors.add(PublicServers.MANAGER_FETCHER);
|
accessors.add(PublicServers.MANAGER_FETCHER);
|
||||||
accessors.add(PublicServerExecuter.MANAGER_FETCHER);
|
accessors.add(PublicServerExecuter.MANAGER_FETCHER);
|
||||||
|
accessors.add(PBPreferences.MANAGER_FETCHER);
|
||||||
|
|
||||||
for (final AccountManagerAccess accessor : accessors) {
|
for (final AccountManagerAccess accessor : accessors) {
|
||||||
accessor.setManager(instance);
|
accessor.setManager(instance);
|
||||||
@@ -60,30 +64,51 @@ public final class AccountManager {
|
|||||||
return account;
|
return account;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final boolean login(final String user, final String pass) {
|
public final boolean login(final String user, final String pass, boolean requestTwoStep) {
|
||||||
if (account != null) {
|
if (account != null) {
|
||||||
throw new IllegalStateException("Already logged in.");
|
throw new IllegalStateException("Already logged in.");
|
||||||
}
|
}
|
||||||
JSONObject result;
|
JSONObject result = null;
|
||||||
|
if (!requestTwoStep){
|
||||||
try {
|
try {
|
||||||
BufferedReader contents = WebUtil.getReader(WebUtil.getConnection(
|
BufferedReader contents = WebUtil.getReader(WebUtil.getConnection(
|
||||||
new URL(Configuration.LOGIN_SERVER),
|
new URL(Configuration.LOGIN_SERVER),
|
||||||
URLEncoder.encode(user, "UTF-8"),
|
URLEncoder.encode(user, "UTF-8"),
|
||||||
URLEncoder.encode(pass, "UTF-8")));
|
URLEncoder.encode(pass, "UTF-8")));
|
||||||
|
|
||||||
result = (JSONObject) WebUtil.getJsonParser().parse(contents);
|
result = (JSONObject) WebUtil.getJsonParser().parse(contents);
|
||||||
|
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}else{
|
||||||
|
try {
|
||||||
|
String two = JOptionPane.showInputDialog("Please provide your two factor authentication code\nYou can find this in either your email or the app you've setup");
|
||||||
|
if (two != null && two.length() > 0) {
|
||||||
|
String contents = WebUtil.getContents(Configuration.LOGIN_SERVER,
|
||||||
|
"username=" + URLEncoder.encode(user, "UTF-8") + "&password=" + URLEncoder.encode(pass, "UTF-8") + "&2fa=" + URLEncoder.encode(two, "UTF-8")
|
||||||
|
);
|
||||||
|
result = (JSONObject) WebUtil.getJsonParser().parse(contents);
|
||||||
|
}
|
||||||
|
} catch (Throwable t) {
|
||||||
|
t.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (result != null){
|
if (result != null) {
|
||||||
if (result.get("complete") != null) {
|
if (result.get("complete") != null) {
|
||||||
String api = (String) ((JSONObject) result.get("data")).get("api");
|
String api = (String) ((JSONObject) result.get("data")).get("api");
|
||||||
account = new Account(user, pass, api);
|
account = new Account(user, pass, api);
|
||||||
|
Context.setUsername(user);
|
||||||
return true;
|
return true;
|
||||||
}else if (result.get("error") != null){
|
} else if (result.get("error") != null) {
|
||||||
Core.verbose((String) result.get("error"));
|
String errorResult = (String) result.get("error");
|
||||||
|
if (errorResult.equals("2fa") || errorResult.equals("2fae")) {
|
||||||
|
return login(user, pass, true);
|
||||||
|
}
|
||||||
|
Core.verbose(errorResult);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,14 +47,11 @@ public class NetworkInterface {
|
|||||||
if (realMac != null)
|
if (realMac != null)
|
||||||
return realMac;
|
return realMac;
|
||||||
try {
|
try {
|
||||||
byte[] b = java.net.NetworkInterface.getByInetAddress(
|
return realMac = java.net.NetworkInterface.getByInetAddress(
|
||||||
InetAddress.getLocalHost()).getHardwareAddress();
|
InetAddress.getLocalHost()).getHardwareAddress();
|
||||||
if(b == null)
|
|
||||||
b = mac;
|
|
||||||
realMac = b;
|
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
return realMac = mac;
|
return mac;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NetworkInterface getByInetAddress(InetAddress addr) {
|
public static NetworkInterface getByInetAddress(InetAddress addr) {
|
||||||
|
|||||||
@@ -1,25 +1,14 @@
|
|||||||
package org.parabot.core.network.proxy;
|
package org.parabot.core.network.proxy;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import org.parabot.core.ui.utils.UILog;
|
||||||
import java.io.DataInputStream;
|
|
||||||
import java.io.DataOutputStream;
|
import javax.swing.*;
|
||||||
import java.io.IOException;
|
import java.io.*;
|
||||||
import java.io.InputStream;
|
import java.net.*;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.InetAddress;
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.Socket;
|
|
||||||
import java.net.SocketAddress;
|
|
||||||
import java.net.UnknownHostException;
|
|
||||||
import java.nio.channels.SocketChannel;
|
import java.nio.channels.SocketChannel;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.swing.JOptionPane;
|
|
||||||
|
|
||||||
import org.parabot.core.ui.utils.UILog;
|
|
||||||
|
|
||||||
public class ProxySocket extends Socket {
|
public class ProxySocket extends Socket {
|
||||||
|
|
||||||
private static List<ProxySocket> connections = new ArrayList<ProxySocket>();
|
private static List<ProxySocket> connections = new ArrayList<ProxySocket>();
|
||||||
@@ -294,7 +283,7 @@ public class ProxySocket extends Socket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void setLogin(String user, char[] pass) {
|
public static void setLogin(String user, char[] pass) {
|
||||||
setLogin(user,new String(pass));
|
setLogin(user, new String(pass));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setLogin(String user, String pass) {
|
public static void setLogin(String user, String pass) {
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public class LoginUI extends JFrame {
|
|||||||
String password = new String(txtPassword.getPassword());
|
String password = new String(txtPassword.getPassword());
|
||||||
|
|
||||||
if (username.length() > 0 && password.length() > 0) {
|
if (username.length() > 0 && password.length() > 0) {
|
||||||
if (manager.login(username, password)) {
|
if (manager.login(username, password, false)) {
|
||||||
Core.verbose("Logged in.");
|
Core.verbose("Logged in.");
|
||||||
instance.dispose();
|
instance.dispose();
|
||||||
Core.verbose("Running server selector.");
|
Core.verbose("Running server selector.");
|
||||||
@@ -53,7 +53,7 @@ public class LoginUI extends JFrame {
|
|||||||
|
|
||||||
private void attempt(String user, String pass) {
|
private void attempt(String user, String pass) {
|
||||||
Core.verbose("Logging in...");
|
Core.verbose("Logging in...");
|
||||||
if (manager.login(user, pass)) {
|
if (manager.login(user, pass, false)) {
|
||||||
Core.verbose("Logged in.");
|
Core.verbose("Logged in.");
|
||||||
instance.dispose();
|
instance.dispose();
|
||||||
Core.verbose("Running server selector.");
|
Core.verbose("Running server selector.");
|
||||||
|
|||||||
@@ -1,24 +1,11 @@
|
|||||||
package org.parabot.core.ui;
|
package org.parabot.core.ui;
|
||||||
|
|
||||||
import java.awt.event.ActionEvent;
|
import org.parabot.core.network.NetworkInterface;
|
||||||
import java.awt.event.ActionListener;
|
import org.parabot.core.network.proxy.ProxySocket;
|
||||||
import java.awt.event.KeyEvent;
|
import org.parabot.core.network.proxy.ProxyType;
|
||||||
import java.awt.event.KeyListener;
|
import org.parabot.core.ui.utils.UILog;
|
||||||
|
|
||||||
import javax.swing.Box;
|
import javax.swing.*;
|
||||||
import javax.swing.BoxLayout;
|
|
||||||
import javax.swing.JButton;
|
|
||||||
import javax.swing.JCheckBox;
|
|
||||||
import javax.swing.JComboBox;
|
|
||||||
import javax.swing.JFrame;
|
|
||||||
import javax.swing.JLabel;
|
|
||||||
import javax.swing.JList;
|
|
||||||
import javax.swing.JOptionPane;
|
|
||||||
import javax.swing.JPanel;
|
|
||||||
import javax.swing.JPasswordField;
|
|
||||||
import javax.swing.JScrollPane;
|
|
||||||
import javax.swing.JTextField;
|
|
||||||
import javax.swing.ListSelectionModel;
|
|
||||||
import javax.swing.border.EmptyBorder;
|
import javax.swing.border.EmptyBorder;
|
||||||
import javax.swing.event.DocumentEvent;
|
import javax.swing.event.DocumentEvent;
|
||||||
import javax.swing.event.DocumentListener;
|
import javax.swing.event.DocumentListener;
|
||||||
@@ -26,11 +13,10 @@ import javax.swing.text.AttributeSet;
|
|||||||
import javax.swing.text.BadLocationException;
|
import javax.swing.text.BadLocationException;
|
||||||
import javax.swing.text.Document;
|
import javax.swing.text.Document;
|
||||||
import javax.swing.text.PlainDocument;
|
import javax.swing.text.PlainDocument;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
import org.parabot.core.network.NetworkInterface;
|
import java.awt.event.ActionListener;
|
||||||
import org.parabot.core.network.proxy.ProxySocket;
|
import java.awt.event.KeyEvent;
|
||||||
import org.parabot.core.network.proxy.ProxyType;
|
import java.awt.event.KeyListener;
|
||||||
import org.parabot.core.ui.utils.UILog;
|
|
||||||
|
|
||||||
public class NetworkUI extends JFrame implements KeyListener, ActionListener,
|
public class NetworkUI extends JFrame implements KeyListener, ActionListener,
|
||||||
DocumentListener {
|
DocumentListener {
|
||||||
@@ -102,12 +88,14 @@ public class NetworkUI extends JFrame implements KeyListener, ActionListener,
|
|||||||
}
|
}
|
||||||
|
|
||||||
authCheckBox = new JCheckBox("Auth");
|
authCheckBox = new JCheckBox("Auth");
|
||||||
|
authCheckBox.setSelected(ProxySocket.auth);
|
||||||
authCheckBox.addActionListener(this);
|
authCheckBox.addActionListener(this);
|
||||||
|
|
||||||
authUsername = new JTextField();
|
authUsername = new JTextField();
|
||||||
authUsername.setEnabled(false);
|
|
||||||
authPassword = new JPasswordField();
|
authPassword = new JPasswordField();
|
||||||
authPassword.setEnabled(false);
|
|
||||||
|
authUsername.setEnabled(authCheckBox.isSelected());
|
||||||
|
authPassword.setEnabled(authCheckBox.isSelected());
|
||||||
|
|
||||||
JPanel p = createPanelUI();
|
JPanel p = createPanelUI();
|
||||||
add(p);
|
add(p);
|
||||||
@@ -247,8 +235,11 @@ public class NetworkUI extends JFrame implements KeyListener, ActionListener,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String username = authUsername.getText();
|
||||||
|
char[] password = authPassword.getPassword();
|
||||||
|
|
||||||
ProxySocket
|
ProxySocket
|
||||||
.setLogin(authUsername.getText(), authPassword.getPassword());
|
.setLogin(username, password);
|
||||||
|
|
||||||
byte[] mac = new byte[macList.length];
|
byte[] mac = new byte[macList.length];
|
||||||
for (int i = 0; i < mac.length; i++)
|
for (int i = 0; i < mac.length; i++)
|
||||||
@@ -265,8 +256,11 @@ public class NetworkUI extends JFrame implements KeyListener, ActionListener,
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ProxySocket.setProxy((ProxyType) proxyType.getSelectedItem(),
|
ProxyType type = (ProxyType) proxyType.getSelectedItem();
|
||||||
proxyHost.getText(), proxyPort.getValue());
|
String host = proxyHost.getText();
|
||||||
|
int port = proxyPort.getValue();
|
||||||
|
|
||||||
|
ProxySocket.setProxy(type, host, port);
|
||||||
UILog.log("Info", "Network settings have been set!");
|
UILog.log("Info", "Network settings have been set!");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
UILog.log("Error",
|
UILog.log("Error",
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import org.parabot.core.Context;
|
|||||||
import org.parabot.core.Directories;
|
import org.parabot.core.Directories;
|
||||||
import org.parabot.core.desc.ScriptDescription;
|
import org.parabot.core.desc.ScriptDescription;
|
||||||
import org.parabot.core.parsers.scripts.ScriptParser;
|
import org.parabot.core.parsers.scripts.ScriptParser;
|
||||||
|
import org.parabot.environment.api.utils.WebUtil;
|
||||||
import org.parabot.environment.scripts.Category;
|
import org.parabot.environment.scripts.Category;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
@@ -16,6 +17,9 @@ import java.awt.*;
|
|||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -156,6 +160,11 @@ public final class ScriptSelector extends JFrame {
|
|||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
String s = getScriptName(tree.getSelectionPath().toString());
|
String s = getScriptName(tree.getSelectionPath().toString());
|
||||||
if (s != null) {
|
if (s != null) {
|
||||||
|
try {
|
||||||
|
WebUtil.getContents("http://bdn.parabot.org/api/v2/scripts/local", "script=" + URLEncoder.encode(s, "UTF-8") + "&username=" + URLEncoder.encode(Context.getUsername(), "UTF-8"));
|
||||||
|
} catch (MalformedURLException | UnsupportedEncodingException e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
runScript(format.get(s));
|
runScript(format.get(s));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.parabot.core.ui.components;
|
package org.parabot.core.ui.components;
|
||||||
|
|
||||||
import org.parabot.core.Context;
|
|
||||||
import org.parabot.core.Core;
|
import org.parabot.core.Core;
|
||||||
import org.parabot.core.forum.AccountManager;
|
import org.parabot.core.forum.AccountManager;
|
||||||
import org.parabot.core.forum.AccountManagerAccess;
|
import org.parabot.core.forum.AccountManagerAccess;
|
||||||
@@ -61,8 +60,7 @@ public class VerboseLoader extends JPanel implements ProgressListener {
|
|||||||
setOpaque(false);
|
setOpaque(false);
|
||||||
|
|
||||||
if(username != null && password != null) {
|
if(username != null && password != null) {
|
||||||
if(Core.inDebugMode() || manager.login(username, password)) {
|
if(Core.inDebugMode() || manager.login(username, password, false)) {
|
||||||
Context.setUsername(username);
|
|
||||||
currentState = STATE_SERVER_SELECT;
|
currentState = STATE_SERVER_SELECT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -133,7 +131,7 @@ public class VerboseLoader extends JPanel implements ProgressListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
if(manager.login(userInput.getText(), passInput.getText())) {
|
if(manager.login(userInput.getText(), passInput.getText(), false)) {
|
||||||
switchState(STATE_SERVER_SELECT);
|
switchState(STATE_SERVER_SELECT);
|
||||||
} else {
|
} else {
|
||||||
Core.verbose("Failed to log in.");
|
Core.verbose("Failed to log in.");
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package org.parabot.environment.api.utils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author JKetelaar
|
||||||
|
*/
|
||||||
|
public class FileUtil {
|
||||||
|
|
||||||
|
public static String getChecksum(File file){
|
||||||
|
if (file.isFile()) {
|
||||||
|
try {
|
||||||
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||||
|
if (file.exists()) {
|
||||||
|
FileInputStream fis = new FileInputStream(file);
|
||||||
|
byte[] dataBytes = new byte[1024];
|
||||||
|
|
||||||
|
int nread;
|
||||||
|
|
||||||
|
while ((nread = fis.read(dataBytes)) != -1) {
|
||||||
|
md.update(dataBytes, 0, nread);
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] mdbytes = md.digest();
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder("");
|
||||||
|
for (int i = 0; i < mdbytes.length; i++) {
|
||||||
|
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
} catch (NoSuchAlgorithmException | IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] getChecksumBytes(File file) {
|
||||||
|
if (file.isFile()) {
|
||||||
|
try {
|
||||||
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||||
|
if (file.exists()) {
|
||||||
|
FileInputStream fis = new FileInputStream(file);
|
||||||
|
byte[] dataBytes = new byte[1024];
|
||||||
|
|
||||||
|
int nread;
|
||||||
|
|
||||||
|
while ((nread = fis.read(dataBytes)) != -1) {
|
||||||
|
md.update(dataBytes, 0, nread);
|
||||||
|
}
|
||||||
|
|
||||||
|
return md.digest();
|
||||||
|
}
|
||||||
|
} catch (NoSuchAlgorithmException | IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,132 @@
|
|||||||
|
package org.parabot.environment.api.utils;
|
||||||
|
|
||||||
|
import org.json.simple.JSONArray;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
import org.json.simple.parser.ParseException;
|
||||||
|
import org.parabot.core.forum.AccountManager;
|
||||||
|
import org.parabot.core.forum.AccountManagerAccess;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author JKetelaar
|
||||||
|
*/
|
||||||
|
public class PBPreferences {
|
||||||
|
|
||||||
|
private static AccountManager manager;
|
||||||
|
|
||||||
|
private Properties properties;
|
||||||
|
private int scriptID;
|
||||||
|
|
||||||
|
public static final AccountManagerAccess MANAGER_FETCHER = new AccountManagerAccess() {
|
||||||
|
@Override
|
||||||
|
public final void setManager(AccountManager manager) {
|
||||||
|
PBPreferences.manager = manager;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public PBPreferences(int scriptID) {
|
||||||
|
this.scriptID = scriptID;
|
||||||
|
this.updateSettings();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateSettings() {
|
||||||
|
properties = new Properties();
|
||||||
|
try {
|
||||||
|
JSONObject result = (JSONObject) WebUtil.getJsonParser().parse(
|
||||||
|
WebUtil.getContents("http://bdn.parabot.org/api/v2/user/preferences/" + scriptID,
|
||||||
|
"apikey=" + manager.getAccount().getApi())
|
||||||
|
);
|
||||||
|
|
||||||
|
JSONArray resultArray;
|
||||||
|
if ((resultArray = ((JSONArray) result.get("result"))) != null) {
|
||||||
|
for(Object rObject : resultArray) {
|
||||||
|
JSONObject resultObject = (JSONObject) rObject;
|
||||||
|
for (Object map : resultObject.entrySet()) {
|
||||||
|
Map.Entry<?, ?> pairs = (Map.Entry<?, ?>) map;
|
||||||
|
properties.put(pairs.getKey(), pairs.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException | ParseException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change a setting
|
||||||
|
* @param key
|
||||||
|
* @param value
|
||||||
|
*/
|
||||||
|
public void adjustSettings(Object key, Object value) {
|
||||||
|
this.addSetting(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a setting value
|
||||||
|
* @param key
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Object getSetting(Object key){
|
||||||
|
return this.properties.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a setting value as string
|
||||||
|
* @param key
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getSetting(String key){
|
||||||
|
return this.properties.getProperty(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a setting
|
||||||
|
* @param key
|
||||||
|
*/
|
||||||
|
public void removeSetting(Object key){
|
||||||
|
try {
|
||||||
|
JSONObject result = (JSONObject) WebUtil.getJsonParser().parse(
|
||||||
|
WebUtil.getContents("http://bdn.parabot.org/api/v2/user/preferences/set/",
|
||||||
|
"apikey=" + manager.getAccount().getApi() +
|
||||||
|
"&key=" + URLEncoder.encode(String.valueOf(key), "UTF-8") +
|
||||||
|
"&script=" + String.valueOf(scriptID)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
if ((boolean)result.get("result")){
|
||||||
|
this.properties.remove(key);
|
||||||
|
}
|
||||||
|
} catch (ParseException | MalformedURLException | UnsupportedEncodingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a setting
|
||||||
|
* @param key
|
||||||
|
* @param value
|
||||||
|
*/
|
||||||
|
public void addSetting(Object key, Object value){
|
||||||
|
try {
|
||||||
|
JSONObject result = (JSONObject) WebUtil.getJsonParser().parse(
|
||||||
|
WebUtil.getContents("http://bdn.parabot.org/api/v2/user/preferences/set/",
|
||||||
|
"apikey=" + manager.getAccount().getApi() +
|
||||||
|
"&key=" + URLEncoder.encode(String.valueOf(key), "UTF-8") +
|
||||||
|
"&value=" + URLEncoder.encode(String.valueOf(value), "UTF-8") +
|
||||||
|
"&script=" + String.valueOf(scriptID)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
if ((boolean)result.get("result")){
|
||||||
|
this.properties.put(key, value);
|
||||||
|
}
|
||||||
|
} catch (ParseException | MalformedURLException | UnsupportedEncodingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import org.parabot.core.Context;
|
|||||||
import org.parabot.core.Core;
|
import org.parabot.core.Core;
|
||||||
import org.parabot.core.ui.BotUI;
|
import org.parabot.core.ui.BotUI;
|
||||||
import org.parabot.core.ui.Logger;
|
import org.parabot.core.ui.Logger;
|
||||||
|
import org.parabot.environment.api.utils.PBPreferences;
|
||||||
import org.parabot.environment.api.utils.Time;
|
import org.parabot.environment.api.utils.Time;
|
||||||
import org.parabot.environment.scripts.framework.*;
|
import org.parabot.environment.scripts.framework.*;
|
||||||
import org.parabot.environment.scripts.framework.Frameworks;
|
import org.parabot.environment.scripts.framework.Frameworks;
|
||||||
@@ -28,9 +29,11 @@ public class Script implements Runnable {
|
|||||||
public static final int STATE_STOPPED = 2;
|
public static final int STATE_STOPPED = 2;
|
||||||
|
|
||||||
private Collection<Strategy> strategies;
|
private Collection<Strategy> strategies;
|
||||||
|
private PBPreferences preferences;
|
||||||
private AbstractFramework frameWork;
|
private AbstractFramework frameWork;
|
||||||
private int state;
|
private int state;
|
||||||
private int frameWorkType;
|
private int frameWorkType;
|
||||||
|
private int scriptID;
|
||||||
|
|
||||||
public boolean onExecute() {
|
public boolean onExecute() {
|
||||||
return true;
|
return true;
|
||||||
@@ -167,4 +170,15 @@ public class Script implements Runnable {
|
|||||||
public int getState() {
|
public int getState() {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PBPreferences getPreferences(){
|
||||||
|
if (this.preferences == null){
|
||||||
|
this.preferences = new PBPreferences(scriptID);
|
||||||
|
}
|
||||||
|
return this.preferences;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScriptID(int scriptID){
|
||||||
|
this.scriptID = scriptID;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ public class BDNScriptsExecuter extends ScriptExecuter {
|
|||||||
final Class<?> scriptClass = loader.loadClass(className);
|
final Class<?> scriptClass = loader.loadClass(className);
|
||||||
final Constructor<?> con = scriptClass.getConstructor();
|
final Constructor<?> con = scriptClass.getConstructor();
|
||||||
final Script script = (Script) con.newInstance();
|
final Script script = (Script) con.newInstance();
|
||||||
|
script.setScriptID(this.id);
|
||||||
super.finalize(tg, script);
|
super.finalize(tg, script);
|
||||||
|
|
||||||
if (manager.getAccount().getApi() != null) {
|
if (manager.getAccount().getApi() != null) {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package org.parabot.environment.scripts.executers;
|
package org.parabot.environment.scripts.executers;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
|
|
||||||
import org.parabot.environment.scripts.Script;
|
import org.parabot.environment.scripts.Script;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Loads a locally stored script
|
* Loads a locally stored script
|
||||||
|
|||||||
Reference in New Issue
Block a user