Chat filter API changes from TwoHat/PottyMouth to Inversoft/CleanSpeak.

This commit is contained in:
Peter Miller 2014-07-30 20:09:58 -04:00
parent 4d397438be
commit fb0469adfa
1 changed files with 69 additions and 9 deletions

View File

@ -49,9 +49,12 @@ public class Chat extends MiniPlugin
private CoreClientManager _clientManager;
private PreferencesManager _preferences;
private String _filterUrl = "https://mp9wbhy6.pottymouthfilter.com/v1/";
//private String _filterUrl = "https://mp9wbhy6.pottymouthfilter.com/v1/";
//private String _filterUrl = "https://10.33.53.5:8003/content/item/filter";
private String _filterUrl = "https://10.33.53.5:8003/content/item/moderate";
private String _appId = "34018d65-466d-4a91-8e92-29ca49f022c4";
private String _apiKey = "oUywMpwZcIzZO5AWnfDx";
private String _authName = "";
//private String _authName = "";
private String _serverName;
private long _silenced = 0;
@ -190,21 +193,53 @@ public class Chat extends MiniPlugin
if (event.isAsynchronous())
{
final Player player = event.getPlayer();
final String plyrname = player.toString();
//final String plyrname = player.toString();
final String plyrname = player.getUniqueId().toString();
final String msg = event.getMessage().replaceAll("[^\\x00-\\x7F]", "").trim();
final String filtertype = "chat";
//final String filtertype = "chat";
final String filtertype = "moderate";
final String dname = player.getPlayerListName();
JSONObject message = buildJsonChatObject(filtertype, dname, plyrname, msg, _serverName, 1);
String response = getResponseFromTwoHat(message, filtertype);
String response = getResponseFromCleanSpeak(message, filtertype);
if (response == null)
{
System.out.println("[ERROR] Unable to filter chat message...thanks a lot TwoHat.");
System.out.println("[ERROR] Unable to filter chat message...thanks a lot CleanSpeak.");
return;
}
/* TESTING OUTPUT - POSSIBLY USEFUL
System.out.println(message);
System.out.println(response);
System.out.println(JSONValue.parse(response));
//NullPointerException occasionally happening, JSONValue.parse(String) returns null randomly, why?
for (Object o : ((JSONObject)JSONValue.parse(response)).values())
{
System.out.println(o.toString());
}
*/
String filteredMsg = "";
filteredMsg = ((JSONObject) JSONValue.parse(response)).get("content").toString();
if (filteredMsg.contains("parts"))
{
filteredMsg = ((JSONObject) JSONValue.parse(filteredMsg)).get("parts").toString();
filteredMsg = filteredMsg.replace('[', ' ').replace(']', ' ').trim();
filteredMsg = ((JSONObject) JSONValue.parse(filteredMsg)).get("replacement").toString();
for (Player onlinePlayer : event.getRecipients())
{
onlinePlayer.sendMessage(String.format(event.getFormat(), event.getPlayer().getDisplayName(), filteredMsg));
}
event.setCancelled(true);
}
/*
int risk = Integer.parseInt(((JSONObject) JSONValue.parse(response)).get("risk").toString());
if (risk >= 5)
@ -232,6 +267,7 @@ public class Chat extends MiniPlugin
event.setCancelled(true);
}
*/
}
}
@ -295,6 +331,7 @@ public class Chat extends MiniPlugin
switch (filtertype)
{
case "chat":
/*
message.put("player_display_name", name);
message.put("player", player);
message.put("text", msg);
@ -302,6 +339,25 @@ public class Chat extends MiniPlugin
message.put("room", server);
message.put("language", "en");
message.put("rule", rule);
*/
message.put("content", msg);
break;
case "moderate":
JSONObject content = new JSONObject();
content.put("content", msg);
content.put("type", "text");
JSONArray parts = new JSONArray();
parts.add(content);
JSONObject mainContent = new JSONObject();
mainContent.put("applicationId", _appId);
mainContent.put("createInstant", System.currentTimeMillis());
mainContent.put("parts", parts);
mainContent.put("senderDisplayName", name);
mainContent.put("senderId", player);
message.put("content", mainContent);
break;
case "username":
message.put("player_id", name);
@ -313,12 +369,15 @@ public class Chat extends MiniPlugin
return message;
}
private String getResponseFromTwoHat(JSONObject message, String filtertype)
private String getResponseFromCleanSpeak(JSONObject message, String filtertype)
{
/*
String authString = _authName + ":" + _apiKey;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
String url = _filterUrl + filtertype;
*/
String url = _filterUrl;
StringBuffer response = null;
@ -335,8 +394,9 @@ public class Chat extends MiniPlugin
// add request header con.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
//connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.addRequestProperty("Authentication", _apiKey);
String urlParameters = message.toString();
@ -361,7 +421,7 @@ public class Chat extends MiniPlugin
}
catch (Exception exception)
{
System.out.println("Error getting response from TwoHat : " + exception.getMessage());
System.out.println("Error getting response from CleanSpeak : " + exception.getMessage());
}
finally
{