import axios from "axios"; import { Channel } from "./Channel.js"; import { Client } from "./Client.js"; import { Server } from "./Server.js"; export class IRCUser { private clients: Set private channels: Map public server: Server public nick: string private ident: string private hostname: string public accountName: string public isAuthed: boolean private txnIdStore: Set private nextBatch: string constructor(private initialClient: Client, public mxid: string, private accessToken: string) { this.clients = new Set([initialClient]); this.channels = new Map(); this.server = initialClient.server; const mxidSplit = mxid.split(':') this.nick = mxidSplit[0].substr(1); this.ident = this.nick; this.hostname = mxidSplit[1]; this.accountName = mxid.slice(1); this.isAuthed = false; this.txnIdStore = new Set(); this.nextBatch = ""; this.doSync(); } verifyCredentials(accessToken: string): boolean { return accessToken === this.accessToken; } getMask(): string { return `${this.nick}!${this.ident}@${this.hostname}`; } doSync(): void { let endpoint = `https://matrix.org/_matrix/client/v3/sync?access_token=${this.accessToken}`; if (this.nextBatch !== "") endpoint = `${endpoint}&since=${this.nextBatch}`; axios.get(endpoint).then(response => { const data = response.data; this.nextBatch = data.next_batch; const rooms = data.rooms; if (rooms['join']) { for (const roomId of Object.keys(rooms.join)) { const targetChannel = this.server.matrixRooms.get(roomId) || new Channel(roomId, this); rooms.join[roomId].state.events.forEach((nextEvent: any) => targetChannel.handleMatrixEvent(nextEvent)); } } }) } }