Skip to content

Instantly share code, notes, and snippets.

@legastero
Last active January 1, 2020 22:06
Show Gist options
  • Save legastero/af44f4bb332a7d85f102c40fc1215885 to your computer and use it in GitHub Desktop.
Save legastero/af44f4bb332a7d85f102c40fc1215885 to your computer and use it in GitHub Desktop.
diff --git a/src/Namespaces.ts b/src/Namespaces.ts
index fd2e0ca..127b285 100644
--- a/src/Namespaces.ts
+++ b/src/Namespaces.ts
@@ -373,6 +373,9 @@ export const NS_OMEMO_AXOLOTL = 'eu.siacs.conversations.axolotl';
export const NS_OMEMO_AXOLOTL_DEVICELIST = 'eu.siacs.conversations.axolotl.devicelist';
export const NS_OMEMO_AXOLOTL_BUNDLES = 'eu.siacs.conversations.axolotl.bundles';
+// XEP-????
+export const NS_UDT_0 = 'urn:xmpp:udt:0';
+
// istanbul ignore next
export const NS_OMEMO_AXOLOTL_BUNDLE = (deviceId: string) =>
`${NS_OMEMO_AXOLOTL_BUNDLES}:${deviceId}`;
diff --git a/src/plugins/index.ts b/src/plugins/index.ts
index 4cbc69b..8938377 100644
--- a/src/plugins/index.ts
+++ b/src/plugins/index.ts
@@ -18,6 +18,7 @@ export * from './pubsub';
export * from './roster';
export * from './sasl';
export * from './sharing';
+export * from './udt';
import Account from './account';
import Avatar from './avatar';
@@ -38,6 +39,8 @@ import Roster from './roster';
import SASL from './sasl';
import Sharing from './sharing';
+import UDT from './udt';
+
export function core(client: Agent) {
client.use(Features);
client.use(Disco);
@@ -61,4 +64,5 @@ export default function(client: Agent) {
client.use(PubSub);
client.use(Roster);
client.use(Sharing);
+ client.use(UDT);
}
diff --git a/src/plugins/udt.ts b/src/plugins/udt.ts
new file mode 100644
index 0000000..333b2a5
--- /dev/null
+++ b/src/plugins/udt.ts
@@ -0,0 +1,36 @@
+import { Agent } from '../';
+import * as JID from '../JID';
+import { NS_UDT_0 } from '../Namespaces';
+import { UDT, IQ } from '../protocol';
+
+declare module '../' {
+ export interface Agent {
+ sendJSON(jid: string, dataType: string, data: any): void;
+ }
+
+ export interface AgentEvents {
+ json: { from: string; dataType: string; json: any };
+ }
+}
+
+export default function(client: Agent) {
+ client.disco.addFeature(NS_UDT_0);
+
+ client.sendJSON = (jid: string, dataType: string, data: any) => {
+ client.sendMessage({
+ to: jid,
+ udt: [{ dataType, json: data }]
+ });
+ };
+
+ client.on('message', msg => {
+ if (msg.udt) {
+ for (const udt of msg.udt) {
+ client.emit('custom-data', {
+ from: msg.from,
+ ...udt
+ });
+ }
+ }
+ });
+}
diff --git a/src/protocol/index.ts b/src/protocol/index.ts
index 2c16820..155f8b7 100644
--- a/src/protocol/index.ts
+++ b/src/protocol/index.ts
@@ -81,6 +81,7 @@ export * from './xep0363';
export * from './xep0380';
export * from './xep0384';
export * from './xrd';
+export * from './xep_udt';
import RFC3921 from './rfc3921';
import RFC4287 from './rfc4287';
@@ -163,6 +164,7 @@ import XEP0363 from './xep0363';
import XEP0380 from './xep0380';
import XEP0384 from './xep0384';
import XRD from './xrd';
+import XEP_UDT from './xep_udt';
const Protocol: Array<DefinitionOptions | DefinitionOptions[]> = [
RFC3921,
@@ -245,7 +247,8 @@ const Protocol: Array<DefinitionOptions | DefinitionOptions[]> = [
XEP0363,
XEP0380,
XEP0384,
- XRD
+ XRD,
+ XEP_UDT
];
export default Protocol;
diff --git a/src/protocol/xep_udt.ts b/src/protocol/xep_udt.ts
new file mode 100644
index 0000000..9118067
--- /dev/null
+++ b/src/protocol/xep_udt.ts
@@ -0,0 +1,44 @@
+// ====================================================================
+// XEP-????: XEP-XXXX: User-defined Data Transfer
+// --------------------------------------------------------------------
+// Source: https://xmpp.org/extensions/xep-????.html
+// Version: ???
+// ====================================================================
+
+import {
+ childJSON,
+ DefinitionOptions,
+ extendMessage,
+ pubsubItemContentAliases,
+ textJSON,
+ attribute
+} from '../jxt';
+import { NS_UDT_0, NS_JSON_0 } from '../Namespaces';
+
+declare module './' {
+ export interface Message {
+ udt?: UDT[];
+ }
+
+ export interface IQ {
+ udt?: UDT;
+ }
+}
+
+export interface UDT {
+ dataType: string;
+ json: any;
+}
+
+const Protocol: DefinitionOptions[] = [
+ {
+ namespace: NS_UDT_0,
+ element: 'udt',
+ aliases: [{ path: 'message.udt', multiple: true }, { path: 'iq.udt' }],
+ fields: {
+ dataType: attribute('datatype'),
+ json: childJSON(NS_JSON_0, 'json')
+ }
+ }
+];
+export default Protocol;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment