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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright @ 2015 Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. var UIUtil = require("../../util/UIUtil");
  17. /**
  18. * List with supported commands. The keys are the names of the commands and
  19. * the value is the function that processes the message.
  20. * @type {{String: function}}
  21. */
  22. var commands = {
  23. "topic" : processTopic
  24. };
  25. /**
  26. * Extracts the command from the message.
  27. * @param message the received message
  28. * @returns {string} the command
  29. */
  30. function getCommand(message)
  31. {
  32. if(message)
  33. {
  34. for(var command in commands)
  35. {
  36. if(message.indexOf("/" + command) == 0)
  37. return command;
  38. }
  39. }
  40. return "";
  41. };
  42. /**
  43. * Processes the data for topic command.
  44. * @param commandArguments the arguments of the topic command.
  45. */
  46. function processTopic(commandArguments)
  47. {
  48. var topic = UIUtil.escapeHtml(commandArguments);
  49. APP.xmpp.setSubject(topic);
  50. }
  51. /**
  52. * Constructs new CommandProccessor instance from a message that
  53. * handles commands received via chat messages.
  54. * @param message the message
  55. * @constructor
  56. */
  57. function CommandsProcessor(message)
  58. {
  59. var command = getCommand(message);
  60. /**
  61. * Returns the name of the command.
  62. * @returns {String} the command
  63. */
  64. this.getCommand = function()
  65. {
  66. return command;
  67. };
  68. var messageArgument = message.substr(command.length + 2);
  69. /**
  70. * Returns the arguments of the command.
  71. * @returns {string}
  72. */
  73. this.getArgument = function()
  74. {
  75. return messageArgument;
  76. };
  77. }
  78. /**
  79. * Checks whether this instance is valid command or not.
  80. * @returns {boolean}
  81. */
  82. CommandsProcessor.prototype.isCommand = function()
  83. {
  84. if(this.getCommand())
  85. return true;
  86. return false;
  87. };
  88. /**
  89. * Processes the command.
  90. */
  91. CommandsProcessor.prototype.processCommand = function()
  92. {
  93. if(!this.isCommand())
  94. return;
  95. commands[this.getCommand()](this.getArgument());
  96. };
  97. module.exports = CommandsProcessor;