| 1 |
using Gee; |
| 2 |
namespace Xmpp.Xep.CallInvites { |
| 3 |
|
| 4 |
public const string NS_URI = "urn:xmpp:call-invites:0"; |
| 5 |
public const string NS_URI_CUSTOM = "urn:xmpp:call-message:1"; |
| 6 |
|
| 7 |
public class Module : XmppStreamModule { |
| 8 |
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "call_invites"); |
| 9 |
|
| 10 |
public signal void call_proposed(Jid from, Jid to, string call_id, bool video, Gee.List<StanzaNode> join_methods, MessageStanza message); |
| 11 |
public signal void call_retracted(Jid from, Jid to, string call_id, string message_type); |
| 12 |
public signal void call_accepted(Jid from, Jid to, string call_id, string message_type); |
| 13 |
public signal void call_rejected(Jid from, Jid to, string call_id, string message_type); |
| 14 |
public signal void call_left(Jid from, Jid to, string call_id, string message_type); |
| 15 |
|
| 16 |
public void send_jingle_propose(XmppStream stream, string call_id, Jid invitee, string sid, bool video, string message_type = MessageStanza.TYPE_CHAT) { |
| 17 |
StanzaNode method = new StanzaNode.build("jingle", CallInvites.NS_URI).put_attribute("sid", sid); |
| 18 |
StanzaNode legacy_method = new StanzaNode.build("jingle", CallInvites.NS_URI_CUSTOM).put_attribute("sid", sid); |
| 19 |
send_propose(stream, call_id, invitee, video, false, message_type, method, legacy_method); |
| 20 |
} |
| 21 |
|
| 22 |
public void send_muji_propose(XmppStream stream, string call_id, Jid invitee, Jid muc_jid, bool video, string message_type) { |
| 23 |
StanzaNode method = new StanzaNode.build("muji", Muji.NS_URI).add_self_xmlns().put_attribute("room", muc_jid.to_string()); |
| 24 |
send_propose(stream, call_id, invitee, video, true, message_type, method, method); |
| 25 |
} |
| 26 |
|
| 27 |
public void send_propose(XmppStream stream, string call_id, Jid invitee, bool video, bool multi, string message_type, StanzaNode method, StanzaNode? legacy_method = null) { |
| 28 |
MessageStanza invite_message = new MessageStanza() { to=invitee, type_=message_type }; |
| 29 |
invite_message.stanza.put_node( |
| 30 |
new StanzaNode.build("invite", NS_URI).add_self_xmlns() |
| 31 |
.put_attribute("id", call_id) |
| 32 |
.put_attribute("video", video.to_string()) |
| 33 |
.put_attribute("multi", multi.to_string()) |
| 34 |
.put_node(method) |
| 35 |
); |
| 36 |
if (legacy_method != null) { |
| 37 |
invite_message.stanza.put_node( // Custom legacy protocol |
| 38 |
new StanzaNode.build("propose", NS_URI_CUSTOM).add_self_xmlns() |
| 39 |
.put_attribute("id", call_id) |
| 40 |
.put_attribute("video", video.to_string()) |
| 41 |
.put_attribute("multi", multi.to_string()) |
| 42 |
.put_node(legacy_method) |
| 43 |
); |
| 44 |
} |
| 45 |
MessageProcessingHints.set_message_hint(invite_message, MessageProcessingHints.HINT_STORE); |
| 46 |
stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, invite_message); |
| 47 |
} |
| 48 |
|
| 49 |
public void send_jingle_accept(XmppStream stream, Jid inviter, string call_id, string sid, string message_type) { |
| 50 |
StanzaNode method = new StanzaNode.build("jingle", NS_URI).put_attribute("sid", sid); |
| 51 |
StanzaNode legacy_method = new StanzaNode.build("jingle", NS_URI_CUSTOM).put_attribute("sid", sid); |
| 52 |
send_accept(stream, inviter, call_id, message_type, method, legacy_method); |
| 53 |
} |
| 54 |
|
| 55 |
public void send_muji_accept(XmppStream stream, Jid inviter, string call_id, Jid room, string message_type) { |
| 56 |
StanzaNode method = new StanzaNode.build("muji", Muji.NS_URI).add_self_xmlns().put_attribute("room", room.to_string()); |
| 57 |
send_accept(stream, inviter, call_id, message_type, method, method); |
| 58 |
} |
| 59 |
|
| 60 |
public void send_accept(XmppStream stream, Jid inviter, string call_id, string message_type, StanzaNode method, StanzaNode? legacy_method = null) { |
| 61 |
MessageStanza invite_message = new MessageStanza() { to=inviter, type_=message_type }; |
| 62 |
invite_message.stanza.put_node( |
| 63 |
new StanzaNode.build("accept", NS_URI).add_self_xmlns() |
| 64 |
.put_attribute("id", call_id) |
| 65 |
.put_node(method) |
| 66 |
); |
| 67 |
if (legacy_method != null) { |
| 68 |
invite_message.stanza.put_node( |
| 69 |
new StanzaNode.build("accept", NS_URI_CUSTOM).add_self_xmlns() |
| 70 |
.put_attribute("id", call_id) |
| 71 |
.put_node(legacy_method) |
| 72 |
); |
| 73 |
} |
| 74 |
MessageProcessingHints.set_message_hint(invite_message, MessageProcessingHints.HINT_STORE); |
| 75 |
stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, invite_message); |
| 76 |
} |
| 77 |
|
| 78 |
public void send_retract(XmppStream stream, Jid to, string call_id, string message_type) { |
| 79 |
send_message(stream, to, call_id, "retract", message_type); |
| 80 |
} |
| 81 |
|
| 82 |
public void send_reject(XmppStream stream, Jid to, string call_id, string message_type) { |
| 83 |
send_message(stream, to, call_id, "reject", message_type); |
| 84 |
} |
| 85 |
|
| 86 |
public void send_left(XmppStream stream, Jid to, string call_id, string message_type) { |
| 87 |
MessageStanza message = new MessageStanza() { to=to, type_=message_type }; |
| 88 |
|
| 89 |
StanzaNode inner_node = new StanzaNode.build("left", NS_URI).add_self_xmlns().put_attribute("id", call_id); |
| 90 |
message.stanza.put_node(inner_node); |
| 91 |
|
| 92 |
// Custom legacy protocol |
| 93 |
StanzaNode custom_node = new StanzaNode.build("finish", NS_URI_CUSTOM).add_self_xmlns().put_attribute("id", call_id); |
| 94 |
message.stanza.put_node(custom_node); |
| 95 |
|
| 96 |
MessageProcessingHints.set_message_hint(message, MessageProcessingHints.HINT_STORE); |
| 97 |
stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, message); |
| 98 |
} |
| 99 |
|
| 100 |
private void send_message(XmppStream stream, Jid to, string call_id, string action, string message_type) { |
| 101 |
MessageStanza message = new MessageStanza() { to=to, type_=message_type }; |
| 102 |
|
| 103 |
StanzaNode inner_node = new StanzaNode.build(action, NS_URI).add_self_xmlns().put_attribute("id", call_id); |
| 104 |
message.stanza.put_node(inner_node); |
| 105 |
|
| 106 |
// Custom legacy protocol |
| 107 |
StanzaNode custom_node = new StanzaNode.build(action, NS_URI_CUSTOM).add_self_xmlns().put_attribute("id", call_id); |
| 108 |
message.stanza.put_node(custom_node); |
| 109 |
|
| 110 |
MessageProcessingHints.set_message_hint(message, MessageProcessingHints.HINT_STORE); |
| 111 |
stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, message); |
| 112 |
} |
| 113 |
|
| 114 |
private void on_received_message(XmppStream stream, MessageStanza message) { |
| 115 |
Xmpp.MessageArchiveManagement.MessageFlag? mam_flag = Xmpp.MessageArchiveManagement.MessageFlag.get_flag(message); |
| 116 |
if (mam_flag != null) return; |
| 117 |
|
| 118 |
StanzaNode? relevant_node = null; |
| 119 |
|
| 120 |
foreach (StanzaNode node in message.stanza.sub_nodes) { |
| 121 |
if (node.ns_uri == NS_URI) { |
| 122 |
relevant_node = node; |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
if (relevant_node == null) { |
| 127 |
foreach (StanzaNode node in message.stanza.sub_nodes) { |
| 128 |
if (node.ns_uri == NS_URI_CUSTOM) { |
| 129 |
relevant_node = node; |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
if (relevant_node == null) return; |
| 135 |
|
| 136 |
string? call_id = relevant_node.get_attribute("id"); |
| 137 |
if (call_id == null) return; |
| 138 |
|
| 139 |
if (relevant_node.name == "invite" || /* custom legacy */relevant_node.name == "propose") { |
| 140 |
if (relevant_node.sub_nodes.is_empty) return; |
| 141 |
|
| 142 |
// If there's also a JMI node, just use that one instead. |
| 143 |
foreach (StanzaNode node in message.stanza.sub_nodes) { |
| 144 |
if (node.ns_uri == JingleMessageInitiation.NS_URI) return; |
| 145 |
} |
| 146 |
|
| 147 |
bool video = relevant_node.get_attribute_bool("video", false); |
| 148 |
call_proposed(message.from, message.to, call_id, video, relevant_node.sub_nodes, message); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
switch (relevant_node.name) { |
| 153 |
case "accept": |
| 154 |
call_accepted(message.from, message.to, call_id, message.type_); |
| 155 |
break; |
| 156 |
case "retract": |
| 157 |
call_retracted(message.from, message.to, call_id, message.type_); |
| 158 |
break; |
| 159 |
case "reject": |
| 160 |
call_rejected(message.from, message.to, call_id, message.type_); |
| 161 |
break; |
| 162 |
case "finish": |
| 163 |
call_left(message.from, message.to, call_id, message.type_); |
| 164 |
break; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
public override void attach(XmppStream stream) { |
| 169 |
stream.get_module(MessageModule.IDENTITY).received_message.connect(on_received_message); |
| 170 |
} |
| 171 |
|
| 172 |
public override void detach(XmppStream stream) { |
| 173 |
stream.get_module(MessageModule.IDENTITY).received_message.disconnect(on_received_message); |
| 174 |
} |
| 175 |
|
| 176 |
public override string get_ns() { return NS_URI; } |
| 177 |
public override string get_id() { return IDENTITY.id; } |
| 178 |
} |
| 179 |
} |