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);
// Temp
/*
var models = require("./lib/models.js");
var network = new models.Network({host: "irc.network.org"});
@ -26,5 +27,6 @@ network.get("channels").add(new models.Channel({
users: [
new models.User({name: "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")
// Sort windows by `data-id` value.
.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 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({
defaults: {
time: moment().format("HH:mm"),
user: "user",
text: "text"
user: "",
text: ""
}
});
models.MessageCollection = Backbone.Collection.extend({
model: models.Message
});
models.Channel = Backbone.Model.extend({
defaults: {
type: "channel",
name: "",
topic: "",
users: [],
messages: []
topic: ""
},
initialize: function() {
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({
defaults: {
host: "",
nick: "default_username",
connect: false
},
initialize: function() {
this.set({
id: id++,
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({
type: "network",
name: this.get("host")
@ -64,12 +83,15 @@ models.NetworkCollection = Backbone.Collection.extend({
host: "Lobby"
}));
},
getChannel: function(id) {
find: function(id) {
var networks = this.models;
for (var i = 0; i < networks.length; i++) {
var find = networks[i].get("channels").findWhere({id: id});
if (find) {
return find;
return {
network: networks[i],
channel: find
};
}
}
}

View File

@ -1,8 +1,8 @@
var connect = require("connect");
var io = require("socket.io");
var io = require("socket.io");
// Local library
var models = require("./models.js");
var models = require("./models.js");
module.exports = Server;
@ -19,7 +19,10 @@ Server.prototype.listen = function(port) {
this.sockets = io.listen(http).sockets;
this.sockets.on("connection", function(socket) {
init.call(self, socket);
socket.emit(
"event",
self.networks
);
socket.on(
"input",
function(input) {
@ -31,22 +34,57 @@ Server.prototype.listen = function(port) {
return this;
};
function init(socket) {
socket.emit(
"event",
this.networks
)
}
function handleUserInput(input) {
var channel = this.networks.getChannel(input.id);
if (channel) {
channel.get("messages").push(
var target = this.networks.find(input.id);
if (!target) {
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})
);
this.sockets.emit(
"event",
this.networks
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(
"event",
this.networks
);
}