Refactor(low-risk): oscar baseOpt helper, shared screenname utils, remove dead loadFeatures
This commit is contained in:
parent
ba6fb4ef6d
commit
d25812161c
6 changed files with 31 additions and 66 deletions
|
|
@ -1,3 +1,4 @@
|
|||
export { isIcqScreenName } from './screenname.js';
|
||||
// src/icq-presence.js - isolated best-effort ICQ presence shim.
|
||||
//
|
||||
// Problem: AIM users see webui login/logout presence correctly, ICQ (all-digit UIN)
|
||||
|
|
@ -7,9 +8,7 @@
|
|||
// Do not add guessing here. Only safe, documented TOC commands with clear intent.
|
||||
// Wrapped in try/catch by caller so failures never break normal login.
|
||||
|
||||
export function isIcqScreenName(screenName) {
|
||||
return /^\d+$/.test(String(screenName || '').trim());
|
||||
}
|
||||
import { isIcqScreenName } from './screenname.js';
|
||||
|
||||
// Best-effort: after TOC signon, nudge presence for ICQ UINs.
|
||||
// toc_set_idle 0 is documented by TiK/Open OSCAR Server as "not idle at all".
|
||||
|
|
|
|||
26
src/oscar.js
26
src/oscar.js
|
|
@ -61,6 +61,8 @@ async function call(path, params = {}, { base = WEBAPI_BASE, method = 'GET', for
|
|||
return response.data;
|
||||
}
|
||||
|
||||
const baseOpt = (base) => (base ? { base } : {});
|
||||
|
||||
// --- Auth / session ---
|
||||
|
||||
export async function clientLogin(screenName, password, { base } = {}) {
|
||||
|
|
@ -72,7 +74,7 @@ export async function clientLogin(screenName, password, { base } = {}) {
|
|||
return call('/auth/clientLogin', params, {
|
||||
method: 'POST',
|
||||
formBody: { username: screenName, password },
|
||||
...(base ? { base } : {}),
|
||||
...baseOpt(base),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -80,54 +82,50 @@ export async function startSession(token, { events = [], base } = {}) {
|
|||
// TODO(M0): confirm param names and how event subscriptions are requested.
|
||||
const params = { a: token };
|
||||
if (events.length) params.events = events.join(',');
|
||||
return call('/aim/startSession', params, base ? { base } : {});
|
||||
return call('/aim/startSession', params, baseOpt(base));
|
||||
}
|
||||
|
||||
export async function fetchEvents(aimsid, seqNum, { timeout = 25000, base } = {}) {
|
||||
// TODO(M0): confirm timeout units/param name and seqNum semantics.
|
||||
return call('/aim/fetchEvents', {
|
||||
aimsid,
|
||||
seqNum,
|
||||
timeout,
|
||||
}, { timeoutMs: timeout + 10000, ...(base ? { base } : {}) });
|
||||
return call('/aim/fetchEvents', { aimsid, seqNum, timeout }, { timeoutMs: timeout + 10000, ...baseOpt(base) });
|
||||
}
|
||||
|
||||
export async function endSession(aimsid, { base } = {}) {
|
||||
return call('/aim/endSession', { aimsid }, base ? { base } : {});
|
||||
return call('/aim/endSession', { aimsid }, baseOpt(base));
|
||||
}
|
||||
|
||||
// --- IM ---
|
||||
|
||||
export async function sendIM(aimsid, to, message, { base } = {}) {
|
||||
// TODO(M0): confirm param names (t/target, message/msg).
|
||||
return call('/im/sendIM', { aimsid, t: to, message }, base ? { base } : {});
|
||||
return call('/im/sendIM', { aimsid, t: to, message }, baseOpt(base));
|
||||
}
|
||||
|
||||
// --- Chat ---
|
||||
|
||||
export async function createAndJoinChat(aimsid, room, { base } = {}) {
|
||||
// TODO(M0): confirm param name for room/exchange.
|
||||
return call('/chat/createAndJoinChat', { aimsid, room }, base ? { base } : {});
|
||||
return call('/chat/createAndJoinChat', { aimsid, room }, baseOpt(base));
|
||||
}
|
||||
|
||||
export async function sendChatMessage(aimsid, chatId, message, { base } = {}) {
|
||||
// TODO(M0): confirm chat id param name.
|
||||
return call('/chat/sendMessage', { aimsid, c: chatId, message }, base ? { base } : {});
|
||||
return call('/chat/sendMessage', { aimsid, c: chatId, message }, baseOpt(base));
|
||||
}
|
||||
|
||||
export async function leaveChat(aimsid, chatId, { base } = {}) {
|
||||
return call('/chat/leaveChat', { aimsid, c: chatId }, base ? { base } : {});
|
||||
return call('/chat/leaveChat', { aimsid, c: chatId }, baseOpt(base));
|
||||
}
|
||||
|
||||
// --- Presence / buddy list ---
|
||||
|
||||
export async function getPresence(aimsid, { base } = {}) {
|
||||
return call('/presence/get', { aimsid }, base ? { base } : {});
|
||||
return call('/presence/get', { aimsid }, baseOpt(base));
|
||||
}
|
||||
|
||||
export async function addBuddy(aimsid, screenName, { group = 'Buddies', base } = {}) {
|
||||
// TODO(M0): confirm param names and group behavior.
|
||||
return call('/buddylist/addBuddy', { aimsid, buddy: screenName, group }, base ? { base } : {});
|
||||
return call('/buddylist/addBuddy', { aimsid, buddy: screenName, group }, baseOpt(base));
|
||||
}
|
||||
|
||||
// --- Management API (localhost only, unauthenticated) ---
|
||||
|
|
|
|||
9
src/screenname.js
Normal file
9
src/screenname.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
// src/screenname.js - shared screen name helpers.
|
||||
|
||||
export function normalizeScreenName(sn) {
|
||||
return String(sn || '').toLowerCase().replace(/\s+/g, '');
|
||||
}
|
||||
|
||||
export function isIcqScreenName(screenName) {
|
||||
return /^\d+$/.test(String(screenName || '').trim());
|
||||
}
|
||||
|
|
@ -3,17 +3,14 @@ import express from 'express';
|
|||
import session from 'express-session';
|
||||
import rateLimit from 'express-rate-limit';
|
||||
import helmet from 'helmet';
|
||||
import { readdir } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
import { PORT, LISTEN_HOST, SESSION_SECRET, TRUST_PROXY, COOKIE_SECURE, SESSION_MAX_AGE_MS, logStartupConfig } from './config.js';
|
||||
import { getDb } from './db.js';
|
||||
import { SqliteSessionStore } from './sqlite-session-store.js';
|
||||
import * as oscar from './oscar.js';
|
||||
import { bus } from './bus.js';
|
||||
import { broadcast } from './poller.js';
|
||||
import { authRouter, requireLogin, cleanupSessionById } from './routes/auth.js';
|
||||
import { authRouter, cleanupSessionById } from './routes/auth.js';
|
||||
import { stopTOCSession } from './toc-session.js';
|
||||
import { streamRouter } from './routes/stream.js';
|
||||
import { messagesRouter } from './routes/messages.js';
|
||||
|
|
@ -119,34 +116,6 @@ app.use((err, _req, res, _next) => {
|
|||
res.status(500).json({ error: 'Internal server error' });
|
||||
});
|
||||
|
||||
async function loadFeatures() {
|
||||
const featuresDir = path.resolve(__dirname, 'features');
|
||||
let entries = [];
|
||||
try {
|
||||
entries = await readdir(featuresDir);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
const ctx = {
|
||||
app,
|
||||
bus,
|
||||
oscar,
|
||||
sessions: sessionsStore,
|
||||
broadcast,
|
||||
requireLogin,
|
||||
};
|
||||
|
||||
for (const file of entries.filter((f) => f.endsWith('.js')).sort()) {
|
||||
const full = path.join(featuresDir, file);
|
||||
const mod = await import(pathToFileURL(full).href);
|
||||
if (typeof mod.register === 'function') {
|
||||
mod.register(ctx);
|
||||
console.log(`[features] loaded ${file}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let shuttingDown = false;
|
||||
|
||||
async function shutdown(signal) {
|
||||
|
|
@ -204,7 +173,6 @@ function startSessionSweep() {
|
|||
async function main() {
|
||||
logStartupConfig();
|
||||
await oscar.healthCheck();
|
||||
await loadFeatures();
|
||||
startSessionSweep();
|
||||
|
||||
app.listen(PORT, LISTEN_HOST, () => {
|
||||
|
|
|
|||
|
|
@ -6,10 +6,7 @@ import { TOCClient } from './toc.js';
|
|||
import { bus } from './bus.js';
|
||||
import { broadcast } from './poller.js';
|
||||
import { applyIcqPresenceHints } from './icq-presence.js';
|
||||
|
||||
function normalize(sn) {
|
||||
return String(sn || '').toLowerCase().replace(/\s+/g, '');
|
||||
}
|
||||
import { normalizeScreenName } from './screenname.js';
|
||||
|
||||
function makeEvent(type, data) {
|
||||
return { type, time: Date.now(), data, raw: { type, eventData: data } };
|
||||
|
|
@ -158,7 +155,7 @@ function extractBuddiesFromConfig(rest) {
|
|||
// heuristic; avoid obvious config markers
|
||||
}
|
||||
}
|
||||
return [...new Set(out.map(normalize).filter(Boolean))];
|
||||
return [...new Set(out.map(normalizeScreenName).filter(Boolean))];
|
||||
}
|
||||
|
||||
export function stopTOCSession(rec) {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
export { isIcqScreenName } from "./screenname.js";
|
||||
// src/web-presence.js - isolated webui presence overlay.
|
||||
//
|
||||
// Workaround: some ICQ UINs do not get BuddyArrived broadcast by the OSCAR server
|
||||
|
|
@ -9,17 +10,10 @@
|
|||
|
||||
import { broadcast } from './poller.js';
|
||||
import { allSessions, getSessionByScreenName } from './sessions.js';
|
||||
import { isIcqScreenName, normalizeScreenName } from './screenname.js';
|
||||
|
||||
const webuiOnline = new Set(); // normalized screen names
|
||||
|
||||
function normalize(sn) {
|
||||
return String(sn || '').toLowerCase().replace(/\s+/g, '');
|
||||
}
|
||||
|
||||
export function isIcqScreenName(screenName) {
|
||||
return /^\d+$/.test(String(screenName || '').trim());
|
||||
}
|
||||
|
||||
function makePresenceEvent(screenName, online) {
|
||||
return {
|
||||
type: 'presence',
|
||||
|
|
@ -39,14 +33,14 @@ export function broadcastWebuiPresence(screenName, online) {
|
|||
|
||||
export function markWebuiOnline(screenName) {
|
||||
if (!isIcqScreenName(screenName)) return;
|
||||
webuiOnline.add(normalize(screenName));
|
||||
webuiOnline.add(normalizeScreenName(screenName));
|
||||
console.log(`[web-presence] online ${screenName}, sessions=${[...allSessions()].length}`);
|
||||
broadcastWebuiPresence(screenName, true);
|
||||
}
|
||||
|
||||
export function markWebuiOfflineIfLast(screenName) {
|
||||
if (!isIcqScreenName(screenName)) return;
|
||||
const norm = normalize(screenName);
|
||||
const norm = normalizeScreenName(screenName);
|
||||
// If another webui session exists for same screen name, keep online.
|
||||
if (getSessionByScreenName(screenName)) return;
|
||||
webuiOnline.delete(norm);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue