Debian Codegraph @

xmpp-vala/src/module/xep/0085_chat_state_notifications.vala

raw
1 using Gee;
2
3 namespace Xmpp.Xep.ChatStateNotifications {
4 private const string NS_URI = "http://jabber.org/protocol/chatstates";
5
6 public const string STATE_ACTIVE = "active";
7 public const string STATE_INACTIVE = "inactive";
8 public const string STATE_GONE = "gone";
9 public const string STATE_COMPOSING = "composing";
10 public const string STATE_PAUSED = "paused";
11
12 private const string[] STATES = { STATE_ACTIVE, STATE_INACTIVE, STATE_GONE, STATE_COMPOSING, STATE_PAUSED };
13
14 public class Module : XmppStreamModule {
15 public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0085_chat_state_notifications");
16
17 public signal void chat_state_received(XmppStream stream, Jid jid, string state, MessageStanza stanza);
18
19 /**
20 * "A message stanza that does not contain standard messaging content [...] SHOULD be a state other than <active/>" (0085, 5.6)
21 */
22 public void send_state(XmppStream stream, Jid jid, string message_type, string state) {
23 MessageStanza message = new MessageStanza() { to=jid, type_=message_type };
24 add_state_to_message(message, state);
25
26 MessageProcessingHints.set_message_hint(message, MessageProcessingHints.HINT_NO_STORE);
27
28 stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, message);
29 }
30
31 public override void attach(XmppStream stream) {
32 stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI);
33 stream.get_module(MessageModule.IDENTITY).received_message.connect(on_received_message);
34 }
35
36 public override void detach(XmppStream stream) {
37 stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI);
38 stream.get_module(MessageModule.IDENTITY).received_message.disconnect(on_received_message);
39 }
40
41 public override string get_ns() { return NS_URI; }
42 public override string get_id() { return IDENTITY.id; }
43
44 private void on_received_message(XmppStream stream, MessageStanza message) {
45 if (!message.is_error()) {
46 Gee.List<StanzaNode> nodes = message.stanza.get_all_subnodes();
47 foreach (StanzaNode node in nodes) {
48 if (node.ns_uri == NS_URI && node.name in STATES) {
49 chat_state_received(stream, message.from, node.name, message);
50 }
51 }
52 }
53 }
54 }
55
56 public static void add_state_to_message(MessageStanza message, string state) {
57 message.stanza.put_node(new StanzaNode.build(state, NS_URI).add_self_xmlns());
58 }
59
60 }