From 9e41d4d74636a346d6a186f2b94a20ae6bc90676 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Thu, 3 Jul 2014 00:00:11 +0200 Subject: [PATCH] Added input plugins --- client/css/style.css | 1 + client/js/chat.js | 26 +++++++++++++----------- lib/models/client.js | 19 ++++++++++++++++++ lib/plugins/inputs/action.js | 3 ++- lib/plugins/inputs/connect.js | 3 +++ lib/plugins/inputs/invite.js | 3 ++- lib/plugins/inputs/join.js | 9 ++++++++- lib/plugins/inputs/kick.js | 3 ++- lib/plugins/inputs/mode.js | 3 ++- lib/plugins/inputs/msg.js | 38 ++++++++++++++++++++++++++++++++++- lib/plugins/inputs/nick.js | 3 ++- lib/plugins/inputs/notice.js | 3 ++- lib/plugins/inputs/part.js | 10 ++++++++- lib/plugins/inputs/quit.js | 3 ++- lib/plugins/inputs/raw.js | 3 ++- lib/plugins/inputs/server.js | 2 -- lib/plugins/inputs/topic.js | 3 ++- lib/plugins/inputs/whois.js | 3 ++- lib/shout.js | 21 ++++++++++++++++--- 19 files changed, 129 insertions(+), 30 deletions(-) create mode 100644 lib/plugins/inputs/connect.js delete mode 100644 lib/plugins/inputs/server.js diff --git a/client/css/style.css b/client/css/style.css index c57572ab..0bbd8a94 100644 --- a/client/css/style.css +++ b/client/css/style.css @@ -216,6 +216,7 @@ button { } #messages .from { background: #f9f9f9; + border-right: 1px solid #f4f4f4; color: #ddd; padding-right: 10px; text-align: right; diff --git a/client/js/chat.js b/client/js/chat.js index 3fc6f3f4..a470dc12 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -120,17 +120,6 @@ $(function() { } }); - networks.on("click", ".chan", function() { - var id = $(this).data("id"); - var chan = find(id); - if (typeof chan !== "undefined") { - activeChannel = chan; - chat.html( - render("chat", chan) - ); - } - }); - var input = $("#input").tab(complete, { hint: false }); @@ -140,10 +129,23 @@ $(function() { var value = input.val(); input.val(""); socket.emit("input", { - // .. + target: chat.data("target"), + text: value }); }); + networks.on("click", ".chan", function() { + var id = $(this).data("id"); + var chan = find(id); + chat.data("target", id); + if (typeof chan !== "undefined") { + activeChannel = chan; + chat.html( + render("chat", chan) + ); + } + }); + function isActive(chan) { return activeChannel !== null && chan == activeChannel; } diff --git a/lib/models/client.js b/lib/models/client.js index 6517be11..ed38f0a7 100644 --- a/lib/models/client.js +++ b/lib/models/client.js @@ -20,3 +20,22 @@ Client.prototype.emit = function(event, data) { 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/action.js b/lib/plugins/inputs/action.js index 89e5d4ca..88d20dd4 100644 --- a/lib/plugins/inputs/action.js +++ b/lib/plugins/inputs/action.js @@ -1,2 +1,3 @@ -module.exports = function() { +module.exports = function(network, chan, cmd, args) { + console.log("action"); }; diff --git a/lib/plugins/inputs/connect.js b/lib/plugins/inputs/connect.js new file mode 100644 index 00000000..16be6212 --- /dev/null +++ b/lib/plugins/inputs/connect.js @@ -0,0 +1,3 @@ +module.exports = function(network, chan, cmd, args) { + console.log("connect"); +}; diff --git a/lib/plugins/inputs/invite.js b/lib/plugins/inputs/invite.js index 89e5d4ca..11bdbe94 100644 --- a/lib/plugins/inputs/invite.js +++ b/lib/plugins/inputs/invite.js @@ -1,2 +1,3 @@ -module.exports = function() { +module.exports = function(network, chan, cmd, args) { + console.log("invite"); }; diff --git a/lib/plugins/inputs/join.js b/lib/plugins/inputs/join.js index 89e5d4ca..1c508ba3 100644 --- a/lib/plugins/inputs/join.js +++ b/lib/plugins/inputs/join.js @@ -1,2 +1,9 @@ -module.exports = function() { +module.exports = function(network, chan, cmd, args) { + if (cmd != "join") { + return + } + if (args.length != 0) { + var irc = network.irc; + irc.join(args); + } }; diff --git a/lib/plugins/inputs/kick.js b/lib/plugins/inputs/kick.js index 89e5d4ca..aa89b3a1 100644 --- a/lib/plugins/inputs/kick.js +++ b/lib/plugins/inputs/kick.js @@ -1,2 +1,3 @@ -module.exports = function() { +module.exports = function(network, chan, cmd, args) { + console.log("kick"); }; diff --git a/lib/plugins/inputs/mode.js b/lib/plugins/inputs/mode.js index 89e5d4ca..451f7561 100644 --- a/lib/plugins/inputs/mode.js +++ b/lib/plugins/inputs/mode.js @@ -1,2 +1,3 @@ -module.exports = function() { +module.exports = function(network, chan, cmd, args) { + console.log("mode"); }; diff --git a/lib/plugins/inputs/msg.js b/lib/plugins/inputs/msg.js index 89e5d4ca..49879a4d 100644 --- a/lib/plugins/inputs/msg.js +++ b/lib/plugins/inputs/msg.js @@ -1,2 +1,38 @@ -module.exports = function() { +var _ = require("lodash"); +var Msg = require("../../models/msg"); + +module.exports = function(network, chan, cmd, args) { + if (cmd != "say" && cmd != "msg") { + return + } + + var client = this; + var irc = network.irc; + + if (args.length === 0) { + return; + } + + var target = args[0].charAt(0) == "#" ? args[0] : "#" + chan.name; + if (target !== chan.name) { + targetChan = _.findWhere(network.channels, { + name: target + }); + } + + var text = args.join(" "); + irc.send(target, text); + + if (typeof chan !== "undefined") { + var msg = new Msg({ + type: Msg.Type.MESSAGE, + from: irc.me, + text: text + }); + chan.messages.push(msg); + client.emit("msg", { + chan: chan.id, + msg: msg + }); + } }; diff --git a/lib/plugins/inputs/nick.js b/lib/plugins/inputs/nick.js index 89e5d4ca..9c125bb5 100644 --- a/lib/plugins/inputs/nick.js +++ b/lib/plugins/inputs/nick.js @@ -1,2 +1,3 @@ -module.exports = function() { +module.exports = function(network, chan, cmd, args) { + console.log("nick"); }; diff --git a/lib/plugins/inputs/notice.js b/lib/plugins/inputs/notice.js index 89e5d4ca..61f4c8c4 100644 --- a/lib/plugins/inputs/notice.js +++ b/lib/plugins/inputs/notice.js @@ -1,2 +1,3 @@ -module.exports = function() { +module.exports = function(network, chan, cmd, args) { + console.log("notice"); }; diff --git a/lib/plugins/inputs/part.js b/lib/plugins/inputs/part.js index 89e5d4ca..0464c884 100644 --- a/lib/plugins/inputs/part.js +++ b/lib/plugins/inputs/part.js @@ -1,2 +1,10 @@ -module.exports = function() { +module.exports = function(network, chan, cmd, args) { + if (cmd != "part") { + return; + } + var irc = network.irc; + if (args.length === 0) { + args.push("#" + chan.name); + } + irc.part(args); }; diff --git a/lib/plugins/inputs/quit.js b/lib/plugins/inputs/quit.js index 89e5d4ca..2e640d65 100644 --- a/lib/plugins/inputs/quit.js +++ b/lib/plugins/inputs/quit.js @@ -1,2 +1,3 @@ -module.exports = function() { +module.exports = function(network, chan, cmd, args) { + console.log("quit"); }; diff --git a/lib/plugins/inputs/raw.js b/lib/plugins/inputs/raw.js index 89e5d4ca..49448ef4 100644 --- a/lib/plugins/inputs/raw.js +++ b/lib/plugins/inputs/raw.js @@ -1,2 +1,3 @@ -module.exports = function() { +module.exports = function(network, chan, cmd, args) { + console.log("raw"); }; diff --git a/lib/plugins/inputs/server.js b/lib/plugins/inputs/server.js deleted file mode 100644 index 89e5d4ca..00000000 --- a/lib/plugins/inputs/server.js +++ /dev/null @@ -1,2 +0,0 @@ -module.exports = function() { -}; diff --git a/lib/plugins/inputs/topic.js b/lib/plugins/inputs/topic.js index 89e5d4ca..a53bca0c 100644 --- a/lib/plugins/inputs/topic.js +++ b/lib/plugins/inputs/topic.js @@ -1,2 +1,3 @@ -module.exports = function() { +module.exports = function(network, chan, cmd, args) { + console.log("topic"); }; diff --git a/lib/plugins/inputs/whois.js b/lib/plugins/inputs/whois.js index 89e5d4ca..186261c3 100644 --- a/lib/plugins/inputs/whois.js +++ b/lib/plugins/inputs/whois.js @@ -1,2 +1,3 @@ -module.exports = function() { +module.exports = function(network, chan, cmd, args) { + console.log("whois"); }; diff --git a/lib/shout.js b/lib/shout.js index 04f559cd..1476d870 100644 --- a/lib/shout.js +++ b/lib/shout.js @@ -13,6 +13,7 @@ var clients = []; var inputs = [ "action", + "connect", "invite", "join", "kick", @@ -23,7 +24,6 @@ var inputs = [ "part", "quit", "raw", - "server", "topic", "whois" ]; @@ -75,7 +75,7 @@ function auth(data) { // Temporary: var client = clients[0]; if (clients.length === 0) { - var client = new Client({sockets: sockets}); + client = new Client({sockets: sockets}); clients.push(client); connect(client, { host: "irc.freenode.org" @@ -126,5 +126,20 @@ function connect(client, args) { } function input(client, data) { - console.log(data); + var target = client.find(data.target); + if (!target) { + return; + } + + var text = data.text; + if (text.charAt(0) !== "/") { + text = "/say " + text; + } + + var args = text.split(" "); + var cmd = args.shift().replace("/", "").toLowerCase(); + + inputs.forEach(function(plugin) { + require("./plugins/inputs/" + plugin).apply(client, [target.network, target.chan, cmd, args]); + }); }