| 1 | import type { Person } from './types'; |
| 2 | |
| 3 | export interface ParsedCommand { |
| 4 | name: string; |
| 5 | args: string; |
| 6 | } |
| 7 | |
| 8 | export const COMMANDS = [ |
| 9 | 'me', |
| 10 | 'notice', |
| 11 | 'msg', |
| 12 | 'join', |
| 13 | 'leave', |
| 14 | 'invite', |
| 15 | 'disinvite', |
| 16 | 'kick', |
| 17 | 'ban', |
| 18 | 'unban', |
| 19 | 'powerlevel', |
| 20 | 'mute', |
| 21 | 'unmute', |
| 22 | 'ignore', |
| 23 | 'unignore', |
| 24 | 'delete', |
| 25 | 'roomnick', |
| 26 | 'globalnick', |
| 27 | 'roomavatar', |
| 28 | 'globalavatar' |
| 29 | ] as const; |
| 30 | export type CommandName = (typeof COMMANDS)[number]; |
| 31 | |
| 32 | /** A leading `/word`, or null for plain text. `//text` sends `/text` as a message. */ |
| 33 | export function parseCommand(text: string): ParsedCommand | null { |
| 34 | const match = /^\/([a-z]+)(?:\s+([\s\S]*))?$/i.exec(text.trim()); |
| 35 | if (!match) return null; |
| 36 | return { name: match[1].toLowerCase(), args: (match[2] ?? '').trim() }; |
| 37 | } |
| 38 | |
| 39 | export function isKnownCommand(name: string): name is CommandName { |
| 40 | return (COMMANDS as readonly string[]).includes(name); |
| 41 | } |
| 42 | |
| 43 | export function unescapeCommand(text: string): string { |
| 44 | return text.startsWith('//') ? text.slice(1) : text; |
| 45 | } |
| 46 | |
| 47 | export type Target = { user_id: string; rest: string } | { error: string }; |
| 48 | |
| 49 | /** The user an argument string starts with, by matrix id or by a known display name. */ |
| 50 | export function splitTarget(args: string, people: Person[]): Target { |
| 51 | if (!args) return { error: 'Name someone' }; |
| 52 | if (args.startsWith('@')) { |
| 53 | const [id, ...rest] = args.split(/\s+/); |
| 54 | if (/^@[^:\s]+:\S+$/.test(id)) return { user_id: id, rest: rest.join(' ') }; |
| 55 | } |
| 56 | const lower = args.toLowerCase(); |
| 57 | let best: Person[] = []; |
| 58 | let bestLength = 0; |
| 59 | for (const person of people) { |
| 60 | const name = person.name.toLowerCase(); |
| 61 | if (!name || name.length < bestLength) continue; |
| 62 | if (lower !== name && !lower.startsWith(`${name} `)) continue; |
| 63 | if (name.length > bestLength) { |
| 64 | best = [person]; |
| 65 | bestLength = name.length; |
| 66 | } else { |
| 67 | best.push(person); |
| 68 | } |
| 69 | } |
| 70 | const ids = [...new Set(best.map((p) => p.user_id))]; |
| 71 | if (ids.length === 0) return { error: `No one here is called ${args.split(/\s+/)[0]}` }; |
| 72 | if (ids.length > 1) return { error: `Several people are called ${best[0].name}, use their @id` }; |
| 73 | return { user_id: ids[0], rest: args.slice(bestLength).trim() }; |
| 74 | } |
| 75 | |
| 76 | export const COMMAND_USAGE: Record<CommandName, string> = { |
| 77 | me: '<action>', |
| 78 | notice: '<text>', |
| 79 | msg: '<user> [message]', |
| 80 | join: '<room>', |
| 81 | leave: '', |
| 82 | invite: '<user>', |
| 83 | disinvite: '<user> [reason]', |
| 84 | kick: '<user> [reason]', |
| 85 | ban: '<user> [reason]', |
| 86 | unban: '<user> [reason]', |
| 87 | powerlevel: '<user> <level>', |
| 88 | mute: '<user>', |
| 89 | unmute: '<user>', |
| 90 | ignore: '<user>', |
| 91 | unignore: '<user>', |
| 92 | delete: '[reason]', |
| 93 | roomnick: '[name]', |
| 94 | globalnick: '<name>', |
| 95 | roomavatar: '[mxc]', |
| 96 | globalavatar: '[mxc]' |
| 97 | }; |
| 98 | |
| 99 | /** The command name being typed when the caret sits in a leading `/word`. */ |
| 100 | export function commandQueryAt(text: string, caret: number): string | null { |
| 101 | const match = /^\/([a-z]*)$/i.exec(text.slice(0, caret)); |
| 102 | return match ? match[1].toLowerCase() : null; |
| 103 | } |
| 104 | |
| 105 | export function matchCommands(prefix: string): CommandName[] { |
| 106 | return COMMANDS.filter((name) => name.startsWith(prefix)); |
| 107 | } |