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 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* global APP, require */
  2. var UIUtil = require("../../util/UIUtil");
  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. var 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(var command in commands) {
  19. if(message.indexOf("/" + command) === 0)
  20. return command;
  21. }
  22. }
  23. return "";
  24. }
  25. /**
  26. * Processes the data for topic command.
  27. * @param commandArguments the arguments of the topic command.
  28. */
  29. function processTopic(commandArguments) {
  30. var topic = UIUtil.escapeHtml(commandArguments);
  31. APP.xmpp.setSubject(topic);
  32. }
  33. /**
  34. * Constructs a new CommandProccessor instance from a message that
  35. * handles commands received via chat messages.
  36. * @param message the message
  37. * @constructor
  38. */
  39. function CommandsProcessor(message) {
  40. var command = getCommand(message);
  41. /**
  42. * Returns the name of the command.
  43. * @returns {String} the command
  44. */
  45. this.getCommand = function() {
  46. return command;
  47. };
  48. var messageArgument = message.substr(command.length + 2);
  49. /**
  50. * Returns the arguments of the command.
  51. * @returns {string}
  52. */
  53. this.getArgument = function() {
  54. return messageArgument;
  55. };
  56. }
  57. /**
  58. * Checks whether this instance is valid command or not.
  59. * @returns {boolean}
  60. */
  61. CommandsProcessor.prototype.isCommand = function() {
  62. if (this.getCommand())
  63. return true;
  64. return false;
  65. };
  66. /**
  67. * Processes the command.
  68. */
  69. CommandsProcessor.prototype.processCommand = function() {
  70. if(!this.isCommand())
  71. return;
  72. commands[this.getCommand()](this.getArgument());
  73. };
  74. module.exports = CommandsProcessor;