<commit_before>var Utils = require("../utils");
var async = require("async");
var debug = require("debug")("Reply:customFunction");

module.exports = function (reply, match, options, callback) {
  
  var plugins = options.system.plugins;
  var scope = options.system.scope;

  // We use async to capture multiple matches in the same reply
  return async.whilst(
    function () {
      return match;
    },
    function (cb) {
      // Call Function here

      var main = match[0];
      var pluginName = Utils.trim(match[1]);
      var partsStr = Utils.trim(match[2]);
      var parts = partsStr.split(",");

      var args = [];
      for (var i = 0; i < parts.length; i++) {
        if (parts[i] !== "") {
          args.push(parts[i].trim());
        }
      }

      if (plugins[pluginName]) {

        // SubReply is the results of the object coming back
        // TODO. Subreply should be optional and could be undefined, or null
        args.push(function customFunctionHandle(err, subreply) {

          match = false;
          reply = reply.replace(main, subreply);
          match = reply.match(/\^(\w+)\(([~\w<>,\s]*)\)/);
          if (err) {
            cb(err);
          } else {
            cb();
          }
        });

        debug("Calling Plugin Function", pluginName);
        plugins[pluginName].apply(scope, args);

      } else {
        // If a function is missing, we kill the line and return empty handed
        debug("Custom Function not-found", pluginName);
        match = false;
        cb(null, "");
      }
    },
    function (err) {
      debug("Callback from custom function");
      return callback(err, Utils.trim(reply), scope.message.props, {});
    }
  );
};<commit_msg>paying down tech dept<commit_after>var Utils = require("../utils");
var async = require("async");
var debug = require("debug")("Reply:customFunction");

module.exports = function (reply, match, options, callback) {
  
  var plugins = options.system.plugins;
  var scope = options.system.scope;

  // We use async to capture multiple matches in the same reply
  return async.whilst(
    function () {
      return match;
    },
    function (cb) {
      // Call Function here

      var main = match[0];
      var pluginName = Utils.trim(match[1]);
      var partsStr = Utils.trim(match[2]);
      var parts = partsStr.split(",");

      var args = [];
      for (var i = 0; i < parts.length; i++) {
        if (parts[i] !== "") {
          args.push(parts[i].trim());
        }
      }

      if (plugins[pluginName]) {

        // SubReply is the results of the object coming back
        // TODO. Subreply should be optional and could be undefined, or null
        args.push(function customFunctionHandle(err, subreply) {

          match = false;
          reply = reply.replace(main, subreply);
          match = reply.match(/\^(\w+)\(([~\w<>,\s]*)\)/);
          if (err) {
            cb(err);
          } else {
            cb();
          }
        });

        debug("Calling Plugin Function", pluginName);
        plugins[pluginName].apply(scope, args);

      } else {
        // If a function is missing, we kill the line and return empty handed
        debug("Custom Function not-found", pluginName);
        match = false;
        cb(null, "");
      }
    },
    function (err) {
      debug("Callback from custom function", err);
      return callback(err, Utils.trim(reply), scope.message.props, {});
    }
  );
};
{'fraction_non_alphanumeric': 0.14583937373151637, 'fraction_numerical': 0.002319512902290519, 'mean_word_length': 2.1109107303877366, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '2822588', 'n_tokens_mistral': 1000, 'n_tokens_neox': 981, 'n_words': 284}