Permalink
Browse files

Adds web-proxy service and readme

  • Loading branch information...
1 parent e70e72d commit 1f97e578f7d1911aa0107dc5ef469fe5afa3ed7e @aaronpk committed Mar 22, 2013
Showing with 118 additions and 1 deletion.
  1. +34 −1 README.md
  2. +4 −0 config.json.dist
  3. +80 −0 web-proxy.js
View
@@ -1,4 +1,37 @@
zen-web-proxy
=============
-Simple web listener for sending messages to a ZenIRCBot
+Simple web listener for sending messages to a ZenIRCBot.
+
+### API
+
+#### `POST /raw`
+
+* `payload` - JSON payload to send to the ZenIRCBot. Will be sent in the "out" Redis channel. The parameter must be a form-encoded JSON string.
+* `token` - Auth token as specified in the config file.
+
+#### `POST /channel/:channel`
+
+Note: the "channel" parameter must include the URL-encoded hash sign.
+
+* `message` - Text to send to the channel
+* `token` - Auth token as specified in the config file.
+
+
+#### `GET /channel/:channel/nicks`
+
+* `token` - Auth token as specified in the config file. (As query string parameter.)
+
+Note: the "channel" parameter must include the URL-encoded hash sign.
+
+Response:
+
+```
+{
+ "nicks": {
+ "Loqi": "@",
+ "aaronpk": ""
+ }
+}
+```
+
View
@@ -0,0 +1,4 @@
+{
+ "port": 10000,
+ "authToken": ""
+}
View
@@ -0,0 +1,80 @@
+process.chdir(__dirname);
+
+var api = require('zenircbot-api');
+var bot_config = api.load_config('../../bot.json');
+var zen = new api.ZenIRCBot(bot_config.redis.host,
+ bot_config.redis.port,
+ bot_config.redis.db);
+var redis = zen.get_redis_client();
+var sub = zen.get_redis_client();
+var web_config = api.load_config('./config.json');
+var express = require('express');
+var app = express();
+
+function send_raw_command(command) {
+ redis.publish('out', JSON.stringify({
+ version: 1,
+ type: 'raw',
+ command: command
+ }));
+}
+
+// TODO: Add optional authentication via query string parameter
+
+zen.register_commands('web-proxy.js', []);
+
+app.use(express.bodyParser());
+
+app.post('/raw', function(req, res) {
+ if(req.body.token != web_config.authToken) {
+ res.send('forbidden', 403);
+ return;
+ }
+
+ console.log(req.body.payload);
+ redis.publish('out', req.body.payload);
+ res.send('ok', 200);
+});
+
+app.post('/channel/:channel', function(req, res) {
+ if(req.body.token != web_config.authToken) {
+ res.send('forbidden', 403);
+ return;
+ }
+
+ console.log(req.params.channel + ": " + req.body.message);
+ zen.send_privmsg(req.params.channel, req.body.message);
+ res.send('ok', 200);
+});
+
+var openRequests = [];
+
+app.get('/channel/:channel/nicks', function(req, res) {
+ if(req.query.token != web_config.authToken) {
+ res.send('forbidden', 403);
+ return;
+ }
+
+ openRequests.push(res);
+ send_raw_command("NAMES "+req.params.channel);
+});
+
+app.listen(web_config.port);
+
+sub.subscribe('in');
+sub.on('message', function(channel, message) {
+ var data = JSON.parse(message);
+
+ if(data.type == 'names') {
+ var response = {
+ nicks: data.data.nicks
+ };
+
+ for(var i in openRequests) {
+ openRequests[i].send(JSON.stringify(response), 200);
+ }
+ openRequests = [];
+ }
+});
+
+console.log("Listening on "+web_config.port);

0 comments on commit 1f97e57

Please sign in to comment.