about summary refs log tree commit diff stats
path: root/src/xmpp/form.c
blob: e1bd4a938ac557568ab8213678d3a2565d53f105 (plain) (blame)
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
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
if __name__ == '__main__': from __init__ import init; init()

import unittest
import curses

from ranger.gui import ui

from test import Fake, OK, raise_ok

ui.curses = Fake()

class Test(unittest.TestCase):
	def setUp(self):

		self.fm = Fake()
		self.ui = ui.UI(env=Fake(), fm=self.fm)

		def fakesetup():
			self.ui.widget = Fake()
			self.ui.add_child(self.ui.widget)
		self.ui.setup = fakesetup

		self.ui.initialize()

	def tearDown(self):
		self.ui.destroy()
	
	def test_passing(self):
		# Test whether certain method calls are passed to widgets
		widget = self.ui.widget

		widget.draw = raise_ok
		self.assertRaises(OK, self.ui.draw)
		widget.__clear__()

		widget.finalize = raise_ok
		self.assertRaises(OK, self.ui.finalize)
		widget.__clear__()

		widget.press = raise_ok
		random_key = 123
		self.assertRaises(OK, self.ui.handle_key, random_key)
		widget.__clear__()

		widget.destroy = raise_ok
		self.assertRaises(OK, self.ui.destroy)
		widget.__clear__()

if __name__ == '__main__':
	unittest.main()
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
/*
 * form.c
 *
 * Copyright (C) 2012 - 2017 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 <https://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 "config.h"

#include <string.h>
#include <stdlib.h>

#ifdef HAVE_LIBMESODE
#include <mesode.h>
#endif

#ifdef HAVE_LIBSTROPHE
#include <strophe.h>
#endif

#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)
{
    const 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;
    }

    const 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;
    }

    const char *type = xmpp_stanza_get_type(stanza);
    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)
{
    const 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) {
        const 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) {
                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);
    }
}

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);
}

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;
}

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;
}

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;
}

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;
}

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;
}

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);
        }
    }
}

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);
        }
    }
}

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;
}

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;
}

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;
}

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;
}

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;
}

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;
}

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;
}

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);
    }
}