2023-11-02 13:59:06 +01:00

60 lines
1.4 KiB
JavaScript

import { xml } from "@xmpp/client";
export function randomHex () {
return Math.random().toString(16).slice(2, 8);
}
export function pingXml (to) {
return xml(
"iq", { to, id: `ping-${randomHex()}`, type: "get" },
xml("ping", { xmlns: "urn:xmpp:ping" })
)
}
export function isPingResponse (stanza) {
return stanza.attrs.id.match(/^ping/) &&
stanza.attrs.type === "result";
}
export function roomDirectMessageXml (room, nick, messageBody) {
return xml(
"message", { type: "chat", to: `${room}/${nick}` },
xml("body", {}, messageBody)
);
}
export function grantVoiceMessageXml (room, nick) {
return xml(
"iq", { id: `voice-${randomHex()}`, to: room, type: "set" },
xml("query", { xmlns: "http://jabber.org/protocol/muc#admin" },
xml("item", { nick: nick, role: "participant" }
))
);
}
export function parseForm (stanza) {
const form = stanza.getChild("x", "jabber:x:data");
if (!form) return false;
const fieldElements = form.getChildren("field");
const fields = [];
for (const e of fieldElements) {
fields.push({
name: e.attrs.var,
value: e.getChild("value").text()
})
}
return fields;
}
export function isVoiceRequest (stanza) {
const formFields = parseForm(stanza);
return !!formFields && formFields.find(f => {
return f.name === "FORM_TYPE" &&
f.value === "http://jabber.org/protocol/muc#request"
});
}