Replace node-irc with slate-irc

This commit is contained in:
Mattias Erming 2014-03-24 16:47:29 +01:00
parent dc0add0fef
commit d88967e712
5 changed files with 163 additions and 67 deletions

View File

@ -90,7 +90,7 @@
</div> </div>
<div class="users"> <div class="users">
{{#each users}} {{#each users}}
<a href="{{name}}" class="user" style="color: {{color name}}"> <a href="{{name}}" class="user">
{{mode}}{{name}} {{mode}}{{name}}
</a> </a>
{{/each}} {{/each}}
@ -99,8 +99,8 @@
{{#each messages}} {{#each messages}}
<div class="message {{type}}"> <div class="message {{type}}">
<span class="time">{{time}}</span> <span class="time">{{time}}</span>
<a href="{{user}}" class="user" style="color: {{color user}}">{{mode}}{{user}}</a> <a href="{{user}}" class="user">{{mode}}{{user}}</a>
<span class="text">{{type}} {{{link text}}}</span> <span class="text">{{type}} {{{link message}}}</span>
</div> </div>
{{/each}} {{/each}}
</div> </div>
@ -114,7 +114,7 @@
<script type="text/html" id="user"> <script type="text/html" id="user">
{{#users}} {{#users}}
<a href="{{name}}" class="user" style="color: {{color name}}"> <a href="{{name}}" class="user">
{{mode}}{{name}} {{mode}}{{name}}
</a> </a>
{{/users}} {{/users}}
@ -124,8 +124,8 @@
{{#messages}} {{#messages}}
<div class="message {{type}}"> <div class="message {{type}}">
<span class="time">{{time}}</span> <span class="time">{{time}}</span>
<a href="{{user}}" class="user" style="color: {{color user}}">{{mode}}{{user}}</a> <a href="{{user}}" class="user">{{mode}}{{user}}</a>
<span class="text">{{type}} {{{link text}}}</span> <span class="text">{{type}} {{{link message}}}</span>
</div> </div>
{{/messages}} {{/messages}}
</script> </script>

View File

@ -1,6 +1,6 @@
$(function() { $(function() {
var socket = io.connect(""); var socket = io.connect("");
$.each(["networks", "channels", "messages", "users"], function(i, type) { $.each(["network", "channel", "message", "user"], function(i, type) {
socket.on(type, function(json) { socket.on(type, function(json) {
event(type, json); event(type, json);
}); });
@ -16,9 +16,10 @@ $(function() {
} }
function event(type, json) { function event(type, json) {
console.log(json);
switch (type) { switch (type) {
case "networks": case "network":
var html = ""; var html = "";
json.forEach(function(network) { json.forEach(function(network) {
html += render("#window", network); html += render("#window", network);
@ -37,7 +38,7 @@ $(function() {
.bringToTop(); .bringToTop();
break; break;
case "channels": case "channel":
var target = json.target; var target = json.target;
if (json.action == "remove") { if (json.action == "remove") {
$("[data-id='" + json.data.id + "']").remove(); $("[data-id='" + json.data.id + "']").remove();
@ -65,7 +66,7 @@ $(function() {
.scrollGlue({animate: 400}); .scrollGlue({animate: 400});
break; break;
case "users": case "user":
var target; var target;
if (typeof json.target !== "undefined") { if (typeof json.target !== "undefined") {
target = chat.find(".window[data-id='" + json.target + "']"); target = chat.find(".window[data-id='" + json.target + "']");
@ -75,7 +76,7 @@ $(function() {
target.html(render("#user", {users: json.data})); target.html(render("#user", {users: json.data}));
break; break;
case "messages": case "message":
var target; var target;
if (typeof json.target !== "undefined") { if (typeof json.target !== "undefined") {
target = chat.find(".window[data-id='" + json.target + "']"); target = chat.find(".window[data-id='" + json.target + "']");

View File

@ -1,22 +1,20 @@
var irc = require("irc");
var _ = require("lodash"); var _ = require("lodash");
var Backbone = require("backbone"); var backbone = require("backbone");
var moment = require("moment"); var moment = require("moment");
var id = 1;
var models = var models =
module.exports = module.exports =
{}; {};
var id = 1; models.User = backbone.Model.extend({
models.User = Backbone.Model.extend({
defaults: { defaults: {
mode: "", mode: "",
name: "" name: "",
} }
}); });
models.Users = Backbone.Collection.extend({ models.Users = backbone.Collection.extend({
model: models.User, model: models.User,
sort: function(options) { sort: function(options) {
this.models = _.sortBy( this.models = _.sortBy(
@ -39,23 +37,23 @@ models.Users = Backbone.Collection.extend({
} }
}); });
models.Message = Backbone.Model.extend({ models.Message = backbone.Model.extend({
defaults: { defaults: {
type: "",
time: "", time: "",
user: "", from: "",
text: "", message: "",
type: ""
}, },
initialize: function() { initialize: function() {
this.set("time", moment().format("HH:mm")); this.set("time", moment().format("HH:mm"));
} }
}); });
models.Messages = Backbone.Collection.extend({ models.Messages = backbone.Collection.extend({
model: models.Message model: models.Message
}); });
models.Channel = Backbone.Model.extend({ models.Channel = backbone.Model.extend({
defaults: { defaults: {
type: "channel", type: "channel",
name: "" name: ""
@ -67,7 +65,7 @@ models.Channel = Backbone.Model.extend({
this.set("messages", new models.Messages()); this.set("messages", new models.Messages());
this.get("messages").on("all", function(action, data) { this.get("messages").on("all", function(action, data) {
this.trigger("MESSAGES", { this.trigger("message", {
target: this.get("id"), target: this.get("id"),
type: "message", type: "message",
data: data, data: data,
@ -77,7 +75,7 @@ models.Channel = Backbone.Model.extend({
this.set("users", new models.Users()); this.set("users", new models.Users());
this.get("users").on("all", function(action) { this.get("users").on("all", function(action) {
this.trigger("USERS", { this.trigger("user", {
target: this.get("id"), target: this.get("id"),
type: "user", type: "user",
data: this.get("users"), data: this.get("users"),
@ -87,14 +85,13 @@ models.Channel = Backbone.Model.extend({
} }
}); });
models.Channels = Backbone.Collection.extend({ models.Channels = backbone.Collection.extend({
model: models.Channel model: models.Channel
}); });
models.Network = Backbone.Model.extend({ models.Network = backbone.Model.extend({
defaults: { defaults: {
host: "", host: ""
nick: "nick"
}, },
initialize: function() { initialize: function() {
this.set({ this.set({
@ -103,10 +100,10 @@ models.Network = Backbone.Model.extend({
this.set("channels", new models.Channels()); this.set("channels", new models.Channels());
this.get("channels").on("all", function(action, data) { this.get("channels").on("all", function(action, data) {
if (action == "USERS" || action == "MESSAGES") { if (action == "user" || action == "message") {
this.trigger(action, data); this.trigger(action, data);
} else { } else {
this.trigger("CHANNELS", { this.trigger("channel", {
target: this.get("id"), target: this.get("id"),
type: "channel", type: "channel",
data: data, data: data,
@ -122,12 +119,11 @@ models.Network = Backbone.Model.extend({
} }
}); });
models.Networks = Backbone.Collection.extend({ models.Networks = backbone.Collection.extend({
model: models.Network, model: models.Network,
initialize: function() { initialize: function() {
this.add(new models.Network({ this.add(new models.Network({
host: "Lobby", host: "Status"
connect: false
})); }));
}, },
find: function(id) { find: function(id) {

View File

@ -1,31 +1,71 @@
var connect = require("connect"); var http = require("connect");
var _ = require("lodash"); var io = require("socket.io");
var io = require("socket.io"); var irc = require("slate-irc");
var models = require(__dirname + "/models");
// Local library var net = require("net");
var models = require("./models"); var _ = require("lodash");
var sockets; var sockets;
var networks = new models.Networks; var networks = new models.Networks;
module.exports = function server(options) { var events = [
var app = connect().use(connect.static("client")).listen(9000); "away",
sockets = io.listen(app, {log: 0}).sockets; "join",
sockets.on("connection", function(s) { "kick",
init(s); "names",
"nick",
"notice",
"part",
"pong",
"privmsg",
"quit",
"topic",
"welcome",
"whois"
];
module.exports = function listen() {
var app = http()
.use(http.static("client"))
.listen(9000);
sockets = io.listen(app, {log: 0}).sockets.on("connection", function(s) {
s.on("input", input);
sockets.emit("network", networks);
}); });
networks.on("all", function(e) { networks.on("all", function() {
sockets.emit("networks", networks); sockets.emit.apply(sockets, arguments);
}); });
}; };
function init(socket) { function connect(host) {
socket.on("input", function(json) { clientInput(json); }); var network = new models.Network({host: host}, {silent: true});
sockets.emit("networks", networks); networks.add(network);
networks.trigger("network", networks);
var stream = net.connect({
port: 6667,
host: host,
});
stream.on("error", function(e) {
console.log(e);
});
var client = irc(stream);
client.nick("t_user");
client.user("t_user", "temp user");
events.forEach(function(e) {
client.on(e, function(data) {
event.apply(network, [e, data]);
});
});
} }
function clientInput(json) { function input(json) {
var target = networks.find(json.id); var target = networks.find(json.id);
if (!target) { if (!target) {
return; return;
@ -48,18 +88,14 @@ function clientInput(json) {
channel.get("name") channel.get("name")
); );
case "msg": case "msg":
var m = new models.Message({text: _.tail(args, 2)}); channel.get("messages").add(new models.Message({text: _.tail(args, 2)}));
channel.get("messages").add(m);
break; break;
case "server": case "server":
case "connect": case "connect":
if (!args[1]) { if (args[1]) {
break; connect(args[1]);
} }
var n = new models.Network({host: args[1]});
networks.add(n);
break; break;
case "quit": case "quit":
@ -68,4 +104,68 @@ function clientInput(json) {
break; break;
} }
}
function event(event, data) {
var network = this;
var channels = network.get("channels");
switch (event) {
case "away":
console.log(event);
break;
case "join":
console.log(event);
break;
case "kick":
console.log(event);
break;
case "names":
console.log(event);
break;
case "nick":
console.log(event);
break;
case "notice":
channels.first().get("messages").add(new models.Message(data));
break;
case "part":
console.log(event);
break;
case "pong":
console.log(event);
break;
case "privmsg":
console.log(event);
break;
case "quit":
console.log(event);
break;
case "topic":
console.log(event);
break;
case "welcome":
console.log(event);
break;
case "whois":
console.log(event);
break;
}
// Debug
console.log(data);
} }

View File

@ -7,13 +7,12 @@
"email": "mattias@mattiaserming.com" "email": "mattias@mattiaserming.com"
}, },
"dependencies": { "dependencies": {
"backbone": "1.1.2", "lodash": "~2.4.1",
"connect": "2.13.0", "backbone": "~1.1.2",
"irc": "0.3.6", "slate-irc": "~0.4.0",
"lodash": "2.4.1", "moment": "~2.5.1",
"moment": "2.5.1", "connect": "~2.14.3",
"socket.io": "0.9.16", "socket.io": "~0.9.16"
"slate-irc": "~0.4.0"
}, },
"keywords": [ "keywords": [
"chat", "chat",