Skip to content

Commit 70fce09

Browse files
committed
Merge pull request element-hq#11 from matrix-org/irc-style-commands
IRC-style commands
2 parents 37108ac + 00c45e4 commit 70fce09

3 files changed

Lines changed: 265 additions & 1 deletion

File tree

‎src/SlashCommands.js‎

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var MatrixClientPeg = require("./MatrixClientPeg");
20+
var dis = require("./dispatcher");
21+
22+
var reject = function(msg) {
23+
return {
24+
error: msg
25+
};
26+
};
27+
28+
var success = function(promise) {
29+
return {
30+
promise: promise
31+
};
32+
};
33+
34+
var commands = {
35+
// Change your nickname
36+
nick: function(room_id, args) {
37+
if (args) {
38+
return success(
39+
MatrixClientPeg.get().setDisplayName(args)
40+
);
41+
}
42+
return reject("Usage: /nick <display_name>");
43+
},
44+
45+
// Invite a user
46+
invite: function(room_id, args) {
47+
if (args) {
48+
var matches = args.match(/^(\S+)$/);
49+
if (matches) {
50+
return success(
51+
MatrixClientPeg.get().invite(room_id, matches[1])
52+
);
53+
}
54+
}
55+
return reject("Usage: /invite <userId>");
56+
},
57+
58+
// Join a room
59+
join: function(room_id, args) {
60+
if (args) {
61+
var matches = args.match(/^(\S+)$/);
62+
if (matches) {
63+
var room_alias = matches[1];
64+
// Try to find a room with this alias
65+
var rooms = MatrixClientPeg.get().getRooms();
66+
var roomId;
67+
for (var i = 0; i < rooms.length; i++) {
68+
var aliasEvents = rooms[i].currentState.getStateEvents(
69+
"m.room.aliases"
70+
);
71+
for (var j = 0; j < aliasEvents.length; j++) {
72+
var aliases = aliasEvents[j].getContent().aliases || [];
73+
for (var k = 0; k < aliases.length; k++) {
74+
if (aliases[k] === room_alias) {
75+
roomId = rooms[i].roomId;
76+
break;
77+
}
78+
}
79+
if (roomId) { break; }
80+
}
81+
if (roomId) { break; }
82+
}
83+
if (roomId) { // we've already joined this room, view it.
84+
dis.dispatch({
85+
action: 'view_room',
86+
room_id: roomId
87+
});
88+
return success();
89+
}
90+
else {
91+
// attempt to join this alias.
92+
return success(
93+
MatrixClientPeg.get().joinRoom(room_alias).done(
94+
function(room) {
95+
dis.dispatch({
96+
action: 'view_room',
97+
room_id: room.roomId
98+
});
99+
}, function(err) {
100+
console.error(
101+
"Failed to join room: %s", JSON.stringify(err)
102+
);
103+
})
104+
);
105+
}
106+
}
107+
}
108+
return reject("Usage: /join <room_alias> [NOT IMPLEMENTED]");
109+
},
110+
111+
// Kick a user from the room with an optional reason
112+
kick: function(room_id, args) {
113+
if (args) {
114+
var matches = args.match(/^(\S+?)( +(.*))?$/);
115+
if (matches) {
116+
return success(
117+
MatrixClientPeg.get().kick(room_id, matches[1], matches[3])
118+
);
119+
}
120+
}
121+
return reject("Usage: /kick <userId> [<reason>]");
122+
},
123+
124+
// Ban a user from the room with an optional reason
125+
ban: function(room_id, args) {
126+
if (args) {
127+
var matches = args.match(/^(\S+?)( +(.*))?$/);
128+
if (matches) {
129+
return success(
130+
MatrixClientPeg.get().ban(room_id, matches[1], matches[3])
131+
);
132+
}
133+
}
134+
return reject("Usage: /ban <userId> [<reason>]");
135+
},
136+
137+
// Unban a user from the room
138+
unban: function(room_id, args) {
139+
if (args) {
140+
var matches = args.match(/^(\S+)$/);
141+
if (matches) {
142+
// Reset the user membership to "leave" to unban him
143+
return success(
144+
MatrixClientPeg.get().unban(room_id, matches[1])
145+
);
146+
}
147+
}
148+
return reject("Usage: /unban <userId>");
149+
},
150+
151+
// Define the power level of a user
152+
op: function(room_id, args) {
153+
if (args) {
154+
var matches = args.match(/^(\S+?)( +(\d+))?$/);
155+
var powerLevel = 50; // default power level for op
156+
if (matches) {
157+
var user_id = matches[1];
158+
if (matches.length === 4 && undefined !== matches[3]) {
159+
powerLevel = parseInt(matches[3]);
160+
}
161+
if (powerLevel !== NaN) {
162+
var room = MatrixClientPeg.get().getRoom(room_id);
163+
if (!room) {
164+
return reject("Bad room ID: " + room_id);
165+
}
166+
var powerLevelEvent = room.currentState.getStateEvents(
167+
"m.room.power_levels", ""
168+
);
169+
return success(
170+
MatrixClientPeg.get().setPowerLevel(
171+
room_id, user_id, powerLevel, powerLevelEvent
172+
)
173+
);
174+
}
175+
}
176+
}
177+
return reject("Usage: /op <userId> [<power level>]");
178+
},
179+
180+
// Reset the power level of a user
181+
deop: function(room_id, args) {
182+
if (args) {
183+
var matches = args.match(/^(\S+)$/);
184+
if (matches) {
185+
var room = MatrixClientPeg.get().getRoom(room_id);
186+
if (!room) {
187+
return reject("Bad room ID: " + room_id);
188+
}
189+
190+
var powerLevelEvent = room.currentState.getStateEvents(
191+
"m.room.power_levels", ""
192+
);
193+
return success(
194+
MatrixClientPeg.get().setPowerLevel(
195+
room_id, args, undefined, powerLevelEvent
196+
)
197+
);
198+
}
199+
}
200+
return reject("Usage: /deop <userId>");
201+
}
202+
};
203+
204+
module.exports = {
205+
/**
206+
* Process the given text for /commands and perform them.
207+
* @param {string} roomId The room in which the command was performed.
208+
* @param {string} input The raw text input by the user.
209+
* @return {Object|null} An object with the property 'error' if there was an error
210+
* processing the command, or 'promise' if a request was sent out.
211+
* Returns null if the input didn't match a command.
212+
*/
213+
processInput: function(roomId, input) {
214+
// trim any trailing whitespace, as it can confuse the parser for
215+
// IRC-style commands
216+
input = input.replace(/\s+$/, "");
217+
if (input[0] === "/" && input[1] !== "/") {
218+
var bits = input.match(/^(\S+?)( +(.*))?$/);
219+
var cmd = bits[1].substring(1).toLowerCase();
220+
var args = bits[3];
221+
if (commands[cmd]) {
222+
return commands[cmd](roomId, args);
223+
}
224+
}
225+
return null; // not a command
226+
}
227+
};

‎src/TextForEvent.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ function textForMemberEvent(ev) {
33
// XXX: SYJS-16
44
var senderName = ev.sender ? ev.sender.name : ev.getSender();
55
var targetName = ev.target ? ev.target.name : ev.getContent().target;
6+
var reason = ev.getContent().reason ? (
7+
" Reason: " + ev.getContent().reason
8+
) : "";
69
switch (ev.getContent().membership) {
710
case 'invite':
811
return senderName + " invited " + targetName + ".";
12+
case 'ban':
13+
return senderName + " banned " + targetName + "." + reason;
914
case 'join':
1015
if (ev.getPrevContent() && ev.getPrevContent().membership == 'join') {
1116
if (ev.getPrevContent().displayname && ev.getContent().displayname) {
@@ -21,7 +26,18 @@ function textForMemberEvent(ev) {
2126
return targetName + " joined the room.";
2227
}
2328
case 'leave':
24-
return targetName + " left the room.";
29+
if (ev.getSender() === ev.getStateKey()) {
30+
return targetName + " left the room.";
31+
}
32+
else if (ev.getPrevContent().membership === "ban") {
33+
return senderName + " unbanned " + targetName + ".";
34+
}
35+
else if (ev.getPrevContent().membership === "join") {
36+
return senderName + " kicked " + targetName + "." + reason;
37+
}
38+
else {
39+
return targetName + " left the room.";
40+
}
2541
}
2642
};
2743

‎src/controllers/molecules/MessageComposer.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
'use strict';
1818

1919
var MatrixClientPeg = require("../../MatrixClientPeg");
20+
var SlashCommands = require("../../SlashCommands");
2021

2122
var dis = require("../../dispatcher");
2223
var KeyCode = {
@@ -171,6 +172,26 @@ module.exports = {
171172

172173
onEnter: function(ev) {
173174
var contentText = this.refs.textarea.getDOMNode().value;
175+
176+
var cmd = SlashCommands.processInput(this.props.room.roomId, contentText);
177+
if (cmd) {
178+
ev.preventDefault();
179+
if (!cmd.error) {
180+
this.refs.textarea.getDOMNode().value = '';
181+
}
182+
if (cmd.promise) {
183+
cmd.promise.done(function() {
184+
console.log("Command success.");
185+
}, function(err) {
186+
console.error("Command failure: %s", err);
187+
});
188+
}
189+
else if (cmd.error) {
190+
console.error(cmd.error);
191+
}
192+
return;
193+
}
194+
174195
var content = null;
175196
if (/^\/me /i.test(contentText)) {
176197
content = {

0 commit comments

Comments
 (0)