Skip to content

Commit 155ac64

Browse files
committed
fix(PMP-Command): fix the syntax handling of pmp related command
1 parent 2a4fd48 commit 155ac64

1 file changed

Lines changed: 35 additions & 24 deletions

File tree

src/app/hooks/useCommands.ts

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ const FLAG_PAT = String.raw`(?:^|\s)-(\w+)\b`;
5151
const FLAG_REG = new RegExp(FLAG_PAT);
5252
const FLAG_REG_G = new RegExp(FLAG_PAT, 'g');
5353

54+
const ADDPMP_REGEX = /(\w+) (name=)?"?([\w\s]*)"? (avatar=)?([\w.:/]+)/;
55+
const USEPMP_REGEX = /^(\w+)\s*(-g)?(-o)?(-u)?\s*(\d+)?$/;
56+
5457
export const splitPayloadContentAndFlags = (payload: string): [string, string | undefined] => {
5558
const flagMatch = new RegExp(FLAG_REG).exec(payload);
5659

@@ -493,26 +496,19 @@ export const useCommands = (mx: MatrixClient, room: Room): CommandRecord => {
493496
'Add or update a per message profile to your account. Example: /addpmp profileId name=Profile Name avatar=mxc://xyzabc',
494497
exe: async (payload) => {
495498
// Parse key=value pairs
496-
const parts = payload.split(' ');
497-
let avatarUrl: string | undefined;
498-
let name: string | undefined;
499-
parts.forEach((part, index) => {
500-
const [key, value] = part.split('=');
501-
if (key && value) {
502-
if (key === 'name' || key === 'avatar') {
503-
if (key === 'name') {
504-
name = parts
505-
.slice(index)
506-
.map((p) => p.split('=')[1])
507-
.join(' ');
508-
return;
509-
}
510-
if (key === 'avatar') avatarUrl = value;
511-
}
512-
}
513-
});
499+
const args = ADDPMP_REGEX.exec(payload);
500+
if (!args) {
501+
sendFeedback(`invalid payload`, room, mx.getSafeUserId());
502+
return;
503+
}
504+
const avatarUrl: string | undefined = args[5];
505+
const name: string | undefined = args[3];
506+
const profileId = args[1];
514507

515-
const profileId = parts[0]; // profileId is positional (before any key=)
508+
if (!avatarUrl || !name || !profileId) {
509+
sendFeedback(`invalid payload`, room, mx.getSafeUserId());
510+
return;
511+
}
516512

517513
const pmp: PerMessageProfile = {
518514
id: profileId,
@@ -568,10 +564,25 @@ export const useCommands = (mx: MatrixClient, room: Room): CommandRecord => {
568564
description:
569565
'Use a per message profile for this room once, or until reset. Example: /usepmp [profileId,reset]',
570566
exe: async (payload) => {
571-
// this command doesn't need to do anything, the composer will pick it up and apply the profile to the message being composed
572-
const profileId: string = splitWithSpace(payload)[0];
573-
const durationStr: string | undefined = splitWithSpace(payload)[1];
574-
let validUntil: number | undefined;
567+
const args = USEPMP_REGEX.exec(payload);
568+
if (!args) {
569+
sendFeedback(`invalid payload`, room, mx.getSafeUserId());
570+
return;
571+
}
572+
const profileId = args[1];
573+
const globalFlag = args[2] !== undefined;
574+
const onceFlag = args[3] !== undefined;
575+
// const untilFlag = args[4] !== undefined;
576+
const validUntil = Number.parseInt(args[5], 10);
577+
if (onceFlag || globalFlag) {
578+
sendFeedback(
579+
'Currently not implemented, consider using shorthands, with /pmpproxy id ✨:text',
580+
room,
581+
mx.getSafeUserId()
582+
);
583+
return;
584+
}
585+
575586
if (profileId.normalize() === 'reset') {
576587
setCurrentlyUsedPerMessageProfileIdForRoom(mx, room.roomId, undefined, undefined, true)
577588
.then(() => {
@@ -590,7 +601,7 @@ export const useCommands = (mx: MatrixClient, room: Room): CommandRecord => {
590601
.then(() => {
591602
sendFeedback(
592603
`Per message profile "${profileId}" will be used for messages in this room for the until ${
593-
durationStr ?? 'reset'
604+
validUntil ?? 'reset'
594605
}. Use \`/usepmp reset\` to reset it at any time.`,
595606
room,
596607
mx.getSafeUserId()

0 commit comments

Comments
 (0)