File: xdata.c

package info (click to toggle)
jabberd2 2.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 4,016 kB
  • sloc: ansic: 37,099; sh: 1,108; perl: 656; xml: 561; makefile: 511; python: 238; ruby: 145; sql: 55
file content (390 lines) | stat: -rw-r--r-- 12,666 bytes parent folder | download | duplicates (5)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * jabberd - Jabber Open Source Server
 * Copyright (c) 2002-2003 Jeremie Miller, Thomas Muldowney,
 *                         Ryan Eatmon, Robert Norris
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
 */

/* xdata, whee! */

#include "util.h"
#include <string.h>

/** creation */
xdata_t xdata_new(xdata_type_t type, const char *title, const char *instructions) {
    pool_t p;
    xdata_t xd;

    assert((int) type);

    p = pool_new();

    xd = pmalloco(p, sizeof(struct _xdata_st));

    xd->p = p;

    xd->type = type;

    if(title != NULL) xd->title = pstrdup(xd->p, title);
    if(instructions != NULL) xd->instructions = pstrdup(xd->p, instructions);

    log_debug(ZONE, "created new xd; title=%s, instructions=%s", title, instructions);

    return xd;
}

/** new field */
xdata_field_t xdata_field_new(xdata_t xd, xdata_field_type_t type, const char *var, const char *label, const char *desc, int required) {
    xdata_field_t xdf;

    assert((int) (xd != NULL));
    assert((int) type);
    assert((int) (var != NULL));

    xdf = pmalloco(xd->p, sizeof(struct _xdata_field_st));

    xdf->p = xd->p;

    xdf->type = type;

    xdf->var = pstrdup(xdf->p, var);

    if(label != NULL) xdf->label = pstrdup(xdf->p, label);
    if(desc != NULL) xdf->desc = pstrdup(xdf->p, desc);

    xdf->required = required;

    return xdf;
}

/** new item */
xdata_item_t xdata_item_new(xdata_t xd) {
    xdata_item_t xdi;

    assert((int) (xd != NULL));

    xdi = pmalloco(xd->p, sizeof(struct _xdata_item_st));

    xdi->p = xd->p;

    return xdi;
}

/** field insertion */
void xdata_add_field(xdata_t xd, xdata_field_t xdf) {
    assert((int) (xd != NULL));
    assert((int) (xdf != NULL));

    if(xd->fields == NULL)
        xd->fields = xd->flast = xdf;
    else {
        xd->flast->next = xdf;
        xd->flast = xdf;
    }
}

void xdata_add_rfield(xdata_t xd, xdata_field_t xdf) {
    assert((int) (xd != NULL));
    assert((int) (xdf != NULL));

    if(xd->rfields == NULL)
        xd->rfields = xd->rflast = xdf;
    else {
        xd->rflast->next = xdf;
        xd->rflast = xdf;
    }
}

void xdata_add_field_item(xdata_item_t xdi, xdata_field_t xdf) {
    assert((int) (xdi != NULL));
    assert((int) (xdf != NULL));

    if(xdi->fields == NULL)
        xdi->fields = xdi->flast = xdf;
    else {
        xdi->flast->next = xdf;
        xdi->flast = xdf;
    }
}

/** item insertion */
void xdata_add_item(xdata_t xd, xdata_item_t xdi) {
    assert((int) (xd != NULL));
    assert((int) (xdi != NULL));

    if(xd->items == NULL)
        xd->items = xd->ilast = xdi;
    else {
        xd->ilast->next = xdi;
        xd->ilast = xdi;
    }
}

/** option insertion */
static void xdata_option_new(xdata_field_t xdf, const char *value, int lvalue, const char *label, int llabel) {
    xdata_option_t xdo;

    assert((int) (xdf != NULL));
    assert((int) (value != NULL));

    xdo = pmalloco(xdf->p, sizeof(struct _xdata_option_st));

    xdo->p = xdf->p;

    if(lvalue <= 0) lvalue = strlen(value);
    xdo->value = pstrdupx(xdo->p, value, lvalue);

    if(label != NULL) {
        if(llabel <= 0) llabel = strlen(label);
        xdo->label = pstrdupx(xdo->p, label, llabel);
    }

    xdf->olast->next = xdo;
    xdf->olast = xdo;
    if(xdf->options == NULL) xdf->options = xdo;
}

/** value insertion */
void xdata_add_value(xdata_field_t xdf, const char *value, int vlen) {
    int first = 0;

    assert((int) (xdf != NULL));
    assert((int) (value != NULL));

    if(vlen <= 0) vlen = strlen(value);

    if(xdf->values == NULL)
        first = 1;

    xdf->values = (char **) realloc(xdf->values, sizeof(char *) * (xdf->nvalues + 1));
    xdf->values[xdf->nvalues] = pstrdupx(xdf->p, value, vlen);
    xdf->nvalues++;

    if(first)
        pool_cleanup(xdf->p, free, xdf->values);
}

/** rip out a field */
static xdata_field_t _xdata_field_parse(xdata_t xd, nad_t nad, int root) {
    xdata_field_t xdf;
    int attr, elem, eval;

    xdf = pmalloco(xd->p, sizeof(struct _xdata_field_st));

    xdf->p = xd->p;

    attr = nad_find_attr(nad, root, -1, "var", NULL);
    if(attr >= 0)
        xdf->var = pstrdupx(xdf->p, NAD_AVAL(nad, attr), NAD_AVAL_L(nad, attr));

    attr = nad_find_attr(nad, root, -1, "label", NULL);
    if(attr >= 0)
        xdf->label = pstrdupx(xdf->p, NAD_AVAL(nad, attr), NAD_AVAL_L(nad, attr));

    attr = nad_find_attr(nad, root, -1, "desc", NULL);
    if(attr >= 0)
        xdf->desc = pstrdupx(xdf->p, NAD_AVAL(nad, attr), NAD_AVAL_L(nad, attr));

    if(nad_find_elem(nad, root, NAD_ENS(nad, root), "required", 1) >= 0)
        xdf->required = 1;

    attr = nad_find_attr(nad, root, -1, "type", NULL);
    if(attr >= 0) {
        if(NAD_AVAL_L(nad, attr) == 7 && strncmp("boolean", NAD_AVAL(nad, attr), 7) == 0)
            xdf->type = xd_field_BOOLEAN;
        else if(NAD_AVAL_L(nad, attr) == 5 && strncmp("fixed", NAD_AVAL(nad, attr), 5) == 0)
            xdf->type = xd_field_FIXED;
        else if(NAD_AVAL_L(nad, attr) == 6 && strncmp("hidden", NAD_AVAL(nad, attr), 6) == 0)
            xdf->type = xd_field_HIDDEN;
        else if(NAD_AVAL_L(nad, attr) == 9 && strncmp("jid-multi", NAD_AVAL(nad, attr), 9) == 0)
            xdf->type = xd_field_JID_MULTI;
        else if(NAD_AVAL_L(nad, attr) == 10 && strncmp("jid-single", NAD_AVAL(nad, attr), 10) == 0)
            xdf->type = xd_field_JID_SINGLE;
        else if(NAD_AVAL_L(nad, attr) == 10 && strncmp("list-multi", NAD_AVAL(nad, attr), 10) == 0)
            xdf->type = xd_field_LIST_MULTI;
        else if(NAD_AVAL_L(nad, attr) == 11 && strncmp("list-single", NAD_AVAL(nad, attr), 11) == 0)
            xdf->type = xd_field_LIST_SINGLE;
        else if(NAD_AVAL_L(nad, attr) == 10 && strncmp("text-multi", NAD_AVAL(nad, attr), 10) == 0)
            xdf->type = xd_field_TEXT_MULTI;
        else if(NAD_AVAL_L(nad, attr) == 12 && strncmp("text-private", NAD_AVAL(nad, attr), 12) == 0)
            xdf->type = xd_field_TEXT_PRIVATE;
        else if(NAD_AVAL_L(nad, attr) == 11 && strncmp("text-single", NAD_AVAL(nad, attr), 11) == 0)
            xdf->type = xd_field_TEXT_SINGLE;
        else {
            log_debug(ZONE, "unknown field type '%.*s'", NAD_AVAL_L(nad, attr), NAD_AVAL(nad, attr));
            return NULL;
        }
    }

    elem = nad_find_elem(nad, root, NAD_ENS(nad, root), "value", 1);
    while(elem >= 0) {
        if(NAD_CDATA_L(nad, elem) <= 0) {
            log_debug(ZONE, "value element requires cdata");
            return NULL;
        }

        xdata_add_value(xdf, NAD_CDATA(nad, elem), NAD_CDATA_L(nad, elem));

        elem = nad_find_elem(nad, elem, NAD_ENS(nad, elem), "value", 0);
    }

    elem = nad_find_elem(nad, root, NAD_ENS(nad, root), "options", 1);
    while(elem >= 0) {
        eval = nad_find_elem(nad, elem, NAD_ENS(nad, elem), "value", 1);
        if(eval < 0) {
            log_debug(ZONE, "option requires value subelement");
            return NULL;
        }

        if(NAD_CDATA_L(nad, eval) <= 0) {
            log_debug(ZONE, "value element requires cdata");
            return NULL;
        }

        attr = nad_find_attr(nad, elem, -1, "label", NULL);
        if(attr < 0)
            xdata_option_new(xdf, NAD_CDATA(nad, eval), NAD_CDATA_L(nad, eval), NAD_AVAL(nad, eval), NAD_AVAL_L(nad, eval));
        else
            xdata_option_new(xdf, NAD_CDATA(nad, eval), NAD_CDATA_L(nad, eval), NULL, 0);

        elem = nad_find_elem(nad, elem, NAD_ENS(nad, elem), "options", 0);
    }

    return xdf;
}

/** parse a nad and build */
xdata_t xdata_parse(nad_t nad, int root) {
    xdata_t xd;
    int atype, elem, field;
    xdata_field_t xdf;

    assert((int) (nad != NULL));
    assert((int) (root >= 0));

    log_debug(ZONE, "building xd from nad");

    if(root >= nad->ecur || NAD_NURI_L(nad, NAD_ENS(nad, root)) != strlen(uri_XDATA) || strncmp(uri_XDATA, NAD_NURI(nad, NAD_ENS(nad, root)), NAD_NURI_L(nad, NAD_ENS(nad, root))) || NAD_ENAME_L(nad, root) != 1 || (NAD_ENAME(nad, root))[0] != 'x') {
        log_debug(ZONE, "elem %d does not exist, or is not {x:data}x", root);
        return NULL;
    }

    atype = nad_find_attr(nad, root, -1, "type", NULL);
    if(atype < 0) {
        log_debug(ZONE, "no type attribute");
        return NULL;
    }

    if(NAD_AVAL_L(nad, atype) == 4 && strncmp("form", NAD_AVAL(nad, atype), NAD_AVAL_L(nad, atype)) == 0)
        xd = xdata_new(xd_type_FORM, NULL, NULL);
    else if(NAD_AVAL_L(nad, atype) == 6 && strncmp("result", NAD_AVAL(nad, atype), NAD_AVAL_L(nad, atype)) == 0)
        xd = xdata_new(xd_type_RESULT, NULL, NULL);
    else if(NAD_AVAL_L(nad, atype) == 6 && strncmp("submit", NAD_AVAL(nad, atype), NAD_AVAL_L(nad, atype)) == 0)
        xd = xdata_new(xd_type_SUBMIT, NULL, NULL);
    else if(NAD_AVAL_L(nad, atype) == 6 && strncmp("cancel", NAD_AVAL(nad, atype), NAD_AVAL_L(nad, atype)) == 0)
        xd = xdata_new(xd_type_CANCEL, NULL, NULL);
    else {
        log_debug(ZONE, "unknown xd type %.*s", NAD_AVAL_L(nad, atype), NAD_AVAL(nad, atype));
        return NULL;
    }

    elem = nad_find_elem(nad, root, NAD_ENS(nad, root), "title", 1);
    if(elem < 0 || NAD_CDATA_L(nad, elem) <= 0) {
        log_debug(ZONE, "no cdata on x/title element");
        pool_free(xd->p);
        return NULL;
    }

    xd->title = pmalloco(xd->p, sizeof(char) * (NAD_CDATA_L(nad, elem) + 1));
    strncpy(xd->title, NAD_CDATA(nad, elem), NAD_CDATA_L(nad, elem));

    elem = nad_find_elem(nad, root, NAD_ENS(nad, root), "instructions", 1);
    if(elem < 0 || NAD_CDATA_L(nad, elem) <= 0) {
        log_debug(ZONE, "no cdata on x/instructions element");
        pool_free(xd->p);
        return NULL;
    }

    xd->instructions = pstrdupx(xd->p, NAD_CDATA(nad, elem), NAD_CDATA_L(nad, elem));

    switch(xd->type) {
        case xd_type_FORM:
        case xd_type_SUBMIT:
            /* form and submit just have fields, one level */
            field = nad_find_elem(nad, root, NAD_ENS(nad, root), "field", 1);
            while(field >= 0) {
                xdf = _xdata_field_parse(xd, nad, field);
                if(xdf == NULL) {
                    log_debug(ZONE, "field parse failed");
                    pool_free(xd->p);
                    return NULL;
                }

                xdata_add_field(xd, xdf);

                field = nad_find_elem(nad, field, NAD_ENS(nad, root), "field", 0);
            }

            break;

        case xd_type_RESULT:
            /* result has reported and item */
            elem = nad_find_elem(nad, root, NAD_ENS(nad, root), "reported", 1);
            if(elem >= 0) {
                field = nad_find_elem(nad, elem, NAD_ENS(nad, root), "field", 1);
                while(field >= 0) {
                    xdf = _xdata_field_parse(xd, nad, field);
                    if(xdf == NULL) {
                        log_debug(ZONE, "field parse failed");
                        pool_free(xd->p);
                        return NULL;
                    }

                    xdata_add_field(xd, xdf);

                    field = nad_find_elem(nad, field, NAD_ENS(nad, root), "field", 0);
                }
            }

            elem = nad_find_elem(nad, root, NAD_ENS(nad, root), "item", 1);
            if(elem >= 0) {
                field = nad_find_elem(nad, elem, NAD_ENS(nad, root), "field", 1);
                while(field >= 0) {
                    xdf = _xdata_field_parse(xd, nad, field);
                    if(xdf == NULL) {
                        log_debug(ZONE, "field parse failed");
                        pool_free(xd->p);
                        return NULL;
                    }

                    xdata_add_field(xd, xdf);

                    field = nad_find_elem(nad, field, NAD_ENS(nad, root), "field", 0);
                }
            }

            break;

        case xd_type_CANCEL:
            /* nothing to do with cancel, its all based on context */
            break;

        case xd_type_NONE:
            break;
    }

    return xd;
}