From 3f531d6b91edab6c79fa232143db828bad13853c Mon Sep 17 00:00:00 2001 From: fiaxh Date: Sat, 11 Nov 2017 21:29:13 +0100 Subject: Read+(write) stream async --- xmpp-vala/src/core/stanza_reader.vala | 121 +++++++++++---------- xmpp-vala/src/core/stanza_writer.vala | 2 +- xmpp-vala/src/core/xmpp_stream.vala | 26 +++-- xmpp-vala/src/module/message/module.vala | 17 +-- xmpp-vala/src/module/util.vala | 59 ++++++++++ .../module/xep/0085_chat_state_notifications.vala | 23 ++-- .../module/xep/0184_message_delivery_receipts.vala | 26 +++-- .../src/module/xep/0203_delayed_delivery.vala | 18 ++- xmpp-vala/src/module/xep/0280_message_carbons.vala | 47 ++++---- .../xep/0313_message_archive_management.vala | 44 +++++--- xmpp-vala/src/module/xep/0333_chat_markers.vala | 13 ++- 11 files changed, 255 insertions(+), 141 deletions(-) (limited to 'xmpp-vala/src') diff --git a/xmpp-vala/src/core/stanza_reader.vala b/xmpp-vala/src/core/stanza_reader.vala index f4b900d1..4b4d98ab 100644 --- a/xmpp-vala/src/core/stanza_reader.vala +++ b/xmpp-vala/src/core/stanza_reader.vala @@ -44,12 +44,12 @@ public class StanzaReader { cancellable.cancel(); } - private void update_buffer() throws XmlError { + private async void update_buffer() throws XmlError { try { InputStream? input = this.input; if (input == null) throw new XmlError.EOF("No input stream specified and end of buffer reached."); if (cancellable.is_cancelled()) throw new XmlError.EOF("Input stream is canceled."); - buffer_fill = (int) ((!)input).read(buffer, cancellable); + buffer_fill = (int) yield ((!)input).read_async(buffer, GLib.Priority.DEFAULT, cancellable); if (buffer_fill == 0) throw new XmlError.EOF("End of input stream reached."); buffer_pos = 0; } catch (GLib.IOError e) { @@ -57,15 +57,16 @@ public class StanzaReader { } } - private char read_single() throws XmlError { + private async char read_single() throws XmlError { if (buffer_pos >= buffer_fill) { - update_buffer(); + yield update_buffer(); } - return (char) buffer[buffer_pos++]; + char c = (char) buffer[buffer_pos++]; + return c; } - private char peek_single() throws XmlError { - var res = read_single(); + private async char peek_single() throws XmlError { + var res = yield read_single(); buffer_pos--; return res; } @@ -78,53 +79,53 @@ public class StanzaReader { buffer_pos++; } - private void skip_until_non_ws() throws XmlError { - while (is_ws(peek_single())) { + private async void skip_until_non_ws() throws XmlError { + while (is_ws(yield peek_single())) { skip_single(); } } - private string read_until_ws() throws XmlError { + private async string read_until_ws() throws XmlError { var res = new StringBuilder(); - var what = peek_single(); + var what = yield peek_single(); while (!is_ws(what)) { - res.append_c(read_single()); - what = peek_single(); + res.append_c(yield read_single()); + what = yield peek_single(); } return res.str; } - private string read_until_char_or_ws(char x, char y = 0) throws XmlError { + private async string read_until_char_or_ws(char x, char y = 0) throws XmlError { var res = new StringBuilder(); - var what = peek_single(); + var what = yield peek_single(); while (what != x && what != y && !is_ws(what)) { - res.append_c(read_single()); - what = peek_single(); + res.append_c(yield read_single()); + what = yield peek_single(); } return res.str; } - private string read_until_char(char x) throws XmlError { + private async string read_until_char(char x) throws XmlError { var res = new StringBuilder(); - var what = peek_single(); + var what = yield peek_single(); while (what != x) { - res.append_c(read_single()); - what = peek_single(); + res.append_c(yield read_single()); + what = yield peek_single(); } return res.str; } - private StanzaAttribute read_attribute() throws XmlError { + private async StanzaAttribute read_attribute() throws XmlError { var res = new StanzaAttribute(); - res.name = read_until_char_or_ws('='); - if (read_single() == '=') { - var quot = peek_single(); + res.name = yield read_until_char_or_ws('='); + if ((yield read_single()) == '=') { + var quot = yield peek_single(); if (quot == '\'' || quot == '"') { skip_single(); - res.encoded_val = read_until_char(quot); + res.encoded_val = yield read_until_char(quot); skip_single(); } else { - res.encoded_val = read_until_ws(); + res.encoded_val = yield read_until_ws(); } } return res; @@ -161,17 +162,17 @@ public class StanzaReader { } } - public StanzaNode read_node_start() throws XmlError { + public async StanzaNode read_node_start() throws XmlError { var res = new StanzaNode(); res.attributes = new ArrayList(); var eof = false; - if (peek_single() == '<') skip_single(); - if (peek_single() == '?') res.pseudo = true; - if (peek_single() == '/') { + if ((yield peek_single()) == '<') skip_single(); + if ((yield peek_single()) == '?') res.pseudo = true; + if ((yield peek_single()) == '/') { eof = true; skip_single(); - res.name = read_until_char_or_ws('>'); - while (peek_single() != '>') { + res.name = yield read_until_char_or_ws('>'); + while ((yield peek_single()) != '>') { skip_single(); } skip_single(); @@ -180,13 +181,15 @@ public class StanzaReader { handle_stanza_ns(res); return res; } - res.name = read_until_char_or_ws('>', '/'); - skip_until_non_ws(); - while (peek_single() != '/' && peek_single() != '>' && peek_single() != '?') { - res.attributes.add(read_attribute()); - skip_until_non_ws(); + res.name = yield read_until_char_or_ws('>', '/'); + yield skip_until_non_ws(); + char next_char = yield peek_single(); + while (next_char != '/' && next_char != '>' && next_char != '?') { + res.attributes.add(yield read_attribute()); + yield skip_until_non_ws(); + next_char = yield peek_single(); } - if (read_single() == '/' || res.pseudo) { + if ((yield read_single()) == '/' || res.pseudo) { res.has_nodes = false; skip_single(); } else { @@ -196,20 +199,20 @@ public class StanzaReader { return res; } - public StanzaNode read_text_node() throws XmlError { + public async StanzaNode read_text_node() throws XmlError { var res = new StanzaNode(); res.name = "#text"; res.ns_uri = ns_state.current_ns_uri; - res.encoded_val = read_until_char('<').strip(); + res.encoded_val = (yield read_until_char('<')).strip(); return res; } - public StanzaNode read_root_node() throws XmlError { - skip_until_non_ws(); - if (peek_single() == '<') { - var res = read_node_start(); + public async StanzaNode read_root_node() throws XmlError { + yield skip_until_non_ws(); + if ((yield peek_single()) == '<') { + var res = yield read_node_start(); if (res.pseudo) { - return read_root_node(); + return yield read_root_node(); } return res; } else { @@ -217,18 +220,18 @@ public class StanzaReader { } } - public StanzaNode read_stanza_node() throws XmlError { + public async StanzaNode read_stanza_node() throws XmlError { ns_state = ns_state.push(); - var res = read_node_start(); + var res = yield read_node_start(); if (res.has_nodes) { bool finishNodeSeen = false; do { - skip_until_non_ws(); - if (peek_single() == '<') { + yield skip_until_non_ws(); + if ((yield peek_single()) == '<') { skip_single(); - if (peek_single() == '/') { + if ((yield peek_single()) == '/') { skip_single(); - string desc = read_until_char('>'); + string desc = yield read_until_char('>'); skip_single(); if (desc.contains(":")) { var split = desc.split(":"); @@ -240,10 +243,10 @@ public class StanzaReader { } finishNodeSeen = true; } else { - res.sub_nodes.add(read_stanza_node()); + res.sub_nodes.add(yield read_stanza_node()); } } else { - res.sub_nodes.add(read_text_node()); + res.sub_nodes.add(yield read_text_node()); } } while (!finishNodeSeen); if (res.sub_nodes.size == 0) res.has_nodes = false; @@ -252,12 +255,12 @@ public class StanzaReader { return res; } - public StanzaNode read_node() throws XmlError { - skip_until_non_ws(); - if (peek_single() == '<') { - return read_stanza_node(); + public async StanzaNode read_node() throws XmlError { + yield skip_until_non_ws(); + if ((yield peek_single()) == '<') { + return yield read_stanza_node(); } else { - return read_text_node(); + return yield read_text_node(); } } } diff --git a/xmpp-vala/src/core/stanza_writer.vala b/xmpp-vala/src/core/stanza_writer.vala index 26524d7b..e67920db 100644 --- a/xmpp-vala/src/core/stanza_writer.vala +++ b/xmpp-vala/src/core/stanza_writer.vala @@ -24,4 +24,4 @@ public class StanzaWriter { } } } -} \ No newline at end of file +} diff --git a/xmpp-vala/src/core/xmpp_stream.vala b/xmpp-vala/src/core/xmpp_stream.vala index 0a1f4120..fc4e7fd7 100644 --- a/xmpp-vala/src/core/xmpp_stream.vala +++ b/xmpp-vala/src/core/xmpp_stream.vala @@ -42,7 +42,7 @@ public class XmppStream { register_connection_provider(new StartTlsConnectionProvider()); } - public void connect(string? remote_name = null) throws IOStreamError { + public async void connect(string? remote_name = null) throws IOStreamError { if (remote_name != null) this.remote_name = (!)remote_name; attach_negotation_modules(); try { @@ -67,7 +67,7 @@ public class XmppStream { stderr.printf("CONNECTION LOST?\n"); throw new IOStreamError.CONNECT(e.message); } - loop(); + yield loop(); } public void disconnect() throws IOStreamError { @@ -96,11 +96,11 @@ public class XmppStream { return setup_needed; } - public StanzaNode read() throws IOStreamError { + public async StanzaNode read() throws IOStreamError { StanzaReader? reader = this.reader; if (reader == null) throw new IOStreamError.READ("trying to read, but no stream open"); try { - StanzaNode node = ((!)reader).read_node(); + StanzaNode node = yield ((!)reader).read_node(); log.node("IN", node); return node; } catch (XmlError e) { @@ -175,7 +175,7 @@ public class XmppStream { return false; } - private void setup() throws IOStreamError { + private async void setup() throws IOStreamError { StanzaNode outs = new StanzaNode.build("stream", "http://etherx.jabber.org/streams") .put_attribute("to", remote_name) .put_attribute("version", "1.0") @@ -184,17 +184,21 @@ public class XmppStream { outs.has_nodes = true; log.node("OUT ROOT", outs); write(outs); - received_root_node(this, read_root()); + received_root_node(this, yield read_root()); } - private void loop() throws IOStreamError { + private async void loop() throws IOStreamError { while (true) { if (setup_needed) { - setup(); + yield setup(); setup_needed = false; } - StanzaNode node = read(); + StanzaNode node = yield read(); + + Idle.add(loop.callback); + yield; + received_node(this, node); if (node.ns_uri == NS_URI && node.name == "features") { @@ -266,11 +270,11 @@ public class XmppStream { } } - private StanzaNode read_root() throws IOStreamError { + private async StanzaNode read_root() throws IOStreamError { StanzaReader? reader = this.reader; if (reader == null) throw new IOStreamError.READ("trying to read, but no stream open"); try { - StanzaNode node = ((!)reader).read_root_node(); + StanzaNode node = yield ((!)reader).read_root_node(); log.node("IN ROOT", node); return node; } catch (XmlError e) { diff --git a/xmpp-vala/src/module/message/module.vala b/xmpp-vala/src/module/message/module.vala index f7038ef8..2ca06dc4 100644 --- a/xmpp-vala/src/module/message/module.vala +++ b/xmpp-vala/src/module/message/module.vala @@ -8,24 +8,27 @@ namespace Xmpp.Message { public class Module : XmppStreamModule { public static ModuleIdentity IDENTITY = new ModuleIdentity(NS_URI, "message_module"); - public signal void pre_send_message(XmppStream stream, Message.Stanza message); + public StanzaListenerHolder received_pipeline = new StanzaListenerHolder(); + public StanzaListenerHolder send_pipeline = new StanzaListenerHolder(); + public signal void pre_received_message(XmppStream stream, Message.Stanza message); public signal void received_message(XmppStream stream, Message.Stanza message); public void send_message(XmppStream stream, Message.Stanza message) { - pre_send_message(stream, message); + send_pipeline.run.begin(stream, message); stream.write(message.stanza); } - public void received_message_stanza(XmppStream stream, StanzaNode node) { + public async void received_message_stanza_async(XmppStream stream, StanzaNode node) { Message.Stanza message = new Message.Stanza.from_stanza(node, stream.get_flag(Bind.Flag.IDENTITY).my_jid); - do { - message.rerun_parsing = false; - pre_received_message(stream, message); - } while(message.rerun_parsing); + yield received_pipeline.run(stream, message); received_message(stream, message); } + private void received_message_stanza(XmppStream stream, StanzaNode node) { + received_message_stanza_async.begin(stream, node); + } + public override void attach(XmppStream stream) { stream.received_message_stanza.connect(received_message_stanza); } diff --git a/xmpp-vala/src/module/util.vala b/xmpp-vala/src/module/util.vala index 365170b0..e6626049 100644 --- a/xmpp-vala/src/module/util.vala +++ b/xmpp-vala/src/module/util.vala @@ -1,3 +1,5 @@ +using Gee; + namespace Xmpp { public string get_bare_jid(string jid) { return jid.split("/")[0]; @@ -20,4 +22,61 @@ namespace Xmpp { uint32 b5_2 = Random.next_int(); return "%08x-%04x-%04x-%04x-%04x%08x".printf(b1, b2, b3, b4, b5_1, b5_2); } + +public abstract class StanzaListener : Object { + public abstract string action_group { get; } + public abstract string[] after_actions { get; } + public abstract async void run(Core.XmppStream stream, T stanza); +} + +public class StanzaListenerHolder : Object { + private Gee.List> listeners = new ArrayList>(); + + public new void connect(StanzaListener listener) { + listeners.add(listener); + resort_list(); + } + + public async void run(Core.XmppStream stream, T stanza) { + foreach (StanzaListener l in listeners) { + yield l.run(stream, stanza); + } + } + + private Gee.List> set_minus(Gee.List> main_set, Gee.List> minus) { + Gee.List> res = new ArrayList>(); + foreach (StanzaListener l in main_set) { + if (!minus.contains(l)) { + res.add(l); + } + } + return res; + } + + private bool set_contains_action(Gee.List> s, string[] actions) { + foreach(StanzaListener l in s) { + if (l.action_group in actions) { + return true; + } + } + return false; + } + + private void resort_list() { + Gee.List> new_list = new ArrayList>(); + while (listeners.size > new_list.size) { + bool changed = false; + foreach (StanzaListener l in listeners) { + Gee.List> remaining = set_minus(listeners, new_list); + if (!set_contains_action(remaining, l.after_actions)) { + new_list.add(l); + changed = true; + } + } + if (!changed) warning("Can't sort listeners"); + } + listeners = new_list; + } +} + } diff --git a/xmpp-vala/src/module/xep/0085_chat_state_notifications.vala b/xmpp-vala/src/module/xep/0085_chat_state_notifications.vala index 5de504a2..3ca97282 100644 --- a/xmpp-vala/src/module/xep/0085_chat_state_notifications.vala +++ b/xmpp-vala/src/module/xep/0085_chat_state_notifications.vala @@ -31,24 +31,17 @@ public class Module : XmppStreamModule { public override void attach(XmppStream stream) { stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); - stream.get_module(Message.Module.IDENTITY).pre_send_message.connect(on_pre_send_message); + stream.get_module(Message.Module.IDENTITY).send_pipeline.connect(new SendPipelineListener()); stream.get_module(Message.Module.IDENTITY).received_message.connect(on_received_message); } public override void detach(XmppStream stream) { - stream.get_module(Message.Module.IDENTITY).pre_send_message.disconnect(on_pre_send_message); stream.get_module(Message.Module.IDENTITY).received_message.disconnect(on_received_message); } public override string get_ns() { return NS_URI; } public override string get_id() { return IDENTITY.id; } - private void on_pre_send_message(XmppStream stream, Message.Stanza message) { - if (message.body == null) return; - if (message.type_ != Message.Stanza.TYPE_CHAT) return; - message.stanza.put_node(new StanzaNode.build(STATE_ACTIVE, NS_URI).add_self_xmlns()); - } - private void on_received_message(XmppStream stream, Message.Stanza message) { if (!message.is_error()) { Gee.List nodes = message.stanza.get_all_subnodes(); @@ -62,4 +55,18 @@ public class Module : XmppStreamModule { } } +public class SendPipelineListener : StanzaListener { + + private const string[] after_actions_const = {"MODIFY_BODY"}; + + public override string action_group { get { return "ADD_NODES"; } } + public override string[] after_actions { get { return after_actions_const; } } + + public override async void run(Core.XmppStream stream, Message.Stanza message) { + if (message.body == null) return; + if (message.type_ != Message.Stanza.TYPE_CHAT) return; + message.stanza.put_node(new StanzaNode.build(STATE_ACTIVE, NS_URI).add_self_xmlns()); + } +} + } diff --git a/xmpp-vala/src/module/xep/0184_message_delivery_receipts.vala b/xmpp-vala/src/module/xep/0184_message_delivery_receipts.vala index 71a864c3..c9cb2d40 100644 --- a/xmpp-vala/src/module/xep/0184_message_delivery_receipts.vala +++ b/xmpp-vala/src/module/xep/0184_message_delivery_receipts.vala @@ -22,12 +22,11 @@ namespace Xmpp.Xep.MessageDeliveryReceipts { public override void attach(XmppStream stream) { stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); stream.get_module(Message.Module.IDENTITY).received_message.connect(received_message); - stream.get_module(Message.Module.IDENTITY).pre_send_message.connect(pre_send_message); + stream.get_module(Message.Module.IDENTITY).send_pipeline.connect(new SendPipelineListener()); } public override void detach(XmppStream stream) { stream.get_module(Message.Module.IDENTITY).received_message.disconnect(received_message); - stream.get_module(Message.Module.IDENTITY).pre_send_message.disconnect(pre_send_message); } public override string get_ns() { return NS_URI; } @@ -39,13 +38,22 @@ namespace Xmpp.Xep.MessageDeliveryReceipts { receipt_received(stream, message.from, received_node.get_attribute("id", NS_URI)); } } + } - private void pre_send_message(XmppStream stream, Message.Stanza message) { - StanzaNode? received_node = message.stanza.get_subnode("received", NS_URI); - if (received_node != null) return; - if (message.body == null) return; - if (message.type_ == Message.Stanza.TYPE_GROUPCHAT) return; - message.stanza.put_node(new StanzaNode.build("request", NS_URI).add_self_xmlns()); - } +public class SendPipelineListener : StanzaListener { + + private const string[] after_actions_const = {}; + + public override string action_group { get { return "ADD_NODES"; } } + public override string[] after_actions { get { return after_actions_const; } } + + public override async void run(Core.XmppStream stream, Message.Stanza message) { + StanzaNode? received_node = message.stanza.get_subnode("received", NS_URI); + if (received_node != null) return; + if (message.body == null) return; + if (message.type_ == Message.Stanza.TYPE_GROUPCHAT) return; + message.stanza.put_node(new StanzaNode.build("request", NS_URI).add_self_xmlns()); } } + +} diff --git a/xmpp-vala/src/module/xep/0203_delayed_delivery.vala b/xmpp-vala/src/module/xep/0203_delayed_delivery.vala index 8ca300c9..89c761f2 100644 --- a/xmpp-vala/src/module/xep/0203_delayed_delivery.vala +++ b/xmpp-vala/src/module/xep/0203_delayed_delivery.vala @@ -27,19 +27,27 @@ namespace Xmpp.Xep.DelayedDelivery { } public override void attach(XmppStream stream) { - stream.get_module(Message.Module.IDENTITY).pre_received_message.connect(on_pre_received_message); + stream.get_module(Message.Module.IDENTITY).received_pipeline.connect(new ReceivedPipelineListener()); } public override void detach(XmppStream stream) { } public override string get_ns() { return NS_URI; } public override string get_id() { return IDENTITY.id; } + } - private void on_pre_received_message(XmppStream stream, Message.Stanza message) { - DateTime? datetime = get_time_for_message(message); - if (datetime != null) message.add_flag(new MessageFlag(datetime)); - } +public class ReceivedPipelineListener : StanzaListener { + + private const string[] after_actions_const = {}; + + public override string action_group { get { return "ADD_NODE"; } } + public override string[] after_actions { get { return after_actions_const; } } + + public override async void run(Core.XmppStream stream, Message.Stanza message) { + DateTime? datetime = Module.get_time_for_message(message); + if (datetime != null) message.add_flag(new MessageFlag(datetime)); } +} public class MessageFlag : Message.MessageFlag { public const string ID = "delayed_delivery"; diff --git a/xmpp-vala/src/module/xep/0280_message_carbons.vala b/xmpp-vala/src/module/xep/0280_message_carbons.vala index b2d21646..930c5234 100644 --- a/xmpp-vala/src/module/xep/0280_message_carbons.vala +++ b/xmpp-vala/src/module/xep/0280_message_carbons.vala @@ -18,43 +18,50 @@ namespace Xmpp.Xep.MessageCarbons { public override void attach(XmppStream stream) { stream.stream_negotiated.connect(enable); - stream.get_module(Message.Module.IDENTITY).pre_received_message.connect(pre_received_message); + stream.get_module(Message.Module.IDENTITY).received_pipeline.connect(new ReceivedPipelineListener()); stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); } public override void detach(XmppStream stream) { stream.stream_negotiated.disconnect(enable); - stream.get_module(Message.Module.IDENTITY).pre_received_message.disconnect(pre_received_message); } public override string get_ns() { return NS_URI; } public override string get_id() { return IDENTITY.id; } + } + +public class ReceivedPipelineListener : StanzaListener { + + private const string[] after_actions_const = {"EXTRACT_MESSAGE_1"}; - private void pre_received_message(XmppStream stream, Message.Stanza message) { - StanzaNode? received_node = message.stanza.get_subnode("received", NS_URI); - StanzaNode? sent_node = received_node == null ? message.stanza.get_subnode("sent", NS_URI) : null; - StanzaNode? carbons_node = received_node != null ? received_node : sent_node; - if (carbons_node != null) { - StanzaNode? forwarded_node = carbons_node.get_subnode("forwarded", "urn:xmpp:forward:0"); - if (forwarded_node != null) { - StanzaNode? message_node = forwarded_node.get_subnode("message", Message.NS_URI); - string? from_attribute = message_node.get_attribute("from", Message.NS_URI); - // Any forwarded copies received by a Carbons-enabled client MUST be from that user's bare JID; any copies that do not meet this requirement MUST be ignored. - if (from_attribute != null && from_attribute == get_bare_jid(stream.get_flag(Bind.Flag.IDENTITY).my_jid)) { - if (received_node != null) { - message.add_flag(new MessageFlag(MessageFlag.TYPE_RECEIVED)); - } else if (sent_node != null) { - message.add_flag(new MessageFlag(MessageFlag.TYPE_SENT)); - } - message.stanza = message_node; - message.rerun_parsing = true; + public override string action_group { get { return "EXTRACT_MESSAGE_2"; } } + public override string[] after_actions { get { return after_actions_const; } } + + public override async void run(Core.XmppStream stream, Message.Stanza message) { + StanzaNode? received_node = message.stanza.get_subnode("received", NS_URI); + StanzaNode? sent_node = received_node == null ? message.stanza.get_subnode("sent", NS_URI) : null; + StanzaNode? carbons_node = received_node != null ? received_node : sent_node; + if (carbons_node != null) { + StanzaNode? forwarded_node = carbons_node.get_subnode("forwarded", "urn:xmpp:forward:0"); + if (forwarded_node != null) { + StanzaNode? message_node = forwarded_node.get_subnode("message", Message.NS_URI); + string? from_attribute = message_node.get_attribute("from", Message.NS_URI); + // Any forwarded copies received by a Carbons-enabled client MUST be from that user's bare JID; any copies that do not meet this requirement MUST be ignored. + if (from_attribute != null && from_attribute == get_bare_jid(stream.get_flag(Bind.Flag.IDENTITY).my_jid)) { + if (received_node != null) { + message.add_flag(new MessageFlag(MessageFlag.TYPE_RECEIVED)); + } else if (sent_node != null) { + message.add_flag(new MessageFlag(MessageFlag.TYPE_SENT)); } message.stanza = message_node; message.rerun_parsing = true; } + message.stanza = message_node; + message.rerun_parsing = true; } } } +} public class MessageFlag : Message.MessageFlag { public const string ID = "message_carbons"; diff --git a/xmpp-vala/src/module/xep/0313_message_archive_management.vala b/xmpp-vala/src/module/xep/0313_message_archive_management.vala index 522f6dca..ac68e190 100644 --- a/xmpp-vala/src/module/xep/0313_message_archive_management.vala +++ b/xmpp-vala/src/module/xep/0313_message_archive_management.vala @@ -5,6 +5,10 @@ namespace Xmpp.Xep.MessageArchiveManagement { public const string NS_URI = "urn:xmpp:mam:2"; public const string NS_URI_1 = "urn:xmpp:mam:1"; +private static string NS_VER(XmppStream stream) { + return stream.get_flag(Flag.IDENTITY).ns_ver; +} + public class Module : XmppStreamModule { public static ModuleIdentity IDENTITY = new ModuleIdentity(NS_URI, "0313_message_archive_management"); @@ -38,7 +42,7 @@ public class Module : XmppStreamModule { } public override void attach(XmppStream stream) { - stream.get_module(Message.Module.IDENTITY).pre_received_message.connect(on_pre_received_message); + stream.get_module(Message.Module.IDENTITY).received_pipeline.connect(new ReceivedPipelineListener()); stream.stream_negotiated.connect(query_availability); } @@ -47,21 +51,6 @@ public class Module : XmppStreamModule { public override string get_ns() { return NS_URI; } public override string get_id() { return IDENTITY.id; } - private void on_pre_received_message(XmppStream stream, Message.Stanza message) { -// if (message.from != stream.remote_name) return; - if (stream.get_flag(Flag.IDENTITY) == null) return; - - StanzaNode? message_node = message.stanza.get_deep_subnode(NS_VER(stream) + ":result", "urn:xmpp:forward:0:forwarded", Message.NS_URI + ":message"); - if (message_node != null) { - StanzaNode? forward_node = message.stanza.get_deep_subnode(NS_VER(stream) + ":result", "urn:xmpp:forward:0:forwarded", DelayedDelivery.NS_URI + ":delay"); - DateTime? datetime = DelayedDelivery.Module.get_time_for_node(forward_node); - message.add_flag(new MessageFlag(datetime)); - - message.stanza = message_node; - message.rerun_parsing = true; - } - } - private static void page_through_results(XmppStream stream, Iq.Stanza iq) { string? last = iq.stanza.get_deep_string_content(NS_VER(stream) + ":fin", "http://jabber.org/protocol/rsm" + ":set", "last"); if (last == null) { @@ -89,9 +78,28 @@ public class Module : XmppStreamModule { if (stream.get_flag(Flag.IDENTITY) != null) feature_available(stream); }); } +} + +public class ReceivedPipelineListener : StanzaListener { + + private const string[] after_actions_const = {}; + + public override string action_group { get { return "EXTRACT_MESSAGE_1"; } } + public override string[] after_actions { get { return after_actions_const; } } - private static string NS_VER(XmppStream stream) { - return stream.get_flag(Flag.IDENTITY).ns_ver; + public override async void run(Core.XmppStream stream, Message.Stanza message) { + // if (message.from != stream.remote_name) return; + if (stream.get_flag(Flag.IDENTITY) == null) return; + + StanzaNode? message_node = message.stanza.get_deep_subnode(NS_VER(stream) + ":result", "urn:xmpp:forward:0:forwarded", Message.NS_URI + ":message"); + if (message_node != null) { + StanzaNode? forward_node = message.stanza.get_deep_subnode(NS_VER(stream) + ":result", "urn:xmpp:forward:0:forwarded", DelayedDelivery.NS_URI + ":delay"); + DateTime? datetime = DelayedDelivery.Module.get_time_for_node(forward_node); + message.add_flag(new MessageFlag(datetime)); + + message.stanza = message_node; + message.rerun_parsing = true; + } } } diff --git a/xmpp-vala/src/module/xep/0333_chat_markers.vala b/xmpp-vala/src/module/xep/0333_chat_markers.vala index a0e42510..9c3251dc 100644 --- a/xmpp-vala/src/module/xep/0333_chat_markers.vala +++ b/xmpp-vala/src/module/xep/0333_chat_markers.vala @@ -31,12 +31,11 @@ public class Module : XmppStreamModule { public override void attach(XmppStream stream) { stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); - stream.get_module(Message.Module.IDENTITY).pre_send_message.connect(on_pre_send_message); + stream.get_module(Message.Module.IDENTITY).send_pipeline.connect(new SendPipelineListener()); stream.get_module(Message.Module.IDENTITY).received_message.connect(on_received_message); } public override void detach(XmppStream stream) { - stream.get_module(Message.Module.IDENTITY).pre_send_message.disconnect(on_pre_send_message); stream.get_module(Message.Module.IDENTITY).received_message.disconnect(on_received_message); } @@ -52,8 +51,16 @@ public class Module : XmppStreamModule { } } } +} + +public class SendPipelineListener : StanzaListener { + + private const string[] after_actions_const = {}; + + public override string action_group { get { return "ADD_NODES"; } } + public override string[] after_actions { get { return after_actions_const; } } - private void on_pre_send_message(XmppStream stream, Message.Stanza message) { + public override async void run(Core.XmppStream stream, Message.Stanza message) { StanzaNode? received_node = message.stanza.get_subnode("received", NS_URI); if (received_node != null) return; if (message.body == null) return; -- cgit v1.2.3-70-g09d2