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

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