Debian Codegraph @

xmpp-vala/src/module/stream_error.vala

raw
1 using Gee;
2
3 namespace Xmpp.StreamError {
4 private const string NS_URI = "jabber:client";
5 private const string NS_ERROR = "urn:ietf:params:xml:ns:xmpp-streams";
6
7 public class Module : XmppStreamModule {
8 public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "stream_error_module");
9
10 public override void attach(XmppStream stream) {
11 stream.received_nonza.connect(on_received_nonstanza);
12 }
13
14 public override void detach(XmppStream stream) {
15 stream.received_nonza.disconnect(on_received_nonstanza);
16 }
17
18 public static void require(XmppStream stream) {
19 if (stream.get_module(IDENTITY) == null) stream.add_module(new Module());
20 }
21
22 public override string get_ns() { return NS_URI; }
23 public override string get_id() { return IDENTITY.id; }
24
25 private void on_received_nonstanza(XmppStream stream, StanzaNode node) {
26 if (node.name == "error" && node.ns_uri == "http://etherx.jabber.org/streams") {
27 stream.add_flag(generate_error_flag(node));
28 }
29 }
30
31 private Flag generate_error_flag(StanzaNode node) {
32 string? subnode_name = null;
33 Gee.List<StanzaNode> subnodes = node.sub_nodes;
34 foreach (StanzaNode subnode in subnodes) { // TODO get subnode by ns
35 if (subnode.ns_uri == "urn:ietf:params:xml:ns:xmpp-streams" && subnode.name != "text") {
36 subnode_name = subnode.name;
37 }
38 }
39 Flag flag = new StreamError.Flag();
40 flag.error_type = subnode_name;
41 switch (subnode_name) {
42 case "bad-format":
43 case "conflict":
44 case "connection-timeout":
45 case "bad-namespace-prefix":
46 flag.reconnection_recomendation = StreamError.Flag.Reconnect.NOW;
47 break;
48 case "host-gone":
49 case "host-unknown":
50 flag.reconnection_recomendation = StreamError.Flag.Reconnect.LATER;
51 break;
52 case "improper-addressing":
53 case "internal-server-error":
54 case "invalid-from":
55 case "invalid-namespace":
56 case "invalid-xml":
57 case "not-authorized":
58 case "not-well-formed":
59 case "policy-violation":
60 case "remote-connection-failed":
61 case "reset":
62 flag.reconnection_recomendation = StreamError.Flag.Reconnect.NOW;
63 break;
64 case "resource-constraint":
65 flag.reconnection_recomendation = StreamError.Flag.Reconnect.LATER;
66 break;
67 case "restricted-xml":
68 flag.reconnection_recomendation = StreamError.Flag.Reconnect.NOW;
69 break;
70 case "see-other-host":
71 case "system-shutdown":
72 flag.reconnection_recomendation = StreamError.Flag.Reconnect.LATER;
73 break;
74 case "undefined-condition":
75 case "unsupported-encoding":
76 case "unsupported-feature":
77 case "unsupported-stanza-type":
78 case "unsupported-version":
79 flag.reconnection_recomendation = StreamError.Flag.Reconnect.NOW;
80 break;
81 }
82
83 if (subnode_name == "conflict") flag.resource_rejected = true;
84 return flag;
85 }
86 }
87
88 public class Flag : XmppStreamFlag {
89 public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "stream_error");
90
91 public enum Reconnect {
92 UNKNOWN,
93 NOW,
94 LATER,
95 NEVER
96 }
97
98 public string? error_type;
99 public Reconnect reconnection_recomendation = Reconnect.UNKNOWN;
100 public bool resource_rejected = false;
101
102 public override string get_ns() { return NS_URI; }
103 public override string get_id() { return IDENTITY.id; }
104 }
105 }