| 1 |
using Gee; |
| 2 |
|
| 3 |
namespace Xmpp.Xep.Coin { |
| 4 |
private const string NS_RFC = "urn:ietf:params:xml:ns:conference-info"; |
| 5 |
|
| 6 |
public class Module : XmppStreamModule, Iq.Handler { |
| 7 |
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_RFC, "0298_coin"); |
| 8 |
|
| 9 |
public signal void coin_info_received(Jid jid, ConferenceInfo info); |
| 10 |
|
| 11 |
public async void on_iq_set(XmppStream stream, Iq.Stanza iq) { |
| 12 |
ConferenceInfo? info = parse_node(iq.stanza.get_subnode("conference-info", NS_RFC), null); |
| 13 |
if (info == null) return; |
| 14 |
|
| 15 |
coin_info_received(iq.from, info); |
| 16 |
} |
| 17 |
|
| 18 |
public override void attach(XmppStream stream) { |
| 19 |
stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_RFC, this); |
| 20 |
} |
| 21 |
|
| 22 |
public override void detach(XmppStream stream) { } |
| 23 |
|
| 24 |
public override string get_ns() { return NS_RFC; } |
| 25 |
|
| 26 |
public override string get_id() { return IDENTITY.id; } |
| 27 |
} |
| 28 |
|
| 29 |
public ConferenceInfo? parse_node(StanzaNode conference_node, ConferenceInfo? previous_conference_info) { |
| 30 |
string? version_str = conference_node.get_attribute("version"); |
| 31 |
string? conference_state = conference_node.get_attribute("state"); |
| 32 |
if (version_str == null || conference_state == null) return null; |
| 33 |
|
| 34 |
int version = int.parse(version_str); |
| 35 |
if (previous_conference_info != null && version <= previous_conference_info.version) return null; |
| 36 |
|
| 37 |
ConferenceInfo conference_info = previous_conference_info ?? new ConferenceInfo(); |
| 38 |
conference_info.version = version; |
| 39 |
|
| 40 |
Gee.List<StanzaNode> user_nodes = conference_node.get_deep_subnodes(NS_RFC + ":users", NS_RFC + ":user"); |
| 41 |
foreach (StanzaNode user_node in user_nodes) { |
| 42 |
string? jid_string = user_node.get_attribute("entity"); |
| 43 |
if (jid_string == null) continue; |
| 44 |
// if (!jid_string.has_prefix("xmpp:")) continue; // silk does this wrong |
| 45 |
Jid? jid = null; |
| 46 |
try { |
| 47 |
jid = new Jid(jid_string.substring(4)); |
| 48 |
} catch (Error e) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
string user_state = user_node.get_attribute("state"); |
| 52 |
if (conference_state == "full" && user_state != "full") return null; |
| 53 |
|
| 54 |
if (user_state == "deleted") { |
| 55 |
conference_info.users.unset(jid); |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
ConferenceUser user = new ConferenceUser(); |
| 60 |
user.jid = jid; |
| 61 |
user.display_text = user_node.get_deep_string_content(NS_RFC + ":display-text"); |
| 62 |
|
| 63 |
Gee.List<StanzaNode> endpoint_nodes = user_node.get_subnodes("endpoint"); |
| 64 |
foreach (StanzaNode entpoint_node in endpoint_nodes) { |
| 65 |
Gee.List<StanzaNode> media_nodes = entpoint_node.get_subnodes("media"); |
| 66 |
foreach (StanzaNode media_node in media_nodes) { |
| 67 |
string? id = media_node.get_attribute("id"); |
| 68 |
string? ty = media_node.get_deep_string_content(NS_RFC + ":type"); |
| 69 |
string? src_id_str = media_node.get_deep_string_content(NS_RFC + ":src-id"); |
| 70 |
|
| 71 |
if (id == null) continue; |
| 72 |
|
| 73 |
ConferenceMedia media = new ConferenceMedia(); |
| 74 |
media.id = id; |
| 75 |
media.src_id = src_id_str != null ? int.parse(src_id_str) : -1; |
| 76 |
media.ty = ty; |
| 77 |
user.medias[id] = media; |
| 78 |
} |
| 79 |
|
| 80 |
conference_info.users[user.jid] = user; |
| 81 |
} |
| 82 |
} |
| 83 |
return conference_info; |
| 84 |
} |
| 85 |
|
| 86 |
public class ConferenceInfo { |
| 87 |
public int version = -1; |
| 88 |
public HashMap<Jid, ConferenceUser> users = new HashMap<Jid, ConferenceUser>(Jid.hash_func, Jid.equals_func); |
| 89 |
|
| 90 |
public StanzaNode to_xml() { |
| 91 |
StanzaNode ret = new StanzaNode.build("conference-info", NS_RFC).add_self_xmlns() |
| 92 |
.put_attribute("version", this.version.to_string()) |
| 93 |
.put_attribute("state", "full"); |
| 94 |
StanzaNode users_node = new StanzaNode.build("users", NS_RFC); |
| 95 |
|
| 96 |
foreach (ConferenceUser user in this.users.values) { |
| 97 |
users_node.put_node(user.to_xml()); |
| 98 |
} |
| 99 |
ret.put_node(users_node); |
| 100 |
return ret; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public class ConferenceUser { |
| 105 |
public Jid jid; |
| 106 |
public string? display_text; |
| 107 |
public HashMap<string, ConferenceMedia> medias = new HashMap<string, ConferenceMedia>(); |
| 108 |
|
| 109 |
public StanzaNode to_xml() { |
| 110 |
StanzaNode user_node = new StanzaNode.build("user", NS_RFC) |
| 111 |
.put_attribute("entity", jid.to_string()); |
| 112 |
foreach (ConferenceMedia media in medias.values) { |
| 113 |
user_node.put_node(media.to_xml()); |
| 114 |
} |
| 115 |
return user_node; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
public class ConferenceMedia { |
| 120 |
public string id; |
| 121 |
public string? ty; |
| 122 |
public int src_id = -1; |
| 123 |
|
| 124 |
public StanzaNode to_xml() { |
| 125 |
StanzaNode media_node = new StanzaNode.build("media", NS_RFC) |
| 126 |
.put_attribute("id", id); |
| 127 |
if (ty != null) { |
| 128 |
media_node.put_node(new StanzaNode.build("type", NS_RFC).put_node(new StanzaNode.text(ty))); |
| 129 |
} |
| 130 |
if (src_id != -1) { |
| 131 |
media_node.put_node(new StanzaNode.build("src-id", NS_RFC).put_node(new StanzaNode.text(src_id.to_string()))); |
| 132 |
} |
| 133 |
return media_node; |
| 134 |
} |
| 135 |
} |
| 136 |
} |