2018-07-06 15:30:00 +02:00
|
|
|
package com.eu.habbo.habbohotel.modtool;
|
|
|
|
|
|
|
|
import com.eu.habbo.Emulator;
|
|
|
|
import com.eu.habbo.habbohotel.messenger.Message;
|
|
|
|
import com.eu.habbo.habbohotel.rooms.RoomChatMessage;
|
|
|
|
import com.eu.habbo.habbohotel.users.Habbo;
|
|
|
|
import com.eu.habbo.messages.outgoing.friends.FriendChatMessageComposer;
|
|
|
|
import com.eu.habbo.plugin.events.users.UserTriggerWordFilterEvent;
|
|
|
|
import gnu.trove.iterator.hash.TObjectHashIterator;
|
|
|
|
import gnu.trove.set.hash.THashSet;
|
2018-09-28 21:25:00 +02:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2018-09-28 21:25:00 +02:00
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.sql.Statement;
|
2018-07-06 15:30:00 +02:00
|
|
|
import java.text.Normalizer;
|
2018-10-07 00:28:00 +02:00
|
|
|
import java.util.regex.Pattern;
|
2018-07-06 15:30:00 +02:00
|
|
|
|
|
|
|
public class WordFilter
|
|
|
|
{
|
|
|
|
//Configuration. Loaded from database & updated accordingly.
|
|
|
|
public static boolean ENABLED_FRIENDCHAT = true;
|
2018-10-07 00:28:00 +02:00
|
|
|
public static String DEFAULT_REPLACEMENT = "bobba";
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2018-10-07 00:28:00 +02:00
|
|
|
protected THashSet<WordFilterWord> autoReportWords = new THashSet<>();
|
|
|
|
protected THashSet<WordFilterWord> hideMessageWords = new THashSet<>();
|
|
|
|
protected THashSet<WordFilterWord> words = new THashSet<>();
|
2018-07-06 15:30:00 +02:00
|
|
|
|
|
|
|
public WordFilter()
|
|
|
|
{
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
this.reload();
|
|
|
|
Emulator.getLogging().logStart("WordFilter -> Loaded! (" + (System.currentTimeMillis() - start) + " MS)");
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void reload()
|
|
|
|
{
|
|
|
|
if(!Emulator.getConfig().getBoolean("hotel.wordfilter.enabled"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.autoReportWords.clear();
|
|
|
|
this.hideMessageWords.clear();
|
|
|
|
this.words.clear();
|
|
|
|
|
|
|
|
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); Statement statement = connection.createStatement();)
|
|
|
|
{
|
|
|
|
try (ResultSet set = statement.executeQuery("SELECT * FROM wordfilter"))
|
|
|
|
{
|
|
|
|
while (set.next())
|
|
|
|
{
|
|
|
|
WordFilterWord word;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
word = new WordFilterWord(set);
|
|
|
|
}
|
|
|
|
catch (SQLException e)
|
|
|
|
{
|
|
|
|
Emulator.getLogging().logSQLException(e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (word.autoReport)
|
|
|
|
this.autoReportWords.add(word);
|
|
|
|
else if (word.hideMessage)
|
|
|
|
this.hideMessageWords.add(word);
|
2018-09-28 21:25:00 +02:00
|
|
|
|
|
|
|
this.words.add(word);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (SQLException e)
|
|
|
|
{
|
|
|
|
Emulator.getLogging().logSQLException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public String normalise(String message)
|
|
|
|
{
|
2018-10-07 00:28:00 +02:00
|
|
|
return DIACRITICS_AND_FRIENDS.matcher(Normalizer.normalize(StringUtils.stripAccents(message), Normalizer.Form.NFKD).replaceAll("[,.;:'\"]", "").replace("I", "l")
|
|
|
|
.replaceAll("[^\\p{ASCII}*$]", "").replaceAll("\\p{M}", "").replaceAll("^\\p{M}*$]", "").replaceAll("[1|]", "i").replace("2", "z").replace("3", "e").replace("4","a").replace("5", "s").replace("8", "b").replace("0", "o").replace(" ", "").replace("$", "s").replace("ß", "b").trim()).replaceAll("");
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean autoReportCheck(RoomChatMessage roomChatMessage)
|
|
|
|
{
|
|
|
|
String message = this.normalise(roomChatMessage.getMessage()).toLowerCase();
|
|
|
|
|
|
|
|
TObjectHashIterator iterator = this.autoReportWords.iterator();
|
|
|
|
|
|
|
|
while (iterator.hasNext())
|
|
|
|
{
|
|
|
|
WordFilterWord word = (WordFilterWord) iterator.next();
|
|
|
|
|
|
|
|
if (message.contains(word.key))
|
|
|
|
{
|
|
|
|
Emulator.getGameEnvironment().getModToolManager().quickTicket(roomChatMessage.getHabbo(), "Automatic WordFilter", roomChatMessage.getMessage());
|
|
|
|
|
|
|
|
if(Emulator.getConfig().getBoolean("notify.staff.chat.auto.report"))
|
|
|
|
{
|
|
|
|
Emulator.getGameEnvironment().getHabboManager().sendPacketToHabbosWithPermission(new FriendChatMessageComposer(new Message(roomChatMessage.getHabbo().getHabboInfo().getId(), 0, Emulator.getTexts().getValue("warning.auto.report").replace("%user%", roomChatMessage.getHabbo().getHabboInfo().getUsername()).replace("%word%", word.key))).compose(), "acc_staff_chat");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hideMessageCheck(String message)
|
|
|
|
{
|
|
|
|
message = this.normalise(message).toLowerCase();
|
|
|
|
|
|
|
|
TObjectHashIterator iterator = this.hideMessageWords.iterator();
|
|
|
|
|
|
|
|
while (iterator.hasNext())
|
|
|
|
{
|
|
|
|
WordFilterWord word = (WordFilterWord) iterator.next();
|
|
|
|
|
|
|
|
if (message.contains(word.key))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String[] filter(String[] messages)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < messages.length; i++)
|
|
|
|
{
|
|
|
|
messages[i] = this.filter(messages[i], null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String filter(String message, Habbo habbo)
|
|
|
|
{
|
2018-09-28 21:25:00 +02:00
|
|
|
String filteredMessage = message;
|
2018-07-06 15:30:00 +02:00
|
|
|
if(Emulator.getConfig().getBoolean("hotel.wordfilter.normalise"))
|
|
|
|
{
|
2018-09-28 21:25:00 +02:00
|
|
|
filteredMessage = this.normalise(filteredMessage);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TObjectHashIterator iterator = this.words.iterator();
|
|
|
|
|
|
|
|
boolean foundShit = false;
|
|
|
|
|
|
|
|
while(iterator.hasNext())
|
|
|
|
{
|
|
|
|
WordFilterWord word = (WordFilterWord) iterator.next();
|
|
|
|
|
2018-09-28 21:25:00 +02:00
|
|
|
if(StringUtils.containsIgnoreCase(filteredMessage, word.key))
|
2018-07-06 15:30:00 +02:00
|
|
|
{
|
|
|
|
if(habbo != null)
|
|
|
|
{
|
|
|
|
if(Emulator.getPluginManager().fireEvent(new UserTriggerWordFilterEvent(habbo, word)).isCancelled())
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-28 21:25:00 +02:00
|
|
|
filteredMessage = filteredMessage.replaceAll("(?i)" + word.key, word.replacement);
|
2018-07-06 15:30:00 +02:00
|
|
|
foundShit = true;
|
|
|
|
|
2018-09-28 21:25:00 +02:00
|
|
|
if (habbo != null && word.muteTime > 0)
|
2018-07-06 15:30:00 +02:00
|
|
|
{
|
|
|
|
habbo.mute(word.muteTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundShit)
|
|
|
|
{
|
2018-09-28 21:25:00 +02:00
|
|
|
return message;
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:25:00 +02:00
|
|
|
return filteredMessage;
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void filter (RoomChatMessage roomChatMessage, Habbo habbo)
|
|
|
|
{
|
|
|
|
String message = roomChatMessage.getMessage().toLowerCase();
|
|
|
|
|
|
|
|
if(Emulator.getConfig().getBoolean("hotel.wordfilter.normalise"))
|
|
|
|
{
|
|
|
|
message = this.normalise(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
TObjectHashIterator iterator = this.words.iterator();
|
|
|
|
|
|
|
|
while(iterator.hasNext())
|
|
|
|
{
|
|
|
|
WordFilterWord word = (WordFilterWord) iterator.next();
|
|
|
|
|
2018-10-07 00:28:00 +02:00
|
|
|
if(StringUtils.containsIgnoreCase(message, word.key))
|
2018-07-06 15:30:00 +02:00
|
|
|
{
|
|
|
|
if(habbo != null)
|
|
|
|
{
|
|
|
|
if(Emulator.getPluginManager().fireEvent(new UserTriggerWordFilterEvent(habbo, word)).isCancelled())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
message = message.replace(word.key, word.replacement);
|
|
|
|
roomChatMessage.filtered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (roomChatMessage.filtered)
|
|
|
|
{
|
|
|
|
roomChatMessage.setMessage(message);
|
|
|
|
}
|
|
|
|
}
|
2018-10-07 00:28:00 +02:00
|
|
|
|
|
|
|
private static final Pattern DIACRITICS_AND_FRIENDS = Pattern.compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
|
|
|
|
|
|
|
|
private static String stripDiacritics(String str) {
|
|
|
|
str = Normalizer.normalize(str, Normalizer.Form.NFD);
|
|
|
|
str = DIACRITICS_AND_FRIENDS.matcher(str).replaceAll("");
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addWord(WordFilterWord word)
|
|
|
|
{
|
|
|
|
this.words.add(word);
|
|
|
|
}
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|