General fixes. Works.
Formatted source files. Fixed command-line arguments processing. Added ability to specify logging levels by name (ALL, FINE, FINER...) Generally fixed unnoticed problems in Configuration and Priestess classes. Stopped spuriously committing configuration file path to preferences store, changed some Priestess methods to use her own tokenizer, and fixed a null check in setCultableChannels method.
This commit is contained in:
parent
7d97078e43
commit
f2eec77df0
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@
|
||||
/priestessconfig.txt
|
||||
/priestessconfig.txtbkup
|
||||
/target/
|
||||
dependency-reduced-pom.xml
|
||||
|
@ -22,105 +22,106 @@ import reactor.core.publisher.Mono;
|
||||
*/
|
||||
public class Command {
|
||||
|
||||
public static final HashMap<String, Command> commands = new HashMap<>();
|
||||
public static final HashMap<String, Command> commands = new HashMap<>();
|
||||
|
||||
private static final Logger log = Logger.getLogger(Priestess.class.getName());
|
||||
private static final Logger log = Logger.getLogger(Priestess.class.getName());
|
||||
|
||||
public static void replyTo(Message m, String s){
|
||||
if(m != null){
|
||||
m.getChannel().block().createMessage(s).withMessageReference(m.getId()).block();
|
||||
} else {
|
||||
log.log(Level.INFO, s);
|
||||
}
|
||||
}
|
||||
public static void replyTo(Message m, String s) {
|
||||
if (m != null) {
|
||||
m.getChannel().block().createMessage(s).withMessageReference(m.getId()).block();
|
||||
} else {
|
||||
log.log(Level.INFO, s);
|
||||
}
|
||||
}
|
||||
|
||||
static void tryAll(String commandline, Message m, int permissionLevel) {
|
||||
for(String s: commands.keySet()){
|
||||
if(commandline.startsWith(s)){
|
||||
log.log(Level.INFO, "Matched command " + s);
|
||||
switch(commands.get(s).tryExecuteCommand(commandline, permissionLevel, m)){
|
||||
case SUCCESS:
|
||||
return;
|
||||
case BADPERM:
|
||||
replyTo(m, "Insufficient permissions for command.");
|
||||
return;
|
||||
case FAILURE:
|
||||
replyTo(m, String.format("Command %s failed for some reason idfk", s));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
replyTo(m, "Unknown command");
|
||||
}
|
||||
static void tryAll(String commandline, Message m, int permissionLevel) {
|
||||
for (String s : commands.keySet()) {
|
||||
if (commandline.startsWith(s)) {
|
||||
log.log(Level.INFO, "Matched command " + s);
|
||||
switch (commands.get(s).tryExecuteCommand(commandline, permissionLevel, m)) {
|
||||
case SUCCESS:
|
||||
return;
|
||||
case BADPERM:
|
||||
replyTo(m, "Insufficient permissions for command.");
|
||||
return;
|
||||
case FAILURE:
|
||||
replyTo(m, String.format("Command %s failed for some reason idfk", s));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
replyTo(m, "Unknown command");
|
||||
}
|
||||
|
||||
private final String name;
|
||||
private final int namelen;
|
||||
private final int permissionLevel;
|
||||
private Callback callback;
|
||||
private final String name;
|
||||
private final int namelen;
|
||||
private final int permissionLevel;
|
||||
private Callback callback;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param commandline
|
||||
* @param permissionLevel
|
||||
* @param a
|
||||
* @return
|
||||
*/
|
||||
public CommandStatus tryExecuteCommand(String commandline, int permissionLevel, Message m){
|
||||
if(!commandline.startsWith(this.name)){
|
||||
return NOMATCH;
|
||||
}
|
||||
if(permissionLevel < this.permissionLevel){
|
||||
return BADPERM;
|
||||
}
|
||||
String payload = commandline.substring(this.namelen).trim();
|
||||
this.callback.apply(new CommandArgs(m, payload, permissionLevel));
|
||||
return SUCCESS;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param commandline
|
||||
* @param permissionLevel
|
||||
* @param a
|
||||
* @return
|
||||
*/
|
||||
public CommandStatus tryExecuteCommand(String commandline, int permissionLevel, Message m) {
|
||||
if (!commandline.startsWith(this.name)) {
|
||||
return NOMATCH;
|
||||
}
|
||||
if (permissionLevel < this.permissionLevel) {
|
||||
return BADPERM;
|
||||
}
|
||||
String payload = commandline.substring(this.namelen).trim();
|
||||
this.callback.apply(new CommandArgs(m, payload, permissionLevel));
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
public Command setCallback(Callback c){
|
||||
this.callback = c;
|
||||
return this;
|
||||
}
|
||||
public Command setCallback(Callback c) {
|
||||
this.callback = c;
|
||||
return this;
|
||||
}
|
||||
|
||||
Command(String name, int permissionLevel){
|
||||
this.name = name;
|
||||
this.namelen = name.length();
|
||||
this.permissionLevel = permissionLevel;
|
||||
this.callback = new Callback(){
|
||||
@Override
|
||||
public Publisher<Mono> apply(CommandArgs t) {
|
||||
t.replyWith(String.format("Callback for function %s not registered", name));
|
||||
return null;
|
||||
}
|
||||
Command(String name, int permissionLevel) {
|
||||
this.name = name;
|
||||
this.namelen = name.length();
|
||||
this.permissionLevel = permissionLevel;
|
||||
this.callback = new Callback() {
|
||||
@Override
|
||||
public Publisher<Mono> apply(CommandArgs t) {
|
||||
t.replyWith(String.format("Callback for function %s not registered", name));
|
||||
return null;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void registerCommand(Command c){
|
||||
commands.put(c.name, c);
|
||||
}
|
||||
public static void registerCommand(Command c) {
|
||||
commands.put(c.name, c);
|
||||
}
|
||||
|
||||
/*= "use emoji";
|
||||
public static final String REPLYTO_COMMAND = "reply to";
|
||||
public static final String SENDTO_COMMAND = "send to";*/
|
||||
/*
|
||||
* = "use emoji"; public static final String REPLYTO_COMMAND = "reply to";
|
||||
* public static final String SENDTO_COMMAND = "send to";
|
||||
*/
|
||||
|
||||
/**
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
/**
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public static Command commandThatJustRepliesWith(String name, String reply){
|
||||
Command c = new Command(name, 1);
|
||||
c.setCallback(new Callback(){
|
||||
public static Command commandThatJustRepliesWith(String name, String reply) {
|
||||
Command c = new Command(name, 1);
|
||||
c.setCallback(new Callback() {
|
||||
|
||||
@Override
|
||||
public Publisher<Mono> apply(CommandArgs t) {
|
||||
t.replyWith(reply);
|
||||
return Mono.empty();
|
||||
}
|
||||
});
|
||||
return c;
|
||||
}
|
||||
@Override
|
||||
public Publisher<Mono> apply(CommandArgs t) {
|
||||
t.replyWith(reply);
|
||||
return Mono.empty();
|
||||
}
|
||||
});
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
@ -13,39 +13,39 @@ import discord4j.core.object.entity.channel.MessageChannel;
|
||||
*/
|
||||
public class CommandArgs {
|
||||
|
||||
private Message m;
|
||||
private String payload;
|
||||
private final int permCalledWith;
|
||||
private Message m;
|
||||
private String payload;
|
||||
private final int permCalledWith;
|
||||
|
||||
public CommandArgs(Message m, String payload, int permCalledWith) {
|
||||
this.m = m;
|
||||
this.payload = payload;
|
||||
this.permCalledWith = permCalledWith;
|
||||
}
|
||||
public CommandArgs(Message m, String payload, int permCalledWith) {
|
||||
this.m = m;
|
||||
this.payload = payload;
|
||||
this.permCalledWith = permCalledWith;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the m
|
||||
*/
|
||||
public Message getMessage() {
|
||||
return m;
|
||||
}
|
||||
/**
|
||||
* @return the m
|
||||
*/
|
||||
public Message getMessage() {
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the payload
|
||||
*/
|
||||
public String getPayload() {
|
||||
return payload;
|
||||
}
|
||||
/**
|
||||
* @return the payload
|
||||
*/
|
||||
public String getPayload() {
|
||||
return payload;
|
||||
}
|
||||
|
||||
public void replyWith(String s){
|
||||
Command.replyTo(m, s);
|
||||
}
|
||||
public void replyWith(String s) {
|
||||
Command.replyTo(m, s);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the permCalledWith
|
||||
*/
|
||||
public int getPermCalledWith() {
|
||||
return permCalledWith;
|
||||
}
|
||||
/**
|
||||
* @return the permCalledWith
|
||||
*/
|
||||
public int getPermCalledWith() {
|
||||
return permCalledWith;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,8 +9,5 @@ package com.tinyplantnews.priestess;
|
||||
* @author atomb
|
||||
*/
|
||||
public enum CommandStatus {
|
||||
NOMATCH,
|
||||
SUCCESS,
|
||||
FAILURE,
|
||||
BADPERM;
|
||||
NOMATCH, SUCCESS, FAILURE, BADPERM;
|
||||
}
|
||||
|
@ -30,171 +30,179 @@ import java.util.prefs.BackingStoreException;
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
/**
|
||||
* "Ms Merry," you may say, "You literally import the Preferences class, and yet
|
||||
* you insist on writing your own configuration store. Why?" "Iunno," I reply.
|
||||
*
|
||||
* @author twi
|
||||
* @author sandy
|
||||
*/
|
||||
public class Configuration {
|
||||
|
||||
private static String CONFIG_FILE_LOCATION = null;
|
||||
public static final String CONFIG_FILE_KEY = "PRIESTESS_CONFIG";
|
||||
public static final Properties props = System.getProperties();
|
||||
public static final Preferences prefs = Preferences.userNodeForPackage(Configuration.class);
|
||||
public static final String DEFAULT_CONFIG_FILE_NAME = "priestessconfig.txt";
|
||||
private static String CONFIG_FILE_LOCATION = null;
|
||||
public static final String CONFIG_FILE_KEY = "PRIESTESS_CONFIG";
|
||||
public static final Properties props = System.getProperties();
|
||||
public static final Preferences prefs = Preferences.userNodeForPackage(Configuration.class);
|
||||
public static final String DEFAULT_CONFIG_FILE_NAME = "priestessconfig.txt";
|
||||
|
||||
private static HashMap<String, String> configuration = new HashMap<>();
|
||||
private static HashMap<String, String> configuration = new HashMap<>();
|
||||
|
||||
private static final Logger log = Logger.getLogger(Configuration.class.getName());
|
||||
private static final Logger log = Logger.getLogger(Configuration.class.getName());
|
||||
|
||||
public static String findConfigurationFilePath() {
|
||||
if (getCONFIG_FILE_LOCATION() != null) {
|
||||
return getCONFIG_FILE_LOCATION();
|
||||
}
|
||||
setCONFIG_FILE_LOCATION(setConfigurationFilePath(), false);
|
||||
return getCONFIG_FILE_LOCATION();
|
||||
}
|
||||
public static String findConfigurationFilePath() {
|
||||
if (getCONFIG_FILE_LOCATION() != null) {
|
||||
return getCONFIG_FILE_LOCATION();
|
||||
}
|
||||
setCONFIG_FILE_LOCATION(setConfigurationFilePath(), false);
|
||||
return getCONFIG_FILE_LOCATION();
|
||||
}
|
||||
|
||||
private static String setConfigurationFilePath() {
|
||||
String s = System.getenv(CONFIG_FILE_KEY);
|
||||
if (s != null && !s.isBlank()) {
|
||||
return s;
|
||||
} else if (props.containsKey(CONFIG_FILE_KEY)) {
|
||||
return props.getProperty(CONFIG_FILE_KEY);
|
||||
}
|
||||
s = prefs.get(CONFIG_FILE_KEY, "");
|
||||
if (s != null && !s.isBlank()) {
|
||||
return s;
|
||||
}
|
||||
if (Files.exists(Paths.get(DEFAULT_CONFIG_FILE_NAME), LinkOption.NOFOLLOW_LINKS)) {
|
||||
return DEFAULT_CONFIG_FILE_NAME;
|
||||
}
|
||||
//JDialog jd = new JDialog();
|
||||
//jd.add(new JLabel("Choose where to put Priestess Working Directory"));
|
||||
//jd.setVisible(true);
|
||||
JOptionPane.showConfirmDialog(null, "Select where cult files will be stored.");
|
||||
JFileChooser jfc = new JFileChooser("Config File for Discord Cult Priestess");
|
||||
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
||||
int rv;
|
||||
boolean approved = false;
|
||||
while (!approved) {
|
||||
rv = jfc.showDialog(null, "Select");
|
||||
if (rv == JFileChooser.APPROVE_OPTION) {
|
||||
File f = jfc.getSelectedFile();
|
||||
if (f.isDirectory() && f.canRead() && f.canWrite()) {
|
||||
prefs.put(CONFIG_FILE_KEY, f.getAbsolutePath());
|
||||
try {
|
||||
prefs.flush();
|
||||
} catch (BackingStoreException e) {
|
||||
log.log(Level.WARNING, "Backing store exception while committing configuration file path to persistent storage {0}", e.getMessage());
|
||||
}
|
||||
return f.getAbsolutePath();
|
||||
}
|
||||
} else if(rv == JFileChooser.CANCEL_OPTION) {
|
||||
return "";
|
||||
}
|
||||
private static String setConfigurationFilePath() {
|
||||
String s = System.getenv(CONFIG_FILE_KEY);
|
||||
if (s != null && !s.isBlank()) {
|
||||
return s;
|
||||
} else if (props.containsKey(CONFIG_FILE_KEY)) {
|
||||
return props.getProperty(CONFIG_FILE_KEY);
|
||||
}
|
||||
s = prefs.get(CONFIG_FILE_KEY, "");
|
||||
if (s != null && !s.isBlank()) {
|
||||
return s;
|
||||
}
|
||||
if (Files.exists(Paths.get(DEFAULT_CONFIG_FILE_NAME), LinkOption.NOFOLLOW_LINKS)) {
|
||||
return DEFAULT_CONFIG_FILE_NAME;
|
||||
}
|
||||
// JDialog jd = new JDialog();
|
||||
// jd.add(new JLabel("Choose where to put Priestess Working Directory"));
|
||||
// jd.setVisible(true);
|
||||
JOptionPane.showConfirmDialog(null, "Select where cult files will be stored.");
|
||||
JFileChooser jfc = new JFileChooser("Config File for Discord Cult Priestess");
|
||||
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
||||
int rv;
|
||||
boolean approved = false;
|
||||
while (!approved) {
|
||||
rv = jfc.showDialog(null, "Select");
|
||||
if (rv == JFileChooser.APPROVE_OPTION) {
|
||||
File f = jfc.getSelectedFile();
|
||||
if (f.isDirectory() && f.canRead() && f.canWrite()) {
|
||||
return f.getAbsolutePath();
|
||||
}
|
||||
} else if (rv == JFileChooser.CANCEL_OPTION) {
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private static final String delimiter = ": ";
|
||||
private static final String delimiter = ": ";
|
||||
|
||||
public static void getConfigurationFromFile() {
|
||||
if (CONFIG_FILE_LOCATION == null) {
|
||||
setConfigurationFilePath();
|
||||
}
|
||||
try {
|
||||
if (Files.isDirectory(Paths.get(CONFIG_FILE_LOCATION), LinkOption.NOFOLLOW_LINKS)) {
|
||||
CONFIG_FILE_LOCATION = CONFIG_FILE_LOCATION + System.getProperty("file.separator") + DEFAULT_CONFIG_FILE_NAME;
|
||||
}
|
||||
BufferedReader br = new BufferedReader(new FileReader(Paths.get(CONFIG_FILE_LOCATION).toFile()));
|
||||
br.lines().forEach((s) -> {
|
||||
String key = s.substring(0, s.indexOf(delimiter));
|
||||
if (s.length() == s.indexOf(delimiter) + delimiter.length()) {
|
||||
System.out.println("read empty value for key (" + key + ") from configuration file");
|
||||
configuration.put(key, "");
|
||||
} else {
|
||||
String value = s.substring(s.indexOf(delimiter) + delimiter.length());
|
||||
configuration.put(key, value);
|
||||
}
|
||||
});
|
||||
} catch (FileNotFoundException ex) {
|
||||
public static void getConfigurationFromFile() {
|
||||
if (CONFIG_FILE_LOCATION == null) {
|
||||
setConfigurationFilePath();
|
||||
}
|
||||
try {
|
||||
if (Files.isDirectory(Paths.get(CONFIG_FILE_LOCATION), LinkOption.NOFOLLOW_LINKS)) {
|
||||
CONFIG_FILE_LOCATION = CONFIG_FILE_LOCATION + System.getProperty("file.separator")
|
||||
+ DEFAULT_CONFIG_FILE_NAME;
|
||||
}
|
||||
BufferedReader br = new BufferedReader(new FileReader(Paths.get(CONFIG_FILE_LOCATION).toFile()));
|
||||
br.lines().forEach((s) -> {
|
||||
String key = s.substring(0, s.indexOf(delimiter));
|
||||
if (s.length() == s.indexOf(delimiter) + delimiter.length()) {
|
||||
log.log(Level.FINE, "read empty value for key (" + key + ") from configuration file");
|
||||
configuration.put(key, "");
|
||||
} else {
|
||||
String value = s.substring(s.indexOf(delimiter) + delimiter.length());
|
||||
configuration.put(key, value);
|
||||
}
|
||||
});
|
||||
} catch (FileNotFoundException ex) {
|
||||
|
||||
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, "Error loading configuration file from (" + CONFIG_FILE_LOCATION + ") {0}", ex);
|
||||
}
|
||||
}
|
||||
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE,
|
||||
"Error loading configuration file from (" + CONFIG_FILE_LOCATION + ") {0}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the CONFIG_FILE_LOCATION
|
||||
*/
|
||||
public static String getCONFIG_FILE_LOCATION() {
|
||||
return CONFIG_FILE_LOCATION;
|
||||
}
|
||||
/**
|
||||
* @return the CONFIG_FILE_LOCATION
|
||||
*/
|
||||
public static String getCONFIG_FILE_LOCATION() {
|
||||
return CONFIG_FILE_LOCATION;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param aCONFIG_FILE_LOCATION the CONFIG_FILE_LOCATION to set
|
||||
*/
|
||||
public static void setCONFIG_FILE_LOCATION(String aCONFIG_FILE_LOCATION, boolean persistent) {
|
||||
CONFIG_FILE_LOCATION = aCONFIG_FILE_LOCATION;
|
||||
if (persistent) {
|
||||
props.setProperty(CONFIG_FILE_KEY, CONFIG_FILE_LOCATION);
|
||||
}
|
||||
/**
|
||||
* @param aCONFIG_FILE_LOCATION the CONFIG_FILE_LOCATION to set
|
||||
*/
|
||||
public static void setCONFIG_FILE_LOCATION(String aCONFIG_FILE_LOCATION, boolean persistent) {
|
||||
CONFIG_FILE_LOCATION = aCONFIG_FILE_LOCATION;
|
||||
if (persistent) {
|
||||
props.setProperty(CONFIG_FILE_KEY, CONFIG_FILE_LOCATION);
|
||||
prefs.put(CONFIG_FILE_KEY, CONFIG_FILE_LOCATION);
|
||||
try {
|
||||
prefs.flush();
|
||||
} catch (BackingStoreException e) {
|
||||
log.log(Level.WARNING,
|
||||
"Backing store exception while committing configuration file path to persistent storage {0}",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static void setConfigurationParameter(String key, String value) {
|
||||
configuration.put(key, value);
|
||||
}
|
||||
public static void setConfigurationParameter(String key, String value) {
|
||||
configuration.put(key, value);
|
||||
}
|
||||
|
||||
public static String getConfigurationParameter(String key) {
|
||||
if (configuration.containsKey(key)) {
|
||||
return configuration.get(key);
|
||||
} else {
|
||||
configuration.put(key, "");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
public static String getConfigurationParameter(String key) {
|
||||
if (configuration.containsKey(key)) {
|
||||
return configuration.get(key);
|
||||
} else {
|
||||
configuration.put(key, "");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static void commitConfigurationToFile() {
|
||||
public static void commitConfigurationToFile() {
|
||||
|
||||
try {
|
||||
Files.deleteIfExists(Paths.get(CONFIG_FILE_LOCATION + "bkup"));
|
||||
try {
|
||||
Files.copy(Paths.get(CONFIG_FILE_LOCATION), Paths.get(CONFIG_FILE_LOCATION + "bkup"));
|
||||
} catch (IOException ioe) {
|
||||
if(ioe instanceof NoSuchFileException) {
|
||||
log.log(Level.FINE, "No existing configuration file--not creating backup");
|
||||
} else {
|
||||
log.log(Level.WARNING, "Exception encountered while trying to create configuration file backup: {0}", ioe);
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
System.err.println("Making backup configuration file failed. Exiting without committing configuration to file");
|
||||
return;
|
||||
}
|
||||
try ( BufferedWriter br = new BufferedWriter(new FileWriter(Paths.get(CONFIG_FILE_LOCATION).toFile(), false))) {
|
||||
configuration.forEach((key, value) -> {
|
||||
try {
|
||||
br.append(key + delimiter + value + "\n");
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
});
|
||||
try {
|
||||
Files.deleteIfExists(Paths.get(CONFIG_FILE_LOCATION + "bkup"));
|
||||
try {
|
||||
Files.copy(Paths.get(CONFIG_FILE_LOCATION), Paths.get(CONFIG_FILE_LOCATION + "bkup"));
|
||||
} catch (IOException ioe) {
|
||||
if (ioe instanceof NoSuchFileException) {
|
||||
log.log(Level.FINE, "No existing configuration file--not creating backup");
|
||||
} else {
|
||||
log.log(Level.WARNING,
|
||||
"Exception encountered while trying to create configuration file backup: {0}", ioe);
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
System.err.println(
|
||||
"Making backup configuration file failed. Exiting without committing configuration to file");
|
||||
return;
|
||||
}
|
||||
try (BufferedWriter br = new BufferedWriter(new FileWriter(Paths.get(CONFIG_FILE_LOCATION).toFile(), false))) {
|
||||
configuration.forEach((key, value) -> {
|
||||
try {
|
||||
br.append(key + delimiter + value + "\n");
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
});
|
||||
|
||||
br.flush();
|
||||
br.close();
|
||||
} catch (FileNotFoundException ex) {
|
||||
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
br.flush();
|
||||
br.close();
|
||||
} catch (FileNotFoundException ex) {
|
||||
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static String enumerateConfiguration() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
configuration.forEach((key, value) -> {
|
||||
sb.append(key + delimiter + value + "\n");
|
||||
});
|
||||
return sb.toString();
|
||||
}
|
||||
public static String enumerateConfiguration() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
configuration.forEach((key, value) -> {
|
||||
sb.append(key + delimiter + value + "\n");
|
||||
});
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ import java.util.logging.Level;
|
||||
*/
|
||||
public class CustomLoggingLevel extends Level {
|
||||
|
||||
public CustomLoggingLevel(int i) {
|
||||
super("custom", i);
|
||||
}
|
||||
public CustomLoggingLevel(int i) {
|
||||
super("custom", i);
|
||||
}
|
||||
}
|
||||
|
@ -17,65 +17,60 @@ import java.util.function.BiConsumer;
|
||||
*/
|
||||
class GuildProfile {
|
||||
|
||||
private final Guild g;
|
||||
private final Guild g;
|
||||
|
||||
public GuildProfile(Guild g1) {
|
||||
g = g1;
|
||||
}
|
||||
public GuildProfile(Guild g1) {
|
||||
g = g1;
|
||||
}
|
||||
|
||||
private HashMap<String, Snowflake> customemojis = new HashMap<>();
|
||||
|
||||
private HashMap<String, Snowflake> customemojis = new HashMap<>();
|
||||
private HashMap<String, Snowflake> approvingcustomemojis = new HashMap<>();
|
||||
|
||||
private HashMap<String, Snowflake> approvingcustomemojis = new HashMap<>();
|
||||
// Just so it's seekable, for the random approving emoji methods.
|
||||
private ArrayList<String> approvingcustomemojinames = new ArrayList<>();
|
||||
|
||||
//Just so it's seekable, for the random approving emoji methods.
|
||||
private ArrayList<String> approvingcustomemojinames = new ArrayList<>();
|
||||
/**
|
||||
* @return the customemojis
|
||||
*/
|
||||
public HashMap<String, Snowflake> getCustomemojis() {
|
||||
return customemojis;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the customemojis
|
||||
*/
|
||||
public HashMap<String, Snowflake> getCustomemojis() {
|
||||
return customemojis;
|
||||
}
|
||||
/**
|
||||
* @param customemojis the customemojis to set
|
||||
*/
|
||||
public void setCustomemojis(HashMap<String, Snowflake> customemojis) {
|
||||
this.customemojis = customemojis;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param customemojis the customemojis to set
|
||||
*/
|
||||
public void setCustomemojis(HashMap<String, Snowflake> customemojis) {
|
||||
this.customemojis = customemojis;
|
||||
}
|
||||
public String toFile() {
|
||||
return "";
|
||||
}
|
||||
|
||||
public String toFile() {
|
||||
return "";
|
||||
}
|
||||
public String emojisToString() {
|
||||
StringBuilder s = new StringBuilder("");
|
||||
customemojis.forEach((String name, Snowflake snowflake) -> {
|
||||
s.append(new JKRSONTag(name, snowflake.asString()));
|
||||
});
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
public String emojisToString() {
|
||||
StringBuilder s = new StringBuilder("");
|
||||
customemojis.forEach((String name, Snowflake snowflake) -> {
|
||||
s.append(new JKRSONTag(name, snowflake.asString()));
|
||||
});
|
||||
return s.toString();
|
||||
}
|
||||
public void setApprovingEmoji(String name, Snowflake snowflake) {
|
||||
approvingcustomemojis.put(name, snowflake);
|
||||
approvingcustomemojinames.add(name);
|
||||
}
|
||||
|
||||
public void setApprovingEmoji(String name, Snowflake snowflake) {
|
||||
approvingcustomemojis.put(name, snowflake);
|
||||
approvingcustomemojinames.add(name);
|
||||
}
|
||||
public int emojiCount() {
|
||||
return customemojis.size();
|
||||
}
|
||||
|
||||
public int emojiCount() {
|
||||
return customemojis.size();
|
||||
}
|
||||
public int approvingEmojiCount() {
|
||||
return approvingcustomemojis.size();
|
||||
}
|
||||
|
||||
public int approvingEmojiCount() {
|
||||
return approvingcustomemojis.size();
|
||||
}
|
||||
|
||||
public GuildEmoji getEmojiByNumber(int i) {
|
||||
return g.getGuildEmojiById(
|
||||
approvingcustomemojis.get(approvingcustomemojinames
|
||||
.get(i)
|
||||
)
|
||||
).block();
|
||||
}
|
||||
public GuildEmoji getEmojiByNumber(int i) {
|
||||
return g.getGuildEmojiById(approvingcustomemojis.get(approvingcustomemojinames.get(i))).block();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -31,151 +31,155 @@ import javax.swing.JOptionPane;
|
||||
*/
|
||||
public class InstantiableConfiguration {
|
||||
|
||||
private static String CONFIG_FILE_LOCATION = null;
|
||||
public static final String CONFIG_FILE_KEY = "PRIESTESS_CONFIG";
|
||||
public static final Properties p = System.getProperties();
|
||||
public static final String DEFAULT_CONFIG_FILE_NAME = "priestessconfig.txt";
|
||||
private static String CONFIG_FILE_LOCATION = null;
|
||||
public static final String CONFIG_FILE_KEY = "PRIESTESS_CONFIG";
|
||||
public static final Properties p = System.getProperties();
|
||||
public static final String DEFAULT_CONFIG_FILE_NAME = "priestessconfig.txt";
|
||||
|
||||
private static HashMap<String, String> configuration = new HashMap<>();
|
||||
private static HashMap<String, String> configuration = new HashMap<>();
|
||||
|
||||
private static final Logger log = Logger.getLogger(InstantiableConfiguration.class.getName());
|
||||
private static final Logger log = Logger.getLogger(InstantiableConfiguration.class.getName());
|
||||
|
||||
public static String findConfigurationFilePath() {
|
||||
if (getCONFIG_FILE_LOCATION() != null) {
|
||||
return getCONFIG_FILE_LOCATION();
|
||||
}
|
||||
setCONFIG_FILE_LOCATION(setConfigurationFilePath(), false);
|
||||
return getCONFIG_FILE_LOCATION();
|
||||
}
|
||||
public static String findConfigurationFilePath() {
|
||||
if (getCONFIG_FILE_LOCATION() != null) {
|
||||
return getCONFIG_FILE_LOCATION();
|
||||
}
|
||||
setCONFIG_FILE_LOCATION(setConfigurationFilePath(), false);
|
||||
return getCONFIG_FILE_LOCATION();
|
||||
}
|
||||
|
||||
private static String setConfigurationFilePath() {
|
||||
String s = System.getenv(CONFIG_FILE_KEY);
|
||||
if (s != null && s.length() > 0) {
|
||||
private static String setConfigurationFilePath() {
|
||||
String s = System.getenv(CONFIG_FILE_KEY);
|
||||
if (s != null && s.length() > 0) {
|
||||
|
||||
return s;
|
||||
} else if (p.containsKey(CONFIG_FILE_KEY)) {
|
||||
return p.getProperty(CONFIG_FILE_KEY);
|
||||
}
|
||||
if (Files.exists(Paths.get(DEFAULT_CONFIG_FILE_NAME), LinkOption.NOFOLLOW_LINKS)) {
|
||||
return DEFAULT_CONFIG_FILE_NAME;
|
||||
}
|
||||
//JDialog jd = new JDialog();
|
||||
//jd.add(new JLabel("Choose where to put Priestess Working Directory"));
|
||||
//jd.setVisible(true);
|
||||
JOptionPane.showConfirmDialog(null, "Select where cult files will be stored.");
|
||||
JFileChooser jfc = new JFileChooser("Config File for Discord Cult Priestess");
|
||||
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
||||
int rv;
|
||||
boolean approved = false;
|
||||
while (!approved) {
|
||||
rv = jfc.showDialog(null, "Select");
|
||||
if (rv == JFileChooser.APPROVE_OPTION) {
|
||||
File f = jfc.getSelectedFile();
|
||||
if (f.isDirectory() && f.canRead() && f.canWrite()) {
|
||||
p.setProperty(CONFIG_FILE_KEY, f.getAbsolutePath());
|
||||
return f.getAbsolutePath();
|
||||
}
|
||||
}
|
||||
return s;
|
||||
} else if (p.containsKey(CONFIG_FILE_KEY)) {
|
||||
return p.getProperty(CONFIG_FILE_KEY);
|
||||
}
|
||||
if (Files.exists(Paths.get(DEFAULT_CONFIG_FILE_NAME), LinkOption.NOFOLLOW_LINKS)) {
|
||||
return DEFAULT_CONFIG_FILE_NAME;
|
||||
}
|
||||
// JDialog jd = new JDialog();
|
||||
// jd.add(new JLabel("Choose where to put Priestess Working Directory"));
|
||||
// jd.setVisible(true);
|
||||
JOptionPane.showConfirmDialog(null, "Select where cult files will be stored.");
|
||||
JFileChooser jfc = new JFileChooser("Config File for Discord Cult Priestess");
|
||||
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
||||
int rv;
|
||||
boolean approved = false;
|
||||
while (!approved) {
|
||||
rv = jfc.showDialog(null, "Select");
|
||||
if (rv == JFileChooser.APPROVE_OPTION) {
|
||||
File f = jfc.getSelectedFile();
|
||||
if (f.isDirectory() && f.canRead() && f.canWrite()) {
|
||||
p.setProperty(CONFIG_FILE_KEY, f.getAbsolutePath());
|
||||
return f.getAbsolutePath();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private static final String delimiter = ": ";
|
||||
private static final String delimiter = ": ";
|
||||
|
||||
public static void getConfigurationFromFile() {
|
||||
if (CONFIG_FILE_LOCATION == null) {
|
||||
setConfigurationFilePath();
|
||||
}
|
||||
try {
|
||||
if (Files.isDirectory(Paths.get(CONFIG_FILE_LOCATION), LinkOption.NOFOLLOW_LINKS)) {
|
||||
CONFIG_FILE_LOCATION = CONFIG_FILE_LOCATION + System.getProperty("file.separator") + DEFAULT_CONFIG_FILE_NAME;
|
||||
}
|
||||
BufferedReader br = new BufferedReader(new FileReader(Paths.get(CONFIG_FILE_LOCATION).toFile()));
|
||||
br.lines().forEach((s) -> {
|
||||
String key = s.substring(0, s.indexOf(delimiter));
|
||||
if (s.length() == s.indexOf(delimiter) + delimiter.length()) {
|
||||
System.out.println("read empty value for key (" + key + ") from configuration file");
|
||||
configuration.put(key, "");
|
||||
} else {
|
||||
String value = s.substring(s.indexOf(delimiter) + delimiter.length());
|
||||
configuration.put(key, value);
|
||||
}
|
||||
});
|
||||
} catch (FileNotFoundException ex) {
|
||||
public static void getConfigurationFromFile() {
|
||||
if (CONFIG_FILE_LOCATION == null) {
|
||||
setConfigurationFilePath();
|
||||
}
|
||||
try {
|
||||
if (Files.isDirectory(Paths.get(CONFIG_FILE_LOCATION), LinkOption.NOFOLLOW_LINKS)) {
|
||||
CONFIG_FILE_LOCATION = CONFIG_FILE_LOCATION + System.getProperty("file.separator")
|
||||
+ DEFAULT_CONFIG_FILE_NAME;
|
||||
}
|
||||
BufferedReader br = new BufferedReader(new FileReader(Paths.get(CONFIG_FILE_LOCATION).toFile()));
|
||||
br.lines().forEach((s) -> {
|
||||
String key = s.substring(0, s.indexOf(delimiter));
|
||||
if (s.length() == s.indexOf(delimiter) + delimiter.length()) {
|
||||
System.out.println("read empty value for key (" + key + ") from configuration file");
|
||||
configuration.put(key, "");
|
||||
} else {
|
||||
String value = s.substring(s.indexOf(delimiter) + delimiter.length());
|
||||
configuration.put(key, value);
|
||||
}
|
||||
});
|
||||
} catch (FileNotFoundException ex) {
|
||||
|
||||
Logger.getLogger(InstantiableConfiguration.class.getName()).log(Level.SEVERE, "Error loading configuration file from (" + CONFIG_FILE_LOCATION + ") {0}", ex);
|
||||
}
|
||||
}
|
||||
Logger.getLogger(InstantiableConfiguration.class.getName()).log(Level.SEVERE,
|
||||
"Error loading configuration file from (" + CONFIG_FILE_LOCATION + ") {0}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the CONFIG_FILE_LOCATION
|
||||
*/
|
||||
public static String getCONFIG_FILE_LOCATION() {
|
||||
return CONFIG_FILE_LOCATION;
|
||||
}
|
||||
/**
|
||||
* @return the CONFIG_FILE_LOCATION
|
||||
*/
|
||||
public static String getCONFIG_FILE_LOCATION() {
|
||||
return CONFIG_FILE_LOCATION;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param aCONFIG_FILE_LOCATION the CONFIG_FILE_LOCATION to set
|
||||
*/
|
||||
public static void setCONFIG_FILE_LOCATION(String aCONFIG_FILE_LOCATION, boolean persistent) {
|
||||
CONFIG_FILE_LOCATION = aCONFIG_FILE_LOCATION;
|
||||
if (persistent) {
|
||||
p.setProperty(CONFIG_FILE_KEY, CONFIG_FILE_LOCATION);
|
||||
}
|
||||
/**
|
||||
* @param aCONFIG_FILE_LOCATION the CONFIG_FILE_LOCATION to set
|
||||
*/
|
||||
public static void setCONFIG_FILE_LOCATION(String aCONFIG_FILE_LOCATION, boolean persistent) {
|
||||
CONFIG_FILE_LOCATION = aCONFIG_FILE_LOCATION;
|
||||
if (persistent) {
|
||||
p.setProperty(CONFIG_FILE_KEY, CONFIG_FILE_LOCATION);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static void setConfigurationParameter(String key, String value) {
|
||||
configuration.put(key, value);
|
||||
}
|
||||
public static void setConfigurationParameter(String key, String value) {
|
||||
configuration.put(key, value);
|
||||
}
|
||||
|
||||
public static String getConfigurationParameter(String key) {
|
||||
if (configuration.containsKey(key)) {
|
||||
return configuration.get(key);
|
||||
} else {
|
||||
configuration.put(key, "");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
public static String getConfigurationParameter(String key) {
|
||||
if (configuration.containsKey(key)) {
|
||||
return configuration.get(key);
|
||||
} else {
|
||||
configuration.put(key, "");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static void commitConfigurationToFile() {
|
||||
public static void commitConfigurationToFile() {
|
||||
|
||||
try {
|
||||
Files.deleteIfExists(Paths.get(CONFIG_FILE_LOCATION + "bkup"));
|
||||
try {
|
||||
Files.copy(Paths.get(CONFIG_FILE_LOCATION), Paths.get(CONFIG_FILE_LOCATION + "bkup"));
|
||||
} catch (IOException ioe) {
|
||||
log.log(Level.WARNING, "Exception encountered while trying to create confirguation file backup: {0}", ioe);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(InstantiableConfiguration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
System.err.println("Making backup configuration file failed. Exiting without committing configuration to file");
|
||||
return;
|
||||
}
|
||||
try ( BufferedWriter br = new BufferedWriter(new FileWriter(Paths.get(CONFIG_FILE_LOCATION).toFile(), false))) {
|
||||
configuration.forEach((key, value) -> {
|
||||
try {
|
||||
br.append(key + delimiter + value + "\n");
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(InstantiableConfiguration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
});
|
||||
try {
|
||||
Files.deleteIfExists(Paths.get(CONFIG_FILE_LOCATION + "bkup"));
|
||||
try {
|
||||
Files.copy(Paths.get(CONFIG_FILE_LOCATION), Paths.get(CONFIG_FILE_LOCATION + "bkup"));
|
||||
} catch (IOException ioe) {
|
||||
log.log(Level.WARNING, "Exception encountered while trying to create confirguation file backup: {0}",
|
||||
ioe);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(InstantiableConfiguration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
System.err.println(
|
||||
"Making backup configuration file failed. Exiting without committing configuration to file");
|
||||
return;
|
||||
}
|
||||
try (BufferedWriter br = new BufferedWriter(new FileWriter(Paths.get(CONFIG_FILE_LOCATION).toFile(), false))) {
|
||||
configuration.forEach((key, value) -> {
|
||||
try {
|
||||
br.append(key + delimiter + value + "\n");
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(InstantiableConfiguration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
});
|
||||
|
||||
br.flush();
|
||||
br.close();
|
||||
} catch (FileNotFoundException ex) {
|
||||
Logger.getLogger(InstantiableConfiguration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(InstantiableConfiguration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
br.flush();
|
||||
br.close();
|
||||
} catch (FileNotFoundException ex) {
|
||||
Logger.getLogger(InstantiableConfiguration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(InstantiableConfiguration.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static String enumerateConfiguration() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
configuration.forEach((key, value) -> {
|
||||
sb.append(key + delimiter + value + "\n");
|
||||
});
|
||||
return sb.toString();
|
||||
}
|
||||
public static String enumerateConfiguration() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
configuration.forEach((key, value) -> {
|
||||
sb.append(key + delimiter + value + "\n");
|
||||
});
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
@ -10,42 +10,43 @@ package com.tinyplantnews.priestess;
|
||||
*/
|
||||
public class JKRSONTag {
|
||||
|
||||
public JKRSONTag(String key1, String val1) {
|
||||
key = key1;
|
||||
val = val1;
|
||||
}
|
||||
public JKRSONTag(String key1, String val1) {
|
||||
key = key1;
|
||||
val = val1;
|
||||
}
|
||||
|
||||
public JKRSONTag() {
|
||||
}
|
||||
public JKRSONTag() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the key
|
||||
*/
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
/**
|
||||
* @return the key
|
||||
*/
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key the key to set
|
||||
*/
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
/**
|
||||
* @param key the key to set
|
||||
*/
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the val
|
||||
*/
|
||||
public String getVal() {
|
||||
return val;
|
||||
}
|
||||
/**
|
||||
* @return the val
|
||||
*/
|
||||
public String getVal() {
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param val the val to set
|
||||
*/
|
||||
public void setVal(String val) {
|
||||
this.val = val;
|
||||
}
|
||||
private String key;
|
||||
private String val;
|
||||
/**
|
||||
* @param val the val to set
|
||||
*/
|
||||
public void setVal(String val) {
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
private String key;
|
||||
private String val;
|
||||
|
||||
}
|
||||
|
@ -18,25 +18,25 @@ import java.util.regex.Pattern;
|
||||
*/
|
||||
public class JKRowlingSON {
|
||||
|
||||
public static final String TAG_PATTERN = "^\\{([a-z]*):([\\\"\\{a-z][a-z:\\}\\\"]*)\\}";
|
||||
public static final String TAG_PATTERN = "^\\{([a-z]*):([\\\"\\{a-z][a-z:\\}\\\"]*)\\}";
|
||||
|
||||
public static String keyVal(String key, String val) {
|
||||
return String.format("\"%s:\" \"%s\"", key, val);
|
||||
}
|
||||
public static String keyVal(String key, String val) {
|
||||
return String.format("\"%s:\" \"%s\"", key, val);
|
||||
}
|
||||
|
||||
private static Pattern tagPattern = Pattern.compile(TAG_PATTERN);
|
||||
private static Pattern tagPattern = Pattern.compile(TAG_PATTERN);
|
||||
|
||||
public static JKRSONTag decode(String tag) {
|
||||
Matcher m = tagPattern.matcher(tag);
|
||||
public static JKRSONTag decode(String tag) {
|
||||
Matcher m = tagPattern.matcher(tag);
|
||||
|
||||
JKRSONTag db = new JKRSONTag();
|
||||
if (m.matches()) {
|
||||
db.setKey(m.group(1));
|
||||
db.setVal(m.group(2));
|
||||
} else {
|
||||
Logger.getLogger(JKRowlingSON.class.toString()).log(Level.SEVERE, "Could not decode " + tag);
|
||||
}
|
||||
return db;
|
||||
}
|
||||
JKRSONTag db = new JKRSONTag();
|
||||
if (m.matches()) {
|
||||
db.setKey(m.group(1));
|
||||
db.setVal(m.group(2));
|
||||
} else {
|
||||
Logger.getLogger(JKRowlingSON.class.toString()).log(Level.SEVERE, "Could not decode " + tag);
|
||||
}
|
||||
return db;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -17,121 +17,134 @@ import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* OH IT STORES THE PROPHECIES
|
||||
*
|
||||
* The configuration file (priestessconfig.txt) contains a key, 'messages', for which the value is a path to the messages store.
|
||||
* The messages store contains prophecies that are given every few bean goose invocations.
|
||||
* This class first loads the file, scans it and generates a list of indices for the messages it contains.
|
||||
*
|
||||
* This class could definitely use a cache of the message starts--a special block at the beginning/end of the file with a list of message starts/ends,
|
||||
* but I'm not up to writing that today - 7 May 2023
|
||||
*
|
||||
* @author atomb
|
||||
*/
|
||||
public class MessageStore {
|
||||
|
||||
private RandomAccessFile f;
|
||||
private long filelength = 0;
|
||||
private FileChannel fr = null;
|
||||
private RandomAccessFile f;
|
||||
private long filelength = 0;
|
||||
private FileChannel fr = null;
|
||||
|
||||
private MessageCacher mc;
|
||||
private MessageCacher mc;
|
||||
|
||||
private int[] messagestarts = null;
|
||||
Thread t;
|
||||
private int[] messagestarts = null;
|
||||
Thread t;
|
||||
|
||||
private boolean messagesCached = false;
|
||||
private boolean messagesCached = false;
|
||||
private long loadStart = Long.MAX_VALUE;
|
||||
|
||||
public MessageStore(File f1) throws FileNotFoundException, IOException {
|
||||
f = new RandomAccessFile(f1.getPath(), "r");
|
||||
System.out.println("Message store is " + f.length() + " bytes long");
|
||||
fr = f.getChannel();
|
||||
filelength = f.length();
|
||||
mc = new MessageCacher();
|
||||
t = new Thread(mc);
|
||||
t.start();
|
||||
}
|
||||
public MessageStore(File f1) throws FileNotFoundException, IOException {
|
||||
loadStart = System.nanoTime();
|
||||
f = new RandomAccessFile(f1.getPath(), "r");
|
||||
System.out.println("Message store is " + f.length() + " bytes long");
|
||||
fr = f.getChannel();
|
||||
filelength = f.length();
|
||||
mc = new MessageCacher();
|
||||
t = new Thread(mc);
|
||||
t.start();
|
||||
}
|
||||
|
||||
public String messageAt(int i) {
|
||||
waitUntilLoaded();
|
||||
public String messageAt(int i) {
|
||||
waitUntilLoaded();
|
||||
|
||||
int start = messagestarts[i];
|
||||
StringBuilder s = new StringBuilder();
|
||||
int start = messagestarts[i];
|
||||
StringBuilder s = new StringBuilder();
|
||||
|
||||
try {
|
||||
f.seek(start);
|
||||
int a = 0;
|
||||
int j = 0;
|
||||
char c = '\0';
|
||||
try {
|
||||
f.seek(start);
|
||||
int a = 0;
|
||||
int j = 0;
|
||||
char c = '\0';
|
||||
|
||||
while ((j = f.read()) != -1 && ((char) j != '}')) {
|
||||
c = (char) j;
|
||||
s.append(c);
|
||||
a++;
|
||||
}
|
||||
while ((j = f.read()) != -1 && ((char) j != '}')) {
|
||||
c = (char) j;
|
||||
s.append(c);
|
||||
a++;
|
||||
}
|
||||
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(MessageStore.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(MessageStore.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
|
||||
while (s.charAt(0) == '{') {
|
||||
s.deleteCharAt(0);
|
||||
}
|
||||
while (s.charAt(0) == '{') {
|
||||
s.deleteCharAt(0);
|
||||
}
|
||||
|
||||
return s.toString();
|
||||
}
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
public String randomMessage() {
|
||||
return messageAt((int) (messagestarts.length * Math.random()));
|
||||
}
|
||||
public String randomMessage() {
|
||||
return messageAt((int) (messagestarts.length * Math.random()));
|
||||
}
|
||||
|
||||
boolean messagesAvailable() {
|
||||
return messagesCached;
|
||||
}
|
||||
boolean messagesAvailable() {
|
||||
return messagesCached;
|
||||
}
|
||||
|
||||
void waitUntilLoaded() {
|
||||
if (t.isAlive()) {
|
||||
try {
|
||||
t.join();
|
||||
} catch (InterruptedException ex) {
|
||||
Logger.getLogger(MessageStore.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
void waitUntilLoaded() {
|
||||
if (t.isAlive()) {
|
||||
try {
|
||||
t.join();
|
||||
} catch (InterruptedException ex) {
|
||||
Logger.getLogger(MessageStore.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int messageCount() {
|
||||
return messagestarts.length;
|
||||
}
|
||||
public int messageCount() {
|
||||
return messagestarts.length;
|
||||
}
|
||||
|
||||
private class MessageCacher implements Runnable {
|
||||
private class MessageCacher implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Logger log = Logger.getLogger(this.getClass().getName());
|
||||
log.setLevel(Level.FINE);
|
||||
log.log(Level.INFO, "Attempting to characterize message store");
|
||||
try {
|
||||
ArrayList<Integer> messagestartsal = new ArrayList<>(); //contains index of every '{'
|
||||
char c;
|
||||
int a = 0;
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
ByteBuffer b = ByteBuffer.allocate(20);
|
||||
b.rewind();
|
||||
log.log(Level.INFO, "Starting to characterize message store at position (" + fr.position()
|
||||
+ ") with channel size " + fr.size());
|
||||
while ((j = f.read()) != -1) {
|
||||
c = (char) j;
|
||||
if (c == '{') {
|
||||
messagestartsal.add(a);
|
||||
@Override
|
||||
public void run() {
|
||||
Logger log = Logger.getLogger(this.getClass().getName());
|
||||
log.setLevel(Level.FINE);
|
||||
log.log(Level.INFO, "Attempting to characterize message store");
|
||||
try {
|
||||
ArrayList<Integer> messagestartsal = new ArrayList<>(); // contains index of every '{'
|
||||
char c;
|
||||
int a = 0;
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
ByteBuffer b = ByteBuffer.allocate(20);
|
||||
b.rewind();
|
||||
log.log(Level.INFO, "Starting to characterize message store at position (" + fr.position()
|
||||
+ ") with channel size " + fr.size());
|
||||
while ((j = f.read()) != -1) {
|
||||
c = (char) j;
|
||||
if (c == '{') {
|
||||
messagestartsal.add(a);
|
||||
|
||||
}
|
||||
a++;
|
||||
}
|
||||
Integer[] messagestarts1 = messagestartsal.toArray(new Integer[0]);
|
||||
messagestarts = new int[messagestarts1.length];
|
||||
for (i = 0; i < messagestarts1.length; i++) {
|
||||
messagestarts[i] = messagestarts1[i].intValue();
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(MessageStore.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
for (int i : messagestarts) {
|
||||
System.out.println("Found invocation message at position " + i);
|
||||
}
|
||||
messagesCached = true;
|
||||
}
|
||||
}
|
||||
a++;
|
||||
}
|
||||
Integer[] messagestarts1 = messagestartsal.toArray(new Integer[0]);
|
||||
messagestarts = new int[messagestarts1.length];
|
||||
for (i = 0; i < messagestarts1.length; i++) {
|
||||
messagestarts[i] = messagestarts1[i].intValue();
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(MessageStore.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
for (int i : messagestarts) {
|
||||
System.out.println("Found invocation message at position " + i);
|
||||
}
|
||||
messagesCached = true;
|
||||
|
||||
}
|
||||
long loadEnd = System.nanoTime();
|
||||
long tdiff = loadEnd - loadStart;
|
||||
log.log(Level.FINE, "Prophecies loaded in {0} ms", tdiff);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -17,101 +17,87 @@ import org.junit.jupiter.params.ParameterizedTest;
|
||||
*/
|
||||
public class PriestessTest {
|
||||
|
||||
public PriestessTest() {
|
||||
}
|
||||
public PriestessTest() {
|
||||
}
|
||||
|
||||
@org.junit.jupiter.api.BeforeAll
|
||||
public static void setUpClass() throws Exception {
|
||||
}
|
||||
@org.junit.jupiter.api.BeforeAll
|
||||
public static void setUpClass() throws Exception {
|
||||
}
|
||||
|
||||
@org.junit.jupiter.api.AfterAll
|
||||
public static void tearDownClass() throws Exception {
|
||||
}
|
||||
@org.junit.jupiter.api.AfterAll
|
||||
public static void tearDownClass() throws Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of main method, of class Priestess.
|
||||
*/
|
||||
@org.junit.jupiter.api.Test
|
||||
public void testMain() {
|
||||
System.out.println("main");
|
||||
String[] args = {"testsuite"};
|
||||
Priestess.main(args);
|
||||
}
|
||||
/**
|
||||
* Test of main method, of class Priestess.
|
||||
*/
|
||||
@org.junit.jupiter.api.Test
|
||||
public void testMain() {
|
||||
System.out.println("main");
|
||||
String[] args = { "testsuite" };
|
||||
Priestess.main(args);
|
||||
}
|
||||
|
||||
@org.junit.jupiter.api.Test
|
||||
public void testCallbackHelpTexts() {
|
||||
assertEquals("boi", "son".replaceAll("son", "boi"));
|
||||
|
||||
@org.junit.jupiter.api.Test
|
||||
public void testCallbackHelpTexts(){
|
||||
assertEquals("boi", "son".replaceAll("son", "boi"));
|
||||
Priestess p = new Priestess();
|
||||
p.shutdownCommand.help = "ha boi";
|
||||
testCallbackHelpText(p.shutdownCommand, "shut down", "ha boi");
|
||||
|
||||
p.shutdownCommand.help = "${commandname}";
|
||||
testCallbackHelpText(p.shutdownCommand, "shut down", "shut down");
|
||||
|
||||
Priestess p = new Priestess();
|
||||
p.shutdownCommand.help = "ha boi";
|
||||
testCallbackHelpText(p.shutdownCommand,
|
||||
"shut down",
|
||||
"ha boi");
|
||||
p.shutdownCommand.help = "${commandname} turns off the entire internet";
|
||||
testCallbackHelpText(p.shutdownCommand, "shut down", "shut down turns off the entire internet");
|
||||
}
|
||||
|
||||
p.shutdownCommand.help = "${commandname}";
|
||||
testCallbackHelpText(p.shutdownCommand,
|
||||
"shut down",
|
||||
"shut down");
|
||||
private void testCallbackHelpText(Priestess.Callback c, String name, String expHelp) {
|
||||
String help = c.getHelp(name);
|
||||
Assertions.assertEquals(expHelp, help);
|
||||
}
|
||||
|
||||
p.shutdownCommand.help = "${commandname} turns off the entire internet";
|
||||
testCallbackHelpText(p.shutdownCommand,
|
||||
"shut down",
|
||||
"shut down turns off the entire internet");
|
||||
}
|
||||
/**
|
||||
* Test of tokenize method, of class Priestess.
|
||||
*/
|
||||
@Test
|
||||
public void testTokenize() {
|
||||
testTokenize1("a", new String[] { "a" });
|
||||
testTokenize1("a, b", new String[] { "a,", "b" });
|
||||
testTokenize1("ya boi", new String[] { "ya", "boi" });
|
||||
// Should handle these trivially. Single quotes not special.
|
||||
testTokenize1("'''' ''' ' '''", new String[] { "''''", "'''", "'", "'''" });
|
||||
testTokenize1("*^@^$/><::;''", new String[] { "*^@^$/><::;''" });
|
||||
testTokenize1("*^@^$/> <::;''", new String[] { "*^@^$/>", "<::;''" });
|
||||
testTokenize1("this is not quoted, \"this is\"", new String[] { "this", "is", "not", "quoted,", "this is" });
|
||||
testTokenize1("this is not quoted, \"this is", null);
|
||||
/*
|
||||
* testTokenize1("this is not quoted, \\\"this is", new String[]{"this", "is",
|
||||
* "not", "quoted,", "\"this is"});
|
||||
*/
|
||||
}
|
||||
|
||||
private void testCallbackHelpText(Priestess.Callback c, String name, String expHelp){
|
||||
String help = c.getHelp(name);
|
||||
Assertions.assertEquals(expHelp, help);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of tokenize method, of class Priestess.
|
||||
*/
|
||||
@Test
|
||||
public void testTokenize() {
|
||||
testTokenize1("a",
|
||||
new String[]{"a"});
|
||||
testTokenize1("a, b",
|
||||
new String[]{"a,", "b"});
|
||||
testTokenize1("ya boi",
|
||||
new String[]{"ya", "boi"});
|
||||
//Should handle these trivially. Single quotes not special.
|
||||
testTokenize1("'''' ''' ' '''",
|
||||
new String[]{"''''", "'''", "'", "'''"});
|
||||
testTokenize1("*^@^$/><::;''",
|
||||
new String[]{"*^@^$/><::;''"});
|
||||
testTokenize1("*^@^$/> <::;''",
|
||||
new String[]{"*^@^$/>", "<::;''"});
|
||||
testTokenize1("this is not quoted, \"this is\"",
|
||||
new String[]{"this", "is", "not", "quoted,", "this is"});
|
||||
testTokenize1("this is not quoted, \"this is",
|
||||
null);
|
||||
/*testTokenize1("this is not quoted, \\\"this is",
|
||||
new String[]{"this", "is", "not", "quoted,", "\"this is"});*/
|
||||
}
|
||||
|
||||
private void testTokenize1(String payload, String[] expResult) {
|
||||
System.out.println("tokenize for payload " + payload);
|
||||
String[] result = Priestess.tokenize(payload);
|
||||
if (expResult == null && result == null) {
|
||||
return;
|
||||
} else if (result == null && expResult != null) {
|
||||
fail("Tokenize() returned null array");
|
||||
}
|
||||
for (int i = 0; i < Math.max(expResult.length, result.length); i++) {
|
||||
String a = "<nothing>";
|
||||
String b = "<nothing>";
|
||||
if (expResult.length > i) {
|
||||
a = expResult[i];
|
||||
}
|
||||
if (result.length > i) {
|
||||
b = result[i];
|
||||
}
|
||||
System.out.println(String.format("\te: %s r: %s", a, b));
|
||||
}
|
||||
assertArrayEquals(expResult, result);
|
||||
}
|
||||
private void testTokenize1(String payload, String[] expResult) {
|
||||
System.out.println("tokenize for payload " + payload);
|
||||
String[] result = Priestess.tokenize(payload);
|
||||
if (expResult == null && result == null) {
|
||||
return;
|
||||
} else if (result == null && expResult != null) {
|
||||
fail("Tokenize() returned null array");
|
||||
}
|
||||
for (int i = 0; i < Math.max(expResult.length, result.length); i++) {
|
||||
String a = "<nothing>";
|
||||
String b = "<nothing>";
|
||||
if (expResult.length > i) {
|
||||
a = expResult[i];
|
||||
}
|
||||
if (result.length > i) {
|
||||
b = result[i];
|
||||
}
|
||||
System.out.println(String.format("\te: %s r: %s", a, b));
|
||||
}
|
||||
assertArrayEquals(expResult, result);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user