+
{{name}}
{{/channels}}
diff --git a/client/js/chat.js b/client/js/chat.js
index de6f01f..ca9a153 100644
--- a/client/js/chat.js
+++ b/client/js/chat.js
@@ -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;
}
diff --git a/lib/models.js b/lib/models.js
index 8372180..8a08dca 100644
--- a/lib/models.js
+++ b/lib/models.js
@@ -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 = "";
diff --git a/lib/server.js b/lib/server.js
index b32154c..16ec433 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -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
- });
+ target: target.channel.id,
+ data: message
+ }));
+}
- sockets.emit("event", event);
-
- _.each(networks, function(n) {
- var chan = _.findWhere(n.channels, {id: id});
- if (chan !== "undefined") {
- chan.messages.push(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;
}
- });
-};
+ }
+}
diff --git a/package.json b/package.json
index 6e80df3..6755da5 100644
--- a/package.json
+++ b/package.json
@@ -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"
}