Debian Codegraph @

xmpp-vala/src/module/stanza_error.vala

raw
1 using Gee;
2
3 namespace Xmpp {
4
5 private const string ERROR_NS_URI = "urn:ietf:params:xml:ns:xmpp-stanzas";
6
7 public class ErrorStanza {
8 public const string CONDITION_BAD_REQUEST = "bad-request";
9 public const string CONDITION_CONFLICT = "conflict";
10 public const string CONDITION_FEATURE_NOT_IMPLEMENTED = "feature-not-implemented";
11 public const string CONDITION_FORBIDDEN = "forbidden";
12 public const string CONDITION_GONE = "gone";
13 public const string CONDITION_INTERNAL_SERVER_ERROR = "internal-server-error";
14 public const string CONDITION_ITEM_NOT_FOUND = "item-not-found";
15 public const string CONDITION_JID_MALFORMED = "jid-malformed";
16 public const string CONDITION_NOT_ACCEPTABLE = "not-acceptable";
17 public const string CONDITION_NOT_ALLOWED = "not-allowed";
18 public const string CONDITION_NOT_AUTHORIZED = "not-authorized";
19 public const string CONDITION_POLICY_VIOLATION = "policy-violation";
20 public const string CONDITION_RECIPIENT_UNAVAILABLE = "recipient-unavailable";
21 public const string CONDITION_REDIRECT = "redirect";
22 public const string CONDITION_REGISTRATION_REQUIRED = "registration-required";
23 public const string CONDITION_REMOTE_SERVER_NOT_FOUND = "remote-server-not-found";
24 public const string CONDITION_REMOTE_SERVER_TIMEOUT = "remote-server-timeout";
25 public const string CONDITION_RESOURCE_CONSTRAINT = "resource-constraint";
26 public const string CONDITION_SERVICE_UNAVAILABLE = "service-unavailable";
27 public const string CONDITION_SUBSCRIPTION_REQUIRED = "subscription-required";
28 public const string CONDITION_UNDEFINED_CONDITION = "undefined-condition";
29 public const string CONDITION_UNEXPECTED_REQUEST = "unexpected-request";
30
31 public const string TYPE_AUTH = "auth";
32 public const string TYPE_CANCEL = "cancel";
33 public const string TYPE_CONTINUE = "continue";
34 public const string TYPE_MODIFY = "modify";
35 public const string TYPE_WAIT = "wait";
36
37 public string? by {
38 get { return error_node.get_attribute("by"); }
39 }
40
41 public string? text {
42 get { return error_node.get_deep_string_content(ERROR_NS_URI + ":text"); }
43 }
44
45 public string condition {
46 get {
47 Gee.List<StanzaNode> subnodes = error_node.sub_nodes;
48 foreach (StanzaNode subnode in subnodes) { // TODO get subnode by ns
49 if (subnode.ns_uri == ERROR_NS_URI) {
50 return subnode.name;
51 }
52 }
53 return CONDITION_UNDEFINED_CONDITION; // TODO hm!
54 }
55 }
56
57 public string type_ {
58 get { return error_node.get_attribute("type"); }
59 }
60
61 public StanzaNode error_node;
62
63 public static ErrorStanza? from_stanza(StanzaNode stanza) {
64 var error_stanza = new ErrorStanza();
65 error_stanza.error_node = stanza.get_subnode("error");
66 if (error_stanza.error_node == null) return null;
67 return error_stanza;
68 }
69
70 public ErrorStanza.build(string type, string condition, string? human_readable, StanzaNode? application_condition) {
71 error_node = new StanzaNode.build("error")
72 .put_attribute("type", type)
73 .put_node(new StanzaNode.build(condition, ERROR_NS_URI).add_self_xmlns());
74 if (application_condition != null) {
75 error_node.put_node(application_condition);
76 }
77 if (human_readable != null) {
78 error_node.put_node(new StanzaNode.build("text", ERROR_NS_URI)
79 .add_self_xmlns()
80 .put_attribute("xml:lang", "en")
81 .put_node(new StanzaNode.text(human_readable))
82 );
83 }
84 }
85 public ErrorStanza.bad_request(string? human_readable = null) {
86 this.build(TYPE_MODIFY, CONDITION_BAD_REQUEST, human_readable, null);
87 }
88 public ErrorStanza.feature_not_implemented(string? human_readable = null) {
89 this.build(TYPE_MODIFY, CONDITION_FEATURE_NOT_IMPLEMENTED, human_readable, null);
90 }
91 public ErrorStanza.item_not_found(StanzaNode? application_condition = null) {
92 this.build(TYPE_CANCEL, CONDITION_ITEM_NOT_FOUND, null, application_condition);
93 }
94 public ErrorStanza.not_acceptable(string? human_readable = null) {
95 this.build(TYPE_MODIFY, CONDITION_NOT_ACCEPTABLE, human_readable, null);
96 }
97 public ErrorStanza.not_allowed(string? human_readable = null) {
98 this.build(TYPE_CANCEL, CONDITION_NOT_ALLOWED, human_readable, null);
99 }
100 public ErrorStanza.resource_constraint(string? human_readable = null) {
101 this.build(TYPE_WAIT, CONDITION_RESOURCE_CONSTRAINT, human_readable, null);
102 }
103 public ErrorStanza.service_unavailable() {
104 this.build(TYPE_CANCEL, CONDITION_SERVICE_UNAVAILABLE, null, null);
105 }
106 }
107 }