diff options
author | snowleo <schneeleo@gmail.com> | 2011-10-12 03:14:07 +0200 |
---|---|---|
committer | snowleo <schneeleo@gmail.com> | 2011-10-12 03:14:26 +0200 |
commit | ea192ddd6d5b538e0bd0f6f1721890eb3c25de30 (patch) | |
tree | 344d5ad4eec389eedc6a72dbfb1bdfba1047a447 /EssentialsUpdate/src/f00f/net/irc/martyr/util/CtcpUtil.java | |
parent | 1b34ed40736b03f440352f8193f24b6b0525868a (diff) | |
download | Essentials-ea192ddd6d5b538e0bd0f6f1721890eb3c25de30.tar Essentials-ea192ddd6d5b538e0bd0f6f1721890eb3c25de30.tar.gz Essentials-ea192ddd6d5b538e0bd0f6f1721890eb3c25de30.tar.lz Essentials-ea192ddd6d5b538e0bd0f6f1721890eb3c25de30.tar.xz Essentials-ea192ddd6d5b538e0bd0f6f1721890eb3c25de30.zip |
EssentialsUpdate WIP
Diffstat (limited to 'EssentialsUpdate/src/f00f/net/irc/martyr/util/CtcpUtil.java')
-rw-r--r-- | EssentialsUpdate/src/f00f/net/irc/martyr/util/CtcpUtil.java | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/EssentialsUpdate/src/f00f/net/irc/martyr/util/CtcpUtil.java b/EssentialsUpdate/src/f00f/net/irc/martyr/util/CtcpUtil.java new file mode 100644 index 000000000..ed31c46e7 --- /dev/null +++ b/EssentialsUpdate/src/f00f/net/irc/martyr/util/CtcpUtil.java @@ -0,0 +1,99 @@ +package f00f.net.irc.martyr.util; + +import java.util.NoSuchElementException; + +public class CtcpUtil +{ + public static final char CTCP_TAG_DELIM = '\001'; + + /** + * Returns a new string ready for sending via MessageCommand. + * + * @param action Action string to create + * @return Action string ready for sending + */ + public static String makeActionString( String action ) + { + return makeCtcpString( "ACTION " + action ); + } + + public static String makeCtcpString( String s ) + { + return "" + CTCP_TAG_DELIM + s + CTCP_TAG_DELIM; + } + + /** + * Parses the string into tokens, where each token is either a + * CTCP escaped sequence or not. + */ + public static class CtcpTokenizer + { + private String str; + + public CtcpTokenizer( String in ) + { + this.str = in; + } + + public boolean isNextACtcp() + { + return str.charAt(0) == CTCP_TAG_DELIM; + } + + public boolean hasNext() + { + return !str.equals(""); + } + + public String next() + { + return nextToken(); + } + public String nextToken() + { + if( !hasNext() ) + { + throw new NoSuchElementException(); + } + + int pos = str.indexOf( CTCP_TAG_DELIM, 1 ); + String result; + if( isNextACtcp() ) + { + if( pos < 0 ) + { + // Error? Well, whatever, return the rest of the + // string. + result = str.substring( 1 ); + str = ""; + return result; + } + else + { + // ^Aour string^A(rest of string) + // Lose both ^A + result = str.substring( 1, pos ); + str = str.substring( pos + 1 ); + return result; + } + } + else + { + // Not a CTCP + if( pos < 0 ) + { + result = str; + str = ""; + return result; + } + else + { + result = str.substring( 0, pos ); + str = str.substring( pos ); + return result; + } + } + } + } +} + |