All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.kaisle.util.ConfigWriter Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
package com.github.kaisle.util;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

/**
 * Created by Anders Borum on 03-06-2015.
 */
public class ConfigWriter {
    final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ConfigWriter.class);

    /**
     * Have the Steam Guard Code sent to your mail.
     * @param accountName
     * @param password
     */
    public static void sendGuardCode(String accountName, String password) {
        log.info("Setting credentials.");
        setCredentials(accountName, password, "");
    }

    /**
     * Set the user credentials for the Steam bot.
     * @param accountName The account name.
     * @param password The password.
     * @param steamGuardCode The Steam Guard Code.
     */
    public static void setCredentials(String accountName, String password, String steamGuardCode) {

        String  workingDirPath = FileUtil.getWorkingDirectory();
        String configFilePath = "config.js";

        // Retrieve file
        log.info("Retrieving configuration file, 'config.js'.");
        FileUtil.ensureFileExists(workingDirPath, configFilePath);
        File config = new File(System.getProperty("user.dir") + "/config.js");

        // Write account name
        log.info("Writing account name.");
        setAccountName(config, accountName);

        // Write password
        log.info("Writing password.");
        setPassword(config, password);

        // Write steam guard code
        log.info("Writing steam guard code.");
        setSteamGuardCode(config, steamGuardCode);

    }


    public static void setAccountName(File config, String accountName) {
        String accountNamePrefix = NodeDotaDefines.config_account_name;
        int configAccountNameLineNumber = FileUtil.findLine(config, accountNamePrefix);
        if (configAccountNameLineNumber < 0) {
            log.info("'config.steam.user' not present in config.js, writing to file.");
            FileUtil.appendLine(config, accountNamePrefix + " = '" + accountName + "';");
        }
        else {
            String accountNameLine = accountNamePrefix + " = '" + accountName + "';";
            log.info("Writing account name to config.js.");
            FileUtil.modifyLine(config, accountNameLine, configAccountNameLineNumber);
        }
    }

    public static void setPassword(File config, String password) {
        String passwordPrefix = NodeDotaDefines.config_pass;
        int configPasswordLineNumber = FileUtil.findLine(config, passwordPrefix);
        if (configPasswordLineNumber < 0) {
            log.info("'config.steam.pass' not present in config.js, writing to file.");
            FileUtil.appendLine(config, passwordPrefix + " = '" + password + "';");
        }
        else {
            String passwordLine = passwordPrefix + " = '" + password + "';";
            log.info("Writing password to file.");
            FileUtil.modifyLine(config, passwordLine, configPasswordLineNumber);
        }
    }

    public static void setSteamGuardCode(File config, String steamGuardCode) {
        String steamGuardPrefix = NodeDotaDefines.config_steam_guard_code;
        int configSteamGuardCodeLineNumber = FileUtil.findLine(config, steamGuardPrefix);
        if (configSteamGuardCodeLineNumber < 0) {
            log.info("'config.steam_guard_code' not present in config.js, writing to file.");
            FileUtil.appendLine(config, steamGuardPrefix + " = '" +
                    steamGuardCode + "';");
        }
        else {
            String steamGuardCodeLine = steamGuardPrefix + " = '" + steamGuardCode + "';";
            log.info("Writing steam guard code to file.");
            FileUtil.modifyLine(config, steamGuardCodeLine, configSteamGuardCodeLineNumber);
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy