Connect to IRC networks

This commit is contained in:
Mattias Erming 2014-03-09 22:22:37 +01:00
parent 04b2455aaf
commit ea2d20e118
4 changed files with 88 additions and 26 deletions

4
app.js
View File

@ -12,6 +12,7 @@ var server =
.listen(PORT); .listen(PORT);
// Temp // Temp
/*
var models = require("./lib/models.js"); var models = require("./lib/models.js");
var network = new models.Network({host: "irc.network.org"}); var network = new models.Network({host: "irc.network.org"});
@ -26,5 +27,6 @@ network.get("channels").add(new models.Channel({
users: [ users: [
new models.User({name: "user"}), new models.User({name: "user"}),
new models.User({name: "other_user"}), new models.User({name: "other_user"}),
] ]:
})); }));
*/

View File

@ -31,7 +31,7 @@ $(function() {
}) })
); );
chat.find(".messages").sticky(); chat.find(".messages").sticky().scrollToBottom();
chat.find(".window") chat.find(".window")
// Sort windows by `data-id` value. // Sort windows by `data-id` value.
.sort(function(a, b) { return ($(a).data("id") - $(b).data("id")); }) .sort(function(a, b) { return ($(a).data("id") - $(b).data("id")); })

View File

@ -1,3 +1,4 @@
var irc = require("irc");
var Backbone = require("backbone"); var Backbone = require("backbone");
var moment = require("moment"); var moment = require("moment");
@ -14,25 +15,33 @@ models.User = Backbone.Model.extend({
} }
}); });
models.UserCollection = Backbone.Collection.extend({
model: models.User
});
models.Message = Backbone.Model.extend({ models.Message = Backbone.Model.extend({
defaults: { defaults: {
time: moment().format("HH:mm"), time: moment().format("HH:mm"),
user: "user", user: "",
text: "text" text: ""
} }
}); });
models.MessageCollection = Backbone.Collection.extend({
model: models.Message
});
models.Channel = Backbone.Model.extend({ models.Channel = Backbone.Model.extend({
defaults: { defaults: {
type: "channel", type: "channel",
name: "", name: "",
topic: "", topic: ""
users: [],
messages: []
}, },
initialize: function() { initialize: function() {
this.set({ this.set({
id: id++ id: id++,
users: new models.UserCollection(),
messages: new models.MessageCollection()
}); });
} }
}); });
@ -44,12 +53,22 @@ models.ChannelCollection = Backbone.Collection.extend({
models.Network = Backbone.Model.extend({ models.Network = Backbone.Model.extend({
defaults: { defaults: {
host: "", host: "",
nick: "default_username",
connect: false
}, },
initialize: function() { initialize: function() {
this.set({ this.set({
id: id++, id: id++,
channels: new models.ChannelCollection() channels: new models.ChannelCollection()
}); });
if (this.get("connect")) {
this.conn = new irc.Client(
this.get("host"),
this.get("nick"), {
channels: ["#testchan"]
}
);
}
this.get("channels").add(new models.Channel({ this.get("channels").add(new models.Channel({
type: "network", type: "network",
name: this.get("host") name: this.get("host")
@ -64,12 +83,15 @@ models.NetworkCollection = Backbone.Collection.extend({
host: "Lobby" host: "Lobby"
})); }));
}, },
getChannel: function(id) { find: function(id) {
var networks = this.models; var networks = this.models;
for (var i = 0; i < networks.length; i++) { for (var i = 0; i < networks.length; i++) {
var find = networks[i].get("channels").findWhere({id: id}); var find = networks[i].get("channels").findWhere({id: id});
if (find) { if (find) {
return find; return {
network: networks[i],
channel: find
};
} }
} }
} }

View File

@ -19,7 +19,10 @@ Server.prototype.listen = function(port) {
this.sockets = io.listen(http).sockets; this.sockets = io.listen(http).sockets;
this.sockets.on("connection", function(socket) { this.sockets.on("connection", function(socket) {
init.call(self, socket); socket.emit(
"event",
self.networks
);
socket.on( socket.on(
"input", "input",
function(input) { function(input) {
@ -31,22 +34,57 @@ Server.prototype.listen = function(port) {
return this; return this;
}; };
function init(socket) {
socket.emit(
"event",
this.networks
)
}
function handleUserInput(input) { function handleUserInput(input) {
var channel = this.networks.getChannel(input.id); var target = this.networks.find(input.id);
if (channel) { if (!target) {
channel.get("messages").push( return;
}
var argv = input.text.substr(1).split(" ");
var cmd = input.text.charAt(0) == "/" ? argv[0].toUpperCase()
: "";
switch (cmd) {
case "":
target.channel.get("messages").add(
new models.Message({user: "user", text: input.text}) new models.Message({user: "user", text: input.text})
); );
break;
case "CONNECT":
var network = new models.Network({
host: "irc.freenode.org",
connect: true
});
this.networks.add(network);
var messages = network.get("channels").at(0).get("messages");
messages.add(
new models.Message({text: "Connecting..."})
);
var self = this;
network.conn.addListener("raw", function(argv) {
messages.add(
new models.Message({user: argv.args[0], text: argv.args[1]})
);
self.sockets.emit(
"event",
self.networks
);
});
break;
default:
target.channel.get("messages").add(
new models.Message({text: "Command `/" + cmd + "` does not exist."})
);
break;
}
this.sockets.emit( this.sockets.emit(
"event", "event",
this.networks this.networks
); );
}
} }