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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
|
/*
* form.c
*
* Copyright (C) 2012 - 2014 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity 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 3 of the License, or
* (at your option) any later version.
*
* Profanity 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 Profanity. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
*/
#include <string.h>
#include <stdlib.h>
#include <strophe.h>
#include <glib.h>
#include "log.h"
#include "xmpp/xmpp.h"
#include "xmpp/stanza.h"
#include "xmpp/connection.h"
static gboolean
_is_valid_form_element(xmpp_stanza_t *stanza)
{
char *name = xmpp_stanza_get_name(stanza);
if (g_strcmp0(name, STANZA_NAME_X) != 0) {
log_error("Error parsing form, root element not <x/>.");
return FALSE;
}
char *ns = xmpp_stanza_get_ns(stanza);
if (g_strcmp0(ns, STANZA_NS_DATA) != 0) {
log_error("Error parsing form, namespace not %s.", STANZA_NS_DATA);
return FALSE;
}
char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE);
if ((g_strcmp0(type, "form") != 0) &&
(g_strcmp0(type, "submit") != 0) &&
(g_strcmp0(type, "cancel") != 0) &&
(g_strcmp0(type, "result") != 0)) {
log_error("Error parsing form, unknown type.");
return FALSE;
}
return TRUE;
}
static DataForm *
_form_new(void)
{
DataForm *form = malloc(sizeof(DataForm));
form->type = NULL;
form->title = NULL;
form->instructions = NULL;
form->fields = NULL;
form->var_to_tag = NULL;
form->tag_to_var = NULL;
form->tag_ac = NULL;
return form;
}
static FormField *
_field_new(void)
{
FormField *field = malloc(sizeof(FormField));
field->label = NULL;
field->type = NULL;
field->var = NULL;
field->description = NULL;
field->required = FALSE;
field->values = NULL;
field->options = NULL;
return field;
}
static char *
_get_property(xmpp_stanza_t * const stanza, const char * const property)
{
char *result = NULL;
xmpp_ctx_t *ctx = connection_get_ctx();
xmpp_stanza_t *child = xmpp_stanza_get_child_by_name(stanza, property);
if (child) {
char *child_text = xmpp_stanza_get_text(child);
if (child_text) {
result = strdup(child_text);
xmpp_free(ctx, child_text);
}
}
return result;
}
static char *
_get_attr(xmpp_stanza_t * const stanza, const char * const attr)
{
char *result = xmpp_stanza_get_attribute(stanza, attr);
if (result) {
return strdup(result);
} else {
return NULL;
}
}
static gboolean
_is_required(xmpp_stanza_t * const stanza)
{
xmpp_stanza_t *child = xmpp_stanza_get_child_by_name(stanza, "required");
if (child) {
return TRUE;
} else {
return FALSE;
}
}
static form_field_type_t
_get_field_type(const char * const type)
{
if (g_strcmp0(type, "hidden") == 0) {
return FIELD_HIDDEN;
}
if (g_strcmp0(type, "text-single") == 0) {
return FIELD_TEXT_SINGLE;
}
if (g_strcmp0(type, "text-private") == 0) {
return FIELD_TEXT_PRIVATE;
}
if (g_strcmp0(type, "text-multi") == 0) {
return FIELD_TEXT_MULTI;
}
if (g_strcmp0(type, "boolean") == 0) {
return FIELD_BOOLEAN;
}
if (g_strcmp0(type, "list-single") == 0) {
return FIELD_LIST_SINGLE;
}
if (g_strcmp0(type, "list-multi") == 0) {
return FIELD_LIST_MULTI;
}
if (g_strcmp0(type, "jid-single") == 0) {
return FIELD_JID_SINGLE;
}
if (g_strcmp0(type, "jid-multi") == 0) {
return FIELD_JID_MULTI;
}
if (g_strcmp0(type, "fixed") == 0) {
return FIELD_FIXED;
}
return FIELD_UNKNOWN;
}
DataForm *
form_create(xmpp_stanza_t * const form_stanza)
{
xmpp_ctx_t *ctx = connection_get_ctx();
if (!_is_valid_form_element(form_stanza)) {
return NULL;
}
DataForm *form = _form_new();
form->type = _get_attr(form_stanza, "type");
form->title = _get_property(form_stanza, "title");
form->instructions = _get_property(form_stanza, "instructions");
form->var_to_tag = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
form->tag_to_var = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
form->tag_ac = autocomplete_new();
form->modified = FALSE;
int tag_num = 1;
// get fields
xmpp_stanza_t *form_child = xmpp_stanza_get_children(form_stanza);
while (form_child) {
char *child_name = xmpp_stanza_get_name(form_child);
if (g_strcmp0(child_name, "field") == 0) {
xmpp_stanza_t *field_stanza = form_child;
FormField *field = _field_new();
field->label = _get_attr(field_stanza, "label");
field->type = _get_attr(field_stanza, "type");
field->type_t = _get_field_type(field->type);
field->value_ac = autocomplete_new();
field->var = _get_attr(field_stanza, "var");
if (field->type_t != FIELD_HIDDEN && field->var != NULL) {
GString *tag = g_string_new("");
g_string_printf(tag, "field%d", tag_num++);
g_hash_table_insert(form->var_to_tag, strdup(field->var), strdup(tag->str));
g_hash_table_insert(form->tag_to_var, strdup(tag->str), strdup(field->var));
autocomplete_add(form->tag_ac, tag->str);
g_string_free(tag, TRUE);
}
field->description = _get_property(field_stanza, "desc");
field->required = _is_required(field_stanza);
// handle repeated field children
xmpp_stanza_t *field_child = xmpp_stanza_get_children(field_stanza);
int value_index = 1;
while (field_child) {
child_name = xmpp_stanza_get_name(field_child);
// handle values
if (g_strcmp0(child_name, "value") == 0) {
char *value = xmpp_stanza_get_text(field_child);
if (value) {
field->values = g_slist_append(field->values, strdup(value));
if (field->type_t == FIELD_TEXT_MULTI) {
GString *ac_val = g_string_new("");
g_string_printf(ac_val, "val%d", value_index++);
autocomplete_add(field->value_ac, ac_val->str);
g_string_free(ac_val, TRUE);
}
if (field->type_t == FIELD_JID_MULTI) {
autocomplete_add(field->value_ac, value);
}
xmpp_free(ctx, value);
}
// handle options
} else if (g_strcmp0(child_name, "option") == 0) {
FormOption *option = malloc(sizeof(FormOption));
option->label = _get_attr(field_child, "label");
option->value = _get_property(field_child, "value");
if ((field->type_t == FIELD_LIST_SINGLE) || (field->type_t == FIELD_LIST_MULTI)) {
autocomplete_add(field->value_ac, option->value);
}
field->options = g_slist_append(field->options, option);
}
field_child = xmpp_stanza_get_next(field_child);
}
form->fields = g_slist_append(form->fields, field);
}
form_child = xmpp_stanza_get_next(form_child);
}
return form;
}
xmpp_stanza_t *
form_create_submission(DataForm *form)
{
xmpp_ctx_t *ctx = connection_get_ctx();
xmpp_stanza_t *x = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(x, STANZA_NAME_X);
xmpp_stanza_set_ns(x, STANZA_NS_DATA);
xmpp_stanza_set_type(x, "submit");
GSList *curr_field = form->fields;
while (curr_field) {
FormField *field = curr_field->data;
if (field->type_t != FIELD_FIXED) {
xmpp_stanza_t *field_stanza = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(field_stanza, "field");
xmpp_stanza_set_attribute(field_stanza, "var", field->var);
xmpp_stanza_t *value_stanza = NULL;
GSList *curr_value = NULL;
switch (field->type_t) {
case FIELD_HIDDEN:
case FIELD_TEXT_SINGLE:
case FIELD_TEXT_PRIVATE:
case FIELD_BOOLEAN:
case FIELD_LIST_SINGLE:
case FIELD_JID_SINGLE:
value_stanza = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(value_stanza, "value");
if (field->values) {
if (field->values->data) {
xmpp_stanza_t *text_stanza = xmpp_stanza_new(ctx);
xmpp_stanza_set_text(text_stanza, field->values->data);
xmpp_stanza_add_child(value_stanza, text_stanza);
xmpp_stanza_release(text_stanza);
}
}
xmpp_stanza_add_child(field_stanza, value_stanza);
xmpp_stanza_release(value_stanza);
break;
case FIELD_TEXT_MULTI:
case FIELD_LIST_MULTI:
case FIELD_JID_MULTI:
curr_value = field->values;
while (curr_value) {
char *value = curr_value->data;
value_stanza = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(value_stanza, "value");
if (value) {
xmpp_stanza_t *text_stanza = xmpp_stanza_new(ctx);
xmpp_stanza_set_text(text_stanza, value);
xmpp_stanza_add_child(value_stanza, text_stanza);
xmpp_stanza_release(text_stanza);
}
xmpp_stanza_add_child(field_stanza, value_stanza);
xmpp_stanza_release(value_stanza);
curr_value = g_slist_next(curr_value);
}
break;
case FIELD_FIXED:
default:
break;
}
xmpp_stanza_add_child(x, field_stanza);
xmpp_stanza_release(field_stanza);
}
curr_field = g_slist_next(curr_field);
}
return x;
}
static void
_free_option(FormOption *option)
{
if (option) {
free(option->label);
free(option->value);
free(option);
}
}
static void
_free_field(FormField *field)
{
if (field) {
free(field->label);
free(field->type);
free(field->var);
free(field->description);
g_slist_free_full(field->values, free);
g_slist_free_full(field->options, (GDestroyNotify)_free_option);
autocomplete_free(field->value_ac);
free(field);
}
}
static void
_form_destroy(DataForm *form)
{
if (form) {
free(form->type);
free(form->title);
free(form->instructions);
g_slist_free_full(form->fields, (GDestroyNotify)_free_field);
g_hash_table_destroy(form->var_to_tag);
g_hash_table_destroy(form->tag_to_var);
autocomplete_free(form->tag_ac);
free(form);
}
}
static int
_field_compare_by_var(FormField *a, FormField *b)
{
return g_strcmp0(a->var, b->var);
}
static GSList *
_form_get_non_form_type_fields_sorted(DataForm *form)
{
GSList *sorted = NULL;
GSList *curr = form->fields;
while (curr) {
FormField *field = curr->data;
if (g_strcmp0(field->var, "FORM_TYPE") != 0) {
sorted = g_slist_insert_sorted(sorted, field, (GCompareFunc)_field_compare_by_var);
}
curr = g_slist_next(curr);
}
return sorted;
}
static GSList *
_form_get_field_values_sorted(FormField *field)
{
GSList *sorted = NULL;
GSList *curr = field->values;
while (curr) {
char *value = curr->data;
if (value) {
sorted = g_slist_insert_sorted(sorted, value, (GCompareFunc)g_strcmp0);
}
curr = g_slist_next(curr);
}
return sorted;
}
static char *
_form_get_form_type_field(DataForm *form)
{
GSList *curr = form->fields;
while (curr) {
FormField *field = curr->data;
if (g_strcmp0(field->var, "FORM_TYPE") == 0) {
return field->values->data;
}
curr = g_slist_next(curr);
}
return NULL;
}
static gboolean
_form_tag_exists(DataForm *form, const char * const tag)
{
GList *tags = g_hash_table_get_keys(form->tag_to_var);
GList *curr = tags;
while (curr) {
if (g_strcmp0(curr->data, tag) == 0) {
return TRUE;
}
curr = g_list_next(curr);
}
g_list_free(tags);
return FALSE;
}
static form_field_type_t
_form_get_field_type(DataForm *form, const char * const tag)
{
char *var = g_hash_table_lookup(form->tag_to_var, tag);
if (var) {
GSList *curr = form->fields;
while (curr) {
FormField *field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
return field->type_t;
}
curr = g_slist_next(curr);
}
}
return FIELD_UNKNOWN;
}
static void
_form_set_value(DataForm *form, const char * const tag, char *value)
{
char *var = g_hash_table_lookup(form->tag_to_var, tag);
if (var) {
GSList *curr = form->fields;
while (curr) {
FormField *field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
if (g_slist_length(field->values) == 0) {
field->values = g_slist_append(field->values, strdup(value));
form->modified = TRUE;
return;
} else if (g_slist_length(field->values) == 1) {
free(field->values->data);
field->values->data = strdup(value);
form->modified = TRUE;
return;
}
}
curr = g_slist_next(curr);
}
}
}
static void
_form_add_value(DataForm *form, const char * const tag, char *value)
{
char *var = g_hash_table_lookup(form->tag_to_var, tag);
if (var) {
GSList *curr = form->fields;
while (curr) {
FormField *field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
field->values = g_slist_append(field->values, strdup(value));
if (field->type_t == FIELD_TEXT_MULTI) {
int total = g_slist_length(field->values);
GString *value_index = g_string_new("");
g_string_printf(value_index, "val%d", total);
autocomplete_add(field->value_ac, value_index->str);
g_string_free(value_index, TRUE);
}
form->modified = TRUE;
return;
}
curr = g_slist_next(curr);
}
}
}
static gboolean
_form_add_unique_value(DataForm *form, const char * const tag, char *value)
{
char *var = g_hash_table_lookup(form->tag_to_var, tag);
if (var) {
GSList *curr = form->fields;
while (curr) {
FormField *field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
GSList *curr_value = field->values;
while (curr_value) {
if (g_strcmp0(curr_value->data, value) == 0) {
return FALSE;
}
curr_value = g_slist_next(curr_value);
}
field->values = g_slist_append(field->values, strdup(value));
if (field->type_t == FIELD_JID_MULTI) {
autocomplete_add(field->value_ac, value);
}
form->modified = TRUE;
return TRUE;
}
curr = g_slist_next(curr);
}
}
return FALSE;
}
static gboolean
_form_remove_value(DataForm *form, const char * const tag, char *value)
{
char *var = g_hash_table_lookup(form->tag_to_var, tag);
if (var) {
GSList *curr = form->fields;
while (curr) {
FormField *field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
GSList *found = g_slist_find_custom(field->values, value, (GCompareFunc)g_strcmp0);
if (found) {
free(found->data);
found->data = NULL;
field->values = g_slist_delete_link(field->values, found);
if (field->type_t == FIELD_JID_MULTI) {
autocomplete_remove(field->value_ac, value);
}
form->modified = TRUE;
return TRUE;
} else {
return FALSE;
}
}
curr = g_slist_next(curr);
}
}
return FALSE;
}
static gboolean
_form_remove_text_multi_value(DataForm *form, const char * const tag, int index)
{
index--;
char *var = g_hash_table_lookup(form->tag_to_var, tag);
if (var) {
GSList *curr = form->fields;
while (curr) {
FormField *field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
GSList *item = g_slist_nth(field->values, index);
if (item) {
free(item->data);
item->data = NULL;
field->values = g_slist_delete_link(field->values, item);
GString *value_index = g_string_new("");
g_string_printf(value_index, "val%d", index+1);
autocomplete_remove(field->value_ac, value_index->str);
g_string_free(value_index, TRUE);
form->modified = TRUE;
return TRUE;
} else {
return FALSE;
}
}
curr = g_slist_next(curr);
}
}
return FALSE;
}
static int
_form_get_value_count(DataForm *form, const char * const tag)
{
char *var = g_hash_table_lookup(form->tag_to_var, tag);
if (var) {
GSList *curr = form->fields;
while (curr) {
FormField *field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
if ((g_slist_length(field->values) == 1) && (field->values->data == NULL)) {
return 0;
} else {
return g_slist_length(field->values);
}
}
curr = g_slist_next(curr);
}
}
return 0;
}
static gboolean
_form_field_contains_option(DataForm *form, const char * const tag, char *value)
{
char *var = g_hash_table_lookup(form->tag_to_var, tag);
if (var) {
GSList *curr = form->fields;
while (curr) {
FormField *field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
GSList *curr_option = field->options;
while (curr_option) {
FormOption *option = curr_option->data;
if (g_strcmp0(option->value, value) == 0) {
return TRUE;
}
curr_option = g_slist_next(curr_option);
}
}
curr = g_slist_next(curr);
}
}
return FALSE;
}
static FormField *
_form_get_field_by_tag(DataForm *form, const char * const tag)
{
char *var = g_hash_table_lookup(form->tag_to_var, tag);
if (var) {
GSList *curr = form->fields;
while (curr) {
FormField *field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
return field;
}
curr = g_slist_next(curr);
}
}
return NULL;
}
static Autocomplete
_form_get_value_ac(DataForm *form, const char * const tag)
{
char *var = g_hash_table_lookup(form->tag_to_var, tag);
if (var) {
GSList *curr = form->fields;
while (curr) {
FormField *field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
return field->value_ac;
}
curr = g_slist_next(curr);
}
}
return NULL;
}
static void
_form_reset_autocompleters(DataForm *form)
{
autocomplete_reset(form->tag_ac);
GSList *curr_field = form->fields;
while (curr_field) {
FormField *field = curr_field->data;
autocomplete_reset(field->value_ac);
curr_field = g_slist_next(curr_field);
}
}
void
form_init_module(void)
{
form_destroy = _form_destroy;
form_get_form_type_field = _form_get_form_type_field;
form_get_field_type = _form_get_field_type;
form_set_value = _form_set_value;
form_add_unique_value = _form_add_unique_value;
form_add_value = _form_add_value;
form_remove_value = _form_remove_value;
form_remove_text_multi_value = _form_remove_text_multi_value;
form_field_contains_option = _form_field_contains_option;
form_tag_exists = _form_tag_exists;
form_get_value_count = _form_get_value_count;
form_get_value_ac = _form_get_value_ac;
form_get_field_by_tag = _form_get_field_by_tag;
form_reset_autocompleters = _form_reset_autocompleters;
form_get_non_form_type_fields_sorted = _form_get_non_form_type_fields_sorted;
form_get_field_values_sorted = _form_get_field_values_sorted;
}
|