2021-12-05 20:22:28 -05:00
|
|
|
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>
|
2021-12-06 10:23:29 -05:00
|
|
|
public channels: Map<string, Channel>
|
2021-12-05 18:33:49 -05:00
|
|
|
public nick: string
|
|
|
|
private ident: string
|
|
|
|
private hostname: string
|
|
|
|
public accountName: string
|
2021-12-06 10:23:29 -05:00
|
|
|
public realname: string
|
2021-12-05 18:33:49 -05:00
|
|
|
private txnIdStore: Set<string>
|
2021-12-06 10:23:29 -05:00
|
|
|
public nextBatch: string
|
2021-12-06 12:07:40 -05:00
|
|
|
private initialSync: boolean
|
2021-12-06 10:23:29 -05:00
|
|
|
constructor(public mxid: string, private accessToken: string, private server: Server) {
|
|
|
|
this.clients = new Set();
|
2021-12-05 18:33:49 -05:00
|
|
|
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);
|
2021-12-06 10:23:29 -05:00
|
|
|
this.realname = this.accountName;
|
2021-12-05 18:33:49 -05:00
|
|
|
this.txnIdStore = new Set();
|
2021-12-05 20:22:28 -05:00
|
|
|
this.nextBatch = "";
|
2021-12-06 12:07:40 -05:00
|
|
|
this.initialSync = false;
|
2021-12-05 18:33:49 -05:00
|
|
|
}
|
|
|
|
|
2021-12-06 12:07:40 -05:00
|
|
|
isSynced() {
|
|
|
|
return this.initialSync;
|
2021-12-06 10:23:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getVerification() {
|
|
|
|
return axios.get(`https://matrix.org/_matrix/client/v3/account/whoami?access_token=${this.accessToken}`);
|
2021-12-05 18:33:49 -05:00
|
|
|
}
|
2021-12-05 18:44:21 -05:00
|
|
|
|
|
|
|
getMask(): string {
|
|
|
|
return `${this.nick}!${this.ident}@${this.hostname}`;
|
|
|
|
}
|
2021-12-05 20:22:28 -05:00
|
|
|
|
2021-12-06 10:23:29 -05:00
|
|
|
addClient(client: Client, passedTags: Map<string, string>) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 20:22:28 -05:00
|
|
|
doSync(): void {
|
2021-12-06 12:07:40 -05:00
|
|
|
const endpoint = `https://matrix.org/_matrix/client/v3/sync?access_token=${this.accessToken}&since=${this.nextBatch}`;
|
2021-12-05 20:22:28 -05:00
|
|
|
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)) {
|
2021-12-06 10:23:29 -05:00
|
|
|
const targetChannel = this.server.matrixRooms.get(roomId) || new Channel(roomId, this.server);
|
|
|
|
rooms.join[roomId].state.events.forEach((nextEvent: any) => targetChannel.routeMatrixEvent(nextEvent));
|
2021-12-05 20:22:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-12-06 12:07:40 -05:00
|
|
|
|
|
|
|
sendToAll(prefix: string, command: string, params: string[], tags: Map<string, string> = 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<string, string> = 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<string, string> = new Map(), skipClient: Client|null = null) {
|
|
|
|
this.clients.forEach(client => {
|
|
|
|
if (client !== skipClient && !client.enabledCaps.has(cap)) {
|
|
|
|
client.sendMessage(prefix, command, params, tags);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-12-05 18:33:49 -05:00
|
|
|
}
|