Added node-irc

This commit is contained in:
Mattias Erming 2014-03-06 10:02:43 -08:00
parent 4cc34d6456
commit 7f3656e458
6 changed files with 122 additions and 35 deletions

View File

@ -27,22 +27,25 @@ h2 {
border-right: 1px solid #ccc; border-right: 1px solid #ccc;
float: left; float: left;
height: 100%; height: 100%;
line-height: 20px; line-height: 36px;
width: 199px; width: 199px;
} }
#sidebar .channel { #sidebar .channel {
clear: both; border-bottom: 1px solid transparent;
color: #f00; color: #f00;
cursor: pointer; cursor: pointer;
float: left; padding: 0 12px;
margin-left: 20px; }
#sidebar .channel[data-id='0'] {
background: #eaeaea;
border-bottom-color: #e5e5e5;
}
#sidebar .channel:first-child {
color: #333;
overflow: hidden;
} }
#sidebar .channel:hover { #sidebar .channel:hover {
color: #999; text-decoration: underline;
}
#sidebar .channel.network {
margin-top: 10px;
overflow: hidden;
} }
#chat { #chat {
bottom: 0; bottom: 0;

View File

@ -14,7 +14,7 @@
{{#networks}} {{#networks}}
<div class="network" data-id="{{id}}"> <div class="network" data-id="{{id}}">
{{#channels}} {{#channels}}
<div class="channel {{type}}" data-id="{{id}}"> <div class="channel" data-id="{{id}}">
{{name}} {{name}}
</div> </div>
{{/channels}} {{/channels}}

View File

@ -3,6 +3,7 @@ $(function() {
socket.on( socket.on(
"event", "event",
function(event) { function(event) {
console.log(event);
View[event.action](event); View[event.action](event);
} }
); );
@ -18,7 +19,7 @@ $(function() {
var View = {}; var View = {};
View.redraw = function(event) { View.refresh = function(event) {
if (event.data == undefined || event.data == []) { if (event.data == undefined || event.data == []) {
return; return;
} }

View File

@ -1,3 +1,5 @@
var _ = require("lodash");
var models = exports; var models = exports;
var id = 0; var id = 0;
@ -8,6 +10,10 @@ models.Network = function() {
this.channels = []; this.channels = [];
}; };
models.Network.prototype.toJSON = function() {
return _.omit(this, "irc");
};
models.Channel = function() { models.Channel = function() {
this.id = id++; this.id = id++;
this.name = ""; this.name = "";

View File

@ -1,5 +1,6 @@
var _ = require("lodash"); var _ = require("lodash");
var connect = require("connect"); var connect = require("connect");
var irc = require("irc");
var io = require("socket.io"); var io = require("socket.io");
var models = require("./models.js"); var models = require("./models.js");
@ -8,14 +9,7 @@ exports.listen = listen;
var sockets; var sockets;
var networks = []; var networks = [];
var network = new models.Network; addNetwork("Lobby", false);
var chan = _.assign(new models.Channel, {
name: "Network",
type: "network"
});
network.channels.push(chan);
networks.push(network);
function listen(port) { function listen(port) {
var http = connect() var http = connect()
@ -25,39 +19,121 @@ function listen(port) {
sockets.on("connection", function(socket) { sockets.on("connection", function(socket) {
init(socket); init(socket);
}); });
}; }
function init(socket) { function init(socket) {
refresh();
socket.on( socket.on(
"input", "input",
function(input) { function(input) {
handleUserInput(input) handleUserInput(input)
} }
); );
}
function refresh() {
if (typeof sockets === "undefined") {
return;
}
sockets.emit("event", _.assign(new models.Event, { sockets.emit("event", _.assign(new models.Event, {
action: "redraw", action: "refresh",
data: networks data: networks
})); }));
}; }
function handleUserInput(input) { function handleUserInput(input) {
var id = input.id;
var text = input.text; var text = input.text;
var target = getChannel(input.id);
if (text.charAt(0) != "/") {
return addMessage(target, text);
}
var args = text.substr(1).split(" ");
switch (args[0]) {
case "connect":
if (typeof args[1] !== "undefined") {
addNetwork(args[1], true);
}
break;
case "join":
console.log(args);
if (typeof args[1] === "undefined") {
return;
}
target.network.channels.push(
_.assign(new models.Channel, {
name: args[1]
})
);
refresh();
break;
default:
console.log("DEFAULT");
addMessage(
target,
"Command '/" + args[0] + "' does not exist."
);
break;
}
}
function addNetwork(addr, bool) {
bool = bool || false;
var chan = _.assign(new models.Channel, {name: addr, type: "network"});
var network = _.assign(
new models.Network, {channels: [chan]}
);
if (bool) {
console.log("IRC");
network.irc = new irc.Client(addr, "default_user", {
channels: ["#default_channel"]
});
network.irc.addListener("raw", function() {
handleEvent.apply(this, [network].concat(arguments));
});
}
networks.push(network);
refresh();
}
function handleEvent(network) {
var args = arguments;
var target = {
network: network,
channel: network.channels[0]
};
console.log(args[1]);
addMessage(target, args[1][0].args);
}
function addMessage(target, text) {
var message = _.assign(new models.Message, {text: text}); var message = _.assign(new models.Message, {text: text});
var event = _.assign(new models.Event, { target.channel.messages.push(message);
sockets.emit("event", _.assign(new models.Event, {
action: "add", action: "add",
type: "message", type: "message",
data: message, target: target.channel.id,
target: id data: message
}); }));
}
sockets.emit("event", event); function getChannel(id) {
for (var i = 0; i < networks.length; i++) {
_.each(networks, function(n) { var find = {
var chan = _.findWhere(n.channels, {id: id}); network: networks[i],
if (chan !== "undefined") { channel: _.findWhere(networks[i].channels, {id: id})
chan.messages.push(message); };
if (typeof find.channel !== "undefined") {
return find;
} }
}); }
}; }

View File

@ -7,6 +7,7 @@
"dependencies": { "dependencies": {
"commander": "2.1.0", "commander": "2.1.0",
"connect": "2.13.0", "connect": "2.13.0",
"irc": "0.3.6",
"lodash": "2.4.1", "lodash": "2.4.1",
"socket.io": "0.9.16" "socket.io": "0.9.16"
} }