This commit is contained in:
Mattias Erming 2014-04-25 01:57:51 +02:00
parent bea3136078
commit cdad2f64d9
2 changed files with 40 additions and 37 deletions

View File

@ -8,7 +8,7 @@
<meta name="viewport" content="width=device-width, user-scalable=no"> <meta name="viewport" content="width=device-width, user-scalable=no">
<link rel="stylesheet" href="/style.css"> <link rel="stylesheet" href="/style.css">
<link rel="stylesheet" href="<%= typeof theme !== 'undefined' ? theme : "" %>"> <link rel="stylesheet" href="<%= typeof theme !== 'undefined' ? theme : '' %>">
</head> </head>
<body> <body>

View File

@ -45,9 +45,7 @@ function listen() {
var self = this; var self = this;
sockets = io.listen(app, {log: 0}).sockets.on("connection", function(s) { sockets = io.listen(app, {log: 0}).sockets.on("connection", function(s) {
s.on("input", input); s.on("input", input);
s.emit("networks", { s.emit("networks", {networks: networks});
networks: networks
});
}); });
(config.networks || []).forEach(function(n) { (config.networks || []).forEach(function(n) {
@ -56,30 +54,27 @@ function listen() {
} }
function index(req, res, next) { function index(req, res, next) {
if (req.url == "/" || req.url == "/index.html") { if (req.url != "/") return next();
var data = _.extend( fs.readFile("client/index.html", function(err, file) {
var data = _.merge(
require("../package.json"), require("../package.json"),
config config
); );
fs.readFile( res.end(_.template(
"client/index.html", file,
function(e, file) { data
res.end(_.template(file, data)); ));
} });
);
} else {
next();
}
} }
function connect(params) { function connect(params) {
params = _.extend( _.defaults(
config.defaults || {}, params,
params config.defaults
); );
var host = params.host; var host = params.host;
var port = params.port; var port = params.port || 6667;
var stream = net.connect({ var stream = net.connect({
port: port, port: port,
@ -97,9 +92,7 @@ function connect(params) {
}); });
networks.push(network); networks.push(network);
sockets.emit("networks", { sockets.emit("networks", {networks: networks});
networks: networks
});
client.nick(params.nick); client.nick(params.nick);
client.user(params.nick, params.realname); client.user(params.nick, params.realname);
@ -243,11 +236,9 @@ function input(data) {
case "quit": case "quit":
case "disconnect": case "disconnect":
if (client) { if (client) {
client.quit();
networks = _.without(networks, network); networks = _.without(networks, network);
sockets.emit("networks", { sockets.emit("networks", {networks: networks});
networks: networks client.quit();
});
} }
break; break;
} }
@ -289,6 +280,10 @@ function event(e, data) {
case "kick": case "kick":
var chan = _.findWhere(channels, {name: data.channel}); var chan = _.findWhere(channels, {name: data.channel});
if (typeof chan === "undefined") {
// TODO: Throw error
break;
}
if (data.client == this.client.me) { if (data.client == this.client.me) {
chan.users = []; chan.users = [];
} else { } else {
@ -375,6 +370,10 @@ function event(e, data) {
case "names": case "names":
var chan = _.findWhere(channels, {name: data.channel}); var chan = _.findWhere(channels, {name: data.channel});
if (typeof chan === "undefined") {
// TODO: Throw error
break;
}
chan.users = []; chan.users = [];
data.names.forEach(function(n) { data.names.forEach(function(n) {
chan.users.push(new User({name: n})); chan.users.push(new User({name: n}));
@ -437,6 +436,10 @@ function event(e, data) {
case "part": case "part":
var chan = _.findWhere(channels, {name: data.channels[0]}); var chan = _.findWhere(channels, {name: data.channels[0]});
if (typeof chan === "undefined") {
// TODO: Throw error
break;
}
if (data.nick == this.client.me) { if (data.nick == this.client.me) {
remove(chan.id); remove(chan.id);
sockets.emit("part", { sockets.emit("part", {
@ -485,6 +488,10 @@ function event(e, data) {
case "topic": case "topic":
var chan = _.findWhere(channels, {name: data.channel}); var chan = _.findWhere(channels, {name: data.channel});
if (typeof chan === "undefined") {
// TODO: Throw error
break;
}
var from = data.nick || chan.name; var from = data.nick || chan.name;
var msg = new Msg({ var msg = new Msg({
type: "topic", type: "topic",
@ -546,21 +553,17 @@ function event(e, data) {
} }
} }
// Utils
function find(id) { function find(id) {
var result = false; for (var i = 0; i < networks.length; i++) {
networks.forEach(function(n) { var result = {
result = { network: networks[i],
network: n, chan: _.findWhere(networks[i].channels, {id: id}),
chan: _.findWhere(n.channels, {id: id}),
}; };
if (!result.chan) { if (result.chan) {
result = false;
}
});
return result; return result;
} }
}
}
function remove(id) { function remove(id) {
networks.forEach(function(n) { networks.forEach(function(n) {