Shuo/lib/server.js

250 lines
4.5 KiB
JavaScript

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");
var sockets = null;
var networks = new models.Networks;
var events = [
"join",
"kick",
"message",
"names",
"nick",
"notice",
"part",
"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);
});
networks.on("all", function() {
sockets.emit.apply(sockets, arguments);
});
};
function connect(host) {
var stream = net.connect({
port: 6667,
host: host,
});
stream.on("error", function(e) {
console.log(e);
});
var client = irc(stream);
var network = networks.add({
host: host,
client: client,
}, {silent: true});
networks.trigger("network", networks);
client.nick("t_user");
client.user("t_user", "temp user");
events.forEach(function(e) {
client.on(e, function(data) {
event.apply(network, [e, data]);
});
});
}
function input(json) {
var target = networks.find(json.id);
if (!target) {
return;
}
var network = target.network;
var channel = target.channel;
var client = network.get("client");
var id = json.id;
var text = json.text;
var args = text.replace(/^\//, '').split(" ");
var cmd = text.charAt(0) == "/" ? args[0].toLowerCase() : "";
switch (cmd) {
case "":
args.unshift(
"msg",
channel.get("name")
);
case "msg":
var user;
var message = _.tail(args, 2).join(" ");
if (client) {
user = client.me;
client.send(args[1], message);
}
channel.addMessage({
from: user,
message: message,
});
break;
case "server":
case "connect":
if (args[1]) {
connect(args[1]);
}
break;
case "join":
case "part":
case "nick":
if (client && args[1]) {
client[cmd](args[1]);
}
break;
case "topic":
if (client && args[1]) {
var str = "TOPIC " + args[1];
if (args[2]) {
str += " :" + args.slice(2).join(" ");
}
client.write(str);
}
break;
case "quit":
case "disconnect":
networks.remove(network);
networks.trigger("network", networks);
break;
}
}
function event(event, data) {
var channels = this.get("channels");
switch (event) {
case "join":
var chan = channels.findWhere({name: data.channel}) || channels.add({name: data.channel});
chan.addUser({name: data.nick});
chan.addMessage({
from: data.nick,
type: "join",
});
break;
case "kick":
console.log(event);
break;
case "message":
var chan = channels.findWhere({name: data.to}) || channels.add({type: "query", name: data.from});
chan.addMessage({
from: data.from,
message: data.message
});
break;
case "names":
var chan = channels.findWhere({name: data.channel});
chan.get("users").reset(_.map(data.names, function(n) { return {name: n}; }));
break;
case "nick":
channels.each(function(chan) {
var users = chan.get("users");
var user = users.findWhere({name: data.nick});
if (!user) {
return;
}
user.set("name", data["new"]);
users.trigger("change", {}, users);
chan.addMessage({
from: data.nick,
message: data["new"],
type: "nick",
});
});
break;
case "notice":
channels.first().addMessage(data);
break;
case "part":
var i = data.channels.length;
while (i--) {
var chan = channels.findWhere({name: data.channels[i]});
if (data.nick == this.get("client").me) {
channels.remove(chan);
return;
}
var users = chan.get("users");
users.remove(users.findWhere({name: data.nick}));
chan.addMessage({
from: data.nick,
type: "part",
});
}
break;
case "quit":
channels.each(function(chan) {
var users = chan.get("users");
var user = users.findWhere({name: data.nick});
if (user) {
users.remove(user);
chan.addMessage({
from: data.nick,
type: "quit",
});
}
});
break;
case "topic":
var chan = channels.findWhere({name: data.channel});
var from = data.nick;
if (from == this.get("client").me) {
from = data.channel;
}
chan.addMessage({
from: from,
message: data.topic,
type: "topic",
});
break;
case "welcome":
channels.first().addMessage({message: "Connected."});
break;
case "whois":
// ..
break;
}
// Debug
console.log(event + ":");
console.log(data);
}