Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow templating realname #1374

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions changelog.d/1374.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Add ability to specify realname by template.

This deprecates the old mxid/reverse-mxid format (though it's still available).
Behaves similar to nickTemplate. Available templating variables:

* `$DISPLAY`: Matrix user display name
* `$USERID`:User mxid
* `$LOCALPART`: Matrix user localpart
* `$REVERSEID`: User mxid with host and localpart swapped
* `$IRCUSER`: IRC username

7 changes: 5 additions & 2 deletions config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,11 @@ 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
realnameFormat: "mxid"
# The format of the realname defined for users. This MUST have either
# $DISPLAY, $USERID, $LOCALPART, $REVERSEID or $IRCUSER somewhere in it.
# The template to apply to every IRC client nick.
# Note that realnames longer than 48 characters will be trucated.
realnameFormat: "$USERID"
# The minimum time to wait between connection attempts if we were disconnected
# due to throttling.
# pingTimeoutMs: 600000
Expand Down
4 changes: 2 additions & 2 deletions config.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ properties:
type: "string"
aliasTemplate:
type: "string"
pattern: "^(#.*\\$CHANNEL|\\$CHANNEL)$"
pattern: "^(#.*\\$CHANNEL.*|\\$CHANNEL)$"
whitelist:
type: "array"
items:
Expand Down Expand Up @@ -391,7 +391,7 @@ properties:
type: "boolean"
realnameFormat:
type: "string"
enum: ["mxid","reverse-mxid"]
pattern: "\\$USERID|\\$LOCALPART|\\$DISPLAY|\\$REVERSEID|\\$IRCUSER"
ipv6:
type: "object"
properties:
Expand Down
17 changes: 16 additions & 1 deletion spec/unit/IdentGenerator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe("Username generation", function() {
expect(info.username).toEqual(uname);
});

it("should reverse the userID", async function() {
it("should reverse the userID according to legacy format", async function() {
var userId = "@myreallylonguseridhere:localhost";
const info = await identGenerator.getIrcNames(ircClientConfig, {
getRealNameFormat: () => "reverse-mxid",
Expand All @@ -74,6 +74,21 @@ 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';
let info = await identGenerator.getIrcNames(ircClientConfig, {
getRealNameFormat: () => "$USERID_$REVERSEID_suffix",
}, user);
expect(info.realname).toEqual("@mxuser:localhost_localhost:mxuser_suffix");
info = await identGenerator.getIrcNames(ircClientConfig, {
getRealNameFormat: () => "$IRCUSER_$DISPLAY_$LOCALPART_suffix",
}, user);
expect(info.realname).toEqual("foo_bar_mxuser_suffix");
});

it("should start with '_1' on an occupied user ID", async function() {
const userId = "@myreallylonguseridhere:localhost";
const uname = "myreal_1";
Expand Down
20 changes: 16 additions & 4 deletions src/irc/IdentGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { DataStore } from "../datastore/DataStore";
import { MatrixUser } from "matrix-appservice-bridge";
import { IrcClientConfig } from "../models/IrcClientConfig";
import { IrcServer } from "./IrcServer";
import { renderTemplate } from "../util/Template";

const log = getLogger("IdentGenerator");

Expand Down Expand Up @@ -66,18 +67,29 @@ export class IdentGenerator {

let realname: string;
if (!matrixUser) {
realname = IdentGenerator.sanitiseRealname(username || "");
realname = username || "";
}
else if (server.getRealNameFormat() === "mxid") {
realname = IdentGenerator.sanitiseRealname(matrixUser.getId());
realname = matrixUser.getId();
log.warn("** The IrcClient.realnameFormat config schema has changed, allowing legacy format for now. **");
log.warn("See https://github.com/matrix-org/matrix-appservice-irc/blob/master/CHANGELOG.md for details");
}
else if (server.getRealNameFormat() === "reverse-mxid") {
realname = IdentGenerator.sanitiseRealname(IdentGenerator.switchAroundMxid(matrixUser));
realname = IdentGenerator.switchAroundMxid(matrixUser);
log.warn("** The IrcClient.realnameFormat config schema has changed, allowing legacy format for now. **");
log.warn("See https://github.com/matrix-org/matrix-appservice-irc/blob/master/CHANGELOG.md for details");
}
else {
throw Error('Invalid value for realNameFormat');
realname = renderTemplate(server.getRealNameFormat(), {
userId: matrixUser.userId,
localpart: matrixUser.localpart,
display: matrixUser.getDisplayName() || "",
reverseId: IdentGenerator.switchAroundMxid(matrixUser),
ircUser: username || ""
});
}

realname = IdentGenerator.sanitiseRealname(realname);
realname = realname.substring(0, IdentGenerator.MAX_REAL_NAME_LENGTH);

if (matrixUser) {
Expand Down