<commit_before>
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

var debug = require("debug")("sparkbot");
var fine = require("debug")("sparkbot:fine");

var Utils = require("./utils");
var CommandInterpreter = require("./interpreter");
var CommandRouter = require("./router");

var webhookResources = [ "memberships", "messages", "rooms"];
var webhookEvents = [ "created", "deleted", "updated"];

/* Creates a Cisco Spark webhook with specified configuration structure: 
 *  
 *  { 
 * 		port, 		// int: local port on which the webhook is accessible
 * 		path,  		// string: path to which new webhook POST events are expected
 * 		token,		// string: spark API access token  
 *      trimMention // boolean: filters out mentions of token owner        
 * 		ignoreSelf  // boolean: ignores message created by token owner       	
 *  }
 *  
 * If no configuration is specified, the defaults below apply: 
 *  { 
 * 		port 			: process.env.PORT || 8080,
 * 		path			: process.env.WEBHOOK_URL || "/" ,  
 * 		token			: process.env.SPARK_TOKEN,
 * 		trimMention	 	: true,
 * 		commandPrefix	: process.env.COMMAND_PREFIX || "/",
 * 		ignoreSelf		: true                   	
 *  }
 * 
 */
function Webhook(config) {
	// Inject defaults if no configuration specified
	if (!config) {
		debug("webhook instantiated with default configuration");
		config = {
			// [WORKAROUND] in some container situation (ie, Cisco Shipped), we need to use an OVERRIDE_PORT to force our bot to start and listen to the port defined in the Dockerfile (ie, EXPOSE), 
			// and not the PORT dynamically assigned by the host or scheduler.
			port 			: process.env.OVERRIDE_PORT || process.env.PORT || 8080,
			path			: process.env.WEBHOOK_URL || "/",
			token			: process.env.SPARK_TOKEN,
			trimMention	 	: true,
			commandPrefix 	: process.env.COMMAND_PREFIX || "/" 
			// The ignoreSelf variable is not initialized, will default to true if a bot is used, and false otherwise 
			//ignoreSelf		: false
		};
	} 

	// Robustify: it is usually safer not to read ourselves, especially if no commandPrefix is specified
	if (!config.ignoreSelf && !config.commandPrefix) {
		debug("WARNING: configuration does not prevent for reading from yourself => possible infinite loop, continuing...");
	}
	
	// Abort if mandatory copnfig parameters are not present
	if (!config.port || !config.path) {
		debug("bad configuration for webhook, aborting Webhook constructor");
		return null;
	}

	// If a Spark token is specified, create a command interpreter
	if (!config.token) {
		debug("no Spark access token specified, will not fetch message contents and room titles, nor interpret commands");
	}
	this.token = config.token;

	// Webhook listeners
	this.listeners = {};
	var self = this;
	function fire(trigger) {
		// Retreive listener for incoming event
		var entry = trigger.resource + "/" + trigger.event;
		var listener = self.listeners[entry];
		if (!listener) {
			debug("no listener found for resource/event: " + entry);
			return;
		}

		// Invoke listener
		debug("calling listener for resource/event: " + entry + ", with data context: " + trigger.data.id);
		listener(trigger);		
	}

	// Initialize command processors
	this.interpreter = new CommandInterpreter(config);
	this.router = new CommandRouter(this);

	// Webhook API routes
	started = Date.now();
	app.route(config.path)
		.get(function (req, res) {
			debug("healtch check hitted");
			res.json({
				message		: "Congrats, your Cisco Spark webhook is up and running",
				since			: new Date(started).toISOString(),
				listeners		: Object.keys(self.listeners),
				token			: (self.token != null),
				account			: {
					type		: self.interpreter.accountType,
					person		: self.interpreter.person,
				},
				interpreter		: {
					prefix		: self.interpreter.prefix,
					trimMention	: self.interpreter.trimMention,
					nickName	: self.nickName,
					ignoreSelf  : self.interpreter.ignoreSelf
				},
				commands        : Object.keys(self.router.commands),
				tip			: "Register your bot as a WebHook to start receiving events: https://developer.ciscospark.com/endpoint-webhooks-post.html"
			});
		})
		.post(function (req, res) {
			debug("webhook invoked");
			
			// analyse incoming payload, should conform to Spark webhook trigger specifications
        	if (!req.body || !Utils.checkWebhookEvent(req.body)) {
				debug("unexpected payload POSTed, aborting...");
				res.status(400).json({message: "Bad payload for Webhook",
										details: "either the bot is misconfigured or Cisco Spark is running a new API version"});
				return;
			}

			// event is ready to be processed, let's send a response to Spark without waiting any longer
			res.status(200).json({message: "message received and being processed by webhook"});

			// process incoming resource/event, see https://developer.ciscospark.com/webhooks-explained.html<commit_msg>moved nickname from interpreter to account<commit_after>
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

var debug = require("debug")("sparkbot");
var fine = require("debug")("sparkbot:fine");

var Utils = require("./utils");
var CommandInterpreter = require("./interpreter");
var CommandRouter = require("./router");

var webhookResources = [ "memberships", "messages", "rooms"];
var webhookEvents = [ "created", "deleted", "updated"];

/* Creates a Cisco Spark webhook with specified configuration structure: 
 *  
 *  { 
 * 		port, 		// int: local port on which the webhook is accessible
 * 		path,  		// string: path to which new webhook POST events are expected
 * 		token,		// string: spark API access token  
 *      trimMention // boolean: filters out mentions of token owner        
 * 		ignoreSelf  // boolean: ignores message created by token owner       	
 *  }
 *  
 * If no configuration is specified, the defaults below apply: 
 *  { 
 * 		port 			: process.env.PORT || 8080,
 * 		path			: process.env.WEBHOOK_URL || "/" ,  
 * 		token			: process.env.SPARK_TOKEN,
 * 		trimMention	 	: true,
 * 		commandPrefix	: process.env.COMMAND_PREFIX || "/",
 * 		ignoreSelf		: true                   	
 *  }
 * 
 */
function Webhook(config) {
	// Inject defaults if no configuration specified
	if (!config) {
		debug("webhook instantiated with default configuration");
		config = {
			// [WORKAROUND] in some container situation (ie, Cisco Shipped), we need to use an OVERRIDE_PORT to force our bot to start and listen to the port defined in the Dockerfile (ie, EXPOSE), 
			// and not the PORT dynamically assigned by the host or scheduler.
			port 			: process.env.OVERRIDE_PORT || process.env.PORT || 8080,
			path			: process.env.WEBHOOK_URL || "/",
			token			: process.env.SPARK_TOKEN,
			trimMention	 	: true,
			commandPrefix 	: process.env.COMMAND_PREFIX || "/" 
			// The ignoreSelf variable is not initialized, will default to true if a bot is used, and false otherwise 
			//ignoreSelf		: false
		};
	} 

	// Robustify: it is usually safer not to read ourselves, especially if no commandPrefix is specified
	if (!config.ignoreSelf && !config.commandPrefix) {
		debug("WARNING: configuration does not prevent for reading from yourself => possible infinite loop, continuing...");
	}
	
	// Abort if mandatory copnfig parameters are not present
	if (!config.port || !config.path) {
		debug("bad configuration for webhook, aborting Webhook constructor");
		return null;
	}

	// If a Spark token is specified, create a command interpreter
	if (!config.token) {
		debug("no Spark access token specified, will not fetch message contents and room titles, nor interpret commands");
	}
	this.token = config.token;

	// Webhook listeners
	this.listeners = {};
	var self = this;
	function fire(trigger) {
		// Retreive listener for incoming event
		var entry = trigger.resource + "/" + trigger.event;
		var listener = self.listeners[entry];
		if (!listener) {
			debug("no listener found for resource/event: " + entry);
			return;
		}

		// Invoke listener
		debug("calling listener for resource/event: " + entry + ", with data context: " + trigger.data.id);
		listener(trigger);		
	}

	// Initialize command processors
	this.interpreter = new CommandInterpreter(config);
	this.router = new CommandRouter(this);

	// Webhook API routes
	started = Date.now();
	app.route(config.path)
		.get(function (req, res) {
			debug("healtch check hitted");
			res.json({
				message		: "Congrats, your Cisco Spark webhook is up and running",
				since			: new Date(started).toISOString(),
				listeners		: Object.keys(self.listeners),
				token			: (self.token != null),
				account			: {
					type		: self.interpreter.accountType,
					nickName	: self.interpreter.nickName,
					person		: self.interpreter.person
				},
				interpreter		: {
					prefix		: self.interpreter.prefix,
					trimMention	: self.interpreter.trimMention,
					ignoreSelf  : self.interpreter.ignoreSelf
				},
				commands        : Object.keys(self.router.commands),
				tip			: "Register your bot as a WebHook to start receiving events: https://developer.ciscospark.com/endpoint-webhooks-post.html"
			});
		})
		.post(function (req, res) {
			debug("webhook invoked");
			
			// analyse incoming payload, should conform to Spark webhook trigger specifications
        	if (!req.body || !Utils.checkWebhookEvent(req.body)) {
				debug("unexpected payload POSTed, aborting...");
				res.status(400).json({message: "Bad payload for Webhook",
										details: "either the bot is misconfigured or Cisco Spark is running a new API version"});
				return;
			}

			// event is ready to be processed, let's send a response to Spark without waiting any longer
			res.status(200).json({message: "message received and being processed by webhook"});

			// process incoming resource/event, see https://developer.ciscospark.com/webhooks-explained.html
{'fraction_non_alphanumeric': 0.11680287024117998, 'fraction_numerical': 0.0027905122583217062, 'mean_word_length': 3.346037245560849, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, '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': '4359558', 'n_tokens_mistral': 3369, 'n_tokens_neox': 2955, 'n_words': 932}