Skip to content

Commit b01024b

Browse files
committed
Implemented XEP-0106 JID Escaping Transformations for Matrix room IDs and added unit tests for the same
1 parent 278e537 commit b01024b

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/MatrixRoomHandler.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,24 @@ const log = Logging.get("MatrixRoomHandler");
2525

2626
const ACCOUNT_LOCK_MS = 1000;
2727
const EVENT_MAPPING_SIZE = 16384;
28+
const XEP0106_ESCAPING_TRANSFORMATIONS = {
29+
'"': "\\22",
30+
"&": "\\26",
31+
"'": "\\27",
32+
"/": "\\2f",
33+
":": "\\3a",
34+
"<": "\\3c",
35+
">": "\\3e",
36+
"@": "\\40",
37+
};
38+
39+
export function XEP0106ApplyEscapingTransformations(roomId: string): string {
40+
return roomId
41+
.split("")
42+
.map(char => XEP0106_ESCAPING_TRANSFORMATIONS[char] || char)
43+
.join("");
44+
}
2845

29-
/**
30-
* Handles creation and handling of rooms.
31-
*/
3246
export class MatrixRoomHandler {
3347
private bridge: Bridge;
3448
private accountRoomLock: Set<string>;
@@ -161,6 +175,8 @@ export class MatrixRoomHandler {
161175
log.info("User joined, can now send messages");
162176
}
163177
this.roomCreationLock.delete(remoteId);
178+
roomId = XEP0106ApplyEscapingTransformations(roomId!);
179+
164180
return roomId!;
165181
}
166182

@@ -221,6 +237,8 @@ export class MatrixRoomHandler {
221237
throw Error("Room doesn't exist, refusing to make room");
222238
}
223239
log.info(`Found ${roomId} for ${alias}`);
240+
roomId = XEP0106ApplyEscapingTransformations(roomId);
241+
224242
return roomId;
225243
}
226244

@@ -251,6 +269,8 @@ export class MatrixRoomHandler {
251269
this.roomCreationLock.set(remoteId, createPromise as Promise<any>);
252270
await createPromise;
253271
this.roomCreationLock.delete(remoteId);
272+
roomId = XEP0106ApplyEscapingTransformations(roomId);
273+
254274
return roomId;
255275
}
256276

test/test_matrixroomhandler.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// tslint:disable: no-any
2+
import * as Chai from "chai";
3+
import { XEP0106ApplyEscapingTransformations } from "../src/MatrixRoomHandler";
4+
const expect = Chai.expect;
5+
6+
describe("XEP0106JIDEscaping", () => {
7+
it("implement XEP-0106 JID escaping for matrix room ID", () => {
8+
expect(XEP0106ApplyEscapingTransformations('callme"ishmael"')).to.equal(
9+
"callme\\22ishmael\\22"
10+
);
11+
expect(XEP0106ApplyEscapingTransformations("at&tguy")).to.equal(
12+
"at\\26tguy"
13+
);
14+
expect(XEP0106ApplyEscapingTransformations("d'artagnan")).to.equal(
15+
"d\\27artagnan"
16+
);
17+
expect(XEP0106ApplyEscapingTransformations("/.fanboy")).to.equal(
18+
"\\2f.fanboy"
19+
);
20+
expect(XEP0106ApplyEscapingTransformations("::foo::")).to.equal(
21+
"\\3a\\3afoo\\3a\\3a"
22+
);
23+
expect(XEP0106ApplyEscapingTransformations("<foo>")).to.equal(
24+
"\\3cfoo\\3e"
25+
);
26+
expect(XEP0106ApplyEscapingTransformations("user@host")).to.equal(
27+
"user\\40host"
28+
);
29+
});
30+
});

0 commit comments

Comments
 (0)