You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Commands.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import UIUtil from '../../util/UIUtil';
  2. import UIEvents from '../../../../service/UI/UIEvents';
  3. /**
  4. * List with supported commands. The keys are the names of the commands and
  5. * the value is the function that processes the message.
  6. * @type {{String: function}}
  7. */
  8. const commands = {
  9. 'topic': processTopic
  10. };
  11. /**
  12. * Extracts the command from the message.
  13. * @param message the received message
  14. * @returns {string} the command
  15. */
  16. function getCommand(message) {
  17. if (message) {
  18. for (const command in commands) {
  19. if (message.indexOf(`/${command}`) === 0) {
  20. return command;
  21. }
  22. }
  23. }
  24. return '';
  25. }
  26. /**
  27. * Processes the data for topic command.
  28. * @param commandArguments the arguments of the topic command.
  29. */
  30. function processTopic(commandArguments, emitter) {
  31. const topic = UIUtil.escapeHtml(commandArguments);
  32. emitter.emit(UIEvents.SUBJECT_CHANGED, topic);
  33. }
  34. /**
  35. * Constructs a new CommandProccessor instance from a message that
  36. * handles commands received via chat messages.
  37. * @param message the message
  38. * @constructor
  39. */
  40. function CommandsProcessor(message, emitter) {
  41. const command = getCommand(message);
  42. this.emitter = emitter;
  43. /**
  44. * Returns the name of the command.
  45. * @returns {String} the command
  46. */
  47. this.getCommand = function() {
  48. return command;
  49. };
  50. const messageArgument = message.substr(command.length + 2);
  51. /**
  52. * Returns the arguments of the command.
  53. * @returns {string}
  54. */
  55. this.getArgument = function() {
  56. return messageArgument;
  57. };
  58. }
  59. /**
  60. * Checks whether this instance is valid command or not.
  61. * @returns {boolean}
  62. */
  63. CommandsProcessor.prototype.isCommand = function() {
  64. if (this.getCommand()) {
  65. return true;
  66. }
  67. return false;
  68. };
  69. /**
  70. * Processes the command.
  71. */
  72. CommandsProcessor.prototype.processCommand = function() {
  73. if (!this.isCommand()) {
  74. return;
  75. }
  76. commands[this.getCommand()](this.getArgument(), this.emitter);
  77. };
  78. export default CommandsProcessor;