diff --git a/client/lib/chatMessages.coffee b/client/lib/chatMessages.coffee index 6a5294ea35124..43e260a0bf682 100644 --- a/client/lib/chatMessages.coffee +++ b/client/lib/chatMessages.coffee @@ -73,7 +73,18 @@ msg = input.value input.value = '' stopTyping(rid) - Meteor.call 'sendMessage', { _id: Random.id(), rid: rid, msg: msg, day: window.day } + #Check if message starts with /command + if msg[0] is '/' + match = msg.match(/^\/([^\s]+)(?:\s+(.*))?$/m) + if(match?) + command = match[1] + param = match[2] + internalMsg = { _id: Random.id(), rid: rid, msg: msg, day: window.day } + Meteor.call 'slashCommand', {cmd: command, params: param, msg: internalMsg } + else + #Run to allow local encryption + #Meteor.call 'onClientBeforeSendMessage', {} + Meteor.call 'sendMessage', { _id: Random.id(), rid: rid, msg: msg, day: window.day } deleteMsg = (element) -> id = element.getAttribute("id") diff --git a/packages/rocketchat-lib/client/slashCommand.coffee b/packages/rocketchat-lib/client/slashCommand.coffee new file mode 100644 index 0000000000000..f1be087062c97 --- /dev/null +++ b/packages/rocketchat-lib/client/slashCommand.coffee @@ -0,0 +1,20 @@ +RocketChat.slashCommands = {} + +RocketChat.slashCommands.add = (command, callback) -> + if !RocketChat.slashCommands[command]? + RocketChat.slashCommands[command] = callback + return + +RocketChat.slashCommands.run = (command, params, item) -> + if RocketChat.slashCommands[command]? + callback = RocketChat.slashCommands[command] + callback command, params, item + + +Meteor.methods + slashCommand: (command) -> + if not Meteor.userId() + throw new Meteor.Error 203, t('User_logged_out') + + RocketChat.slashCommands.run command.cmd, command.params, command.msg + diff --git a/packages/rocketchat-lib/package.js b/packages/rocketchat-lib/package.js index 5344ecdb892ba..350ef2aa9e748 100644 --- a/packages/rocketchat-lib/package.js +++ b/packages/rocketchat-lib/package.js @@ -19,6 +19,8 @@ Package.onUse(function(api) { api.addFiles('lib/core.coffee', ['server', 'client']); api.addFiles('lib/callbacks.coffee', ['server', 'client']); api.addFiles('server/sendMessage.coffee', ['server']); + api.addFiles('server/slashCommand.coffee', ['server']); + api.addFiles('client/slashCommand.coffee', ['client']); api.addFiles([ 'settings/lib/settings.coffee', diff --git a/packages/rocketchat-lib/server/slashCommand.coffee b/packages/rocketchat-lib/server/slashCommand.coffee new file mode 100644 index 0000000000000..f1be087062c97 --- /dev/null +++ b/packages/rocketchat-lib/server/slashCommand.coffee @@ -0,0 +1,20 @@ +RocketChat.slashCommands = {} + +RocketChat.slashCommands.add = (command, callback) -> + if !RocketChat.slashCommands[command]? + RocketChat.slashCommands[command] = callback + return + +RocketChat.slashCommands.run = (command, params, item) -> + if RocketChat.slashCommands[command]? + callback = RocketChat.slashCommands[command] + callback command, params, item + + +Meteor.methods + slashCommand: (command) -> + if not Meteor.userId() + throw new Meteor.Error 203, t('User_logged_out') + + RocketChat.slashCommands.run command.cmd, command.params, command.msg + diff --git a/packages/rocketchat-me/me.coffee b/packages/rocketchat-me/me.coffee index 965b2b6d296cd..881f70170d975 100644 --- a/packages/rocketchat-me/me.coffee +++ b/packages/rocketchat-me/me.coffee @@ -4,11 +4,12 @@ ### class Me - constructor: (message) -> - if _.trim message.html - # If message starts with /me, replace it for text formatting - if message.html.indexOf('/me ') is 0 - message.html = '_' + message.html.replace('/me ','') + '_' - return message + constructor: (command, params, item) -> + if(command == "me") + if _.trim params + currentUser = Meteor.user() + msg = item + msg.msg = '_' + params + '_' + Meteor.call 'sendMessage', msg -RocketChat.callbacks.add 'renderMessage', Me +RocketChat.slashCommands.add 'me', Me \ No newline at end of file