Skip to content

Instantly share code, notes, and snippets.

@adam-singer
Created April 21, 2012 06:18
Show Gist options
  • Save adam-singer/2434695 to your computer and use it in GitHub Desktop.
Save adam-singer/2434695 to your computer and use it in GitHub Desktop.
simple dart bot that joins irc.freenode.net/#dart
#import("dart:io");
#import("dart:utf");
#import('dart:json');
#import('dart:isolate', prefix:'isolate');
String API_KEY = "INSERT_KEY_HERE";
class Bot {
Socket socket;
bool sendNick = true;
String botName = "dartBot";
String botChannel = "#dartnews";
String ircServer = "irc.freenode.net";
String superUser = ':ficode!~ficode@gateway/tor-sasl/ficode';
Bot() {
socket = new Socket(ircServer,6667);
socket.onError = (e) {};
socket.onConnect = () {};
socket.onData = () {
var available = socket.available();
if (available > 0) {
ByteArray buffer = new ByteArray(available);
int numBytes = socket.readList(buffer, 0, available);
String data = decodeUtf8(buffer, 0, numBytes);
if (!sendNick) {
List<String> server_response = data.split(' ');
String command_reply = server_response[0];
switch (command_reply) {
case 'PING':
ircPong(server_response[1]);
break;
case superUser:
if (server_response[1] == "PRIVMSG") {
if (server_response[3] == ":printnews\r\n") {
isolate.spawnFunction(isolated_dart_news_stream).call("").then((Map m) {
m.forEach((k,String v) {
v = v.replaceAll('\r\n', "").replaceAll('\r', "").replaceAll("\n", "");
var send_to_channel = encodeUtf8("PRIVMSG ${botChannel} :${v} \r\n");
socket.writeList(send_to_channel, 0, send_to_channel.length);
});
});
}
}
break;
}
}
}
};
socket.onWrite = () {
if (sendNick) {
var nick = encodeUtf8("NICK ${botName}\r\n");
var user = encodeUtf8("USER ${botName} ${ircServer} bla :${botName}\r\n");
socket.writeList(nick, 0, nick.length);
socket.writeList(user, 0, user.length);
var join = encodeUtf8("JOIN ${botChannel}\r\n");
socket.writeList(join, 0, join.length);
sendNick = false;
}
};
}
ircPong(String s) {
var pongEncoded = encodeUtf8("PONG ${s.trim()}");
socket.writeList(pongEncoded, 0, pongEncoded.length);
}
}
void isolated_dart_news_stream() {
isolate.port.receive((msg, reply) {
dart_news_stream().then((m) {
reply.send(m);
print(m); /// send news
isolate.port.close();
});
});
}
void isolate_goo_gl_shortner() {
isolate.port.receive((msg, reply) {
goo_gl_shortner(msg).then((m) {
reply.send(m);
print(m);
isolate.port.close();
});
});
}
void isolate_goog_gl_shortner_proc() {
isolate.port.receive((msg, reply) {
var dart_news = {};
var args = ["-X", "POST","https://www.googleapis.com/urlshortener/v1/url",
"-H","Content-Type: application/json", "-d",'{"longUrl": "${msg["url"]}"}'];
Process short_url_proc = new Process.run("curl", args, new ProcessOptions(),
(errorCode, stdout, stderr) {
if (errorCode == 0) {
Map short_url_map = JSON.parse(stdout);
dart_news[short_url_map["id"]] = "g+/#dartlang: ${msg["title"].trim()} ${short_url_map["id"]}";
}
reply.send(dart_news);
isolate.port.close();
});
});
}
Future goo_gl_shortner(dart_items) {
Completer completer = new Completer();
Map dart_news = {};
List<Future> shortner_procs = new List<Future>();
dart_items.forEach((item) => shortner_procs.add(isolate.spawnFunction(isolate_goog_gl_shortner_proc).call(item)));
Futures.wait(shortner_procs).transform((List<Map> values) {
values.forEach((Map m) => m.getKeys().forEach((k) => dart_news[k] = m[k]));
completer.complete(dart_news);
});
return completer.future;
}
Future dart_news_stream() {
Completer complete = new Completer();
// Since we do not have https in HttpClient, going to run with curl for now.
String activity_query = "%23dartlang";
String gplus_url = "https://www.googleapis.com/plus/v1/activities?query=${activity_query}&pp=1&key=${API_KEY}";
Map dart_news = {};
Process p = new Process.run("curl", [gplus_url], new ProcessOptions(),
(exitCode, stdout, stderr) {
if (exitCode == 0) {
Map dart_gplus_activities = JSON.parse(stdout);
List<Map> dart_items = dart_gplus_activities["items"];
isolate.spawnFunction(isolate_goo_gl_shortner).call(dart_items).then((Map m) => complete.complete(m));
}
});
return complete.future;
}
void main() {
Bot b = new Bot();
}
@abourget
Copy link

Sweet! I've just ported it to Dart M4:

https://gist.github.com/abourget/5603331

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment