Skip to content

Commit

Permalink
bump version; actually save config; add message aliases; stop giving …
Browse files Browse the repository at this point in the history
…me permission everywhere
  • Loading branch information
SocraticPhoenix committed Nov 7, 2017
1 parent 7c2b4d5 commit 2a8ac53
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ apply plugin : 'idea'
//End Plugins

//Program Specific Variables
def programVersion = "0.0.3"
def programVersion = "0.0.4"
def programGroup = "com.gmail.socraticphoenix"
//End Variables

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Stream;

public class MessageListener implements Consumer<MessagePostedEvent> {
private static Tio tio = Tio.MAIN;
Expand All @@ -25,11 +26,11 @@ public class MessageListener implements Consumer<MessagePostedEvent> {
private Set<Long> permitted = new HashSet<>();
private Map<String, List<String>> languages = new LinkedHashMap<>();
private Map<String, String> commands = new LinkedHashMap<>();
private Map<String, String> messages = new LinkedHashMap<>();

public MessageListener(LanguageCache cache, SessionCache sessions) {
this.cache = cache;
this.sessions = sessions;
this.permitted.add(226059L);
}

@Override
Expand All @@ -53,6 +54,7 @@ public void accept(MessagePostedEvent messagePostedEvent) {
}

private int limit = 0;

private void executeCommand(User user, Room room, long id, String handle, String text) {
if (limit >= 255) {
limit = 0;
Expand Down Expand Up @@ -192,9 +194,9 @@ private void executeCommand(User user, Room room, long id, String handle, String
} else if (cmd.equals("cancel")) {
sessions.remove(id);
} else if (cmd.equals("help")) {
room.send(handle + " [TIOBot command list](https://gist.github.com/SocraticPhoenix/bf98c72d0c1274acce76bc02ac6ee253)");
room.send(handle + " [TIOBot command list](https://github.com/SocraticPhoenix/TioBot/wiki/Commands)");
} else if (cmd.equals("version")) {
room.send(handle + " TIOBot v 0.0.3");
room.send(handle + " TIOBot v 0.0.4");
} else if (cmd.equals("alias")) {
if (content == null) {
room.send(handle + " expected more arguments...");
Expand All @@ -211,8 +213,7 @@ private void executeCommand(User user, Room room, long id, String handle, String
builder.append("\nLanguage Aliases:\n");
this.languages.forEach((k, v) -> builder.append(k).append(" -> ").append(v).append("\n"));
room.send(codeBlock(builder.toString()));
} else
if (tc.length < 2) {
} else if (tc.length < 2) {
room.send(handle + " expected more arguments...");
} else {
String type = tc[0];
Expand All @@ -230,6 +231,13 @@ private void executeCommand(User user, Room room, long id, String handle, String
} else {
room.send(handle + " removed alias for \"" + tc[1] + "\"");
}
} else if (type.equals("rmessage")) {
String k = messages.remove(tc[1].toLowerCase());
if (k == null) {
room.send(handle + " no alias exists for \"" + tc[1] + "\"");
} else {
room.send(handle + " removed alias for \"" + tc[1] + "\"");
}
} else {
String[] aliasContent = tc[1].split(" ", 2);
if (aliasContent.length < 2) {
Expand All @@ -242,6 +250,10 @@ private void executeCommand(User user, Room room, long id, String handle, String
String command = aliasContent[0].toLowerCase();
commands.put(command, aliasContent[1]);
room.send(handle + " Added alias for " + command);
} else if (type.equals("message")) {
String command = aliasContent[0].toLowerCase();
messages.put(command, aliasContent[1]);
room.send(handle + " Added alias for " + command);
} else {
room.send(handle + " No alias type called \"" + type + "\"");
}
Expand Down Expand Up @@ -371,6 +383,11 @@ private void executeCommand(User user, Room room, long id, String handle, String

String cmd = this.commands.get(key.get()).replace("%args%", text);
executeCommand(user, room, id, handle, cmd);
} else {
key = this.messages.keySet().stream().filter(finalText::equalsIgnoreCase).findFirst();
if (key.isPresent()) {
room.send(this.messages.get(key.get()).replace("%handle%", handle));
}
}
}
limit--;
Expand All @@ -383,19 +400,22 @@ public void saveState(JSONObject rooms, Room room) {

JSONObject langAliases = new JSONObject();
JSONObject commandAliases = new JSONObject();
JSONObject messageAliases = new JSONObject();

this.languages.forEach((k, v) -> {
JSONArray list = new JSONArray();
v.forEach(list::put);
langAliases.put(k, list);
});
this.commands.forEach(commandAliases::put);
this.messages.forEach(messageAliases::put);

JSONArray permitted = new JSONArray();
this.permitted.forEach(permitted::put);

object.put("languages", langAliases);
object.put("commands", commandAliases);
object.put("messages", messageAliases);
object.put("permitted", permitted);

rooms.put(id, object);
Expand All @@ -406,8 +426,11 @@ public void loadState(JSONObject rooms, Room room) {
if (rooms.keySet().contains(key)) {
JSONObject object = rooms.getJSONObject(key);

Stream.of("languages", "commands", "messages").filter(s -> !object.keySet().contains(s)).forEach(s -> object.put(s, new JSONObject()));

JSONObject langAliases = object.getJSONObject("languages");
JSONObject commandAliases = object.getJSONObject("commands");
JSONObject messages = object.getJSONObject("messages");

langAliases.keySet().forEach(s -> {
List<String> list = new ArrayList<>();
Expand All @@ -418,6 +441,7 @@ public void loadState(JSONObject rooms, Room room) {
this.languages.put(s, list);
});
commandAliases.keySet().forEach(s -> commands.put(s, String.valueOf(commandAliases.get(s))));
messages.keySet().forEach(s -> messages.put(s, String.valueOf(messages.get(s))));

JSONArray permitted = object.getJSONArray("permitted");
for (int i = 0; i < permitted.length(); i++) {
Expand Down
82 changes: 51 additions & 31 deletions src/main/java/com/gmail/socraticphoenix/tiobot/TioBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

public class TioBot {
private static boolean silentJoin = false;
public static boolean running = true;
public static boolean silentLeave = false;

public static void main(String[] args) throws IOException {
if (args.length == 0) {
Expand Down Expand Up @@ -106,6 +108,16 @@ public static void main(String[] args) throws IOException {
e.printStackTrace();
}
}

synchronized (config) {
String content = configJson.toString(0);
try (FileWriter writer = new FileWriter(config)) {
writer.write(content);
} catch (IOException e) {
System.err.println("Failed to save config");
e.printStackTrace();
}
}
}

try {
Expand Down Expand Up @@ -133,33 +145,11 @@ public static void main(String[] args) throws IOException {
}
}

while (true) {
while (TioBot.running) {
String command = scanner.nextLine();
if (command.startsWith("exit")) {
boolean silent = command.equals("exit silent");
running.set(false);
System.out.println("Logging off...");
System.out.println("Saving config.");

while (!finished.get()) {
;
}

synchronized (roomConf) {
rooms.forEach((r, m) -> {
if (!silent) {
r.send("TIOBot logging off!");
}
m.saveState(roomConf, r);
});

client.close();

try (FileWriter writer = new FileWriter(roomStates)) {
writer.write(roomConf.toString());
}
}

TioBot.silentLeave = command.equals("exit silent");
TioBot.running = false;
break;
} else if (command.startsWith("join")) {
String[] pieces = command.split(" ");
Expand All @@ -183,13 +173,15 @@ public static void main(String[] args) throws IOException {
configJson.put("joins", new JSONObject());
}

JSONObject joins = configJson.getJSONObject("joins");
String host = pieces[1].toUpperCase();
if (!joins.keySet().contains(host)) {
joins.put(host, new JSONArray());
synchronized (configJson) {
JSONObject joins = configJson.getJSONObject("joins");
String host = pieces[1].toUpperCase();
if (!joins.keySet().contains(host)) {
joins.put(host, new JSONArray());
}
joins.getJSONArray(host).put(Integer.parseInt(pieces[2]));
System.out.println("Added room to autojoin list");
}
joins.getJSONArray(host).put(Integer.parseInt(pieces[2]));
System.out.println("Added room to autojoin list");
}
} catch (NumberFormatException e) {
System.out.println(pieces[2] + " must be an integer room id");
Expand All @@ -200,6 +192,34 @@ public static void main(String[] args) throws IOException {
}
}
}

running.set(false);
System.out.println("Logging off...");
System.out.println("Saving config.");

while (!finished.get());

synchronized (roomConf) {
rooms.forEach((r, m) -> {
if (!TioBot.silentLeave) {
r.send("TIOBot logging off!");
}
m.saveState(roomConf, r);
});

client.close();

try (FileWriter writer = new FileWriter(roomStates)) {
writer.write(roomConf.toString());
}
}

synchronized (configJson) {
String content = configJson.toString(0);
try (FileWriter writer = new FileWriter(config)) {
writer.write(content);
}
}
}

private static void join(StackExchangeClient client, Map<Room, MessageListener> rooms, String host, int id, LanguageCache cache, JSONObject roomConf) {
Expand Down

0 comments on commit 2a8ac53

Please sign in to comment.