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;
float: left;
height: 100%;
line-height: 20px;
line-height: 36px;
width: 199px;
}
#sidebar .channel {
clear: both;
border-bottom: 1px solid transparent;
color: #f00;
cursor: pointer;
float: left;
margin-left: 20px;
padding: 0 12px;
}
#sidebar .channel[data-id='0'] {
background: #eaeaea;
border-bottom-color: #e5e5e5;
}
#sidebar .channel:first-child {
color: #333;
overflow: hidden;
}
#sidebar .channel:hover {
color: #999;
}
#sidebar .channel.network {
margin-top: 10px;
overflow: hidden;
text-decoration: underline;
}
#chat {
bottom: 0;

View File

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

View File

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

View File

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

View File

@ -1,5 +1,6 @@
var _ = require("lodash");
var connect = require("connect");
var irc = require("irc");
var io = require("socket.io");
var models = require("./models.js");
@ -8,14 +9,7 @@ exports.listen = listen;
var sockets;
var networks = [];
var network = new models.Network;
var chan = _.assign(new models.Channel, {
name: "Network",
type: "network"
});
network.channels.push(chan);
networks.push(network);
addNetwork("Lobby", false);
function listen(port) {
var http = connect()
@ -25,39 +19,121 @@ function listen(port) {
sockets.on("connection", function(socket) {
init(socket);
});
};
}
function init(socket) {
refresh();
socket.on(
"input",
function(input) {
handleUserInput(input)
}
);
}
function refresh() {
if (typeof sockets === "undefined") {
return;
}
sockets.emit("event", _.assign(new models.Event, {
action: "redraw",
action: "refresh",
data: networks
}));
};
}
function handleUserInput(input) {
var id = input.id;
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 event = _.assign(new models.Event, {
target.channel.messages.push(message);
sockets.emit("event", _.assign(new models.Event, {
action: "add",
type: "message",
data: message,
target: id
});
sockets.emit("event", event);
_.each(networks, function(n) {
var chan = _.findWhere(n.channels, {id: id});
if (chan !== "undefined") {
chan.messages.push(message);
target: target.channel.id,
data: message
}));
}
});
function getChannel(id) {
for (var i = 0; i < networks.length; i++) {
var find = {
network: networks[i],
channel: _.findWhere(networks[i].channels, {id: id})
};
if (typeof find.channel !== "undefined") {
return find;
}
}
}

View File

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