package chess4j;

import org.apache.commons.net.telnet.TelnetClient


def readUntil( pattern ) {
    sb = new StringBuffer()
    while ((ch = reader.read()) != -1) {
        sb << (char) ch
        if (sb.toString().endsWith(pattern)) {
            def found = sb.toString()
            sb = new StringBuffer()
            return found
        }
    }
    return null
}

telnet = new TelnetClient()
telnet.connect( 'chessclub.com', 5000 )
reader = telnet.inputStream.newReader()
writer = new PrintWriter(new OutputStreamWriter(telnet.outputStream),true)
//readUntil( "login" )
println readUntil( "login: " )
writer.println("lesserprophet")
println readUntil( "password: ")
writer.println("mypassword")
writer.println("set style 10")
while (true) {
	println readUntil("aics%")
}
//readUntil( "continue:" )
//writer.println()
//readUntil( "-- " )
//writer.println()
//readUntil( "Selection:" )
//writer.println("10")
//readUntil( "Selection:" )
//writer.println("3")
//x = readUntil( "Return" )
//while (!x.contains('SYDNEY')) {
//   writer.println()
//    x = readUntil( "Return" )
//}
//m = (x =~ /(?sm).*(SYDNEY.*?)$/)
telnet.disconnect()
//println m[0][1]
