2017-03-02 14:37:32 +00:00
|
|
|
using Gee;
|
|
|
|
|
|
|
|
namespace Xmpp.Xep.ChatStateNotifications {
|
|
|
|
private const string NS_URI = "http://jabber.org/protocol/chatstates";
|
|
|
|
|
|
|
|
public const string STATE_ACTIVE = "active";
|
|
|
|
public const string STATE_INACTIVE = "inactive";
|
|
|
|
public const string STATE_GONE = "gone";
|
|
|
|
public const string STATE_COMPOSING = "composing";
|
|
|
|
public const string STATE_PAUSED = "paused";
|
|
|
|
|
2018-01-19 21:37:02 +00:00
|
|
|
private const string[] STATES = { STATE_ACTIVE, STATE_INACTIVE, STATE_GONE, STATE_COMPOSING, STATE_PAUSED };
|
2017-03-02 14:37:32 +00:00
|
|
|
|
|
|
|
public class Module : XmppStreamModule {
|
2017-03-19 11:55:36 +00:00
|
|
|
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0085_chat_state_notifications");
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2018-03-07 16:24:57 +00:00
|
|
|
public signal void chat_state_received(XmppStream stream, Jid jid, string state, MessageStanza stanza);
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-11-21 22:27:27 +00:00
|
|
|
private SendPipelineListener send_pipeline_listener = new SendPipelineListener();
|
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
/**
|
|
|
|
* "A message stanza that does not contain standard messaging content [...] SHOULD be a state other than <active/>" (0085, 5.6)
|
|
|
|
*/
|
2019-05-11 22:09:50 +00:00
|
|
|
public void send_state(XmppStream stream, Jid jid, string message_type, string state) {
|
|
|
|
MessageStanza message = new MessageStanza() { to=jid, type_=message_type };
|
2017-03-02 14:37:32 +00:00
|
|
|
message.stanza.put_node(new StanzaNode.build(state, NS_URI).add_self_xmlns());
|
2019-05-11 22:09:50 +00:00
|
|
|
|
|
|
|
MessageProcessingHints.set_message_hint(message, MessageProcessingHints.HINT_NO_STORE);
|
|
|
|
|
2020-03-05 11:21:43 +00:00
|
|
|
stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, message);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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).received_message.disconnect(on_received_message);
|
|
|
|
stream.get_module(MessageModule.IDENTITY).send_pipeline.disconnect(send_pipeline_listener);
|
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-03-02 14:37:32 +00:00
|
|
|
if (!message.is_error()) {
|
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) {
|
2019-12-23 20:53:45 +00:00
|
|
|
if (node.ns_uri == NS_URI && node.name in STATES) {
|
2018-03-07 16:24:57 +00:00
|
|
|
chat_state_received(stream, message.from, node.name, message);
|
2017-03-02 14:37:32 +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 = {"MODIFY_BODY"};
|
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; } }
|
|
|
|
|
2018-01-19 21:37:02 +00:00
|
|
|
public override async bool run(XmppStream stream, MessageStanza message) {
|
|
|
|
if (message.body == null) return false;
|
|
|
|
if (message.type_ != MessageStanza.TYPE_CHAT) return false;
|
2017-11-11 20:29:13 +00:00
|
|
|
message.stanza.put_node(new StanzaNode.build(STATE_ACTIVE, NS_URI).add_self_xmlns());
|
2018-01-19 21:37:02 +00:00
|
|
|
return false;
|
2017-11-11 20:29:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|