forked from SableClient/Sable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPKitCommandMessageHandler.ts
More file actions
249 lines (235 loc) · 8.48 KB
/
PKitCommandMessageHandler.ts
File metadata and controls
249 lines (235 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import {
addOrUpdatePerMessageProfile,
associateProxyWithProfile,
dropProxyAssociationForPMP,
getAllPerMessageProfiles,
getPerMessageProfileById,
PerMessageProfile,
} from '$hooks/usePerMessageProfile';
import { sendFeedback } from '$utils/sendFeedbackToUser';
import { MatrixClient, Room } from 'matrix-js-sdk';
import { generateShortId } from '$utils/shortIdGen';
const pkMemberRenameRegex = /^(pk;member)\s+"?([\w\s]+)"?\s*rename\s+"?([\w\s]+)"?$/;
const pkMemberNewRegex = /^(pk;member)\s+new\s+"?([\w\s]+)"?$/;
const pkMemberNewProxy = /^(pk;member)\s+"?([\w\s]+)"?\s+proxy(\s+add)?\s+(.*text.*)$/;
const pkMemberRemoveProxy = /^(pk;member)\s+"?([\w\s]+)"?\s+proxy\s+remove\s+(.*text.*)$/;
const helpTextPkMemberNew = 'To create a new persona: pk;member new Yumi';
const helpTextPkMemberRename = 'To rename a persona: pk;member "Rain Deer" rename "Micky Mouse"';
const helpTextPkMemberNewProxy = 'To add a shorthand to a persona: pk;member Yumi proxy [text]';
const helpTextPkMemberRemoveProxy =
'To remove a shorthand from a persona: pk;member Yumi proxy remove [text]';
const helpTextPkMember = `Available 'pk;member' commands:\n${helpTextPkMemberNew}\n${helpTextPkMemberRename}\n${helpTextPkMemberNewProxy}\n${helpTextPkMemberRemoveProxy}`;
function regexEscapeFallBackFunc(template: string): string {
return template.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
/**
* build a regex to recognize proxies
* a template can be for example `[text]` or `f:text`
*
* @param {string} template
* @return {*} {RegExp}
*/
function buildRegex(template: string): RegExp {
const [before, after] = template.split('text');
// RegExp.escape is baseline since May 2025
// @ts-ignore TS2322
const escape = RegExp.escape ?? regexEscapeFallBackFunc;
const pattern = `${escape(before)}(.+)${escape(after)}`;
return new RegExp(`^${pattern}$`);
}
/**
* a class to use as PluralKit command message handler
*
* @export
* @class PluralKitCommandMessageHandler
* @author Rye
*/
export class PKitCommandMessageHandler {
/**
* the matrix client we use, is set in the constructor
*
* @private
* @type {MatrixClient}
* @memberof PKitCommandMessageHandler
*/
private readonly mx: MatrixClient;
private message = '';
private readonly room: Room;
/**
* flag to interpret the name given as id
*
* @private
* @type {boolean}
* @memberof PKitCommandMessageHandler
*/
private useIdInsteadOfNameWherePossible: boolean;
public constructor(mx: MatrixClient, room: Room) {
this.mx = mx;
this.room = room;
this.useIdInsteadOfNameWherePossible = false;
}
/**
* Handler for `pk;member` commands
* @async
*/
private async memberHandler() {
if (this.message.match(pkMemberNewRegex)) {
// adding a new member
const cmdParts = pkMemberNewRegex.exec(this.message);
if (!cmdParts) {
sendFeedback(`malformed input, ${helpTextPkMemberNew}`, this.room, this.mx.getSafeUserId());
return;
}
const memberName = cmdParts[2];
const generatedID = generateShortId(5);
sendFeedback(
`adding new member has been created with id: ${generatedID} and name ${memberName}`,
this.room,
this.mx.getSafeUserId()
);
addOrUpdatePerMessageProfile(this.mx, { id: generatedID, name: memberName });
sendFeedback(
`added new member has been created with id: ${generatedID} and name ${memberName}`,
this.room,
this.mx.getSafeUserId()
);
} else if (this.message.match(pkMemberRenameRegex)) {
// renaming a profile based on the name
const cmdParts = pkMemberRenameRegex.exec(this.message);
if (!cmdParts) {
sendFeedback(
`malformed input, ${helpTextPkMemberRename}`,
this.room,
this.mx.getSafeUserId()
);
return;
}
// extract from the cmd the old and the new name
/**
* The old name we want to search for in our records, is in capture group 2
*/
const oldName = cmdParts[2];
/**
* The new name we want to set is in capture group 3
*/
const newName = cmdParts[3];
/**
* The id of the per-message-profile
*/
const pmpId = (await getAllPerMessageProfiles(this.mx)).find(
(pmp) => pmp.name === oldName
)?.id;
if (!pmpId) {
sendFeedback(
`Persona with name "${oldName}" doesn't exist in your records, ${helpTextPkMemberNew}`,
this.room,
this.mx.getSafeUserId()
);
return;
}
/**
* get the persona record we already have for the id
*/
const pmp = await getPerMessageProfileById(this.mx, pmpId);
if (!pmp) {
sendFeedback(
"Persona record can't be retrieved, data might be corrupted",
this.room,
this.mx.getSafeUserId()
);
return;
}
// actually change the name
pmp.name = newName;
sendFeedback(
`renaming your profile ${pmpId} from ${oldName} to ${newName}`,
this.room,
this.mx.getSafeUserId()
);
addOrUpdatePerMessageProfile(this.mx, pmp);
} else if (pkMemberRemoveProxy.test(this.message)) {
const cmdParts = pkMemberRemoveProxy.exec(this.message);
if (!cmdParts) return;
const name = cmdParts[2];
const matchAgainst = cmdParts[3];
const pmpId = this.useIdInsteadOfNameWherePossible
? name
: (await getAllPerMessageProfiles(this.mx)).find((pmp) => pmp.name === name)?.id;
if (!pmpId) {
sendFeedback(
`Persona with ${this.useIdInsteadOfNameWherePossible ? 'id' : 'name'} "${name}" doesn't exist in your records, ${helpTextPkMemberNew}`,
this.room,
this.mx.getSafeUserId()
);
return;
}
dropProxyAssociationForPMP(this.mx, matchAgainst);
sendFeedback(
`Persona with ${this.useIdInsteadOfNameWherePossible ? 'id' : 'name'} "${name}" (${pmpId}) is now no longer associated with ${matchAgainst}`,
this.room,
this.mx.getSafeUserId()
);
} else if (pkMemberNewProxy.test(this.message)) {
const cmdParts = pkMemberNewProxy.exec(this.message);
if (!cmdParts) return;
const name = cmdParts[2];
const matchAgainst = cmdParts[4];
const pmpId = this.useIdInsteadOfNameWherePossible
? name
: (await getAllPerMessageProfiles(this.mx)).find((pmp) => pmp.name === name)?.id;
if (!pmpId) {
sendFeedback(
`Persona with ${this.useIdInsteadOfNameWherePossible ? 'id' : 'name'} "${name}" doesn't exist in your records, ${helpTextPkMemberNew}`,
this.room,
this.mx.getSafeUserId()
);
return;
}
const matchAgainstRegExp = buildRegex(matchAgainst);
associateProxyWithProfile(this.mx, pmpId, matchAgainst, matchAgainstRegExp, false);
sendFeedback(
`Persona with ${this.useIdInsteadOfNameWherePossible ? 'id' : 'name'} "${name}" (${pmpId}) is now associated with ${matchAgainst}`,
this.room,
this.mx.getSafeUserId()
);
} else {
// default to looking up member info
const listOfProfiles: PerMessageProfile[] = await getAllPerMessageProfiles(this.mx);
const stringListOfProfiles: string = listOfProfiles
.map((pmp: PerMessageProfile) => `${pmp.id}: ${pmp.name ? pmp.name : '(empty name)'}`)
.join('\n');
sendFeedback(
`If you see this, you have messed up a command\n\nYou currently have the following persona set up:\n${stringListOfProfiles}\n\n${helpTextPkMember}`,
this.room,
this.mx.getSafeUserId()
);
}
}
/**
* check if a message is a pluralkit-style command
* @param message the message to check
* @returns true if it's a pluralkit style command
*/
public static isPKCommand(message: string): boolean {
return message.startsWith('pk;');
}
/**
* handle a message, which might be a pk command
* @param message the message we want to handle
* @returns void
*/
public async handleMessage(message: string, useId?: boolean): Promise<void> {
this.message = message;
this.useIdInsteadOfNameWherePossible = useId ?? false;
if (!this.message.startsWith('pk;')) return;
if (this.message.startsWith('pk;member')) {
await this.memberHandler();
return;
}
sendFeedback(
`Command currently not supported, right now compatibility is limited and only a subset of pk;member is supported\n\n${helpTextPkMember}`,
this.room,
this.mx.getSafeUserId()
);
}
}