aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/xep/0077_in_band_registration.vala
blob: 87b654a8956f9daa539ac52d7d017fa384af4541 (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
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
using Gee;

namespace Xmpp.Xep.InBandRegistration {

public const string NS_URI = "jabber:iq:register";

public class Module : XmppStreamNegotiationModule {
    public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0077_in_band_registration");

    public async Form? get_from_server(XmppStream stream, Jid jid) {
        StanzaNode query_node = new StanzaNode.build("query", NS_URI).add_self_xmlns();
        Iq.Stanza request_form_iq = new Iq.Stanza.get(query_node) { to=jid };
        request_form_iq.to = jid;

        Iq.Stanza iq_result = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, request_form_iq);
        return new Form.from_node(stream, iq_result);
    }

    public async string? submit_to_server(XmppStream stream, Jid jid, Form form) {
        StanzaNode query_node = new StanzaNode.build("query", NS_URI).add_self_xmlns();
        query_node.put_node(form.get_submit_node());
        Iq.Stanza iq = new Iq.Stanza.set(query_node) { to=jid };

        Iq.Stanza iq_result = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq);
        if (iq_result.is_error()) {
            ErrorStanza? error_stanza = iq_result.get_error();
            return error_stanza.text ?? "Error";
        }

        return null;
    }

    public async ErrorStanza? change_password(XmppStream stream, Jid jid, string new_pw) {
        StanzaNode pw_change_node = new StanzaNode.build("query", NS_URI).add_self_xmlns();
        StanzaNode username_node = new StanzaNode.build("username", NS_URI);
        StanzaNode pw_node = new StanzaNode.build("password", NS_URI);
        username_node.put_node(new StanzaNode.text(jid.localpart));
        pw_node.put_node(new StanzaNode.text(new_pw));
        pw_change_node.put_node(username_node);
        pw_change_node.put_node(pw_node);
        Iq.Stanza set_password_iq = new Iq.Stanza.set(pw_change_node) { to=jid.bare_jid.domain_jid };

        Iq.Stanza chpw_result = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, set_password_iq);
        if (chpw_result.is_error()) {
            return chpw_result.get_error();
        }

        return null;
    }

    public override bool mandatory_outstanding(XmppStream stream) { return false; }

    public override bool negotiation_active(XmppStream stream) { return false; }

    public override void attach(XmppStream stream) { }

    public override void detach(XmppStream stream) { }

    public override string get_ns() { return NS_URI; }
    public override string get_id() { return IDENTITY.id; }
}

public class Form : DataForms.DataForm {
    public string? oob = null;

    internal Form.from_node(XmppStream stream, Iq.Stanza iq) {
        StanzaNode? x_node = iq.stanza.get_deep_subnode(NS_URI + ":query", DataForms.NS_URI + ":x");
        base.from_node(x_node ?? new StanzaNode.build("x", NS_URI).add_self_xmlns());

        oob = iq.stanza.get_deep_string_content(NS_URI + ":query", "jabber:x:oob:x", "url");
    }
}

}