2014-03-24 15:47:29 +00:00
|
|
|
var http = require("connect");
|
|
|
|
var io = require("socket.io");
|
|
|
|
var irc = require("slate-irc");
|
|
|
|
var models = require(__dirname + "/models");
|
|
|
|
var net = require("net");
|
|
|
|
var _ = require("lodash");
|
2014-03-06 15:11:25 +00:00
|
|
|
|
2014-03-24 16:47:14 +00:00
|
|
|
var sockets = null;
|
2014-03-24 13:44:41 +00:00
|
|
|
var networks = new models.Networks;
|
2014-03-06 15:11:25 +00:00
|
|
|
|
2014-03-24 15:47:29 +00:00
|
|
|
var events = [
|
|
|
|
"away",
|
|
|
|
"join",
|
|
|
|
"kick",
|
|
|
|
"names",
|
|
|
|
"nick",
|
|
|
|
"notice",
|
|
|
|
"part",
|
|
|
|
"pong",
|
|
|
|
"privmsg",
|
|
|
|
"quit",
|
|
|
|
"topic",
|
|
|
|
"welcome",
|
|
|
|
"whois"
|
|
|
|
];
|
|
|
|
|
|
|
|
module.exports = function listen() {
|
|
|
|
var app = http()
|
|
|
|
.use(http.static("client"))
|
|
|
|
.listen(9000);
|
|
|
|
|
|
|
|
sockets = io.listen(app, {log: 0}).sockets.on("connection", function(s) {
|
|
|
|
s.on("input", input);
|
|
|
|
sockets.emit("network", networks);
|
2014-03-13 17:50:02 +00:00
|
|
|
});
|
2014-03-24 13:44:41 +00:00
|
|
|
|
2014-03-24 15:47:29 +00:00
|
|
|
networks.on("all", function() {
|
|
|
|
sockets.emit.apply(sockets, arguments);
|
2014-03-13 17:50:02 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-03-24 15:47:29 +00:00
|
|
|
function connect(host) {
|
2014-03-24 16:47:14 +00:00
|
|
|
var network = networks.add({host: host}, {silent: true});
|
2014-03-24 15:47:29 +00:00
|
|
|
networks.trigger("network", networks);
|
|
|
|
|
|
|
|
var stream = net.connect({
|
|
|
|
port: 6667,
|
|
|
|
host: host,
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.on("error", function(e) {
|
|
|
|
console.log(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
var client = irc(stream);
|
|
|
|
|
|
|
|
client.nick("t_user");
|
|
|
|
client.user("t_user", "temp user");
|
|
|
|
|
|
|
|
events.forEach(function(e) {
|
|
|
|
client.on(e, function(data) {
|
|
|
|
event.apply(network, [e, data]);
|
|
|
|
});
|
|
|
|
});
|
2014-03-24 13:44:41 +00:00
|
|
|
}
|
|
|
|
|
2014-03-24 15:47:29 +00:00
|
|
|
function input(json) {
|
2014-03-24 13:44:41 +00:00
|
|
|
var target = networks.find(json.id);
|
2014-03-09 21:22:37 +00:00
|
|
|
if (!target) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-12 19:45:41 +00:00
|
|
|
|
2014-03-13 15:25:01 +00:00
|
|
|
var network = target.network;
|
|
|
|
var channel = target.channel;
|
2014-03-24 13:44:41 +00:00
|
|
|
|
|
|
|
var id = json.id;
|
|
|
|
var text = json.text;
|
|
|
|
|
|
|
|
var args = text.replace(/^\//, '').split(" ");
|
|
|
|
var cmd = text.charAt(0) == "/" ? args[0].toLowerCase() : "";
|
|
|
|
|
2014-03-09 21:22:37 +00:00
|
|
|
switch (cmd) {
|
2014-03-24 13:44:41 +00:00
|
|
|
|
|
|
|
case "":
|
|
|
|
args.unshift(
|
|
|
|
"msg",
|
2014-03-13 15:25:01 +00:00
|
|
|
channel.get("name")
|
|
|
|
);
|
2014-03-29 15:36:12 +00:00
|
|
|
case "msg":
|
|
|
|
channel.addMessage({
|
|
|
|
message: _.tail(args, 2).join(" ")
|
|
|
|
});
|
2014-03-24 13:44:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "server":
|
|
|
|
case "connect":
|
2014-03-24 15:47:29 +00:00
|
|
|
if (args[1]) {
|
|
|
|
connect(args[1]);
|
2014-03-14 15:12:01 +00:00
|
|
|
}
|
2014-03-12 18:06:08 +00:00
|
|
|
break;
|
2014-03-24 13:44:41 +00:00
|
|
|
|
|
|
|
case "quit":
|
|
|
|
case "disconnect":
|
|
|
|
networks.remove(network);
|
2014-03-12 19:45:41 +00:00
|
|
|
break;
|
2014-03-24 13:44:41 +00:00
|
|
|
|
2014-03-12 15:09:37 +00:00
|
|
|
}
|
2014-03-24 15:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function event(event, data) {
|
|
|
|
var network = this;
|
|
|
|
var channels = network.get("channels");
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
|
|
|
|
case "away":
|
|
|
|
console.log(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "join":
|
|
|
|
console.log(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "kick":
|
|
|
|
console.log(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "names":
|
|
|
|
console.log(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "nick":
|
|
|
|
console.log(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "notice":
|
2014-03-24 16:47:14 +00:00
|
|
|
channels.first().addMessage(data);
|
2014-03-24 15:47:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "part":
|
|
|
|
console.log(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "pong":
|
|
|
|
console.log(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "privmsg":
|
|
|
|
console.log(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "quit":
|
|
|
|
console.log(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "topic":
|
|
|
|
console.log(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "welcome":
|
|
|
|
console.log(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "whois":
|
|
|
|
console.log(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug
|
|
|
|
console.log(data);
|
2014-03-24 13:44:41 +00:00
|
|
|
}
|