Guest User

Untitled

a guest
Jul 2nd, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.63 KB | None | 0 0
  1. package org.rsbot.gui;
  2.  
  3. import org.rsbot.Configuration;
  4. import org.rsbot.bot.Bot;
  5. import org.rsbot.script.AccountStore;
  6. import org.rsbot.script.AccountStore.Account;
  7. import org.rsbot.script.randoms.ImprovedLoginBot;
  8. import org.rsbot.security.UniqueID;
  9.  
  10. import javax.swing.*;
  11. import javax.swing.event.ListSelectionEvent;
  12. import javax.swing.event.ListSelectionListener;
  13. import javax.swing.table.AbstractTableModel;
  14. import javax.swing.table.DefaultTableCellRenderer;
  15. import javax.swing.table.TableColumnModel;
  16. import java.awt.*;
  17. import java.awt.event.ActionEvent;
  18. import java.awt.event.ActionListener;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.logging.Logger;
  25.  
  26. /**
  27.  * @author Tekk
  28.  * @author Aion
  29.  * @author Timer
  30.  * @author Paris
  31.  */
  32. public class AccountManager extends JDialog implements ActionListener {
  33.     private static final long serialVersionUID = 2834954922670757338L;
  34.  
  35.     private static final String FILE_ACCOUNT_STORAGE = Configuration.Paths.getAccountsFile();
  36.  
  37.     private static final String[] RANDOM_REWARDS = {"Cash", "Runes", "Coal", "Essence", "Ore", "Bars", "Gems", "Herbs",
  38.             "Seeds", "Charms", "Surprise", "Emote", "Costume", "Attack",
  39.             "Defence", "Strength", "Constitution", "Range", "Prayer", "Magic",
  40.             "Cooking", "Woodcutting", "Fletching", "Fishing", "Firemaking",
  41.             "Crafting", "Smithing", "Mining", "Herblore", "Agility", "Thieving",
  42.             "Slayer", "Farming", "Runecrafting", "Hunter", "Construction",
  43.             "Summoning", "Dungeoneering"};
  44.  
  45.     private static final String[] VALID_KEYS = {"pin", "reward", "member", "take_breaks"};
  46.  
  47.     private static final Logger log = Logger.getLogger(AccountManager.class.getName());
  48.  
  49.     private static final AccountStore accountStore = new AccountStore(new File(FILE_ACCOUNT_STORAGE));
  50.  
  51.     static {
  52.         accountStore.setPassword(UniqueID.getID());
  53.         try {
  54.             accountStore.load();
  55.         } catch (final IOException ignored) {
  56.             log.warning("Could not load accounts");
  57.         }
  58.     }
  59.  
  60.     private static class RandomRewardEditor extends DefaultCellEditor {
  61.         private static final long serialVersionUID = 6519185448833736787L;
  62.  
  63.         public RandomRewardEditor() {
  64.             super(new JComboBox(RANDOM_REWARDS));
  65.         }
  66.     }
  67.  
  68.     private static class PasswordCellEditor extends DefaultCellEditor {
  69.         private static final long serialVersionUID = -8042183192369284908L;
  70.  
  71.         public PasswordCellEditor() {
  72.             super(new JPasswordField());
  73.         }
  74.     }
  75.  
  76.     private static class PasswordCellRenderer extends DefaultTableCellRenderer {
  77.         private static final long serialVersionUID = -8149913137634230574L;
  78.  
  79.         @Override
  80.         protected void setValue(final Object value) {
  81.             if (value == null) {
  82.                 setText("");
  83.             } else {
  84.                 final String str = value.toString();
  85.                 final StringBuilder b = new StringBuilder();
  86.                 for (int i = 0; i < str.length(); ++i) {
  87.                     b.append("\u25CF");
  88.                 }
  89.                 setText(b.toString());
  90.             }
  91.         }
  92.     }
  93.  
  94.     private class TableSelectionListener implements ListSelectionListener {
  95.         public void valueChanged(final ListSelectionEvent evt) {
  96.             final int row = table.getSelectedRow();
  97.             if (!evt.getValueIsAdjusting()) {
  98.                 removeButton.setEnabled(row >= 0 && row < table.getRowCount());
  99.             }
  100.         }
  101.     }
  102.  
  103.     private class AccountTableModel extends AbstractTableModel {
  104.         private static final long serialVersionUID = 3233063410900758383L;
  105.  
  106.         public int getRowCount() {
  107.             return accountStore.list().size();
  108.         }
  109.  
  110.         public int getColumnCount() {
  111.             return VALID_KEYS.length + 2;
  112.         }
  113.  
  114.         public Object getValueAt(final int row, final int column) {
  115.             if (column == 0) {
  116.                 return userForRow(row);
  117.             } else if (column == 1) {
  118.                 return accountStore.get(userForRow(row)).getPassword();
  119.             } else {
  120.                 final AccountStore.Account acc = accountStore.get(userForRow(row));
  121.                 if (acc != null) {
  122.                     final String str = acc.getAttribute(VALID_KEYS[column - 2]);
  123.                     if (str == null || str.isEmpty()) {
  124.                         return null;
  125.                     }
  126.                     if (getColumnClass(column) == Boolean.class) {
  127.                         return Boolean.parseBoolean(str);
  128.                     } else if (getColumnClass(column) == Integer.class) {
  129.                         return Integer.parseInt(str);
  130.                     } else {
  131.                         return str;
  132.                     }
  133.                 }
  134.             }
  135.             return null;
  136.         }
  137.  
  138.         @Override
  139.         public String getColumnName(final int column) {
  140.             if (column == 0) {
  141.                 return "Username";
  142.             } else if (column == 1) {
  143.                 return "Password";
  144.             }
  145.             final String str = VALID_KEYS[column - 2];
  146.             final StringBuilder b = new StringBuilder();
  147.             boolean space = true;
  148.             for (char c : str.toCharArray()) {
  149.                 if (c == '_') {
  150.                     c = ' ';
  151.                 }
  152.                 b.append(space ? Character.toUpperCase(c) : c);
  153.                 space = c == ' ';
  154.             }
  155.             return b.toString();
  156.         }
  157.  
  158.         @Override
  159.         public Class<?> getColumnClass(final int column) {
  160.             if (getColumnName(column).equals("Member")) {
  161.                 return Boolean.class;
  162.             }
  163.             if (getColumnName(column).equals("Take Breaks")) {
  164.                 return Boolean.class;
  165.             }
  166.             return Object.class;
  167.         }
  168.  
  169.         @Override
  170.         public boolean isCellEditable(final int row, final int column) {
  171.             return column > 0;
  172.         }
  173.  
  174.         @Override
  175.         public void setValueAt(final Object value, final int row, final int column) {
  176.             final AccountStore.Account acc = accountStore.get(userForRow(row));
  177.             if (acc == null) {
  178.                 return;
  179.             }
  180.             if (column == 1) {
  181.                 acc.setPassword(String.valueOf(value));
  182.             } else {
  183.                 acc.setAttribute(getColumnName(column).toLowerCase().replace(' ', '_'), String.valueOf(value));
  184.             }
  185.             fireTableCellUpdated(row, column);
  186.         }
  187.  
  188.         public String userForRow(final int row) {
  189.             final Iterator<AccountStore.Account> it = accountStore.list().iterator();
  190.             for (int k = 0; it.hasNext() && k < row; k++) {
  191.                 it.next();
  192.             }
  193.             if (it.hasNext()) {
  194.                 return it.next().getUsername();
  195.             }
  196.             return null;
  197.         }
  198.     }
  199.  
  200.     private JTable table;
  201.     private JButton removeButton;
  202.  
  203.     private AccountManager() {
  204.         super(Frame.getFrames()[0], "Accounts", true);
  205.         setIconImage(Configuration.getImage(Configuration.Paths.Resources.ICON_REPORTKEY));
  206.     }
  207.  
  208.     public void actionPerformed(final ActionEvent e) {
  209.         if (e.getSource() instanceof JButton) {
  210.             final JButton button = (JButton) e.getSource();
  211.             if (button.getText().equals("Save")) {
  212.                 try {
  213.                     accountStore.save();
  214.                 } catch (final IOException ioe) {
  215.                     log.info("Could not save accounts: " + ioe.getMessage());
  216.                 }
  217.                 dispose();
  218.             } else if (button.getToolTipText().equals("Add")) {
  219.                 final String str = JOptionPane.showInputDialog(getParent(), "Enter the account username:", "New Account", JOptionPane.QUESTION_MESSAGE);
  220.                 if (str == null || str.isEmpty()) {
  221.                     return;
  222.                 }
  223.                 final AccountStore.Account account = new AccountStore.Account(str);
  224.                 accountStore.add(account);
  225.                 accountStore.get(account.getUsername()).setAttribute("reward", RANDOM_REWARDS[0]);
  226.                 final int row = table.getRowCount();
  227.                 ((AccountTableModel) table.getModel()).fireTableRowsInserted(row, row);
  228.             } else if (button.getToolTipText().equals("Remove")) {
  229.                 final int row = table.getSelectedRow();
  230.                 final String user = ((AccountTableModel) table.getModel()).userForRow(row);
  231.                 if (user != null) {
  232.                     accountStore.remove(user);
  233.                     ((AccountTableModel) table.getModel()).fireTableRowsDeleted(row, row);
  234.                 }
  235.             }
  236.         }
  237.     }
  238.  
  239.     /**
  240.      * Creates and displays the main GUI This GUI has the list and the main  * buttons
  241.      */
  242.     public void showGUI() {
  243.         final JScrollPane scrollPane = new JScrollPane();
  244.         table = new JTable(new AccountTableModel());
  245.         final JToolBar bar = new JToolBar();
  246.         bar.setMargin(new Insets(1, 1, 1, 1));
  247.         bar.setFloatable(false);
  248.         removeButton = new JButton("Remove", new ImageIcon(Configuration.getImage(Configuration.Paths.Resources.ICON_CLOSE)));
  249.         final JButton newButton = new JButton("Add", new ImageIcon(Configuration.getImage(Configuration.Paths.Resources.ICON_ADD)));
  250.         final JButton doneButton = new JButton("Save", new ImageIcon(Configuration.getImage(Configuration.Paths.Resources.ICON_REPORT_DISK)));
  251.         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  252.         table.getSelectionModel().addListSelectionListener(new TableSelectionListener());
  253.         table.setShowGrid(false);
  254.         final TableColumnModel cm = table.getColumnModel();
  255.         cm.getColumn(cm.getColumnIndex("Password")).setCellRenderer(new PasswordCellRenderer());
  256.         cm.getColumn(cm.getColumnIndex("Password")).setCellEditor(new PasswordCellEditor());
  257.         cm.getColumn(cm.getColumnIndex("Pin")).setCellRenderer(new PasswordCellRenderer());
  258.         cm.getColumn(cm.getColumnIndex("Pin")).setCellEditor(new PasswordCellEditor());
  259.         cm.getColumn(cm.getColumnIndex("Reward")).setCellEditor(new RandomRewardEditor());
  260.         scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  261.         scrollPane.setViewportView(table);
  262.         add(scrollPane, BorderLayout.CENTER);
  263.         newButton.setFocusable(false);
  264.         newButton.setToolTipText(newButton.getText());
  265.         newButton.setText("");
  266.         bar.add(newButton);
  267.         removeButton.setFocusable(false);
  268.         removeButton.setToolTipText(removeButton.getText());
  269.         removeButton.setText("");
  270.         bar.add(removeButton);
  271.         bar.add(Box.createHorizontalGlue());
  272.         doneButton.setToolTipText(doneButton.getText());
  273.         bar.add(doneButton);
  274.         newButton.addActionListener(this);
  275.         removeButton.addActionListener(this);
  276.         doneButton.addActionListener(this);
  277.         add(bar, BorderLayout.SOUTH);
  278.         final int row = table.getSelectedRow();
  279.         removeButton.setEnabled(row >= 0 && row < table.getRowCount());
  280.         table.clearSelection();
  281.         doneButton.requestFocus();
  282.         setPreferredSize(new Dimension(600, 300));
  283.         pack();
  284.         setLocationRelativeTo(getOwner());
  285.         setResizable(false);
  286.         setVisible(true);
  287.     }
  288.  
  289.     /**
  290.      * Access the list of names for loaded accounts
  291.      *
  292.      * @return Array of the names.
  293.      */
  294.     public static String[] getAccountNames() {
  295.         assertLoginBot();
  296.         final List<String> list = new ArrayList<String>();
  297.         for (final Account account : accountStore.list()) {
  298.             list.add(account.getUsername());
  299.         }
  300.         final String[] names = new String[list.size()];
  301.         list.toArray(names);
  302.         return names;
  303.     }
  304.  
  305.     public static AccountManager getInstance() {
  306.         return new AccountManager();
  307.     }
  308.  
  309.     /**
  310.      * Access the account password of the given string
  311.      *
  312.      * @param name The name of the account
  313.      * @return Password or an empty string
  314.      */
  315.     public static String getPassword(final String name) {
  316.         assertLoginBot();
  317.         final AccountStore.Account values = AccountManager.accountStore.get(name);
  318.         String pass = values.getPassword();
  319.         if (pass == null) {
  320.             pass = "";
  321.         }
  322.         return pass;
  323.     }
  324.  
  325.     /**
  326.      * Access the account pin of the given string
  327.      *
  328.      * @param name The name of the account
  329.      * @return Pin or an empty string
  330.      */
  331.     public static String getPin(final String name) {
  332.         final AccountStore.Account values = AccountManager.accountStore.get(name);
  333.         String pin = values.getAttribute("pin");
  334.         if (pin == null) {
  335.             pin = "-1";
  336.         }
  337.         return pin;
  338.     }
  339.  
  340.     /**
  341.      * Access the account desired reward of the given string
  342.      *
  343.      * @param name The name of the account
  344.      * @return The desired reward
  345.      */
  346.     public static String getReward(final String name) {
  347.         final AccountStore.Account values = AccountManager.accountStore.get(name);
  348.         final String reward = values.getAttribute("reward");
  349.         if (reward == null) {
  350.             return "Cash";
  351.         }
  352.         return reward;
  353.     }
  354.  
  355.     /**
  356.      * Access the account state of the given string
  357.      *
  358.      * @param name Name of the account
  359.      * @return true if the account is member, false if it isn't
  360.      */
  361.     public static boolean isMember(final String name) {
  362.         final AccountStore.Account values = AccountManager.accountStore.get(name);
  363.         final String member = values.getAttribute("member");
  364.         return member != null && member.equalsIgnoreCase("true");
  365.     }
  366.  
  367.     /**
  368.      * Access the account state of the given string
  369.      *
  370.      * @param name Name of the account
  371.      * @return true if the account is member, false if it isn't
  372.      */
  373.     public static boolean isTakingBreaks(final String name) {
  374.         final AccountStore.Account values = AccountManager.accountStore.get(name);
  375.         final String member = values.getAttribute("take_breaks");
  376.         return member != null && member.equalsIgnoreCase("true");
  377.     }
  378.  
  379.     /**
  380.      * Check if the string is a valid key
  381.      *
  382.      * @param key The key
  383.      * @return true if the object is supported, false if it isn't
  384.      */
  385.     @SuppressWarnings("unused")
  386.     private static boolean isValidKey(final String key) {
  387.         for (final String check : VALID_KEYS) {
  388.             if (key.equalsIgnoreCase(check)) {
  389.                 return true;
  390.             }
  391.         }
  392.         return false;
  393.     }
  394.  
  395.     /**
  396.      * Checks if the given string is a valid pin
  397.      *
  398.      * @param pin The pin
  399.      * @return true if the pin is valid, false if it isn't
  400.      */
  401.     @SuppressWarnings("unused")
  402.     private static boolean isValidPin(final String pin) {
  403.         if (pin.length() == 4) {
  404.             for (int i = 0; i < pin.length(); i++) {
  405.                 final char charAt = pin.charAt(i);
  406.                 if (charAt < '0' || charAt > '9') {
  407.                     return false;
  408.                 }
  409.             }
  410.             return true;
  411.         }
  412.         return false;
  413.     }
  414.  
  415.     public static void assertLoginBot() {
  416.         final StackTraceElement[] s = Thread.currentThread().getStackTrace();
  417.         if (s.length < 4 ||
  418.                 !s[0].getClassName().equals(Thread.class.getName()) ||
  419.                 !s[1].getClassName().equals(AccountManager.class.getName()) ||
  420.                 !(s[2].getClassName().equals(AccountManager.class.getName())) ||
  421.                 !(
  422.                         s[3].getClassName().equals(Chrome.class.getName()) ||
  423.                                 s[3].getClassName().equals(ScriptSelector.class.getName()) ||
  424.                                 s[3].getClassName().equals(AccountManager.class.getName()) ||
  425.                                 s[3].getClassName().equals(Bot.class.getName()) ||
  426.                                 s[3].getClassName().equals(ImprovedLoginBot.class.getName()))
  427.                 ) {
  428.             throw new SecurityException();
  429.         }
  430.     }
  431. }
Add Comment
Please, Sign In to add comment