Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.speljohan.rsbot.gui;
- import java.awt.Component;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Insets;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.Map;
- import java.util.TreeMap;
- import javax.naming.NamingException;
- import javax.swing.JButton;
- import javax.swing.JCheckBox;
- 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.JSpinner;
- import javax.swing.JTextField;
- import javax.swing.ListSelectionModel;
- import javax.swing.SpinnerNumberModel;
- import com.speljohan.rsbot.util.GlobalConfiguration;
- /**
- * This will handle the management of Accounts for the Bot.<br>
- * Information will be stored in a file specified by fileName.<br>
- * Format for accounts should be:<br>
- * Name:Password[:Pin] <br>
- * Pin may be omitted if no Pin on Account
- *
- * @author Fusion89k
- */
- // The brackets [] are there to denote optional.
- public class AccountManager extends JFrame implements ActionListener {
- private static final long serialVersionUID = 6401178388485322197L;
- private JList names;
- private GridBagConstraints c;
- public final static String sepChar = ":",
- fileName = GlobalConfiguration.Paths.getAccountsFile();
- private final String emptyName = "No Accounts Found";
- static String key;
- private static Map<String, String> accounts;
- public AccountManager() {
- if (accounts.size() < 1)
- accounts.put(emptyName, "");
- createAndDisplayGUI();
- }
- /**
- * Access the password of the given account
- *
- * @param name
- * Name of account to access password of
- * @return Unencrypted Password
- */
- public static String getPassword(String name) {
- String password = accounts.get(name);
- if (password != null && password.contains(sepChar))
- password = password.split(sepChar, 2)[0];
- return crypt(password, false);
- }
- /**
- * Access the pin for the given account
- *
- * @param name
- * Name of the account
- * @return Pin or -1 if no pin
- */
- public static String getPin(String name) {
- String all = accounts.get(name);
- if (all.contains(sepChar))
- return all.split(sepChar, 2)[1];
- return "-1";
- }
- /**
- * Access the list of names for loaded accounts
- *
- * @return Array of the Names
- */
- public static String[] getAccountNames() {
- return accounts.keySet().toArray(new String[0]);
- }
- /**
- * Allows addition of an account
- *
- * @param info
- * Account info in format of name:pass[:pin]
- */
- public static void addAccount(String info) throws NamingException {
- if (!info.contains(sepChar))
- throw new NamingException(info + " is not in the correct format.");
- String[] parts = info.split(sepChar);
- accounts.put(fixName(parts[0]), crypt(parts[1], true));
- if (parts.length > 2)
- accounts.put(fixName(parts[0]), crypt(parts[1], true) + sepChar
- + parts[2]);
- saveAccounts();
- }
- /**
- * Creates and Displays the main GUI
- *
- * This GUI has the list and the main buttons
- */
- private void createAndDisplayGUI() {
- c = new GridBagConstraints();
- // Main Panel with everything
- JPanel pane = new JPanel(new GridBagLayout());
- // Makes the List
- names = new JList(accounts.keySet().toArray(new String[0]));
- // Only one selection at a time
- names.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- // Will display the password and pin when double clicked or right
- // clicked
- names.addMouseListener(new MouseListener() {
- public void mouseClicked(MouseEvent click) {
- if (click.getClickCount() > 1
- || click.getButton() == MouseEvent.BUTTON3) {
- String clicked = accounts
- .get(((JList) click.getComponent())
- .getSelectedValue());
- if (clicked.isEmpty())
- return;
- String pass = clicked.contains(sepChar) ? clicked
- .substring(0, clicked.indexOf(sepChar)) : clicked;
- pass = crypt(pass, false);
- String info = "Password: " + pass;
- if (clicked.contains(sepChar)) {
- info += "\nPin: "
- + clicked.substring(
- clicked.indexOf(sepChar) + 1, clicked
- .length());
- }
- JOptionPane.showMessageDialog(null, info);
- }
- }
- public void mouseEntered(MouseEvent click) {
- }
- public void mouseExited(MouseEvent click) {
- }
- public void mousePressed(MouseEvent click) {
- }
- public void mouseReleased(MouseEvent click) {
- }
- });
- // Enables scrolling through the List
- JScrollPane scroll = new JScrollPane(names);
- JButton add = new JButton("Add Account");// Button 1
- JButton del = new JButton("Delete Account");// Button 2
- JButton save = new JButton("Save Accounts");// Button 3
- add.addActionListener(this);
- del.addActionListener(this);
- save.addActionListener(this);
- // Positions Everything Correctly on the panel
- c.gridy = 0;
- c.gridwidth = GridBagConstraints.REMAINDER;
- c.fill = GridBagConstraints.BOTH;
- pane.add(scroll, c);
- c.gridwidth = 1;
- c.gridy = 1;
- c.gridx = 0;
- c.fill = GridBagConstraints.NONE;
- c.insets = new Insets(5, 5, 0, 5);
- c.anchor = GridBagConstraints.CENTER;
- pane.add(add, c);
- c.gridx = GridBagConstraints.RELATIVE;
- pane.add(del, c);
- pane.add(save, c);
- names.setSelectedIndex(0);
- add(pane);
- pack();
- setLocationRelativeTo(getOwner());
- setVisible(true);
- setResizable(false);
- setTitle("Account Manager");
- }
- /**
- * Controls all of the button and checkBox actions of the program
- */
- public void actionPerformed(ActionEvent event) {
- JFrame parentFrame;
- Component comp = ((Component) event.getSource()).getParent();
- while (!(comp instanceof JFrame))
- comp = comp.getParent();
- parentFrame = (JFrame) comp;
- if (event.getSource() instanceof JButton) {
- switch (((JButton) event.getSource()).getText().charAt(0)) {
- case 'A':// Add Accounts
- if (event.getActionCommand().contains("dd"))
- addAccount();
- else {// Update the List with the new Account
- JPanel pane = (JPanel) ((JButton) event.getSource())
- .getParent();
- String name = null, pass = null, pin = "";
- for (Component c : pane.getComponents()) {
- if (c instanceof JPasswordField)
- pass = ((JTextField) c).getText();
- else if (c instanceof JTextField)
- name = ((JTextField) c).getText();
- else if (c instanceof JSpinner)
- pin += ((JSpinner) c).getValue();
- }
- if (name.isEmpty() || pass.isEmpty()) {
- JOptionPane.showMessageDialog(null, "Empty Fields");
- return;
- }
- accounts.put(fixName(name), crypt(pass, true)
- + (pin.isEmpty() ? "" : sepChar + pin));
- refreshList();
- names.validate();
- parentFrame.dispose();
- }
- break;
- case 'D':// Delete Accounts
- if (accounts.get(names.getSelectedValue()).isEmpty())
- return;
- int yes = JOptionPane.showConfirmDialog(this,
- "Are you sure you want to delete "
- + names.getSelectedValue() + "?", "Delete",
- JOptionPane.YES_NO_OPTION);
- if (yes == 0) {
- accounts.remove(names.getSelectedValue());
- refreshList();
- names.validate();
- }
- break;
- case 'S':// Save Accounts
- saveAccounts();
- break;
- }
- } else if (event.getSource() instanceof JCheckBox) {
- // CheckBox for Pins add and remove JSpinners
- JCheckBox jcb = ((JCheckBox) event.getSource());
- if (jcb.isSelected()) {
- // Add Spinners
- c = new GridBagConstraints();
- c.gridy = 2;
- c.gridx = 1;
- c.weightx = 1;
- c.insets = new Insets(0, 0, 5, 5);
- JSpinner[] spins = new JSpinner[4];
- for (int i = 0; i < spins.length; i++) {
- spins[i] = new JSpinner(new SpinnerNumberModel(0, 0, 9, 1));
- jcb.getParent().add(spins[i], c);
- c.gridx = GridBagConstraints.RELATIVE;
- }
- parentFrame.pack();
- } else {
- // Remove Spinners
- for (Component c : jcb.getParent().getComponents())
- if (c instanceof JSpinner)
- jcb.getParent().remove(c);
- parentFrame.validate();
- }
- }
- }
- /**
- * Gets the necessary information from the user to make a new account
- */
- private void addAccount() {
- JFrame addFrame = new JFrame("Add Account");
- JPanel pane = new JPanel(new GridBagLayout());
- c = new GridBagConstraints();
- JTextField userName = new JTextField(8);
- JPasswordField userPass = new JPasswordField(8);
- userPass.setEchoChar('*');
- c.gridwidth = 2;
- c.fill = GridBagConstraints.HORIZONTAL;
- c.insets = new Insets(5, 5, 0, 7);
- pane.add(new JLabel("User Name: ", JLabel.CENTER), c);
- c.gridx = GridBagConstraints.RELATIVE;
- c.gridwidth = 3;
- c.insets = new Insets(5, 0, 0, 5);
- pane.add(userName, c);
- c.insets = new Insets(5, 5, 5, 7);
- c.gridwidth = 2;
- c.gridx = 0;
- c.gridy = 1;
- pane.add(new JLabel("Password: ", JLabel.CENTER), c);
- c.insets = new Insets(5, 0, 5, 5);
- c.gridwidth = 3;
- c.gridx = GridBagConstraints.RELATIVE;
- pane.add(userPass, c);
- JCheckBox pinCheck = new JCheckBox("Pin");
- pinCheck.addActionListener(this);
- c = new GridBagConstraints();
- c.gridy = 2;
- c.insets = new Insets(0, 5, 5, 5);
- pane.add(pinCheck, c);
- JButton save = new JButton("Add Account");
- save.setActionCommand("Update");
- save.addActionListener(this);
- c.gridy = 3;
- c.gridwidth = GridBagConstraints.REMAINDER;
- pane.add(save, c);
- addFrame.add(pane);
- addFrame.pack();
- addFrame.setLocationRelativeTo(this);
- addFrame.setVisible(true);
- }
- /**
- * Resets the Model for the List to reflect changes made to the accounts Map
- */
- private void refreshList() {
- if (accounts.keySet().size() > 1
- && accounts.keySet().contains(emptyName))
- accounts.remove(emptyName);
- else if (accounts.keySet().size() < 1)
- accounts.put(emptyName, "");
- names.setListData(accounts.keySet().toArray(new String[0]));
- names.getParent().validate();
- }
- /**
- * Capitalizes the first character and replaces spaces with underscores
- * Purely esthetic
- *
- * @param name
- * Name to fix
- * @return Fixed name
- */
- private static String fixName(String name) {
- if (name.charAt(0) > 91)
- name = ((char) ((int) name.charAt(0) - 32)) + name.substring(1);
- while (name.contains(" "))
- name = name.substring(0, name.indexOf(" ")) + "_"
- + name.substring(name.indexOf(" ") + 1);
- return name;
- }
- /**
- * Encrypts/Decrypts a string using a SHA1 hash of (String) key from other
- * users computers
- *
- * @author Jacmob
- */
- private static String crypt(String start, boolean en) {
- String delim = "a";
- if (start == null)
- return null;
- byte[] hashedkey;
- byte[] password;
- int i;
- try {
- hashedkey = SHA1(key);
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- return start;
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- return start;
- }
- if (en) {
- String end = "";
- password = start.getBytes();
- for (i = 0; i < hashedkey.length; i ++) {
- if (i < start.length()) {
- end += (hashedkey[i] + password[i]) + delim;
- } else {
- end += hashedkey[i] + delim;
- }
- }
- return end.substring(0, end.length() - delim.length());
- } else {
- String[] temp = start.split(delim);
- password = new byte[temp.length];
- for (i = 0; i < hashedkey.length; i ++) {
- int temp2 = Integer.parseInt(temp[i]);
- if (hashedkey[i] == temp2)
- break;
- else
- password[i] = (byte)(temp2 - hashedkey[i]);
- }
- return new String(password, 0, i);
- }
- }
- /**
- * Returns SHA1 hash of a String.
- */
- private static byte[] SHA1(String in) throws NoSuchAlgorithmException,
- UnsupportedEncodingException {
- MessageDigest md;
- md = MessageDigest.getInstance("SHA-1");
- byte[] sha1hash = new byte[40];
- md.update(in.getBytes("iso-8859-1"), 0, in.length());
- sha1hash = md.digest();
- return sha1hash;
- }
- /**
- * Loads the accounts from the file
- */
- private static Map<String, String> loadAccounts() throws NamingException {
- TreeMap<String, String> accounts = new TreeMap<String, String>();
- File accountFile = new File(fileName);
- if (accountFile.exists()) {
- try {
- BufferedReader in = new BufferedReader(new FileReader(
- accountFile));
- String temp;// Used to store the lines from the file
- while ((temp = in.readLine()) != null) {
- if (temp.isEmpty())
- continue;
- // Means that it is in old format
- if (temp.contains("USERNAME"))
- return fixAccounts();
- // If for some reason the format is not as expected
- if (!temp.contains(sepChar))
- throw new NamingException("Invliad Storage of: " + temp);
- String[] parts = temp.split(sepChar, 2);
- for (String s : parts)
- if (s.isEmpty())
- throw new NamingException("Invliad Storage of: "
- + temp);
- // Formats the name
- parts[0] = fixName(parts[0]);
- // Checks Pin Validity
- try {
- if (parts.length > 2
- && (parts[2].length() != 4 || Integer
- .parseInt(parts[2]) >= 0))
- throw new NamingException("Invalid Pin: "
- + parts[2] + " On Account: " + parts[0]);
- } catch (NumberFormatException e) {
- throw new NamingException("Invalid Pin: " + parts[2]
- + " On Account: " + parts[0]);
- }
- System.out
- .println("Loaded: " + parts[0] + " | " + parts[1]);
- accounts.put(parts[0], parts[1]);
- }
- in.close();
- } catch (FileNotFoundException e) {// From File Loading
- e.printStackTrace();
- } catch (IOException e) {// From Reader traversal
- e.printStackTrace();
- }
- }
- return accounts;
- }
- /**
- * Will convert account files from the old format to the new format
- *
- * Old Format - key=value, key=value,[key=value,]
- *
- * New Format - name:pass[:pin]
- *
- * @return A mapping of name password/pin pairs
- */
- private static Map<String, String> fixAccounts() throws NamingException {
- String endResult = "";
- try {
- BufferedReader in = new BufferedReader(new FileReader(fileName));
- String temp;
- while ((temp = in.readLine()) != null) {
- if (temp.contains("USERNAME")) {
- String pass = null, name = null, pin = null;
- // Old Format key=value,
- for (String part : temp.split(",")) {
- // Splits Keys and Values
- String[] pieces = part.split("=", 2);
- if (pieces[0].equals("PASSWORD"))// Key
- pass = crypt(pieces[1], true);
- else if (pieces[0].equals("USERNAME"))// Key
- name = pieces[1];
- else if (pieces[0].equals("PIN"))// Key
- pin = pieces[1];
- }
- try {
- // Converts to new format name:pass[:pin]
- endResult += name + sepChar + pass
- + (pin != null ? (sepChar + pin) : "");
- } catch (NullPointerException e) {
- // Will only happen if one of the keys is not found
- System.out.println("File Already Fixed");
- try {
- return loadAccounts();
- } catch (NamingException e1) {
- e1.printStackTrace();
- }
- }
- endResult += System.getProperty("line.separator");
- }
- }
- in.close();
- // Writes the new format to the file
- BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
- out.append(endResult);
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- // Reload the accounts
- return loadAccounts();
- }
- /**
- * Writes the accounts to the file
- */
- private static void saveAccounts() {
- File accountFile = new File(fileName);
- try {
- BufferedWriter out = new BufferedWriter(new FileWriter(accountFile));
- for (String s : accounts.keySet()) {
- if (accounts.get(s).isEmpty())
- continue;
- out.append(s + sepChar + accounts.get(s));
- out.newLine();
- }
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- static {
- key = System.getProperty("user.name").toLowerCase()
- + System.getProperty("user.language");
- try {
- accounts = loadAccounts();
- } catch (NamingException e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment