Add message cleaner.
[xudocci.git] / src / main / java / net / pterodactylus / irc / util / MessageCleaner.java
diff --git a/src/main/java/net/pterodactylus/irc/util/MessageCleaner.java b/src/main/java/net/pterodactylus/irc/util/MessageCleaner.java
new file mode 100644 (file)
index 0000000..6d0e3a2
--- /dev/null
@@ -0,0 +1,171 @@
+/*
+ * XdccDownloader - MessageCleaner.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.irc.util;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.EnumSet;
+import java.util.Set;
+
+/**
+ * Removes formatting codes from IRC messages.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class MessageCleaner {
+
+       /** The kind of attributes to clear. */
+       public enum Attributes {
+
+               bold,
+               color,
+               clear
+
+       }
+
+       /** A default instance. */
+       private static final MessageCleaner DEFAULT_INSTANCE = new MessageCleaner();
+
+       /**
+        * Cleans all formatting from the given line.
+        *
+        * @param line
+        *              The line to remove all formatting from
+        * @return The given line with all formatting removed
+        */
+       public String clean(String line) {
+               return clean(line, EnumSet.allOf(Attributes.class));
+       }
+
+       /**
+        * Cleans the given formattings from the given line.
+        *
+        * @param line
+        *              The line to remove all formatting from
+        * @param attributes
+        *              The attributes to remove
+        * @return The given line with all given formatting removed
+        */
+       public String clean(String line, Set<Attributes> attributes) {
+               StringBuilder clean = new StringBuilder(line.length());
+
+               StringReader reader = new StringReader(line);
+
+               try {
+                       int inColorCode = 0;
+                       while (true) {
+                               int r = reader.read();
+                               if (r == -1) {
+                                       break;
+                               }
+                               char c = (char) r;
+                               if ((c == 2) && (attributes.contains(Attributes.bold))) {
+                                       continue;
+                               }
+                               if ((c == 3) && (attributes.contains(Attributes.color))) {
+                                       inColorCode = 1;
+                                       continue;
+                               }
+                               if ((c == 15) && (attributes.contains(Attributes.clear))) {
+                                       continue;
+                               }
+                               if (inColorCode > 0) {
+                                       if (inColorCode == 1) {
+                                               if ((c < '0') || (c > '9')) {
+                                                       inColorCode = 0;
+                                               } else {
+                                                       if (c == '9') {
+                                                               inColorCode = 8;
+                                                       } else {
+                                                               ++inColorCode;
+                                                       }
+                                                       continue;
+                                               }
+                                       } else if (inColorCode == 2) {
+                                               if (c == ',') {
+                                                       inColorCode = 4;
+                                                       continue;
+                                               }
+                                               if ((c < '0') || (c > '5')) {
+                                                       inColorCode = 0;
+                                               } else {
+                                                       ++inColorCode;
+                                                       continue;
+                                               }
+                                       } else if (inColorCode == 3) {
+                                               if (c == ',') {
+                                                       ++inColorCode;
+                                                       continue;
+                                               } else {
+                                                       inColorCode = 0;
+                                               }
+                                       } else if (inColorCode == 4) {
+                                               if (c == '9') {
+                                                       inColorCode = 9;
+                                                       continue;
+                                               } else if ((c < '0') || (c > '9')) {
+                                                       inColorCode = 0;
+                                               } else {
+                                                       ++inColorCode;
+                                                       continue;
+                                               }
+                                       } else if (inColorCode == 5) {
+                                               inColorCode = 0;
+                                               if ((c >= '0') && (c <= '5')) {
+                                                       continue;
+                                               }
+                                       } else if (inColorCode == 8) {
+                                               if (c == '9') {
+                                                       inColorCode = 3;
+                                                       continue;
+                                               } else if (c == ',') {
+                                                       inColorCode = 4;
+                                                       continue;
+                                               } else {
+                                                       inColorCode = 0;
+                                               }
+                                       } else if (inColorCode == 9) {
+                                               inColorCode = 0;
+                                               if (c == '9') {
+                                                       continue;
+                                               }
+                                       }
+                               }
+                               clean.append(c);
+                       }
+               } catch (IOException ioe1) {
+                       /* StringReader will never throw. */
+               }
+
+               return clean.toString();
+       }
+
+       //
+       // STATIC METHODS
+       //
+
+       /**
+        * Returns the default instance.
+        *
+        * @return The default instance
+        */
+       public static MessageCleaner getDefaultInstance() {
+               return DEFAULT_INSTANCE;
+       }
+
+}