atagen

malice

public
Star0
Watch0
77eddb6b710ffe2b884b8bdf7b0611041f7358e6
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git

Blame: web/src/lib/commands.ts

e4c64c36f105atagen2026-10-24 07:09 UTC1
import type { Person } from './types';
e4c64c36f105atagen2026-10-24 07:09 UTC2
e4c64c36f105atagen2026-10-24 07:09 UTC3
export interface ParsedCommand {
e4c64c36f105atagen2026-10-24 07:09 UTC4
  name: string;
e4c64c36f105atagen2026-10-24 07:09 UTC5
  args: string;
e4c64c36f105atagen2026-10-24 07:09 UTC6
}
e4c64c36f105atagen2026-10-24 07:09 UTC7
e4c64c36f105atagen2026-10-24 07:09 UTC8
export const COMMANDS = [
e4c64c36f105atagen2026-10-24 07:09 UTC9
  'me',
7cca1d5628d1atagen2026-10-24 07:36 UTC10
  'notice',
e4c64c36f105atagen2026-10-24 07:09 UTC11
  'msg',
e4c64c36f105atagen2026-10-24 07:09 UTC12
  'join',
e4c64c36f105atagen2026-10-24 07:09 UTC13
  'leave',
e4c64c36f105atagen2026-10-24 07:09 UTC14
  'invite',
e4c64c36f105atagen2026-10-24 07:09 UTC15
  'disinvite',
e4c64c36f105atagen2026-10-24 07:09 UTC16
  'kick',
e4c64c36f105atagen2026-10-24 07:09 UTC17
  'ban',
e4c64c36f105atagen2026-10-24 07:09 UTC18
  'unban',
e4c64c36f105atagen2026-10-24 07:09 UTC19
  'powerlevel',
7cca1d5628d1atagen2026-10-24 07:36 UTC20
  'mute',
7cca1d5628d1atagen2026-10-24 07:36 UTC21
  'unmute',
7cca1d5628d1atagen2026-10-24 07:36 UTC22
  'ignore',
7cca1d5628d1atagen2026-10-24 07:36 UTC23
  'unignore',
e4c64c36f105atagen2026-10-24 07:09 UTC24
  'delete',
7cca1d5628d1atagen2026-10-24 07:36 UTC25
  'roomnick',
e4c64c36f105atagen2026-10-24 07:09 UTC26
  'globalnick',
7cca1d5628d1atagen2026-10-24 07:36 UTC27
  'roomavatar',
e4c64c36f105atagen2026-10-24 07:09 UTC28
  'globalavatar'
e4c64c36f105atagen2026-10-24 07:09 UTC29
] as const;
e4c64c36f105atagen2026-10-24 07:09 UTC30
export type CommandName = (typeof COMMANDS)[number];
e4c64c36f105atagen2026-10-24 07:09 UTC31
e4c64c36f105atagen2026-10-24 07:09 UTC32
/** A leading `/word`, or null for plain text. `//text` sends `/text` as a message. */
e4c64c36f105atagen2026-10-24 07:09 UTC33
export function parseCommand(text: string): ParsedCommand | null {
e4c64c36f105atagen2026-10-24 07:09 UTC34
  const match = /^\/([a-z]+)(?:\s+([\s\S]*))?$/i.exec(text.trim());
e4c64c36f105atagen2026-10-24 07:09 UTC35
  if (!match) return null;
e4c64c36f105atagen2026-10-24 07:09 UTC36
  return { name: match[1].toLowerCase(), args: (match[2] ?? '').trim() };
e4c64c36f105atagen2026-10-24 07:09 UTC37
}
e4c64c36f105atagen2026-10-24 07:09 UTC38
e4c64c36f105atagen2026-10-24 07:09 UTC39
export function isKnownCommand(name: string): name is CommandName {
e4c64c36f105atagen2026-10-24 07:09 UTC40
  return (COMMANDS as readonly string[]).includes(name);
e4c64c36f105atagen2026-10-24 07:09 UTC41
}
e4c64c36f105atagen2026-10-24 07:09 UTC42
e4c64c36f105atagen2026-10-24 07:09 UTC43
export function unescapeCommand(text: string): string {
e4c64c36f105atagen2026-10-24 07:09 UTC44
  return text.startsWith('//') ? text.slice(1) : text;
e4c64c36f105atagen2026-10-24 07:09 UTC45
}
e4c64c36f105atagen2026-10-24 07:09 UTC46
e4c64c36f105atagen2026-10-24 07:09 UTC47
export type Target = { user_id: string; rest: string } | { error: string };
e4c64c36f105atagen2026-10-24 07:09 UTC48
e4c64c36f105atagen2026-10-24 07:09 UTC49
/** The user an argument string starts with, by matrix id or by a known display name. */
e4c64c36f105atagen2026-10-24 07:09 UTC50
export function splitTarget(args: string, people: Person[]): Target {
e4c64c36f105atagen2026-10-24 07:09 UTC51
  if (!args) return { error: 'Name someone' };
e4c64c36f105atagen2026-10-24 07:09 UTC52
  if (args.startsWith('@')) {
e4c64c36f105atagen2026-10-24 07:09 UTC53
    const [id, ...rest] = args.split(/\s+/);
e4c64c36f105atagen2026-10-24 07:09 UTC54
    if (/^@[^:\s]+:\S+$/.test(id)) return { user_id: id, rest: rest.join(' ') };
e4c64c36f105atagen2026-10-24 07:09 UTC55
  }
e4c64c36f105atagen2026-10-24 07:09 UTC56
  const lower = args.toLowerCase();
e4c64c36f105atagen2026-10-24 07:09 UTC57
  let best: Person[] = [];
e4c64c36f105atagen2026-10-24 07:09 UTC58
  let bestLength = 0;
e4c64c36f105atagen2026-10-24 07:09 UTC59
  for (const person of people) {
e4c64c36f105atagen2026-10-24 07:09 UTC60
    const name = person.name.toLowerCase();
e4c64c36f105atagen2026-10-24 07:09 UTC61
    if (!name || name.length < bestLength) continue;
e4c64c36f105atagen2026-10-24 07:09 UTC62
    if (lower !== name && !lower.startsWith(`${name} `)) continue;
e4c64c36f105atagen2026-10-24 07:09 UTC63
    if (name.length > bestLength) {
e4c64c36f105atagen2026-10-24 07:09 UTC64
      best = [person];
e4c64c36f105atagen2026-10-24 07:09 UTC65
      bestLength = name.length;
e4c64c36f105atagen2026-10-24 07:09 UTC66
    } else {
e4c64c36f105atagen2026-10-24 07:09 UTC67
      best.push(person);
e4c64c36f105atagen2026-10-24 07:09 UTC68
    }
e4c64c36f105atagen2026-10-24 07:09 UTC69
  }
e4c64c36f105atagen2026-10-24 07:09 UTC70
  const ids = [...new Set(best.map((p) => p.user_id))];
e4c64c36f105atagen2026-10-24 07:09 UTC71
  if (ids.length === 0) return { error: `No one here is called ${args.split(/\s+/)[0]}` };
e4c64c36f105atagen2026-10-24 07:09 UTC72
  if (ids.length > 1) return { error: `Several people are called ${best[0].name}, use their @id` };
e4c64c36f105atagen2026-10-24 07:09 UTC73
  return { user_id: ids[0], rest: args.slice(bestLength).trim() };
e4c64c36f105atagen2026-10-24 07:09 UTC74
}