Skip to content

Commit 56523b5

Browse files
author
3nprob
committed
Allow templating realname
1 parent 98cc5bc commit 56523b5

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

config.sample.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,9 @@ ircService:
387387
# through the bridge e.g. caller ID as there is no way to /ACCEPT.
388388
# Default: "" (no user modes)
389389
# userModes: "R"
390-
# The format of the realname defined for users, either mxid or reverse-mxid
390+
# The format of the realname defined for users.
391+
# either "mxid", "reverse-mxid", or "template:TEMPLATE", where TEMPLATE must
392+
# contain either of $USERID, $LOCALPART, $DISPLAY, $IRCUSER
391393
realnameFormat: "mxid"
392394
# The minimum time to wait between connection attempts if we were disconnected
393395
# due to throttling.

config.schema.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ properties:
379379
type: "boolean"
380380
realnameFormat:
381381
type: "string"
382-
enum: ["mxid","reverse-mxid"]
382+
pattern: "^template:(\\$USERID|\\$LOCALPART|\\$DISPLAY|\\$IRCUSER)|^mxid$|reverse-mxid$"
383383
ipv6:
384384
type: "object"
385385
properties:

spec/unit/IdentGenerator.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ describe("Username generation", function() {
7272
expect(info.realname).toEqual("localhost:myreallylonguseridhere");
7373
});
7474

75+
it("should allow template userID", async function() {
76+
var userId = "@mxuser:localhost";
77+
const user = new MatrixUser(userId);
78+
user.setDisplayName('bar');
79+
ircClientConfig.getUsername = () => 'foo';
80+
const info = await identGenerator.getIrcNames(ircClientConfig, {
81+
getRealNameFormat: () => "template:$IRCUSER_$DISPLAY_$LOCALPART_$USERID_suffix",
82+
}, user);
83+
expect(info.realname).toEqual("foo_bar_mxuser_@mxuser:localhost_suffix");
84+
});
85+
7586
it("should start with '_1' on an occupied user ID", async function() {
7687
const userId = "@myreallylonguseridhere:localhost";
7788
const uname = "myreal_1";

src/irc/IdentGenerator.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ export class IdentGenerator {
7474
else if (server.getRealNameFormat() === "reverse-mxid") {
7575
realname = IdentGenerator.sanitiseRealname(IdentGenerator.switchAroundMxid(matrixUser));
7676
}
77+
else if (server.getRealNameFormat().startsWith('template:')) {
78+
realname = server.getRealNameFormat().replace(/^template:/, '').replace(/\$USERID/g, matrixUser.userId);
79+
realname = realname.replace(/\$LOCALPART/g, matrixUser.localpart);
80+
realname = realname.replace(/\$DISPLAY/g, matrixUser.getDisplayName() || '');
81+
realname = realname.replace(/\$IRCUSER/g, username || '');
82+
}
7783
else {
7884
throw Error('Invalid value for realNameFormat');
7985
}

0 commit comments

Comments
 (0)