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

View file

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

View file

@ -1,3 +1,3 @@
process.chdir(__dirname);
var shout = require("./lib/shout");
var shout = require("./lib/server");
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) {
// 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 irc = network.irc;
if (args.length === 0 || args[0] == "") {
if (args.length === 0 || args[0] === "") {
return;
}

View file

@ -1,12 +1,8 @@
var _ = require("lodash");
var Client = require("./models/client");
var Client = require("./client");
var config = require("../config") || {};
var http = require("connect");
var net = require("net");
var Network = require("./models/network");
var io = require("socket.io");
var slate = require("slate-irc");
var tls = require("tls");
var sockets = null;
var clients = [];
@ -28,23 +24,6 @@ var inputs = [
"whois"
];
var events = [
"errors",
"join",
"kick",
"mode",
"motd",
"message",
"names",
"nick",
"notice",
"part",
"quit",
"topic",
"welcome",
"whois"
];
module.exports = function() {
sockets = io(http().use(http.static("client")).listen(config.port || 9000));
sockets.on("connection", function(socket) {
@ -77,7 +56,7 @@ function auth(data) {
if (clients.length === 0) {
client = new Client({sockets: sockets});
clients.push(client);
connect(client, {
client.connect({
host: "irc.freenode.org"
});
}
@ -89,48 +68,9 @@ 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) {
var target = client.find(data.target);
if (!target) {
return;
}
var text = data.text;
if (text.charAt(0) !== "/") {
text = "/say " + text;
@ -140,6 +80,16 @@ function input(client, data) {
var cmd = args.shift().replace("/", "").toLowerCase();
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) {
// ..
}
});
}