hardlounge/lib/server.js

301 lines
5.5 KiB
JavaScript
Raw Normal View History

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 = [
"join",
"kick",
2014-04-03 20:55:51 +00:00
"motd",
2014-03-30 03:12:29 +00:00
"message",
2014-03-24 15:47:29 +00:00
"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);
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) {
2014-04-02 15:24:33 +00:00
client.on(e, function() {
event.apply(network, [e, arguments]);
2014-03-24 15:47:29 +00:00
});
});
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-13 15:25:01 +00:00
var network = target.network;
var channel = target.channel;
2014-03-24 13:44:41 +00:00
2014-03-30 03:12:29 +00:00
var client = network.get("client");
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":
2014-03-30 03:12:29 +00:00
var user;
var message = _.tail(args, 2).join(" ");
if (client) {
user = client.me;
client.send(args[1], message);
}
2014-03-29 15:36:12 +00:00
channel.addMessage({
2014-03-30 03:12:29 +00:00
from: user,
message: message,
2014-03-29 15:36:12 +00:00
});
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
2014-03-29 23:59:28 +00:00
case "join":
2014-03-30 03:12:29 +00:00
case "part":
case "nick":
if (client && args[1]) {
client[cmd](args[1]);
2014-03-29 23:59:28 +00:00
}
break;
2014-03-30 03:12:29 +00:00
case "topic":
if (client && args[1]) {
var str = "TOPIC " + args[1];
if (args[2]) {
str += " :" + args.slice(2).join(" ");
}
client.write(str);
2014-03-29 23:59:28 +00:00
}
break;
2014-04-02 15:24:33 +00:00
case "query":
case "whois":
if (client && args[1]) {
client["whois"](args[1]);
}
break;
case "kick":
break;
2014-03-30 03:12:29 +00:00
case "quit":
case "disconnect":
networks.remove(network);
networks.trigger("network", networks);
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 "join":
2014-04-02 15:24:33 +00:00
var chan = channels.findWhere({name: data[0].channel}) || channels.add({name: data[0].channel});
chan.addUser({name: data[0].nick});
2014-03-29 23:59:28 +00:00
chan.addMessage({
2014-04-02 15:24:33 +00:00
from: data[0].nick,
2014-03-29 23:59:28 +00:00
type: "join",
});
2014-03-24 15:47:29 +00:00
break;
case "kick":
2014-04-03 20:55:51 +00:00
break;
case "motd":
var messages = data[0].motd;
messages.forEach(function(msg) {
channels.first().addMessage({message: msg});
});
2014-03-24 15:47:29 +00:00
break;
2014-03-30 03:12:29 +00:00
case "message":
2014-04-02 15:24:33 +00:00
var chan = channels.findWhere({name: data[0].to}) || channels.add({type: "query", name: data[0].from});
2014-03-30 03:12:29 +00:00
chan.addMessage({
2014-04-02 15:24:33 +00:00
from: data[0].from,
message: data[0].message
2014-03-30 03:12:29 +00:00
});
break;
2014-03-24 15:47:29 +00:00
case "names":
2014-04-02 15:24:33 +00:00
var chan = channels.findWhere({name: data[0].channel});
chan.get("users").reset(
_.map(data[0].names, function(n) {
return {name: n};
})
);
2014-03-24 15:47:29 +00:00
break;
case "nick":
2014-04-02 15:24:33 +00:00
if (data[0].new == this.get("client").me) {
channels.first().addMessage({
message: "You're now known as " + data[0]["new"],
});
}
2014-03-30 03:12:29 +00:00
channels.each(function(chan) {
var users = chan.get("users");
2014-04-02 15:24:33 +00:00
var user = users.findWhere({name: data[0].nick});
2014-03-30 03:12:29 +00:00
if (!user) {
return;
}
2014-04-02 15:24:33 +00:00
user.set("name", data[0]["new"]);
2014-03-30 03:12:29 +00:00
users.trigger("change", {}, users);
chan.addMessage({
2014-04-02 15:24:33 +00:00
from: data[0].nick,
message: data[0]["new"],
2014-03-30 03:12:29 +00:00
type: "nick",
});
});
2014-03-24 15:47:29 +00:00
break;
case "notice":
2014-04-02 15:24:33 +00:00
if (data[0].to = "*") {
data[0].from = "-!-";
}
channels.first().addMessage(data[0]);
2014-03-24 15:47:29 +00:00
break;
case "part":
2014-04-02 15:24:33 +00:00
var i = data[0].channels.length;
2014-03-30 03:12:29 +00:00
while (i--) {
2014-04-02 15:24:33 +00:00
var chan = channels.findWhere({name: data[0].channels[i]});
if (data[0].nick == this.get("client").me) {
2014-03-29 23:59:28 +00:00
channels.remove(chan);
return;
}
2014-03-30 03:12:29 +00:00
var users = chan.get("users");
2014-04-02 15:24:33 +00:00
users.remove(users.findWhere({name: data[0].nick}));
2014-03-29 23:59:28 +00:00
chan.addMessage({
2014-04-02 15:24:33 +00:00
from: data[0].nick,
2014-03-29 23:59:28 +00:00
type: "part",
});
}
2014-03-24 15:47:29 +00:00
break;
case "quit":
2014-03-30 03:12:29 +00:00
channels.each(function(chan) {
var users = chan.get("users");
2014-04-02 15:24:33 +00:00
var user = users.findWhere({name: data[0].nick});
2014-03-30 03:12:29 +00:00
if (user) {
users.remove(user);
chan.addMessage({
2014-04-02 15:24:33 +00:00
from: data[0].nick,
2014-03-30 03:12:29 +00:00
type: "quit",
});
}
});
2014-03-24 15:47:29 +00:00
break;
case "topic":
2014-04-02 15:24:33 +00:00
var chan = channels.findWhere({name: data[0].channel});
var from = data[0].nick;
2014-03-30 03:12:29 +00:00
if (from == this.get("client").me) {
2014-04-02 15:24:33 +00:00
from = data[0].channel;
2014-03-30 03:12:29 +00:00
}
chan.addMessage({
from: from,
2014-04-02 15:24:33 +00:00
message: data[0].topic,
2014-03-30 03:12:29 +00:00
type: "topic",
});
2014-03-24 15:47:29 +00:00
break;
case "welcome":
2014-04-02 15:24:33 +00:00
channels.first().addMessage([
{message: "You're now connected to " + this.get("host")},
{message: "You're now known as " + data[0]}
]);
2014-03-24 15:47:29 +00:00
break;
case "whois":
2014-04-02 15:24:33 +00:00
// If this value is null, an error has occured.
if (data[1] == null) {
channels.first().addMessage({
type: "error",
message: data[0]
});
break;
}
var name = data[1].nickname;
var chan = channels.findWhere({name: name}) || channels.add({type: "query", name: name});
var i = 0;
for (var k in data[1]) {
if (i++ == 5) break;
chan.addMessage({
message: k + ": " + data[1][k]
});
}
2014-03-24 15:47:29 +00:00
break;
}
// Debug
2014-04-02 15:24:33 +00:00
console.log(event);
2014-03-24 15:47:29 +00:00
console.log(data);
2014-04-02 15:24:33 +00:00
}