73 lines
1.9 KiB
TypeScript
73 lines
1.9 KiB
TypeScript
import { IRCConnectionWrapper } from '../irc';
|
|
import { INickServOptions, INickStore } from '../types/irc.interfaces';
|
|
import { Collector } from './collector';
|
|
|
|
export class NickServCollector extends Collector {
|
|
constructor(
|
|
nickservOptions: INickServOptions,
|
|
resolve: (authed: boolean) => void,
|
|
) {
|
|
super(
|
|
nickservOptions.responseCommand || 'NOTICE',
|
|
(line) => {
|
|
if (!line.trailing) {
|
|
return resolve(false);
|
|
}
|
|
|
|
const splitline = line.trailing.trim().split(' ');
|
|
let result = false;
|
|
if (splitline[2] !== '0') {
|
|
result = true;
|
|
}
|
|
|
|
resolve(result);
|
|
},
|
|
nickservOptions.nickservBot || 'NickServ',
|
|
);
|
|
}
|
|
}
|
|
|
|
export class NickServValidator {
|
|
public nickservStore: { [key: string]: INickStore } = {};
|
|
|
|
constructor(public irc: IRCConnectionWrapper) {}
|
|
|
|
async getNickStatus(nickname: string): Promise<boolean> {
|
|
return new Promise<boolean>((resolve) => {
|
|
if (this.nickservStore[nickname] != null) {
|
|
if (this.nickservStore[nickname].result === true) {
|
|
resolve(true);
|
|
return;
|
|
} else {
|
|
if (this.nickservStore[nickname].checked < Date.now() - 1800000) {
|
|
// 30 minutes
|
|
delete this.nickservStore[nickname];
|
|
}
|
|
}
|
|
}
|
|
|
|
if (
|
|
this.irc.options.nickserv &&
|
|
this.irc.options.nickserv.enabled &&
|
|
this.irc.options.nickserv.command
|
|
) {
|
|
this.irc.useCollector(
|
|
new NickServCollector(
|
|
this.irc.options.nickserv,
|
|
(result: boolean) => {
|
|
this.nickservStore[nickname] = {
|
|
result,
|
|
checked: Date.now(),
|
|
};
|
|
resolve(result);
|
|
},
|
|
),
|
|
'PRIVMSG nickserv :%s %s',
|
|
this.irc.options.nickserv.command,
|
|
nickname,
|
|
);
|
|
}
|
|
});
|
|
}
|
|
}
|