diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f2ccdb4 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +node_modules +dist +.env +.git diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..4f403df --- /dev/null +++ b/.env.example @@ -0,0 +1,19 @@ +# Copy to .env and change the password before deploying. +POSTGRES_USER=bbs +POSTGRES_PASSWORD=change-me +POSTGRES_DB=bbs + +# Host port the BBS web UI is published on +PORT=8080 + +# Simulated modem speed for server output (bps). 0 = no throttling. +BAUD=14400 + +# Number of simultaneous callers ("nodes") +MAX_NODES=7 + +# Hang up on idle callers after this many minutes +IDLE_TIMEOUT_MIN=10 + +# Timezone for BBS clocks / caller logs +TZ=America/Denver diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9c97bbd --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +dist +.env diff --git a/src/screens/mainMenu.ts b/src/screens/mainMenu.ts index 7392368..bc3b27b 100644 --- a/src/screens/mainMenu.ts +++ b/src/screens/mainMenu.ts @@ -1,7 +1,7 @@ import type { Session } from '../session/Session'; import { esc, padRight, center } from '../session/ansi'; import { nodes } from '../nodes'; -import { lastCallers } from '../users/userRepo'; +import { lastCallers, SYSOP_LEVEL } from '../users/userRepo'; import { config } from '../config'; const W = 78; // inner box width @@ -20,7 +20,7 @@ function minutesOn(s: Session): number { } function securityLabel(level: number): string { - if (level >= 255) return 'Sysop'; + if (level >= SYSOP_LEVEL) return 'Sysop'; if (level >= 100) return 'Co-Sysop'; if (level >= 50) return 'Elite'; if (level >= 20) return 'Validated'; diff --git a/src/screens/newUser.ts b/src/screens/newUser.ts index aee3810..9029eaa 100644 --- a/src/screens/newUser.ts +++ b/src/screens/newUser.ts @@ -1,5 +1,5 @@ import type { Session } from '../session/Session'; -import { createUser, handleExists, HandleTakenError, type User } from '../users/userRepo'; +import { createUser, handleExists, HandleTakenError, isSysop, type User } from '../users/userRepo'; import { esc } from '../session/ansi'; const HANDLE_RE = /^[A-Za-z0-9][A-Za-z0-9 _.\-]{1,18}[A-Za-z0-9_.\-]$/; @@ -89,7 +89,12 @@ export async function newUser(s: Session): Promise { try { const user = await createUser({ handle, password, realName, location, email }); - await s.println('\r\n |10Account created! |07Your access level is |15New User|07.'); + if (isSysop(user)) { + await s.println('\r\n |10Account created! |14You are the first user on this system,'); + await s.println(' |14so you have been made the |15SYSOP|14. |07The board is yours.'); + } else { + await s.println('\r\n |10Account created! |07Your access level is |15New User|07.'); + } return user; } catch (err) { if (err instanceof HandleTakenError) { diff --git a/src/users/userRepo.ts b/src/users/userRepo.ts index b0997ac..3fecd78 100644 --- a/src/users/userRepo.ts +++ b/src/users/userRepo.ts @@ -24,6 +24,10 @@ export interface NewUser { export class HandleTakenError extends Error {} +/** Security levels. Sysop-only features should check isSysop(). */ +export const NEW_USER_LEVEL = 10; +export const SYSOP_LEVEL = 255; + // Used so a login attempt for an unknown handle costs the same as a real one. const DUMMY_HASH = bcrypt.hashSync('not-a-real-password', 10); @@ -42,21 +46,36 @@ export async function verifyLogin(handle: string, password: string): Promise { const hash = await bcrypt.hash(u.password, 10); + const client = await pool.connect(); try { - const { rows } = await pool.query( - `INSERT INTO users (handle, password_hash, real_name, location, email) - VALUES ($1, $2, $3, $4, $5) RETURNING *`, - [u.handle, hash, u.realName, u.location, u.email], + await client.query('BEGIN'); + // Serialise signups so two simultaneous "first" users can't both become Sysop. + await client.query('SELECT pg_advisory_xact_lock(727275)'); + const { rows } = await client.query( + `INSERT INTO users (handle, password_hash, real_name, location, email, security_level) + VALUES ($1, $2, $3, $4, $5, + CASE WHEN EXISTS (SELECT 1 FROM users) THEN $6::int ELSE $7::int END) + RETURNING *`, + [u.handle, hash, u.realName, u.location, u.email, NEW_USER_LEVEL, SYSOP_LEVEL], ); + await client.query('COMMIT'); return rows[0]; } catch (err: unknown) { + await client.query('ROLLBACK'); if ((err as { code?: string }).code === '23505') throw new HandleTakenError(); throw err; + } finally { + client.release(); } } +export function isSysop(user: User): boolean { + return user.security_level >= SYSOP_LEVEL; +} + /** Records a successful logon. Returns the system caller number and the refreshed user row. */ export async function recordCall(userId: number, node: number): Promise<{ callerNo: number; user: User }> { const client = await pool.connect();