<commit_before>import FactoidClass = require('./Factoid');
var Factoid = FactoidClass.Factoid;

export class Plugin {
	bot:any;
	config:any;
	database:any;
	client:any;
	commands:any;
	factoids:any;

	factoidSchema:any;
	Factoid:any;

	constructor(bot:any, config:any) {
		this.bot = bot;
		this.config = config;
		this.database = bot.database;
		this.client = bot.client;
		this.commands = {
			'remember': 'onCommandRemember',
			'r': 'onCommandRemember',
			'forget': 'onCommandForget',
			'f': 'onCommandForget'
		};

	}

	onCommandForget(from:string, to:string, message:string, args:any) {
        this.bot.hasAccess(from, to, this.config.acl, (function(hasAccess) {
            if(!hasAccess) return;

            if (args.length < 2) {
                this.bot.reply(from, to, 'Usage: ' + this.bot.config.bot.command + args[0] + ' <factoid>', 'notice');
                return;
            }
            var factoidName = args[1].toLowerCase();


            var factoid = new Factoid(this.database);
            factoid.forgetActive(factoidName, (function(err, numAffected) {
                if(err) {
                    this.bot.log.warn(err);
                    this.bot.reply(from, to, 'An error occurred', 'notice');
                    return;
                }

                this.bot.reply(from, to, 'Forgot: ' + factoidName, 'notice');
            }).bind(this));
        }).bind(this));
	}

	onCommandRemember(from:string, to:string, message:string, args:any) {
        this.bot.hasAccess(from, to, this.config.acl, (function(hasAccess) {
            if(!hasAccess) return;

            if (args.length < 3) {
                this.bot.reply(from, to, 'Usage: ' + this.bot.config.bot.command + args[0] + ' <factoid> <text>', 'notice');
                return;
            }

            var factoidName = args[1].toLowerCase();

            var contents = args.splice(2);
            contents = contents.join(' ').trim();

            var factoid = new Factoid(this.database);

            factoid.factoid = factoidName;
            factoid.content = contents;
            factoid.owner = from;
            factoid.channel = (to.charAt(0) == '#' ? to : '');

            var plugin = this;
            factoid.save(function saveFactoid(numAffected, err, factoid) {
                if (err) {
                    plugin.bot.reply(from, to, err, 'notice');
                    return;
                }

                plugin.bot.reply(from, to, (numAffected ? 'Updated: ' : 'Added: ') + factoid.factoid + '.', 'notice');
            });
        }).bind(this));
	}

	/**
	 * Handle potential factoids by binding to the global message evnet.
	 *
	 * Currently handles piping, special factoids and regular factoids.
	 *
	 * @param from
	 * @param to
	 * @param message
	 */
	 onMessage(from:string, to:string, message:string) {
		if (this.isFactoid(message)) {
			var factoidName = message.split(' ')[0].replace(this.config.command, '').toLowerCase();

			var factoid = new Factoid(this.database);
			var plugin = this;

			factoid.active(factoidName, (function(err, factoid) {
				if(err){
					plugin.bot.reply(from, to, err, 'notice');
					return;
				}

				if(!factoid) {
					return;
				}

				// By default no prefix
				var prefix = '';
				var pipe = message.match(/\|[ ]?([\S]+)$/i);
				if (pipe) {
					prefix = pipe[1] + ': ';
				}

				// If the factoid has an special flags inside <>'s
				var special = factoid.content.match(/^<([a-z]+)>(.*)/i);
				if(special){
					var content = special[2];
					special = special[1];
					switch(special){
						case 'alias':
							plugin.onMessage(from, to, this.config.command + content + (prefix ? (' | ' + prefix.slice(0, -2)) : ''));
							break;
						case 'cmd':
							var args = content.split(' ');
							var command = args.shift();

							// TODO: Improve this
							plugin.client.emit('command.' + command, from, to, plugin.bot.config.command + command + ' ' + args.join(' ') + ' ' + message.replace(new RegExp('/^' + plugin.config.command.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + factoidName.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + '[ ]?/i'), ''));
							break;
					}
				} else {
					plugin.client.say(plugin.bot.getReplyTo(from, to), prefix + factoid.content);
				}
			}).bind(this));
		}
	}

	/**
	 * Is the string a factoid?
	 *
	 * @param command
	 * @returns {boolean}
	 */
		isFactoid(command:any) {
		return (command.charAt(0) == this.config.command);
	}

	/**
	 * Are we requesting factoid information?
	 *
	 * @param command
	 * @returns {boolean}
	 */
		isFactoidInfo(command:any) {
		return (command.charAt(1) == '+');
	}

	getAllFactoids(callback){
		var factoid = new Factoid(this.database);<commit_msg>Factoids %input% variable - closes #37<commit_after>import FactoidClass = require('./Factoid');
var Factoid = FactoidClass.Factoid;

export class Plugin {
	bot:any;
	config:any;
	database:any;
	client:any;
	commands:any;
	factoids:any;

	factoidSchema:any;
	Factoid:any;

	constructor(bot:any, config:any) {
		this.bot = bot;
		this.config = config;
		this.database = bot.database;
		this.client = bot.client;
		this.commands = {
			'remember': 'onCommandRemember',
			'r': 'onCommandRemember',
			'forget': 'onCommandForget',
			'f': 'onCommandForget'
		};

	}

	onCommandForget(from:string, to:string, message:string, args:any) {
        this.bot.hasAccess(from, to, this.config.acl, (function(hasAccess) {
            if(!hasAccess) return;

            if (args.length < 2) {
                this.bot.reply(from, to, 'Usage: ' + this.bot.config.bot.command + args[0] + ' <factoid>', 'notice');
                return;
            }
            var factoidName = args[1].toLowerCase();


            var factoid = new Factoid(this.database);
            factoid.forgetActive(factoidName, (function(err, numAffected) {
                if(err) {
                    this.bot.log.warn(err);
                    this.bot.reply(from, to, 'An error occurred', 'notice');
                    return;
                }

                this.bot.reply(from, to, 'Forgot: ' + factoidName, 'notice');
            }).bind(this));
        }).bind(this));
	}

	onCommandRemember(from:string, to:string, message:string, args:any) {
        this.bot.hasAccess(from, to, this.config.acl, (function(hasAccess) {
            if(!hasAccess) return;

            if (args.length < 3) {
                this.bot.reply(from, to, 'Usage: ' + this.bot.config.bot.command + args[0] + ' <factoid> <text>', 'notice');
                return;
            }

            var factoidName = args[1].toLowerCase();

            var contents = args.splice(2);
            contents = contents.join(' ').trim();

            var factoid = new Factoid(this.database);

            factoid.factoid = factoidName;
            factoid.content = contents;
            factoid.owner = from;
            factoid.channel = (to.charAt(0) == '#' ? to : '');

            var plugin = this;
            factoid.save(function saveFactoid(numAffected, err, factoid) {
                if (err) {
                    plugin.bot.reply(from, to, err, 'notice');
                    return;
                }

                plugin.bot.reply(from, to, (numAffected ? 'Updated: ' : 'Added: ') + factoid.factoid + '.', 'notice');
            });
        }).bind(this));
	}

	/**
	 * Handle potential factoids by binding to the global message event.
	 *
	 * Currently handles piping, special factoids and regular factoids.
	 *
	 * @param from
	 * @param to
	 * @param message
	 */
	 onMessage(from:string, to:string, message:string) {
		if (this.isFactoid(message)) {
			var factoidName = message.split(' ')[0].replace(this.config.command, '').toLowerCase();

			var factoid = new Factoid(this.database);
			var plugin = this;

			factoid.active(factoidName, (function(err, factoid) {
				if(err){
					plugin.bot.reply(from, to, err, 'notice');
					return;
				}

				if(!factoid) {
					return;
				}

				// By default no prefix
				var prefix = '';
				var pipe = message.match(/\|[ ]?([\S]+)$/i);
				if (pipe) {
					prefix = pipe[1] + ': ';
				}

				// If the factoid has an special flags inside <>'s
				var special = factoid.content.match(/^<([a-z]+)>(.*)/i);
				if(special){
					var content = special[2];
					special = special[1];
					switch(special){
						case 'alias':
							plugin.onMessage(from, to, this.config.command + content + (prefix ? (' | ' + prefix.slice(0, -2)) : ''));
							break;
						case 'cmd':
							var args = content.split(' ');
							var command = args.shift();

							// TODO: Improve this
							plugin.client.emit('command.' + command, from, to, plugin.bot.config.command + command + ' ' + args.join(' ') + ' ' + message.replace(new RegExp('/^' + plugin.config.command.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + factoidName.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + '[ ]?/i'), ''));
							break;
					}
				} else {
                    // Replace %input% with factoid's args.
                    var factoidContent = factoid.content.toString();
                    var args:any = message.split(' ');
                    args.shift();
                    args = args.join(' ');

                    if (prefix) {
                        args = args.replace(prefix.replace(/: $/, ''), '').replace(/[\s\|]+$/, '');
                    }

                    factoidContent = factoidContent.replace(/%input%/gi, args);
					plugin.client.say(plugin.bot.getReplyTo(from, to), prefix + factoidContent);
				}
			}).bind(this));
		}
	}

	/**
	 * Is the string a factoid?
	 *
	 * @param command
	 * @returns {boolean}
	 */
		isFactoid(command:any) {
		return (command.charAt(0) == this.config.command);
	}

	/**
	 * Are we requesting factoid information?
	 *
	 * @param command
	 * @returns {boolean}
	 */
		isFactoidInfo(command:any) {
		return (command.charAt(1) == '+');
	}

	getAllFactoids(callback){
		var factoid = new Factoid(this.database);
{'fraction_non_alphanumeric': 0.17550694236340025, 'fraction_numerical': 0.0033962641094795725, 'mean_word_length': 2.119975070115301, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4611614', 'n_tokens_mistral': 3644, 'n_tokens_neox': 3142, 'n_words': 633}