Skip to content

Instantly share code, notes, and snippets.

@RyanCopley
Created February 20, 2013 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyanCopley/6004c3ce372e060bbf18 to your computer and use it in GitHub Desktop.
Save RyanCopley/6004c3ce372e060bbf18 to your computer and use it in GitHub Desktop.
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs")
port = process.argv[2] || 12345;
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('chatdb.sqlite');
var newchat = db.prepare("INSERT INTO `messages` (channel,nick,msg,date) VALUES(?,?,?,?);");
/*
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
console.log(row.id + ": " + row.info);
});
*/
var irc = require('irc');
var channels = ['#test'];
var nick = 'testnick';
var client = new irc.Client('irc.slashnet.org', nick, {
channels: channels,
});
client.addListener('message', function (from, to, message) {
console.log(from + ' => ' + to + ': ' + message);
newchat.run(to,from,message,Math.round(new Date().getTime() / 1000));
});
client.addListener('pm', function (from, message) {
console.log(from + ' => ME: ' + message);
});
http.createServer(function (req, res) {
this.res = res;
var that = this;
res.writeHead(200, {'Content-Type': 'text/plain'});
var vars = url.parse(req.url, true);
var q = vars.query;
var fcn = vars.pathname.substr(1);
switch (fcn){
case "join":
console.log("Trying to join with this str: "+q.str);
client.join(q.str);
break;
case "send":
newchat.run(q.chan,nick,q.msg,Math.round(new Date().getTime() / 1000));
client.say(q.chan, q.msg);
break;
case "receive":
q.chan = q.chan.replace("'","\'");
q.chan = q.chan.replace('"','\"');
q.since = q.since.replace("'","\'");
q.since = q.since.replace('"','\"');
var msgs = [];
that.res.write("["); // works
db.each("SELECT * FROM messages WHERE channel = '"+q.chan+"' AND id > "+q.since+" ORDER BY id", function(err, row) {
res.write("Found row!"); //does not work
that.res.write("Found row!"); //does not work
console.log("Found row!");
});
res.write("]");//works
break;
}
res.end();
}).listen(port);
console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment