Skip to content

Latest commit

 

History

History
105 lines (86 loc) · 3.36 KB

README.md

File metadata and controls

105 lines (86 loc) · 3.36 KB

xmppjs-client-plugins

Set of plugins for xmppjs

Usage:

OMEMO [OUTDATED - There has been some additions and namespace changes in the protocol]

import {
  client,
} from "@xmpp/client";
import {
  setupOMEMO,
} from 'xmppjs-client-plugins';


// Set up the plugin
const xmpp = client({service: 'wss://xmpp.example.com'});
const omemoPlugin = setupOMEMO(xmpp);

// Announce OMEMO support, and publish the current/latest devices list and each device's bundle info
OMEMOPlugin.announceOMEMOSupport(myDevicesArray, myBareJid)
OMEMOPlugin.announceBundleInfo(payload, deviceId, bareJid).then((res: any): any => {
}).catch((err: any): any => {
});

// Set listener
OMEMOPlugin.on('omemo.devicelist', (deviceList: Object) => {
});

// Subscribe to contact's devices list update If not already subscribed
OMEMOPlugin.subscribeToDeviceListUpdate(fromFullJid, toBareJid).then((stanza: Object): Object => {
  const child = stanza.getChild('pubsub');
  if (!child || stanza.attrs.type !== 'result') {
    return stanza;
  }
  const subscription = child.getChild('subscription');
  if (subscription && subscription.attrs.subscription) {
    // Cache subscription status
  }
  return stanza;
});

// Subscribe to contact's device's bundle update If not already subscribed
OMEMOPlugin.subscribeToBundleUpdate(fromFullJid, toBareJid, deviceId).then((stanza: Object): Object => {
  const child = stanza.getChild('pubsub');
  if (!child || stanza.attrs.type !== 'result') {
    return stanza;
  }
  const subscription = child.getChild('subscription');
  if (subscription && subscription.attrs.subscription && subscription.attrs.node) {
    // Cache subscription status
  }
  return stanza;
});

// Request for contact's devices list on demand
omemoPlugin.requestDeviceList(fromFullJid, toBareJid).then((deviceList: Object) => {
  // Cache devices
}).catch((err) => {
});

// Request for contact's device's bundle on demand
OMEMOPlugin.requestBundle(bareFromJid, bareToJid, deviceId).then((bundle: Object): any => {
  // Cache bundle
});

// Send encrypted message
const fromJid = "";
const toJid = "";
const chatType = "chat";
const encryptionPayload = {
  ciphertext: "",
  iv: "",
  keys: [{
    prekey: "",
    key: "",
    deviceId: "",
  }],
};
const messageId = "unique_messgae_id"; // set as `id` on <message> element
const sid = ""; // `sid` set on <header> element
const otherElements = [
  xml('active', {
    xmlns: "http://jabber.org/protocol/chatstates",
  })
]; // Any other supported xml elements, say chat state.

OMEMOPlugin.sendMessage(fromJid, toJid, chatType, encryptionPayload, messageId, sid, otherElements).then(() => {
});

// Message reception can be handled inside xmpp client's "on('stanza', async (stanza: any) => {})" listener as usual. Check for "encrypted" child element.