use of org.json.JSONStringer in project quorrabot by GloriousEggroll.
the class SoundBoard method handleMessage.
private void handleMessage(WebSocket webSocket, String jsonString) {
JSONObject jsonObject;
JSONArray jsonArray;
wsSession sessionData;
Boolean authenticated;
String dataString;
String uniqueID;
int dataInt;
try {
jsonObject = new JSONObject(jsonString);
} catch (JSONException ex) {
com.gmt2001.Console.err.println("SoundBoard: Bad JSON passed [" + jsonString + "]");
com.gmt2001.Console.err.printStackTrace(ex);
return;
} catch (Exception ex) {
com.gmt2001.Console.err.println("SoundBoard: Exception Occurred");
com.gmt2001.Console.err.printStackTrace(ex);
return;
}
sessionData = wsSessionMap.get(genSessionKey(webSocket));
if (jsonObject.has("authenticate")) {
authenticated = jsonObject.getString("authenticate").equals(authString);
if (authenticated) {
sessionData.setAuthenticated(authenticated);
sessionData.setReadOnly(false);
} else {
authenticated = jsonObject.getString("authenticate").equals(authStringRO);
sessionData.setAuthenticated(authenticated);
sessionData.setReadOnly(true);
if (!authenticated) {
authenticated = authenticateOauth(jsonObject.getString("authenticate"));
sessionData.setAuthenticated(authenticated);
sessionData.setReadOnly(false);
}
}
return;
}
if (!sessionData.isAuthenticated()) {
JSONStringer jsonStringer = new JSONStringer();
jsonStringer.object().key("autherror").value("not authenticated").endObject();
webSocket.send(jsonStringer.toString());
return;
}
// debugMsg("PanelSocketServer::onMessage(" + jsonString + ")");
try {
if (jsonObject.has("command")) {
dataString = jsonObject.getString("command");
if (jsonObject.has("username")) {
//Quorrabot.instance().handleCommand(jsonObject.getString("username"), dataString);
} else {
//Quorrabot.instance().handleCommand(Quorrabot.instance().getBotName(), dataString);
}
return;
} else if (jsonObject.has("version")) {
uniqueID = jsonObject.getString("version");
doVersion(webSocket, uniqueID);
} else if (jsonObject.has("dbquery")) {
uniqueID = jsonObject.getString("dbquery");
String table = jsonObject.getJSONObject("query").getString("table");
String key = jsonObject.getJSONObject("query").getString("key");
doDBQuery(webSocket, uniqueID, table, key);
return;
} else if (jsonObject.has("dbkeys")) {
uniqueID = jsonObject.getString("dbkeys");
String table = jsonObject.getJSONObject("query").getString("table");
doDBKeysQuery(webSocket, uniqueID, table);
} else if (jsonObject.has("dbupdate") && !sessionData.isReadOnly()) {
uniqueID = jsonObject.getString("dbupdate");
String table = jsonObject.getJSONObject("update").getString("table");
String key = jsonObject.getJSONObject("update").getString("key");
String value = jsonObject.getJSONObject("update").getString("value");
doDBUpdate(webSocket, uniqueID, table, key, value);
} else if (jsonObject.has("dbincr") && !sessionData.isReadOnly()) {
uniqueID = jsonObject.getString("dbincr");
String table = jsonObject.getJSONObject("incr").getString("table");
String key = jsonObject.getJSONObject("incr").getString("key");
String value = jsonObject.getJSONObject("incr").getString("value");
doDBIncr(webSocket, uniqueID, table, key, value);
} else if (jsonObject.has("dbdecr") && !sessionData.isReadOnly()) {
uniqueID = jsonObject.getString("dbdecr");
String table = jsonObject.getJSONObject("decr").getString("table");
String key = jsonObject.getJSONObject("decr").getString("key");
String value = jsonObject.getJSONObject("decr").getString("value");
doDBDecr(webSocket, uniqueID, table, key, value);
} else if (jsonObject.has("dbdelkey") && !sessionData.isReadOnly()) {
uniqueID = jsonObject.getString("dbdelkey");
String table = jsonObject.getJSONObject("delkey").getString("table");
String key = jsonObject.getJSONObject("delkey").getString("key");
doDBDelKey(webSocket, uniqueID, table, key);
} else if (jsonObject.has("soundboard_hooks") && !sessionData.isReadOnly()) {
doSoundBoardsUpdate(jsonObject);
} else {
com.gmt2001.Console.err.println("SoundBoard: Unknown JSON passed [" + jsonString + "]");
return;
}
} catch (JSONException ex) {
com.gmt2001.Console.err.println("SoundBoard::JSONException(" + ex.getMessage() + "): " + jsonString);
}
}
use of org.json.JSONStringer in project quorrabot by GloriousEggroll.
the class SoundBoard method alertImage.
public void alertImage(String imageInfo) {
JSONStringer jsonObject = new JSONStringer();
jsonObject.object().key("alert_image").value(imageInfo).endObject();
debugMsg("alertImage(" + imageInfo + ")");
sendToAll(jsonObject.toString());
}
use of org.json.JSONStringer in project quorrabot by GloriousEggroll.
the class SoundBoard method doDBQuery.
private void doDBQuery(WebSocket webSocket, String id, String table, String key) {
JSONStringer jsonObject = new JSONStringer();
String value = "";
try {
value = Quorrabot.instance().getDataStore().GetString(table, "", key);
} catch (NullPointerException ex) {
if (!dbCallNull) {
dbCallNull = true;
debugMsg("NULL returned from DB. DB Object not created yet.");
}
return;
}
dbCallNull = false;
jsonObject.object().key("query_id").value(id).key("results").object();
jsonObject.key("table").value(table).key(key).value(value).endObject().endObject();
webSocket.send(jsonObject.toString());
}
use of org.json.JSONStringer in project quorrabot by GloriousEggroll.
the class SoundBoard method doDBUpdate.
private void doDBUpdate(WebSocket webSocket, String id, String table, String key, String value) {
JSONStringer jsonObject = new JSONStringer();
try {
Quorrabot.instance().getDataStore().set(table, key, value);
} catch (NullPointerException ex) {
if (!dbCallNull) {
debugMsg("NULL returned from DB. DB Object not created yet.");
}
return;
}
jsonObject.object().key("query_id").value(id).endObject();
webSocket.send(jsonObject.toString());
}
use of org.json.JSONStringer in project quorrabot by GloriousEggroll.
the class SoundBoard method triggerAudioPanel.
public void triggerAudioPanel(String soundHook) {
JSONStringer jsonObject = new JSONStringer();
jsonObject.object().key("soundboard_hook").value(soundHook).endObject();
debugMsg("triggerAudioPanel(" + soundHook + ")");
sendToAll(jsonObject.toString());
}
Aggregations