Skip to content

Commit

Permalink
Merge pull request #333 from warcode/feature/slashcommands
Browse files Browse the repository at this point in the history
Slash command handler
  • Loading branch information
engelgabriel committed Jul 23, 2015
2 parents cc93d95 + 1fccfb8 commit 3f34c20
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
13 changes: 12 additions & 1 deletion client/lib/chatMessages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
20 changes: 20 additions & 0 deletions packages/rocketchat-lib/client/slashCommand.coffee
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions packages/rocketchat-lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
20 changes: 20 additions & 0 deletions packages/rocketchat-lib/server/slashCommand.coffee
Original file line number Diff line number Diff line change
@@ -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

15 changes: 8 additions & 7 deletions packages/rocketchat-me/me.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3f34c20

Please sign in to comment.