jiti-meet/modules/UI/side_pannels/chat/Commands.js

86 lines
1.9 KiB
JavaScript
Raw Normal View History

/* global APP, require */
2015-01-23 12:01:44 +00:00
var UIUtil = require("../../util/UIUtil");
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}}
*/
var commands = {
"topic" : processTopic
};
/**
* Extracts the command from the message.
* @param message the received message
* @returns {string} the command
*/
function getCommand(message) {
if(message) {
for(var command in commands) {
if(message.indexOf("/" + command) === 0)
2015-01-07 14:54:03 +00:00
return command;
}
}
return "";
}
2015-01-07 14:54:03 +00:00
/**
* Processes the data for topic command.
* @param commandArguments the arguments of the topic command.
*/
function processTopic(commandArguments) {
2015-01-23 12:01:44 +00:00
var topic = UIUtil.escapeHtml(commandArguments);
APP.xmpp.setSubject(topic);
2015-01-07 14:54:03 +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
*/
function CommandsProcessor(message) {
2015-01-07 14:54:03 +00:00
var command = getCommand(message);
/**
* Returns the name of the command.
* @returns {String} the command
*/
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}
*/
this.getArgument = function() {
2015-01-07 14:54:03 +00:00
return messageArgument;
};
}
/**
* Checks whether this instance is valid command or not.
* @returns {boolean}
*/
CommandsProcessor.prototype.isCommand = function() {
if (this.getCommand())
2015-01-07 14:54:03 +00:00
return true;
return false;
};
/**
* Processes the command.
*/
CommandsProcessor.prototype.processCommand = function() {
2015-01-07 14:54:03 +00:00
if(!this.isCommand())
return;
commands[this.getCommand()](this.getArgument());
};
module.exports = CommandsProcessor;