Implemented client.connect()

This commit is contained in:
Mattias Erming 2014-07-06 17:22:43 +02:00
parent 9491de4cb1
commit ce792d1a60
8 changed files with 133 additions and 113 deletions

View File

@ -42,6 +42,7 @@ button {
left: 0; left: 0;
overflow: auto; overflow: auto;
overflow-x: hidden; overflow-x: hidden;
-webkit-overflow-scrolling: touch;
position: absolute; position: absolute;
top: 0; top: 0;
width: 220px; width: 220px;
@ -173,6 +174,9 @@ button {
#chat .window { #chat .window {
bottom: 40px; bottom: 40px;
left: 0; left: 0;
overflow: auto;
overflow-x: hidden;
-webkit-overflow-scrolling: touch;
position: absolute; position: absolute;
right: 180px; right: 180px;
top: 0; top: 0;
@ -293,6 +297,7 @@ button {
#users { #users {
bottom: 0; bottom: 0;
overflow: auto; overflow: auto;
-webkit-overflow-scrolling: touch;
padding: 15px 20px; padding: 15px 20px;
position: absolute; position: absolute;
top: 120px; top: 120px;

View File

@ -28,7 +28,6 @@ $(function() {
"/slap", "/slap",
"/topic", "/topic",
"/voice", "/voice",
"/whoami",
"/whois" "/whois"
]; ];
@ -49,7 +48,6 @@ $(function() {
}); });
socket.on("init", function(data) { socket.on("init", function(data) {
console.log("INIT");
networks.empty(); networks.empty();
channels = $.map(data.networks, function(n) { channels = $.map(data.networks, function(n) {
return n.channels; return n.channels;
@ -94,6 +92,8 @@ $(function() {
}); });
socket.on("network", function(data) { socket.on("network", function(data) {
var lobby = data.network.channels[0];
channels.push(lobby);
networks.append( networks.append(
render("networks", { render("networks", {
networks: [data.network] networks: [data.network]
@ -151,11 +151,13 @@ $(function() {
networks.on("click", ".chan", function() { networks.on("click", ".chan", function() {
var self = $(this); var self = $(this);
var id = self.data("id"); var id = self.data("id");
if (self.hasClass("active")) {
networks.find(".active").removeClass("active"); return;
self.addClass("active"); }
chat.data("target", id); chat.data("target", id);
networks.find(".active").removeClass("active");
self.addClass("active");
var chan = find(id); var chan = find(id);
if (typeof chan !== "undefined") { if (typeof chan !== "undefined") {

View File

@ -1,3 +1,3 @@
process.chdir(__dirname); process.chdir(__dirname);
var shout = require("./lib/shout"); var shout = require("./lib/server");
shout(); shout();

96
lib/client.js Normal file
View File

@ -0,0 +1,96 @@
var _ = require("lodash");
var net = require("net");
var Network = require("./models/network");
var slate = require("slate-irc");
var tls = require("tls");
module.exports = Client;
var id = 0;
var events = [
"errors",
"join",
"kick",
"mode",
"motd",
"message",
"names",
"nick",
"notice",
"part",
"quit",
"topic",
"welcome",
"whois"
];
function Client(attr) {
_.merge(this, _.extend({
id: id++,
networks: [],
keepAlive: false,
sockets: null
}, attr));
}
Client.prototype.emit = function(event, data) {
if (this.sockets !== null) {
this.sockets.in(this.id).emit(event, data);
}
};
Client.prototype.find = function(id) {
var network = null;
var chan = null;
for (var i in this.networks) {
var n = this.networks[i];
chan = _.find(n.channels, {id: id});
if (chan) {
network = n;
break;
}
}
if (network && chan) {
return {
network: network,
chan: chan
};
} else {
return false;
}
};
Client.prototype.connect = function(args) {
var client = this;
var options = {
host: args.host,
port: args.port || 6667
};
var stream = args.tls ? tls.connect(options) : net.connect(options);
stream.on("error", function(e) {
console.log(e);
});
var irc = slate(stream);
irc.nick("shout");
irc.user("shout", "Shout User");
var network = new Network({
host: options.host,
irc: irc
});
client.networks.push(network);
client.emit("network", {
network: network
});
events.forEach(function(plugin) {
require("./plugins/irc-events/" + plugin).apply(client, [irc, network]);
});
irc.on("welcome", function() {
irc.join("#shout-test");
});
};

View File

@ -1,41 +0,0 @@
var _ = require("lodash");
var Network = require("./network");
module.exports = Client;
var id = 0;
function Client(attr) {
_.merge(this, _.extend({
id: id++,
networks: [],
nick: "",
keepAlive: false,
sockets: null
}, attr));
}
Client.prototype.emit = function(event, data) {
if (this.sockets !== null) {
this.sockets.in(this.id).emit(event, data);
}
};
Client.prototype.find = function(id) {
var network = null;
var chan = null;
this.networks.forEach(function(n) {
chan = _.find(n.channels, {id: id});
if (chan) {
network = n;
}
});
if (network && chan) {
return {
network: network,
chan: chan
};
} else {
return false;
}
};

View File

@ -1,3 +1,11 @@
module.exports = function(network, chan, cmd, args) { module.exports = function(network, chan, cmd, args) {
// Not yet implemented. if (cmd != "connect" && cmd != "server") {
return;
}
if (args.length !== 0) {
var client = this;
client.connect({
host: args[0]
});
}
}; };

View File

@ -9,7 +9,7 @@ module.exports = function(network, chan, cmd, args) {
var client = this; var client = this;
var irc = network.irc; var irc = network.irc;
if (args.length === 0 || args[0] == "") { if (args.length === 0 || args[0] === "") {
return; return;
} }

View File

@ -1,12 +1,8 @@
var _ = require("lodash"); var _ = require("lodash");
var Client = require("./models/client"); var Client = require("./client");
var config = require("../config") || {}; var config = require("../config") || {};
var http = require("connect"); var http = require("connect");
var net = require("net");
var Network = require("./models/network");
var io = require("socket.io"); var io = require("socket.io");
var slate = require("slate-irc");
var tls = require("tls");
var sockets = null; var sockets = null;
var clients = []; var clients = [];
@ -28,23 +24,6 @@ var inputs = [
"whois" "whois"
]; ];
var events = [
"errors",
"join",
"kick",
"mode",
"motd",
"message",
"names",
"nick",
"notice",
"part",
"quit",
"topic",
"welcome",
"whois"
];
module.exports = function() { module.exports = function() {
sockets = io(http().use(http.static("client")).listen(config.port || 9000)); sockets = io(http().use(http.static("client")).listen(config.port || 9000));
sockets.on("connection", function(socket) { sockets.on("connection", function(socket) {
@ -77,7 +56,7 @@ function auth(data) {
if (clients.length === 0) { if (clients.length === 0) {
client = new Client({sockets: sockets}); client = new Client({sockets: sockets});
clients.push(client); clients.push(client);
connect(client, { client.connect({
host: "irc.freenode.org" host: "irc.freenode.org"
}); });
} }
@ -89,47 +68,8 @@ function auth(data) {
} }
} }
function connect(client, args) {
var options = {
host: args.host,
port: args.port || 6667
};
var stream = args.tls ? tls.connect(options) : net.connect(options);
stream.on("error", function(e) {
console.log(e);
});
var irc = slate(stream);
irc.nick("shout");
irc.user("shout", "Shout User");
client.nick = "shout";
var network = new Network({
host: options.host,
irc: irc
});
client.networks.push(network);
client.emit("network", {
network: network
});
events.forEach(function(plugin) {
require("./plugins/irc-events/" + plugin).apply(client, [irc, network]);
});
irc.on("welcome", function() {
irc.join("#shout-test");
});
}
function input(client, data) { function input(client, data) {
var target = client.find(data.target); var target = client.find(data.target);
if (!target) {
return;
}
var text = data.text; var text = data.text;
if (text.charAt(0) !== "/") { if (text.charAt(0) !== "/") {
@ -140,6 +80,16 @@ function input(client, data) {
var cmd = args.shift().replace("/", "").toLowerCase(); var cmd = args.shift().replace("/", "").toLowerCase();
inputs.forEach(function(plugin) { inputs.forEach(function(plugin) {
require("./plugins/inputs/" + plugin).apply(client, [target.network, target.chan, cmd, args]); try {
var fn = require("./plugins/inputs/" + plugin);
fn.apply(client, [
target.network,
target.chan,
cmd,
args
]);
} catch (err) {
// ..
}
}); });
} }