use v6;

warn 'Init';

my $socket = IO::Socket::INET.new;

warn 'Created socket';

$socket.open('irc.freenode.net', 6667);

warn 'opened connection';

$socket.send: qq{USER littletestbot "" "" :littletestbot\r\n};

warn 'sent USER';

$socket.send("NICK imaltb\r\n");

warn 'sent NICK';

$socket.send("JOIN #perl6\r\n");

warn 'sent JOIN';

loop {
	warn 'init loop';

	my $buf = '';

	warn 'cleared buffer';

	until $buf ~~ / \r\n $ / {
		warn 'start of until';
		$buf ~= $socket.recv(1);
		warn 'recv';
	}
	say $buf;
	my $msg = $buf.split(' ');
	warn 'split buf';
	if $msg.shift eq 'PING' {
		warn 'sending ping';
		$socket.send("PONG {$msg.shift}\r\n");
		warn 'sent ping';
	}
	warn 'end of loop';
}
