2017-03-02 14:37:32 +00:00
|
|
|
using Gee;
|
|
|
|
|
|
|
|
namespace Xmpp.Xep.ChatMarkers {
|
|
|
|
private const string NS_URI = "urn:xmpp:chat-markers:0";
|
|
|
|
|
|
|
|
public const string MARKER_RECEIVED = "received";
|
|
|
|
public const string MARKER_DISPLAYED = "displayed";
|
|
|
|
public const string MARKER_ACKNOWLEDGED = "acknowledged";
|
|
|
|
|
|
|
|
private const string[] MARKERS = {MARKER_RECEIVED, MARKER_DISPLAYED, MARKER_ACKNOWLEDGED};
|
|
|
|
|
|
|
|
public class Module : XmppStreamModule {
|
2017-03-19 11:55:36 +00:00
|
|
|
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0333_chat_markers");
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2020-04-28 21:37:01 +00:00
|
|
|
public signal void marker_received(XmppStream stream, Jid jid, string marker, string id, MessageStanza message);
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-11-21 22:27:27 +00:00
|
|
|
private SendPipelineListener send_pipeline_listener = new SendPipelineListener();
|
|
|
|
|
2018-01-12 20:03:09 +00:00
|
|
|
public void send_marker(XmppStream stream, Jid jid, string message_id, string type_, string marker) {
|
|
|
|
MessageStanza received_message = new MessageStanza();
|
2017-03-02 14:37:32 +00:00
|
|
|
received_message.to = jid;
|
|
|
|
received_message.type_ = type_;
|
|
|
|
received_message.stanza.put_node(new StanzaNode.build(marker, NS_URI).add_self_xmlns().put_attribute("id", message_id));
|
2020-03-05 11:21:43 +00:00
|
|
|
stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, received_message);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 20:03:09 +00:00
|
|
|
public static bool requests_marking(MessageStanza message) {
|
2017-03-02 14:37:32 +00:00
|
|
|
StanzaNode markable_node = message.stanza.get_subnode("markable", NS_URI);
|
|
|
|
return markable_node != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void attach(XmppStream stream) {
|
2017-03-11 00:40:42 +00:00
|
|
|
stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI);
|
2018-01-12 20:03:09 +00:00
|
|
|
stream.get_module(MessageModule.IDENTITY).send_pipeline.connect(send_pipeline_listener);
|
|
|
|
stream.get_module(MessageModule.IDENTITY).received_message.connect(on_received_message);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void detach(XmppStream stream) {
|
2020-04-21 14:25:21 +00:00
|
|
|
stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI);
|
2018-01-12 20:03:09 +00:00
|
|
|
stream.get_module(MessageModule.IDENTITY).send_pipeline.disconnect(send_pipeline_listener);
|
2020-04-21 14:25:21 +00:00
|
|
|
stream.get_module(MessageModule.IDENTITY).received_message.disconnect(on_received_message);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override string get_ns() { return NS_URI; }
|
2017-03-19 11:55:36 +00:00
|
|
|
public override string get_id() { return IDENTITY.id; }
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2018-01-12 20:03:09 +00:00
|
|
|
private void on_received_message(XmppStream stream, MessageStanza message) {
|
2017-05-30 20:17:41 +00:00
|
|
|
Gee.List<StanzaNode> nodes = message.stanza.get_all_subnodes();
|
2017-03-02 14:37:32 +00:00
|
|
|
foreach (StanzaNode node in nodes) {
|
|
|
|
if (node.ns_uri == NS_URI && node.name in MARKERS) {
|
2020-04-28 21:37:01 +00:00
|
|
|
string? to_stanza_id = node.get_attribute("id", NS_URI);
|
|
|
|
if (to_stanza_id != null) {
|
|
|
|
marker_received(stream, message.from, node.name, to_stanza_id, message);
|
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-11 20:29:13 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 20:03:09 +00:00
|
|
|
public class SendPipelineListener : StanzaListener<MessageStanza> {
|
2017-11-11 20:29:13 +00:00
|
|
|
|
2021-10-12 14:14:01 +00:00
|
|
|
private string[] after_actions_const = {};
|
2017-11-11 20:29:13 +00:00
|
|
|
|
|
|
|
public override string action_group { get { return "ADD_NODES"; } }
|
|
|
|
public override string[] after_actions { get { return after_actions_const; } }
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2018-01-19 21:37:02 +00:00
|
|
|
public override async bool run(XmppStream stream, MessageStanza message) {
|
2017-03-02 14:37:32 +00:00
|
|
|
StanzaNode? received_node = message.stanza.get_subnode("received", NS_URI);
|
2018-01-19 21:37:02 +00:00
|
|
|
if (received_node != null) return false;
|
|
|
|
if (message.body == null) return false;
|
|
|
|
if (message.type_ != MessageStanza.TYPE_CHAT) return false;
|
2017-03-02 14:37:32 +00:00
|
|
|
message.stanza.put_node(new StanzaNode.build("markable", NS_URI).add_self_xmlns());
|
2018-01-19 21:37:02 +00:00
|
|
|
return false;
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|