Skip to content

Commit

Permalink
Allow templating realname
Browse files Browse the repository at this point in the history
  • Loading branch information
3nprob committed Jun 8, 2021
1 parent 98cc5bc commit fe70843
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/1374.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ability to specify realname by template.
4 changes: 3 additions & 1 deletion config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion config.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions spec/unit/IdentGenerator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
6 changes: 6 additions & 0 deletions src/irc/IdentGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down

0 comments on commit fe70843

Please sign in to comment.