Debian Codegraph @

xmpp-vala/src/core/xmpp_stream.vala

raw
1 using Gee;
2
3 public abstract class Xmpp.XmppStream : Object {
4
5 public signal void received_node(XmppStream stream, StanzaNode node);
6 public signal void received_root_node(XmppStream stream, StanzaNode node);
7 public signal void received_features_node(XmppStream stream);
8 public signal void received_message_stanza(XmppStream stream, StanzaNode node);
9 public signal void received_presence_stanza(XmppStream stream, StanzaNode node);
10 public signal void received_iq_stanza(XmppStream stream, StanzaNode node);
11 public signal void received_nonza(XmppStream stream, StanzaNode node);
12 public signal void stream_negotiated(XmppStream stream);
13 public signal void attached_modules(XmppStream stream);
14
15 public const string NS_URI = "http://etherx.jabber.org/streams";
16
17 public Gee.List<XmppStreamFlag> flags { get; private set; default=new ArrayList<XmppStreamFlag>(); }
18 public Gee.List<XmppStreamModule> modules { get; private set; default=new ArrayList<XmppStreamModule>(); }
19
20 public StanzaNode? features { get; private set; default = new StanzaNode.build("features", NS_URI); }
21 public Jid remote_name;
22
23 public XmppLog log = new XmppLog();
24 public bool negotiation_complete { get; set; default=false; }
25 protected bool non_negotiation_modules_attached = false;
26 protected bool setup_needed = false;
27 protected bool disconnected = false;
28
29 protected XmppStream(Jid remote_name) {
30 this.remote_name = remote_name;
31 }
32
33 public abstract new async void connect() throws IOError;
34
35 public abstract new async void disconnect() throws IOError;
36
37 public abstract async StanzaNode read() throws IOError;
38
39 [Version (deprecated = true, deprecated_since = "0.1", replacement = "write_async")]
40 public abstract void write(StanzaNode node, int io_priority = Priority.DEFAULT);
41
42 public abstract async void write_async(StanzaNode node, int io_priority = Priority.DEFAULT, Cancellable? cancellable = null) throws IOError;
43
44 public abstract async void setup() throws IOError;
45
46 public void require_setup() {
47 setup_needed = true;
48 }
49
50 public bool is_setup_needed() {
51 return setup_needed;
52 }
53
54 public void add_flag(XmppStreamFlag flag) {
55 flags.add(flag);
56 }
57
58 public bool has_flag<T>(FlagIdentity<T>? identity) {
59 return get_flag(identity) != null;
60 }
61
62 public T? get_flag<T>(FlagIdentity<T>? identity) {
63 if (identity == null) return null;
64 foreach (var flag in flags) {
65 if (((!)identity).matches(flag)) return ((!)identity).cast(flag);
66 }
67 return null;
68 }
69
70 public void remove_flag(XmppStreamFlag flag) {
71 flags.remove(flag);
72 }
73
74 public XmppStream add_module(XmppStreamModule module) {
75 foreach (XmppStreamModule m in modules) {
76 if (m.get_ns() == module.get_ns() && m.get_id() == module.get_id()) {
77 warning("[%p] Adding already added module: %s\n", this, module.get_id());
78 return this;
79 }
80 }
81 modules.add(module);
82 if (negotiation_complete) module.attach(this);
83 return this;
84 }
85
86 public void detach_modules() {
87 foreach (XmppStreamModule module in modules) {
88 module.detach(this);
89 }
90 }
91
92 public T? get_module<T>(ModuleIdentity<T>? identity) {
93 if (identity == null) return null;
94 foreach (var module in modules) {
95 if (((!)identity).matches(module)) return ((!)identity).cast(module);
96 }
97 return null;
98 }
99
100 public async void loop() throws IOError {
101 while (true) {
102 if (setup_needed) {
103 yield setup();
104 }
105
106 StanzaNode node = yield read();
107
108 Idle.add(loop.callback);
109 yield;
110
111 if (disconnected) break;
112
113 yield handle_stanza(node);
114
115 if (!non_negotiation_modules_attached && negotiation_modules_done()) {
116 attach_non_negotation_modules();
117 non_negotiation_modules_attached = true;
118 if (!negotiation_complete) {
119 stream_negotiated(this);
120 negotiation_complete = true;
121 }
122 }
123 }
124 }
125
126 private async void handle_stanza(StanzaNode node) {
127 received_node(this, node);
128
129 if (node.ns_uri == NS_URI && node.name == "features") {
130 features = node;
131 received_features_node(this);
132 } else if (node.ns_uri == NS_URI && node.name == "stream" && node.pseudo) {
133 debug("[%p] Server closed stream", this);
134 try {
135 yield disconnect();
136 } catch (Error e) {}
137 return;
138 } else if (node.ns_uri == JABBER_URI) {
139 if (node.name == "message") {
140 received_message_stanza(this, node);
141 } else if (node.name == "presence") {
142 received_presence_stanza(this, node);
143 } else if (node.name == "iq") {
144 received_iq_stanza(this, node);
145 } else {
146 received_nonza(this, node);
147 }
148 } else {
149 received_nonza(this, node);
150 }
151 }
152
153 public bool is_negotiation_active() {
154 foreach (XmppStreamModule module in modules) {
155 if (module is XmppStreamNegotiationModule) {
156 XmppStreamNegotiationModule negotiation_module = (XmppStreamNegotiationModule) module;
157 if (negotiation_module.negotiation_active(this)) return true;
158 }
159 }
160 return false;
161 }
162
163 private bool negotiation_modules_done() throws IOError {
164 if (setup_needed) return false;
165 if (is_negotiation_active()) return false;
166
167 foreach (XmppStreamModule module in modules) {
168 if (module is XmppStreamNegotiationModule) {
169 XmppStreamNegotiationModule negotiation_module = (XmppStreamNegotiationModule) module;
170 if (negotiation_module.mandatory_outstanding(this)) {
171 throw new IOError.FAILED("mandatory-to-negotiate feature not negotiated: " + negotiation_module.get_id());
172 }
173 }
174 }
175 return true;
176 }
177
178 private void attach_non_negotation_modules() {
179 foreach (XmppStreamModule module in modules) {
180 if (module as XmppStreamNegotiationModule == null) {
181 module.attach(this);
182 }
183 }
184 attached_modules(this);
185 }
186
187 public void attach_negotation_modules() {
188 foreach (XmppStreamModule module in modules) {
189 if (module as XmppStreamNegotiationModule != null) {
190 module.attach(this);
191 }
192 }
193 }
194 }