diff options
Diffstat (limited to 'EssentialsUpdate/src/f00f/net/irc/martyr/util')
4 files changed, 472 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; + } + } + } + } +} + diff --git a/EssentialsUpdate/src/f00f/net/irc/martyr/util/FullNick.java b/EssentialsUpdate/src/f00f/net/irc/martyr/util/FullNick.java new file mode 100644 index 000000000..c83cd98d4 --- /dev/null +++ b/EssentialsUpdate/src/f00f/net/irc/martyr/util/FullNick.java @@ -0,0 +1,159 @@ +package f00f.net.irc.martyr.util; + +import java.util.StringTokenizer; + +/** + * Parses out a full nick (ex: sork<exclaimation>sork<at>f00f.net) and stores it for + * use. It also provides a consistent hashing method. + */ +public class FullNick +{ + + private String nick; + private String user; + private String remotehost; + + private String original; + + public FullNick( String original ) + { + this.original = original; + parse( original ); + } + + + /** + * It can't deal with parameters that have no '!'. When given a parameter with + * no '!', it simply places the entire string into the 'nick' field. FullNick + * is intended to be immutable. + * + * TODO: Should this enforce proper nick syntax? + * @param original Original nick we will parse + */ + private void parse( String original ) + { + if( original == null ) + return; + + StringTokenizer tokens = new StringTokenizer( original, "!", false ); + + nick = tokens.nextToken(); + + if( tokens.hasMoreTokens() ) + { + user = tokens.nextToken("@"); + if( user.charAt(0) == '!' ) + user = user.substring(1); + } + + if( tokens.hasMoreTokens() ) + { + remotehost = tokens.nextToken(""); + if( remotehost.charAt(0) == '@' ) + remotehost = remotehost.substring(1); + } + } + + public String getNick() + { + return nick; + } + + public String getUser() + { + return user; + } + + public String getHost() + { + return remotehost; + } + + public String getSource() + { + //return nick+"!"+user+"@"+remotehost; + return original; + } + + + public int hashCode() + { + if( nick == null ) + return 0; + + return nick.hashCode(); + } + + /** + * Performs case insesitive equals on the nicks only. Does not strip + * off any leading @ or +. ({ == [ and ] == } and | == \) It appears + * that servers are not RFC complient on this, so we will not as well. + * + * @param nick Nick to compare this nick with + * @return True or false of nick is the same + */ + + public boolean equals( String nick ) + { + if( nick == null ) + return false; + + return nick.equalsIgnoreCase( this.nick ); + } + + public boolean equals( FullNick nick ) + { + if( nick == null ) + return false; + return equals( nick.getNick() ); + } + + public boolean equals( Object object ) + { + if( object instanceof FullNick ) + return equals( (FullNick)object ); + return false; + } + + /** + * @return the nick part + * */ + public String toString() + { + return nick; + } + + /** + * Unit test. + * + * @param args Args passed to program + */ + public static void main( String args[] ) + { + + FullNick nick = new FullNick( args[0] ); + + System.out.println( nick.getNick() ); + System.out.println( nick.getUser() ); + System.out.println( nick.getHost() ); + System.out.println( nick.getSource() ); + + if( args.length > 1 ) + { + + FullNick nick2 = new FullNick( args[1] ); + + System.out.println( "" ); + System.out.println( nick2.getNick() ); + System.out.println( nick2.getUser() ); + System.out.println( nick2.getHost() ); + System.out.println( nick2.getSource() ); + + System.out.println( nick2.equals( nick ) ? "Equal." : "Not equal." ); + + } + } + +} + + diff --git a/EssentialsUpdate/src/f00f/net/irc/martyr/util/IRCStringUtils.java b/EssentialsUpdate/src/f00f/net/irc/martyr/util/IRCStringUtils.java new file mode 100644 index 000000000..3b4fa6075 --- /dev/null +++ b/EssentialsUpdate/src/f00f/net/irc/martyr/util/IRCStringUtils.java @@ -0,0 +1,90 @@ +package f00f.net.irc.martyr.util; + +import java.text.CharacterIterator; +import java.text.StringCharacterIterator; +import java.util.ArrayList; +import java.util.regex.Pattern; +import java.util.regex.Matcher; + +/** + * @author Daniel Henninger + */ +public class IRCStringUtils +{ + + /** + * Returns the message with all control characters stripped from it. + * + * @param msg Message to remove control chars from. + * @return Stripped form of message. + */ + public static String stripControlChars(String msg) + { + Pattern pa = Pattern.compile("\u0003\\p{Digit}\\p{Digit}"); + Matcher ma = pa.matcher(msg); + Pattern pb = Pattern.compile("\\p{Cntrl}"); + Matcher mb = pb.matcher(ma.replaceAll("")); + return mb.replaceAll(""); + } + + /** + * Returns the message with all formatting characters converted into associated html characters. + * + * TODO: Should actually parse colors. + * @param msg Message to convert to HTML format. + * @return Message in HTML format. + */ + public static String convertToHTML(String msg) + { + CharacterIterator ci = new StringCharacterIterator(msg); + String htmlStr = ""; + ArrayList<String> formatList = new ArrayList<String>(); + for (char c = ci.first(); c != CharacterIterator.DONE; c = ci.next()) { + if (c == '\u0002') { + if (formatList.contains("</b>")) { + formatList.remove("</b>"); + htmlStr += "</b>"; + } + else { + formatList.add("</b>"); + htmlStr += "<b>"; + } + } + else if (c == '\u001F') { + if (formatList.contains("</u>")) { + formatList.remove("</u>"); + htmlStr += "</u>"; + } + else { + formatList.add("</u>"); + htmlStr += "<u>"; + } + } + else if (c == '\u0016') { + if (formatList.contains("</i>")) { + formatList.remove("</i>"); + htmlStr += "</i>"; + } + else { + formatList.add("</i>"); + htmlStr += "<i>"; + } + } + else if (c == '\u000F' || c == '\u0015') { + for (String f : formatList) { + htmlStr += f; + } + formatList.clear(); + } + else { + htmlStr += c; + } + } + for (String f : formatList) { + htmlStr += f; + } + formatList.clear(); + return stripControlChars(htmlStr); + } + +} diff --git a/EssentialsUpdate/src/f00f/net/irc/martyr/util/ParameterIterator.java b/EssentialsUpdate/src/f00f/net/irc/martyr/util/ParameterIterator.java new file mode 100644 index 000000000..d1e2c8507 --- /dev/null +++ b/EssentialsUpdate/src/f00f/net/irc/martyr/util/ParameterIterator.java @@ -0,0 +1,124 @@ +/* + * Original author: Ben Damm <bdamm@dammfine.com> + * Changes by: Mog + * - Fixed bug with substring handling + * */ +package f00f.net.irc.martyr.util; + + +import java.util.Iterator; +import java.util.NoSuchElementException; + +//TODO: Unit test + +/** + * This class iterates over the parameter string of an IRC command, + * returning each parameter in order as next() is called. This class + * also knows about the ":" parameters, which is the large string at + * the end of most commands, and treats it specially. + */ +public class ParameterIterator implements Iterator +{ + //static Logger log = Logger.getLogger(ParameterIterator.class); + + private String paramStr; + private int position; + private String last = null; + + public ParameterIterator( String paramStr ) + { + //log.debug("ParameterIterator: Params: `" + paramStr + "'"); + // We don't check for null here because hasNext is the place + // to do it, according to the definition for Iterator. + // next() should throw an exception. + if( paramStr != null ) + { + this.paramStr = paramStr.trim(); + position = 0; + } + else + { + this.paramStr = null; + position = -1; + } + } + + /** + * @return true if there are more parameters, and false + * otherwise. + */ + public boolean hasNext() + { + if( paramStr == null ) + return false; + + return position < paramStr.length(); + } + + /** + * @throws NoSuchElementException if there are no more params + * @return true if the next parameter is also the ":" parameter. + * */ + public boolean nextIsLast() + { + if( ! hasNext() ) + { + throw new NoSuchElementException("No more parameters."); + } + return paramStr.charAt(position) == ':'; + } + + /** + * @throws NoSuchElementException if there are no more params + * */ + public Object next() + { + if( ! hasNext() ) + { + throw new NoSuchElementException("No more parameters."); + } + + // If : is the first char, the rest of the string is a + // parameter. + if( paramStr.charAt(position) == ':' ) + { + String result = paramStr.substring(position + 1); + position = paramStr.length(); + last = result; + return result; + } + + int spaceIndex = paramStr.indexOf( ' ', position ); + // We can't have a space after the last parameter, it gets + // trimmed in the constructor. Also, we can't have only + // spaces, so we don't need to check for -1. Finally, we are + // guaranteed to have a space before the colon, so we don't + // have to do any checking at all! + + String result = paramStr.substring( position, spaceIndex ); + position = spaceIndex + 1; + return result; + } + + /** + * Forwards the iterator to the last element and returns it. The + * "last" parameter should be the ":" parameter. + * + * @return Last parameter + * */ + public String last() + { + while( hasNext() ) + next(); + + return last; + } + + public void remove() + { + // hmm, nah. This can be implemented some other time. + throw new UnsupportedOperationException( "Remove on the parameters? Why?" ); + } +} + + |