Created
April 22, 2012 00:44
-
-
Save Davis-Desormeaux/2440553 to your computer and use it in GitHub Desktop.
simple dart bot that joins irc.freenode.net/#dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import("dart:io"); | |
#import("dart:utf"); | |
class Bot { | |
Socket socket; | |
bool sendNick = true; | |
Bot() { | |
socket = new Socket("irc.freenode.net",6667); | |
socket.onError = (e) { | |
print("onError: ${e}"); | |
}; | |
socket.onConnect = () { | |
print("onConnect: "); | |
}; | |
socket.onData = () { | |
print("onData: "); | |
var available = socket.available(); | |
if (available > 0) { | |
ByteArray buffer = new ByteArray(available); | |
int numBytes = socket.readList(buffer, 0, available); | |
print("numBytes = ${numBytes}"); | |
String data = decodeUtf8(buffer, 0, numBytes); | |
print("data = ${data}"); | |
if (!sendNick) { | |
List<String> server_response = data.split(' '); | |
print("server_response = ${server_response}"); | |
String command_reply = server_response[0]; | |
print("command_reply = ${command_reply}"); | |
switch (command_reply) { | |
case 'PING': | |
print("responding to ping"); | |
ircPong(server_response[1]); | |
break; | |
} | |
} | |
} | |
}; | |
socket.onWrite = () { | |
print("onWrite: "); | |
if (sendNick) { | |
String n = "NICK dartBot\r\n"; | |
String u = "USER dartBot irc.freenode.net bla :dartBot\r\n"; | |
var nn = encodeUtf8(n); | |
var uu = encodeUtf8(u); | |
socket.writeList(nn, 0, nn.length); | |
socket.writeList(uu, 0, uu.length); | |
String j = "JOIN #dart\r\n"; | |
var jj = encodeUtf8(j); | |
socket.writeList(jj, 0, jj.length); | |
sendNick = false; | |
} | |
}; | |
} | |
ircPong(String s) { | |
print("ircPong(String ${s})"); | |
StringBuffer sb = new StringBuffer(); | |
String p = s.trim(); | |
sb.add("PONG ${p}"); | |
String pong = sb.toString(); | |
print ("ircPong = ${pong}"); | |
var pp = encodeUtf8(pong); | |
socket.writeList(pp, 0, pp.length); | |
} | |
} | |
void main() { | |
Bot b = new Bot(); | |
print("Hello World"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment