TeaWeb/shared/js/profiles/Identity.ts

122 lines
3.8 KiB
TypeScript
Raw Normal View History

import {AbstractServerConnection, ServerCommand} from "../connection/ConnectionBase";
import {HandshakeIdentityHandler} from "../connection/HandshakeHandler";
import {AbstractCommandHandler} from "../connection/AbstractCommandHandler";
import { tr } from "tc-shared/i18n/localize";
2020-03-30 13:44:18 +02:00
export enum IdentitifyType {
TEAFORO,
TEAMSPEAK,
NICKNAME
}
export interface Identity {
fallback_name(): string | undefined ;
uid() : string;
type() : IdentitifyType;
valid() : boolean;
encode?() : string;
decode(data: string) : Promise<void>;
spawn_identity_handshake_handler(connection: AbstractServerConnection) : HandshakeIdentityHandler;
}
/* avoid circular dependencies here */
export async function decode_identity(type: IdentitifyType, data: string) : Promise<Identity> {
let identity: Identity;
switch (type) {
case IdentitifyType.NICKNAME:
const nidentity = require("tc-shared/profiles/identities/NameIdentity");
identity = new nidentity.NameIdentity();
break;
case IdentitifyType.TEAFORO:
const fidentity = require("tc-shared/profiles/identities/TeaForumIdentity");
identity = new fidentity.TeaForumIdentity(undefined);
break;
case IdentitifyType.TEAMSPEAK:
const tidentity = require("tc-shared/profiles/identities/TeamSpeakIdentity");
identity = new tidentity.TeaSpeakIdentity(undefined, undefined);
break;
}
2020-03-30 13:44:18 +02:00
if(!identity)
return undefined;
try {
await identity.decode(data)
} catch(error) {
/* todo better error handling! */
console.error(error);
return undefined;
}
2019-02-23 14:15:22 +01:00
2020-03-30 13:44:18 +02:00
return identity;
}
export function create_identity(type: IdentitifyType) {
let identity: Identity;
switch (type) {
case IdentitifyType.NICKNAME:
const nidentity = require("tc-shared/profiles/identities/NameIdentity");
identity = new nidentity.NameIdentity();
break;
case IdentitifyType.TEAFORO:
const fidentity = require("tc-shared/profiles/identities/TeaForumIdentity");
identity = new fidentity.TeaForumIdentity(undefined);
break;
case IdentitifyType.TEAMSPEAK:
const tidentity = require("tc-shared/profiles/identities/TeamSpeakIdentity");
identity = new tidentity.TeaSpeakIdentity(undefined, undefined);
break;
2019-02-23 14:15:22 +01:00
}
2020-03-30 13:44:18 +02:00
return identity;
}
2019-02-23 14:15:22 +01:00
2020-03-30 13:44:18 +02:00
export class HandshakeCommandHandler<T extends AbstractHandshakeIdentityHandler> extends AbstractCommandHandler {
readonly handle: T;
2020-03-30 13:44:18 +02:00
constructor(connection: AbstractServerConnection, handle: T) {
super(connection);
this.handle = handle;
}
2020-03-30 13:44:18 +02:00
handle_command(command: ServerCommand): boolean {
if(typeof this[command.command] === "function") {
2020-03-30 13:44:18 +02:00
this[command.command](command.arguments);
} else if(command.command == "error") {
2020-03-30 13:44:18 +02:00
return false;
} else {
console.warn(tr("Received unknown command while handshaking (%o)"), command);
}
2020-03-30 13:44:18 +02:00
return true;
2020-03-27 23:36:57 +01:00
}
2020-03-30 13:44:18 +02:00
}
2020-03-30 13:44:18 +02:00
export abstract class AbstractHandshakeIdentityHandler implements HandshakeIdentityHandler {
connection: AbstractServerConnection;
2020-03-30 13:44:18 +02:00
protected callbacks: ((success: boolean, message?: string) => any)[] = [];
2020-03-30 13:44:18 +02:00
protected constructor(connection: AbstractServerConnection) {
this.connection = connection;
}
registerCallback(callback: (success: boolean, message?: string) => any) {
2020-03-30 13:44:18 +02:00
this.callbacks.push(callback);
}
fillClientInitData(data: any) { }
abstract executeHandshake();
2020-03-30 13:44:18 +02:00
protected trigger_success() {
for(const callback of this.callbacks)
callback(true);
}
2020-03-30 13:44:18 +02:00
protected trigger_fail(message: string) {
for(const callback of this.callbacks)
callback(false, message);
}
}