From 4ef13d6a18551a27d473cb38583bb5300e0cb72c Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sun, 29 Jun 2014 21:41:02 +0200 Subject: [PATCH] Re-implemented all irc-events --- client/css/style.css | 2 +- lib/models/chan.js | 4 +++ lib/models/client.js | 7 +++-- lib/models/msg.js | 1 + lib/models/network.js | 12 ++++++-- lib/plugins/irc-events/errors.js | 21 +++++++++++++- lib/plugins/irc-events/join.js | 36 ++++++++++++++++++++++- lib/plugins/irc-events/kick.js | 31 +++++++++++++++++++- lib/plugins/irc-events/message.js | 41 ++++++++++++++++++++++++++- lib/plugins/irc-events/mode.js | 27 +++++++++++++++++- lib/plugins/irc-events/motd.js | 20 ++++++++++++- lib/plugins/irc-events/names.js | 21 +++++++++++++- lib/plugins/irc-events/nick.js | 42 ++++++++++++++++++++++++++- lib/plugins/irc-events/notice.js | 22 ++++++++++++++- lib/plugins/irc-events/part.js | 34 +++++++++++++++++++++- lib/plugins/irc-events/quit.js | 28 +++++++++++++++++- lib/plugins/irc-events/topic.js | 23 ++++++++++++++- lib/plugins/irc-events/welcome.js | 19 ++++++++++++- lib/plugins/irc-events/whois.js | 47 ++++++++++++++++++++++++++++++- lib/shout.js | 44 ++++++++++++++++++++++++++--- 20 files changed, 459 insertions(+), 23 deletions(-) diff --git a/client/css/style.css b/client/css/style.css index a13012ba..19f483f3 100644 --- a/client/css/style.css +++ b/client/css/style.css @@ -74,7 +74,7 @@ button { padding: 6px 10px 8px; position: relative; text-align: left; - transition: background .1s, color 5s; + transition: all .2s; width: 160px; } #channels .chan:first-child { diff --git a/lib/models/chan.js b/lib/models/chan.js index bca66921..91fd0061 100644 --- a/lib/models/chan.js +++ b/lib/models/chan.js @@ -19,3 +19,7 @@ function Chan(attr) { users: [] })); } + +Chan.prototype.sortUsers = function() { + // .. +}; diff --git a/lib/models/client.js b/lib/models/client.js index 342cf2f8..6517be11 100644 --- a/lib/models/client.js +++ b/lib/models/client.js @@ -1,4 +1,5 @@ var _ = require("lodash"); +var Network = require("./network"); module.exports = Client; @@ -8,12 +9,14 @@ 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) { + if (this.sockets !== null) { this.sockets.in(this.id).emit(event, data); } -}; \ No newline at end of file +}; diff --git a/lib/models/msg.js b/lib/models/msg.js index 4eb73a59..6e6c6cde 100644 --- a/lib/models/msg.js +++ b/lib/models/msg.js @@ -2,6 +2,7 @@ var _ = require("lodash"); var moment = require("moment"); Msg.Type = { + ACTION: "action", ERROR: "error", JOIN: "join", KICK: "kick", diff --git a/lib/models/network.js b/lib/models/network.js index d8a3cc69..3d13f722 100644 --- a/lib/models/network.js +++ b/lib/models/network.js @@ -1,4 +1,5 @@ var _ = require("lodash"); +var Chan = require("./chan"); module.exports = Network; @@ -8,13 +9,20 @@ function Network(attr) { _.merge(this, _.extend({ id: id++, connected: false, - slate: null, host: "", - name: capitalize(this.host.split(".")[1]) || this.host, + irc: null, + name: capitalize(attr.host.split(".")[1]) || attr.host, channels: [] }, attr)); + this.channels.unshift( + new Chan({name: this.name, type: Chan.Type.LOBBY}) + ); } +Network.prototype.toJSON = function() { + return _.omit(this, "irc"); +}; + function capitalize(str) { return str.charAt(0).toUpperCase() + str.slice(1); } diff --git a/lib/plugins/irc-events/errors.js b/lib/plugins/irc-events/errors.js index 89e5d4ca..f37c8ab4 100644 --- a/lib/plugins/irc-events/errors.js +++ b/lib/plugins/irc-events/errors.js @@ -1,2 +1,21 @@ -module.exports = function() { +var Msg = require("../../models/msg"); + +module.exports = function(irc, network) { + var client = this; + irc.on("errors", function(data) { + var msg = new Msg({ + type: Msg.Type.ERROR, + from: "*", + text: data.message, + }); + client.emit("msg", { + msg: msg + }); + if (!network.connected) { + if (data.cmd == "ERR_NICKNAMEINUSE") { + var random = client.nick + Math.floor(10 + (Math.random() * 89)); + irc.nick(random); + } + } + }); }; diff --git a/lib/plugins/irc-events/join.js b/lib/plugins/irc-events/join.js index 89e5d4ca..9ef67af2 100644 --- a/lib/plugins/irc-events/join.js +++ b/lib/plugins/irc-events/join.js @@ -1,2 +1,36 @@ -module.exports = function() { +var _ = require("lodash"); +var Msg = require("../../models/msg"); +var User = require("../../models/user"); + +module.exports = function(irc, network) { + var client = this; + irc.on("join", function(data) { + var chan = _.find(network.channels, {name: data.channel}); + if (typeof chan === "undefined") { + chan = new Chan({ + name: data.channel + }); + network.channels.push(chan); + client.emit("join", { + network: network.id, + chan: chan + }); + } + var users = chan.users; + users.push(new User({name: data.nick})); + chan.sortUsers(); + client.emit("users", { + chan: chan.id, + users: users + }); + var msg = new Msg({ + from: data.nick, + type: Msg.Type.JOIN + }); + chan.messages.push(msg); + client.emit("msg", { + id: chan.id, + msg: msg + }); + }); }; diff --git a/lib/plugins/irc-events/kick.js b/lib/plugins/irc-events/kick.js index 89e5d4ca..e340fad8 100644 --- a/lib/plugins/irc-events/kick.js +++ b/lib/plugins/irc-events/kick.js @@ -1,2 +1,31 @@ -module.exports = function() { +var _ = require("lodash"); +var Msg = require("../../models/msg"); + +module.exports = function(irc, network) { + var client = this; + irc.on("kick", function(data) { + var chan = _.findWhere(network.channels, {name: data.channel}); + if (typeof chan === "undefined") { + return; + } + if (data.client == irc.me) { + chan.users = []; + } else { + chan.users = _.without(chan.users, _.findWhere(chan.users, {name: data.client})); + } + client.emit("users", { + chan: chan.id, + users: chan.users + }); + var msg = new Msg({ + type: Msg.Type.KICK, + from: data.nick, + text: data.client + }); + chan.messages.push(msg); + client.emit("msg", { + chan: chan.id, + msg: msg + }); + }); }; diff --git a/lib/plugins/irc-events/message.js b/lib/plugins/irc-events/message.js index 89e5d4ca..dc9d86f4 100644 --- a/lib/plugins/irc-events/message.js +++ b/lib/plugins/irc-events/message.js @@ -1,2 +1,41 @@ -module.exports = function() { +var _ = require("lodash"); +var Chan = require("../../models/chan"); +var Msg = require("../../models/msg"); + +module.exports = function(irc, network) { + var client = this; + irc.on("message", function(data) { + var target = data.to; + var chan = _.findWhere(network.channels, {name: target.charAt(0) == "#" ? target : data.from}); + if (typeof chan === "undefined") { + chan = new Chan({ + type: Chan.Type.QUERY, + name: data.from + }); + network.channels.push(chan); + client.emit("join", { + network: network.id, + chan: chan + }); + } + var type = ""; + var text = data.message; + if (text.split(" ")[0] === "\u0001ACTION") { + type = Msg.Type.ACTION; + text = text.replace(/\u0001|ACTION/g, ""); + } + text.split(" ").forEach(function(w) { + if (w.indexOf(irc.me) === 0) type += " highlight"; + }); + var msg = new Msg({ + type: type || Msg.Type.MESSAGE, + from: data.from, + text: text + }); + chan.messages.push(msg); + client.emit("msg", { + chan: chan.id, + msg: msg + }); + }); }; diff --git a/lib/plugins/irc-events/mode.js b/lib/plugins/irc-events/mode.js index 89e5d4ca..04762cfd 100644 --- a/lib/plugins/irc-events/mode.js +++ b/lib/plugins/irc-events/mode.js @@ -1,2 +1,27 @@ -module.exports = function() { +var _ = require("lodash"); + +module.exports = function(irc, network) { + var client = this; + irc.on("mode", function(data) { + var chan = _.findWhere(network.channels, {name: data.target}); + if (typeof chan !== "undefined") { + setTimeout(function() { + irc.write("NAMES " + data.target); + }, 200); + var nick = data.nick; + if (nick.indexOf(".") !== -1) { + nick = data.target; + } + var msg = new Msg({ + type: Msg.Type.MODE, + from: nick, + text: data.mode + " " + data.client, + }); + chan.messages.push(msg); + client.emit("msg", { + chan: chan.id, + msg: msg, + }); + } + }); }; diff --git a/lib/plugins/irc-events/motd.js b/lib/plugins/irc-events/motd.js index 89e5d4ca..c76f04a8 100644 --- a/lib/plugins/irc-events/motd.js +++ b/lib/plugins/irc-events/motd.js @@ -1,2 +1,20 @@ -module.exports = function() { +var Msg = require("../../models/msg"); + +module.exports = function(irc, network) { + var client = this; + irc.on("motd", function(data) { + var lobby = network.channels[0]; + data.motd.forEach(function(text) { + var msg = new Msg({ + type: Msg.Type.MOTD, + from: "*", + text: text + }); + lobby.messages.push(msg); + client.emit("msg", { + chan: lobby.id, + msg: msg + }); + }); + }); }; diff --git a/lib/plugins/irc-events/names.js b/lib/plugins/irc-events/names.js index 89e5d4ca..57a795b5 100644 --- a/lib/plugins/irc-events/names.js +++ b/lib/plugins/irc-events/names.js @@ -1,2 +1,21 @@ -module.exports = function() { +var _ = require("lodash"); +var User = require("../../models/user"); + +module.exports = function(irc, network) { + var client = this; + irc.on("names", function(data) { + var chan = _.findWhere(network.channels, {name: data.channel}); + if (typeof chan === "undefined") { + return; + } + chan.users = []; + _.each(data.names, function(u) { + chan.users.push(new User(u)); + }); + chan.sortUsers(); + client.emit("users", { + chan: chan.id, + users: chan.users + }); + }); }; diff --git a/lib/plugins/irc-events/nick.js b/lib/plugins/irc-events/nick.js index 89e5d4ca..9cbe9a9b 100644 --- a/lib/plugins/irc-events/nick.js +++ b/lib/plugins/irc-events/nick.js @@ -1,2 +1,42 @@ -module.exports = function() { +var _ = require("lodash"); +var Msg = require("../../models/msg"); + +module.exports = function(irc, network) { + var client = this; + irc.on("nick", function(data) { + if (data["new"] == irc.me) { + var lobby = network.channels[0]; + var msg = new Msg({ + from: "*", + text: "You're now known as " + data["new"], + }); + chan.messages.push(msg); + client.emit("msg", { + chan: lobby.id, + msg: msg + }); + } + network.channels.forEach(function(chan) { + var user = _.findWhere(chan.users, {name: data.nick}); + if (typeof user === "undefined") { + return; + } + user.name = data["new"]; + chan.sortUsers(); + client.emit("users", { + chan: chan.id, + users: chan.users + }); + var msg = new Msg({ + type: Msg.Type.NICK, + from: data.nick, + text: data["new"] + }); + chan.messages.push(msg); + client.emit("msg", { + chan: chan.id, + msg: msg + }); + }); + }); }; diff --git a/lib/plugins/irc-events/notice.js b/lib/plugins/irc-events/notice.js index 89e5d4ca..3e5085ea 100644 --- a/lib/plugins/irc-events/notice.js +++ b/lib/plugins/irc-events/notice.js @@ -1,2 +1,22 @@ -module.exports = function() { +var Msg = require("../../models/msg"); + +module.exports = function(irc, network) { + var client = this; + irc.on("notice", function(data) { + var lobby = network.channels[0]; + var from = data.from || "*"; + if (data.to == "*" || data.from.indexOf(".") !== -1) { + from = "*"; + } + var msg = new Msg({ + type: Msg.Type.NOTICE, + from: from, + text: data.message + }); + lobby.messages.push(msg); + client.emit("msg", { + chan: lobby.id, + msg: msg + }); + }); }; diff --git a/lib/plugins/irc-events/part.js b/lib/plugins/irc-events/part.js index 89e5d4ca..af791a7c 100644 --- a/lib/plugins/irc-events/part.js +++ b/lib/plugins/irc-events/part.js @@ -1,2 +1,34 @@ -module.exports = function() { +var _ = require("lodash"); +var Msg = require("../../models/msg"); + +module.exports = function(irc, network) { + var client = this; + irc.on("part", function(data) { + var chan = _.findWhere(network.channels, {name: data.channels[0]}); + if (typeof chan === "undefined") { + return; + } + if (data.nick == irc.me) { + network.channels = _.without(network.channels, chan); + client.emit("part", { + chan: chan.id + }); + } else { + var user = _.findWhere(chan.users, {name: data.nick}); + chan.users = _.without(chan.users, user); + client.emit("users", { + chan: chan.id, + users: chan.users + }); + var msg = new Msg({ + type: Msg.Type.PART, + from: data.nick + }); + chan.messages.push(msg); + client.emit("msg", { + chan: chan.id, + msg: msg + }); + } + }); }; diff --git a/lib/plugins/irc-events/quit.js b/lib/plugins/irc-events/quit.js index 89e5d4ca..0f9f3f86 100644 --- a/lib/plugins/irc-events/quit.js +++ b/lib/plugins/irc-events/quit.js @@ -1,2 +1,28 @@ -module.exports = function() { +var _ = require("lodash"); +var Msg = require("../../models/msg"); + +module.exports = function(irc, network) { + var client = this; + irc.on("quit", function(data) { + network.channels.forEach(function(chan) { + var user = _.findWhere(chan.users, {name: data.nick}); + if (typeof user === "undefined") { + return; + } + chan.users = _.without(chan.users, user); + client.emit("users", { + chan: chan.id, + users: chan.users + }); + var msg = new Msg({ + type: Msg.Type.QUIT, + from: data.nick + }); + chan.messages.push(msg); + client.emit("msg", { + chan: chan.id, + msg: msg + }); + }); + }); }; diff --git a/lib/plugins/irc-events/topic.js b/lib/plugins/irc-events/topic.js index 89e5d4ca..b7663d25 100644 --- a/lib/plugins/irc-events/topic.js +++ b/lib/plugins/irc-events/topic.js @@ -1,2 +1,23 @@ -module.exports = function() { +var _ = require("lodash"); +var Msg = require("../../models/msg"); + +module.exports = function(irc, network) { + var client = this; + irc.on("topic", function(data) { + var chan = _.findWhere(network.channels, {name: data.channel}); + if (typeof chan === "undefined") { + return; + } + var from = data.nick || chan.name; + var msg = new Msg({ + type: Msg.Type.TOPIC, + from: from, + text: data.topic, + }); + chan.messages.push(msg); + client.emit("msg", { + chan: chan.id, + msg: msg + }); + }); }; diff --git a/lib/plugins/irc-events/welcome.js b/lib/plugins/irc-events/welcome.js index 89e5d4ca..627ad920 100644 --- a/lib/plugins/irc-events/welcome.js +++ b/lib/plugins/irc-events/welcome.js @@ -1,2 +1,19 @@ -module.exports = function() { +var Msg = require("../../models/msg"); + +module.exports = function(irc, network) { + var client = this; + irc.on("welcome", function(data) { + network.connected = true; + irc.write("PING " + network.host); + var lobby = network.channels[0]; + var msg = new Msg({ + from: "*", + text: "You're now known as " + data + }); + lobby.messages.push(msg); + client.emit("msg", { + chan: lobby.id, + msg: msg + }); + }); }; diff --git a/lib/plugins/irc-events/whois.js b/lib/plugins/irc-events/whois.js index 89e5d4ca..6e4bd9a8 100644 --- a/lib/plugins/irc-events/whois.js +++ b/lib/plugins/irc-events/whois.js @@ -1,2 +1,47 @@ -module.exports = function() { +var _ = require("lodash"); +var Chan = require("../../models/chan"); +var Msg = require("../../models/msg"); + +module.exports = function(irc, network) { + var client = this; + irc.on("whois", function(data) { + if (!data) { + return; + } + var chan = _.findWhere(network.channels, {name: data.nickname}); + if (typeof chan === "undefined") { + chan = new Chan({ + type: Chan.Type.QUERY, + name: data.nickname + }); + network.channels.push(chan); + client.emit("join", { + network: network.id, + chan: chan + }); + } + var prefix = { + hostname: "from", + realname: "is", + channels: "on", + server: "using" + }; + var i = 0; + for (var k in data) { + var key = prefix[k]; + if (!key || data[k].toString() == "") { + continue; + } + var msg = new Msg({ + type: Msg.Type.WHOIS, + from: data.nickname, + text: key + " " + data[k] + }); + chan.messages.push(msg); + client.emit("msg", { + chan: chan.id, + msg: msg + }); + } + }); }; diff --git a/lib/shout.js b/lib/shout.js index 984c6f1c..0673b0ad 100644 --- a/lib/shout.js +++ b/lib/shout.js @@ -2,7 +2,11 @@ var _ = require("lodash"); var Client = require("./models/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 = []; @@ -45,7 +49,9 @@ module.exports = function() { sockets = io(http().use(http.static("client")).listen(config.port || 9000)); sockets.on("connection", function(socket) { if (config.public) { - init(socket, new Client()); + var client = new Client({sockets: sockets}); + init(socket, client); + connect(client, {host: "irc.rizon.net"}); } else { init(socket); } @@ -60,11 +66,41 @@ function init(socket, client) { socket.on("input", function(data) { input(client, data); }); socket.join(client.id); socket.emit("init", { - networks: client.networks + init: client.networks }); } -}; +} function auth() { var socket = this; -}; +} + +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]); + }); +} + +function input(client, data) { + console.log(data); +}