Skip to content
This repository has been archived by the owner on Dec 21, 2018. It is now read-only.

Commit

Permalink
Check nickserv for user's ident status.
Browse files Browse the repository at this point in the history
  • Loading branch information
mythmon committed Aug 8, 2012
1 parent eccb48b commit 37e66b1
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 39 deletions.
56 changes: 56 additions & 0 deletions auth.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,56 @@
var events = require('events');
var _ = require('underscore');


this.AuthManager = function() {
this.neededLevel = 2;
this.STATUSRE = /^STATUS ([^ ]+) (\d)$/;
this.users = {};
};

this.AuthManager.prototype._user = function(nick) {
this.users[nick] = _.extend({}, {
auth: null,
emitter: new events.EventEmitter()
}, this.users[nick]);

return this.users[nick];
};

this.AuthManager.prototype.notice = function(nick, message) {
if (nick !== 'nickserv') {
return;
}
var match = this.STATUSRE.exec(message);
if (match) {
var user_nick = match[1];
var level = match[2];
var user = this._user(user_nick);

if (level >= this.neededLevel) {
user.auth = true;
user.emitter.emit('authorized');
} else {
user.auth = false;
user.emitter.emit('unauthorized');
}
}
};

this.AuthManager.prototype.checkUser = function(nick) {
var self = this;
var user = this._user(nick);
process.nextTick(function() {
if (user.auth) {
user.emitter.emit('authorized');
} else {
self._askNickserv(nick);
}
});
return user.emitter;
}

this.AuthManager.prototype._askNickserv = function(nick) {
var msg = 'status ' + nick;
client.say('nickserv', msg);
}
30 changes: 28 additions & 2 deletions standup-irc.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var inireader = require('inireader');


var utils = require('./utils'); var utils = require('./utils');
var api = require('./api'); var api = require('./api');
var auth = require('./auth');


var options = nomnom.opts({ var options = nomnom.opts({
config: { config: {
Expand Down Expand Up @@ -59,13 +60,17 @@ logger = new (winston.Logger)({
transports: transports transports: transports
}); });


// Global authentication manager
auth = new auth.AuthManager();

// This regex matches valid IRC nicks. // This regex matches valid IRC nicks.
var NICK_RE = /[a-z_\-\[\]\\{}|`][a-z0-9_\-\[\]\\{}|`]*/; var NICK_RE = /[a-z_\-\[\]\\{}|`][a-z0-9_\-\[\]\\{}|`]*/;
var TARGET_MSG_RE = new RegExp('^(?:(' + NICK_RE.source + ')[:,]\\s*)?(.*)$'); var TARGET_MSG_RE = new RegExp('^(?:(' + NICK_RE.source + ')[:,]\\s*)?(.*)$');


/********** IRC Client **********/ /********** IRC Client **********/


var client = new irc.Client(CONFIG.irc.host, CONFIG.irc.nick, { // Global client
client = new irc.Client(CONFIG.irc.host, CONFIG.irc.nick, {
channels: CONFIG.irc.channels channels: CONFIG.irc.channels
}); });
client.on('connect', function() { client.on('connect', function() {
Expand Down Expand Up @@ -125,6 +130,16 @@ client.on('message', function(user, channel, msg) {
} }
}); });


client.on('notice', function(from, to, text) {
if (from === undefined) {
from = '';
}
from = from.toLowerCase();
if (from === 'nickserv') {
auth.notice(from, text);
}
});

var commands = { var commands = {
/* Simple presence check. */ /* Simple presence check. */
ping: function(user, channel, message, args) { ping: function(user, channel, message, args) {
Expand Down Expand Up @@ -159,10 +174,10 @@ var commands = {
} else { } else {
client.say(channel, "I'm a failure, I couldn't do it."); client.say(channel, "I'm a failure, I couldn't do it.");
} }
console.log(data);
}); });
}, },


/* Every bot loves botsnacks. */
'botsnack': function(user, channel, message, args) { 'botsnack': function(user, channel, message, args) {
var responses = [ var responses = [
'Yummy!', 'Yummy!',
Expand All @@ -175,6 +190,17 @@ var commands = {
client.say(channel, responses[r]); client.say(channel, responses[r]);
}, },


/* Check a user's authorization status. */
'trust': function(user, channel, message, args) {
var a = auth.checkUser(args);
a.on('authorized', function() {
client.say(channel, 'I trust ' + args);
});
a.on('unauthorized', function() {
client.say(channel, "I don't trust " + args);
});
},

/* The default action. Return an error. */ /* The default action. Return an error. */
'default': function(user, channel, message) { 'default': function(user, channel, message) {
client.say(channel, user + ": Wait, what?"); client.say(channel, user + ": Wait, what?");
Expand Down
37 changes: 0 additions & 37 deletions utils.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,43 +2,6 @@ var _ = require('underscore');
var http = require('http'); var http = require('http');
var events = require('events'); var events = require('events');


/* Find a user's canonical username based on `canonicalNicks`.
*
* If no configured user can be matched, return `ircNick` unmodified.
*/
this.canonicalUsername = function(ircNick, canonicalNicks) {
var matches = [];
_.each(canonicalNicks, function(user) {
if (this.isPrefix(user, ircNick)) {
matches.push(user);
}
});
if (matches.length === 0) {
return ircNick;
}
// Sort by length.
matches.sort(function(a, b) {
return b.length - a.length;
});
// Grab the longest.
return matches[0];
};


// Check if `a` is a prefix of `b`.
this.isPrefix = function(a, b) {
var i;
if (a.length > b.length) {
return false;
}
for (i = 0; i < a.length; i++) {
if (a.charAt(i) !== b.charAt(i)) {
return false;
}
}
return true;
};



function request(path, method, data, emitter) { function request(path, method, data, emitter) {
if (data === undefined) { if (data === undefined) {
Expand Down

0 comments on commit 37e66b1

Please sign in to comment.