Files
WT-Offline/packages/local-service/src/chat/irc-protocol.ts
T
N0VAN0M4LY b260d2899e chore(battle): snapshot the LocalMP rework baseline
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.
2026-11-28 03:15:47 +08:00

89 lines
3.2 KiB
TypeScript

/**
* RFC 1459 + Gaijin IRC wire parser and line formatters used by the local chat server.
*/
export const IRC_SERVER_HOST = 'wt.local';
export interface IrcCommand {
readonly verb: string;
readonly params: readonly string[];
}
const HANDSHAKE_PREFIX = /^(?:PASS |NICK |USER |CAP |PING |QUIT(?: |\r|\n|$))/i;
export function startsIrcHandshake(firstChunk: Buffer): boolean {
if (firstChunk.length < 4) return false;
const head = firstChunk.subarray(0, Math.min(firstChunk.length, 16)).toString('latin1');
return HANDSHAKE_PREFIX.test(head);
}
export function parseIrcLine(rawLine: string): IrcCommand | undefined {
let rest = rawLine.replace(/[\r\n]+$/, '');
if (rest.length === 0) return undefined;
if (rest.startsWith(':')) {
const space = rest.indexOf(' ');
if (space < 0) return undefined;
rest = rest.slice(space + 1).trimStart();
}
if (rest.length === 0) return undefined;
const verbEnd = rest.indexOf(' ');
if (verbEnd < 0) {
return { verb: rest.toUpperCase(), params: [] };
}
const verb = rest.slice(0, verbEnd).toUpperCase();
let cursor = rest.slice(verbEnd + 1);
const params: string[] = [];
while (cursor.length > 0) {
while (cursor.startsWith(' ')) cursor = cursor.slice(1);
if (cursor.length === 0) break;
if (cursor.startsWith(':')) {
params.push(cursor.slice(1));
break;
}
const nextSpace = cursor.indexOf(' ');
if (nextSpace < 0) {
params.push(cursor);
break;
}
params.push(cursor.slice(0, nextSpace));
cursor = cursor.slice(nextSpace + 1);
}
return { verb, params };
}
export function formatServerNumeric(code: string, nick: string, params: readonly string[], trailing?: string): string {
const target = sanitizeToken(nick || '*');
const middle = params.length > 0 ? ` ${params.map(sanitizeToken).join(' ')}` : '';
const suffix = trailing !== undefined ? ` :${sanitizeTrailing(trailing)}` : '';
return `:${IRC_SERVER_HOST} ${code} ${target}${middle}${suffix}\r\n`;
}
export function formatServerCommand(verb: string, params: readonly string[], trailing?: string): string {
const middle = params.length > 0 ? ` ${params.map(sanitizeToken).join(' ')}` : '';
const suffix = trailing !== undefined ? ` :${sanitizeTrailing(trailing)}` : '';
return `:${IRC_SERVER_HOST} ${verb}${middle}${suffix}\r\n`;
}
export function formatUserEvent(nick: string, user: string, verb: string, params: readonly string[], trailing?: string): string {
const safeNick = sanitizeToken(nick || 'Player');
const safeUser = sanitizeToken(user || '0');
const middle = params.length > 0 ? ` ${params.map(sanitizeToken).join(' ')}` : '';
const suffix = trailing !== undefined ? ` :${sanitizeTrailing(trailing)}` : '';
return `:${safeNick}!${safeUser}@127.0.0.1 ${verb}${middle}${suffix}\r\n`;
}
export function sanitizeChannelName(raw: string): string {
const trimmed = sanitizeToken(raw);
if (trimmed.length === 0) return '#general';
return trimmed.startsWith('#') || trimmed.startsWith('&') ? trimmed : `#${trimmed}`;
}
function sanitizeToken(text: string): string {
return text.replace(/[\r\n\0 ]+/g, '');
}
function sanitizeTrailing(text: string): string {
return text.replace(/[\r\n\0]+/g, ' ');
}