hardlounge/lib/server.js

362 lines
7.1 KiB
JavaScript
Raw Normal View History

2014-03-04 16:31:52 +00:00
var connect = require("connect");
2014-03-09 21:22:37 +00:00
var io = require("socket.io");
2014-03-06 15:11:25 +00:00
2014-03-07 21:24:02 +00:00
// Local library
2014-03-09 21:22:37 +00:00
var models = require("./models.js");
2014-03-06 15:11:25 +00:00
2014-03-07 21:24:02 +00:00
module.exports = Server;
2014-03-06 15:11:25 +00:00
2014-03-07 21:24:02 +00:00
function Server() {
this.sockets = false;
this.networks = new models.NetworkCollection;
}
2014-03-06 15:11:25 +00:00
2014-03-07 21:24:02 +00:00
Server.prototype.listen = function(port) {
var self = this;
2014-03-06 15:11:25 +00:00
var http = connect()
.use(connect.static("client"))
.listen(port);
2014-03-11 22:57:29 +00:00
this.networks.on(
"all",
2014-03-12 13:10:53 +00:00
function(type, data) {
2014-03-13 00:38:20 +00:00
if (type != "users" && type != "messages") {
type = "networks";
data = self.networks;
2014-03-12 13:10:53 +00:00
}
2014-03-13 00:38:20 +00:00
self.sockets
.emit(type, data);
2014-03-11 22:57:29 +00:00
}
);
2014-03-12 15:09:37 +00:00
this.sockets = io
.listen(http, {log: false})
2014-03-12 15:09:37 +00:00
.sockets;
2014-03-07 21:24:02 +00:00
this.sockets.on("connection", function(socket) {
2014-03-09 21:22:37 +00:00
socket.emit(
2014-03-12 13:10:53 +00:00
"networks",
2014-03-09 21:22:37 +00:00
self.networks
);
2014-03-09 19:27:44 +00:00
socket.on(
"input",
function(input) {
2014-03-09 23:14:22 +00:00
handleInput.call(self, input);
2014-03-09 19:27:44 +00:00
}
);
2014-03-07 21:24:02 +00:00
});
2014-03-06 18:02:43 +00:00
2014-03-07 21:24:02 +00:00
return this;
};
2014-03-06 18:02:43 +00:00
2014-03-09 23:14:22 +00:00
function handleInput(input) {
2014-03-09 21:22:37 +00:00
var target = this.networks.find(input.id);
if (!target) {
return;
}
2014-03-09 21:22:37 +00:00
var argv = input.text.substr(1).split(" ");
var cmd = input.text.charAt(0) == "/" ? argv[0].toUpperCase()
: "";
switch (cmd) {
case "":
var irc = target.network.irc;
if (typeof irc !== "undefined") {
2014-03-13 00:00:59 +00:00
var chan = target.channel.get("name");
irc.say(chan, input.text);
}
2014-03-09 21:22:37 +00:00
target.channel.get("messages").add(
new models.Message({
user: target.network.get("nick"),
text: input.text
})
2014-03-09 19:27:44 +00:00
);
2014-03-09 21:22:37 +00:00
break;
2014-03-13 00:00:59 +00:00
case "NOTICE":
var irc = target.network.irc;
if (argv[2] && typeof irc !== "undefined") {
var user = argv[1];
var text = argv
.slice(2)
.join(" ");
irc.notice(user, text);
target.channel.get("messages").add(
new models.Message({
user: target.network.get("nick"),
text: "to " + user + ": " + text
})
);
}
break;
case "QUERY":
var irc = target.network.irc;
if (argv[1] && typeof irc !== "undefined") {
var channels = target.network.get("channels");
if (argv[1].charAt(0) != "#" && !channels.findWhere({name: argv[1]})) {
channels.add(
new models.Channel({
name: argv[1]
})
);
}
}
break;
2014-03-12 13:10:53 +00:00
case "JOIN":
2014-03-12 15:09:37 +00:00
var irc = target.network.irc;
if (argv[1] && typeof irc !== "undefined") {
irc.join(argv[1]);
}
break;
case "PART":
var irc = target.network.irc;
if (typeof irc !== "undefined") {
irc.part(argv[1] ? argv[1] : target.channel.get("name"));
2014-03-12 13:10:53 +00:00
}
break;
2014-03-12 18:06:08 +00:00
case "NAMES":
var irc = target.network.irc;
if (typeof irc !== "undefined") {
2014-03-12 21:58:24 +00:00
var channel = argv[1] ? argv[1] : target.channel.get("name");
irc.send("NAMES", channel);
}
break;
2014-03-13 00:38:20 +00:00
case "NICK":
var irc = target.network.irc;
if (argv[1] && typeof irc !== "undefined") {
irc.send("NICK", argv[1]);
}
break;
2014-03-12 21:58:24 +00:00
case "TOPIC":
var irc = target.network.irc;
if (typeof irc !== "undefined") {
if (!argv[1] || argv[1].charAt(0) != "#") {
argv.splice(1, 0, target.channel.get("name"));
}
var topic = argv.slice(2).join(" ");
irc.send.apply(
irc,
argv.length == 2 ? argv
: argv.slice(0, 2).concat(topic)
);
2014-03-12 18:06:08 +00:00
}
break;
2014-03-12 15:09:37 +00:00
case "SERVER":
2014-03-09 21:22:37 +00:00
case "CONNECT":
2014-03-12 15:09:37 +00:00
if (argv[1]) {
var network = this.networks.add(
new models.Network({host: argv[1]})
);
network.irc.addListener(
"raw",
function() {
handleEvent.apply(network, arguments);
}
);
2014-03-09 23:14:22 +00:00
}
break;
2014-03-09 21:22:37 +00:00
2014-03-12 15:09:37 +00:00
case "QUIT":
2014-03-09 23:14:22 +00:00
case "DISCONNECT":
this.networks.remove(target.network);
2014-03-09 21:22:37 +00:00
break;
default:
target.channel.get("messages").add(
new models.Message({text: "Command `/" + cmd + "` does not exist."})
2014-03-09 19:27:44 +00:00
);
2014-03-09 21:22:37 +00:00
break;
2014-03-09 19:27:44 +00:00
}
2014-03-09 23:14:22 +00:00
}
2014-03-09 21:22:37 +00:00
2014-03-09 23:14:22 +00:00
function handleEvent(argv) {
2014-03-12 15:09:37 +00:00
var network = this;
var channels = network.get("channels");
var event = argv.command;
switch (event) {
case "PRIVMSG":
var target = argv.args[0];
if (target.charAt(0) != "#") {
target = argv.nick;
}
var channel = channels.findWhere({name: target});
var message = argv.args[1];
if (typeof channel == "undefined") {
channel = channels.add(
new models.Channel({
name: target
})
);
}
channel.get("messages").add(
new models.Message({
user: argv.nick,
text: message
})
);
break;
2014-03-13 00:00:59 +00:00
case "NOTICE":
var from = argv.nick ? argv.nick : argv.prefix;
var message = new models.Message({
user: from,
text: "notice: " + argv.args[1]
});
2014-03-13 00:38:20 +00:00
channels.each(function(channel) {
channel.get("messages").add(message);
2014-03-13 00:00:59 +00:00
});
break;
2014-03-12 15:09:37 +00:00
case "JOIN":
if (argv.nick == network.get("nick")) {
channels.add(
2014-03-12 18:06:08 +00:00
new models.Channel({
name: argv.args[0]
})
2014-03-12 15:09:37 +00:00
);
} else {
var channel = channels.findWhere({name: argv.args[0]});
var users = channel.get("users");
users.add(
new models.User({
name: argv.nick
})
);
var messages = channel.get("messages");
messages.add(
new models.Message({
user: argv.nick,
text: "has joined the channel."
})
);
}
break;
case "PART":
var channel = channels.findWhere({name: argv.args[0]});
if (argv.nick == network.get("nick")) {
channels.remove(channel);
} else {
var users = channel.get("users");
users.remove(
users.findWhere({
name: argv.nick
})
);
var messages = channel.get("messages");
messages.add(
new models.Message({
user: argv.nick,
text: "has left the channel."
})
);
}
break;
2014-03-12 18:06:08 +00:00
2014-03-13 00:38:20 +00:00
case "NICK":
var message = new models.Message({
user: argv.nick,
text: "changed name to " + argv.args[0]
});
if (network.get("nick") == argv.nick) {
network.set("nick", argv.nick);
}
channels.each(function(channel) {
var user = channel.get("users").findWhere({name: argv.nick});
if (typeof user !== "undefined") {
user.set("name", argv.args[0]);
channel.get("messages").add(message);
}
});
break;
2014-03-12 21:58:24 +00:00
case "TOPIC":
2014-03-13 00:38:20 +00:00
var channel = channels.findWhere({name: argv.args[0]});
2014-03-12 21:58:24 +00:00
var messages = channel.get("messages");
messages.add(
new models.Message({
user: argv.nick,
text: "changed the topic of " + argv.args[0] + " to: "
+ argv.args[1]
})
);
break;
case "rpl_notopic":
case "rpl_topic":
var channel = channels.findWhere({name: argv.args[1]});
if (typeof channel !== "undefined") {
var messages = channel.get("messages");
messages.add(
new models.Message({
text: "Topic for " + argv.args[1] + ": "
+ argv.args[2]
})
);
}
break;
2014-03-12 18:06:08 +00:00
case "rpl_namreply":
var names = argv.args[3].split(' ');
var channel = network.get("channels").findWhere({name: argv.args[2]});
var users = channel.get("users");
if (names[0] == network.get("nick")) {
users.reset();
}
for (var i in names) {
users.add(
new models.User({name: names[i]}), {
silent: true
}
);
}
break;
case "rpl_endofnames":
var channel = network.get("channels").findWhere({name: argv.args[1]});
var users = channel.get("users");
users.trigger(
"add", {}, users
);
break;
2014-03-13 00:38:20 +00:00
case "err_nicknameinuse":
2014-03-12 21:58:24 +00:00
case "err_chanoprivsneeded":
case "err_notonchannel":
case "err_nosuchchannel":
case "err_cannotsendtochan":
case "err_nosuchnick":
2014-03-13 00:00:59 +00:00
channels.first().get("messages").add(
new models.Message({
2014-03-13 00:38:20 +00:00
text: argv.args.slice(2).join(" ")
})
);
break;
2014-03-13 00:00:59 +00:00
default:
channels.first().get("messages").add(
new models.Message({
user: "-!-",
2014-03-13 00:38:20 +00:00
text: argv.args.slice(1).join(" ")
2014-03-13 00:00:59 +00:00
})
);
2014-03-12 15:09:37 +00:00
}
2014-03-11 22:57:29 +00:00
// Debug
console.log(argv);
2014-03-09 19:27:44 +00:00
}