Remove code

This commit is contained in:
Mattias Erming 2014-03-24 14:44:41 +01:00
parent b298681b4e
commit dc0add0fef
6 changed files with 94 additions and 621 deletions

View File

@ -1,6 +1,6 @@
$(function() {
var socket = io.connect("");
$.each(["NETWORKS", "CHANNELS", "MESSAGES", "USERS"], function(i, type) {
$.each(["networks", "channels", "messages", "users"], function(i, type) {
socket.on(type, function(json) {
event(type, json);
});
@ -18,7 +18,7 @@ $(function() {
function event(type, json) {
switch (type) {
case "NETWORKS":
case "networks":
var html = "";
json.forEach(function(network) {
html += render("#window", network);
@ -37,7 +37,7 @@ $(function() {
.bringToTop();
break;
case "CHANNELS":
case "channels":
var target = json.target;
if (json.action == "remove") {
$("[data-id='" + json.data.id + "']").remove();
@ -65,7 +65,7 @@ $(function() {
.scrollGlue({animate: 400});
break;
case "USERS":
case "users":
var target;
if (typeof json.target !== "undefined") {
target = chat.find(".window[data-id='" + json.target + "']");
@ -75,7 +75,7 @@ $(function() {
target.html(render("#user", {users: json.data}));
break;
case "MESSAGES":
case "messages":
var target;
if (typeof json.target !== "undefined") {
target = chat.find(".window[data-id='" + json.target + "']");

View File

@ -1,10 +0,0 @@
module.exports = {
port: 9000,
nick: "temp_name",
fullname: "Temporary Name",
autoConnect: true,
host: "irc.freenode.org",
channels: [
"#temp_chan"
],
};

View File

@ -1,3 +1 @@
var server =
new (require("./lib/server.js"))()
.listen();
require("./lib/server")();

View File

@ -3,9 +3,6 @@ var _ = require("lodash");
var Backbone = require("backbone");
var moment = require("moment");
// Local library
var config = require("./../config.js");
var models =
module.exports =
{};
@ -19,8 +16,7 @@ models.User = Backbone.Model.extend({
}
});
var x = 1;
models.UserCollection = Backbone.Collection.extend({
models.Users = Backbone.Collection.extend({
model: models.User,
sort: function(options) {
this.models = _.sortBy(
@ -55,7 +51,7 @@ models.Message = Backbone.Model.extend({
}
});
models.MessageCollection = Backbone.Collection.extend({
models.Messages = Backbone.Collection.extend({
model: models.Message
});
@ -69,7 +65,7 @@ models.Channel = Backbone.Model.extend({
id: id++
});
this.set("messages", new models.MessageCollection());
this.set("messages", new models.Messages());
this.get("messages").on("all", function(action, data) {
this.trigger("MESSAGES", {
target: this.get("id"),
@ -79,7 +75,7 @@ models.Channel = Backbone.Model.extend({
});
}, this);
this.set("users", new models.UserCollection());
this.set("users", new models.Users());
this.get("users").on("all", function(action) {
this.trigger("USERS", {
target: this.get("id"),
@ -91,21 +87,21 @@ models.Channel = Backbone.Model.extend({
}
});
models.ChannelCollection = Backbone.Collection.extend({
models.Channels = Backbone.Collection.extend({
model: models.Channel
});
models.Network = Backbone.Model.extend({
defaults: {
host: "",
nick: config.nick
nick: "nick"
},
initialize: function() {
this.set({
id: id++
});
this.set("channels", new models.ChannelCollection());
this.set("channels", new models.Channels());
this.get("channels").on("all", function(action, data) {
if (action == "USERS" || action == "MESSAGES") {
this.trigger(action, data);
@ -123,32 +119,10 @@ models.Network = Backbone.Model.extend({
type: "network",
name: this.get("host")
}));
},
connect: function(channels) {
var client = new irc.Client(
this.get("host"),
this.get("nick"), {
fullname: config.fullname,
channels: channels
}
);
this.irc = client;
this.irc.addListener("error", function() {
// ..
});
this.on("remove", function() {
if (typeof this.irc !== "undefined") {
this.irc.disconnect();
}
});
return this.irc;
}
});
models.NetworkCollection = Backbone.Collection.extend({
models.Networks = Backbone.Collection.extend({
model: models.Network,
initialize: function() {
this.add(new models.Network({

View File

@ -1,58 +1,32 @@
var connect = require("connect");
var _ = require("lodash");
var io = require("socket.io");
// Local libraries
var config = require("./../config.js");
var models = require("./models.js");
// Local library
var models = require("./models");
module.exports = Server;
var sockets;
var networks = new models.Networks;
function Server() {
this.sockets = false;
this.networks = new models.NetworkCollection;
module.exports = function server(options) {
var app = connect().use(connect.static("client")).listen(9000);
sockets = io.listen(app, {log: 0}).sockets;
sockets.on("connection", function(s) {
init(s);
});
networks.on("all", function(e) {
sockets.emit("networks", networks);
});
};
function init(socket) {
socket.on("input", function(json) { clientInput(json); });
sockets.emit("networks", networks);
}
Server.prototype.listen = function() {
var self = this;
var http = connect()
.use(connect.static("client"))
.listen(config.port);
this.networks.on("all", function(action, data) {
this.sockets.emit(action, data);
}, this);
this.sockets = io.listen(http, {log: false}).sockets;
this.sockets.on("connection", function(socket) {
socket.on("input", function(input) { handleInput.call(self, input); });
socket.emit("NETWORKS", self.networks);
});
if (config.autoConnect) {
this.connect(config.host, config.channels);
}
return this;
};
Server.prototype.connect = function(host, channels) {
var network = new models.Network({
host: host
});
this.networks.add(network, {silent: true});
this.networks.trigger(
"NETWORKS",
this.networks
);
network.connect(channels).addListener("raw", function() {
handleEvent.apply(network, arguments);
});
};
function handleInput(input) {
var target = this.networks.find(input.id);
function clientInput(json) {
var target = networks.find(json.id);
if (!target) {
return;
}
@ -60,502 +34,38 @@ function handleInput(input) {
var network = target.network;
var channel = target.channel;
var argv = input.text.substr(1).split(" ");
var cmd = input.text.charAt(0) == "/" ? argv[0].toUpperCase()
: "MESSAGE";
var id = json.id;
var text = json.text;
var args = text.replace(/^\//, '').split(" ");
var cmd = text.charAt(0) == "/" ? args[0].toLowerCase() : "";
switch (cmd) {
case "MESSAGE":
var irc = network.irc;
if (typeof irc !== "undefined") {
irc.say(target.channel.get("name"), input.text);
}
channel.get("messages").add(
new models.Message({
user: network.get("nick"),
text: input.text
})
);
break;
case "SERVER":
case "CONNECT":
this.connect(argv[1]);
break;
case "QUIT":
case "DISCONNECT":
this.networks.remove(network);
break;
case "JOIN":
case "PART":
var irc = network.irc;
if (typeof irc !== "undefined") {
irc.send(cmd, argv[1] || target.channel.get("name"));
}
break;
case "UNQUERY":
case "LEAVE":
var channel = target.channel;
if (channel.get("name").charAt(0) == "#") {
handleInput.call(this, {
id: input.id,
text: "/part"
});
} else if (channel.get("type") != "network") {
network.get("channels").remove(
channel
);
}
break;
case "NOTICE":
var irc = network.irc;
if (!argv[2] || typeof irc === "undefined") {
break;
}
var user = argv[1];
var text = argv.slice(2).join(" ");
irc.notice(user, text);
channel.get("messages").add(
new models.Message({
text: "Notice to " + user + ": " + text,
type: "notice"
})
);
break;
case "TOPIC":
var irc = network.irc;
if (typeof irc === "undefined") {
break;
}
var params = [
"topic",
case "":
args.unshift(
"msg",
channel.get("name")
];
if (argv[1]) {
var text = argv.slice(1).join(" ");
params.push(text);
}
irc.send.apply(
irc,
params
);
case "msg":
var m = new models.Message({text: _.tail(args, 2)});
channel.get("messages").add(m);
break;
case "NICK":
var irc = network.irc;
if (typeof irc !== "undefined") {
irc.send("NICK", argv[1] || config.nick);
}
break;
case "KICK":
var irc = network.irc;
if (argv[1] && typeof irc !== "undefined") {
irc.send(
"KICK",
target.channel.get("name"),
argv[1]
);
}
break;
case "QUERY":
case "WHOIS":
var irc = network.irc;
if (typeof irc !== "undefined") {
irc.send("WHOIS", argv[1] || target.channel.get("name"));
}
break;
case "OP":
case "DEOP":
case "VOICE":
case "DEVOICE":
case "MODE":
var irc = network.irc;
if (!argv[1] && typeof irc === "undefined") {
case "server":
case "connect":
if (!args[1]) {
break;
}
var mode;
var user;
if (cmd != "MODE") {
user = argv[1];
mode = {
"OP": "+o",
"DEOP": "-o",
"VOICE": "+v",
"DEVOICE": "-v"
}[cmd];
} else if (!argv[2]) {
break;
} else {
mode = argv[1];
user = argv[2];
}
irc.send(
"MODE",
target.channel.get("name"),
mode,
user
);
var n = new models.Network({host: args[1]});
networks.add(n);
break;
// This command is used for debugging purposes.
// Send raw commands.
case "SEND":
var irc = network.irc;
if (typeof irc !== "undefined") {
irc.send.apply(irc, argv.splice(1));
}
break;
default:
channel.get("messages").add(
new models.Message({
text: cmd + " Unknown command",
type: "error"
})
);
case "quit":
case "disconnect":
networks.remove(network);
break;
}
}
function handleEvent(argv) {
var network = this;
var channels = network.get("channels");
var event = argv.commandType != "error" ? argv.command
: "ERROR";
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({
type: "query",
name: target
})
);
}
var mode;
var user = channel.get("users").findWhere({name: argv.nick});
if (typeof user !== "undefined") {
mode = user.get("mode");
}
channel.get("messages").add(
new models.Message({
mode: mode,
user: argv.nick,
text: message
})
);
break;
case "NOTICE":
var from = argv.nick ? argv.nick : "-!-";
channels.first().get("messages").add(new models.Message({
user: from,
text: argv.args[1],
type: "notice"
}));
break;
case "JOIN":
var channel = channels.findWhere({name: argv.args[0]});
if (typeof channel === "undefined") {
channel = channels.add(
new models.Channel({
name: argv.args[0]
})
);
}
var users = channel.get("users");
var messages = channel.get("messages");
if (argv.nick != network.get("nick")) {
users.add(
new models.User({
name: argv.nick
})
);
users.sort();
}
messages.add(
new models.Message({
user: argv.nick,
text: channel.get("name"),
type: "join"
})
);
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: channel.get("name"),
type: "part"
})
);
}
break;
case "NICK":
channels.each(function(channel) {
var users = channel.get("users");
var messages = channel.get("messages");
var user = users.findWhere({name: argv.nick});
if (typeof user === "undefined") {
return;
}
user.set("name", argv.args[0]);
users.sort();
users.trigger(
"change", {}, users
);
messages.add(new models.Message({
user: argv.nick,
text: argv.args[0],
type: "nick"
}));
});
if (argv.nick == network.get("nick")) {
network.set({nick: argv.args[0]}, {silent: true});
}
break;
case "KICK":
var channel = channels.findWhere({
name: argv.args[0]
});
var users = channel.get("users");
var messages = channel.get("messages");
if (argv.args[1] == network.get("nick")) {
users.reset();
} else {
users.remove(
users.findWhere({
name: argv.args[1]
})
);
}
messages.add(
new models.Message({
user: argv.args[1],
text: "was kicked by " + argv.nick,
type: "kick"
})
);
break;
case "QUIT":
var message = new models.Message({
user: argv.nick,
type: "quit"
});
channels.each(function(channel) {
var user = channel.get("users").findWhere({name: argv.nick});
if (typeof user !== "undefined") {
channel.get("users").remove(user);
channel.get("messages").add(message);
}
});
break;
case "MODE":
var channel = channels.findWhere({name: argv.args[0]});
if (typeof channel === "undefined") {
break;
}
var target = argv.args[2] || argv.args[0];
var messages = channel.get("messages");
messages.add(
new models.Message({
user: argv.nick,
text: argv.args[1] + " " + target,
type: "mode"
})
);
var irc = network.irc;
if (argv.args[2] && typeof irc !== "undefined") {
irc.send(
"NAMES",
channel.get("name")
);
}
break;
case "TOPIC":
var channel = channels.findWhere({name: argv.args[0]});
var messages = channel.get("messages");
messages.add(
new models.Message({
user: argv.nick,
text: "changed topic to: " + argv.args[1],
type: "topic"
})
);
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],
type: "topic"
})
);
}
break;
case "rpl_namreply":
var channel = network.get("channels").findWhere({name: argv.args[2]});
var users = channel.get("users");
if (typeof channel === "undefined") {
channel = channels.add(
new models.Channel({
name: argv.args[2]
})
);
}
if (typeof rpl_namreply === "undefined") {
channel.get("users").reset(undefined, {silent: true});
}
// This variable is global but will be deleted when
// rpl_endofnames is triggered..
rpl_namreply = true;
var names = argv.args[3].split(' ');
for (var i in names) {
var mode = (names[i].match(/^\W/) || "").toString();
var user = new models.User({
mode: mode,
name: names[i].replace(mode, '')
});
users.add(
user,
{silent: true}
);
}
break;
case "rpl_endofnames":
var channel = channels.findWhere({name: argv.args[1]});
var users = channel.get("users");
// .. here!
delete rpl_namreply;
users.sort();
users.trigger(
"add", {}, users
);
break;
case "001": // 'registered' event
network.set("nick", argv.args[0]);
case "rpl_motdstart":
case "rpl_endofmotd":
case "rpl_motd":
channels.first().get("messages").add(
new models.Message({
user: "-!-",
text: argv.args[1]
})
);
break;
case "rpl_whoisuser":
case "rpl_whoischannels":
case "rpl_whoisserver":
case "rpl_endofwhois":
var channel = channels.findWhere({name: argv.args[1]});
var message = argv.args.slice(2).join(" ");
if (typeof channel == "undefined") {
channel = channels.add(
new models.Channel({
type: "query",
name: argv.args[1]
})
);
}
channel.get("messages").add(
new models.Message({
user: argv.args[1],
text: message
})
);
break;
case "ERROR":
var args = argv.args;
channels.first().get("messages").add(
new models.Message({
text: args[args.length - 2].toUpperCase() + " " + args[args.length - 1],
type: "error"
})
);
break;
}
// Debug
console.log(argv);
}

View File

@ -1,32 +1,33 @@
{
"name": "chat",
"description": "Web client/server IRC chat",
"version": "0.1.0",
"author": {
"name": "Mattias Erming",
"email": "mattias@mattiaserming.com"
},
"dependencies": {
"backbone": "1.1.2",
"connect": "2.13.0",
"irc": "0.3.6",
"lodash": "2.4.1",
"moment": "2.5.1",
"socket.io": "0.9.16"
},
"keywords": [
"chat",
"irc",
"server",
"web",
"browser"
],
"main": "./index.js",
"scripts": {
"start": "nodemon index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/erming/chat.git"
}
"name": "chat",
"description": "Web client/server IRC chat",
"version": "0.1.0",
"author": {
"name": "Mattias Erming",
"email": "mattias@mattiaserming.com"
},
"dependencies": {
"backbone": "1.1.2",
"connect": "2.13.0",
"irc": "0.3.6",
"lodash": "2.4.1",
"moment": "2.5.1",
"socket.io": "0.9.16",
"slate-irc": "~0.4.0"
},
"keywords": [
"chat",
"irc",
"server",
"web",
"browser"
],
"main": "./index.js",
"scripts": {
"start": "nodemon index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/erming/chat.git"
}
}