2013-04-13 00:30:45 +02:00
|
|
|
var _ = require('underscore')._,
|
|
|
|
uuid = require('node-uuid');
|
2013-01-12 22:40:16 +01:00
|
|
|
|
2012-12-11 17:04:52 +01:00
|
|
|
var quotes = function(dbot) {
|
2013-01-15 15:51:07 +01:00
|
|
|
dbot.sessionData.rmCache = [];
|
2013-04-13 00:30:45 +02:00
|
|
|
this.rmCache = dbot.sessionData.rmCache;
|
|
|
|
this.quotes = dbot.db.quoteArrs;
|
|
|
|
this.rmAllowed = true;
|
2013-01-15 15:47:46 +01:00
|
|
|
this.rmTimer;
|
2012-12-11 17:04:52 +01:00
|
|
|
|
2013-01-15 15:47:46 +01:00
|
|
|
this.internalAPI = {
|
2013-04-13 00:30:45 +02:00
|
|
|
// Parse quote interpolations
|
2013-09-01 00:57:29 +02:00
|
|
|
'interpolatedQuote': function(server, channel, username, key, quote, callback) {
|
2013-04-12 23:08:11 +02:00
|
|
|
var quoteRefs = quote.match(/~~([\d\w\s-]*)~~/g);
|
|
|
|
if(quoteRefs) {
|
2015-08-13 16:36:22 +02:00
|
|
|
var ref = quoteRefs[0].replace(/^~~/,'').replace(/~~$/,'').trim();
|
2013-04-12 23:08:11 +02:00
|
|
|
if(ref === '-nicks-') {
|
2014-09-08 12:41:09 +02:00
|
|
|
if(_.has(dbot.instance.connections[server].channels, channel)) {
|
|
|
|
var nicks =
|
|
|
|
_.keys(dbot.instance.connections[server].channels[channel].nicks);
|
|
|
|
var randomUser = nicks[_.random(0, nicks.length -1)];
|
|
|
|
quote = quote.replace('~~' + ref + '~~', randomUser);
|
2013-09-01 00:57:29 +02:00
|
|
|
this.internalAPI.interpolatedQuote(server, channel,
|
|
|
|
username, key, quote, callback);
|
2014-09-08 12:41:09 +02:00
|
|
|
}
|
2013-09-01 00:57:29 +02:00
|
|
|
} else if(ref === '-nick-') {
|
|
|
|
quote = quote.replace('~~' + ref + '~~', username);
|
|
|
|
this.internalAPI.interpolatedQuote(server, channel,
|
|
|
|
username, key, quote, callback);
|
2013-04-12 23:08:11 +02:00
|
|
|
} else {
|
|
|
|
this.api.getQuote(ref, function(interQuote) {
|
2015-08-13 16:36:22 +02:00
|
|
|
if(!interQuote) {
|
2013-04-12 23:08:11 +02:00
|
|
|
interQuote = '';
|
|
|
|
}
|
|
|
|
quote = quote.replace('~~' + ref + '~~', interQuote);
|
2013-09-01 00:57:29 +02:00
|
|
|
this.internalAPI.interpolatedQuote(server, channel,
|
|
|
|
username, key, quote, callback);
|
2015-08-13 16:36:22 +02:00
|
|
|
}.bind(this), true);
|
2013-01-15 15:47:46 +01:00
|
|
|
}
|
2013-04-12 23:08:11 +02:00
|
|
|
} else {
|
|
|
|
callback(quote);
|
2013-01-15 15:47:46 +01:00
|
|
|
}
|
|
|
|
}.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;
|
2013-01-27 21:52:11 +01:00
|
|
|
setTimeout(function() {
|
2013-01-15 15:47:46 +01:00
|
|
|
this.rmAllowed = true;
|
2013-01-27 21:52:11 +01:00
|
|
|
}.bind(this), 5000);
|
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
|
|
|
|
2013-01-27 21:33:27 +01:00
|
|
|
clearTimeout(this.rmTimer);
|
2013-06-27 17:26:21 +02:00
|
|
|
if(this.rmCache.length < this.config.rmLimit) {
|
2013-01-27 21:52:11 +01:00
|
|
|
this.rmTimer = setTimeout(function() {
|
2013-01-15 15:47:46 +01:00
|
|
|
this.rmCache.length = 0; // lol what
|
2013-01-27 21:52:11 +01:00
|
|
|
}.bind(this), 600000);
|
2013-01-15 15:47:46 +01:00
|
|
|
} 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-04-13 00:30:45 +02:00
|
|
|
'addQuote': function(key, quote, user, callback) {
|
|
|
|
var key = key.toLowerCase().trim(),
|
|
|
|
newCount,
|
|
|
|
category = false;
|
|
|
|
|
|
|
|
this.db.search('quote_category', { 'name': key }, function(result) {
|
|
|
|
category = result;
|
|
|
|
}, function(err) {
|
|
|
|
if(!category) {
|
|
|
|
var id = uuid.v4();
|
|
|
|
category = {
|
|
|
|
'id': id,
|
|
|
|
'name': key,
|
|
|
|
'quotes': [],
|
|
|
|
'owner': user
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-04-22 17:50:05 +02:00
|
|
|
if(_.include(category.quotes, quote)) {
|
|
|
|
callback(false);
|
|
|
|
} else {
|
|
|
|
newCount = category.quotes.push(quote);
|
|
|
|
this.db.save('quote_category', category.id, category, function(err) {
|
|
|
|
this.rmAllowed = true;
|
|
|
|
callback(newCount);
|
|
|
|
}.bind(this));
|
|
|
|
}
|
2013-04-13 00:30:45 +02:00
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2015-08-13 16:36:22 +02:00
|
|
|
'getQuote': function(key, callback, removeRefs) {
|
2013-04-23 19:26:44 +02:00
|
|
|
this.api.getQuoteCategory(key, function(category) {
|
2013-04-12 23:08:11 +02:00
|
|
|
if(category) {
|
|
|
|
var quotes = category.quotes;
|
2015-08-13 16:36:22 +02:00
|
|
|
|
|
|
|
if(removeRefs) {
|
|
|
|
quotes = _.filter(quotes, function(q) {
|
2021-08-24 21:16:26 +02:00
|
|
|
console.log('DEBUG (if removeRefs): ' + removeRefs + quotes + key)
|
2015-08-13 16:36:22 +02:00
|
|
|
return !q.match('~~'+key+'~~');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-04-12 23:08:11 +02:00
|
|
|
var index = _.random(0, quotes.length - 1);
|
|
|
|
callback(quotes[index]);
|
2013-01-03 22:27:06 +01:00
|
|
|
} else {
|
2013-04-12 23:08:11 +02:00
|
|
|
callback(false);
|
2013-01-03 22:27:06 +01:00
|
|
|
}
|
2013-04-12 23:08:11 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-09-01 00:57:29 +02:00
|
|
|
'getInterpolatedQuote': function(server, channel, username, key, callback) {
|
2021-08-24 21:16:26 +02:00
|
|
|
console.log('DEBUG: ' + channel + ' ' + username + ' ' + key)
|
2013-04-12 23:08:11 +02:00
|
|
|
key = key.trim().toLowerCase(),
|
|
|
|
|
|
|
|
this.api.getQuote(key, function(quote) {
|
2013-04-22 16:22:35 +02:00
|
|
|
if(quote) {
|
2013-09-01 00:57:29 +02:00
|
|
|
this.internalAPI.interpolatedQuote(server, channel,
|
|
|
|
username, key, quote, callback);
|
2013-04-22 16:22:35 +02:00
|
|
|
} else {
|
2021-08-24 21:16:26 +02:00
|
|
|
console.log('DEBUG (else callback quote): ' + quote)
|
2013-04-22 16:22:35 +02:00
|
|
|
callback(quote);
|
|
|
|
}
|
2013-04-12 23:08:11 +02:00
|
|
|
}.bind(this));
|
2013-04-23 19:26:44 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'getQuoteCategory': function(key, callback) {
|
2021-08-24 21:16:26 +02:00
|
|
|
console.log('DEBUG: ' + key)
|
2013-04-23 19:26:44 +02:00
|
|
|
var category = false,
|
|
|
|
key = key.trim().toLowerCase();
|
|
|
|
|
|
|
|
this.db.search('quote_category', { 'name': key }, function(result) {
|
2021-08-24 21:16:26 +02:00
|
|
|
console.log('DEBUG: ' + key + ' ' + result)
|
2013-04-23 19:26:44 +02:00
|
|
|
category = result;
|
|
|
|
}, function(err) {
|
|
|
|
callback(category);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
'getCategoryKeys': function(callback) {
|
|
|
|
var keys = [];
|
|
|
|
this.db.scan('quote_category', function(result) {
|
2021-08-24 21:16:26 +02:00
|
|
|
console.log('DEBUG: ' + result)
|
2013-04-23 19:26:44 +02:00
|
|
|
if(result) keys.push(result.name);
|
|
|
|
}, function(err) {
|
|
|
|
callback(keys);
|
|
|
|
});
|
2013-01-03 22:27:06 +01:00
|
|
|
}
|
|
|
|
};
|
2013-11-19 01:16:17 +01:00
|
|
|
|
|
|
|
this.api['getQuote'].external = true;
|
|
|
|
this.api['getQuote'].extMap = [ 'key', 'callback' ];
|
2015-02-06 13:40:15 +01:00
|
|
|
this.api['getQuoteCategory'].external = true;
|
|
|
|
this.api['getQuoteCategory'].extMap = [ 'key', 'callback' ];
|
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) {
|
2013-04-13 00:30:45 +02:00
|
|
|
this.api.addQuote('realityonce', 'reality' + once[1], event.user, function(newCount) {
|
|
|
|
event.reply('\'reality ' + once[1] + '\' saved (' + newCount + ').');
|
|
|
|
});
|
2013-01-15 15:47:46 +01:00
|
|
|
}
|
|
|
|
} else if(event.action == 'JOIN') {
|
2013-05-17 09:04:57 +02:00
|
|
|
if(this.config.quotesOnJoin == true) {
|
2013-05-17 09:40:46 +02:00
|
|
|
this.api.getQuote(event.user, function(quote) {
|
|
|
|
if(quote) {
|
|
|
|
event.reply(event.user + ': ' + quote);
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
};
|