2013-01-12 22:40:16 +01:00
|
|
|
var _ = require('underscore')._;
|
|
|
|
|
2012-12-11 17:04:52 +01:00
|
|
|
var quotes = function(dbot) {
|
2013-01-15 15:47:46 +01:00
|
|
|
this.name = 'quotes';
|
|
|
|
this.ignorable = true;
|
|
|
|
|
2013-01-15 15:51:07 +01:00
|
|
|
dbot.sessionData.rmCache = [];
|
2013-01-15 15:47:46 +01:00
|
|
|
this.quotes = dbot.db.quoteArrs,
|
|
|
|
this.addStack = [],
|
|
|
|
this.rmAllowed = true,
|
|
|
|
this.rmCache = dbot.sessionData.rmCache,
|
|
|
|
this.rmTimer;
|
2012-12-11 17:04:52 +01:00
|
|
|
|
2013-01-15 15:47:46 +01:00
|
|
|
this.internalAPI = {
|
|
|
|
// Retrieve a random quote from a given category, interpolating any quote
|
|
|
|
// references (~~QUOTE CATEGORY~~) within it
|
|
|
|
'interpolatedQuote': function(event, key, quoteTree) {
|
|
|
|
if(!_.isUndefined(quoteTree) && quoteTree.indexOf(key) != -1) {
|
|
|
|
return '';
|
|
|
|
} else if(_.isUndefined(quoteTree)) {
|
|
|
|
quoteTree = [];
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
|
2013-01-15 15:47:46 +01:00
|
|
|
var index = _.random(0, this.quotes[key].length - 1);
|
|
|
|
var quoteString = this.quotes[key][index];
|
|
|
|
|
|
|
|
// Parse quote interpolations
|
|
|
|
var quoteRefs = quoteString.match(/~~([\d\w\s-]*)~~/g);
|
|
|
|
var thisRef;
|
|
|
|
|
|
|
|
while(quoteRefs && (thisRef = quoteRefs.shift()) !== undefined) {
|
|
|
|
var cleanRef = dbot.cleanNick(thisRef.replace(/^~~/,'').replace(/~~$/,'').trim());
|
|
|
|
if(cleanRef === '-nicks-') {
|
|
|
|
var randomNick = _.keys(event.channel.nicks)[_.random(0, _.size(event.channel.nicks) -1)];
|
|
|
|
quoteString = quoteString.replace("~~" + cleanRef + "~~", randomNick);
|
|
|
|
quoteTree.pop();
|
|
|
|
} else if(_.has(this.quotes, cleanRef)) {
|
|
|
|
quoteTree.push(key);
|
|
|
|
quoteString = quoteString.replace("~~" + cleanRef + "~~",
|
|
|
|
interpolatedQuote(event, cleanRef, quoteTree.slice()));
|
|
|
|
quoteTree.pop();
|
|
|
|
}
|
|
|
|
}
|
2012-12-19 22:03:03 +01:00
|
|
|
|
2013-01-15 15:47:46 +01:00
|
|
|
return quoteString;
|
|
|
|
}.bind(this),
|
2013-01-12 22:40:16 +01:00
|
|
|
|
2013-01-15 15:47:46 +01:00
|
|
|
'resetRemoveTimer': function(event, key, quote) {
|
|
|
|
this.rmAllowed = false;
|
|
|
|
dbot.timers.addOnceTimer(5000, function() {
|
|
|
|
this.rmAllowed = true;
|
2012-12-19 22:03:03 +01:00
|
|
|
});
|
2013-01-15 15:47:46 +01:00
|
|
|
|
|
|
|
this.rmCache.push({
|
|
|
|
'key': key,
|
|
|
|
'quote': quote
|
2013-01-12 22:40:16 +01:00
|
|
|
});
|
2013-01-15 15:47:46 +01:00
|
|
|
dbot.timers.clearTimeout(this.rmTimer);
|
|
|
|
|
|
|
|
if(this.rmCache.length < dbot.config.quotes.rmLimit) {
|
|
|
|
this.rmTimer = dbot.timers.addOnceTimer(600000, function() {
|
|
|
|
this.rmCache.length = 0; // lol what
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_.each(dbot.config.admins, function(admin) {
|
|
|
|
dbot.say(event.server, admin, dbot.t('rm_cache_limit'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}.bind(this)
|
2012-12-19 22:03:03 +01:00
|
|
|
};
|
|
|
|
|
2013-01-15 15:47:46 +01:00
|
|
|
this.api = {
|
2013-01-12 12:39:17 +01:00
|
|
|
'getQuote': function(event, category) {
|
2013-01-03 22:27:06 +01:00
|
|
|
var key = category.trim().toLowerCase();
|
|
|
|
var altKey;
|
|
|
|
if(key.split(' ').length > 0) {
|
|
|
|
altKey = key.replace(/ /g, '_');
|
|
|
|
}
|
|
|
|
|
|
|
|
if(key.charAt(0) !== '_') { // lol
|
2013-01-15 15:47:46 +01:00
|
|
|
if(_.has(this.quotes, key)) {
|
|
|
|
return this.internalAPI.interpolatedQuote(event, key);
|
|
|
|
} else if(_.has(this.quotes, altKey)) {
|
|
|
|
return this.internalAPI.interpolatedQuote(event, altKey);
|
2013-01-03 22:27:06 +01:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-01-15 15:47:46 +01:00
|
|
|
|
|
|
|
this.listener = function(event) {
|
|
|
|
if(event.action == 'PRIVMSG') {
|
|
|
|
if(event.user == 'reality') {
|
|
|
|
var once = event.message.valMatch(/^I ([\d\w\s,'-]* once)/, 2);
|
2012-12-19 22:03:03 +01:00
|
|
|
} else {
|
2013-01-15 15:47:46 +01:00
|
|
|
var once = event.message.valMatch(/^reality ([\d\w\s,'-]* once)/, 2);
|
2012-12-19 22:03:03 +01:00
|
|
|
}
|
|
|
|
|
2013-01-15 15:47:46 +01:00
|
|
|
if(once) {
|
|
|
|
event.message = '~qadd realityonce=reality ' + once[1];
|
|
|
|
event.action = 'PRIVMSG';
|
|
|
|
event.params = event.message.split(' ');
|
|
|
|
dbot.instance.emit(event);
|
|
|
|
}
|
|
|
|
} else if(event.action == 'JOIN') {
|
|
|
|
var userQuote = this.api.getQuote(event, event.user)
|
|
|
|
if(userQuote) {
|
|
|
|
event.reply(event.user + ': ' + this.api.getQuote(event, event.user));
|
2012-12-19 22:03:03 +01:00
|
|
|
}
|
2013-01-15 15:47:46 +01:00
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
this.on = ['PRIVMSG', 'JOIN'];
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
2013-01-15 15:47:46 +01:00
|
|
|
return new quotes(dbot);
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|