Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package donutretrieve;
- import com.beust.jcommander.JCommander;
- import com.beust.jcommander.Parameter;
- import com.google.common.collect.Maps;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- import com.google.gson.JsonPrimitive;
- import org.apache.commons.io.FileUtils;
- import java.io.File;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.sql.*;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import java.util.Date;
- public class HumAid {
- Connection mailDB;
- @Parameter(names = {"--db_address", "-dba"})
- String dbAddress = "127.0.0.1:3306";
- @Parameter(names = {"--mail_db", "-mdb"})
- String mailDb = "mc_mail_oft";
- @Parameter(names = {"--mail_db_user", "-mdbu"})
- String mailDbUser = "mc_mail_oft";
- @Parameter(names = {"--mail_db_password", "-mdbp"}, required = true)
- String mailDbPassword;
- @Parameter(names = {"-topic"})
- String topic = "Гум. помощь";
- @Parameter(names = {"-msg"})
- String message = "";
- @Parameter(names = {"-sum"})
- int sum = 10000;
- List<String> players;
- public static void main(String[] args) throws Exception {
- HumAid donutRetrieve = new HumAid();
- donutRetrieve.players = FileUtils.readLines(new File("whitelist.txt"));
- new JCommander(donutRetrieve, args);
- donutRetrieve.run();
- }
- void run() throws Exception {
- mailDB = DriverManager.getConnection(String.format("jdbc:mysql://%s/%s?useUnicode=true&characterEncoding=utf-8&user=%s&password=%s",
- dbAddress, mailDb, mailDbUser, mailDbPassword));
- int counter = 0;
- for (String username : players) {
- JsonObject attachment = new JsonObject();
- attachment.add("money", new JsonPrimitive(sum));
- attachment.add("itemCount", new JsonPrimitive(0));
- sendMessageFromSystem(message, topic, "common", username, escapeString(attachment.toString()));
- System.out.println(username + " " + attachment.toString());
- counter++;
- }
- System.out.println(counter);
- /*sendMessageFromSystem("Я сделяль", "Возврат доната", "common", "Folken",
- "{\"money\":0,\"itemCount\":1,\"item_0\":{\"id\":12510,\"stackSize\":1,\"itemDamage\":0,\"tag\":{\"owner\":{\"type\": \"string\", \"value\": \"Folken\"}}}}");*/
- mailDB.close();
- }
- /**
- * returns primary key for last pasted row
- */
- public int executeUpdate(String update) {
- try {
- Statement statement = mailDB.createStatement();
- statement.executeUpdate(update, Statement.RETURN_GENERATED_KEYS);
- int generatedKey = -1;
- final ResultSet keyset = statement.getGeneratedKeys();
- if (keyset.next()) {
- generatedKey = keyset.getInt(1);
- }
- statement.close();
- return generatedKey;
- } catch (Exception e) {
- e.printStackTrace();
- return -1;
- }
- }
- private static String postMessage = "INSERT INTO messages(message, sender, receiver, topic, date, category, attachment) VALUES('%s', '%d', '%d', '%s', '%s', '%s', '%s');";
- //posts message into 2 mailboxes - one for the sender and another for the receiver
- private static String postMessageIntoMailbox = "INSERT INTO mailbox(userid, msgid, folder) VALUES('%d', '%d', '%s'), ('%d', '%d', '%s');";
- public static final DateFormat DEFAULT_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- public void sendMessageFromSystem(final String message, final String topic, final String category, final String target, final String attachment) {
- final int userId = 52; //system userid
- final int targetId = getUserId(target);
- String update = String.format(postMessage, message, userId, targetId, topic, DEFAULT_FORMAT.format(new Date()), category, attachment);
- int msgId = executeUpdate(update);
- update = String.format(postMessageIntoMailbox, userId, msgId, "sent", targetId, msgId, "received");
- executeUpdate(update);
- System.out.println("Sending to " + target);
- }
- protected static String idQuery = "SELECT id FROM users WHERE username = '%s';";
- public int getUserId(String username) {
- int userId = -1;
- String query = String.format(idQuery, escapeString(username));
- try {
- userId = getIntParam("id", query);
- } catch (Exception e) {
- return registerUser(escapeString(username));
- }
- return userId;
- }
- public int getIntParam(String key, String query) {
- List<Map<String, Integer>> list = getIntParams(query);
- if (list != null)
- return getIntParams(query).get(0).get(key);
- return -1;
- }
- public List<Map<String, Integer>> getIntParams(String query) {
- List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
- try {
- Pair<ResultSet, Statement> queryData = executeQuery(query);
- ResultSet set = queryData.first();
- while (set.next()) {
- Map<String, Integer> data = Maps.newHashMap();
- int k = set.getMetaData().getColumnCount();
- for (int i = 1; i < k + 1; i++)
- data.put(set.getMetaData().getColumnLabel(i), set.getInt(i));
- list.add(data);
- }
- queryData.second().close();
- queryData.first().close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return list;
- }
- protected static String registerUser = "INSERT INTO users(username) VALUES('%s');";
- /* The user may be registered by anyone, though this won't result in anything bad,
- * as mysql just links the id to usernames (thanks, normal forms) */
- public int registerUser(String username) {
- return executeUpdate(String.format(registerUser, escapeString(username)));
- }
- public static String escapeString(String str) {
- if (str == null)
- return null;
- if (str.replaceAll("[a-zA-Z0-9_!@#$%^&*()-=+~.;:,\\Q[\\E\\Q]\\E<>{}\\/? ]", "").length() < 1)
- return str;
- String clean_string = str;
- clean_string = clean_string.replaceAll("\\\\", "\\\\\\\\");
- clean_string = clean_string.replaceAll("\\n", "\\\\n");
- clean_string = clean_string.replaceAll("\\r", "\\\\r");
- clean_string = clean_string.replaceAll("\\t", "\\\\t");
- clean_string = clean_string.replaceAll("\\00", "\\\\0");
- clean_string = clean_string.replaceAll("'", "\\\\'");
- clean_string = clean_string.replaceAll("\\\"", "\\\\\"");
- return clean_string;
- }
- public Pair<ResultSet, Statement> executeQuery(String query) {
- try {
- Statement statement = mailDB.createStatement();
- ResultSet resultSet = statement.executeQuery(query);
- return new Pair<ResultSet, Statement>(resultSet, statement);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- class Pair<K, V> {
- public K first;
- public V second;
- public Pair(K first, V second) {
- this.first = first;
- this.second = second;
- }
- public K first() {
- return first;
- }
- public V second() {
- return second;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement