Advertisement
Guest User

Untitled

a guest
Nov 10th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.58 KB | None | 0 0
  1. package donutretrieve;
  2.  
  3. import com.beust.jcommander.JCommander;
  4. import com.beust.jcommander.Parameter;
  5. import com.google.common.collect.Maps;
  6. import com.google.gson.JsonElement;
  7. import com.google.gson.JsonObject;
  8. import com.google.gson.JsonParser;
  9. import com.google.gson.JsonPrimitive;
  10. import org.apache.commons.io.FileUtils;
  11.  
  12. import java.io.File;
  13. import java.nio.file.Files;
  14. import java.nio.file.Paths;
  15. import java.sql.*;
  16. import java.text.DateFormat;
  17. import java.text.SimpleDateFormat;
  18. import java.util.*;
  19. import java.util.Date;
  20.  
  21. public class HumAid {
  22.  
  23.     Connection mailDB;
  24.  
  25.     @Parameter(names = {"--db_address", "-dba"})
  26.     String dbAddress = "127.0.0.1:3306";
  27.  
  28.     @Parameter(names = {"--mail_db", "-mdb"})
  29.     String mailDb = "mc_mail_oft";
  30.  
  31.     @Parameter(names = {"--mail_db_user", "-mdbu"})
  32.     String mailDbUser = "mc_mail_oft";
  33.  
  34.     @Parameter(names = {"--mail_db_password", "-mdbp"}, required = true)
  35.     String mailDbPassword;
  36.  
  37.     @Parameter(names = {"-topic"})
  38.     String topic = "Гум. помощь";
  39.  
  40.     @Parameter(names = {"-msg"})
  41.     String message = "";
  42.  
  43.     @Parameter(names = {"-sum"})
  44.     int sum = 10000;
  45.  
  46.     List<String> players;
  47.  
  48.     public static void main(String[] args) throws Exception {
  49.         HumAid donutRetrieve = new HumAid();
  50.         donutRetrieve.players = FileUtils.readLines(new File("whitelist.txt"));
  51.         new JCommander(donutRetrieve, args);
  52.         donutRetrieve.run();
  53.     }
  54.  
  55.     void run() throws Exception {
  56.         mailDB = DriverManager.getConnection(String.format("jdbc:mysql://%s/%s?useUnicode=true&characterEncoding=utf-8&user=%s&password=%s",
  57.                 dbAddress, mailDb, mailDbUser, mailDbPassword));
  58.  
  59.         int counter = 0;
  60.         for (String username : players) {
  61.             JsonObject attachment = new JsonObject();
  62.             attachment.add("money", new JsonPrimitive(sum));
  63.             attachment.add("itemCount", new JsonPrimitive(0));
  64.             sendMessageFromSystem(message, topic, "common", username, escapeString(attachment.toString()));
  65.             System.out.println(username + " " + attachment.toString());
  66.             counter++;
  67.         }
  68.  
  69.         System.out.println(counter);
  70.         /*sendMessageFromSystem("Я сделяль", "Возврат доната", "common", "Folken",
  71.                 "{\"money\":0,\"itemCount\":1,\"item_0\":{\"id\":12510,\"stackSize\":1,\"itemDamage\":0,\"tag\":{\"owner\":{\"type\": \"string\", \"value\": \"Folken\"}}}}");*/
  72.         mailDB.close();
  73.     }
  74.  
  75.     /**
  76.      * returns primary key for last pasted row
  77.      */
  78.     public int executeUpdate(String update) {
  79.         try {
  80.             Statement statement = mailDB.createStatement();
  81.             statement.executeUpdate(update, Statement.RETURN_GENERATED_KEYS);
  82.             int generatedKey = -1;
  83.             final ResultSet keyset = statement.getGeneratedKeys();
  84.             if (keyset.next()) {
  85.                 generatedKey = keyset.getInt(1);
  86.             }
  87.             statement.close();
  88.             return generatedKey;
  89.         } catch (Exception e) {
  90.             e.printStackTrace();
  91.             return -1;
  92.         }
  93.     }
  94.  
  95.     private static String postMessage = "INSERT INTO messages(message, sender, receiver, topic, date, category, attachment) VALUES('%s', '%d', '%d', '%s', '%s', '%s', '%s');";
  96.     //posts message into 2 mailboxes - one for the sender and another for the receiver
  97.     private static String postMessageIntoMailbox = "INSERT INTO mailbox(userid, msgid, folder) VALUES('%d', '%d', '%s'), ('%d', '%d', '%s');";
  98.  
  99.  
  100.     public static final DateFormat DEFAULT_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  101.  
  102.     public void sendMessageFromSystem(final String message, final String topic, final String category, final String target, final String attachment) {
  103.         final int userId = 52; //system userid
  104.         final int targetId = getUserId(target);
  105.         String update = String.format(postMessage, message, userId, targetId, topic, DEFAULT_FORMAT.format(new Date()), category, attachment);
  106.         int msgId = executeUpdate(update);
  107.         update = String.format(postMessageIntoMailbox, userId, msgId, "sent", targetId, msgId, "received");
  108.         executeUpdate(update);
  109.         System.out.println("Sending to " + target);
  110.     }
  111.  
  112.     protected static String idQuery = "SELECT id FROM users WHERE username = '%s';";
  113.  
  114.     public int getUserId(String username) {
  115.         int userId = -1;
  116.         String query = String.format(idQuery, escapeString(username));
  117.         try {
  118.             userId = getIntParam("id", query);
  119.         } catch (Exception e) {
  120.             return registerUser(escapeString(username));
  121.         }
  122.         return userId;
  123.     }
  124.  
  125.  
  126.     public int getIntParam(String key, String query) {
  127.         List<Map<String, Integer>> list = getIntParams(query);
  128.         if (list != null)
  129.             return getIntParams(query).get(0).get(key);
  130.         return -1;
  131.     }
  132.  
  133.  
  134.     public List<Map<String, Integer>> getIntParams(String query) {
  135.         List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
  136.         try {
  137.             Pair<ResultSet, Statement> queryData = executeQuery(query);
  138.             ResultSet set = queryData.first();
  139.             while (set.next()) {
  140.                 Map<String, Integer> data = Maps.newHashMap();
  141.                 int k = set.getMetaData().getColumnCount();
  142.                 for (int i = 1; i < k + 1; i++)
  143.                     data.put(set.getMetaData().getColumnLabel(i), set.getInt(i));
  144.                 list.add(data);
  145.             }
  146.             queryData.second().close();
  147.             queryData.first().close();
  148.         } catch (SQLException e) {
  149.             e.printStackTrace();
  150.         }
  151.         return list;
  152.     }
  153.  
  154.     protected static String registerUser = "INSERT INTO users(username) VALUES('%s');";
  155.  
  156.     /* The user may be registered by anyone, though this won't result in anything bad,
  157.      * as mysql just links the id to usernames (thanks, normal forms) */
  158.     public int registerUser(String username) {
  159.         return executeUpdate(String.format(registerUser, escapeString(username)));
  160.     }
  161.  
  162.     public static String escapeString(String str) {
  163.         if (str == null)
  164.             return null;
  165.  
  166.         if (str.replaceAll("[a-zA-Z0-9_!@#$%^&*()-=+~.;:,\\Q[\\E\\Q]\\E<>{}\\/? ]", "").length() < 1)
  167.             return str;
  168.  
  169.         String clean_string = str;
  170.         clean_string = clean_string.replaceAll("\\\\", "\\\\\\\\");
  171.         clean_string = clean_string.replaceAll("\\n", "\\\\n");
  172.         clean_string = clean_string.replaceAll("\\r", "\\\\r");
  173.         clean_string = clean_string.replaceAll("\\t", "\\\\t");
  174.         clean_string = clean_string.replaceAll("\\00", "\\\\0");
  175.         clean_string = clean_string.replaceAll("'", "\\\\'");
  176.         clean_string = clean_string.replaceAll("\\\"", "\\\\\"");
  177.  
  178.         return clean_string;
  179.     }
  180.  
  181.  
  182.     public Pair<ResultSet, Statement> executeQuery(String query) {
  183.         try {
  184.             Statement statement = mailDB.createStatement();
  185.             ResultSet resultSet = statement.executeQuery(query);
  186.             return new Pair<ResultSet, Statement>(resultSet, statement);
  187.         } catch (Exception e) {
  188.             e.printStackTrace();
  189.             return null;
  190.         }
  191.     }
  192.  
  193.  
  194.     class Pair<K, V> {
  195.         public K first;
  196.         public V second;
  197.  
  198.         public Pair(K first, V second) {
  199.             this.first = first;
  200.             this.second = second;
  201.         }
  202.  
  203.         public K first() {
  204.             return first;
  205.         }
  206.  
  207.         public V second() {
  208.             return second;
  209.         }
  210.     }
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement