From fe708437d9334dad4f4f5ddb5cbf879406bc8da8 Mon Sep 17 00:00:00 2001 From: 3nprob <3nprob@example.com> Date: Tue, 8 Jun 2021 14:38:45 +0900 Subject: [PATCH] Allow templating realname --- changelog.d/1374.feature | 1 + config.sample.yaml | 4 +++- config.schema.yml | 2 +- spec/unit/IdentGenerator.spec.js | 11 +++++++++++ src/irc/IdentGenerator.ts | 6 ++++++ 5 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 changelog.d/1374.feature diff --git a/changelog.d/1374.feature b/changelog.d/1374.feature new file mode 100644 index 000000000..100bd2743 --- /dev/null +++ b/changelog.d/1374.feature @@ -0,0 +1 @@ +Add ability to specify realname by template. diff --git a/config.sample.yaml b/config.sample.yaml index 9e24135a7..e5739e0c0 100644 --- a/config.sample.yaml +++ b/config.sample.yaml @@ -387,7 +387,9 @@ ircService: # through the bridge e.g. caller ID as there is no way to /ACCEPT. # Default: "" (no user modes) # userModes: "R" - # The format of the realname defined for users, either mxid or reverse-mxid + # The format of the realname defined for users. + # either "mxid", "reverse-mxid", or "template:TEMPLATE", where TEMPLATE must + # contain either of $USERID, $LOCALPART, $DISPLAY, $IRCUSER realnameFormat: "mxid" # The minimum time to wait between connection attempts if we were disconnected # due to throttling. diff --git a/config.schema.yml b/config.schema.yml index c4f372ce8..e3a62c123 100644 --- a/config.schema.yml +++ b/config.schema.yml @@ -379,7 +379,7 @@ properties: type: "boolean" realnameFormat: type: "string" - enum: ["mxid","reverse-mxid"] + pattern: "^template:(\\$USERID|\\$LOCALPART|\\$DISPLAY|\\$IRCUSER)|^mxid$|reverse-mxid$" ipv6: type: "object" properties: diff --git a/spec/unit/IdentGenerator.spec.js b/spec/unit/IdentGenerator.spec.js index 5df134175..e506d0802 100644 --- a/spec/unit/IdentGenerator.spec.js +++ b/spec/unit/IdentGenerator.spec.js @@ -72,6 +72,17 @@ describe("Username generation", function() { expect(info.realname).toEqual("localhost:myreallylonguseridhere"); }); + it("should allow template userID", async function() { + var userId = "@mxuser:localhost"; + const user = new MatrixUser(userId); + user.setDisplayName('bar'); + ircClientConfig.getUsername = () => 'foo'; + const info = await identGenerator.getIrcNames(ircClientConfig, { + getRealNameFormat: () => "template:$IRCUSER_$DISPLAY_$LOCALPART_$USERID_suffix", + }, user); + expect(info.realname).toEqual("foo_bar_mxuser_@mxuser:localhost_suffix"); + }); + it("should start with '_1' on an occupied user ID", async function() { const userId = "@myreallylonguseridhere:localhost"; const uname = "myreal_1"; diff --git a/src/irc/IdentGenerator.ts b/src/irc/IdentGenerator.ts index 5572c35f8..46bb8e5e9 100644 --- a/src/irc/IdentGenerator.ts +++ b/src/irc/IdentGenerator.ts @@ -74,6 +74,12 @@ export class IdentGenerator { else if (server.getRealNameFormat() === "reverse-mxid") { realname = IdentGenerator.sanitiseRealname(IdentGenerator.switchAroundMxid(matrixUser)); } + else if (server.getRealNameFormat().startsWith('template:')) { + realname = server.getRealNameFormat().replace(/^template:/, '').replace(/\$USERID/g, matrixUser.userId); + realname = realname.replace(/\$LOCALPART/g, matrixUser.localpart); + realname = realname.replace(/\$DISPLAY/g, matrixUser.getDisplayName() || ''); + realname = realname.replace(/\$IRCUSER/g, username || ''); + } else { throw Error('Invalid value for realNameFormat'); }