From ce792d1a60c0bbd55b0cff19131a8bfcdc1249ef Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sun, 6 Jul 2014 17:22:43 +0200 Subject: [PATCH] Implemented client.connect() --- client/css/style.css | 5 ++ client/js/chat.js | 12 +++-- index.js | 2 +- lib/client.js | 96 +++++++++++++++++++++++++++++++++++ lib/models/client.js | 41 --------------- lib/plugins/inputs/connect.js | 10 +++- lib/plugins/inputs/msg.js | 2 +- lib/{shout.js => server.js} | 78 +++++----------------------- 8 files changed, 133 insertions(+), 113 deletions(-) create mode 100644 lib/client.js delete mode 100644 lib/models/client.js rename lib/{shout.js => server.js} (52%) diff --git a/client/css/style.css b/client/css/style.css index b92c7ad..3ab4db3 100644 --- a/client/css/style.css +++ b/client/css/style.css @@ -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; diff --git a/client/js/chat.js b/client/js/chat.js index ac00879..aa5a51c 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -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") { diff --git a/index.js b/index.js index 34faba0..be03b9f 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,3 @@ process.chdir(__dirname); -var shout = require("./lib/shout"); +var shout = require("./lib/server"); shout(); diff --git a/lib/client.js b/lib/client.js new file mode 100644 index 0000000..3b7ab2f --- /dev/null +++ b/lib/client.js @@ -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"); + }); +}; diff --git a/lib/models/client.js b/lib/models/client.js deleted file mode 100644 index ed38f0a..0000000 --- a/lib/models/client.js +++ /dev/null @@ -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; - } -}; diff --git a/lib/plugins/inputs/connect.js b/lib/plugins/inputs/connect.js index 0f76517..3f3df23 100644 --- a/lib/plugins/inputs/connect.js +++ b/lib/plugins/inputs/connect.js @@ -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] + }); + } }; diff --git a/lib/plugins/inputs/msg.js b/lib/plugins/inputs/msg.js index 422bfaa..78a939c 100644 --- a/lib/plugins/inputs/msg.js +++ b/lib/plugins/inputs/msg.js @@ -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; } diff --git a/lib/shout.js b/lib/server.js similarity index 52% rename from lib/shout.js rename to lib/server.js index 1476d87..d6b0451 100644 --- a/lib/shout.js +++ b/lib/server.js @@ -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) { + // .. + } }); }