UNPKG

xmppjs-chat-bot

Version:
129 lines 3.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PresenceStanza = exports.IqStanza = exports.MessageStanza = exports.Stanza = void 0; const jid_1 = require("@xmpp/jid"); class Stanza { constructor(xml) { this.xml = xml; this.from = xml.attrs.from ? (0, jid_1.parse)(xml.attrs.from) : null; this.to = xml.attrs.to ? (0, jid_1.parse)(xml.attrs.to) : null; this.type = xml.attrs.type ?? null; } static parseIncoming(xml) { switch (xml.name) { case 'message': return new MessageStanza(xml); case 'iq': return new IqStanza(xml); case 'presence': return new PresenceStanza(xml); } return null; } toString() { return JSON.stringify(this.dump()); } dump() { return { stanzaType: this.stanzaType, from: this.from?.toString(), to: this.to?.toString(), xml: this.xml.toString() }; } } exports.Stanza = Stanza; class MessageStanza extends Stanza { constructor() { super(...arguments); this.stanzaType = 'message'; } isDelayed() { return !!this.xml.getChild('delay'); } uniqueAndStableStanzaID() { return this.xml.getChild('stanza-id')?.attrs.id ?? null; } occupantId() { return this.xml.getChild('occupant-id')?.attrs.id ?? null; } body() { return this.xml.getChild('body')?.text() ?? null; } isMentionned(jids) { if (!jids) { return false; } if (!Array.isArray(jids)) { jids = [jids]; } const jidsStrings = jids.map(jid => 'xmpp:' + jid.toString()); const references = this.xml.getChildren('reference'); for (const reference of references) { if (reference.attrs.type !== 'mention') { continue; } if (jidsStrings.includes(reference.attrs.uri)) { return true; } if (jidsStrings.includes(decodeURI(reference.attrs.uri))) { return true; } } return false; } } exports.MessageStanza = MessageStanza; class IqStanza extends Stanza { constructor() { super(...arguments); this.stanzaType = 'iq'; } } exports.IqStanza = IqStanza; class PresenceStanza extends Stanza { constructor(xml) { super(xml); this.stanzaType = 'presence'; this._IsNickNameChange = false; const xElems = this.xml.getChildren('x'); for (const x of xElems) { const statusElems = x.getChildren('status'); for (const status of statusElems) { if (status.attrs.code === '110') { this._isMe = true; } if (status.attrs.code === '303') { this._IsNickNameChange = true; } } const itemsElems = x.getChildren('item'); for (const item of itemsElems) { if (item.attrs.role) { this._Role = item.attrs.role; } if (item.attrs.affiliation) { this._Affiliation = item.attrs.affiliation; } if (this._IsNickNameChange && item.attrs.nick) { this._NewNickname = item.attrs.nick; } } } this._isMe ?? (this._isMe = false); } isMe() { return this._isMe; } role() { return this._Role; } affiliation() { return this._Affiliation; } isNickNameChange() { if (!this._IsNickNameChange || this._NewNickname === undefined) { return false; } return this._NewNickname; } } exports.PresenceStanza = PresenceStanza; //# sourceMappingURL=stanza.js.map