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

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