Permalink
Browse files

Check nickserv for user's ident status.

  • Loading branch information...
1 parent eccb48b commit 37e66b1447cd7f054a6586718a3d584ac3a580eb @mythmon mythmon committed Aug 8, 2012
Showing with 84 additions and 39 deletions.
  1. +56 −0 auth.js
  2. +28 −2 standup-irc.js
  3. +0 −37 utils.js
View
56 auth.js
@@ -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);
+}
View
@@ -6,6 +6,7 @@ var inireader = require('inireader');
var utils = require('./utils');
var api = require('./api');
+var auth = require('./auth');
var options = nomnom.opts({
config: {
@@ -59,13 +60,17 @@ logger = new (winston.Logger)({
transports: transports
});
+// Global authentication manager
+auth = new auth.AuthManager();
+
// This regex matches valid IRC nicks.
var NICK_RE = /[a-z_\-\[\]\\{}|`][a-z0-9_\-\[\]\\{}|`]*/;
var TARGET_MSG_RE = new RegExp('^(?:(' + NICK_RE.source + ')[:,]\\s*)?(.*)$');
/********** 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
});
client.on('connect', function() {
@@ -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 = {
/* Simple presence check. */
ping: function(user, channel, message, args) {
@@ -159,10 +174,10 @@ var commands = {
} else {
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) {
var responses = [
'Yummy!',
@@ -175,6 +190,17 @@ var commands = {
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. */
'default': function(user, channel, message) {
client.say(channel, user + ": Wait, what?");
View
@@ -2,43 +2,6 @@ var _ = require('underscore');
var http = require('http');
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) {
if (data === undefined) {

0 comments on commit 37e66b1

Please sign in to comment.