2018-12-28 15:39:23 +01:00
|
|
|
namespace profiles.identities {
|
|
|
|
export enum IdentitifyType {
|
|
|
|
TEAFORO,
|
|
|
|
TEAMSPEAK,
|
|
|
|
NICKNAME
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Identity {
|
|
|
|
name() : string;
|
|
|
|
uid() : string;
|
|
|
|
type() : IdentitifyType;
|
|
|
|
|
|
|
|
valid() : boolean;
|
|
|
|
|
|
|
|
encode?() : string;
|
2019-01-26 17:10:15 +01:00
|
|
|
decode(data: string) : Promise<void>;
|
2018-12-28 15:39:23 +01:00
|
|
|
|
|
|
|
spawn_identity_handshake_handler(connection: ServerConnection) : HandshakeIdentityHandler;
|
|
|
|
}
|
|
|
|
|
2019-01-26 17:10:15 +01:00
|
|
|
export async function decode_identity(type: IdentitifyType, data: string) : Promise<Identity> {
|
2018-12-28 15:39:23 +01:00
|
|
|
let identity: Identity;
|
|
|
|
switch (type) {
|
|
|
|
case IdentitifyType.NICKNAME:
|
|
|
|
identity = new NameIdentity();
|
|
|
|
break;
|
|
|
|
case IdentitifyType.TEAFORO:
|
|
|
|
identity = new TeaForumIdentity(undefined, undefined);
|
|
|
|
break;
|
|
|
|
case IdentitifyType.TEAMSPEAK:
|
2019-01-26 17:10:15 +01:00
|
|
|
identity = new TeaSpeakIdentity(undefined, undefined);
|
2018-12-28 15:39:23 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!identity)
|
|
|
|
return undefined;
|
|
|
|
|
2019-01-26 17:10:15 +01:00
|
|
|
try {
|
|
|
|
await identity.decode(data)
|
|
|
|
} catch(error) {
|
|
|
|
/* todo better error handling! */
|
|
|
|
console.error(error);
|
2018-12-28 15:39:23 +01:00
|
|
|
return undefined;
|
2019-01-26 17:10:15 +01:00
|
|
|
}
|
2018-12-28 15:39:23 +01:00
|
|
|
|
|
|
|
return identity;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function create_identity(type: IdentitifyType) {
|
|
|
|
let identity: Identity;
|
|
|
|
switch (type) {
|
|
|
|
case IdentitifyType.NICKNAME:
|
|
|
|
identity = new NameIdentity();
|
|
|
|
break;
|
|
|
|
case IdentitifyType.TEAFORO:
|
|
|
|
identity = new TeaForumIdentity(undefined, undefined);
|
|
|
|
break;
|
|
|
|
case IdentitifyType.TEAMSPEAK:
|
2019-01-26 17:10:15 +01:00
|
|
|
identity = new TeaSpeakIdentity(undefined, undefined);
|
2018-12-28 15:39:23 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return identity;
|
|
|
|
}
|
|
|
|
|
|
|
|
export abstract class AbstractHandshakeIdentityHandler implements HandshakeIdentityHandler {
|
|
|
|
connection: ServerConnection;
|
|
|
|
|
|
|
|
protected callbacks: ((success: boolean, message?: string) => any)[] = [];
|
|
|
|
|
|
|
|
protected constructor(connection: ServerConnection) {
|
|
|
|
this.connection = connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
register_callback(callback: (success: boolean, message?: string) => any) {
|
|
|
|
this.callbacks.push(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract start_handshake();
|
|
|
|
|
|
|
|
protected trigger_success() {
|
|
|
|
for(const callback of this.callbacks)
|
|
|
|
callback(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected trigger_fail(message: string) {
|
|
|
|
for(const callback of this.callbacks)
|
|
|
|
callback(false, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|