Last active
December 16, 2015 05:59
-
-
Save oskarhane/5388415 to your computer and use it in GitHub Desktop.
A XMPP bot client that takes commands and replies the result. You can easily add commands your self and remote load them, or manually upload them to the conf dir.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Author: Oskar Hane | |
* Contact: oh@oskarhane.com | |
* URL: http://oskarhane.com | |
* | |
* Dependenies: simple-xmpp and optimist. Install them via npm. | |
* | |
* Run with params | |
* -u username (e.g. you@domain.com) | |
* -p password | |
* -h host | |
* --port portnum | |
* --master who is your daddy? (eg. master@domain.com). This account is auto added as friend. | |
* | |
* node app.js -u you@domain.com -p secret1 -h 'xmpp.server.com' --port 5222 --master daddy@domain.com | |
* | |
* When the bot connects to the network you can write to it. See uptime.js for more info on commands. | |
* | |
*/ | |
var xmpp = require('simple-xmpp'); | |
var argv = require('optimist').argv; | |
var url = require('url'); | |
var http = require('http'); | |
var fs = require('fs'); | |
var conf_dir = 'conf/'; | |
var module_settings = { | |
host: 'your.module-server.com', | |
port: 8081 | |
} | |
var commands = {}; | |
var helper = require('helper'); | |
argv.master = argv.master || ''; | |
xmpp.on('chat', function(from, message) | |
{ | |
var command = message.split(' ')[0]; | |
//helper.sendMessage(from, 'Command: ' + command + ' received'); | |
if(/^add\s/.test(message) && (from == argv.master || !argv.master)) | |
{ | |
helper.sendMessage(from, 'Parsing download URL'); | |
var fn = message.split(' ')[1]; | |
fn = helper.getFilenameFromURL(fn); | |
var options = { | |
hostname: module_settings.host, | |
path: '/' + fn, | |
port: module_settings.port | |
}; | |
var file = fs.createWriteStream(conf_dir + fn); | |
var request = http.get(options, function(response) | |
{ | |
helper.sendMessage(from, 'Downloading file: ' + fn); | |
if(response.statusCode != 200) | |
{ | |
helper.sendMessage(from, 'Module: ' + fn + ' not found. Try again!'); | |
file.end(); | |
fs.unlink(conf_dir + fn); | |
return false; | |
} | |
response.on('data', function (chunk) | |
{ | |
file.write(chunk); | |
}); | |
response.on('end', function() | |
{ | |
file.end(); | |
helper.loadConfFile(fn, commands); | |
helper.sendMessage(from, 'File: ' + fn + ' loaded. Try it out!'); | |
}); | |
response.on('error', function(err) | |
{ | |
console.log(err); | |
helper.sendMessage(from, 'File: ' + fn + ' not loaded. Error: ' + err); | |
}); | |
}); | |
request.on('error', function(err) | |
{ | |
console.log(err); | |
}); | |
} | |
else if(/^remove\s/.test(message) && (from == argv.master || !argv.master)) | |
{ | |
var fn = message.split(' ')[1]; | |
fn = helper.getFilenameFromURL(fn); | |
var cmd = fn.replace('.js', ''); | |
delete commands[cmd]; | |
fs.unlink(conf_dir + fn, function() | |
{ | |
helper.sendMessage(from, 'File: ' + fn + ' removed'); | |
}); | |
} | |
else if(commands.hasOwnProperty(command) && (from == argv.master || !argv.master)) | |
commands[command](from, message); | |
else | |
{ | |
var tmp = []; | |
for(var j in commands) | |
tmp.push(j); | |
helper.sendMessage(from, 'Command not found.\nAvailable commands: ' + tmp.join(', ')); | |
} | |
}); | |
xmpp.on('online', function() { | |
console.log('Connected!'); | |
}); | |
xmpp.on('error', function(err) { | |
console.error(err); | |
}); | |
xmpp.on('subscribe', function(from) { | |
if (from === argv.master) { | |
xmpp.acceptSubscription(from); | |
} | |
}); | |
xmpp.connect({ | |
jid : argv.u, | |
password : argv.p, | |
host : argv.h, | |
port : argv.port || 5222 | |
}); | |
xmpp.getRoster(); | |
if(argv.master) | |
xmpp.subscribe(argv.master); | |
helper.setConfDir(conf_dir); | |
helper.setMsgObj(xmpp); | |
helper.loadInitialConfFiles(fs, commands); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var helper = {}; | |
helper.setConfDir = function(dir) | |
{ | |
this.conf_dir = dir; | |
} | |
helper.setMsgObj = function(obj) | |
{ | |
this.msg = obj; | |
} | |
helper.loadInitialConfFiles = function(fs, command_list) | |
{ | |
var files = fs.readdirSync(this.conf_dir); | |
for(var i in files) | |
{ | |
this.loadConfFile(files[i], command_list); | |
} | |
} | |
helper.loadConfFile = function(fn, command_list) | |
{ | |
fn = fn.replace('.js', ''); | |
require(this.conf_dir + fn)(command_list, this); | |
console.log(fn + ' loaded'); | |
} | |
helper.sendMessage = function(stanza, str) | |
{ | |
this.msg.send(stanza, str); | |
} | |
helper.getFilenameFromURL = function(url) | |
{ | |
var tmp = url.replace('.js', ''); | |
return (tmp + '.js'); | |
} | |
module.exports = helper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* IMPORTANT: This should be in a sub directory call "conf". | |
* | |
* This is a module for the bot. You can add as many as you want. Just put them inside the conf/ dir. | |
* You can also add modules from a remote host. Configure the host in app.js (module_settings) and put the js files | |
* in the root dir on that server. | |
* | |
* Now you can type "add uptime" to the bot and it will download the uptime.js from the server, and activate the module. | |
* "remove uptime" deletes the file from the bot's conf dir. | |
*/ | |
var sys = require('sys'); | |
var exec = require('child_process').exec; | |
var child; | |
var uptime = function(holder, helper) | |
{ | |
holder['uptime'] = function(from, message) | |
{ | |
child = exec("uptime", function (error, stdout, stderr) | |
{ | |
helper.sendMessage(from, stdout); | |
}); | |
}; | |
} | |
module.exports = uptime; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment