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 public channels: Map public nick: string private ident: string private hostname: string public accountName: string public realname: string private txnIdStore: Set public nextBatch: string private initialSync: boolean private syncIntervalID: NodeJS.Timeout; constructor(public mxid: string, private accessToken: string, private server: Server) { this.clients = new Set(); this.channels = new Map(); const mxidSplit = mxid.split(':') this.nick = mxidSplit[0].substr(1); this.ident = this.nick; this.hostname = mxidSplit[1]; this.accountName = mxid.slice(1); this.realname = this.accountName; this.txnIdStore = new Set(); this.nextBatch = ""; this.initialSync = false; this.syncIntervalID = setInterval(this.doSync.bind(this), 15000); } isSynced() { return this.nextBatch !== ""; } getVerification() { return axios.get(`https://matrix.org/_matrix/client/v3/account/whoami?access_token=${this.accessToken}`); } getMask(): string { return `${this.nick}!${this.ident}@${this.hostname}`; } addClient(client: Client, passedTags: Map) { console.log('adding client'); this.clients.add(client); if (this.nextBatch !== "") { console.log('nextBatch is good'); for (const channel of this.channels.values()) { console.log('joining channel'); channel.joinNewIRCClient(client, passedTags); } } } doSync(): void { if (!this.isSynced()) { 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}`; axios.get(endpoint).then(response => { const data = response.data; this.nextBatch = data.next_batch; const rooms = data.rooms; if (rooms && rooms['join']) { for (const roomId of Object.keys(rooms.join)) { const targetChannel = this.server.matrixRooms.get(roomId) || new Channel(roomId, this.server); rooms.join[roomId].timeline.events.forEach((nextEvent: any) => { targetChannel.routeMatrixEvent(nextEvent) }); } } }) } sendToAll(prefix: string, command: string, params: string[], tags: Map = new Map(), skipClient: Client|null = null) { this.clients.forEach(client => { if (client !== skipClient) { client.sendMessage(prefix, command, params, tags); } }) } sendToAllWithCap(cap: string, prefix: string, command: string, params: string[], tags: Map = new Map(), skipClient: Client|null = null) { this.clients.forEach(client => { if (client !== skipClient && client.enabledCaps.has(cap)) { client.sendMessage(prefix, command, params, tags); } }) } sendToAllWithoutCap(cap: string, prefix: string, command: string, params: string[], tags: Map = new Map(), skipClient: Client|null = null) { this.clients.forEach(client => { if (client !== skipClient && !client.enabledCaps.has(cap)) { client.sendMessage(prefix, command, params, tags); } }) } }