Skip to content

Commit cbad30c

Browse files
author
3nprob
committed
Change realname format to templating
1 parent aee6b5f commit cbad30c

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

changelog.d/1374.feature

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Add ability to specify realname by template.
2+
3+
This deprecates the old mxid/reverse-mxid format (though it's still available).
4+
Behaves similar to nickTemplate. Available templating variables:
5+
6+
* `$DISPLAY`: Matrix user display name
7+
* `$USERID`:User mxid
8+
* `$LOCALPART`: Matrix user localpart
9+
* `$REVERSEID`: User mxid with host and localpart swapped
10+
* `$IRCUSER`: IRC username
11+

config.sample.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,11 @@ ircService:
404404
# through the bridge e.g. caller ID as there is no way to /ACCEPT.
405405
# Default: "" (no user modes)
406406
# userModes: "R"
407-
# The format of the realname defined for users, either mxid or reverse-mxid
408-
realnameFormat: "mxid"
407+
# The format of the realname defined for users. This MUST have either
408+
# $DISPLAY, $USERID, $LOCALPART, $REVERSEID or $IRCUSER somewhere in it.
409+
# The template to apply to every IRC client nick.
410+
# Note that realnames longer than 48 characters will be trucated.
411+
realnameFormat: "$USERID"
409412
# The minimum time to wait between connection attempts if we were disconnected
410413
# due to throttling.
411414
# pingTimeoutMs: 600000

config.schema.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ properties:
329329
type: "string"
330330
aliasTemplate:
331331
type: "string"
332-
pattern: "^(#.*\\$CHANNEL|\\$CHANNEL)$"
332+
pattern: "^(#.*\\$CHANNEL.*|\\$CHANNEL)$"
333333
whitelist:
334334
type: "array"
335335
items:
@@ -391,7 +391,7 @@ properties:
391391
type: "boolean"
392392
realnameFormat:
393393
type: "string"
394-
enum: ["mxid","reverse-mxid"]
394+
pattern: "\\$USERID|\\$LOCALPART|\\$DISPLAY|\\$REVERSEID|\\$IRCUSER"
395395
ipv6:
396396
type: "object"
397397
properties:

spec/unit/IdentGenerator.spec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe("Username generation", function() {
6565
expect(info.username).toEqual(uname);
6666
});
6767

68-
it("should reverse the userID", async function() {
68+
it("should reverse the userID according to legacy format", async function() {
6969
var userId = "@myreallylonguseridhere:localhost";
7070
const info = await identGenerator.getIrcNames(ircClientConfig, {
7171
getRealNameFormat: () => "reverse-mxid",
@@ -74,6 +74,21 @@ describe("Username generation", function() {
7474
expect(info.realname).toEqual("localhost:myreallylonguseridhere");
7575
});
7676

77+
it("should allow template userID", async function() {
78+
var userId = "@mxuser:localhost";
79+
const user = new MatrixUser(userId);
80+
user.setDisplayName('bar');
81+
ircClientConfig.getUsername = () => 'foo';
82+
let info = await identGenerator.getIrcNames(ircClientConfig, {
83+
getRealNameFormat: () => "$USERID_$REVERSEID_suffix",
84+
}, user);
85+
expect(info.realname).toEqual("@mxuser:localhost_localhost:mxuser_suffix");
86+
info = await identGenerator.getIrcNames(ircClientConfig, {
87+
getRealNameFormat: () => "$IRCUSER_$DISPLAY_$LOCALPART_suffix",
88+
}, user);
89+
expect(info.realname).toEqual("foo_bar_mxuser_suffix");
90+
});
91+
7792
it("should start with '_1' on an occupied user ID", async function() {
7893
const userId = "@myreallylonguseridhere:localhost";
7994
const uname = "myreal_1";

src/irc/IdentGenerator.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { DataStore } from "../datastore/DataStore";
2020
import { MatrixUser } from "matrix-appservice-bridge";
2121
import { IrcClientConfig } from "../models/IrcClientConfig";
2222
import { IrcServer } from "./IrcServer";
23+
import { renderTemplate } from "../util/Template";
2324

2425
const log = getLogger("IdentGenerator");
2526

@@ -66,18 +67,29 @@ export class IdentGenerator {
6667

6768
let realname: string;
6869
if (!matrixUser) {
69-
realname = IdentGenerator.sanitiseRealname(username || "");
70+
realname = username || "";
7071
}
7172
else if (server.getRealNameFormat() === "mxid") {
72-
realname = IdentGenerator.sanitiseRealname(matrixUser.getId());
73+
realname = matrixUser.getId();
74+
log.warn("** The IrcClient.realnameFormat config schema has changed, allowing legacy format for now. **");
75+
log.warn("See https://github.com/matrix-org/matrix-appservice-irc/blob/master/CHANGELOG.md for details");
7376
}
7477
else if (server.getRealNameFormat() === "reverse-mxid") {
75-
realname = IdentGenerator.sanitiseRealname(IdentGenerator.switchAroundMxid(matrixUser));
78+
realname = IdentGenerator.switchAroundMxid(matrixUser);
79+
log.warn("** The IrcClient.realnameFormat config schema has changed, allowing legacy format for now. **");
80+
log.warn("See https://github.com/matrix-org/matrix-appservice-irc/blob/master/CHANGELOG.md for details");
7681
}
7782
else {
78-
throw Error('Invalid value for realNameFormat');
83+
realname = renderTemplate(server.getRealNameFormat(), {
84+
userId: matrixUser.userId,
85+
localpart: matrixUser.localpart,
86+
display: matrixUser.getDisplayName() || "",
87+
reverseId: IdentGenerator.switchAroundMxid(matrixUser),
88+
ircUser: username || ""
89+
});
7990
}
8091

92+
realname = IdentGenerator.sanitiseRealname(realname);
8193
realname = realname.substring(0, IdentGenerator.MAX_REAL_NAME_LENGTH);
8294

8395
if (matrixUser) {

0 commit comments

Comments
 (0)