aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/stanza_error.vala
blob: a5c3956c50c1192c71f15d776a6e2dc41d6c26bc (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
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
using Gee;

namespace Xmpp {

    private const string ERROR_NS_URI = "urn:ietf:params:xml:ns:xmpp-stanzas";

    public class ErrorStanza {
        public const string CONDITION_BAD_REQUEST = "bad-request";
        public const string CONDITION_CONFLICT = "conflict";
        public const string CONDITION_FEATURE_NOT_IMPLEMENTED = "feature-not-implemented";
        public const string CONDITION_FORBIDDEN = "forbidden";
        public const string CONDITION_GONE = "gone";
        public const string CONDITION_INTERNAL_SERVER_ERROR = "internal-server-error";
        public const string CONDITION_ITEM_NOT_FOUND = "item-not-found";
        public const string CONDITION_JID_MALFORMED = "jid-malformed";
        public const string CONDITION_NOT_ACCEPTABLE = "not-acceptable";
        public const string CONDITION_NOT_ALLOWED = "not-allowed";
        public const string CONDITION_NOT_AUTHORIZED = "not-authorized";
        public const string CONDITION_POLICY_VIOLATION = "policy-violation";
        public const string CONDITION_RECIPIENT_UNAVAILABLE = "recipient-unavailable";
        public const string CONDITION_REDIRECT = "redirect";
        public const string CONDITION_REGISTRATION_REQUIRED = "registration-required";
        public const string CONDITION_REMOTE_SERVER_NOT_FOUND = "remote-server-not-found";
        public const string CONDITION_REMOTE_SERVER_TIMEOUT = "remote-server-timeout";
        public const string CONDITION_RESOURCE_CONSTRAINT = "resource-constraint";
        public const string CONDITION_SERVICE_UNAVAILABLE = "service-unavailable";
        public const string CONDITION_SUBSCRIPTION_REQUIRED = "subscription-required";
        public const string CONDITION_UNDEFINED_CONDITION = "undefined-condition";
        public const string CONDITION_UNEXPECTED_REQUEST = "unexpected-request";

        public const string TYPE_AUTH = "auth";
        public const string TYPE_CANCEL = "cancel";
        public const string TYPE_CONTINUE = "continue";
        public const string TYPE_MODIFY = "modify";
        public const string TYPE_WAIT = "wait";

        public string? by {
            get { return error_node.get_attribute("by"); }
        }

        public string? text {
            get { return error_node.get_deep_string_content(ERROR_NS_URI + ":text"); }
        }

        public string condition {
            get {
                Gee.List<StanzaNode> subnodes = error_node.sub_nodes;
                foreach (StanzaNode subnode in subnodes) { // TODO get subnode by ns
                    if (subnode.ns_uri == ERROR_NS_URI) {
                        return subnode.name;
                    }
                }
                return CONDITION_UNDEFINED_CONDITION; // TODO hm!
            }
        }

        public string type_ {
            get { return error_node.get_attribute("type"); }
        }

        public StanzaNode error_node;

        public ErrorStanza.from_stanza(StanzaNode stanza) {
            error_node = stanza.get_subnode("error");
        }

        public ErrorStanza.build(string type, string condition, string? human_readable, StanzaNode? application_condition) {
            error_node = new StanzaNode.build("error")
                .put_attribute("type", type)
                .put_node(new StanzaNode.build(condition, ERROR_NS_URI).add_self_xmlns());
            if (application_condition != null) {
                error_node.put_node(application_condition);
            }
            if (human_readable != null) {
                error_node.put_node(new StanzaNode.build("text", ERROR_NS_URI)
                    .add_self_xmlns()
                    .put_attribute("xml:lang", "en")
                    .put_node(new StanzaNode.text(human_readable))
                );
            }
        }
        public ErrorStanza.bad_request(string? human_readable = null) {
            this.build(TYPE_MODIFY, CONDITION_BAD_REQUEST, human_readable, null);
        }
        public ErrorStanza.feature_not_implemented(string? human_readable = null) {
            this.build(TYPE_MODIFY, CONDITION_FEATURE_NOT_IMPLEMENTED, human_readable, null);
        }
        public ErrorStanza.item_not_found(StanzaNode? application_condition = null) {
            this.build(TYPE_CANCEL, CONDITION_ITEM_NOT_FOUND, null, application_condition);
        }
        public ErrorStanza.not_acceptable(string? human_readable = null) {
            this.build(TYPE_MODIFY, CONDITION_NOT_ACCEPTABLE, human_readable, null);
        }
        public ErrorStanza.not_allowed(string? human_readable = null) {
            this.build(TYPE_CANCEL, CONDITION_NOT_ALLOWED, human_readable, null);
        }
        public ErrorStanza.resource_constraint(string? human_readable = null) {
            this.build(TYPE_WAIT, CONDITION_RESOURCE_CONSTRAINT, human_readable, null);
        }
        public ErrorStanza.service_unavailable() {
            this.build(TYPE_CANCEL, CONDITION_SERVICE_UNAVAILABLE, null, null);
        }
    }
}