Records the uncommitted battle-session work exactly as it was left (local service matchmaking, settlement, chat, native LocalMP hooks, battle gui/exe patches) so the following review and rework commits show every correction as a diff against this state. This commit is not reviewed and is known to be incomplete: in-game testing reported wrong settlement rewards, single-country teams, missing random/fixed supplement slots, misattributed hit messages and other regressions. Do not merge this branch before the rework lands.
285 lines
12 KiB
TypeScript
285 lines
12 KiB
TypeScript
/**
|
|
* In-process RFC 1459 + Gaijin IRC chat server (`IRCClient` in `aces.exe` 0x133c5e0).
|
|
*
|
|
* Serves hangar general rooms, custom player rooms, discussion threads (`XTJOIN` / `XTMETA`),
|
|
* private messages, and session presence so `gchat_is_connected()` and `gchat_is_enabled()`
|
|
* evaluate `true` natively in both hangar and battle.
|
|
*/
|
|
import type { Duplex } from 'node:stream';
|
|
import {
|
|
formatServerCommand,
|
|
formatServerNumeric,
|
|
formatUserEvent,
|
|
IRC_SERVER_HOST,
|
|
parseIrcLine,
|
|
sanitizeChannelName,
|
|
type IrcCommand,
|
|
} from './irc-protocol.ts';
|
|
|
|
interface IrcSession {
|
|
readonly socket: Duplex;
|
|
nick: string;
|
|
user: string;
|
|
realName: string;
|
|
registered: boolean;
|
|
buffer: string;
|
|
readonly channels: Set<string>;
|
|
}
|
|
|
|
interface IrcChannel {
|
|
readonly name: string;
|
|
topic: string;
|
|
readonly members: Set<IrcSession>;
|
|
}
|
|
|
|
const MAX_BUFFER_CHARS = 65_536;
|
|
|
|
export class IrcServer {
|
|
readonly #sessions = new Set<IrcSession>();
|
|
readonly #channels = new Map<string, IrcChannel>();
|
|
#nextThreadId = 1;
|
|
|
|
accept(socket: Duplex): void {
|
|
const session: IrcSession = {
|
|
socket, nick: '', user: '', realName: '', registered: false, buffer: '', channels: new Set(),
|
|
};
|
|
this.#sessions.add(session);
|
|
|
|
socket.on('data', (chunk: Buffer | string) => {
|
|
session.buffer += typeof chunk === 'string' ? chunk : chunk.toString('utf8');
|
|
if (session.buffer.length > MAX_BUFFER_CHARS) {
|
|
socket.destroy();
|
|
return;
|
|
}
|
|
let nl = session.buffer.indexOf('\n');
|
|
while (nl >= 0) {
|
|
const rawLine = session.buffer.slice(0, nl);
|
|
session.buffer = session.buffer.slice(nl + 1);
|
|
const cmd = parseIrcLine(rawLine);
|
|
if (cmd) this.#handleCommand(session, cmd);
|
|
nl = session.buffer.indexOf('\n');
|
|
}
|
|
});
|
|
socket.once('close', () => this.#removeSession(session, 'Client disconnected'));
|
|
socket.once('error', () => socket.destroy());
|
|
}
|
|
|
|
#handleCommand(session: IrcSession, cmd: IrcCommand): void {
|
|
const [p0 = '', p1 = ''] = cmd.params;
|
|
switch (cmd.verb) {
|
|
case 'CAP': if (p0.toUpperCase() === 'LS') this.#send(session, formatServerCommand('CAP', ['*', 'LS'], '')); return;
|
|
case 'PASS':
|
|
case 'PONG': return;
|
|
case 'NICK': this.#handleNick(session, p0); return;
|
|
case 'USER':
|
|
session.user = p0 || '0';
|
|
session.realName = cmd.params[3] ?? session.user;
|
|
this.#tryCompleteRegistration(session);
|
|
return;
|
|
case 'PING': this.#send(session, formatServerCommand('PONG', [IRC_SERVER_HOST], p0 || IRC_SERVER_HOST)); return;
|
|
case 'JOIN': this.#handleJoin(session, p0); return;
|
|
case 'PART': this.#handlePart(session, p0, p1); return;
|
|
case 'PRIVMSG':
|
|
case 'NOTICE': this.#handleMessage(session, cmd.verb, p0, p1); return;
|
|
case 'NAMES': this.#sendNames(session, sanitizeChannelName(p0 || '#general')); return;
|
|
case 'LIST': this.#handleList(session); return;
|
|
case 'TOPIC': this.#handleTopic(session, cmd.params); return;
|
|
case 'MODE': this.#handleMode(session, p0); return;
|
|
case 'WHO': this.#handleWho(session, p0); return;
|
|
case 'WHOIS': this.#handleWhois(session, p0); return;
|
|
case 'XTJOIN': this.#handleXtJoin(session, cmd.params); return;
|
|
case 'XTMETA': this.#handleXtMeta(session, cmd.params); return;
|
|
case 'QUIT':
|
|
this.#removeSession(session, p0 || 'Quit');
|
|
session.socket.end();
|
|
return;
|
|
default: return;
|
|
}
|
|
}
|
|
|
|
#handleNick(session: IrcSession, rawNick: string): void {
|
|
const nextNick = rawNick.trim() || `Player${this.#sessions.size}`;
|
|
const prevNick = session.nick;
|
|
session.nick = nextNick;
|
|
if (session.registered && prevNick && prevNick !== nextNick) {
|
|
this.#send(session, formatUserEvent(prevNick, session.user, 'NICK', [], nextNick));
|
|
return;
|
|
}
|
|
this.#tryCompleteRegistration(session);
|
|
}
|
|
|
|
#tryCompleteRegistration(session: IrcSession): void {
|
|
if (session.registered || !session.nick || !session.user) return;
|
|
session.registered = true;
|
|
const { nick, user } = session;
|
|
this.#send(session, formatServerNumeric('001', nick, [], `Welcome to the War Thunder Local Chat Network ${nick}!${user}@127.0.0.1`));
|
|
this.#send(session, formatServerNumeric('002', nick, [], `Your host is ${IRC_SERVER_HOST}, running version 1.0`));
|
|
this.#send(session, formatServerNumeric('003', nick, [], 'This server was created locally'));
|
|
this.#send(session, formatServerNumeric('004', nick, [IRC_SERVER_HOST, '1.0', 'iow', 'ntov']));
|
|
this.#send(session, formatServerNumeric('375', nick, [], `- ${IRC_SERVER_HOST} Message of the Day -`));
|
|
this.#send(session, formatServerNumeric('372', nick, [], '- Local offline chat service active'));
|
|
this.#send(session, formatServerNumeric('376', nick, [], 'End of /MOTD command.'));
|
|
}
|
|
|
|
#handleJoin(session: IrcSession, rawTargets: string): void {
|
|
if (!rawTargets) return;
|
|
if (rawTargets === '0') {
|
|
for (const chan of [...session.channels]) this.#partOne(session, chan, '');
|
|
return;
|
|
}
|
|
for (const raw of rawTargets.split(',')) {
|
|
if (!raw.trim()) continue;
|
|
this.#joinChannel(session, sanitizeChannelName(raw), '');
|
|
}
|
|
}
|
|
|
|
#joinChannel(session: IrcSession, channelName: string, initialTopic: string): void {
|
|
let channel = this.#channels.get(channelName);
|
|
if (!channel) {
|
|
channel = { name: channelName, topic: initialTopic, members: new Set() };
|
|
this.#channels.set(channelName, channel);
|
|
} else if (initialTopic && !channel.topic) {
|
|
channel.topic = initialTopic;
|
|
}
|
|
channel.members.add(session);
|
|
session.channels.add(channelName);
|
|
|
|
const joinLine = formatUserEvent(session.nick, session.user, 'JOIN', [], channelName);
|
|
for (const member of channel.members) this.#send(member, joinLine);
|
|
if (channel.topic) {
|
|
this.#send(session, formatServerNumeric('332', session.nick, [channelName], channel.topic));
|
|
} else {
|
|
this.#send(session, formatServerNumeric('331', session.nick, [channelName], 'No topic is set'));
|
|
}
|
|
this.#sendNames(session, channelName);
|
|
}
|
|
|
|
#handlePart(session: IrcSession, rawTargets: string, reason: string): void {
|
|
for (const raw of rawTargets.split(',')) {
|
|
if (!raw.trim()) continue;
|
|
this.#partOne(session, sanitizeChannelName(raw), reason);
|
|
}
|
|
}
|
|
|
|
#partOne(session: IrcSession, channelName: string, reason: string): void {
|
|
const channel = this.#channels.get(channelName);
|
|
if (!channel || !channel.members.has(session)) return;
|
|
const partLine = formatUserEvent(session.nick, session.user, 'PART', [channelName], reason || undefined);
|
|
for (const member of channel.members) this.#send(member, partLine);
|
|
channel.members.delete(session);
|
|
session.channels.delete(channelName);
|
|
if (channel.members.size === 0) this.#channels.delete(channelName);
|
|
}
|
|
|
|
#handleMessage(session: IrcSession, verb: 'PRIVMSG' | 'NOTICE', target: string, text: string): void {
|
|
if (!target) return;
|
|
if (target.startsWith('#') || target.startsWith('&')) {
|
|
const channel = this.#channels.get(sanitizeChannelName(target));
|
|
if (!channel) {
|
|
if (verb === 'PRIVMSG') this.#send(session, formatServerNumeric('403', session.nick, [target], 'No such channel'));
|
|
return;
|
|
}
|
|
const line = formatUserEvent(session.nick, session.user, verb, [channel.name], text);
|
|
for (const member of channel.members) {
|
|
if (member !== session) this.#send(member, line);
|
|
}
|
|
return;
|
|
}
|
|
const recipient = [...this.#sessions].find((peer) => peer.registered && peer.nick === target);
|
|
if (!recipient) {
|
|
if (verb === 'PRIVMSG') this.#send(session, formatServerNumeric('401', session.nick, [target], 'No such nick/channel'));
|
|
return;
|
|
}
|
|
this.#send(recipient, formatUserEvent(session.nick, session.user, verb, [recipient.nick], text));
|
|
}
|
|
|
|
#sendNames(session: IrcSession, channelName: string): void {
|
|
const channel = this.#channels.get(channelName);
|
|
const nicks = channel ? [...channel.members].map((m) => m.nick).filter(Boolean).join(' ') : session.nick;
|
|
this.#send(session, formatServerNumeric('353', session.nick, ['=', channelName], nicks));
|
|
this.#send(session, formatServerNumeric('366', session.nick, [channelName], 'End of /NAMES list.'));
|
|
}
|
|
|
|
#handleList(session: IrcSession): void {
|
|
this.#send(session, formatServerNumeric('321', session.nick, ['Channel'], 'Users Name'));
|
|
for (const channel of this.#channels.values()) {
|
|
if (channel.name === '#___empty___') continue;
|
|
this.#send(session, formatServerNumeric('322', session.nick, [channel.name, String(channel.members.size)], channel.topic));
|
|
}
|
|
this.#send(session, formatServerNumeric('323', session.nick, [], 'End of /LIST'));
|
|
}
|
|
|
|
#handleTopic(session: IrcSession, params: readonly string[]): void {
|
|
const channelName = sanitizeChannelName(params[0] ?? '');
|
|
const channel = this.#channels.get(channelName);
|
|
if (!channel) {
|
|
this.#send(session, formatServerNumeric('403', session.nick, [channelName], 'No such channel'));
|
|
return;
|
|
}
|
|
if (params.length >= 2) {
|
|
channel.topic = params[1] ?? '';
|
|
const line = formatUserEvent(session.nick, session.user, 'TOPIC', [channelName], channel.topic);
|
|
for (const member of channel.members) this.#send(member, line);
|
|
return;
|
|
}
|
|
if (channel.topic) this.#send(session, formatServerNumeric('332', session.nick, [channelName], channel.topic));
|
|
else this.#send(session, formatServerNumeric('331', session.nick, [channelName], 'No topic is set'));
|
|
}
|
|
|
|
#handleMode(session: IrcSession, target: string): void {
|
|
if (target.startsWith('#') || target.startsWith('&')) {
|
|
this.#send(session, formatServerNumeric('324', session.nick, [sanitizeChannelName(target), '+nt']));
|
|
} else this.#send(session, formatServerNumeric('221', session.nick, ['+i']));
|
|
}
|
|
|
|
#handleWho(session: IrcSession, target: string): void {
|
|
const channel = target.startsWith('#') ? this.#channels.get(sanitizeChannelName(target)) : undefined;
|
|
for (const m of channel?.members ?? []) {
|
|
this.#send(session, formatServerNumeric('352', session.nick, [channel!.name, m.user, '127.0.0.1', IRC_SERVER_HOST, m.nick, 'H'], `0 ${m.realName}`));
|
|
}
|
|
this.#send(session, formatServerNumeric('315', session.nick, [target || '*'], 'End of /WHO list.'));
|
|
}
|
|
|
|
#handleWhois(session: IrcSession, targetNick: string): void {
|
|
const found = [...this.#sessions].find((peer) => peer.nick === targetNick);
|
|
if (found) this.#send(session, formatServerNumeric('311', session.nick, [found.nick, found.user, '127.0.0.1', '*'], found.realName));
|
|
this.#send(session, formatServerNumeric('318', session.nick, [targetNick || '*'], 'End of /WHOIS list.'));
|
|
}
|
|
|
|
#handleXtJoin(session: IrcSession, params: readonly string[]): void {
|
|
this.#joinChannel(session, `#___xthread_${this.#nextThreadId++}`, params[1] ?? params[0] ?? 'Discussion');
|
|
}
|
|
|
|
#handleXtMeta(session: IrcSession, params: readonly string[]): void {
|
|
const channelName = sanitizeChannelName(params[0] ?? '');
|
|
const channel = this.#channels.get(channelName);
|
|
if (!channel || params[1]?.toLowerCase() !== 'topic') return;
|
|
channel.topic = params[2] ?? '';
|
|
const line = formatUserEvent(session.nick, session.user, 'TOPIC', [channelName], channel.topic);
|
|
for (const member of channel.members) this.#send(member, line);
|
|
}
|
|
|
|
#removeSession(session: IrcSession, reason: string): void {
|
|
if (!this.#sessions.delete(session)) return;
|
|
const quitLine = session.registered ? formatUserEvent(session.nick, session.user, 'QUIT', [], reason) : '';
|
|
const notified = new Set<IrcSession>();
|
|
for (const channelName of session.channels) {
|
|
const channel = this.#channels.get(channelName);
|
|
if (!channel) continue;
|
|
channel.members.delete(session);
|
|
for (const peer of quitLine ? channel.members : []) {
|
|
if (!notified.has(peer)) {
|
|
notified.add(peer);
|
|
this.#send(peer, quitLine);
|
|
}
|
|
}
|
|
if (channel.members.size === 0) this.#channels.delete(channelName);
|
|
}
|
|
session.channels.clear();
|
|
}
|
|
|
|
#send(session: IrcSession, line: string): void {
|
|
if (!session.socket.destroyed && session.socket.writable) session.socket.write(line);
|
|
}
|
|
}
|