forked from legastero/stanza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xep0004.ts
278 lines (257 loc) · 8.73 KB
/
xep0004.ts
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// ====================================================================
// XEP-0004: Data Forms
// --------------------------------------------------------------------
// Source: https://xmpp.org/extensions/xep-0004.html
// Version: 2.9 (2007-08-13)
//
// Additional:
// --------------------------------------------------------------------
// XEP-0122: Data Forms Validation
// --------------------------------------------------------------------
// Source: https://xmpp.org/extensions/xep-0122.html
// Version: 1.0.1 (2018-03-05)
// ====================================================================
import { DataFormFieldType, DataFormType } from '../Constants';
import { JID } from '../JID';
import {
attribute,
childAttribute,
childBoolean,
childEnum,
childIntegerAttribute,
childText,
DefinitionOptions,
multipleChildText,
splicePath,
TranslationContext,
XMLElement
} from '../jxt';
import { NS_DATAFORM, NS_DATAFORM_VALIDATION } from '../Namespaces';
declare module './' {
export interface Message {
forms?: DataForm[];
}
}
export interface DataForm {
type?: DataFormType;
title?: string;
instructions?: string | string[];
reported?: DataFormField[];
items?: DataFormItem[];
fields?: DataFormField[];
}
export interface DataFormItem {
fields: DataFormField[];
}
type DataFormFieldValueType = boolean | string | string[] | JID | JID[];
export interface DataFormFieldBase {
type?: DataFormFieldType;
name?: string;
label?: string;
description?: string;
required?: boolean;
rawValues?: string[];
validation?: DataFormValidation;
}
export interface DataFormFieldBoolean extends DataFormFieldBase {
type: 'boolean';
value?: boolean;
}
export interface DataFormFieldText extends DataFormFieldBase {
type:
| typeof DataFormFieldType.Fixed
| typeof DataFormFieldType.Hidden
| typeof DataFormFieldType.TextPrivate
| typeof DataFormFieldType.Text;
value?: string;
}
export interface DataFormFieldTextMulti extends DataFormFieldBase {
type: typeof DataFormFieldType.TextMultiple;
value?: string[];
}
export interface DataFormFieldList extends DataFormFieldBase {
type: typeof DataFormFieldType.List;
value?: string;
options?: Array<DataFormFieldOption<string>>;
}
export interface DataFormFieldListMulti extends DataFormFieldBase {
type: typeof DataFormFieldType.ListMultiple;
value?: string[];
options?: Array<DataFormFieldOption<string>>;
}
export interface DataFormFieldJID extends DataFormFieldBase {
type: typeof DataFormFieldType.JID;
value?: JID;
options?: Array<DataFormFieldOption<JID>>;
}
export interface DataFormFieldJIDMulti extends DataFormFieldBase {
type: typeof DataFormFieldType.JIDMultiple;
value?: JID[];
options?: Array<DataFormFieldOption<JID>>;
}
export interface DataFormFieldAny extends DataFormFieldBase {
type?: undefined;
value?: DataFormFieldValueType;
}
export interface DataFormFieldOption<T> {
label?: string;
value: T;
}
export type DataFormField =
| DataFormFieldBoolean
| DataFormFieldText
| DataFormFieldTextMulti
| DataFormFieldList
| DataFormFieldListMulti
| DataFormFieldJID
| DataFormFieldJIDMulti
| DataFormFieldAny;
export interface DataFormValidation {
type: string;
method: 'basic' | 'open' | 'range' | 'regex';
rangeMin?: string;
rangeMax?: string;
listMin?: number;
listMax?: number;
regex?: string;
}
const Protocol: DefinitionOptions[] = [
{
aliases: [{ path: 'message.forms', multiple: true }],
element: 'x',
fields: {
instructions: {
...multipleChildText(null, 'instructions'),
exportOrder: 2
},
reported: {
...splicePath(null, 'reported', 'dataformField', true),
exportOrder: 3
},
title: {
...childText(null, 'title'),
exportOrder: 1
},
type: attribute('type')
},
namespace: NS_DATAFORM,
optionalNamespaces: {
xdv: NS_DATAFORM_VALIDATION
},
path: 'dataform'
},
{
aliases: [
{ path: 'dataform.fields', multiple: true },
{ path: 'dataform.items.fields', multiple: true }
],
element: 'field',
fields: {
description: childText(null, 'desc'),
label: attribute('label'),
name: attribute('var'),
rawValues: {
...multipleChildText(null, 'value'),
exporter: () => null
},
required: childBoolean(null, 'required'),
type: attribute('type'),
value: {
importer(
xml: XMLElement,
context: TranslationContext
): DataFormFieldValueType | undefined {
const fieldType = xml.getAttribute('type') as DataFormFieldType;
const converter = multipleChildText(NS_DATAFORM, 'value');
const rawValues: string[] = converter.importer(xml, context)!;
const singleValue = rawValues[0];
switch (fieldType) {
case DataFormFieldType.TextMultiple:
case DataFormFieldType.ListMultiple:
case DataFormFieldType.JIDMultiple:
return rawValues;
case DataFormFieldType.Hidden:
case DataFormFieldType.Fixed:
if (rawValues.length === 1) {
return singleValue;
}
return rawValues;
case DataFormFieldType.Boolean:
if (singleValue) {
return singleValue === '1' || singleValue === 'true';
}
break;
default:
return singleValue;
}
},
exporter(
xml: XMLElement,
data: DataFormFieldValueType,
context: TranslationContext
): void {
const converter = multipleChildText(null, 'value');
let outputData: string[] = [];
const rawData =
context.data && context.data.rawValues
? context.data.rawValues[0]
: undefined;
if (typeof data === 'boolean') {
if (rawData === 'true' || rawData === 'false') {
outputData = [rawData];
} else {
outputData = [data ? '1' : '0'];
}
} else if (!Array.isArray(data)) {
outputData = [data.toString()];
} else {
for (const value of data) {
outputData.push(value.toString());
}
}
converter.exporter(
xml,
outputData,
Object.assign({}, context, {
namespace: NS_DATAFORM
})
);
}
}
},
namespace: NS_DATAFORM,
path: 'dataformField'
},
{
aliases: [{ path: 'dataform.fields.options', multiple: true }],
element: 'option',
fields: {
label: attribute('label'),
value: childText(null, 'value')
},
namespace: NS_DATAFORM
},
{
aliases: [{ path: 'dataform.items', multiple: true }],
element: 'item',
namespace: NS_DATAFORM
},
// ----------------------------------------------------------------
// XEP-0122: Data Forms Validation
// ----------------------------------------------------------------
{
element: 'validate',
fields: {
listMax: childIntegerAttribute(null, 'list-range', 'max'),
listMin: childIntegerAttribute(null, 'list-range', 'min'),
method: childEnum(null, ['basic', 'open', 'range', 'regex'], 'basic'),
rangeMax: childAttribute(null, 'range', 'max'),
rangeMin: childAttribute(null, 'range', 'min'),
regex: childText(null, 'regex'),
type: attribute('datatype', 'xs:string')
},
namespace: NS_DATAFORM_VALIDATION,
path: 'dataform.fields.validation'
}
];
export default Protocol;