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) {
|
|
|
|
var stream = net.connect({
|
|
|
|
port: 6667,
|
|
|
|
host: host,
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.on("error", function(e) {
|
|
|
|
console.log(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
var client = irc(stream);
|
2014-03-29 23:59:28 +00:00
|
|
|
var network = networks.add({
|
|
|
|
host: host,
|
|
|
|
client: client,
|
|
|
|
}, {silent: true});
|
|
|
|
|
|
|
|
networks.trigger("network", networks);
|
2014-03-24 15:47:29 +00:00
|
|
|
|
|
|
|
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-29 23:59:28 +00:00
|
|
|
case "join":
|
|
|
|
if (args[1]) {
|
|
|
|
network.get("client").join(args[1]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "part":
|
|
|
|
if (args[1]) {
|
|
|
|
network.get("client").part(args[1]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-03-12 15:09:37 +00:00
|
|
|
}
|
2014-03-24 15:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function event(event, data) {
|
2014-03-29 23:59:28 +00:00
|
|
|
var channels = this.get("channels");
|
|
|
|
|
2014-03-24 15:47:29 +00:00
|
|
|
switch (event) {
|
|
|
|
|
|
|
|
case "away":
|
|
|
|
console.log(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "join":
|
2014-03-29 23:59:28 +00:00
|
|
|
var chan = channels.findWhere({name: data.channel}) || channels.add({name: data.channel});
|
|
|
|
chan.addUser({name: data.nick});
|
|
|
|
chan.addMessage({
|
|
|
|
user: data.nick,
|
|
|
|
type: "join",
|
|
|
|
});
|
2014-03-24 15:47:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "kick":
|
|
|
|
console.log(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "names":
|
2014-03-29 23:59:28 +00:00
|
|
|
var chan = channels.findWhere({name: data.channel});
|
|
|
|
chan.get("users").reset(_.map(data.names, function(n) { return {name: n}; }));
|
2014-03-24 15:47:29 +00:00
|
|
|
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":
|
2014-03-29 23:59:28 +00:00
|
|
|
var len = data.channels.length;
|
|
|
|
var name = data.nick;
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
var chan = channels.findWhere({name: data.channels[i]});
|
|
|
|
if (name == this.get("client").me) {
|
|
|
|
channels.remove(chan);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var users = channel.get("users");
|
|
|
|
users.remove(users.findWhere({name: name}));
|
|
|
|
chan.addMessage({
|
|
|
|
user: data.nick,
|
|
|
|
type: "part",
|
|
|
|
});
|
|
|
|
}
|
2014-03-24 15:47:29 +00:00
|
|
|
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":
|
2014-03-29 23:59:28 +00:00
|
|
|
channels.first().addMessage({message: "Connected."});
|
2014-03-24 15:47:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "whois":
|
|
|
|
console.log(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug
|
|
|
|
console.log(data);
|
2014-03-24 13:44:41 +00:00
|
|
|
}
|