2015-12-11 14:48:16 +00:00
|
|
|
/* global APP */
|
|
|
|
import UIUtil from '../../util/UIUtil';
|
|
|
|
import UIEvents from '../../../../service/UI/UIEvents';
|
2015-01-23 12:01:44 +00:00
|
|
|
|
2015-01-07 14:54:03 +00:00
|
|
|
/**
|
|
|
|
* List with supported commands. The keys are the names of the commands and
|
|
|
|
* the value is the function that processes the message.
|
|
|
|
* @type {{String: function}}
|
|
|
|
*/
|
2015-12-11 14:48:16 +00:00
|
|
|
const commands = {
|
2015-01-07 14:54:03 +00:00
|
|
|
"topic" : processTopic
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts the command from the message.
|
|
|
|
* @param message the received message
|
|
|
|
* @returns {string} the command
|
|
|
|
*/
|
2015-07-28 21:52:32 +00:00
|
|
|
function getCommand(message) {
|
|
|
|
if(message) {
|
|
|
|
for(var command in commands) {
|
2015-09-11 03:05:53 +00:00
|
|
|
if(message.indexOf("/" + command) === 0)
|
2015-01-07 14:54:03 +00:00
|
|
|
return command;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
2015-07-28 21:52:32 +00:00
|
|
|
}
|
2015-01-07 14:54:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes the data for topic command.
|
|
|
|
* @param commandArguments the arguments of the topic command.
|
|
|
|
*/
|
2015-12-11 14:48:16 +00:00
|
|
|
function processTopic(commandArguments, emitter) {
|
2015-01-23 12:01:44 +00:00
|
|
|
var topic = UIUtil.escapeHtml(commandArguments);
|
2016-01-14 14:23:06 +00:00
|
|
|
emitter.emit(UIEvents.SUBJECT_CHANGED, topic);
|
2015-01-07 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-28 21:52:32 +00:00
|
|
|
* Constructs a new CommandProccessor instance from a message that
|
2015-01-07 14:54:03 +00:00
|
|
|
* handles commands received via chat messages.
|
|
|
|
* @param message the message
|
|
|
|
* @constructor
|
|
|
|
*/
|
2015-12-11 14:48:16 +00:00
|
|
|
function CommandsProcessor(message, emitter) {
|
2015-01-07 14:54:03 +00:00
|
|
|
var command = getCommand(message);
|
|
|
|
|
2015-12-11 14:48:16 +00:00
|
|
|
this.emitter = emitter;
|
|
|
|
|
2015-01-07 14:54:03 +00:00
|
|
|
/**
|
|
|
|
* Returns the name of the command.
|
|
|
|
* @returns {String} the command
|
|
|
|
*/
|
2015-07-28 21:52:32 +00:00
|
|
|
this.getCommand = function() {
|
2015-01-07 14:54:03 +00:00
|
|
|
return command;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var messageArgument = message.substr(command.length + 2);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the arguments of the command.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2015-07-28 21:52:32 +00:00
|
|
|
this.getArgument = function() {
|
2015-01-07 14:54:03 +00:00
|
|
|
return messageArgument;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether this instance is valid command or not.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2015-07-28 21:52:32 +00:00
|
|
|
CommandsProcessor.prototype.isCommand = function() {
|
|
|
|
if (this.getCommand())
|
2015-01-07 14:54:03 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes the command.
|
|
|
|
*/
|
2015-07-28 21:52:32 +00:00
|
|
|
CommandsProcessor.prototype.processCommand = function() {
|
2015-01-07 14:54:03 +00:00
|
|
|
if(!this.isCommand())
|
|
|
|
return;
|
|
|
|
|
2015-12-11 14:48:16 +00:00
|
|
|
commands[this.getCommand()](this.getArgument(), this.emitter);
|
2015-01-07 14:54:03 +00:00
|
|
|
};
|
|
|
|
|
2015-12-11 14:48:16 +00:00
|
|
|
export default CommandsProcessor;
|