reflectionircd/src/IRCUser.ts

57 lines
2 KiB
TypeScript
Raw Normal View History

import axios from "axios";
2021-12-05 18:33:49 -05:00
import { Channel } from "./Channel.js";
import { Client } from "./Client.js";
import { Server } from "./Server.js";
export class IRCUser {
private clients: Set<Client>
private channels: Map<string, Channel>
public server: Server
2021-12-05 18:33:49 -05:00
public nick: string
private ident: string
private hostname: string
public accountName: string
public isAuthed: boolean
private txnIdStore: Set<string>
private nextBatch: string
2021-12-05 18:33:49 -05:00
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();
2021-12-05 18:33:49 -05:00
}
verifyCredentials(accessToken: string): boolean {
return accessToken === this.accessToken;
}
2021-12-05 18:44:21 -05:00
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));
}
}
})
}
2021-12-05 18:33:49 -05:00
}