Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

commands.js 2.3KB

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