Permalink
Browse files

moved bot logic and commands to /lib, created daemon aptly named pwnd…

…, prepped npm package
  • Loading branch information...
1 parent 96d6a76 commit 023f6263c7ab5464c8f17d4cbf6b121066075928 @cpetzold committed Feb 16, 2012
View
@@ -0,0 +1,78 @@
+#!/usr/bin/env node
+
+/**
+ * Module dependencies.
+ */
+
+var fs = require('fs')
+ , optimist = require('optimist')
+ , colors = require('colors')
+ , log = require('logule')
+ , pwn = require('../');
+
+/**
+ * Set arguments and help/usage
+ */
+
+var opts = optimist
+ .usage('\nRuns the pwnbot daemon'.underline.blue + '\n' + 'Usage: $0'.grey)
+ .options('h', {
+ describe: 'Displays this message'
+ , alias: 'help'
+ , boolean: true
+ })
+ .options('c', {
+ describe: 'Path to bot config json file'
+ , alias: 'config'
+ , default: './config.json'
+ });
+
+var argv = opts.argv;
+
+if (argv.h) {
+ opts.showHelp();
+ process.exit();
+}
+
+/**
+ * Load up configuration.
+ */
+
+var config, cf;
+
+try {
+ cf = fs.readFileSync(argv.c)
+} catch (e) {
+ fatal('Unable to read ' + argv.c);
+}
+
+try {
+ config = JSON.parse(cf);
+} catch (e) {
+ fatal('Unable to parse config');
+}
+
+/**
+ * Create the bot and connect
+ */
+
+var bot = pwn.createBot(config);
+bot.connect();
+
+/**
+ * Fatal error
+ */
+
+function fatal(msg) {
+ log.error(msg.red);
+ log.info('Exiting');
+ process.exit(1);
+}
+
+/**
+ * Don't crash on uncaught exception
+ */
+
+process.on('uncaughtException', function(e) {
+ console.error('Exception %s: %s', e.type, e.stack || e.message);
+});
View
103 index.js
@@ -1,100 +1,5 @@
+exports.Bot = require('./lib/bot');
-/**
- * Module dependencies.
- */
-
-var irc = require('irc')
- , fs = require('fs')
- , c = require('irc-colors').global()
-
-/**
- * Environment.
- */
-
-var env = process.env.NODE_ENV || 'development';
-
-/**
- * Load up configuration.
- */
-
-var config;
-
-try {
- config = JSON.parse(fs.readFileSync(__dirname + '/config.json', 'utf8'));
-} catch (e) {
- console.error('\033[90mconfig.json unparseable - ignoring\033[39m');
- config = {
- server: 'irc.the0th.com'
- , nickname: 'pwnbot'
- , channels: ['#pwn']
- , debug: true
- , prefix: '!'
- };
-}
-
-/**
- * Override debug for dev.
- */
-
-if ('development' == env) {
- config.debug = true;
-}
-
-/**
- * Don't crash on uncaught exception
- */
-process.on('uncaughtException', function(e) {
- console.error('Exception %s: %s', e.type, e.stack || e.message);
-});
-
-/**
- * Initialize bot.
- */
-
-var bot = new irc.Client(config.server, config.nickname, config);
-bot.config = config;
-
-/**
- * Register commands.
- */
-
-var commands = {}
- , cmdReg = new RegExp('^((' + config.nickname + ':?)? *!([^ ]+) *)', 'i')
-
-fs.readdirSync('./commands').forEach(function (file) {
- if (file.search(/js$/) != -1) {
- var cmd = require('./commands/' + file);
- commands[cmd.name] = cmd(bot);
- }
-});
-
-bot.on('message', function (from, to, message) {
- // test for a command
- var match = message.match(cmdReg)
- if (!match) {
- return;
- }
-
- var cmd = match[3]
-
- if (commands[cmd]) {
- if (config.debug) {
- console.error('\033[90mgot command - ' + cmd + '\033[39m');
- }
-
- // get raw message
- var msg = message.replace(match[0], '');
-
- // parse options
- var options = {}, opt;
-
- while(opt = msg.match(/([^:]+):((\"([^\"]+)\")|([^ ]+)) */)){
- options[opt[1]] = opt[5] || opt[4];
- msg = msg.replace(opt[0], '');
- }
-
- commands[cmd](msg, function (msg) {
- bot.say(to, msg);
- }, options, from, to);
- }
-});
+exports.createBot = function(config) {
+ return new exports.Bot(config);
+};
View
@@ -0,0 +1,81 @@
+/**
+ * Module dependencies.
+ */
+
+var fs = require('fs')
+ , irc = require('irc')
+ , log = require('logule')
+ , c = require('irc-colors').global()
+
+var Bot = module.exports = function(config) {
+ this.config = config;
+ this.cmdReg = this.config.cmdReg || new RegExp('^((' + config.nickname + ':?)? *!([^ ]+) *)', 'i');
+
+ this.config.autoConnect = false;
+ this.client = new irc.Client(this.config.server, this.config.nickname, this.config);
+
+ this.commands = this._initCommands();
+ this._debug('Commands initialized');
+
+ this.client.on('message', this._onMessage.bind(this));
+};
+
+Bot.prototype.connect = function(retryCount, fn) {
+ var self = this;
+ this.client.connect(retryCount, function() {
+ self._debug('Connected');
+ fn();
+ });
+};
+
+Bot.prototype.say = function() {
+ this.client.say.apply(this.client, arguments);
+};
+
+Bot.prototype._onMessage = function(from, to, message) {
+ var self = this
+ , match = message.match(this.cmdReg)
+
+ if (!match) {
+ return;
+ }
+
+ var cmd = match[3]
+
+ if (this.commands[cmd]) {
+ this._debug('Got command:', cmd);
+
+ // get raw message
+ var msg = message.replace(match[0], '');
+
+ // parse options
+ var options = {}, opt;
+
+ while(opt = msg.match(/([^:]+):((\"([^\"]+)\")|([^ ]+)) */)){
+ options[opt[1]] = opt[5] || opt[4];
+ msg = msg.replace(opt[0], '');
+ }
+
+ this.commands[cmd](msg, function (msg) {
+ self.client.say(to, msg);
+ }, options, from, to);
+ }
+};
+
+Bot.prototype._initCommands = function() {
+ var self = this
+ , commands = {};
+ fs.readdirSync(__dirname + '/commands').forEach(function (file) {
+ if (file.search(/js$/) != -1) {
+ var cmd = require(__dirname + '/commands/' + file);
+ commands[cmd.name] = cmd(self);
+ }
+ });
+ return commands;
+};
+
+Bot.prototype._debug = function() {
+ if (this.config.debug) {
+ log.debug.apply(this, arguments);
+ }
+};
File renamed without changes.
File renamed without changes.
@@ -56,7 +56,6 @@ function define (bot) {
return;
}
var word = text.trim();
- console.log(word);
var message = word.irc.bold.silver() + ': ';
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
View
@@ -1,20 +1,26 @@
{
- "author": "Conner Petzold <conner@storify.com>"
- , "name": "storify-bot"
- , "version": "0.0.0"
- , "repository": {
- "url": ""
- }
- , "engines": {
- "node": "v0.4.9"
- }
+ "name": "pwnbot"
+ , "version": "0.0.1"
+ , "author": "Conner Petzold <cpetzold@gmail.com>"
+ , "contributors": [
+ { "name": "Guillermo Rauch", "email": "rauchg@gmail.com" }
+ , { "name": "Brendan Nee", "email": "brendan.nee@gmail.com" }
+ ]
+ , "keywords": ["irc", "bot", "pwn"]
+ , "repository": "git://github.com/cpetzold/pwnbot"
+ , "engines": { "node": "> v0.4.x < 0.7.0" }
+ , "main": "./index.js"
+ , "bin": { "pwnd": "./bin/pwnd" }
, "dependencies": {
- "irc": "0.2.1"
- , "irc-colors": "1.0.1"
- , "asciimo": "0.3.1"
+ "irc": "0.2.x"
+ , "irc-colors": "1.0.x"
+ , "optimist": "0.3.x"
+ , "colors": "0.6.x"
+ , "logule": "0.5.x"
+ , "asciimo": "0.3.x"
, "wordnik": "0.0.x"
- , "request": "2.1.1"
- , "timeago": "0.0.2"
- , "xml2js": "0.1.11"
+ , "request": "2.1.x"
+ , "timeago": "0.0.x"
+ , "xml2js": "0.1.x"
}
}

0 comments on commit 023f626

Please sign in to comment.