diff --git a/README.md b/README.md index fdd7285..c66fa9c 100644 --- a/README.md +++ b/README.md @@ -44,4 +44,4 @@ That said, it is usable for basic chatting. Copy `config.example.json` to `config.json`, edit the values if needed, then `npm run build` and then `node reflection.js` to start it ## Authentication -Authentication is done via `SASL PLAIN`, the username is your mxid and the password is an access token from another session. \ No newline at end of file +Authentication is done via `SASL PLAIN`, the username is your mxid and the password is an access token from another session plus your server domain, separated by a `:` (so `access_token:matrix.org` if your server is matrix.org). Note this is the domain for `m.server`, not necessarily the homeserver domain. \ No newline at end of file diff --git a/src/Client.ts b/src/Client.ts index 6d03c8c..862f744 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -156,8 +156,12 @@ export class Client { this.closeConnectionWithError('Invalid authentication') } const mxid = authArray[1]; - const accessToken = authArray[2]; - const thisIRCUser = this.server.getOrCreateIRCUser(mxid, accessToken); + const accessTokenAndServer = authArray[2]; + const sepIndex = accessTokenAndServer.indexOf(":"); + const accessToken = accessTokenAndServer.slice(0, sepIndex); + const homeserver = accessTokenAndServer.slice(sepIndex+1); + console.log(accessToken, homeserver); + const thisIRCUser = this.server.getOrCreateIRCUser(mxid, accessToken, homeserver); thisIRCUser.getVerification().then((response) => { if (response.status === 401 || response.status === 403) { this.sendMessage(this.server.name, '904', numerics['904']('*'), message.tags) @@ -180,7 +184,7 @@ export class Client { if (this.user.isSynced()) { this.user.addClient(this, message.tags); } else { - axios.get(`https://matrix.org/_matrix/client/v3/sync?access_token=${accessToken}`).then(response => { + axios.get(`https://${this.user.homeserver}/_matrix/client/v3/sync?access_token=${accessToken}`).then(response => { const data = response.data; const rooms = data.rooms; if (this.user === null) diff --git a/src/IRCUser.ts b/src/IRCUser.ts index 03f5ef3..f48a80a 100644 --- a/src/IRCUser.ts +++ b/src/IRCUser.ts @@ -17,7 +17,7 @@ export class IRCUser { public nextBatch: string private initialSync: boolean private syncIntervalID: NodeJS.Timeout; - constructor(public mxid: string, private accessToken: string, private server: Server) { + constructor(public mxid: string, private accessToken: string, public homeserver: string, private server: Server) { this.clients = new Set(); this.channels = new Set(); const mxidSplit = mxid.split(':') @@ -37,7 +37,7 @@ export class IRCUser { } getVerification() { - return axios.get(`https://matrix.org/_matrix/client/v3/account/whoami?access_token=${this.accessToken}`); + return axios.get(`https://${this.homeserver}/_matrix/client/v3/account/whoami?access_token=${this.accessToken}`); } getMask(): string { @@ -58,7 +58,7 @@ export class IRCUser { console.log("not syncing, initial sync not completed"); return; } - const endpoint = `https://matrix.org/_matrix/client/v3/sync?access_token=${this.accessToken}&since=${this.nextBatch}`; + const endpoint = `https://${this.homeserver}/_matrix/client/v3/sync?access_token=${this.accessToken}&since=${this.nextBatch}`; axios.get(endpoint).then(response => { const data = response.data; this.nextBatch = data.next_batch; @@ -94,7 +94,7 @@ export class IRCUser { } const newTxnid = randomUUID(); this.txnIdStore.set(newTxnid, client); - axios.put(`https://matrix.org/_matrix/client/v3/rooms/${channel.roomId}/send/m.room.message/${newTxnid}?access_token=${this.accessToken}`, content); + axios.put(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/send/m.room.message/${newTxnid}?access_token=${this.accessToken}`, content); } sendToAll(prefix: string, command: string, params: string[], tags: Map = new Map(), skipClient: Client|null = null) { diff --git a/src/Server.ts b/src/Server.ts index f56cba3..3f4e455 100644 --- a/src/Server.ts +++ b/src/Server.ts @@ -22,12 +22,12 @@ export class Server { this.nickToMxid = new Map(); } - getOrCreateIRCUser(mxid: string, accessToken: string): IRCUser { + getOrCreateIRCUser(mxid: string, accessToken: string, homeserver: string): IRCUser { const maybeUser = this.ircUsers.get(mxid); if (maybeUser) { return maybeUser; } - return new IRCUser(mxid, accessToken, this); + return new IRCUser(mxid, accessToken, homeserver, this); } getOrCreateIRCChannel(roomId: string): Channel {