Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slash command handler #333

Merged
merged 1 commit into from
Jul 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -18,6 +18,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