From 6493de52b9c601b48ed582353388f1cd5a3380b3 Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 14 Apr 2013 15:36:48 +0000 Subject: [PATCH 1/6] update ban and quiet in kick.api to use configured chanserv [#261] --- modules/kick/kick.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/kick/kick.js b/modules/kick/kick.js index 5308615..e5be508 100644 --- a/modules/kick/kick.js +++ b/modules/kick/kick.js @@ -4,9 +4,13 @@ var kick = function(dbot) { this.api = { 'ban': function(server, user, channel) { - dbot.instance.connections[server].send('MODE ' + channel + ' +b ' + user + '!*@*'); + dbot.say(server, this.config.chanserv, 'ban ' + channel + ' ' + user) }, + 'quiet': function(server, user, channel) { + dbot.say(server, this.config.chanserv, 'quiet ' + channel + ' ' + user) + } + 'kick': function(server, user, channel, msg) { dbot.instance.connections[server].send('KICK ' + channel + ' ' + user + ' :' + msg); } From a30f1a6a77e73a35ae21a42fe2bbfc740eb62b56 Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 14 Apr 2013 15:40:41 +0000 Subject: [PATCH 2/6] include chanserv config key [#261] --- modules/kick/config.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/kick/config.json b/modules/kick/config.json index d765613..c32561f 100644 --- a/modules/kick/config.json +++ b/modules/kick/config.json @@ -3,5 +3,6 @@ "dependencies": [ "command", "report", "users" ], "help": "http://github.com/reality/depressionbot/blob/master/modules/kick/README.md", "ignorable": true, - "countSilently": true + "countSilently": true, + "chanserv": "ChanServ" } From 3923a38a17399eda2e476f9ff7eac343975d437d Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 14 Apr 2013 15:41:40 +0000 Subject: [PATCH 3/6] syntax error --- modules/kick/kick.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/kick/kick.js b/modules/kick/kick.js index e5be508..6314c07 100644 --- a/modules/kick/kick.js +++ b/modules/kick/kick.js @@ -9,7 +9,7 @@ var kick = function(dbot) { 'quiet': function(server, user, channel) { dbot.say(server, this.config.chanserv, 'quiet ' + channel + ' ' + user) - } + }, 'kick': function(server, user, channel, msg) { dbot.instance.connections[server].send('KICK ' + channel + ' ' + user + ' :' + msg); From a628d12361f2fce054943a3ee433f4326edb6f26 Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 14 Apr 2013 17:21:24 +0000 Subject: [PATCH 4/6] Initial API module [#361] --- modules/api/api.js | 50 ++++++++++++++++++++++++++++++++++++++++++ modules/imgur/imgur.js | 2 ++ modules/web/web.js | 14 ++++++------ 3 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 modules/api/api.js diff --git a/modules/api/api.js b/modules/api/api.js new file mode 100644 index 0000000..1dcb58d --- /dev/null +++ b/modules/api/api.js @@ -0,0 +1,50 @@ +/** + * Name: API + * Description: Expose DBot API functionality with a REST API + */ +var _ = require('underscore')._; + +var api = function(dbot) { + this.onLoad = function() { + dbot.modules.web.app.get('/api/:module/:method', function(req, res) { + var module = req.params.module, + method = req.params.method, + body = { 'err': null, 'data': null }; + + if(!_.has(dbot.api, module)) { + body.err = 'No such API module'; + } else if(!_.has(dbot.api[module], method)) { + body.err = 'No such API function in ' + module; + } else if(dbot.api[module][method].external !== true) { + body.err = 'API function ' + module + '.' + method + + ' not enabled for external access'; + } + + if(!body.err) { + var func = dbot.api[module][method]; + var paramNames = func.extMap; + var args = []; + + // TODO: Use request params to map to extMap args + + var callbackIndex = paramNames.indexOf('callback'); + if(callbackIndex != -1) { + args[callbackIndex] = function() { + body.data = Array.prototype.slice.call(arguments, 0); + res.json(body); + }; + func.apply(null, args); + } else { + body.data = func.call(null, args); + res.json(body); + } + } else { + res.json(body); + } + }.bind(this)); + }.bind(this); +}; + +exports.fetch = function(dbot) { + return new api(dbot); +}; diff --git a/modules/imgur/imgur.js b/modules/imgur/imgur.js index 27209c1..90f091b 100644 --- a/modules/imgur/imgur.js +++ b/modules/imgur/imgur.js @@ -67,6 +67,8 @@ var imgur = function(dbot) { }); } }; + this.api['getRandomImage'].external = true; + this.api['getRandomImage'].extMap = [ 'callback' ]; this.commands = { '~ri': function(event) { diff --git a/modules/web/web.js b/modules/web/web.js index d860f32..4d0c702 100644 --- a/modules/web/web.js +++ b/modules/web/web.js @@ -3,17 +3,17 @@ var express = require('express'), fs = require('fs'); var webInterface = function(dbot) { - var pub = 'public'; - var app = express(); + this.pub = 'public'; + this.app = express(); - app.use(express.static(pub)); - app.set('view engine', 'jade'); + this.app.use(express.static(this.pub)); + this.app.set('view engine', 'jade'); - app.get('/', function(req, res) { + this.app.get('/', function(req, res) { res.render('index', { 'name': dbot.config.name }); }); - var server = app.listen(dbot.config.web.webPort); + var server = this.app.listen(dbot.config.web.webPort); this.reloadPages = function() { var pages = dbot.pages; @@ -21,7 +21,7 @@ var webInterface = function(dbot) { if(_.has(pages, p)) { var func = pages[p]; var mod = func.module; - app.get(p, (function(req, resp) { + this.app.get(p, (function(req, resp) { // Crazy shim to seperate module views. var shim = Object.create(resp); shim.render = (function(view, one, two) { From 1a1a08768a389258cba2e3437af494f615aa0d66 Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 14 Apr 2013 18:14:59 +0000 Subject: [PATCH 5/6] Map request functions to extMap. getQuoteCategory external API function [#361] --- modules/api/api.js | 16 +++++++++++----- modules/quotes/quotes.js | 13 +++++++++++++ modules/web/config.json | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/modules/api/api.js b/modules/api/api.js index 1dcb58d..1c10b98 100644 --- a/modules/api/api.js +++ b/modules/api/api.js @@ -9,6 +9,7 @@ var api = function(dbot) { dbot.modules.web.app.get('/api/:module/:method', function(req, res) { var module = req.params.module, method = req.params.method, + reqArgs = req.query, body = { 'err': null, 'data': null }; if(!_.has(dbot.api, module)) { @@ -21,11 +22,16 @@ var api = function(dbot) { } if(!body.err) { - var func = dbot.api[module][method]; - var paramNames = func.extMap; - var args = []; + var func = dbot.api[module][method], + paramNames = func.extMap, + args = []; - // TODO: Use request params to map to extMap args + _.each(reqArgs, function(arg, name) { + var callbackIndex = paramNames.indexOf(name); + if(callbackIndex != -1) { + args[callbackIndex] = decodeURIComponent(arg); + } + }); var callbackIndex = paramNames.indexOf('callback'); if(callbackIndex != -1) { @@ -35,7 +41,7 @@ var api = function(dbot) { }; func.apply(null, args); } else { - body.data = func.call(null, args); + body.data = func.apply(null, args); res.json(body); } } else { diff --git a/modules/quotes/quotes.js b/modules/quotes/quotes.js index 269e252..f616a84 100644 --- a/modules/quotes/quotes.js +++ b/modules/quotes/quotes.js @@ -83,8 +83,21 @@ var quotes = function(dbot) { return false; } } + }, + + 'getQuoteCategory': function(name) { + console.log(name); + var key = name.trim().toLowerCase(); + if(_.has(this.quotes, key)) { + return this.quotes[key]; + } else { + return false; + } } }; + + this.api['getQuoteCategory'].external = true; + this.api['getQuoteCategory'].extMap = [ 'name' ]; this.listener = function(event) { if(event.action == 'PRIVMSG') { diff --git a/modules/web/config.json b/modules/web/config.json index f714ecc..8087b2e 100644 --- a/modules/web/config.json +++ b/modules/web/config.json @@ -1,6 +1,6 @@ { "webHost": "localhost", - "webPort": 8080, + "webPort": 9001, "externalPath": false, "help": "https://github.com/reality/depressionbot/blob/master/modules/web/README.md" } From 2c03cbbbf37386fba2d429f5e3a4cd47f84f20e3 Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 14 Apr 2013 18:15:51 +0000 Subject: [PATCH 6/6] revert config web change --- modules/web/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/web/config.json b/modules/web/config.json index 8087b2e..f714ecc 100644 --- a/modules/web/config.json +++ b/modules/web/config.json @@ -1,6 +1,6 @@ { "webHost": "localhost", - "webPort": 9001, + "webPort": 8080, "externalPath": false, "help": "https://github.com/reality/depressionbot/blob/master/modules/web/README.md" }